copyhook.cpp
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 7k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. /*----------------------------------------------------------------------------
  2. / Title;
  3. /   copyhook.cpp
  4. /
  5. / Authors;
  6. /   Rick Turner (ricktu)
  7. /
  8. / Notes;
  9. /   Implements ICopyHook for My Documents.
  10. /----------------------------------------------------------------------------*/
  11. #include "precomp.hxx"
  12. #pragma hdrstop
  13. /*-----------------------------------------------------------------------------
  14. / CMyDocsCopyHook
  15. /----------------------------------------------------------------------------*/
  16. CMyDocsCopyHook::CMyDocsCopyHook( )
  17. {
  18.     MDTraceEnter(TRACE_COPYHOOK, "CMyDocsCopyHook::CMyDocsCopyHook");
  19.     MDTraceLeave();
  20. }
  21. // IUnknown bits
  22. #undef CLASS_NAME
  23. #define CLASS_NAME CMyDocsCopyHook
  24. #include "unknown.inc"
  25. STDMETHODIMP CMyDocsCopyHook::QueryInterface(REFIID riid, LPVOID* ppvObject)
  26. {
  27.     INTERFACES iface[] =
  28.     {
  29.         &IID_IShellCopyHook, (ICopyHook *)this
  30.     };
  31.     return HandleQueryInterface(riid, ppvObject, iface, ARRAYSIZE(iface));
  32. }
  33. /*-----------------------------------------------------------------------------
  34. / ICopyHook methods
  35. /----------------------------------------------------------------------------*/
  36. UINT
  37. CMyDocsCopyHook::CopyCallback( HWND hwnd,
  38.                                UINT wFunc,
  39.                                UINT wFlags,
  40.                                LPCTSTR pszSrcFile,
  41.                                DWORD dwSrcAttribs,
  42.                                LPCTSTR pszDestFile,
  43.                                DWORD dwDestAttribs
  44.                               )
  45. {
  46.     UINT uRes = IDYES;
  47.     MDTraceEnter(TRACE_COPYHOOK, "CMyDocsCopyHook::CopyCallback");
  48.     MDTrace(TEXT("wFunc = 0x%X"), wFunc );
  49.     MDTraceAssert(pszSrcFile && (*pszSrcFile));
  50.     if ((!pszSrcFile) || (!(*pszSrcFile)))
  51.     {
  52.         MDTrace(TEXT("pszSrcFile is NULL!"));
  53.         goto exit_gracefully;
  54.     }
  55.     if ( ((wFunc == FO_COPY) || (wFunc == FO_MOVE)) &&
  56.          ((!pszDestFile) || (!(*pszDestFile)))
  57.         )
  58.     {
  59.         MDTrace(TEXT("pszDestFile is NULL on copy or move!"));
  60.         goto exit_gracefully;
  61.     }
  62. #ifdef DEBUG
  63.     MDTrace(TEXT("Source = %s"), pszSrcFile );
  64.     if (pszDestFile)
  65.     {
  66.         MDTrace(TEXT("Dest   = %s"), pszDestFile );
  67.     }
  68. #endif
  69.     if ( (wFunc == FO_COPY) || (wFunc == FO_MOVE) || (wFunc == FO_DELETE) )
  70.     {
  71.         TCHAR szPersonal[ MAX_PATH ];
  72.         DWORD dwRes;
  73.         //
  74.         //  Get the personal directory path
  75.         //
  76.         if (!SHGetSpecialFolderPath( hwnd, szPersonal, CSIDL_PERSONAL, FALSE ))
  77.         {
  78.             goto exit_gracefully;
  79.         }
  80.         //
  81.         // See if the source is the personal directory
  82.         //
  83.         if (lstrcmpi( pszSrcFile, szPersonal )!=0)
  84.         {
  85.             goto exit_gracefully;
  86.         }
  87.         GetMyDocumentsDisplayName( szPersonal, ARRAYSIZE(szPersonal) );
  88.         //
  89.         // check to see if we're trying to delete the MyDocs folder
  90.         // on the disk...this is a no-no!
  91.         //
  92.         if (wFunc == FO_DELETE)
  93.         {
  94.             LPTSTR pName = PathFindFileName( pszSrcFile );
  95.             //
  96.             // Alert the user that they cannot delete this guy...
  97.             //
  98.             uRes = IDNO;
  99.             ShellMessageBox( g_hInstance, hwnd,
  100.                              (LPTSTR)IDS_NODRAG_RECYCLEBIN, szPersonal,
  101.                              MB_OK | MB_ICONSTOP | MB_APPLMODAL | MB_TOPMOST,
  102.                              pName
  103.                             );
  104.             goto exit_gracefully;
  105.         }
  106.         //
  107.         // the source is the personal directory, now check if the
  108.         // destination is on the desktop...
  109.         //
  110.         dwRes = IsPathGoodMyDocsPath( hwnd, (LPTSTR)pszDestFile );
  111.         if (dwRes == PATH_IS_NONEXISTENT)
  112.         {
  113.             LPTSTR pLastSlash = NULL, pTmp;
  114.             //
  115.             // Find the last slash in the file name to isolate the
  116.             // new sub-directory that will be created...
  117.             //
  118.             for( pTmp = (LPTSTR)pszDestFile; *pTmp; pTmp++ )
  119.             {
  120.                 if (*pTmp == TEXT('\'))
  121.                 {
  122.                     pLastSlash = pTmp;
  123.                 }
  124.             }
  125.             //
  126.             // Check to see if the new folder will be a direct sub-folder of the
  127.             // desktop...
  128.             //
  129.             if (pLastSlash)
  130.             {
  131.                 TCHAR ch = *pLastSlash;
  132.                 *pLastSlash = 0;
  133.                 dwRes = IsPathGoodMyDocsPath( hwnd, (LPTSTR)pszDestFile );
  134.                 *pLastSlash = ch;
  135.             }
  136.         }
  137.         switch( dwRes )
  138.         {
  139.         case PATH_IS_DESKTOP:
  140.             //
  141.             // The user is trying to drag the personal folder to the
  142.             // desktop.  This is a no-no!
  143.             //
  144.             {
  145.                 TCHAR szVerb[ 32 ];
  146.                 LPTSTR pName = PathFindFileName( pszSrcFile );
  147.                 UINT verbID = (wFunc == FO_COPY) ? IDS_COPY : IDS_MOVE;
  148.                 szVerb[32] = 0;
  149.                 LoadString( g_hInstance, verbID, szVerb, ARRAYSIZE(szVerb) );
  150.                 uRes = IDNO;
  151.                 if (IsMyDocsHidden())
  152.                 {
  153.                     if ( IDYES == ShellMessageBox( g_hInstance, hwnd,
  154.                                                    (LPTSTR)IDS_NODRAG_DESKTOP_HIDDEN,
  155.                                                    szPersonal,
  156.                                                    MB_YESNO | MB_ICONQUESTION |
  157.                                                    MB_APPLMODAL | MB_TOPMOST,
  158.                                                    pName, szVerb, pName
  159.                                                   )
  160.                        )
  161.                     {
  162.                         RestoreMyDocsFolder( hwnd, g_hInstance, NULL, 0 );
  163.                     }
  164.                 }
  165.                 else
  166.                 {
  167.                     ShellMessageBox( g_hInstance, hwnd,
  168.                                      (LPTSTR)IDS_NODRAG_DESKTOP_NOT_HIDDEN,
  169.                                      szPersonal,
  170.                                      MB_OK | MB_ICONSTOP |
  171.                                      MB_APPLMODAL | MB_TOPMOST,
  172.                                      pName, szVerb
  173.                                     );
  174.                 }
  175.             }
  176.             break;
  177.         default:
  178.             break;
  179.         }
  180.     }
  181. exit_gracefully:
  182. #ifdef DEBUG
  183.     if (uRes == IDYES)
  184.     {
  185.         MDTrace(TEXT("returning IDYES"));
  186.     }
  187.     else if (uRes == IDNO)
  188.     {
  189.         MDTrace(TEXT("returning IDNO"));
  190.     }
  191.     else if (uRes == IDCANCEL)
  192.     {
  193.         MDTrace(TEXT("returning IDCANCEL"));
  194.     }
  195. #endif
  196.     MDTraceLeave();
  197.     return uRes;
  198. }