Collapse the -hHrs options into the -O option.
[wine] / dlls / shell32 / shlfolder.c
1
2 /*
3  *      Shell Folder stuff
4  *
5  *      Copyright 1997                  Marcus Meissner
6  *      Copyright 1998, 1999, 2002      Juergen Schmied
7  *
8  *      IShellFolder2 and related interfaces
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "winerror.h"
33 #include "winbase.h"
34 #include "winreg.h"
35
36 #include "ole2.h"
37 #include "shlguid.h"
38
39 #include "pidl.h"
40 #include "undocshell.h"
41 #include "shell32_main.h"
42 #include "shresdef.h"
43 #include "shlwapi.h"
44 #include "shellfolder.h"
45 #include "wine/debug.h"
46 #include "debughlp.h"
47 #include "shfldr.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL (shell);
50
51 /***************************************************************************
52  * debughelper: print out the return adress
53  *  helps especially to track down unbalanced AddRef/Release
54  */
55 #define MEM_DEBUG 0
56
57 #if MEM_DEBUG
58 #define _CALL_TRACE TRACE("called from: 0x%08x\n", *( ((UINT*)&iface)-1 ));
59 #else
60 #define _CALL_TRACE
61 #endif
62
63 /***************************************************************************
64  *  GetNextElement (internal function)
65  *
66  * gets a part of a string till the first backslash
67  *
68  * PARAMETERS
69  *  pszNext [IN] string to get the element from
70  *  pszOut  [IN] pointer to buffer whitch receives string
71  *  dwOut   [IN] length of pszOut
72  *
73  *  RETURNS
74  *    LPSTR pointer to first, not yet parsed char
75  */
76
77 LPCWSTR GetNextElementW (LPCWSTR pszNext, LPWSTR pszOut, DWORD dwOut)
78 {
79     LPCWSTR pszTail = pszNext;
80     DWORD dwCopy;
81
82     TRACE ("(%s %p 0x%08lx)\n", debugstr_w (pszNext), pszOut, dwOut);
83
84     *pszOut = 0x0000;
85
86     if (!pszNext || !*pszNext)
87         return NULL;
88
89     while (*pszTail && (*pszTail != (WCHAR) '\\'))
90         pszTail++;
91
92     dwCopy = (WCHAR *) pszTail - (WCHAR *) pszNext + 1;
93     lstrcpynW (pszOut, pszNext, (dwOut < dwCopy) ? dwOut : dwCopy);
94
95     if (*pszTail)
96         pszTail++;
97     else
98         pszTail = NULL;
99
100     TRACE ("--(%s %s 0x%08lx %p)\n", debugstr_w (pszNext), debugstr_w (pszOut), dwOut, pszTail);
101     return pszTail;
102 }
103
104 HRESULT SHELL32_ParseNextElement (HWND hwndOwner,
105                                   IShellFolder2 * psf,
106                                   LPITEMIDLIST * pidlInOut, LPOLESTR szNext, DWORD * pEaten, DWORD * pdwAttributes)
107 {
108     HRESULT hr = E_OUTOFMEMORY;
109     LPITEMIDLIST pidlOut = NULL,
110       pidlTemp = NULL;
111     IShellFolder *psfChild;
112
113     TRACE ("(%p, %p, %s)\n", psf, pidlInOut ? *pidlInOut : NULL, debugstr_w (szNext));
114
115     /* get the shellfolder for the child pidl and let it analyse further */
116     hr = IShellFolder_BindToObject (psf, *pidlInOut, NULL, &IID_IShellFolder, (LPVOID *) & psfChild);
117
118     if (SUCCEEDED (hr)) {
119         hr = IShellFolder_ParseDisplayName (psfChild, hwndOwner, NULL, szNext, pEaten, &pidlOut, pdwAttributes);
120         IShellFolder_Release (psfChild);
121
122         pidlTemp = ILCombine (*pidlInOut, pidlOut);
123
124         if (pidlOut)
125             ILFree (pidlOut);
126     }
127
128     ILFree (*pidlInOut);
129     *pidlInOut = pidlTemp;
130
131     TRACE ("-- pidl=%p ret=0x%08lx\n", pidlInOut ? *pidlInOut : NULL, hr);
132     return hr;
133 }
134
135 /***********************************************************************
136  *      SHELL32_CoCreateInitSF
137  *
138  * Creates a shell folder and initializes it with a pidl via IPersistFolder.
139  * This function is meant for virtual forders not backed by a file system
140  * folder.
141  */
142 HRESULT SHELL32_CoCreateInitSF (LPITEMIDLIST pidlRoot,
143                                 LPITEMIDLIST pidlChild, REFCLSID clsid, REFIID iid, LPVOID * ppvOut)
144 {
145     HRESULT hr;
146
147     TRACE ("%p %p\n", pidlRoot, pidlChild);
148
149     if (SUCCEEDED ((hr = SHCoCreateInstance (NULL, clsid, NULL, iid, ppvOut)))) {
150
151         IPersistFolder *pPF;
152
153         if (SUCCEEDED ((hr = IUnknown_QueryInterface ((IUnknown *) * ppvOut, &IID_IPersistFolder, (LPVOID *) & pPF)))) {
154
155             LPITEMIDLIST pidlAbsolute;
156
157             pidlAbsolute = ILCombine (pidlRoot, pidlChild);
158             IPersistFolder_Initialize (pPF, pidlAbsolute);
159             IPersistFolder_Release (pPF);
160             SHFree (pidlAbsolute);
161         }
162     }
163
164     TRACE ("-- (%p) ret=0x%08lx\n", *ppvOut, hr);
165     return hr;
166 }
167
168 /***********************************************************************
169  *      SHELL32_CoCreateInitSFEx
170  *
171  * Creates a shell folder and initializes it with a pidl and a root folder
172  * via IPersistFolder3.
173  * This function is meant for virtual forders backed by a file system
174  * folder.
175  *
176  * NOTES
177  *   pathRoot can be NULL for Folders beeing a drive.
178  *   In this case the absolute path is build from pidlChild (eg. C:)
179  */
180 HRESULT SHELL32_CoCreateInitSFEx (LPITEMIDLIST pidlRoot,
181                                   LPCSTR pathRoot, LPITEMIDLIST pidlChild, REFCLSID clsid, REFIID riid, LPVOID * ppvOut)
182 {
183     HRESULT hr;
184     IPersistFolder3 *ppf;
185
186     TRACE ("%p %s %p\n", pidlRoot, pathRoot, pidlChild);
187
188     if (SUCCEEDED ((hr = SHCoCreateInstance (NULL, &CLSID_ShellFSFolder, NULL, riid, ppvOut)))) {
189         if (SUCCEEDED (IUnknown_QueryInterface ((IUnknown *) * ppvOut, &IID_IPersistFolder3, (LPVOID *) & ppf))) {
190             PERSIST_FOLDER_TARGET_INFO ppfti;
191             LPITEMIDLIST pidlAbsolute;
192             char szDestPath[MAX_PATH];
193
194             ZeroMemory (&ppfti, sizeof (ppfti));
195
196             /* combine pidls */
197             pidlAbsolute = ILCombine (pidlRoot, pidlChild);
198
199             /* build path */
200             if (pathRoot) {
201                 lstrcpyA (szDestPath, pathRoot);
202                 PathAddBackslashA(szDestPath);          /* FIXME: why have drives a backslash here ? */
203             } else {
204                 szDestPath[0] = '\0';
205             }
206             lstrcatA (szDestPath, _ILGetTextPointer (pidlChild));
207
208             /* fill the PERSIST_FOLDER_TARGET_INFO */
209             ppfti.dwAttributes = -1;
210             ppfti.csidl = -1;
211             MultiByteToWideChar (CP_ACP, 0, szDestPath, -1, ppfti.szTargetParsingName, MAX_PATH);
212
213             IPersistFolder3_InitializeEx (ppf, NULL, pidlAbsolute, &ppfti);
214             IPersistFolder3_Release (ppf);
215             ILFree (pidlAbsolute);
216         }
217     }
218     TRACE ("-- (%p) ret=0x%08lx\n", *ppvOut, hr);
219     return hr;
220 }
221
222 /***********************************************************************
223  *      SHELL32_BindToChild
224  *
225  * Common code for IShellFolder_BindToObject.
226  * Creates a shell folder by binding to a root pidl.
227  */
228 HRESULT SHELL32_BindToChild (LPCITEMIDLIST pidlRoot,
229                              LPCSTR pathRoot, LPCITEMIDLIST pidlComplete, REFIID riid, LPVOID * ppvOut)
230 {
231     GUID const *clsid;
232     IShellFolder *pSF;
233     HRESULT hr;
234     LPITEMIDLIST pidlChild;
235
236     if (!pidlRoot || !ppvOut)
237         return E_INVALIDARG;
238
239     *ppvOut = NULL;
240
241     pidlChild = ILCloneFirst (pidlComplete);
242
243     if ((clsid = _ILGetGUIDPointer (pidlChild))) {
244         /* virtual folder */
245         hr = SHELL32_CoCreateInitSF (pidlRoot, pidlChild, clsid, &IID_IShellFolder, (LPVOID *) & pSF);
246     } else {
247         /* file system folder */
248         hr = SHELL32_CoCreateInitSFEx (pidlRoot, pathRoot, pidlChild, &CLSID_ShellFSFolder, &IID_IShellFolder,
249                                        (LPVOID *) & pSF);
250     }
251     ILFree (pidlChild);
252
253     if (SUCCEEDED (hr)) {
254         if (_ILIsPidlSimple (pidlComplete)) {
255             /* no sub folders */
256             hr = IShellFolder_QueryInterface (pSF, riid, ppvOut);
257         } else {
258             /* go deeper */
259             hr = IShellFolder_BindToObject (pSF, ILGetNext (pidlComplete), NULL, riid, ppvOut);
260         }
261         IShellFolder_Release (pSF);
262     }
263
264     TRACE ("-- returning (%p) %08lx\n", *ppvOut, hr);
265
266     return hr;
267 }
268
269 /***********************************************************************
270  *      SHELL32_GetDisplayNameOfChild
271  *
272  * Retrives the display name of a child object of a shellfolder.
273  *
274  * For a pidl eg. [subpidl1][subpidl2][subpidl3]:
275  * - it binds to the child shellfolder [subpidl1]
276  * - asks it for the displayname of [subpidl2][subpidl3]
277  *
278  * Is possible the pidl is a simple pidl. In this case it asks the
279  * subfolder for the displayname of a empty pidl. The subfolder
280  * returns the own displayname eg. "::{guid}". This is used for
281  * virtual folders with the registry key WantsFORPARSING set.
282  */
283 HRESULT SHELL32_GetDisplayNameOfChild (IShellFolder2 * psf,
284                                        LPCITEMIDLIST pidl, DWORD dwFlags, LPSTR szOut, DWORD dwOutLen)
285 {
286     LPITEMIDLIST pidlFirst;
287     HRESULT hr = E_OUTOFMEMORY;
288
289     TRACE ("(%p)->(pidl=%p 0x%08lx %p 0x%08lx)\n", psf, pidl, dwFlags, szOut, dwOutLen);
290     pdump (pidl);
291
292     pidlFirst = ILCloneFirst (pidl);
293     if (pidlFirst) {
294         IShellFolder2 *psfChild;
295
296         hr = IShellFolder_BindToObject (psf, pidlFirst, NULL, &IID_IShellFolder, (LPVOID *) & psfChild);
297         if (SUCCEEDED (hr)) {
298             STRRET strTemp;
299             LPITEMIDLIST pidlNext = ILGetNext (pidl);
300
301             hr = IShellFolder_GetDisplayNameOf (psfChild, pidlNext, dwFlags, &strTemp);
302             if (SUCCEEDED (hr)) {
303                 hr = StrRetToStrNA (szOut, dwOutLen, &strTemp, pidlNext);
304             }
305             IShellFolder_Release (psfChild);
306         }
307         ILFree (pidlFirst);
308     }
309
310     TRACE ("-- ret=0x%08lx %s\n", hr, szOut);
311
312     return hr;
313 }
314
315 /***********************************************************************
316  *  SHELL32_GetItemAttributes
317  *
318  * NOTES
319  * observerd values:
320  *  folder:     0xE0000177      FILESYSTEM | HASSUBFOLDER | FOLDER
321  *  file:       0x40000177      FILESYSTEM
322  *  drive:      0xf0000144      FILESYSTEM | HASSUBFOLDER | FOLDER | FILESYSANCESTOR
323  *  mycomputer: 0xb0000154      HASSUBFOLDER | FOLDER | FILESYSANCESTOR
324  *  (seems to be default for shell extensions if no registry entry exists)
325  *
326  * This functions does not set flags!! It only resets flags when nessesary.
327  */
328 HRESULT SHELL32_GetItemAttributes (IShellFolder * psf, LPITEMIDLIST pidl, LPDWORD pdwAttributes)
329 {
330     GUID const *clsid;
331     DWORD dwAttributes;
332
333     TRACE ("0x%08lx\n", *pdwAttributes);
334
335     if (*pdwAttributes & (0xcff3fe88))
336         WARN ("attribute 0x%08lx not implemented\n", *pdwAttributes);
337     *pdwAttributes &= ~SFGAO_LINK;      /* FIXME: for native filedialogs */
338
339     if (_ILIsDrive (pidl)) {
340         *pdwAttributes &= 0xf0000144;
341     } else if ((clsid = _ILGetGUIDPointer (pidl))) {
342         if (HCR_GetFolderAttributes (clsid, &dwAttributes)) {
343             *pdwAttributes &= dwAttributes;
344         } else {
345             *pdwAttributes &= 0xb0000154;
346         }
347     } else if (_ILGetDataPointer (pidl)) {
348         dwAttributes = _ILGetFileAttributes (pidl, NULL, 0);
349         *pdwAttributes &= ~SFGAO_FILESYSANCESTOR;
350
351         if ((SFGAO_FOLDER & *pdwAttributes) && !(dwAttributes & FILE_ATTRIBUTE_DIRECTORY))
352             *pdwAttributes &= ~(SFGAO_FOLDER | SFGAO_HASSUBFOLDER);
353
354         if ((SFGAO_HIDDEN & *pdwAttributes) && !(dwAttributes & FILE_ATTRIBUTE_HIDDEN))
355             *pdwAttributes &= ~SFGAO_HIDDEN;
356
357         if ((SFGAO_READONLY & *pdwAttributes) && !(dwAttributes & FILE_ATTRIBUTE_READONLY))
358             *pdwAttributes &= ~SFGAO_READONLY;
359     } else {
360         *pdwAttributes &= 0xb0000154;
361     }
362     TRACE ("-- 0x%08lx\n", *pdwAttributes);
363     return S_OK;
364 }
365
366 /***********************************************************************
367  *  SHELL32_GetItemAttributes
368  */
369 HRESULT SHELL32_CompareIDs (IShellFolder * iface, LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
370 {
371     int type1,
372       type2;
373     char szTemp1[MAX_PATH];
374     char szTemp2[MAX_PATH];
375     int nReturn = 0;
376     LPITEMIDLIST firstpidl,
377       nextpidl1,
378       nextpidl2;
379     IShellFolder *psf;
380
381     /* test for empty pidls */
382     BOOL isEmpty1 = _ILIsDesktop (pidl1);
383     BOOL isEmpty2 = _ILIsDesktop (pidl2);
384
385     if (isEmpty1 && isEmpty2)
386         return 0;
387     if (isEmpty1)
388         return -1;
389     if (isEmpty2)
390         return 1;
391
392     /* test for different types. Sort order is the PT_* constant */
393     type1 = _ILGetDataPointer (pidl1)->type;
394     type2 = _ILGetDataPointer (pidl2)->type;
395     if (type1 != type2)
396         return (type1 - type2);
397
398     /* test for name of pidl */
399     _ILSimpleGetText (pidl1, szTemp1, MAX_PATH);
400     _ILSimpleGetText (pidl2, szTemp2, MAX_PATH);
401     nReturn = strcasecmp (szTemp1, szTemp2);
402     if (nReturn != 0)
403         return nReturn;
404
405     /* test of complex pidls */
406     firstpidl = ILCloneFirst (pidl1);
407     nextpidl1 = ILGetNext (pidl1);
408     nextpidl2 = ILGetNext (pidl2);
409
410     /* optimizing: test special cases and bind not deeper */
411     /* the deeper shellfolder would do the same */
412     isEmpty1 = _ILIsDesktop (nextpidl1);
413     isEmpty2 = _ILIsDesktop (nextpidl2);
414
415     if (isEmpty1 && isEmpty2) {
416         nReturn = 0;
417     } else if (isEmpty1) {
418         nReturn = -1;
419     } else if (isEmpty2) {
420         nReturn = 1;
421     /* optimizing end */
422     } else if (SUCCEEDED (IShellFolder_BindToObject (iface, firstpidl, NULL, &IID_IShellFolder, (LPVOID *) & psf))) {
423         nReturn = IShellFolder_CompareIDs (psf, lParam, nextpidl1, nextpidl2);
424         IShellFolder_Release (psf);
425     }
426     ILFree (firstpidl);
427     return nReturn;
428 }