Buffer overflows and strncpy fixes.
[wine] / dlls / shell32 / contmenu.c
1 /*
2  *      IContextMenu
3  *
4  *      Copyright 1998  Juergen Schmied <juergen.schmied@metronet.de>
5  */
6 #include <string.h>
7
8 #include "winerror.h"
9 #include "debugtools.h"
10
11 #include "pidl.h"
12 #include "wine/obj_base.h"
13 #include "wine/obj_contextmenu.h"
14 #include "wine/obj_shellbrowser.h"
15 #include "wine/obj_shellextinit.h"
16 #include "wine/undocshell.h"
17
18 #include "shell32_main.h"
19
20 DEFAULT_DEBUG_CHANNEL(shell)
21
22 /**************************************************************************
23 * IContextMenu VTable
24
25 */
26
27 static HRESULT WINAPI IContextMenu_fnQueryInterface(IContextMenu *iface, REFIID riid, LPVOID *ppvObj);
28 static ULONG WINAPI IContextMenu_fnAddRef(IContextMenu *iface);
29 static ULONG WINAPI IContextMenu_fnRelease(IContextMenu *iface);
30 static HRESULT WINAPI IContextMenu_fnQueryContextMenu(IContextMenu *iface, HMENU hmenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags);
31 static HRESULT WINAPI IContextMenu_fnInvokeCommand(IContextMenu *iface, LPCMINVOKECOMMANDINFO lpcmi);
32 static HRESULT WINAPI IContextMenu_fnGetCommandString(IContextMenu *iface, UINT idCommand, UINT uFlags, LPUINT lpReserved, LPSTR lpszName, UINT uMaxNameLen);
33 static HRESULT WINAPI IContextMenu_fnHandleMenuMsg(IContextMenu *iface, UINT uMsg, WPARAM wParam, LPARAM lParam);
34
35 static struct ICOM_VTABLE(IContextMenu) cmvt = 
36 {       
37         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
38         IContextMenu_fnQueryInterface,
39         IContextMenu_fnAddRef,
40         IContextMenu_fnRelease,
41         IContextMenu_fnQueryContextMenu,
42         IContextMenu_fnInvokeCommand,
43         IContextMenu_fnGetCommandString,
44         IContextMenu_fnHandleMenuMsg,
45         (void *) 0xdeadbabe     /* just paranoia */
46 };
47
48 /**************************************************************************
49 *  IContextMenu Implementation
50 */
51 typedef struct 
52 {       ICOM_VFIELD(IContextMenu);
53         DWORD           ref;
54         IShellFolder*   pSFParent;
55         LPITEMIDLIST    pidl;           /* root pidl */
56         LPITEMIDLIST    *aPidls;        /* array of child pidls */
57         BOOL            bAllValues;
58 } IContextMenuImpl;
59
60
61 static struct ICOM_VTABLE(IContextMenu) cmvt;
62
63 /**************************************************************************
64 *  IContextMenu_AllocPidlTable()
65 */
66 static BOOL IContextMenu_AllocPidlTable(IContextMenuImpl *This, DWORD dwEntries)
67 {
68         TRACE("(%p)->(entrys=%lu)\n",This, dwEntries);
69
70         /*add one for NULL terminator */
71         dwEntries++;
72
73         This->aPidls = (LPITEMIDLIST*)SHAlloc(dwEntries * sizeof(LPITEMIDLIST));
74
75         if(This->aPidls)
76         { ZeroMemory(This->aPidls, dwEntries * sizeof(LPITEMIDLIST));   /*set all of the entries to NULL*/
77         }
78         return (This->aPidls != NULL);
79 }
80
81 /**************************************************************************
82 * IContextMenu_FreePidlTable()
83 */
84 static void IContextMenu_FreePidlTable(IContextMenuImpl *This)
85 {
86         int   i;
87
88         TRACE("(%p)->()\n",This);
89
90         if(This->aPidls)
91         { for(i = 0; This->aPidls[i]; i++)
92           { SHFree(This->aPidls[i]);
93           }
94
95           SHFree(This->aPidls);
96           This->aPidls = NULL;
97         }
98 }
99
100 /**************************************************************************
101 * IContextMenu_FillPidlTable()
102 */
103 static BOOL IContextMenu_FillPidlTable(IContextMenuImpl *This, LPCITEMIDLIST *aPidls, UINT uItemCount)
104 {
105         UINT  i;
106
107         TRACE("(%p)->(apidl=%p count=%u)\n",This, aPidls, uItemCount);
108
109         if(This->aPidls)
110         { for(i = 0; i < uItemCount; i++)
111           { This->aPidls[i] = ILClone(aPidls[i]);
112           }
113           return TRUE;
114         }
115         return FALSE;
116 }
117
118 /**************************************************************************
119 * IContextMenu_CanRenameItems()
120 */
121 static BOOL IContextMenu_CanRenameItems(IContextMenuImpl *This)
122 {       UINT  i;
123         DWORD dwAttributes;
124
125         TRACE("(%p)->()\n",This);
126
127         if(This->aPidls)
128         {
129           for(i = 0; This->aPidls[i]; i++){} /*get the number of items assigned to This object*/
130           { if(i > 1)   /*you can't rename more than one item at a time*/
131             { return FALSE;
132             }
133           }
134           dwAttributes = SFGAO_CANRENAME;
135           IShellFolder_GetAttributesOf(This->pSFParent, i, (LPCITEMIDLIST*)This->aPidls, &dwAttributes);
136
137           return dwAttributes & SFGAO_CANRENAME;
138         }
139         return FALSE;
140 }
141
142 /**************************************************************************
143 *   IContextMenu_Constructor()
144 */
145 IContextMenu *IContextMenu_Constructor(LPSHELLFOLDER pSFParent, LPCITEMIDLIST pidl, LPCITEMIDLIST *aPidls, UINT uItemCount)
146 {       IContextMenuImpl* cm;
147         UINT  u;
148
149         cm = (IContextMenuImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IContextMenuImpl));
150         ICOM_VTBL(cm)=&cmvt;
151         cm->ref = 1;
152         cm->pidl = ILClone(pidl);
153
154         cm->pSFParent = pSFParent;
155
156         if(pSFParent)
157            IShellFolder_AddRef(pSFParent);
158
159         cm->aPidls = NULL;
160
161         IContextMenu_AllocPidlTable(cm, uItemCount);
162
163         if(cm->aPidls)
164         { IContextMenu_FillPidlTable(cm, aPidls, uItemCount);
165         }
166
167         cm->bAllValues = 1;
168         for(u = 0; u < uItemCount; u++)
169         { cm->bAllValues &= (_ILIsValue(aPidls[u]) ? 1 : 0);
170         }
171         TRACE("(%p)->()\n",cm);
172         shell32_ObjCount++;
173         return (IContextMenu*)cm;
174 }
175
176 /**************************************************************************
177 *  IContextMenu_fnQueryInterface
178 */
179 static HRESULT WINAPI IContextMenu_fnQueryInterface(IContextMenu *iface, REFIID riid, LPVOID *ppvObj)
180 {
181         ICOM_THIS(IContextMenuImpl, iface);
182
183         char    xriid[50];
184         WINE_StringFromCLSID((LPCLSID)riid,xriid);
185
186         TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,xriid,ppvObj);
187
188         *ppvObj = NULL;
189
190         if(IsEqualIID(riid, &IID_IUnknown))          /*IUnknown*/
191         { *ppvObj = This; 
192         }
193         else if(IsEqualIID(riid, &IID_IContextMenu))  /*IContextMenu*/
194         { *ppvObj = This;
195         }   
196         else if(IsEqualIID(riid, &IID_IShellExtInit))  /*IShellExtInit*/
197         { FIXME("-- LPSHELLEXTINIT pointer requested\n");
198         }
199
200         if(*ppvObj)
201         { 
202           IContextMenu_AddRef((IContextMenu*)*ppvObj);      
203           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
204           return S_OK;
205         }
206         TRACE("-- Interface: E_NOINTERFACE\n");
207         return E_NOINTERFACE;
208 }
209
210 /**************************************************************************
211 *  IContextMenu_fnAddRef
212 */
213 static ULONG WINAPI IContextMenu_fnAddRef(IContextMenu *iface)
214 {
215         ICOM_THIS(IContextMenuImpl, iface);
216
217         TRACE("(%p)->(count=%lu)\n",This, This->ref);
218
219         shell32_ObjCount++;
220         return ++(This->ref);
221 }
222
223 /**************************************************************************
224 *  IContextMenu_fnRelease
225 */
226 static ULONG WINAPI IContextMenu_fnRelease(IContextMenu *iface)
227 {
228         ICOM_THIS(IContextMenuImpl, iface);
229
230         TRACE("(%p)->()\n",This);
231
232         shell32_ObjCount--;
233
234         if (!--(This->ref)) 
235         { TRACE(" destroying IContextMenu(%p)\n",This);
236
237           if(This->pSFParent)
238             IShellFolder_Release(This->pSFParent);
239
240           if(This->pidl)
241             SHFree(This->pidl);
242           
243           /*make sure the pidl is freed*/
244           if(This->aPidls)
245           { IContextMenu_FreePidlTable(This);
246           }
247
248           HeapFree(GetProcessHeap(),0,This);
249           return 0;
250         }
251         return This->ref;
252 }
253
254 /**************************************************************************
255 *  ICM_InsertItem()
256 */ 
257 void WINAPI _InsertMenuItem (
258         HMENU hmenu,
259         UINT indexMenu,
260         BOOL fByPosition,
261         UINT wID,
262         UINT fType,
263         LPSTR dwTypeData,
264         UINT fState)
265 {
266         MENUITEMINFOA   mii;
267
268         ZeroMemory(&mii, sizeof(mii));
269         mii.cbSize = sizeof(mii);
270         if (fType == MFT_SEPARATOR)
271         { mii.fMask = MIIM_ID | MIIM_TYPE;
272         }
273         else
274         { mii.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE;
275           mii.dwTypeData = dwTypeData;
276           mii.fState = fState;
277         }
278         mii.wID = wID;
279         mii.fType = fType;
280         InsertMenuItemA( hmenu, indexMenu, fByPosition, &mii);
281 }
282 /**************************************************************************
283 * IContextMenu_fnQueryContextMenu()
284 */
285
286 static HRESULT WINAPI IContextMenu_fnQueryContextMenu(
287         IContextMenu *iface,
288         HMENU hmenu,
289         UINT indexMenu,
290         UINT idCmdFirst,
291         UINT idCmdLast,
292         UINT uFlags)
293 {
294         ICOM_THIS(IContextMenuImpl, iface);
295
296         BOOL    fExplore ;
297
298         TRACE("(%p)->(hmenu=%x indexmenu=%x cmdfirst=%x cmdlast=%x flags=%x )\n",This, hmenu, indexMenu, idCmdFirst, idCmdLast, uFlags);
299
300         if(!(CMF_DEFAULTONLY & uFlags))
301         { if(!This->bAllValues) 
302           { /* folder menu */
303             fExplore = uFlags & CMF_EXPLORE;
304             if(fExplore) 
305             { _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED|MFS_DEFAULT);
306               _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_OPEN, MFT_STRING, "&Open", MFS_ENABLED);
307             }
308             else
309             { _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_OPEN, MFT_STRING, "&Open", MFS_ENABLED|MFS_DEFAULT);
310               _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_EXPLORE, MFT_STRING, "&Explore", MFS_ENABLED);
311             }
312
313             if(uFlags & CMF_CANRENAME)
314             { _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
315               _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_RENAME, MFT_STRING, "&Rename", (IContextMenu_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED));
316             }
317           }
318           else  /* file menu */
319           { _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_OPEN, MFT_STRING, "&Open", MFS_ENABLED|MFS_DEFAULT);
320             if(uFlags & CMF_CANRENAME)
321             { _InsertMenuItem(hmenu, indexMenu++, TRUE, 0, MFT_SEPARATOR, NULL, 0);
322               _InsertMenuItem(hmenu, indexMenu++, TRUE, idCmdFirst+IDM_RENAME, MFT_STRING, "&Rename", (IContextMenu_CanRenameItems(This) ? MFS_ENABLED : MFS_DISABLED));
323             }
324           }
325           return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (IDM_LAST + 1));
326         }
327         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
328 }
329
330 /**************************************************************************
331 * IContextMenu_fnInvokeCommand()
332 */
333 static HRESULT WINAPI IContextMenu_fnInvokeCommand(
334         IContextMenu *iface,
335         LPCMINVOKECOMMANDINFO lpcmi)
336 {
337         ICOM_THIS(IContextMenuImpl, iface);
338
339         LPITEMIDLIST    pidlFQ;
340         SHELLEXECUTEINFOA       sei;
341         int   i;
342
343         TRACE("(%p)->(invcom=%p verb=%p wnd=%x)\n",This,lpcmi,lpcmi->lpVerb, lpcmi->hwnd);    
344
345         if(LOWORD(lpcmi->lpVerb) > IDM_LAST)
346           return E_INVALIDARG;
347
348         switch(LOWORD(lpcmi->lpVerb))
349         { case IDM_EXPLORE:
350           case IDM_OPEN:
351           /* Find the first item in the list that is not a value. These commands 
352             should never be invoked if there isn't at least one folder item in the list.*/
353
354             for(i = 0; This->aPidls[i]; i++)
355             { if(!_ILIsValue(This->aPidls[i]))
356                  break;
357             }
358
359             pidlFQ = ILCombine(This->pidl, This->aPidls[i]);
360
361             ZeroMemory(&sei, sizeof(sei));
362             sei.cbSize = sizeof(sei);
363             sei.fMask = SEE_MASK_IDLIST | SEE_MASK_CLASSNAME;
364             sei.lpIDList = pidlFQ;
365             sei.lpClass = "folder";
366             sei.hwnd = lpcmi->hwnd;
367             sei.nShow = SW_SHOWNORMAL;
368
369             if(LOWORD(lpcmi->lpVerb) == IDM_EXPLORE)
370             { sei.lpVerb = "explore";
371             }
372             else
373             { sei.lpVerb = "open";
374             }
375             ShellExecuteExA(&sei);
376             SHFree(pidlFQ);
377             break;
378                 
379           case IDM_RENAME:
380             MessageBeep(MB_OK);
381             /*handle rename for the view here*/
382             break;          
383         }
384         return NOERROR;
385 }
386
387 /**************************************************************************
388 *  IContextMenu_fnGetCommandString()
389 */
390 static HRESULT WINAPI IContextMenu_fnGetCommandString(
391         IContextMenu *iface,
392         UINT idCommand,
393         UINT uFlags,
394         LPUINT lpReserved,
395         LPSTR lpszName,
396         UINT uMaxNameLen)
397 {       
398         ICOM_THIS(IContextMenuImpl, iface);
399
400         HRESULT  hr = E_INVALIDARG;
401
402         TRACE("(%p)->(idcom=%x flags=%x %p name=%p len=%x)\n",This, idCommand, uFlags, lpReserved, lpszName, uMaxNameLen);
403
404         switch(uFlags)
405         { case GCS_HELPTEXT:
406             hr = E_NOTIMPL;
407             break;
408
409           case GCS_VERBA:
410             switch(idCommand)
411             { case IDM_RENAME:
412                 strcpy((LPSTR)lpszName, "rename");
413                 hr = NOERROR;
414                 break;
415             }
416             break;
417
418              /* NT 4.0 with IE 3.0x or no IE will always call This with GCS_VERBW. In This 
419              case, you need to do the lstrcpyW to the pointer passed.*/
420           case GCS_VERBW:
421             switch(idCommand)
422             { case IDM_RENAME:
423                 lstrcpyAtoW((LPWSTR)lpszName, "rename");
424                 hr = NOERROR;
425                 break;
426             }
427             break;
428
429           case GCS_VALIDATE:
430             hr = NOERROR;
431             break;
432         }
433         TRACE("-- (%p)->(name=%s)\n",This, lpszName);
434         return hr;
435 }
436
437 /**************************************************************************
438 * IContextMenu_fnHandleMenuMsg()
439 * NOTES
440 *  should be only in IContextMenu2 and IContextMenu3
441 *  is nevertheless called from word95
442 */
443 static HRESULT WINAPI IContextMenu_fnHandleMenuMsg(
444         IContextMenu *iface,
445         UINT uMsg,
446         WPARAM wParam,
447         LPARAM lParam)
448 {
449         ICOM_THIS(IContextMenuImpl, iface);
450
451         TRACE("(%p)->(msg=%x wp=%x lp=%lx)\n",This, uMsg, wParam, lParam);
452
453         return E_NOTIMPL;
454 }
455