Authors: Rob Shearman <rob@codeweavers.com>, Mike Hearn <mh@codeweavers.com>
[wine] / dlls / shell32 / folders.c
1 /*
2  *      Copyright 1997  Marcus Meissner
3  *      Copyright 1998  Juergen Schmied
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include "config.h"
21 #include "wine/port.h"
22
23 #include <stdlib.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #define COBJMACROS
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "objbase.h"
34 #include "undocshell.h"
35 #include "shlguid.h"
36 #include "winreg.h"
37
38 #include "wine/debug.h"
39
40 #include "pidl.h"
41 #include "shell32_main.h"
42 #include "shfldr.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(shell);
45
46 /***********************************************************************
47 *   IExtractIconW implementation
48 */
49 typedef struct
50 {
51         IExtractIconWVtbl *lpVtbl;
52         DWORD              ref;
53         IPersistFileVtbl  *lpvtblPersistFile;
54         IExtractIconAVtbl *lpvtblExtractIconA;
55         LPITEMIDLIST       pidl;
56 } IExtractIconWImpl;
57
58 static struct IExtractIconAVtbl eiavt;
59 static struct IExtractIconWVtbl eivt;
60 static struct IPersistFileVtbl pfvt;
61
62 #define _IPersistFile_Offset ((int)(&(((IExtractIconWImpl*)0)->lpvtblPersistFile)))
63 #define _ICOM_THIS_From_IPersistFile(class, name) class* This = (class*)(((char*)name)-_IPersistFile_Offset);
64
65 #define _IExtractIconA_Offset ((int)(&(((IExtractIconWImpl*)0)->lpvtblExtractIconA)))
66 #define _ICOM_THIS_From_IExtractIconA(class, name) class* This = (class*)(((char*)name)-_IExtractIconA_Offset);
67
68 /**************************************************************************
69 *  IExtractIconW_Constructor
70 */
71 IExtractIconW* IExtractIconW_Constructor(LPCITEMIDLIST pidl)
72 {
73         IExtractIconWImpl* ei;
74         
75         TRACE("%p\n", pidl);
76
77         ei = (IExtractIconWImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IExtractIconWImpl));
78         ei->ref=1;
79         ei->lpVtbl = &eivt;
80         ei->lpvtblPersistFile = &pfvt;
81         ei->lpvtblExtractIconA = &eiavt;
82         ei->pidl=ILClone(pidl);
83
84         pdump(pidl);
85
86         TRACE("(%p)\n", ei);
87         return (IExtractIconW *)ei;
88 }
89 /**************************************************************************
90  *  IExtractIconW_QueryInterface
91  */
92 static HRESULT WINAPI IExtractIconW_fnQueryInterface(IExtractIconW *iface, REFIID riid, LPVOID *ppvObj)
93 {
94         IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
95
96         TRACE("(%p)->(\n\tIID:\t%s,%p)\n", This, debugstr_guid(riid), ppvObj);
97
98         *ppvObj = NULL;
99
100         if (IsEqualIID(riid, &IID_IUnknown))                            /*IUnknown*/
101         {
102           *ppvObj = This;
103         }
104         else if (IsEqualIID(riid, &IID_IPersistFile))   /*IExtractIcon*/
105         {
106           *ppvObj = (IPersistFile*)&(This->lpvtblPersistFile);
107         }
108         else if (IsEqualIID(riid, &IID_IExtractIconA))  /*IExtractIcon*/
109         {
110           *ppvObj = (IExtractIconA*)&(This->lpvtblExtractIconA);
111         }
112         else if (IsEqualIID(riid, &IID_IExtractIconW))  /*IExtractIcon*/
113         {
114           *ppvObj = (IExtractIconW*)This;
115         }
116
117         if(*ppvObj)
118         {
119           IExtractIconW_AddRef((IExtractIconW*) *ppvObj);
120           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
121           return S_OK;
122         }
123         TRACE("-- Interface: E_NOINTERFACE\n");
124         return E_NOINTERFACE;
125 }
126
127 /**************************************************************************
128 *  IExtractIconW_AddRef
129 */
130 static ULONG WINAPI IExtractIconW_fnAddRef(IExtractIconW * iface)
131 {
132         IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
133         ULONG refCount = InterlockedIncrement(&This->ref);
134
135         TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
136
137         return refCount;
138 }
139 /**************************************************************************
140 *  IExtractIconW_Release
141 */
142 static ULONG WINAPI IExtractIconW_fnRelease(IExtractIconW * iface)
143 {
144         IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
145         ULONG refCount = InterlockedDecrement(&This->ref);
146
147         TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
148
149         if (!refCount)
150         {
151           TRACE(" destroying IExtractIcon(%p)\n",This);
152           SHFree(This->pidl);
153           HeapFree(GetProcessHeap(),0,This);
154           return 0;
155         }
156         return refCount;
157 }
158
159 static HRESULT getIconLocationForFolder(IExtractIconW *iface, UINT uFlags,
160  LPWSTR szIconFile, UINT cchMax, int *piIndex, UINT *pwFlags)
161 {
162     IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
163     DWORD dwNr;
164     WCHAR wszPath[MAX_PATH];
165     WCHAR wszCLSIDValue[CHARS_IN_GUID];
166     static const WCHAR shellClassInfo[] = { '.','S','h','e','l','l','C','l','a','s','s','I','n','f','o',0 };
167     static const WCHAR iconFile[] = { 'I','c','o','n','F','i','l','e',0 };
168     static const WCHAR clsid[] = { 'C','L','S','I','D',0 };
169     static const WCHAR clsid2[] = { 'C','L','S','I','D','2',0 };
170     static const WCHAR iconIndex[] = { 'I','c','o','n','I','n','d','e','x',0 };
171
172     if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, iconFile,
173         wszPath, MAX_PATH))
174     {
175         WCHAR wszIconIndex[10];
176         SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, iconIndex,
177             wszIconIndex, 10);
178         *piIndex = atoiW(wszIconIndex);
179     }
180     else if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, clsid,
181         wszCLSIDValue, CHARS_IN_GUID) &&
182         HCR_GetDefaultIconW(wszCLSIDValue, szIconFile, cchMax, &dwNr))
183     {
184        *piIndex = dwNr;
185     }
186     else if (SHELL32_GetCustomFolderAttribute(This->pidl, shellClassInfo, clsid2,
187         wszCLSIDValue, CHARS_IN_GUID) &&
188         HCR_GetDefaultIconW(wszCLSIDValue, szIconFile, cchMax, &dwNr))
189     {
190        *piIndex = dwNr;
191     }
192     else
193     {
194         static const WCHAR folder[] = { 'F','o','l','d','e','r',0 };
195
196         if (!HCR_GetDefaultIconW(folder, szIconFile, cchMax, &dwNr))
197         {
198             lstrcpynW(szIconFile, swShell32Name, cchMax);
199             dwNr = 3;
200         }
201         *piIndex = (uFlags & GIL_OPENICON) ? dwNr + 1 : dwNr;
202     }
203     return S_OK;
204 }
205
206 WCHAR swShell32Name[MAX_PATH];
207
208 /**************************************************************************
209 *  IExtractIconW_GetIconLocation
210 *
211 * mapping filetype to icon
212 */
213 static HRESULT WINAPI IExtractIconW_fnGetIconLocation(
214         IExtractIconW * iface,
215         UINT uFlags,            /* GIL_ flags */
216         LPWSTR szIconFile,
217         UINT cchMax,
218         int * piIndex,
219         UINT * pwFlags)         /* returned GIL_ flags */
220 {
221         IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
222
223         char    sTemp[MAX_PATH];
224         DWORD   dwNr;
225         GUID const * riid;
226         LPITEMIDLIST    pSimplePidl = ILFindLastID(This->pidl);
227
228         TRACE("(%p) (flags=%u %p %u %p %p)\n", This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
229
230         if (pwFlags)
231           *pwFlags = 0;
232
233         if (_ILIsDesktop(pSimplePidl))
234         {
235           lstrcpynW(szIconFile, swShell32Name, cchMax);
236           *piIndex = 34;
237         }
238
239         /* my computer and other shell extensions */
240         else if ((riid = _ILGetGUIDPointer(pSimplePidl)))
241         {
242           static const WCHAR fmt[] = { 'C','L','S','I','D','\\',
243        '{','%','0','8','l','x','-','%','0','4','x','-','%','0','4','x','-',
244        '%','0','2','x','%','0','2','x','-','%','0','2','x', '%','0','2','x',
245        '%','0','2','x','%','0','2','x','%','0','2','x','%','0','2','x','}',0 };
246           WCHAR xriid[50];
247
248           sprintfW(xriid, fmt,
249                   riid->Data1, riid->Data2, riid->Data3,
250                   riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
251                   riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7]);
252
253           if (HCR_GetDefaultIconW(xriid, szIconFile, cchMax, &dwNr))
254           {
255             *piIndex = dwNr;
256           }
257           else
258           {
259             lstrcpynW(szIconFile, swShell32Name, cchMax);
260             *piIndex = 15;
261           }
262         }
263
264         else if (_ILIsDrive (pSimplePidl))
265         {
266           static const WCHAR drive[] = { 'D','r','i','v','e',0 };
267
268           int icon_idx = -1;
269
270           if (_ILGetDrive(pSimplePidl, sTemp, MAX_PATH))
271           {
272                 switch(GetDriveTypeA(sTemp))
273                 {
274                   case DRIVE_REMOVABLE:   icon_idx =  5;        break;
275                   case DRIVE_CDROM:               icon_idx = 11;        break;
276                   case DRIVE_REMOTE:      icon_idx =  9;        break;
277                   case DRIVE_RAMDISK:     icon_idx = 12;        break;
278                 }
279           }
280
281           if (icon_idx != -1)
282           {
283                 lstrcpynW(szIconFile, swShell32Name, cchMax);
284                 *piIndex = icon_idx;
285           }
286           else
287           {
288                 if (HCR_GetDefaultIconW(drive, szIconFile, cchMax, &dwNr))
289                 {
290                   *piIndex = dwNr;
291                 }
292                 else
293                 {
294                   lstrcpynW(szIconFile, swShell32Name, cchMax);
295                   *piIndex = 8;
296                 }
297           }
298         }
299         else if (_ILIsFolder (pSimplePidl))
300         {
301           getIconLocationForFolder(iface, uFlags, szIconFile, cchMax, piIndex,
302            pwFlags);
303         }
304         else
305         {
306           BOOL found = FALSE;
307
308           if (_ILIsCPanelStruct(pSimplePidl))
309           {
310             if (SUCCEEDED(CPanel_GetIconLocationW(pSimplePidl, szIconFile, cchMax, piIndex)))
311                 found = TRUE;
312           }
313           else if (_ILGetExtension(pSimplePidl, sTemp, MAX_PATH))
314           {
315             if (HCR_MapTypeToValueA(sTemp, sTemp, MAX_PATH, TRUE)
316                 && HCR_GetDefaultIconA(sTemp, sTemp, MAX_PATH, &dwNr))
317             {
318               if (!lstrcmpA("%1", sTemp))               /* icon is in the file */
319               {
320                 SHGetPathFromIDListW(This->pidl, szIconFile);
321                 *piIndex = 0;
322               }
323               else
324               {
325                 MultiByteToWideChar(CP_ACP, 0, sTemp, -1, szIconFile, cchMax);
326                 *piIndex = dwNr;
327               }
328
329               found = TRUE;
330             }
331             else if (!strcasecmp(sTemp, "lnkfile"))
332             {
333               /* extract icon from shell shortcut */
334               IShellFolder* dsf;
335               IShellLinkW* psl;
336
337               if (SUCCEEDED(SHGetDesktopFolder(&dsf)))
338               {
339                 HRESULT hr = IShellFolder_GetUIObjectOf(dsf, NULL, 1, (LPCITEMIDLIST*)&This->pidl, &IID_IShellLinkW, NULL, (LPVOID*)&psl);
340
341                 if (SUCCEEDED(hr))
342                 {
343                   hr = IShellLinkW_GetIconLocation(psl, szIconFile, MAX_PATH, piIndex);
344
345                   if (SUCCEEDED(hr) && *szIconFile)
346                     found = TRUE;
347
348                   IShellLinkW_Release(psl);
349                 }
350
351                 IShellFolder_Release(dsf);
352               }
353             }
354           }
355
356           if (!found)                                   /* default icon */
357           {
358             lstrcpynW(szIconFile, swShell32Name, cchMax);
359             *piIndex = 0;
360           }
361         }
362
363         TRACE("-- %s %x\n", debugstr_w(szIconFile), *piIndex);
364         return NOERROR;
365 }
366
367 /**************************************************************************
368 *  IExtractIconW_Extract
369 */
370 static HRESULT WINAPI IExtractIconW_fnExtract(IExtractIconW * iface, LPCWSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
371 {
372         IExtractIconWImpl *This = (IExtractIconWImpl *)iface;
373
374         FIXME("(%p) (file=%p index=%u %p %p size=%u) semi-stub\n", This, debugstr_w(pszFile), nIconIndex, phiconLarge, phiconSmall, nIconSize);
375
376         if (phiconLarge)
377           *phiconLarge = ImageList_GetIcon(ShellBigIconList, nIconIndex, ILD_TRANSPARENT);
378
379         if (phiconSmall)
380           *phiconSmall = ImageList_GetIcon(ShellSmallIconList, nIconIndex, ILD_TRANSPARENT);
381
382         return S_OK;
383 }
384
385 static struct IExtractIconWVtbl eivt =
386 {
387         IExtractIconW_fnQueryInterface,
388         IExtractIconW_fnAddRef,
389         IExtractIconW_fnRelease,
390         IExtractIconW_fnGetIconLocation,
391         IExtractIconW_fnExtract
392 };
393
394 /**************************************************************************
395 *  IExtractIconA_Constructor
396 */
397 IExtractIconA* IExtractIconA_Constructor(LPCITEMIDLIST pidl)
398 {
399         IExtractIconWImpl *This = (IExtractIconWImpl *)IExtractIconW_Constructor(pidl);
400         IExtractIconA *eia = (IExtractIconA *)&This->lpvtblExtractIconA;
401         
402         TRACE("(%p)->(%p)\n", This, eia);
403         return eia;
404 }
405 /**************************************************************************
406  *  IExtractIconA_QueryInterface
407  */
408 static HRESULT WINAPI IExtractIconA_fnQueryInterface(IExtractIconA * iface, REFIID riid, LPVOID *ppvObj)
409 {
410         _ICOM_THIS_From_IExtractIconA(IExtractIconW, iface);
411
412         return IExtractIconW_QueryInterface(This, riid, ppvObj);
413 }
414
415 /**************************************************************************
416 *  IExtractIconA_AddRef
417 */
418 static ULONG WINAPI IExtractIconA_fnAddRef(IExtractIconA * iface)
419 {
420         _ICOM_THIS_From_IExtractIconA(IExtractIconW, iface);
421
422         return IExtractIconW_AddRef(This);
423 }
424 /**************************************************************************
425 *  IExtractIconA_Release
426 */
427 static ULONG WINAPI IExtractIconA_fnRelease(IExtractIconA * iface)
428 {
429         _ICOM_THIS_From_IExtractIconA(IExtractIconW, iface);
430
431         return IExtractIconW_AddRef(This);
432 }
433 /**************************************************************************
434 *  IExtractIconA_GetIconLocation
435 *
436 * mapping filetype to icon
437 */
438 static HRESULT WINAPI IExtractIconA_fnGetIconLocation(
439         IExtractIconA * iface,
440         UINT uFlags,
441         LPSTR szIconFile,
442         UINT cchMax,
443         int * piIndex,
444         UINT * pwFlags)
445 {
446         HRESULT ret;
447         LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, cchMax * sizeof(WCHAR));
448         _ICOM_THIS_From_IExtractIconA(IExtractIconW, iface);
449         
450         TRACE("(%p) (flags=%u %p %u %p %p)\n", This, uFlags, szIconFile, cchMax, piIndex, pwFlags);
451
452         ret = IExtractIconW_GetIconLocation(This, uFlags, lpwstrFile, cchMax, piIndex, pwFlags);
453         WideCharToMultiByte(CP_ACP, 0, lpwstrFile, -1, szIconFile, cchMax, NULL, NULL);
454         HeapFree(GetProcessHeap(), 0, lpwstrFile);
455
456         TRACE("-- %s %x\n", szIconFile, *piIndex);
457         return ret;
458 }
459 /**************************************************************************
460 *  IExtractIconA_Extract
461 */
462 static HRESULT WINAPI IExtractIconA_fnExtract(IExtractIconA * iface, LPCSTR pszFile, UINT nIconIndex, HICON *phiconLarge, HICON *phiconSmall, UINT nIconSize)
463 {
464         HRESULT ret;
465         INT len = MultiByteToWideChar(CP_ACP, 0, pszFile, -1, NULL, 0);
466         LPWSTR lpwstrFile = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
467         _ICOM_THIS_From_IExtractIconA(IExtractIconW, iface);
468
469         TRACE("(%p) (file=%p index=%u %p %p size=%u)\n", This, pszFile, nIconIndex, phiconLarge, phiconSmall, nIconSize);
470
471         MultiByteToWideChar(CP_ACP, 0, pszFile, -1, lpwstrFile, len);
472         ret = IExtractIconW_Extract(This, lpwstrFile, nIconIndex, phiconLarge, phiconSmall, nIconSize);
473         HeapFree(GetProcessHeap(), 0, lpwstrFile);
474         return ret;
475 }
476
477 static struct IExtractIconAVtbl eiavt =
478 {
479         IExtractIconA_fnQueryInterface,
480         IExtractIconA_fnAddRef,
481         IExtractIconA_fnRelease,
482         IExtractIconA_fnGetIconLocation,
483         IExtractIconA_fnExtract
484 };
485
486 /************************************************************************
487  * IEIPersistFile_QueryInterface (IUnknown)
488  */
489 static HRESULT WINAPI IEIPersistFile_fnQueryInterface(
490         IPersistFile    *iface,
491         REFIID          iid,
492         LPVOID          *ppvObj)
493 {
494         _ICOM_THIS_From_IPersistFile(IExtractIconW, iface);
495
496         return IExtractIconW_QueryInterface(This, iid, ppvObj);
497 }
498
499 /************************************************************************
500  * IEIPersistFile_AddRef (IUnknown)
501  */
502 static ULONG WINAPI IEIPersistFile_fnAddRef(
503         IPersistFile    *iface)
504 {
505         _ICOM_THIS_From_IPersistFile(IExtractIconW, iface);
506
507         return IExtractIconW_AddRef(This);
508 }
509
510 /************************************************************************
511  * IEIPersistFile_Release (IUnknown)
512  */
513 static ULONG WINAPI IEIPersistFile_fnRelease(
514         IPersistFile    *iface)
515 {
516         _ICOM_THIS_From_IPersistFile(IExtractIconW, iface);
517
518         return IExtractIconW_Release(This);
519 }
520
521 /************************************************************************
522  * IEIPersistFile_GetClassID (IPersist)
523  */
524 static HRESULT WINAPI IEIPersistFile_fnGetClassID(
525         IPersistFile    *iface,
526         LPCLSID         lpClassId)
527 {
528         CLSID StdFolderID = { 0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} };
529
530         if (lpClassId==NULL)
531           return E_POINTER;
532
533         memcpy(lpClassId, &StdFolderID, sizeof(StdFolderID));
534
535         return S_OK;
536 }
537
538 /************************************************************************
539  * IEIPersistFile_Load (IPersistFile)
540  */
541 static HRESULT WINAPI IEIPersistFile_fnLoad(IPersistFile* iface, LPCOLESTR pszFileName, DWORD dwMode)
542 {
543         _ICOM_THIS_From_IPersistFile(IExtractIconW, iface);
544         FIXME("%p\n", This);
545         return E_NOTIMPL;
546
547 }
548
549 static struct IPersistFileVtbl pfvt =
550 {
551         IEIPersistFile_fnQueryInterface,
552         IEIPersistFile_fnAddRef,
553         IEIPersistFile_fnRelease,
554         IEIPersistFile_fnGetClassID,
555         (void *) 0xdeadbeef /* IEIPersistFile_fnIsDirty */,
556         IEIPersistFile_fnLoad,
557         (void *) 0xdeadbeef /* IEIPersistFile_fnSave */,
558         (void *) 0xdeadbeef /* IEIPersistFile_fnSaveCompleted */,
559         (void *) 0xdeadbeef /* IEIPersistFile_fnGetCurFile */
560 };