home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2000 April / VPR0004A.BIN / OLS / SADKT100 / sadkt100.lzh / SrcFiles.lzh / CommandSink.cpp < prev    next >
C/C++ Source or Header  |  1999-12-31  |  20KB  |  876 lines

  1. // CommandSink.cpp : CCommandSink のインプリメンテーション
  2. #include "stdafx.h"
  3. #include "AD_Files.h"
  4. #include "CommandSink.h"
  5.  
  6. #include "FilesDlg.h"
  7. #include "GenralPage.h"
  8. #include "MySheet.h"
  9. #include "MFilePage.h"
  10. #include "UEEditPage.h"
  11.  
  12.  
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CCommandSink
  15.  
  16.  
  17. BOOL CCommandSink::SetObjectInfo(ISRAPL *pApl, DWORD dwCookie)
  18. {
  19.     HRESULT hr;
  20.  
  21.     ASSERT(pApl!=NULL && m_pApl==NULL);
  22.     m_pApl=pApl;
  23.  
  24.     ASSERT(dwCookie>0 && m_dwCookie==0);
  25.     m_dwCookie=dwCookie;
  26.  
  27.     hr = AtlAdvise(m_pApl, this->GetUnknown(), DIID__ISRAPLEvents, &m_dwAdvise);
  28.     if(FAILED(hr)){
  29.         m_pApl->Release();
  30.         m_pApl=NULL;
  31.         m_dwCookie=0;
  32.         MessageBox(NULL, "Advise Failed","", MB_OK);
  33.         return FALSE;
  34.     }
  35.  
  36.     m_dwFlag=AfxGetApp()->GetProfileInt(_T("Files"), _T("Flag"), (F_10|F_11|F_12|F_13));
  37.     m_dwFlag &=0x0000FFFF;
  38.     GetRegistryData(F_1|F_2|F_3|F_4);
  39.         
  40.     return TRUE;
  41. }
  42.  
  43.  
  44. void CCommandSink::Unadvise()
  45. {
  46.     if(m_pApl==NULL)
  47.         return;
  48.  
  49.     HRESULT hr=AtlUnadvise(m_pApl, DIID__ISRAPLEvents, m_dwAdvise);
  50.     if(FAILED(hr)){
  51.         MessageBox(NULL, "Unadvise Failed", "", MB_OK);
  52.     }
  53.     m_pApl->Release();
  54.     m_pApl=NULL;
  55. }
  56.  
  57.  
  58.  
  59.  
  60.  
  61. STDMETHODIMP CCommandSink::OnAddinCommand(long lCookie, long lInnerNumber)
  62. {
  63.     AFX_MANAGE_STATE(AfxGetStaticModuleState())
  64.  
  65.     // TODO: この位置にインプリメント用のコードを追加してください
  66.     if(m_dwCookie!=(unsigned long)lCookie)
  67.         return S_OK;
  68.  
  69.     switch(lInnerNumber){
  70.         case    1:
  71.             OnOpenMatchFile();
  72.             break;
  73.         case    2:
  74.             OnEnableEditDir(FALSE);
  75.             break;
  76.         case    3:
  77.             OnEnableEditDir(TRUE);
  78.             break;
  79.         case    4:
  80.             OnEnableEditExt(FALSE);
  81.             break;
  82.         case    5:
  83.             OnEnableEditExt(TRUE);
  84.             break;
  85.         case    6:
  86.             OnFileReadOnly(TRUE);
  87.             break;
  88.         case    7:
  89.             OnFileReadOnly(FALSE);
  90.             break;
  91.         case    8:
  92.             OnDirReadOnly(TRUE);
  93.             break;
  94.         case    9:
  95.             OnDirReadOnly(FALSE);
  96.             break;
  97.         case    10:
  98.             OnSetting();
  99.             break;
  100.  
  101.     }
  102.  
  103.     return S_OK;
  104. }
  105.  
  106. STDMETHODIMP CCommandSink::OnNewInstance()
  107. {
  108.     AFX_MANAGE_STATE(AfxGetStaticModuleState())
  109.  
  110.     // TODO: この位置にインプリメント用のコードを追加してください
  111.  
  112.     return S_OK;
  113. }
  114.  
  115. STDMETHODIMP CCommandSink::OnPreOpen()
  116. {
  117.     AFX_MANAGE_STATE(AfxGetStaticModuleState())
  118.  
  119.     // TODO: この位置にインプリメント用のコードを追加してください
  120.  
  121.     return S_OK;
  122. }
  123.  
  124.  
  125. STDMETHODIMP CCommandSink::OnPostOpen()
  126. {
  127.     AFX_MANAGE_STATE(AfxGetStaticModuleState())
  128.  
  129.     // OnPostSaveはファイル読込時の桁折り処理が別スレッドでつづいて
  130.     // いる時点で呼ばれます。
  131.     // 編集作業をする場合は、必ずISREditインターフェースのIsFormattingNow
  132.     // メソッドで、桁折り処理の終了を待つようにしてください。
  133.     // ただ、ここではユーザーの編集作業を禁止するため、桁折り処理の終了を
  134.     // またないで処理を行います。
  135.  
  136.     ISRFile *pIFile=NULL;
  137.     if(FAILED(m_pApl->CreateFileObject(&pIFile)) || pIFile==NULL){
  138.         MessageBox(NULL, _T("Failed to create ISRFile"), AfxGetAppName(), MB_OK);
  139.         return S_OK; 
  140.     }
  141.     CComBSTR bsSTR;
  142.     pIFile->GetFilePath(&bsSTR);
  143.     CString csFile=bsSTR;
  144.     pIFile->Release();
  145.  
  146.     list<CString>::iterator p=m_sltUEDir.begin();
  147.     for(; p!=m_sltUEDir.end(); p++){
  148.         if(!_tcsnicmp( (LPCTSTR)*p, (LPCTSTR)csFile, p->GetLength())){
  149.             SetEnableEdit(FALSE);
  150.             return S_OK;
  151.         }
  152.     }    
  153.     int nPos=csFile.ReverseFind(_T('.'));
  154.     if(nPos>0){
  155.         CString csExt=csFile.Mid(nPos+1);
  156.         p=m_sltUEExt.begin();
  157.         for(; p!=m_sltUEExt.end(); p++){
  158.             if(!csExt.CompareNoCase(*p)){
  159.                 SetEnableEdit(FALSE);
  160.                 return S_OK;
  161.             }
  162.         }
  163.     }
  164.  
  165.     return S_OK;
  166. }
  167.  
  168. STDMETHODIMP CCommandSink::OnPreSave()
  169. {
  170.     AFX_MANAGE_STATE(AfxGetStaticModuleState())
  171.  
  172.     // TODO: この位置にインプリメント用のコードを追加してください
  173.  
  174.     return S_OK;
  175. }
  176.  
  177.  
  178.  
  179. STDMETHODIMP CCommandSink::OnPostSave()
  180. {
  181.     AFX_MANAGE_STATE(AfxGetStaticModuleState())
  182.  
  183.     // TODO: この位置にインプリメント用のコードを追加してください
  184.  
  185.     return S_OK;
  186. }
  187.  
  188. STDMETHODIMP CCommandSink::OnCloseInstance()
  189. {
  190.     AFX_MANAGE_STATE(AfxGetStaticModuleState())
  191.  
  192.     // TODO: この位置にインプリメント用のコードを追加してください
  193.  
  194.     return S_OK;
  195. }
  196.  
  197.  
  198.  
  199.  
  200. STDMETHODIMP CCommandSink::OnBroadcastMessage(long lCookie, long lMessage)
  201. {
  202.     AFX_MANAGE_STATE(AfxGetStaticModuleState())
  203.  
  204.     // TODO: この位置にインプリメント用のコードを追加してください
  205.  
  206.     if(m_dwCookie!=(unsigned long)lCookie)
  207.         return S_OK;
  208.  
  209.     WORD wNum=HIWORD(lMessage);
  210.     WORD wFlag=LOWORD(lMessage);
  211.     if(wNum!=m_wUnique){
  212.         GetRegistryData(wFlag);
  213.     }
  214.  
  215.     return S_OK;
  216. }
  217.  
  218. ////////////////////////////////////////////////////////////////////////////////
  219. ////////////////////////////////////////////////////////////////////////////////
  220. ////////////////////////////////////////////////////////////////////////////////
  221.  
  222.  
  223. void CCommandSink::OnOpenMatchFile()
  224. {
  225.     ISRFile *pIFile=NULL;
  226.     if(FAILED(m_pApl->CreateFileObject(&pIFile)) || pIFile==NULL){
  227.         MessageBox(NULL, _T("Failed to create ISRFile"), AfxGetAppName(), MB_OK);
  228.         return; 
  229.     }
  230.     CComBSTR bsSTR;
  231.     pIFile->GetFilePath(&bsSTR);
  232.     CString csFile=bsSTR;
  233.  
  234.     CStrSet cSS;
  235.     CString csNewFile;
  236.     set<CStrSet>::iterator p;
  237.     int nPos;
  238.  
  239.     //-----  絶対パスを検索-----------------------------
  240.     cSS.m_csStrA=csFile;
  241.     p=m_sstFile.find(cSS);
  242.     if(p!=m_sstFile.end()){
  243.         DisplayMatchFile(csFile, *p, pIFile);
  244.         pIFile->Release();
  245.         return;
  246.     }
  247.  
  248.     //-------ファイル名を検索-------------------------
  249.     nPos=csFile.ReverseFind(_T('\\'));
  250.     if(nPos>=0) {
  251.         cSS.m_csStrA=csFile.Mid(nPos+1);
  252.         p=m_sstFile.find(cSS);
  253.         if(p!=m_sstFile.end()){
  254.             DisplayMatchFile(csFile, *p, pIFile);
  255.             pIFile->Release();
  256.             return;
  257.         }
  258.     }
  259.  
  260.     // --------拡張子を検索------------------------------
  261.     nPos=csFile.ReverseFind(_T('.'));
  262.     if(nPos>=0){
  263.         cSS.m_csStrA=csFile.Mid(nPos+1);
  264.         p=m_sstExt.find(cSS);
  265.         if(p!=m_sstExt.end()){
  266.             DisplayMatchFile(csFile, *p, pIFile);
  267.             pIFile->Release();
  268.             return;
  269.         }
  270.     }
  271.  
  272.     //-------------------------------------------------
  273.     
  274.     ISREdit *pIEdit=NULL;
  275.     if(FAILED(m_pApl->CreateEditObject(&pIEdit)) || pIEdit==NULL){
  276.         pIFile->Release();
  277.         return; 
  278.     }
  279.     bsSTR=_T("対応するファイル・拡張子は設定されていません。");
  280.     pIEdit->SetStatusBarText(bsSTR);
  281.     pIEdit->Release();
  282.     pIFile->Release();
  283.  
  284. }
  285.  
  286.  
  287. void FileExist(LPCTSTR lpFile, list<CString>& ltF)
  288. {
  289.  
  290.     WIN32_FIND_DATA fData;
  291.     CString csTmp;
  292.     HANDLE hdl=FindFirstFile(lpFile, &fData);
  293.     if(hdl!=INVALID_HANDLE_VALUE){
  294.         do {
  295.             if(!(fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)){
  296.                 csTmp=fData.cFileName;
  297.                 ltF.push_back(csTmp);
  298.             }
  299.         }while(FindNextFile(hdl, &fData));
  300.     }
  301.     FindClose(hdl);
  302. }
  303.  
  304.  
  305.  
  306.  
  307. void CCommandSink::DisplayMatchFile(CString& csFile,  CStrSet& cSS, ISRFile *pIFile)
  308. {
  309.     int nPos;
  310.     CString csDir=_T("");
  311.     CString csNewFile;
  312.     
  313.     if((cSS.m_dwMFlag & FLAG_MASK_A)==1){ // 同一ディレクトリ
  314.         nPos=csFile.ReverseFind(_T('\\'));
  315.         if(nPos<0)
  316.             csNewFile=csFile;
  317.         else {
  318.             csNewFile=csFile.Left(nPos+1);
  319.             csDir=csNewFile;
  320.         }
  321.         csNewFile +=cSS.m_csStrB;
  322.     }
  323.     else if((cSS.m_dwMFlag & FLAG_MASK_A)==2){ // 絶対パス
  324.         csNewFile=cSS.m_csStrB;
  325.     }
  326.     else {        // 拡張子
  327.         nPos=csFile.ReverseFind(_T('.'));
  328.         if(nPos<0)
  329.             csNewFile=csFile + _T(".");
  330.         else
  331.             csNewFile=csFile.Left(nPos+1);
  332.         csNewFile +=cSS.m_csStrB;
  333.     }
  334.  
  335.     CComBSTR bsSTR;
  336.     list<CString> ltF;
  337.     list<CString>::iterator p;
  338.     FileExist(csNewFile, ltF);
  339.  
  340.     if(ltF.size()==0){
  341.         ISREdit *pIEdit=NULL;
  342.         if(FAILED(m_pApl->CreateEditObject(&pIEdit)) || pIEdit==NULL){
  343.             return; 
  344.         }
  345.         bsSTR=_T("対応するファイルが見つかりません。");
  346.         pIEdit->SetStatusBarText(bsSTR);
  347.         pIEdit->Release();
  348.         return;
  349.     }
  350.     
  351.     if(csDir.IsEmpty()){
  352.         nPos=csFile.ReverseFind(_T('\\'));
  353.         if(nPos>=0)
  354.             csDir=csFile.Left(nPos+1);        
  355.     }
  356.     csNewFile=csDir;
  357.     if(ltF.size()>1){
  358.         FilesDlg dlg;
  359.         ltF.sort();
  360.         dlg.SetListPointer(<F);
  361.         if(IDCANCEL==dlg.DoModal())
  362.             return;
  363.         csNewFile +=dlg.m_csSel;
  364.     }
  365.     else {    
  366.         p=ltF.begin();
  367.         csNewFile += *p;    
  368.     }
  369.  
  370.     bsSTR=csNewFile;
  371.     long lhwnd;
  372.     VARIANT_BOOL bResult;
  373.     pIFile->IsAlreadyOpened(bsSTR, &lhwnd, &bResult);
  374.     if(bResult)
  375.         SetForegroundWindow((HWND)lhwnd);
  376.     else
  377.         pIFile->OpenFile(bsSTR, &bResult);
  378.  
  379. }
  380.  
  381.  
  382.  
  383. ///////////////////////////////////////////////////////////////////////////////
  384. ///////////////////////////////////////////////////////////////////////////////
  385. ///////////////////////////////////////////////////////////////////////////////
  386.  
  387.  
  388.  
  389. void CCommandSink::OnEnableEditDir(BOOL bEnable)
  390. {
  391.     CString csFile;
  392.     if(!GetFileName(csFile))
  393.         return;
  394.  
  395.     int nPos=csFile.ReverseFind(_T('\\'));
  396.     if(nPos<0) return;
  397.  
  398.     CString csDir=csFile.Left(nPos+1);
  399.     
  400.     SetEnableEdit(bEnable);
  401.     list<CString>::iterator p=m_sltUEDir.begin();
  402.     if(!bEnable) {
  403.         for( ; p!=m_sltUEDir.end(); p++){
  404.             if(!csDir.CompareNoCase(*p)){
  405.                 break;
  406.             }
  407.         }
  408.         if(p!=m_sltUEDir.end()) return;
  409.         m_sltUEDir.push_back(csDir);
  410.  
  411.     }
  412.     else {
  413.         for(; p!=m_sltUEDir.end(); p++) {
  414.             if(!_tcsnicmp( (LPCTSTR)*p, (LPCTSTR)csFile, p->GetLength())){
  415.                 break;
  416.             }
  417.         }
  418.         if(p==m_sltUEDir.end()) return;
  419.         m_sltUEDir.erase(p);
  420.     }
  421.     WriteRegistryData(F_1);
  422.  
  423. }
  424.  
  425. void CCommandSink::OnEnableEditExt(BOOL bEnable)
  426. {
  427.     CString csFile;
  428.     if(!GetFileName(csFile))
  429.         return;
  430.  
  431.     int nPos=csFile.ReverseFind(_T('.'));
  432.     if(nPos<0) return;
  433.  
  434.     CString csExt=csFile.Mid(nPos+1);
  435.     
  436.     SetEnableEdit(bEnable);
  437.     list<CString>::iterator p=m_sltUEExt.begin();
  438.     for( ; p!=m_sltUEExt.end(); p++){
  439.         if(!csExt.CompareNoCase(*p)){
  440.             break;
  441.         }
  442.     }
  443.     if(!bEnable) {
  444.         if(p!=m_sltUEExt.end()) return;
  445.         m_sltUEExt.push_back(csExt);
  446.     }    
  447.     else {
  448.         if(p==m_sltUEExt.end()) return;
  449.         m_sltUEExt.erase(p);
  450.     }
  451.     WriteRegistryData(F_2);
  452. }
  453.  
  454.  
  455. ///////////////////////////////////////////////////////////////////////////////
  456. ///////////////////////////////////////////////////////////////////////////////
  457. ///////////////////////////////////////////////////////////////////////////////
  458.  
  459. void CCommandSink::OnFileReadOnly(BOOL bReadOnly)
  460. {
  461.     CString csFile;
  462.     if(!GetFileName(csFile))
  463.         return;
  464.  
  465.     CString csMsg=_T("ファイルの属性変更に失敗しました。");
  466.     DWORD dwAttr=GetFileAttributes(csFile);
  467.     if(dwAttr==0xFFFFFFFF){
  468.         MessageBox(NULL, csMsg, AfxGetAppName(), MB_OK);
  469.         return;
  470.     }
  471.  
  472.     (bReadOnly) ? (dwAttr |=FILE_ATTRIBUTE_READONLY) : (dwAttr &=~FILE_ATTRIBUTE_READONLY);
  473.     
  474.     if(!SetFileAttributes(csFile, dwAttr)){
  475.         MessageBox(NULL, csMsg, AfxGetAppName(), MB_OK);
  476.         return;
  477.     }
  478.                 
  479.     SetEnableEdit(!bReadOnly);
  480.  
  481. }
  482.  
  483.  
  484. void ExecAttrChange( DWORD dwBit, BOOL bOn, LPCTSTR lpDir, int nSubDir)
  485. {
  486.     CString csFName=lpDir;
  487.     csFName +="\\*";
  488.     
  489.     WIN32_FIND_DATA fData;
  490.     HANDLE hdl=FindFirstFile(csFName, &fData);
  491.     if(hdl!=INVALID_HANDLE_VALUE){
  492.         do {
  493.             if(!strcmp(fData.cFileName, ".") || !strcmp(fData.cFileName, ".."))
  494.                 continue; 
  495.         
  496.             if(fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
  497.                 if(nSubDir==0){
  498.                     nSubDir=MessageBox(NULL, _T("サブディレクトリのファイルも処理しますか?"), AfxGetAppName(), MB_YESNOCANCEL);
  499.                     if(nSubDir==IDCANCEL){
  500.                         CloseHandle(hdl);
  501.                         return;
  502.                     }
  503.                 }
  504.                 if(nSubDir==IDYES) {
  505.                     CString csDir=lpDir;
  506.                     csDir +=_T("\\");
  507.                     csDir +=fData.cFileName;
  508.                     ExecAttrChange(dwBit, bOn, csDir, nSubDir);
  509.                 }
  510.             }
  511.             else {
  512.                 CString csFile=lpDir;    
  513.                 csFile +=_T("\\");
  514.                 csFile +=fData.cFileName;
  515.                 (bOn) ? (fData.dwFileAttributes |=dwBit) : (fData.dwFileAttributes &=~dwBit);
  516.                 SetFileAttributes(csFile, fData.dwFileAttributes);
  517.             }    
  518.         }while(FindNextFile(hdl,&fData));
  519.     }
  520.     CloseHandle(hdl);
  521. }
  522.  
  523.  
  524.  
  525. void CCommandSink::OnDirReadOnly(BOOL bReadOnly)
  526. {
  527.     CString csFile;
  528.     if(!GetFileName(csFile))
  529.         return;
  530.  
  531.     int nPos=csFile.ReverseFind(_T('\\'));
  532.     if(nPos<0) return;
  533.  
  534.     CString csDir=csFile.Left(nPos);
  535.     
  536.     CString csMsg;
  537.     if(!bReadOnly)
  538.         csMsg=_T("ディレクトリ内の全ファイルの書込み禁止属性を解除します。よろしいですか?");
  539.     else 
  540.         csMsg=_T("ディレクトリ内の全ファイルに書込み禁止属性を設定します。よろしいですか?");
  541.  
  542.     if(IDCANCEL==MessageBox(NULL, csMsg, AfxGetAppName(), MB_OK))
  543.         return;
  544.  
  545.  
  546.     SetEnableEdit(!bReadOnly);
  547.     ExecAttrChange(FILE_ATTRIBUTE_READONLY, bReadOnly, csDir, 0);
  548.     MessageBox(NULL, _T("処理をおわりました。"), AfxGetAppName(), MB_OK);
  549.  
  550. }
  551.  
  552. ///////////////////////////////////////////////////////////////////////////////
  553.  
  554.  
  555.  
  556. void CCommandSink::OnSetting()
  557. {
  558.     long lhMain;
  559.     m_pApl->GetMainWindowHandle(&lhMain);
  560.     if(lhMain==0) return;
  561.  
  562.     CWnd wndParent;
  563.     if(!wndParent.Attach((HWND)lhMain)){
  564.         TRACE("Parent Window Attach Failed");
  565.         return;
  566.     }
  567.  
  568.     CMySheet Sheet("AD_Files.Dllの設定", &wndParent);
  569.     
  570.     CGeneralPage gnPage;
  571.     CMFilePage    mfPageA(IDS_MFILES_A);
  572.     CMFilePage    mfPageB(IDS_MFILES_B);
  573.     CUEEditPage uePage;
  574.     
  575.     gnPage.SetSinkPointer(this);
  576.     mfPageA.SetPageType(1, this);
  577.     mfPageB.SetPageType(2, this);
  578.     uePage.SetSinkPointer(this);
  579.  
  580.     Sheet.AddPage(&gnPage);
  581.     Sheet.AddPage(&mfPageA);
  582.     Sheet.AddPage(&mfPageB);
  583.     Sheet.AddPage(&uePage);
  584.  
  585.     WORD wFlag=0;
  586.     if(Sheet.DoModal()==IDOK){
  587.         DWORD dwDiff=(m_dwFlag ^ gnPage.m_dwFlag);
  588.         if(dwDiff){
  589.             m_dwFlag=gnPage.m_dwFlag;
  590.             AfxGetApp()->WriteProfileInt(_T("Files"), _T("Flag"), (m_dwFlag & 0x0000FFFF));
  591.             MessageBox(NULL, _T("ツールバーに関する設定変更は次に起動するインスタンスから有効になります。"), 
  592.                 _T("AD_Filesに関する設定"), MB_OK);    
  593.         }
  594.         if(!IsSameStringList(&m_sltUEDir, &uePage.m_sltDir)){
  595.             wFlag |=F_1;
  596.             m_sltUEDir.clear();
  597.             copy(uePage.m_sltDir.begin(), uePage.m_sltDir.end(),
  598.                     inserter(m_sltUEDir, m_sltUEDir.begin()));
  599.         }
  600.         if(!IsSameStringList(&m_sltUEExt, &uePage.m_sltExt)){
  601.             wFlag |=F_2;
  602.             m_sltUEExt.clear();
  603.             copy(uePage.m_sltExt.begin(), uePage.m_sltExt.end(),
  604.                     inserter(m_sltUEExt, m_sltUEExt.begin()));
  605.         }
  606.         if(!IsSameSet(&m_sstExt, &mfPageA.m_sstST) ) {
  607.             wFlag |=F_3;
  608.             m_sstExt.clear();
  609.             copy(mfPageA.m_sstST.begin(), mfPageA.m_sstST.end(),
  610.                         inserter(m_sstExt, m_sstExt.begin()));
  611.         }
  612.         if(!IsSameSet(&m_sstFile, &mfPageB.m_sstST) ) {
  613.             wFlag |=F_4;
  614.             m_sstFile.clear();
  615.             copy(mfPageB.m_sstST.begin(), mfPageB.m_sstST.end(),
  616.                         inserter(m_sstFile, m_sstFile.begin()));
  617.  
  618.         }
  619.     }
  620.     wndParent.Detach();
  621.  
  622.     if(wFlag==0) return;
  623.  
  624.     // 変更をレジストリに書き、他のインスタンスへ変更をブロードキャストします。
  625.     WriteRegistryData(wFlag);
  626.     if(m_wUnique==0){ // 二度読みを避けるため、発信元インスタンスを特定します。
  627.         GetUniqueWord();
  628.     }
  629.     LONG lMsg=MAKELONG( wFlag, m_wUnique);
  630.     m_pApl->BroadcastMessage(m_dwCookie, lMsg);
  631.  
  632.  
  633. }
  634.  
  635.  
  636. ///////////////////////////////////////////////////////////////////////////////
  637. ///////////////////////////////////////////////////////////////////////////////
  638.  
  639. BOOL CCommandSink::GetFileName(CString &csFile)
  640. {
  641.     ISRFile *pIFile=NULL;
  642.     if(FAILED(m_pApl->CreateFileObject(&pIFile)) || pIFile==NULL){
  643.         MessageBox(NULL, _T("Failed to create ISRFile"), AfxGetAppName(), MB_OK);
  644.         return  FALSE; 
  645.     }
  646.     CComBSTR bsSTR;
  647.     pIFile->GetFilePath(&bsSTR);
  648.     csFile=bsSTR;
  649.     pIFile->Release();
  650.     return TRUE;
  651. }
  652.  
  653.  
  654. void CCommandSink::SetEnableEdit(BOOL bEnable)
  655. {
  656.     ISREdit *pIEdit=NULL;
  657.     if(FAILED(m_pApl->CreateEditObject(&pIEdit)) || pIEdit==NULL){
  658.         MessageBox(NULL, _T("Failed to create ISREdit"), AfxGetAppName(), MB_OK);
  659.         return; 
  660.     }
  661.     pIEdit->SetEnableEdit(bEnable);
  662.     pIEdit->Release();
  663. }
  664.  
  665.  
  666. ////////////////////////////////////////////////////////////////////////////////
  667. ////////////////////////////////////////////////////////////////////////////////
  668. ////////////////////////////////////////////////////////////////////////////////
  669.  
  670. template <class T>
  671. BOOL WriteBinaryData(HKEY hkey, T& sltLT, LPCTSTR lpKey)
  672. {
  673.     CMemFile mF;
  674.     CArchive ar(&mF, CArchive::store);
  675.  
  676.     
  677.     DWORD dwListSize=sltLT.size();
  678.     ar << dwListSize;
  679.     T::iterator p=sltLT.begin();
  680.     for( p; p!=sltLT.end(); p++)
  681.         ar << *p;
  682.     ar.Flush();
  683.     
  684.     DWORD dwSize=mF.GetLength();
  685.     BYTE *pData=new BYTE[dwSize];
  686.     mF.SeekToBegin();
  687.     mF.Read(pData, dwSize);
  688.     ar.Close();
  689.     mF.Close();
  690.     
  691.     BOOL bRtn=TRUE;
  692.     if(ERROR_SUCCESS!=RegSetValueEx(hkey, lpKey, NULL, REG_BINARY,pData, dwSize)){
  693.         MessageBox(NULL, _T("Failed to write registry"), AfxGetAppName(), MB_OK);
  694.         bRtn=FALSE;
  695.     }
  696.     delete [] pData;
  697.     
  698.     return bRtn;
  699. }
  700.  
  701.  
  702.  
  703.  
  704. void CCommandSink::WriteRegistryData(WORD wFlag)
  705. {
  706.     CRegKey rKeyBase1, rKeyBase2;
  707.     TCHAR szBuff[300];
  708.     wsprintf(szBuff, "Software\\Kazusoft\\%s", DATA_KEY);
  709.     if(ERROR_SUCCESS!=rKeyBase1.Create(HKEY_CURRENT_USER, szBuff)){
  710.         MessageBox(NULL, _T("Failed to create registry key 1"), AfxGetAppName(), MB_OK);
  711.         return;
  712.     }
  713.     if(ERROR_SUCCESS!=rKeyBase2.Create(rKeyBase1.m_hKey, _T("Files"))){
  714.         MessageBox(NULL, _T("Failed to create registry key 2"), AfxGetAppName(), MB_OK);
  715.         rKeyBase1.Close();
  716.         return;
  717.     }
  718.     rKeyBase1.Close();
  719.  
  720.     if(wFlag & F_1 && !WriteBinaryData(rKeyBase2.m_hKey, m_sltUEDir, _T("UEDir"))){        
  721.         rKeyBase2.Close();
  722.         return;
  723.     }
  724.     
  725.     if(wFlag & F_2 && !WriteBinaryData(rKeyBase2.m_hKey, m_sltUEExt, _T("UEExt"))){
  726.         rKeyBase2.Close();
  727.         return;
  728.     }
  729.  
  730.     if(wFlag & F_3 && !WriteBinaryData(rKeyBase2.m_hKey, m_sstExt, _T("ExtSet"))){
  731.         rKeyBase2.Close();
  732.         return;
  733.     }
  734.     
  735.     if(wFlag & F_4 && !WriteBinaryData(rKeyBase2.m_hKey, m_sstFile, _T("FileSet"))){
  736.         rKeyBase2.Close();
  737.         return;
  738.     }
  739.     
  740.     rKeyBase2.Close();
  741.     return;
  742.  
  743.  
  744. }
  745.  
  746.  
  747. template <class T>
  748. void GetBinaryDataA(HKEY hKey, T& sltLT,  LPCTSTR lpKey)
  749. {
  750.     DWORD dwType, dwSize, dwSetSize;
  751.     int i;
  752.     CString csTmp;
  753.     LONG lRes;
  754.  
  755.     lRes = RegQueryValueEx(hKey, lpKey, NULL, &dwType,NULL, &dwSize);
  756.     if(lRes!=ERROR_SUCCESS || dwType!=REG_BINARY) {
  757.         dwSize=0;
  758.     }
  759.     if(dwSize>0){
  760.         BYTE *pData =new BYTE[dwSize];
  761.         lRes=RegQueryValueEx(hKey, lpKey, NULL, &dwType, pData,&dwSize);
  762.         if(lRes==ERROR_SUCCESS){
  763.             CMemFile mF;
  764.             mF.Write(pData , dwSize);
  765.             mF.SeekToBegin();
  766.             CArchive ar(&mF, CArchive::load);
  767.             ar >> dwSetSize;
  768.             for(i=0; i<dwSetSize; i++)     {
  769.                 ar >> csTmp;
  770.                 sltLT.push_back(csTmp);
  771.             }
  772.         }
  773.         delete [] pData;
  774.     }
  775.  
  776. }
  777.  
  778. template <class T>
  779. void GetBinaryDataB(HKEY hKey, T& sltLT, int nHow,  LPCTSTR lpKey)
  780. {
  781.     DWORD dwType, dwSize, dwSetSize;
  782.     int i;
  783.     LONG lRes;
  784.     CStrSet cSS;
  785.  
  786.     lRes = RegQueryValueEx(hKey, lpKey, NULL, &dwType,NULL, &dwSize);
  787.     if(lRes!=ERROR_SUCCESS || dwType!=REG_BINARY) {
  788.         dwSize=0;
  789.         if(nHow==11){
  790.             cSS.m_csStrA=_T("cpp"); cSS.m_csStrB=_T("h"); cSS.m_dwMFlag=0;
  791.             sltLT.insert(cSS);
  792.             cSS.m_csStrA=_T("c"); cSS.m_csStrB=_T("h"); cSS.m_dwMFlag=0;
  793.             sltLT.insert(cSS);
  794.             cSS.m_csStrA=_T("h"); cSS.m_csStrB=_T("cpp"); cSS.m_dwMFlag=0;
  795.             sltLT.insert(cSS);
  796.             cSS.m_csStrA=_T("y"); cSS.m_csStrB=_T("l"); cSS.m_dwMFlag=0;
  797.             sltLT.insert(cSS);
  798.             cSS.m_csStrA=_T("l"); cSS.m_csStrB=_T("y"); cSS.m_dwMFlag=0;
  799.             sltLT.insert(cSS);
  800.             cSS.m_csStrA=_T("rc"); cSS.m_csStrB=_T("resource.h"); cSS.m_dwMFlag=F_1;
  801.             sltLT.insert(cSS);
  802.         }
  803.         else if(nHow==12) {
  804.             cSS.m_csStrA=_T("resource.h"); cSS.m_csStrB=_T("*.rc"); cSS.m_dwMFlag=F_1;
  805.             sltLT.insert(cSS);
  806.         }
  807.     }
  808.     if(dwSize>0){
  809.         BYTE *pData =new BYTE[dwSize];
  810.         lRes=RegQueryValueEx(hKey, lpKey, NULL, &dwType, pData,&dwSize);
  811.         if(lRes==ERROR_SUCCESS){
  812.             CMemFile mF;
  813.             mF.Write(pData , dwSize);
  814.             mF.SeekToBegin();
  815.             CArchive ar(&mF, CArchive::load);
  816.             ar >> dwSetSize;
  817.             for(i=0; i<dwSetSize; i++)     {
  818.                 ar >> cSS;
  819.                 sltLT.insert(cSS);
  820.             }
  821.         }
  822.         delete [] pData;
  823.     }
  824.  
  825. }
  826.  
  827.  
  828.  
  829.  
  830.  
  831. void CCommandSink::GetRegistryData(WORD wFlag)
  832. {
  833.     CRegKey rKeyBase;
  834.     TCHAR szBuff[300];
  835.     wsprintf(szBuff, "Software\\Kazusoft\\%s\\Files", DATA_KEY);
  836.  
  837.     if(ERROR_SUCCESS!=rKeyBase.Open(HKEY_CURRENT_USER, szBuff))
  838.         return;
  839.     
  840.     if(wFlag & F_1){
  841.         m_sltUEDir.clear();
  842.         GetBinaryDataA(rKeyBase.m_hKey, m_sltUEDir, _T("UEDir"));
  843.     }
  844.     
  845.     if(wFlag & F_2) {
  846.         m_sltUEExt.clear();
  847.         GetBinaryDataA(rKeyBase.m_hKey, m_sltUEDir,  _T("UEExt"));
  848.     }
  849.     
  850.     if(wFlag & F_3){
  851.         m_sstExt.clear();
  852.         GetBinaryDataB(rKeyBase.m_hKey, m_sstExt, 11, _T("ExtSet"));
  853.     }
  854.  
  855.     if(wFlag & F_4){
  856.         m_sstFile.clear();
  857.         GetBinaryDataB(rKeyBase.m_hKey, m_sstFile, 12, _T("FileSet"));
  858.     }
  859.  
  860.     rKeyBase.Close();
  861. }
  862.  
  863.  
  864. void CCommandSink::GetUniqueWord()
  865. {
  866.     if(m_wUnique!=0) return;
  867.  
  868.     long lhwnd;
  869.     m_pApl->GetMainWindowHandle(&lhwnd);
  870.     m_wUnique =0x0000FFFF & lhwnd;
  871.  
  872. }
  873.  
  874.  
  875.  
  876.