Implemented NtCreatelFile using the new symlink scheme.
[wine] / dlls / shell32 / cpanelfolder.c
1 /*
2  * Control panel folder
3  *
4  * Copyright 2003 Martin Fuchs
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or(at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
31 #include "winerror.h"
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winreg.h"
35 #include "wingdi.h"
36 #include "winuser.h"
37
38 #include "ole2.h"
39 #include "shlguid.h"
40
41 #include "cpanel.h"
42 #include "enumidlist.h"
43 #include "pidl.h"
44 #include "undocshell.h"
45 #include "shell32_main.h"
46 #include "shresdef.h"
47 #include "shlwapi.h"
48 #include "shellfolder.h"
49 #include "wine/debug.h"
50 #include "debughlp.h"
51 #include "shfldr.h"
52
53 WINE_DEFAULT_DEBUG_CHANNEL(shell);
54
55 /***********************************************************************
56 *   control panel implementation in shell namespace
57 */
58
59 typedef struct {
60     ICOM_VFIELD(IShellFolder2);
61     DWORD ref;
62
63     ICOM_VTABLE(IPersistFolder2)* lpVtblPersistFolder2;
64     ICOM_VTABLE(IShellExecuteHookW)* lpVtblShellExecuteHookW;
65     ICOM_VTABLE(IShellExecuteHookA)* lpVtblShellExecuteHookA;
66
67     IUnknown *pUnkOuter;        /* used for aggregation */
68
69     /* both paths are parsible from the desktop */
70     LPITEMIDLIST pidlRoot;      /* absolute pidl */
71     int dwAttributes;           /* attributes returned by GetAttributesOf FIXME: use it */
72 } ICPanelImpl;
73
74 static ICOM_VTABLE(IShellFolder2) vt_ShellFolder2;
75 static ICOM_VTABLE(IPersistFolder2) vt_PersistFolder2;
76 static ICOM_VTABLE(IShellExecuteHookW) vt_ShellExecuteHookW;
77 static ICOM_VTABLE(IShellExecuteHookA) vt_ShellExecuteHookA;
78
79 #define _IPersistFolder2_Offset     ((int)(&(((ICPanelImpl*)0)->lpVtblPersistFolder2)))
80 #define _ICOM_THIS_From_IPersistFolder2(class, name) class* This = (class*)(((char*)name)-_IPersistFolder2_Offset);
81
82 #define IShellExecuteHookW_Offset   ((int)(&(((ICPanelImpl*)0)->lpVtblShellExecuteHookW)))
83 #define _ICOM_THIS_From_IShellExecuteHookW(class, name) class* This = (class*)(((char*)name)-IShellExecuteHookW_Offset);
84
85 #define IShellExecuteHookA_Offset   ((int)(&(((ICPanelImpl*)0)->lpVtblShellExecuteHookA)))
86 #define _ICOM_THIS_From_IShellExecuteHookA(class, name) class* This = (class*)(((char*)name)-IShellExecuteHookA_Offset);
87
88
89 /*
90   converts This to a interface pointer
91 */
92 #define _IUnknown_(This)           (IUnknown*)&(This->lpVtbl)
93 #define _IShellFolder_(This)       (IShellFolder*)&(This->lpVtbl)
94 #define _IShellFolder2_(This)      (IShellFolder2*)&(This->lpVtbl)
95
96 #define _IPersist_(This)           (IPersist*)&(This->lpVtblPersistFolder2)
97 #define _IPersistFolder_(This)     (IPersistFolder*)&(This->lpVtblPersistFolder2)
98 #define _IPersistFolder2_(This)    (IPersistFolder2*)&(This->lpVtblPersistFolder2)
99 #define _IShellExecuteHookW_(This) (IShellExecuteHookW*)&(This->lpVtblShellExecuteHookW)
100 #define _IShellExecuteHookA_(This) (IShellExecuteHookA*)&(This->lpVtblShellExecuteHookA)
101
102 /***********************************************************************
103 *   IShellFolder [ControlPanel] implementation
104 */
105
106 static shvheader ControlPanelSFHeader[] = {
107     {IDS_SHV_COLUMN8, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},/*FIXME*/
108     {IDS_SHV_COLUMN9, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 200},/*FIXME*/
109 };
110
111 #define CONROLPANELSHELLVIEWCOLUMNS 2
112
113 /**************************************************************************
114 *       IControlPanel_Constructor
115 */
116 HRESULT WINAPI IControlPanel_Constructor(IUnknown* pUnkOuter, REFIID riid, LPVOID * ppv)
117 {
118     ICPanelImpl *sf;
119
120     TRACE("unkOut=%p %s\n", pUnkOuter, shdebugstr_guid(riid));
121
122     if (!ppv)
123         return E_POINTER;
124     if (pUnkOuter && !IsEqualIID (riid, &IID_IUnknown))
125         return CLASS_E_NOAGGREGATION;
126
127     sf = (ICPanelImpl *) LocalAlloc(GMEM_ZEROINIT, sizeof(ICPanelImpl));
128     if (!sf)
129         return E_OUTOFMEMORY;
130
131     sf->ref = 0;
132     sf->lpVtbl = &vt_ShellFolder2;
133     sf->lpVtblPersistFolder2 = &vt_PersistFolder2;
134     sf->lpVtblShellExecuteHookW = &vt_ShellExecuteHookW;
135     sf->lpVtblShellExecuteHookA = &vt_ShellExecuteHookA;
136     sf->pidlRoot = _ILCreateControlPanel();     /* my qualified pidl */
137     sf->pUnkOuter = pUnkOuter ? pUnkOuter : _IUnknown_ (sf);
138
139     if (!SUCCEEDED(IUnknown_QueryInterface(_IUnknown_(sf), riid, ppv))) {
140         IUnknown_Release(_IUnknown_(sf));
141         return E_NOINTERFACE;
142     }
143
144     TRACE("--(%p)\n", sf);
145     return S_OK;
146 }
147
148 /**************************************************************************
149  *      ISF_ControlPanel_fnQueryInterface
150  *
151  * NOTES supports not IPersist/IPersistFolder
152  */
153 static HRESULT WINAPI ISF_ControlPanel_fnQueryInterface(IShellFolder2 * iface, REFIID riid, LPVOID * ppvObject)
154 {
155     ICOM_THIS(ICPanelImpl, iface);
156
157     TRACE("(%p)->(%s,%p)\n", This, shdebugstr_guid(riid), ppvObject);
158
159     *ppvObject = NULL;
160
161     if (IsEqualIID(riid, &IID_IUnknown) ||
162         IsEqualIID(riid, &IID_IShellFolder) || IsEqualIID(riid, &IID_IShellFolder2))
163         *ppvObject = This;
164     else if (IsEqualIID(riid, &IID_IPersist) ||
165                IsEqualIID(riid, &IID_IPersistFolder) || IsEqualIID(riid, &IID_IPersistFolder2))
166         *ppvObject = _IPersistFolder2_(This);
167     else if (IsEqualIID(riid, &IID_IShellExecuteHookW))
168         *ppvObject = _IShellExecuteHookW_(This);
169     else if (IsEqualIID(riid, &IID_IShellExecuteHookA))
170         *ppvObject = _IShellExecuteHookA_(This);
171
172     if (*ppvObject) {
173         IUnknown_AddRef((IUnknown *)(*ppvObject));
174         TRACE("-- Interface:(%p)->(%p)\n", ppvObject, *ppvObject);
175         return S_OK;
176     }
177     TRACE("-- Interface: E_NOINTERFACE\n");
178     return E_NOINTERFACE;
179 }
180
181 static ULONG WINAPI ISF_ControlPanel_fnAddRef(IShellFolder2 * iface)
182 {
183     ICOM_THIS(ICPanelImpl, iface);
184
185     TRACE("(%p)->(count=%lu)\n", This, This->ref);
186
187     return ++(This->ref);
188 }
189
190 static ULONG WINAPI ISF_ControlPanel_fnRelease(IShellFolder2 * iface)
191 {
192     ICOM_THIS(ICPanelImpl, iface);
193
194     TRACE("(%p)->(count=%lu)\n", This, This->ref);
195
196     if (!--(This->ref)) {
197         TRACE("-- destroying IShellFolder(%p)\n", This);
198         if (This->pidlRoot)
199             SHFree(This->pidlRoot);
200         LocalFree((HLOCAL) This);
201         return 0;
202     }
203     return This->ref;
204 }
205
206 /**************************************************************************
207 *       ISF_ControlPanel_fnParseDisplayName
208 */
209 static HRESULT WINAPI
210 ISF_ControlPanel_fnParseDisplayName(IShellFolder2 * iface,
211                                    HWND hwndOwner,
212                                    LPBC pbc,
213                                    LPOLESTR lpszDisplayName,
214                                    DWORD * pchEaten, LPITEMIDLIST * ppidl, DWORD * pdwAttributes)
215 {
216     ICOM_THIS(ICPanelImpl, iface);
217
218     HRESULT hr = E_INVALIDARG;
219
220     FIXME("(%p)->(HWND=%p,%p,%p=%s,%p,pidl=%p,%p)\n",
221            This, hwndOwner, pbc, lpszDisplayName, debugstr_w(lpszDisplayName), pchEaten, ppidl, pdwAttributes);
222
223     *ppidl = 0;
224     if (pchEaten)
225         *pchEaten = 0;
226
227     TRACE("(%p)->(-- ret=0x%08lx)\n", This, hr);
228
229     return hr;
230 }
231
232 static LPITEMIDLIST _ILCreateCPanelApplet(LPCSTR name, LPCSTR displayName,
233  LPCSTR comment, int iconIdx)
234 {
235     PIDLCPanelStruct *p;
236     LPITEMIDLIST pidl;
237     PIDLDATA tmp;
238     int size0 = (char*)&tmp.u.cpanel.szName-(char*)&tmp.u.cpanel;
239     int size = size0;
240     int l;
241
242     tmp.type = 0;
243     tmp.u.cpanel.dummy = 0;
244     tmp.u.cpanel.iconIdx = iconIdx;
245
246     l = strlen(name);
247     size += l+1;
248
249     tmp.u.cpanel.offsDispName = l+1;
250     l = strlen(displayName);
251     size += l+1;
252
253     tmp.u.cpanel.offsComment = tmp.u.cpanel.offsDispName+1+l;
254     l = strlen(comment);
255     size += l+1;
256
257     pidl = SHAlloc(size+4);
258     if (!pidl)
259         return NULL;
260
261     pidl->mkid.cb = size+2;
262     memcpy(pidl->mkid.abID, &tmp, 2+size0);
263
264     p = &((PIDLDATA*)pidl->mkid.abID)->u.cpanel;
265     strcpy(p->szName, name);
266     strcpy(p->szName+tmp.u.cpanel.offsDispName, displayName);
267     strcpy(p->szName+tmp.u.cpanel.offsComment, comment);
268
269     *(WORD*)((char*)pidl+(size+2)) = 0;
270
271     pcheck(pidl);
272
273     return pidl;
274 }
275
276 /**************************************************************************
277  *  _ILGetCPanelPointer()
278  * gets a pointer to the control panel struct stored in the pidl
279  */
280 static PIDLCPanelStruct* _ILGetCPanelPointer(LPCITEMIDLIST pidl)
281 {
282     LPPIDLDATA pdata = _ILGetDataPointer(pidl);
283
284     if (pdata && pdata->type==0)
285         return (PIDLCPanelStruct*)&(pdata->u.cpanel);
286
287     return NULL;
288 }
289
290  /**************************************************************************
291  *              ISF_ControlPanel_fnEnumObjects
292  */
293 static BOOL SHELL_RegisterCPanelApp(IEnumIDList* list, LPCSTR path)
294 {
295     LPITEMIDLIST pidl;
296     CPlApplet* applet;
297     CPanel panel;
298     CPLINFO info;
299     unsigned i;
300     int iconIdx;
301
302     char displayName[MAX_PATH];
303     char comment[MAX_PATH];
304
305     WCHAR wpath[MAX_PATH];
306
307     MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, MAX_PATH);
308
309     panel.first = NULL;
310     applet = Control_LoadApplet(0, wpath, &panel);
311
312     if (applet)
313     {
314         for(i=0; i<applet->count; ++i)
315         {
316             WideCharToMultiByte(CP_ACP, 0, applet->info[i].szName, -1, displayName, MAX_PATH, 0, 0);
317             WideCharToMultiByte(CP_ACP, 0, applet->info[i].szInfo, -1, comment, MAX_PATH, 0, 0);
318
319             applet->proc(0, CPL_INQUIRE, i, (LPARAM)&info);
320
321             if (info.idIcon > 0)
322                 iconIdx = -info.idIcon; /* negative icon index instead of icon number */
323             else
324                 iconIdx = 0;
325
326             pidl = _ILCreateCPanelApplet(path, displayName, comment, iconIdx);
327
328             if (pidl)
329                 AddToEnumList(list, pidl);
330         }
331         Control_UnloadApplet(applet);
332     }
333     return TRUE;
334 }
335
336 static int SHELL_RegisterRegistryCPanelApps(IEnumIDList* list, HKEY hkey_root, LPCSTR szRepPath)
337 {
338     char name[MAX_PATH];
339     char value[MAX_PATH];
340     HKEY hkey;
341
342     int cnt = 0;
343
344     if (RegOpenKeyA(hkey_root, szRepPath, &hkey) == ERROR_SUCCESS)
345     {
346         int idx = 0;
347
348         for(;; ++idx)
349         {
350             DWORD nameLen = MAX_PATH;
351             DWORD valueLen = MAX_PATH;
352
353             if (RegEnumValueA(hkey, idx, name, &nameLen, NULL, NULL, (LPBYTE)&value, &valueLen) != ERROR_SUCCESS)
354                 break;
355
356             if (SHELL_RegisterCPanelApp(list, value))
357                 ++cnt;
358         }
359         RegCloseKey(hkey);
360     }
361
362     return cnt;
363 }
364
365 static int SHELL_RegisterCPanelFolders(IEnumIDList* list, HKEY hkey_root, LPCSTR szRepPath)
366 {
367     char name[MAX_PATH];
368     HKEY hkey;
369
370     int cnt = 0;
371
372     if (RegOpenKeyA(hkey_root, szRepPath, &hkey) == ERROR_SUCCESS)
373     {
374         int idx = 0;
375         for(;; ++idx)
376         {
377             if (RegEnumKeyA(hkey, idx, name, MAX_PATH) != ERROR_SUCCESS)
378                 break;
379
380             if (*name == '{')
381             {
382                 LPITEMIDLIST pidl = _ILCreateGuidFromStrA(name);
383
384                 if (pidl && AddToEnumList(list, pidl))
385                     ++cnt;
386             }
387         }
388
389         RegCloseKey(hkey);
390     }
391
392   return cnt;
393 }
394
395 /**************************************************************************
396  *  CreateCPanelEnumList()
397  */
398 static BOOL CreateCPanelEnumList(
399     IEnumIDList * iface,
400     DWORD dwFlags)
401 {
402     CHAR szPath[MAX_PATH];
403     WIN32_FIND_DATAA wfd;
404     HANDLE hFile;
405
406     TRACE("(%p)->(flags=0x%08lx) \n",iface,dwFlags);
407
408     /* enumerate control panel folders folders */
409     if (dwFlags & SHCONTF_FOLDERS)
410         SHELL_RegisterCPanelFolders(iface, HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ControlPanel\\NameSpace");
411
412     /* enumerate the control panel applets */
413     if (dwFlags & SHCONTF_NONFOLDERS)
414     {
415         LPSTR p;
416
417         GetSystemDirectoryA(szPath, MAX_PATH);
418         p = PathAddBackslashA(szPath);
419         strcpy(p, "*.cpl");
420
421         TRACE("-- (%p)-> enumerate SHCONTF_NONFOLDERS of %s\n",iface,debugstr_a(szPath));
422         hFile = FindFirstFileA(szPath, &wfd);
423
424         if (hFile != INVALID_HANDLE_VALUE)
425         {
426             do
427             {
428                 if (!(dwFlags & SHCONTF_INCLUDEHIDDEN) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
429                     continue;
430
431                 if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
432                     strcpy(p, wfd.cFileName);
433                     SHELL_RegisterCPanelApp((IEnumIDList*)iface, szPath);
434                 }
435             } while(FindNextFileA(hFile, &wfd));
436             FindClose(hFile);
437         }
438
439         SHELL_RegisterRegistryCPanelApps((IEnumIDList*)iface, HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Control Panel\\Cpls");
440         SHELL_RegisterRegistryCPanelApps((IEnumIDList*)iface, HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Control Panel\\Cpls");
441     }
442     return TRUE;
443 }
444
445 /**************************************************************************
446 *               ISF_ControlPanel_fnEnumObjects
447 */
448 static HRESULT WINAPI
449 ISF_ControlPanel_fnEnumObjects(IShellFolder2 * iface, HWND hwndOwner, DWORD dwFlags, LPENUMIDLIST * ppEnumIDList)
450 {
451     ICOM_THIS(ICPanelImpl, iface);
452
453     TRACE("(%p)->(HWND=%p flags=0x%08lx pplist=%p)\n", This, hwndOwner, dwFlags, ppEnumIDList);
454
455     *ppEnumIDList = IEnumIDList_Constructor();
456     if (*ppEnumIDList)
457         CreateCPanelEnumList(*ppEnumIDList, dwFlags);
458
459     TRACE("--(%p)->(new ID List: %p)\n", This, *ppEnumIDList);
460
461     return(*ppEnumIDList) ? S_OK : E_OUTOFMEMORY;
462 }
463
464 /**************************************************************************
465 *               ISF_ControlPanel_fnBindToObject
466 */
467 static HRESULT WINAPI
468 ISF_ControlPanel_fnBindToObject(IShellFolder2 * iface, LPCITEMIDLIST pidl,
469                                LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
470 {
471     ICOM_THIS(ICPanelImpl, iface);
472
473     TRACE("(%p)->(pidl=%p,%p,%s,%p)\n", This, pidl, pbcReserved, shdebugstr_guid(riid), ppvOut);
474
475     return SHELL32_BindToChild(This->pidlRoot, NULL, pidl, riid, ppvOut);
476 }
477
478 /**************************************************************************
479 *       ISF_ControlPanel_fnBindToStorage
480 */
481 static HRESULT WINAPI
482 ISF_ControlPanel_fnBindToStorage(IShellFolder2 * iface,
483                                 LPCITEMIDLIST pidl, LPBC pbcReserved, REFIID riid, LPVOID * ppvOut)
484 {
485     ICOM_THIS(ICPanelImpl, iface);
486
487     FIXME("(%p)->(pidl=%p,%p,%s,%p) stub\n", This, pidl, pbcReserved, shdebugstr_guid(riid), ppvOut);
488
489     *ppvOut = NULL;
490     return E_NOTIMPL;
491 }
492
493 /**************************************************************************
494 *       ISF_ControlPanel_fnCompareIDs
495 */
496
497 static HRESULT WINAPI
498 ISF_ControlPanel_fnCompareIDs(IShellFolder2 * iface, LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
499 {
500     ICOM_THIS(ICPanelImpl, iface);
501
502     int nReturn;
503
504     TRACE("(%p)->(0x%08lx,pidl1=%p,pidl2=%p)\n", This, lParam, pidl1, pidl2);
505     nReturn = SHELL32_CompareIDs(_IShellFolder_(This), lParam, pidl1, pidl2);
506     TRACE("-- %i\n", nReturn);
507     return nReturn;
508 }
509
510 /**************************************************************************
511 *       ISF_ControlPanel_fnCreateViewObject
512 */
513 static HRESULT WINAPI
514 ISF_ControlPanel_fnCreateViewObject(IShellFolder2 * iface, HWND hwndOwner, REFIID riid, LPVOID * ppvOut)
515 {
516     ICOM_THIS(ICPanelImpl, iface);
517
518     LPSHELLVIEW pShellView;
519     HRESULT hr = E_INVALIDARG;
520
521     TRACE("(%p)->(hwnd=%p,%s,%p)\n", This, hwndOwner, shdebugstr_guid(riid), ppvOut);
522
523     if (ppvOut) {
524         *ppvOut = NULL;
525
526         if (IsEqualIID(riid, &IID_IDropTarget)) {
527             WARN("IDropTarget not implemented\n");
528             hr = E_NOTIMPL;
529         } else if (IsEqualIID(riid, &IID_IContextMenu)) {
530             WARN("IContextMenu not implemented\n");
531             hr = E_NOTIMPL;
532         } else if (IsEqualIID(riid, &IID_IShellView)) {
533             pShellView = IShellView_Constructor((IShellFolder *) iface);
534             if (pShellView) {
535                 hr = IShellView_QueryInterface(pShellView, riid, ppvOut);
536                 IShellView_Release(pShellView);
537             }
538         }
539     }
540     TRACE("--(%p)->(interface=%p)\n", This, ppvOut);
541     return hr;
542 }
543
544 /**************************************************************************
545 *  ISF_ControlPanel_fnGetAttributesOf
546 */
547 static HRESULT WINAPI
548 ISF_ControlPanel_fnGetAttributesOf(IShellFolder2 * iface, UINT cidl, LPCITEMIDLIST * apidl, DWORD * rgfInOut)
549 {
550     ICOM_THIS(ICPanelImpl, iface);
551
552     HRESULT hr = S_OK;
553
554     TRACE("(%p)->(cidl=%d apidl=%p mask=0x%08lx)\n", This, cidl, apidl, *rgfInOut);
555
556     if ((!cidl) ||(!apidl) ||(!rgfInOut))
557         return E_INVALIDARG;
558
559     while(cidl > 0 && *apidl) {
560         pdump(*apidl);
561         SHELL32_GetItemAttributes(_IShellFolder_(This), *apidl, rgfInOut);
562         apidl++;
563         cidl--;
564     }
565
566     TRACE("-- result=0x%08lx\n", *rgfInOut);
567     return hr;
568 }
569
570 /**************************************************************************
571 *       ISF_ControlPanel_fnGetUIObjectOf
572 *
573 * PARAMETERS
574 *  HWND           hwndOwner, //[in ] Parent window for any output
575 *  UINT           cidl,      //[in ] array size
576 *  LPCITEMIDLIST* apidl,     //[in ] simple pidl array
577 *  REFIID         riid,      //[in ] Requested Interface
578 *  UINT*          prgfInOut, //[   ] reserved
579 *  LPVOID*        ppvObject) //[out] Resulting Interface
580 *
581 */
582 static HRESULT WINAPI
583 ISF_ControlPanel_fnGetUIObjectOf(IShellFolder2 * iface,
584                                 HWND hwndOwner,
585                                 UINT cidl, LPCITEMIDLIST * apidl, REFIID riid, UINT * prgfInOut, LPVOID * ppvOut)
586 {
587     ICOM_THIS(ICPanelImpl, iface);
588
589     LPITEMIDLIST pidl;
590     IUnknown *pObj = NULL;
591     HRESULT hr = E_INVALIDARG;
592
593     TRACE("(%p)->(%p,%u,apidl=%p,%s,%p,%p)\n",
594            This, hwndOwner, cidl, apidl, shdebugstr_guid(riid), prgfInOut, ppvOut);
595
596     if (ppvOut) {
597         *ppvOut = NULL;
598
599         if (IsEqualIID(riid, &IID_IContextMenu) &&(cidl >= 1)) {
600             pObj = (LPUNKNOWN) ISvItemCm_Constructor((IShellFolder *) iface, This->pidlRoot, apidl, cidl);
601             hr = S_OK;
602         } else if (IsEqualIID(riid, &IID_IDataObject) &&(cidl >= 1)) {
603             pObj = (LPUNKNOWN) IDataObject_Constructor(hwndOwner, This->pidlRoot, apidl, cidl);
604             hr = S_OK;
605         } else if (IsEqualIID(riid, &IID_IExtractIconA) &&(cidl == 1)) {
606             pidl = ILCombine(This->pidlRoot, apidl[0]);
607             pObj = (LPUNKNOWN) IExtractIconA_Constructor(pidl);
608             SHFree(pidl);
609             hr = S_OK;
610         } else if (IsEqualIID(riid, &IID_IExtractIconW) &&(cidl == 1)) {
611             pidl = ILCombine(This->pidlRoot, apidl[0]);
612             pObj = (LPUNKNOWN) IExtractIconW_Constructor(pidl);
613             SHFree(pidl);
614             hr = S_OK;
615         } else if ((IsEqualIID(riid,&IID_IShellLinkW) || IsEqualIID(riid,&IID_IShellLinkA))
616                                 && (cidl == 1)) {
617             pidl = ILCombine(This->pidlRoot, apidl[0]);
618             hr = IShellLink_ConstructFromFile(NULL, riid, pidl,(LPVOID*)&pObj);
619             SHFree(pidl);
620         } else {
621             hr = E_NOINTERFACE;
622         }
623
624         if (SUCCEEDED(hr) && !pObj)
625             hr = E_OUTOFMEMORY;
626
627         *ppvOut = pObj;
628     }
629     TRACE("(%p)->hr=0x%08lx\n", This, hr);
630     return hr;
631 }
632
633 /**************************************************************************
634 *       ISF_ControlPanel_fnGetDisplayNameOf
635 */
636 static HRESULT WINAPI ISF_ControlPanel_fnGetDisplayNameOf(IShellFolder2 * iface, LPCITEMIDLIST pidl, DWORD dwFlags, LPSTRRET strRet)
637 {
638     ICOM_THIS(ICPanelImpl, iface);
639
640     CHAR szPath[MAX_PATH*2];
641     PIDLCPanelStruct* pcpanel;
642
643     *szPath = '\0';
644
645     TRACE("(%p)->(pidl=%p,0x%08lx,%p)\n", This, pidl, dwFlags, strRet);
646     pdump(pidl);
647
648     if (!pidl || !strRet)
649         return E_INVALIDARG;
650
651     pcpanel = _ILGetCPanelPointer(pidl);
652
653     if (pcpanel) {
654         lstrcpyA(szPath, pcpanel->szName+pcpanel->offsDispName);
655
656         if (!(dwFlags & SHGDN_FORPARSING))
657             FIXME("retrieve display name from control panel app\n");
658     }
659     /* take names of special folders only if its only this folder */
660     else if (_ILIsSpecialFolder(pidl)) {
661         BOOL bSimplePidl = _ILIsPidlSimple(pidl);
662
663         if (bSimplePidl) {
664             _ILSimpleGetText(pidl, szPath, MAX_PATH);   /* append my own path */
665         } else {
666             FIXME("special pidl\n");
667         }
668
669         if ((dwFlags & SHGDN_FORPARSING) && !bSimplePidl) { /* go deeper if needed */
670             int len = 0;
671
672             PathAddBackslashA(szPath); /*FIXME*/
673             len = lstrlenA(szPath);
674
675             if (!SUCCEEDED
676               (SHELL32_GetDisplayNameOfChild(iface, pidl, dwFlags | SHGDN_INFOLDER, szPath + len, MAX_PATH - len)))
677                 return E_OUTOFMEMORY;
678         }
679     }
680
681     strRet->uType = STRRET_CSTR;
682     lstrcpynA(strRet->u.cStr, szPath, MAX_PATH);
683
684     TRACE("--(%p)->(%s)\n", This, szPath);
685     return S_OK;
686 }
687
688 /**************************************************************************
689 *  ISF_ControlPanel_fnSetNameOf
690 *  Changes the name of a file object or subfolder, possibly changing its item
691 *  identifier in the process.
692 *
693 * PARAMETERS
694 *  HWND          hwndOwner,  //[in ] Owner window for output
695 *  LPCITEMIDLIST pidl,       //[in ] simple pidl of item to change
696 *  LPCOLESTR     lpszName,   //[in ] the items new display name
697 *  DWORD         dwFlags,    //[in ] SHGNO formatting flags
698 *  LPITEMIDLIST* ppidlOut)   //[out] simple pidl returned
699 */
700 static HRESULT WINAPI ISF_ControlPanel_fnSetNameOf(IShellFolder2 * iface, HWND hwndOwner, LPCITEMIDLIST pidl,   /*simple pidl */
701                                                   LPCOLESTR lpName, DWORD dwFlags, LPITEMIDLIST * pPidlOut)
702 {
703     ICOM_THIS(ICPanelImpl, iface);
704     FIXME("(%p)->(%p,pidl=%p,%s,%lu,%p)\n", This, hwndOwner, pidl, debugstr_w(lpName), dwFlags, pPidlOut);
705     return E_FAIL;
706 }
707
708 static HRESULT WINAPI ISF_ControlPanel_fnGetDefaultSearchGUID(IShellFolder2 * iface, GUID * pguid)
709 {
710     ICOM_THIS(ICPanelImpl, iface);
711     FIXME("(%p)\n", This);
712     return E_NOTIMPL;
713 }
714 static HRESULT WINAPI ISF_ControlPanel_fnEnumSearches(IShellFolder2 * iface, IEnumExtraSearch ** ppenum)
715 {
716     ICOM_THIS(ICPanelImpl, iface);
717     FIXME("(%p)\n", This);
718     return E_NOTIMPL;
719 }
720 static HRESULT WINAPI ISF_ControlPanel_fnGetDefaultColumn(IShellFolder2 * iface, DWORD dwRes, ULONG * pSort, ULONG * pDisplay)
721 {
722     ICOM_THIS(ICPanelImpl, iface);
723
724     TRACE("(%p)\n", This);
725
726     if (pSort) *pSort = 0;
727     if (pDisplay) *pDisplay = 0;
728     return S_OK;
729 }
730 static HRESULT WINAPI ISF_ControlPanel_fnGetDefaultColumnState(IShellFolder2 * iface, UINT iColumn, DWORD * pcsFlags)
731 {
732     ICOM_THIS(ICPanelImpl, iface);
733
734     TRACE("(%p)\n", This);
735
736     if (!pcsFlags || iColumn >= CONROLPANELSHELLVIEWCOLUMNS) return E_INVALIDARG;
737     *pcsFlags = ControlPanelSFHeader[iColumn].pcsFlags;
738     return S_OK;
739 }
740 static HRESULT WINAPI ISF_ControlPanel_fnGetDetailsEx(IShellFolder2 * iface, LPCITEMIDLIST pidl, const SHCOLUMNID * pscid, VARIANT * pv)
741 {
742     ICOM_THIS(ICPanelImpl, iface);
743     FIXME("(%p)\n", This);
744     return E_NOTIMPL;
745 }
746
747 static HRESULT WINAPI ISF_ControlPanel_fnGetDetailsOf(IShellFolder2 * iface, LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS * psd)
748 {
749     ICOM_THIS(ICPanelImpl, iface);
750     HRESULT hr;
751
752     TRACE("(%p)->(%p %i %p)\n", This, pidl, iColumn, psd);
753
754     if (!psd || iColumn >= CONROLPANELSHELLVIEWCOLUMNS)
755         return E_INVALIDARG;
756
757     if (!pidl) {
758         psd->fmt = ControlPanelSFHeader[iColumn].fmt;
759         psd->cxChar = ControlPanelSFHeader[iColumn].cxChar;
760         psd->str.uType = STRRET_CSTR;
761         LoadStringA(shell32_hInstance, ControlPanelSFHeader[iColumn].colnameid, psd->str.u.cStr, MAX_PATH);
762         return S_OK;
763     } else {
764         psd->str.u.cStr[0] = 0x00;
765         psd->str.uType = STRRET_CSTR;
766         switch(iColumn) {
767         case 0:         /* name */
768             hr = IShellFolder_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL | SHGDN_INFOLDER, &psd->str);
769             break;
770         case 1:         /* comment */
771             _ILGetFileType(pidl, psd->str.u.cStr, MAX_PATH);
772             break;
773         }
774         hr = S_OK;
775     }
776
777     return hr;
778 }
779 static HRESULT WINAPI ISF_ControlPanel_fnMapColumnToSCID(IShellFolder2 * iface, UINT column, SHCOLUMNID * pscid)
780 {
781     ICOM_THIS(ICPanelImpl, iface);
782     FIXME("(%p)\n", This);
783     return E_NOTIMPL;
784 }
785
786 static ICOM_VTABLE(IShellFolder2) vt_ShellFolder2 =
787 {
788     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
789
790     ISF_ControlPanel_fnQueryInterface,
791     ISF_ControlPanel_fnAddRef,
792     ISF_ControlPanel_fnRelease,
793     ISF_ControlPanel_fnParseDisplayName,
794     ISF_ControlPanel_fnEnumObjects,
795     ISF_ControlPanel_fnBindToObject,
796     ISF_ControlPanel_fnBindToStorage,
797     ISF_ControlPanel_fnCompareIDs,
798     ISF_ControlPanel_fnCreateViewObject,
799     ISF_ControlPanel_fnGetAttributesOf,
800     ISF_ControlPanel_fnGetUIObjectOf,
801     ISF_ControlPanel_fnGetDisplayNameOf,
802     ISF_ControlPanel_fnSetNameOf,
803
804     /* ShellFolder2 */
805     ISF_ControlPanel_fnGetDefaultSearchGUID,
806     ISF_ControlPanel_fnEnumSearches,
807     ISF_ControlPanel_fnGetDefaultColumn,
808     ISF_ControlPanel_fnGetDefaultColumnState,
809     ISF_ControlPanel_fnGetDetailsEx,
810     ISF_ControlPanel_fnGetDetailsOf,
811     ISF_ControlPanel_fnMapColumnToSCID
812 };
813
814 /************************************************************************
815  *      ICPanel_PersistFolder2_QueryInterface
816  */
817 static HRESULT WINAPI ICPanel_PersistFolder2_QueryInterface(IPersistFolder2 * iface, REFIID iid, LPVOID * ppvObject)
818 {
819     _ICOM_THIS_From_IPersistFolder2(ICPanelImpl, iface);
820
821     TRACE("(%p)\n", This);
822
823     return IUnknown_QueryInterface(_IUnknown_(This), iid, ppvObject);
824 }
825
826 /************************************************************************
827  *      ICPanel_PersistFolder2_AddRef
828  */
829 static ULONG WINAPI ICPanel_PersistFolder2_AddRef(IPersistFolder2 * iface)
830 {
831     _ICOM_THIS_From_IPersistFolder2(ICPanelImpl, iface);
832
833     TRACE("(%p)->(count=%lu)\n", This, This->ref);
834
835     return IUnknown_AddRef(_IUnknown_(This));
836 }
837
838 /************************************************************************
839  *      ISFPersistFolder_Release
840  */
841 static ULONG WINAPI ICPanel_PersistFolder2_Release(IPersistFolder2 * iface)
842 {
843     _ICOM_THIS_From_IPersistFolder2(ICPanelImpl, iface);
844
845     TRACE("(%p)->(count=%lu)\n", This, This->ref);
846
847     return IUnknown_Release(_IUnknown_(This));
848 }
849
850 /************************************************************************
851  *      ICPanel_PersistFolder2_GetClassID
852  */
853 static HRESULT WINAPI ICPanel_PersistFolder2_GetClassID(IPersistFolder2 * iface, CLSID * lpClassId)
854 {
855     _ICOM_THIS_From_IPersistFolder2(ICPanelImpl, iface);
856
857     TRACE("(%p)\n", This);
858
859     if (!lpClassId)
860         return E_POINTER;
861     *lpClassId = CLSID_ControlPanel;
862
863     return S_OK;
864 }
865
866 /************************************************************************
867  *      ICPanel_PersistFolder2_Initialize
868  *
869  * NOTES: it makes no sense to change the pidl
870  */
871 static HRESULT WINAPI ICPanel_PersistFolder2_Initialize(IPersistFolder2 * iface, LPCITEMIDLIST pidl)
872 {
873     _ICOM_THIS_From_IPersistFolder2(ICPanelImpl, iface);
874     TRACE("(%p)->(%p)\n", This, pidl);
875     return E_NOTIMPL;
876 }
877
878 /**************************************************************************
879  *      IPersistFolder2_fnGetCurFolder
880  */
881 static HRESULT WINAPI ICPanel_PersistFolder2_GetCurFolder(IPersistFolder2 * iface, LPITEMIDLIST * pidl)
882 {
883     _ICOM_THIS_From_IPersistFolder2(ICPanelImpl, iface);
884
885     TRACE("(%p)->(%p)\n", This, pidl);
886
887     if (!pidl)
888         return E_POINTER;
889     *pidl = ILClone(This->pidlRoot);
890     return S_OK;
891 }
892
893 static ICOM_VTABLE(IPersistFolder2) vt_PersistFolder2 =
894 {
895     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
896
897     ICPanel_PersistFolder2_QueryInterface,
898     ICPanel_PersistFolder2_AddRef,
899     ICPanel_PersistFolder2_Release,
900     ICPanel_PersistFolder2_GetClassID,
901     ICPanel_PersistFolder2_Initialize,
902     ICPanel_PersistFolder2_GetCurFolder
903 };
904
905 HRESULT WINAPI CPanel_GetIconLocationA(LPITEMIDLIST pidl, LPSTR szIconFile, UINT cchMax, int* piIndex)
906 {
907     PIDLCPanelStruct* pcpanel = _ILGetCPanelPointer(pidl);
908
909     if (!pcpanel)
910         return E_INVALIDARG;
911
912     lstrcpyA(szIconFile, pcpanel->szName);
913     *piIndex = pcpanel->iconIdx!=-1? pcpanel->iconIdx: 0;
914
915     return S_OK;
916 }
917
918 HRESULT WINAPI CPanel_GetIconLocationW(LPITEMIDLIST pidl, LPWSTR szIconFile, UINT cchMax, int* piIndex)
919 {
920     PIDLCPanelStruct* pcpanel = _ILGetCPanelPointer(pidl);
921
922     if (!pcpanel)
923         return E_INVALIDARG;
924
925     MultiByteToWideChar(CP_ACP, 0, pcpanel->szName, -1, szIconFile, cchMax);
926     *piIndex = pcpanel->iconIdx!=-1? pcpanel->iconIdx: 0;
927
928     return S_OK;
929 }
930
931
932 /**************************************************************************
933 * IShellExecuteHookW Implementation
934 */
935
936 static HRESULT WINAPI IShellExecuteHookW_fnQueryInterface(IShellExecuteHookW* iface, REFIID riid, void** ppvObject)
937 {
938     _ICOM_THIS_From_IShellExecuteHookW(ICPanelImpl, iface);
939
940     TRACE("(%p)->(count=%lu)\n", This, This->ref);
941
942     return IUnknown_QueryInterface(This->pUnkOuter, riid, ppvObject);
943 }
944
945 static ULONG STDMETHODCALLTYPE IShellExecuteHookW_fnAddRef(IShellExecuteHookW* iface)
946 {
947     _ICOM_THIS_From_IShellExecuteHookW(ICPanelImpl, iface);
948
949     TRACE("(%p)->(count=%lu)\n", This, This->ref);
950
951     return IUnknown_AddRef(This->pUnkOuter);
952 }
953
954 static ULONG STDMETHODCALLTYPE IShellExecuteHookW_fnRelease(IShellExecuteHookW* iface)
955 {
956     _ICOM_THIS_From_IShellExecuteHookW(ICPanelImpl, iface);
957
958     TRACE("(%p)\n", This);
959
960     return IUnknown_Release(This->pUnkOuter);
961 }
962
963 static HRESULT WINAPI IShellExecuteHookW_fnExecute(IShellExecuteHookW* iface, LPSHELLEXECUTEINFOW psei)
964 {
965     ICOM_THIS(ICPanelImpl, iface);
966
967     SHELLEXECUTEINFOW sei_tmp;
968     PIDLCPanelStruct* pcpanel;
969     WCHAR path[MAX_PATH];
970     BOOL ret;
971     int l;
972
973     TRACE("(%p)->execute(%p)\n", This, psei);
974
975     if (!psei)
976         return E_INVALIDARG;
977
978     pcpanel = _ILGetCPanelPointer(ILFindLastID(psei->lpIDList));
979
980     if (!pcpanel)
981         return E_INVALIDARG;
982
983     path[0] = '\"';
984     l = 1 + MultiByteToWideChar(CP_ACP, 0, pcpanel->szName, -1, path+1, MAX_PATH);
985
986     /* pass applet name to Control_RunDLL to distinguish between applets in one .cpl file */
987     path[++l] = '"';
988     path[++l] = ' ';
989
990     MultiByteToWideChar(CP_ACP, 0, pcpanel->szName+pcpanel->offsDispName, -1, path+l, MAX_PATH);
991
992     memcpy(&sei_tmp, psei, sizeof(sei_tmp));
993     sei_tmp.lpFile = path;
994     sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
995
996     ret = ShellExecuteExW(&sei_tmp);
997     if (ret)
998         return S_OK;
999     else
1000         return S_FALSE;
1001 }
1002
1003 static ICOM_VTABLE(IShellExecuteHookW) vt_ShellExecuteHookW =
1004 {
1005     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1006
1007     IShellExecuteHookW_fnQueryInterface,
1008     IShellExecuteHookW_fnAddRef,
1009     IShellExecuteHookW_fnRelease,
1010
1011     IShellExecuteHookW_fnExecute
1012 };
1013
1014
1015 /**************************************************************************
1016 * IShellExecuteHookA Implementation
1017 */
1018
1019 static HRESULT WINAPI IShellExecuteHookA_fnQueryInterface(IShellExecuteHookA* iface, REFIID riid, void** ppvObject)
1020 {
1021     _ICOM_THIS_From_IShellExecuteHookA(ICPanelImpl, iface);
1022
1023     TRACE("(%p)->(count=%lu)\n", This, This->ref);
1024
1025     return IUnknown_QueryInterface(This->pUnkOuter, riid, ppvObject);
1026 }
1027
1028 static ULONG STDMETHODCALLTYPE IShellExecuteHookA_fnAddRef(IShellExecuteHookA* iface)
1029 {
1030     _ICOM_THIS_From_IShellExecuteHookA(ICPanelImpl, iface);
1031
1032     TRACE("(%p)->(count=%lu)\n", This, This->ref);
1033
1034     return IUnknown_AddRef(This->pUnkOuter);
1035 }
1036
1037 static ULONG STDMETHODCALLTYPE IShellExecuteHookA_fnRelease(IShellExecuteHookA* iface)
1038 {
1039     _ICOM_THIS_From_IShellExecuteHookA(ICPanelImpl, iface);
1040
1041     TRACE("(%p)\n", This);
1042
1043     return IUnknown_Release(This->pUnkOuter);
1044 }
1045
1046 static HRESULT WINAPI IShellExecuteHookA_fnExecute(IShellExecuteHookA* iface, LPSHELLEXECUTEINFOA psei)
1047 {
1048     ICOM_THIS(ICPanelImpl, iface);
1049
1050     SHELLEXECUTEINFOA sei_tmp;
1051     PIDLCPanelStruct* pcpanel;
1052     char path[MAX_PATH];
1053     BOOL ret;
1054
1055     TRACE("(%p)->execute(%p)\n", This, psei);
1056
1057     if (!psei)
1058         return E_INVALIDARG;
1059
1060     pcpanel = _ILGetCPanelPointer(ILFindLastID(psei->lpIDList));
1061
1062     if (!pcpanel)
1063         return E_INVALIDARG;
1064
1065     path[0] = '\"';
1066     lstrcpyA(path+1, pcpanel->szName);
1067
1068     /* pass applet name to Control_RunDLL to distinguish between applets in one .cpl file */
1069     lstrcatA(path, "\" ");
1070     lstrcatA(path, pcpanel->szName+pcpanel->offsDispName);
1071
1072     memcpy(&sei_tmp, psei, sizeof(sei_tmp));
1073     sei_tmp.lpFile = path;
1074     sei_tmp.fMask &= ~SEE_MASK_INVOKEIDLIST;
1075
1076     ret = ShellExecuteExA(&sei_tmp);
1077     if (ret)
1078         return S_OK;
1079     else
1080         return S_FALSE;
1081 }
1082
1083 static ICOM_VTABLE(IShellExecuteHookA) vt_ShellExecuteHookA =
1084 {
1085     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1086
1087     IShellExecuteHookA_fnQueryInterface,
1088     IShellExecuteHookA_fnAddRef,
1089     IShellExecuteHookA_fnRelease,
1090
1091     IShellExecuteHookA_fnExecute
1092 };