| 1 | // CtxMenu.cpp : Implementation of CCtxMenu |
|---|
| 2 | |
|---|
| 3 | #include "stdafx.h" |
|---|
| 4 | #include "CtxMenu.h" |
|---|
| 5 | |
|---|
| 6 | #pragma comment(lib,"shlwapi") |
|---|
| 7 | // CCtxMenu |
|---|
| 8 | |
|---|
| 9 | using namespace Eraser; |
|---|
| 10 | ///////////////////////////////////////////////////////////////////////////// |
|---|
| 11 | |
|---|
| 12 | HRESULT CCtxMenu::Initialize ( |
|---|
| 13 | LPCITEMIDLIST pidlFolder, |
|---|
| 14 | LPDATAOBJECT pDataObj, |
|---|
| 15 | HKEY hProgID ) |
|---|
| 16 | { |
|---|
| 17 | FORMATETC fmt = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL }; |
|---|
| 18 | STGMEDIUM stg = { TYMED_HGLOBAL }; |
|---|
| 19 | HDROP hDrop; |
|---|
| 20 | |
|---|
| 21 | // Look for CF_HDROP data in the data object. |
|---|
| 22 | if ( FAILED( pDataObj->GetData ( &fmt, &stg ))) |
|---|
| 23 | // Nope! Return an "invalid argument" error back to Explorer. |
|---|
| 24 | return E_INVALIDARG; |
|---|
| 25 | |
|---|
| 26 | // Get a pointer to the actual data. |
|---|
| 27 | hDrop = (HDROP) GlobalLock ( stg.hGlobal ); |
|---|
| 28 | |
|---|
| 29 | // Make sure it worked. |
|---|
| 30 | if ( NULL == hDrop ) |
|---|
| 31 | return E_INVALIDARG; |
|---|
| 32 | |
|---|
| 33 | // Sanity check - make sure there is at least one filename. |
|---|
| 34 | UINT uNumFiles = DragQueryFile ( hDrop, 0xFFFFFFFF, NULL, 0 ); |
|---|
| 35 | |
|---|
| 36 | if (!uNumFiles) |
|---|
| 37 | { |
|---|
| 38 | GlobalUnlock(stg.hGlobal); |
|---|
| 39 | ReleaseStgMedium(&stg); |
|---|
| 40 | return E_INVALIDARG; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | HRESULT hr = S_OK; |
|---|
| 44 | |
|---|
| 45 | WCHAR Buffer[sizeof(WCHAR)*MAX_PATH] = {0}; |
|---|
| 46 | for(UINT i = uNumFiles; i < uNumFiles; i++) |
|---|
| 47 | { |
|---|
| 48 | std::basic_string<WCHAR> str = Buffer; |
|---|
| 49 | if (!DragQueryFileW(hDrop, i, (LPWSTR)str.c_str(), str.length() )) |
|---|
| 50 | hr = E_INVALIDARG; |
|---|
| 51 | else |
|---|
| 52 | PathQuoteSpaces ( (LPWSTR) str.c_str() ); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | // TODO: load Eraser Icon and store HBITMAP in m_szEraserIcon |
|---|
| 56 | |
|---|
| 57 | GlobalUnlock ( stg.hGlobal ); |
|---|
| 58 | ReleaseStgMedium ( &stg ); |
|---|
| 59 | |
|---|
| 60 | return hr; |
|---|
| 61 | } |
|---|
| 62 | /* |
|---|
| 63 | +-------------------+ |
|---|
| 64 | | | |
|---|
| 65 | | | |
|---|
| 66 | | | |
|---|
| 67 | | | |
|---|
| 68 | +-------------------+ +-------------------+ |
|---|
| 69 | |(ICON) Eraser v6 > | | Erase selected | //--> erase the files immediately using defaults |
|---|
| 70 | +-------------------+ | Schedule Selected | //--> open the scheduler menu, with files/folders filled in |
|---|
| 71 | | | +-------------------+ |
|---|
| 72 | | | | Secure move | //--> secure move the files |
|---|
| 73 | | | +-------------------+ // Eraser.Manager Algorithms popup |
|---|
| 74 | | | |(*) Customise | // set algorithm for this query only |
|---|
| 75 | +-------------------+ +-------------------+ |
|---|
| 76 | */ |
|---|
| 77 | |
|---|
| 78 | /* |
|---|
| 79 | we have eraser shell scripts (.eshlls) where we use them |
|---|
| 80 | to generate shell extention menus dynamically. |
|---|
| 81 | the syntax is simple. |
|---|
| 82 | This project should be aimed at the 6.1 release. |
|---|
| 83 | |
|---|
| 84 | BEGIN @MENU : POSITION($INDEX) |
|---|
| 85 | [ICON="PATH"] |
|---|
| 86 | [TEXT="DISPLAY_TEXT"] |
|---|
| 87 | |
|---|
| 88 | [FLAGS=MIIM_*] |
|---|
| 89 | //MIIM_BITMAP |
|---|
| 90 | //MIIM_CHECKMARKS |
|---|
| 91 | //MIIM_DATA |
|---|
| 92 | //MIIM_FTYPE |
|---|
| 93 | //MIIM_ID |
|---|
| 94 | //MIIM_STATE |
|---|
| 95 | //MIIM_STRING |
|---|
| 96 | //MIIM_SUBMENU |
|---|
| 97 | //MIIM_TYPE |
|---|
| 98 | |
|---|
| 99 | [TYPE=MF_*] |
|---|
| 100 | //MF_APPEND, |
|---|
| 101 | //MF_BITMAP, |
|---|
| 102 | //MF_BYCOMMAND, |
|---|
| 103 | //MF_BYPOSITION |
|---|
| 104 | //MF_CHECKED |
|---|
| 105 | //MF_DEFAULT |
|---|
| 106 | //MF_DELETE |
|---|
| 107 | //MF_DISABLED |
|---|
| 108 | //MF_ENABLED |
|---|
| 109 | //MF_END |
|---|
| 110 | //MF_GRAYED |
|---|
| 111 | //MF_HELP |
|---|
| 112 | //MF_HILITE |
|---|
| 113 | //MF_INSERT |
|---|
| 114 | //MF_MENUBARBREAK |
|---|
| 115 | //MF_MENUBREAK |
|---|
| 116 | //MF_MOUSESELECT |
|---|
| 117 | //MF_OWNERDRAW |
|---|
| 118 | //MF_POPUP |
|---|
| 119 | //MF_POPUP |
|---|
| 120 | //MF_REMOVE, |
|---|
| 121 | //MF_RIGHTJUSTIFY, |
|---|
| 122 | //MF_SEPARATOR, |
|---|
| 123 | //MF_STRING, |
|---|
| 124 | //MF_SYSMENU, |
|---|
| 125 | //MF_UNCHECKED, |
|---|
| 126 | //MF_UNHILITE |
|---|
| 127 | //MF_USECHECKBITMAPS |
|---|
| 128 | END |
|---|
| 129 | |
|---|
| 130 | // Desirable to have bitmaps cached |
|---|
| 131 | @MENU |
|---|
| 132 | { |
|---|
| 133 | BITMAP = "FILE"; |
|---|
| 134 | TEXT="Eraser"; |
|---|
| 135 | BITMAP CHECKED="FILE"; |
|---|
| 136 | BITMAP UNCHECKED="FILE"; |
|---|
| 137 | |
|---|
| 138 | // submenu creation |
|---|
| 139 | @MENU |
|---|
| 140 | { |
|---|
| 141 | @CHECKBOX |
|---|
| 142 | { |
|---|
| 143 | } |
|---|
| 144 | @SEPERATOR |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | UINT cbSize; |
|---|
| 149 | UINT fMask; |
|---|
| 150 | UINT fType; // used if MIIM_TYPE (4.0) or MIIM_FTYPE (>4.0) |
|---|
| 151 | UINT fState; // used if MIIM_STATE |
|---|
| 152 | UINT wID; // used if MIIM_ID |
|---|
| 153 | HMENU hSubMenu; // used if MIIM_SUBMENU |
|---|
| 154 | HBITMAP hbmpChecked; // used if MIIM_CHECKMARKS |
|---|
| 155 | HBITMAP hbmpUnchecked; // used if MIIM_CHECKMARKS |
|---|
| 156 | ULONG_PTR dwItemData; // used if MIIM_DATA |
|---|
| 157 | __field_ecount_opt(cch) LPSTR dwTypeData; // used if MIIM_TYPE (4.0) or MIIM_STRING (>4.0) |
|---|
| 158 | UINT cch; // used if MIIM_TYPE (4.0) or MIIM_STRING (>4.0) |
|---|
| 159 | HBITMAP hbmpItem; // used if MIIM_BITMAP |
|---|
| 160 | |
|---|
| 161 | */ |
|---|
| 162 | |
|---|
| 163 | /** |
|---|
| 164 | * return a seperate MENUITEMINFO structure */ |
|---|
| 165 | static MENUITEMINFO* GetSeperator() |
|---|
| 166 | { |
|---|
| 167 | MENUITEMINFO *mii = new MENUITEMINFO(); |
|---|
| 168 | mii->cbSize = sizeof(MENUITEMINFO); |
|---|
| 169 | mii->fMask = MIIM_TYPE; |
|---|
| 170 | mii->fType = MF_SEPARATOR; |
|---|
| 171 | return mii; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | HRESULT CCtxMenu::QueryContextMenu ( |
|---|
| 175 | HMENU hmenu, |
|---|
| 176 | UINT uMenuIndex, |
|---|
| 177 | UINT uidFirstCmd, |
|---|
| 178 | UINT uidLastCmd, |
|---|
| 179 | UINT uFlags ) |
|---|
| 180 | { |
|---|
| 181 | // If the flags include CMF_DEFAULTONLY then we shouldn't do anything. |
|---|
| 182 | if ( uFlags & CMF_DEFAULTONLY ) |
|---|
| 183 | return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, 0 ); |
|---|
| 184 | |
|---|
| 185 | // First, create and populate a submenu. |
|---|
| 186 | UINT ctrPos = 0; |
|---|
| 187 | UINT uID = uidFirstCmd; |
|---|
| 188 | HMENU hSubmenu = CreatePopupMenu(); |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | /* Remember this order is defined _Statically_ on the CEraserLPVERB enum */ |
|---|
| 192 | /* -------------------------------- Submenu -------------------------------- */ |
|---|
| 193 | /* [0] */ InsertMenu ( hSubmenu, ctrPos++, MF_BYPOSITION, uID++, _T("&Erase" ) ); |
|---|
| 194 | /* [1] */ InsertMenu ( hSubmenu, ctrPos++, MF_BYPOSITION, uID++, _T("&Schedule" ) ); |
|---|
| 195 | /* [2] */ InsertMenu ( hSubmenu, ctrPos++, MF_BYPOSITION, uID++, _T("Erase on &Restart" ) ); |
|---|
| 196 | /* ------------------------------------------------------------------------- */ |
|---|
| 197 | /* [3] */ InsertMenuItem ( hSubmenu, ctrPos++, TRUE, GetSeperator() ); |
|---|
| 198 | /* [4] */ InsertMenu ( hSubmenu, ctrPos++, MF_BYPOSITION, uID++, _T("Secure &Move" ) ); |
|---|
| 199 | /* ------------------------------------------------------------------------- */ |
|---|
| 200 | /* [5] */ InsertMenuItem ( hSubmenu, ctrPos++, TRUE, GetSeperator() ); |
|---|
| 201 | /* [6] */ InsertMenu ( hSubmenu, ctrPos++, MF_BYPOSITION, uID++, _T("&Customise") ); |
|---|
| 202 | /* ------------------------------ Submenu end ------------------------------ */ |
|---|
| 203 | |
|---|
| 204 | // Insert the submenu into the ctx menu provided by Explorer. |
|---|
| 205 | /* ------------------------------------------------------------------------- */ |
|---|
| 206 | InsertMenuItem ( hSubmenu, uMenuIndex++, TRUE, GetSeperator() ); |
|---|
| 207 | { |
|---|
| 208 | MENUITEMINFO mii = { sizeof(MENUITEMINFO) }; |
|---|
| 209 | mii.wID = uID++; |
|---|
| 210 | mii.fMask = MIIM_SUBMENU | MIIM_STRING | MIIM_ID; |
|---|
| 211 | mii.hSubMenu = hSubmenu; |
|---|
| 212 | mii.dwTypeData = _T("Eraser v6"); |
|---|
| 213 | InsertMenuItem(hmenu, uMenuIndex++, TRUE, &mii); |
|---|
| 214 | } |
|---|
| 215 | /* ------------------------------------------------------------------------- */ |
|---|
| 216 | InsertMenuItem ( hSubmenu, uMenuIndex++, TRUE, GetSeperator() ); |
|---|
| 217 | |
|---|
| 218 | // Set the bitmap for the register item. |
|---|
| 219 | if ( m_szEraserIcon != ((HBITMAP)0) ) |
|---|
| 220 | SetMenuItemBitmaps(hmenu, uMenuIndex++, MF_BYPOSITION, m_szEraserIcon, NULL ); |
|---|
| 221 | |
|---|
| 222 | return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, uID - uidFirstCmd ); |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | HRESULT CCtxMenu::GetCommandString ( |
|---|
| 226 | UINT idCmd, |
|---|
| 227 | UINT uFlags, |
|---|
| 228 | UINT* pwReserved, |
|---|
| 229 | LPSTR pszName, |
|---|
| 230 | UINT cchMax ) |
|---|
| 231 | { |
|---|
| 232 | USES_CONVERSION; |
|---|
| 233 | |
|---|
| 234 | // Check idCmd, it must be 0 or 1 since we have two menu items. |
|---|
| 235 | if ( idCmd > 4 ) |
|---|
| 236 | return E_INVALIDARG; |
|---|
| 237 | |
|---|
| 238 | // If Explorer is asking for a help string, copy our string into the |
|---|
| 239 | // supplied buffer. |
|---|
| 240 | if (!(uFlags & GCS_HELPTEXT)) |
|---|
| 241 | return E_INVALIDARG; |
|---|
| 242 | |
|---|
| 243 | static LPCTSTR szNotepadText = _T("Open the selected file in Notepad"); |
|---|
| 244 | static LPCTSTR szIEText = _T("Open the selected file in Internet Explorer"); |
|---|
| 245 | LPCTSTR pszText = (0 == idCmd) ? szNotepadText : szIEText; |
|---|
| 246 | |
|---|
| 247 | if ( uFlags & GCS_UNICODE ) |
|---|
| 248 | // We need to cast pszName to a Unicode string, and then use the |
|---|
| 249 | // Unicode string copy API. |
|---|
| 250 | lstrcpynW ( (LPWSTR) pszName, T2CW(pszText), cchMax ); |
|---|
| 251 | else |
|---|
| 252 | // Use the ANSI string copy API to return the help string. |
|---|
| 253 | lstrcpynA ( pszName, T2CA(pszText), cchMax ); |
|---|
| 254 | |
|---|
| 255 | return S_OK; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | // TODO: write procedures for Eraser C# engine |
|---|
| 259 | /* |
|---|
| 260 | 1) call scheduler, with all of the required structs etc. |
|---|
| 261 | 2) Read avilable algorithms list |
|---|
| 262 | 3) Set default algorithm |
|---|
| 263 | */ |
|---|
| 264 | |
|---|
| 265 | #define foreach(iter, container) \ |
|---|
| 266 | for(string_list::iterator iter = container.begin(); \ |
|---|
| 267 | iter != container.end(); iter++) |
|---|
| 268 | |
|---|
| 269 | HRESULT CCtxMenu::InvokeCommand ( LPCMINVOKECOMMANDINFO pCmdInfo ) |
|---|
| 270 | { |
|---|
| 271 | // If lpVerb really points to a string, ignore this function call and bail out. |
|---|
| 272 | if ( HIWORD( pCmdInfo->lpVerb ) != 0) |
|---|
| 273 | return E_INVALIDARG; |
|---|
| 274 | |
|---|
| 275 | // Get the command index. |
|---|
| 276 | switch( LOWORD(pCmdInfo->lpVerb + 1) ) |
|---|
| 277 | { |
|---|
| 278 | case CERASER_ENUM(CEraserLPVERBS::CERASER_ERASE): |
|---|
| 279 | { |
|---|
| 280 | List<String ^> ^files = gcnew List<String ^>(m_szSelectedFiles.size()); |
|---|
| 281 | |
|---|
| 282 | foreach(file, m_szSelectedFiles) |
|---|
| 283 | { |
|---|
| 284 | files->Add( UnmanagedToManagedString(*file) ); |
|---|
| 285 | } |
|---|
| 286 | EraseFiles(files); |
|---|
| 287 | } |
|---|
| 288 | case CERASER_ENUM(CEraserLPVERBS::CERASER_SECURE_MOVE): |
|---|
| 289 | { |
|---|
| 290 | List<String ^> ^files = gcnew List<String ^>(m_szSelectedFiles.size()); |
|---|
| 291 | |
|---|
| 292 | foreach(file, m_szSelectedFiles) |
|---|
| 293 | { |
|---|
| 294 | files->Add( UnmanagedToManagedString(*file) ); |
|---|
| 295 | } |
|---|
| 296 | EraseFiles(files); |
|---|
| 297 | } |
|---|
| 298 | // NOT IMPLEMENTED METHODS |
|---|
| 299 | case CERASER_ENUM(CEraserLPVERBS::CERASER_ERASE_ON_RESTART): |
|---|
| 300 | { |
|---|
| 301 | List<String ^> ^files = gcnew List<String ^>(m_szSelectedFiles.size()); |
|---|
| 302 | |
|---|
| 303 | foreach(file, m_szSelectedFiles) |
|---|
| 304 | { |
|---|
| 305 | files->Add( UnmanagedToManagedString(*file) ); |
|---|
| 306 | } |
|---|
| 307 | EraseFiles(files); |
|---|
| 308 | } |
|---|
| 309 | case CERASER_ENUM(CEraserLPVERBS::CERASER_SCHEDULE): |
|---|
| 310 | { |
|---|
| 311 | foreach(file, m_szSelectedFiles) |
|---|
| 312 | { |
|---|
| 313 | } |
|---|
| 314 | } |
|---|
| 315 | case CERASER_ENUM(CEraserLPVERBS::CERASER_CUSTOMISE): |
|---|
| 316 | { |
|---|
| 317 | } |
|---|
| 318 | default: |
|---|
| 319 | { |
|---|
| 320 | TCHAR szMsg [MAX_PATH + 32]; |
|---|
| 321 | wsprintf ( szMsg, _T("Invalid Query was submitted, unable to process!\n\nID = %d\n\n"), LOWORD(pCmdInfo->lpVerb) ); |
|---|
| 322 | MessageBox ( pCmdInfo->hwnd, szMsg, _T("Eraser v6 - Shell Extention Query"), MB_ICONINFORMATION ); |
|---|
| 323 | return E_INVALIDARG; |
|---|
| 324 | } |
|---|
| 325 | break; // unreachable code |
|---|
| 326 | } |
|---|
| 327 | } |
|---|