Display a drive icon for the unix root directory.
[wine] / dlls / shell32 / shfldr_unixfs.c
1 /*
2  * UNIXFS - Shell namespace extension for the unix filesystem
3  *
4  * Copyright (C) 2005 Michael Jung
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 <stdio.h>
23 #include <stdarg.h>
24 #include <limits.h>
25 #include <dirent.h>
26 #ifdef HAVE_SYS_STAT_H
27 # include <sys/stat.h>
28 #endif
29 #ifdef HAVE_PWD_H
30 # include <pwd.h>
31 #endif
32 #include <grp.h>
33
34 #define COBJMACROS
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
37
38 #include "windef.h"
39 #include "winbase.h"
40 #include "winuser.h"
41 #include "objbase.h"
42 #include "winreg.h"
43 #include "winternl.h"
44 #include "wine/debug.h"
45
46 #include "shell32_main.h"
47 #include "shfldr.h"
48 #include "shresdef.h"
49 #include "pidl.h"
50
51 WINE_DEFAULT_DEBUG_CHANNEL(shell);
52
53 const GUID CLSID_UnixFolder = {0xcc702eb2, 0x7dc5, 0x11d9, {0xc6, 0x87, 0x00, 0x04, 0x23, 0x8a, 0x01, 0xcd}};
54
55 #define ADJUST_THIS(c,m,p) ((c*)(((long)p)-(long)&(((c*)0)->lp##m##Vtbl)))
56 #define STATIC_CAST(i,p) ((i*)&p->lp##i##Vtbl)
57
58 /* FileStruct reserves one byte for szNames, thus we don't have to 
59  * alloc a byte for the terminating '\0' of 'name'. Two of the
60  * additional bytes are for SHITEMID's cb field. One is for IDLDATA's
61  * type field. One is for FileStruct's szNames field, to terminate
62  * the alternate DOS name, which we don't use here. And then there's
63  * the additional StatStruct.
64  */
65 #define SHITEMID_LEN_FROM_NAME_LEN(n) \
66     (sizeof(USHORT)+sizeof(PIDLTYPE)+sizeof(FileStruct)+(n)+sizeof(char)+sizeof(StatStruct))
67 #define NAME_LEN_FROM_LPSHITEMID(s) \
68     (((LPSHITEMID)s)->cb-sizeof(USHORT)-sizeof(PIDLTYPE)-sizeof(FileStruct)-sizeof(char)-sizeof(StatStruct))
69 #define LPSTATSTRUCT_FROM_LPSHITEMID(s) ((StatStruct*)(((LPBYTE)s)+((LPSHITEMID)s)->cb-sizeof(StatStruct)))
70
71 /* This structure is appended to shell32's FileStruct type in IDLs to store unix
72  * filesystem specific informationen extracted with the stat system call.
73  */
74 typedef struct tagStatStruct {
75     mode_t st_mode;
76     uid_t  st_uid;
77     gid_t  st_gid;
78 } StatStruct;
79
80 /******************************************************************************
81  * UNIXFS_is_pidl_of_type [INTERNAL]
82  *
83  * Checks for the first SHITEMID of an ITEMIDLIST if it passes a filter.
84  *
85  * PARAMS
86  *  pIDL    [I] The ITEMIDLIST to be checked.
87  *  fFilter [I] Shell condition flags, which specify the filter.
88  *
89  * RETURNS
90  *  TRUE, if pIDL is accepted by fFilter
91  *  FALSE, otherwise
92  */
93 static inline BOOL UNIXFS_is_pidl_of_type(LPITEMIDLIST pIDL, SHCONTF fFilter) {
94     LPSTR pszText = _ILGetTextPointer(pIDL);
95     if (!pszText) return FALSE;
96     if (pszText[0] == '.' && !(fFilter & SHCONTF_INCLUDEHIDDEN)) return FALSE;
97     if (_ILIsFolder(pIDL) && (fFilter & SHCONTF_FOLDERS)) return TRUE;
98     if (_ILIsValue(pIDL) && (fFilter & SHCONTF_NONFOLDERS)) return TRUE;
99     return FALSE;
100 }
101
102 /******************************************************************************
103  * UNIXFS_build_shitemid [Internal]
104  *
105  * Constructs a new SHITEMID for a single path item (up to the next '/' or 
106  * '\0') into a buffer. Decorates the SHITEMID with information from a stat 
107  * system call.
108  *
109  * PARAMS
110  *  name   [I] The name of the next path item. Terminated by either '\0' or '/'.
111  *  pStat  [I] A stat struct variable obtained by a stat system call on the file.
112  *  buffer [O] SHITEMID will be constructed here.
113  *
114  * RETURNS
115  *  A pointer to the next '/' or '\0' character in name.
116  *  
117  * NOTES
118  *  Minimum size of buffer is SHITEMID_LEN_FROM_NAME_LEN(strlen(name)).
119  *  If what you need is a PIDLLIST with a single SHITEMID, don't forget to append
120  *  a 0 USHORT value.
121  */
122 static char* UNIXFS_build_shitemid(char *name, struct stat *pStat, void *buffer) {
123     LARGE_INTEGER time;
124     FILETIME fileTime;
125     LPPIDLDATA pIDLData;
126     StatStruct *pStatStruct;
127     int cNameLen;
128     char *pSlash;
129
130     TRACE("(name=%s, buffer=%p)\n", debugstr_a(name), buffer);
131
132     pSlash = strchr(name, '/');
133     cNameLen = pSlash ? pSlash - name : strlen(name); 
134     
135     memset(buffer, 0, SHITEMID_LEN_FROM_NAME_LEN(cNameLen));
136     ((LPSHITEMID)buffer)->cb = SHITEMID_LEN_FROM_NAME_LEN(cNameLen) ;
137     
138     pIDLData = _ILGetDataPointer((LPCITEMIDLIST)buffer);
139     pIDLData->type = S_ISDIR(pStat->st_mode) ? PT_FOLDER : PT_VALUE;
140     pIDLData->u.file.dwFileSize = (DWORD)pStat->st_size;
141     RtlSecondsSince1970ToTime( pStat->st_mtime, &time );
142     fileTime.dwLowDateTime = time.u.LowPart;
143     fileTime.dwHighDateTime = time.u.HighPart;
144     FileTimeToDosDateTime(&fileTime, &pIDLData->u.file.uFileDate, &pIDLData->u.file.uFileTime);
145     pIDLData->u.file.uFileAttribs = 0;
146     memcpy(pIDLData->u.file.szNames, name, cNameLen);
147
148     pStatStruct = LPSTATSTRUCT_FROM_LPSHITEMID(buffer);
149     pStatStruct->st_mode = pStat->st_mode;
150     pStatStruct->st_uid = pStat->st_uid;
151     pStatStruct->st_gid = pStat->st_gid;
152
153     return pSlash ? pSlash+1 : (name + cNameLen);
154 }
155
156 /******************************************************************************
157  * UNIXFS_path_to_pidl [Internal]
158  *
159  * PARAMS
160  *  path  [I] An absolute unix path 
161  *  ppidl [O] The corresponding ITEMIDLIST. Release with SHFree/ILFree
162  *  
163  * RETURNS
164  *  Success: TRUE
165  *  Failure: FALSE, invalid params, path not absolute or out of memory
166  *
167  * NOTES
168  *  'path' has to be an absolute unix filesystem path starting and
169  *  ending with a slash ('/'). Currently, only directories (no files)
170  *  are accepted.
171  */
172 static BOOL UNIXFS_path_to_pidl(char *path, LPITEMIDLIST *ppidl) {
173     LPITEMIDLIST pidl;
174     struct stat fileStat;
175     int cSubDirs, cPidlLen;
176     char *pSlash, swap;
177
178     TRACE("path=%s, ppidl=%p", debugstr_a(path), ppidl);
179     
180     /* Fail, if no absolute path given */
181     if (!ppidl || !path || path[0] != '/') return FALSE;
182    
183     /* Count the number of sub-directories in the path */
184     cSubDirs = 0;
185     pSlash = path;
186     while (pSlash) {
187         cSubDirs++;
188         pSlash = strchr(pSlash+1, '/');
189     }
190    
191     /* Allocate enough memory to hold the path. The -cSubDirs is for the '/' 
192      * characters, which are not stored in the ITEMIDLIST. */
193     cPidlLen = strlen(path) - cSubDirs + cSubDirs * SHITEMID_LEN_FROM_NAME_LEN(0) + sizeof(USHORT);
194     *ppidl = pidl = (LPITEMIDLIST)SHAlloc(cPidlLen);
195     if (!pidl) return FALSE;
196
197     /* Concatenate the SHITEMIDs of the sub-directories. */
198     pSlash = path; /* First character of path is guaranteed to be '/' */
199     while (*path) {
200         if (pSlash) {
201             /* For the stat call, we temporarily replace the char
202              * after the next '/' with a '\0'. */
203             swap = pSlash[1];
204             pSlash[1] = '\0';
205             if (stat(path, &fileStat)) {
206                 pSlash[1] = swap;
207                 return FALSE;
208             }
209             pSlash[1] = swap;
210         } else {
211             /* We are at the last item in path. */
212             if (stat(path, &fileStat)) return FALSE;
213         }
214                 
215         path = UNIXFS_build_shitemid(path, &fileStat, pidl);
216         pidl = ILGetNext(pidl);
217         pSlash = strchr(path, '/');
218     }
219     pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
220    
221     return TRUE;
222 }
223
224 /******************************************************************************
225  * UNIXFS_pidl_to_path [Internal]
226  *
227  * Construct the unix path that corresponds to a fully qualified ITEMIDLIST
228  *
229  * PARAMS
230  *  pidl [I] ITEMIDLIST that specifies the absolute location of the folder
231  *  path [O] The corresponding unix path as a zero terminated ascii string
232  *
233  * RETURNS
234  *  Success: TRUE
235  *  Failure: FALSE, pidl doesn't specify a unix path or out of memory
236  */
237 static BOOL UNIXFS_pidl_to_path(LPCITEMIDLIST pidl, PSZ *path) {
238     LPCITEMIDLIST current = pidl, root;
239     DWORD dwPathLen;
240     char *pNextDir;
241
242     TRACE("(pidl=%p, path=%p)\n", pidl, path);
243     
244     *path = NULL;
245
246     /* Find the UnixFolderClass root */
247     while (current->mkid.cb) {
248         LPPIDLDATA pData = _ILGetDataPointer(current);
249         if (!pData) return FALSE;
250         if (pData->type == PT_GUID && IsEqualIID(&CLSID_UnixFolder, &pData->u.guid.guid)) break;
251         current = ILGetNext(current);
252     }
253     if (!current->mkid.cb) return FALSE;
254     root = ILGetNext(current);
255
256     /* Determine the path's length bytes */
257     dwPathLen = 1; /* For the terminating '\0' */
258     current = root;
259     while (current->mkid.cb) {
260         dwPathLen += NAME_LEN_FROM_LPSHITEMID(current) + 1; /* For the '/' */
261         current = ILGetNext(current);
262     };
263
264     /* Build the path */
265     *path = pNextDir = SHAlloc(dwPathLen);
266     if (!path) {
267         WARN("SHAlloc failed!\n");
268         return FALSE;
269     }
270     current = root;
271     while (current->mkid.cb) {
272         memcpy(pNextDir, _ILGetTextPointer(current), NAME_LEN_FROM_LPSHITEMID(current));
273         pNextDir += NAME_LEN_FROM_LPSHITEMID(current);
274         *pNextDir++ = '/';
275         current = ILGetNext(current);
276     }
277     *pNextDir='\0';
278     
279     TRACE("resulting path: %s\n", *path);
280     return TRUE;
281 }
282
283 /******************************************************************************
284  * UNIXFS_build_subfolder_pidls [Internal]
285  *
286  * Builds an array of subfolder PIDLs relative to a unix directory
287  *
288  * PARAMS
289  *  path   [I] Name of a unix directory as a zero terminated ascii string
290  *  apidl  [O] The array of PIDLs
291  *  pCount [O] Size of apidl
292  *
293  * RETURNS
294  *  Success: TRUE
295  *  Failure: FALSE, path is not a valid unix directory or out of memory
296  *
297  * NOTES
298  *  The array of PIDLs and each PIDL are allocated with SHAlloc. You'll have
299  *  to release each PIDL as well as the array itself with SHFree.
300  */
301 static BOOL UNIXFS_build_subfolder_pidls(const char *path, LPITEMIDLIST **apidl, DWORD *pCount)
302 {
303     struct dirent *pDirEntry;
304     struct stat fileStat;
305     DIR *dir;
306     DWORD cDirEntries, i;
307     USHORT sLen;
308     char *pszFQPath;
309
310     TRACE("(path=%s, apidl=%p, pCount=%p)\n", debugstr_a(path), apidl, pCount);
311     
312     *apidl = NULL;
313     *pCount = 0;
314    
315     /* Special case for 'My UNIX Filesystem' shell folder:
316      * The unix root directory is the only child of 'My UNIX Filesystem'. 
317      */
318     if (!strlen(path)) {
319         LPSHITEMID pid;
320         
321         if (stat("/", &fileStat)) return FALSE;
322         pid = (LPSHITEMID)SHAlloc(SHITEMID_LEN_FROM_NAME_LEN(0)+sizeof(USHORT));
323         UNIXFS_build_shitemid("", &fileStat, pid);
324         memset(((PBYTE)pid)+pid->cb, 0, sizeof(USHORT));
325         
326         *apidl = SHAlloc(sizeof(LPITEMIDLIST));
327         if (!apidl) {
328             WARN("SHAlloc failed!\n");
329             return FALSE;
330         }
331         (*apidl)[0] = (LPITEMIDLIST)pid;
332         *pCount = 1;
333     
334         return TRUE;
335     }
336     
337     dir = opendir(path);
338     if (!dir) {
339         WARN("Failed to open directory '%s'.\n", path);
340         return FALSE;
341     }
342
343     /* Allocate space for fully qualified paths */
344     pszFQPath = SHAlloc(strlen(path) + NAME_MAX);
345     if (!pszFQPath) {
346         WARN("SHAlloc failed!\n");
347         return FALSE;
348     }
349  
350     /* Count number of directory entries. */
351     for (cDirEntries = 0, pDirEntry = readdir(dir); pDirEntry; pDirEntry = readdir(dir)) { 
352         if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
353         sprintf(pszFQPath, "%s%s", path, pDirEntry->d_name);
354         if (!stat(pszFQPath, &fileStat) && (S_ISDIR(fileStat.st_mode) || S_ISREG(fileStat.st_mode))) cDirEntries++;
355     }
356
357     /* If there are no entries, we are done. */
358     if (cDirEntries == 0) {
359         closedir(dir);
360         SHFree(pszFQPath);
361         return TRUE;
362     }
363
364     /* Allocate the array of PIDLs */
365     *apidl = SHAlloc(cDirEntries * sizeof(LPITEMIDLIST));
366     if (!apidl) {
367         WARN("SHAlloc failed!\n");
368         return FALSE;
369     }
370   
371     /* Allocate and initialize one SHITEMID per sub-directory. */
372     for (rewinddir(dir), pDirEntry = readdir(dir), i = 0; pDirEntry; pDirEntry = readdir(dir)) {
373         LPSHITEMID pid;
374             
375         if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
376
377         sprintf(pszFQPath, "%s%s", path, pDirEntry->d_name);
378         if (stat(pszFQPath, &fileStat)) continue;
379         if (!S_ISDIR(fileStat.st_mode) && !S_ISREG(fileStat.st_mode)) continue;
380    
381         sLen = strlen(pDirEntry->d_name);
382         pid = (LPSHITEMID)SHAlloc(SHITEMID_LEN_FROM_NAME_LEN(sLen)+sizeof(USHORT));
383         if (!pid) {
384             WARN("SHAlloc failed!\n");
385             return FALSE;
386         }
387         UNIXFS_build_shitemid(pDirEntry->d_name, &fileStat, pid);
388         memset(((PBYTE)pid)+pid->cb, 0, sizeof(USHORT));
389
390         (*apidl)[i++] = (LPITEMIDLIST)pid;
391     }
392     
393     *pCount = i;    
394     closedir(dir);
395     SHFree(pszFQPath);
396
397     return TRUE;
398 }
399
400 /******************************************************************************
401  * UnixFolderIcon 
402  *
403  * Singleton class, which is used by the shell to extract icons to represent
404  * folders in tree- and listviews. Currently, all this singleton does is to
405  * provide the shell with the absolute path to "shell32.dll" and with the 
406  * indices of the closed and opened folder icons in the resources of this dll.
407  */
408
409 /* UnixFolderIcon object layout and typedef.
410  */
411 typedef struct _UnixFolderIcon {
412     const IExtractIconWVtbl *lpIExtractIconWVtbl;
413     BOOL bFolder;
414 } UnixFolderIcon;
415
416 static HRESULT WINAPI UnixFolderIcon_IExtractIconW_QueryInterface(IExtractIconW *iface, REFIID riid, 
417     void **ppv) 
418 {
419     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
420     
421     if (!ppv) return E_INVALIDARG;
422     
423     if (IsEqualIID(&IID_IUnknown, riid) ||
424         IsEqualIID(&IID_IExtractIconW, riid))
425     {
426         *ppv = iface;
427     } else {
428         *ppv = NULL;
429         return E_NOINTERFACE;
430     }
431
432     IExtractIconW_AddRef(iface);
433     return S_OK;
434 }
435
436 static ULONG WINAPI UnixFolderIcon_IExtractIconW_AddRef(IExtractIconW *iface) {
437     TRACE("(iface=%p)\n", iface);
438     return 2;
439 }
440
441 static ULONG WINAPI UnixFolderIcon_IExtractIconW_Release(IExtractIconW *iface) {
442     TRACE("(iface=%p)\n", iface);
443     return 1;
444 }
445
446 static HRESULT WINAPI UnixFolderIcon_IExtractIconW_GetIconLocation(IExtractIconW *iface, 
447     UINT uFlags, LPWSTR szIconFile, UINT cchMax, INT* piIndex, UINT* pwFlags)
448 {
449     UnixFolderIcon *This = ADJUST_THIS(UnixFolderIcon, IExtractIconW, iface);
450         
451     TRACE("(iface=%p, uFlags=%u, szIconFile=%s, cchMax=%u, piIndex=%p, pwFlags=%p)\n",
452             iface, uFlags, debugstr_w(szIconFile), cchMax, piIndex, pwFlags);
453     
454     lstrcpynW(szIconFile, swShell32Name, cchMax);
455     if (This->bFolder) {
456         *piIndex = (uFlags & GIL_OPENICON) ? -IDI_SHELL_FOLDER_OPEN : -IDI_SHELL_FOLDER;
457     } else {
458         *piIndex = -IDI_SHELL_DOCUMENT;
459     }
460     *pwFlags = 0;
461
462     return S_OK;
463 }
464
465 static HRESULT WINAPI UnixFolderIcon_IExtractIconW_Extract(
466     IExtractIconW *iface, LPCWSTR pszFile, UINT nIconIndex, HICON* phiconLarge, HICON* phiconSmall, 
467     UINT nIconSize)
468 {
469     TRACE("(iface=%p, pszFile=%s, nIconIndex=%u, phiconLarge=%p, phiconSmall=%p, nIconSize=%u)"
470           "stub\n", iface, debugstr_w(pszFile), nIconIndex, phiconLarge, phiconSmall, nIconSize);
471
472     return E_NOTIMPL;
473 }
474
475 /* VTable for the IExtractIconW interface of the UnixFolderIcon class. 
476  */
477 static const IExtractIconWVtbl UnixFolderIcon_IExtractIconW_Vtbl = {
478     UnixFolderIcon_IExtractIconW_QueryInterface,
479     UnixFolderIcon_IExtractIconW_AddRef,
480     UnixFolderIcon_IExtractIconW_Release,
481     UnixFolderIcon_IExtractIconW_GetIconLocation,
482     UnixFolderIcon_IExtractIconW_Extract
483 };
484
485 /* The singleton instance
486  */
487 UnixFolderIcon UnixFolderIconSingleton = { &UnixFolderIcon_IExtractIconW_Vtbl, TRUE };
488 UnixFolderIcon UnixDocumentIconSingleton = { &UnixFolderIcon_IExtractIconW_Vtbl, FALSE };
489
490 /******************************************************************************
491  * UnixFolder
492  *
493  * Class whose heap based instances represent unix filesystem directories.
494  */
495
496 /* UnixFolder object layout and typedef.
497  */
498 typedef struct _UnixFolder {
499     const IShellFolder2Vtbl  *lpIShellFolder2Vtbl;
500     const IPersistFolderVtbl *lpIPersistFolderVtbl;
501     ULONG m_cRef;
502     CHAR *m_pszPath;
503     LPITEMIDLIST m_pidlLocation;
504     LPITEMIDLIST *m_apidlSubDirs;
505     DWORD m_cSubDirs;
506 } UnixFolder;
507
508 static void UnixFolder_Destroy(UnixFolder *pUnixFolder) {
509     DWORD i;
510
511     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
512     
513     if (pUnixFolder->m_apidlSubDirs) 
514         for (i=0; i < pUnixFolder->m_cSubDirs; i++) 
515             SHFree(pUnixFolder->m_apidlSubDirs[i]);
516     SHFree(pUnixFolder->m_apidlSubDirs);
517     SHFree(pUnixFolder->m_pszPath);
518     ILFree(pUnixFolder->m_pidlLocation);
519     SHFree(pUnixFolder);
520 }
521
522 static HRESULT WINAPI UnixFolder_IShellFolder2_QueryInterface(IShellFolder2 *iface, REFIID riid, 
523     void **ppv) 
524 {
525     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
526         
527     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
528     
529     if (!ppv) return E_INVALIDARG;
530     
531     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IShellFolder, riid) || 
532         IsEqualIID(&IID_IShellFolder2, riid)) 
533     {
534         *ppv = &This->lpIShellFolder2Vtbl;
535     } else if (IsEqualIID(&IID_IPersistFolder, riid) || IsEqualIID(&IID_IPersist, riid)) {
536         *ppv = &This->lpIPersistFolderVtbl;
537     } else {
538         *ppv = NULL;
539         return E_NOINTERFACE;
540     }
541
542     IUnknown_AddRef((IUnknown*)*ppv);
543     return S_OK;
544 }
545
546 static ULONG WINAPI UnixFolder_IShellFolder2_AddRef(IShellFolder2 *iface) {
547     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
548
549     TRACE("(iface=%p)\n", iface);
550
551     return InterlockedIncrement(&This->m_cRef);
552 }
553
554 static ULONG WINAPI UnixFolder_IShellFolder2_Release(IShellFolder2 *iface) {
555     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
556     ULONG cRef;
557     
558     TRACE("(iface=%p)\n", iface);
559
560     cRef = InterlockedDecrement(&This->m_cRef);
561     
562     if (!cRef) 
563         UnixFolder_Destroy(This);
564
565     return cRef;
566 }
567
568 static HRESULT WINAPI UnixFolder_IShellFolder2_ParseDisplayName(IShellFolder2* iface, HWND hwndOwner, 
569     LPBC pbcReserved, LPOLESTR lpszDisplayName, ULONG* pchEaten, LPITEMIDLIST* ppidl, 
570     ULONG* pdwAttributes)
571 {
572     int cPathLen;
573     char *pszAnsiPath;
574     BOOL result;
575
576     TRACE("(iface=%p, hwndOwner=%p, pbcReserved=%p, lpszDisplayName=%s, pchEaten=%p, ppidl=%p, "
577           "pdwAttributes=%p) stub\n", iface, hwndOwner, pbcReserved, debugstr_w(lpszDisplayName), 
578           pchEaten, ppidl, pdwAttributes);
579
580     cPathLen = lstrlenW(lpszDisplayName);
581     pszAnsiPath = (char*)SHAlloc(cPathLen+1);
582     WideCharToMultiByte(CP_ACP, 0, lpszDisplayName, -1, pszAnsiPath, cPathLen+1, NULL, NULL);
583
584     result = UNIXFS_path_to_pidl(pszAnsiPath, ppidl);
585
586     SHFree(pszAnsiPath);
587     
588     return result ? S_OK : E_FAIL;
589 }
590
591 static IUnknown *UnixSubFolderIterator_Construct(UnixFolder *pUnixFolder, SHCONTF fFilter);
592
593 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumObjects(IShellFolder2* iface, HWND hwndOwner, 
594     SHCONTF grfFlags, IEnumIDList** ppEnumIDList)
595 {
596     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
597     IUnknown *newIterator;
598     HRESULT hr;
599     
600     TRACE("(iface=%p, hwndOwner=%p, grfFlags=%08lx, ppEnumIDList=%p)\n", 
601             iface, hwndOwner, grfFlags, ppEnumIDList);
602
603     newIterator = UnixSubFolderIterator_Construct(This, grfFlags);
604     hr = IUnknown_QueryInterface(newIterator, &IID_IEnumIDList, (void**)ppEnumIDList);
605     IUnknown_Release(newIterator);
606     
607     return hr;
608 }
609
610 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToObject(IShellFolder2* iface, LPCITEMIDLIST pidl,
611     LPBC pbcReserved, REFIID riid, void** ppvOut)
612 {
613     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
614     IPersistFolder *persistFolder;
615     LPITEMIDLIST pidlSubFolder;
616     HRESULT hr;
617         
618     TRACE("(iface=%p, pidl=%p, pbcReserver=%p, riid=%p, ppvOut=%p)\n", 
619             iface, pidl, pbcReserved, riid, ppvOut);
620
621     hr = UnixFolder_Constructor(NULL, &IID_IPersistFolder, (void**)&persistFolder);
622     if (!SUCCEEDED(hr)) return hr;
623     hr = IPersistFolder_QueryInterface(persistFolder, riid, (void**)ppvOut);
624     
625     pidlSubFolder = ILCombine(This->m_pidlLocation, pidl);
626     IPersistFolder_Initialize(persistFolder, pidlSubFolder);
627     IPersistFolder_Release(persistFolder);
628     ILFree(pidlSubFolder);
629
630     return hr;
631 }
632
633 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToStorage(IShellFolder2* This, LPCITEMIDLIST pidl, 
634     LPBC pbcReserved, REFIID riid, void** ppvObj)
635 {
636     TRACE("stub\n");
637     return E_NOTIMPL;
638 }
639
640 static HRESULT WINAPI UnixFolder_IShellFolder2_CompareIDs(IShellFolder2* iface, LPARAM lParam, 
641     LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
642 {
643     BOOL isEmpty1, isEmpty2;
644     HRESULT hr = E_FAIL;
645     LPITEMIDLIST firstpidl;
646     IShellFolder2 *psf;
647     int compare;
648
649     TRACE("(iface=%p, lParam=%ld, pidl1=%p, pidl2=%p)\n", iface, lParam, pidl1, pidl2);
650     
651     isEmpty1 = !pidl1 || !pidl1->mkid.cb;
652     isEmpty2 = !pidl2 || !pidl2->mkid.cb;
653
654     if (isEmpty1 && isEmpty2) 
655         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
656     else if (isEmpty1)
657         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
658     else if (isEmpty2)
659         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
660
661     if (_ILIsFolder(pidl1) && !_ILIsFolder(pidl2)) 
662         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
663     if (!_ILIsFolder(pidl1) && _ILIsFolder(pidl2))
664         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
665
666     compare = CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE, 
667                              _ILGetTextPointer(pidl1), NAME_LEN_FROM_LPSHITEMID(pidl1),
668                              _ILGetTextPointer(pidl2), NAME_LEN_FROM_LPSHITEMID(pidl2));
669     
670     if ((compare == CSTR_LESS_THAN) || (compare == CSTR_GREATER_THAN)) 
671         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)(compare == CSTR_LESS_THAN)?-1:1);
672
673     if (pidl1->mkid.cb < pidl2->mkid.cb)
674         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
675     else if (pidl1->mkid.cb > pidl2->mkid.cb)
676         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
677
678     firstpidl = ILCloneFirst(pidl1);
679     pidl1 = ILGetNext(pidl1);
680     pidl2 = ILGetNext(pidl2);
681     
682     hr = IShellFolder2_BindToObject(iface, firstpidl, NULL, &IID_IShellFolder, (LPVOID*)&psf);
683     if (SUCCEEDED(hr)) {
684         hr = IShellFolder_CompareIDs(psf, lParam, pidl1, pidl2);
685         IShellFolder2_Release(psf);
686     }
687
688     ILFree(firstpidl);
689     return hr;
690 }
691
692 static HRESULT WINAPI UnixFolder_IShellFolder2_CreateViewObject(IShellFolder2* iface, HWND hwndOwner,
693     REFIID riid, void** ppv)
694 {
695     HRESULT hr = E_INVALIDARG;
696         
697     TRACE("(iface=%p, hwndOwner=%p, riid=%p, ppv=%p) stub\n", iface, hwndOwner, riid, ppv);
698     
699     if (!ppv) return E_INVALIDARG;
700     *ppv = NULL;
701     
702     if (IsEqualIID(&IID_IShellView, riid)) {
703         LPSHELLVIEW pShellView;
704         
705         pShellView = IShellView_Constructor((IShellFolder*)iface);
706         if (pShellView) {
707             hr = IShellView_QueryInterface(pShellView, riid, ppv);
708             IShellView_Release(pShellView);
709         }
710     } 
711     
712     return hr;
713 }
714
715 static HRESULT WINAPI UnixFolder_IShellFolder2_GetAttributesOf(IShellFolder2* iface, UINT cidl, 
716     LPCITEMIDLIST* apidl, SFGAOF* rgfInOut)
717 {
718     UINT i;
719     SFGAOF flags= ~(SFGAOF)0;
720         
721     TRACE("(iface=%p, cidl=%u, apidl=%p, rgfInOut=%p) semi-stub\n", iface, cidl, apidl, rgfInOut);
722    
723     for (i=0; i<cidl; i++) {
724         LPPIDLDATA pData = _ILGetDataPointer(apidl[i]);
725         if (!pData) continue;
726         if (pData->type == PT_FOLDER) flags &= (SFGAO_FILESYSTEM|SFGAO_FOLDER|SFGAO_HASSUBFOLDER);
727         if (pData->type == PT_VALUE) flags &= SFGAO_FILESYSTEM;
728     }
729     
730     *rgfInOut = *rgfInOut & flags;
731             
732     return S_OK;
733 }
734
735 static HRESULT WINAPI UnixFolder_IShellFolder2_GetUIObjectOf(IShellFolder2* iface, HWND hwndOwner, 
736     UINT cidl, LPCITEMIDLIST* apidl, REFIID riid, UINT* prgfInOut, void** ppvOut)
737 {
738     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
739     
740     TRACE("(iface=%p, hwndOwner=%p, cidl=%d, apidl=%p, riid=%s, prgfInOut=%p, ppv=%p)\n",
741         iface, hwndOwner, cidl, apidl, debugstr_guid(riid), prgfInOut, ppvOut);
742
743     if (IsEqualIID(&IID_IContextMenu, riid)) {
744         *ppvOut = ISvItemCm_Constructor((IShellFolder*)iface, This->m_pidlLocation, apidl, cidl);
745         return S_OK;
746     } else if (IsEqualIID(&IID_IDataObject, riid)) {
747         *ppvOut = IDataObject_Constructor(hwndOwner, This->m_pidlLocation, apidl, cidl);
748         return S_OK;
749     } else if (IsEqualIID(&IID_IExtractIconA, riid)) {
750         FIXME("IExtractIconA\n");
751         return E_FAIL;
752     } else if (IsEqualIID(&IID_IExtractIconW, riid)) {
753         if (cidl != 1) return E_FAIL;
754         if (_ILIsFolder(apidl[0])) 
755             *ppvOut = &UnixFolderIconSingleton;
756         else
757             *ppvOut = &UnixDocumentIconSingleton;
758         return S_OK;
759     } else if (IsEqualIID(&IID_IDropTarget, riid)) {
760         FIXME("IDropTarget\n");
761         return E_FAIL;
762     } else if (IsEqualIID(&IID_IShellLinkW, riid)) {
763         FIXME("IShellLinkW\n");
764         return E_FAIL;
765     } else if (IsEqualIID(&IID_IShellLinkA, riid)) {
766         FIXME("IShellLinkA\n");
767         return E_FAIL;
768     } else {
769         FIXME("Unknown interface %s in GetUIObjectOf\n", debugstr_guid(riid));
770         return E_NOINTERFACE;
771     }
772 }
773
774 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDisplayNameOf(IShellFolder2* iface, 
775     LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET* lpName)
776 {
777     char *pszFileName;
778
779     TRACE("(iface=%p, pidl=%p, uFlags=%lx, lpName=%p)\n", iface, pidl, uFlags, lpName);
780    
781     lpName->uType = STRRET_CSTR;
782     if (!pidl->mkid.cb) {
783         strcpy(lpName->u.cStr, "");
784         return S_OK;
785     }
786
787     pszFileName = _ILGetTextPointer(pidl);
788     
789     if ((uFlags & SHGDN_FORPARSING) && !(uFlags & SHGDN_INFOLDER)) {
790         STRRET strSubfolderName;
791         IShellFolder *pSubFolder;
792         HRESULT hr;
793         LPITEMIDLIST pidlFirst;
794
795         pidlFirst = ILCloneFirst(pidl);
796         if (!pidlFirst) {
797             WARN("ILCloneFirst failed!\n");
798             return E_FAIL;
799         }
800
801         hr = IShellFolder_BindToObject(iface, pidlFirst, NULL, &IID_IShellFolder, 
802                                        (void**)&pSubFolder);
803         if (!SUCCEEDED(hr)) {
804             WARN("BindToObject failed!\n");
805             ILFree(pidlFirst);
806             return hr;
807         }
808
809         ILFree(pidlFirst);
810         
811         hr = IShellFolder_GetDisplayNameOf(pSubFolder, ILGetNext(pidl), uFlags, &strSubfolderName);
812         if (!SUCCEEDED(hr)) {
813             WARN("GetDisplayNameOf failed!\n");
814             return hr;
815         }
816         
817         snprintf(lpName->u.cStr, MAX_PATH, "%s/%s", pszFileName, strSubfolderName.u.cStr);
818
819         IShellFolder_Release(pSubFolder);
820     } else {
821         strcpy(lpName->u.cStr, *pszFileName ? pszFileName : "/");
822     }
823
824     return S_OK;
825 }
826
827 static HRESULT WINAPI UnixFolder_IShellFolder2_SetNameOf(IShellFolder2* This, HWND hwnd, 
828     LPCITEMIDLIST pidl, LPCOLESTR lpszName, SHGDNF uFlags, LPITEMIDLIST* ppidlOut)
829 {
830     TRACE("stub\n");
831     return E_NOTIMPL;
832 }
833
834 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumSearches(IShellFolder2* iface, 
835     IEnumExtraSearch **ppEnum) 
836 {
837     TRACE("stub\n");
838     return E_NOTIMPL;
839 }
840
841 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumn(IShellFolder2* iface, 
842     DWORD dwReserved, ULONG *pSort, ULONG *pDisplay) 
843 {
844     TRACE("stub\n");
845     return E_NOTIMPL;
846 }
847
848 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumnState(IShellFolder2* iface, 
849     UINT iColumn, SHCOLSTATEF *pcsFlags)
850 {
851     TRACE("stub\n");
852     return E_NOTIMPL;
853 }
854
855 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultSearchGUID(IShellFolder2* iface, 
856     GUID *pguid)
857 {
858     TRACE("stub\n");
859     return E_NOTIMPL;
860 }
861
862 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsEx(IShellFolder2* iface, 
863     LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
864 {
865     TRACE("stub\n");
866     return E_NOTIMPL;
867 }
868
869 #define SHELLVIEWCOLUMNS 6 
870
871 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsOf(IShellFolder2* iface, 
872     LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
873 {
874     HRESULT hr = E_FAIL;
875     struct passwd *pPasswd;
876     struct group *pGroup;
877     static const shvheader SFHeader[SHELLVIEWCOLUMNS] = {
878         {IDS_SHV_COLUMN1,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
879         {IDS_SHV_COLUMN5,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
880         {IDS_SHV_COLUMN10, SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
881         {IDS_SHV_COLUMN11, SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
882         {IDS_SHV_COLUMN2,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 8},
883         {IDS_SHV_COLUMN4,  SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10}
884     };
885
886     TRACE("(iface=%p, pidl=%p, iColumn=%d, psd=%p) stub\n", iface, pidl, iColumn, psd);
887     
888     if (!psd || iColumn >= SHELLVIEWCOLUMNS)
889         return E_INVALIDARG;
890
891     if (!pidl) {
892         psd->fmt = SFHeader[iColumn].fmt;
893         psd->cxChar = SFHeader[iColumn].cxChar;
894         psd->str.uType = STRRET_CSTR;
895         LoadStringA(shell32_hInstance, SFHeader[iColumn].colnameid, psd->str.u.cStr, MAX_PATH);
896         return S_OK;
897     } else {
898         StatStruct *pStatStruct = LPSTATSTRUCT_FROM_LPSHITEMID(pidl);
899         psd->str.u.cStr[0] = '\0';
900         psd->str.uType = STRRET_CSTR;
901         switch (iColumn) {
902             case 0:
903                 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL|SHGDN_INFOLDER, &psd->str);
904                 break;
905             case 1:
906                 psd->str.u.cStr[0] = S_ISDIR(pStatStruct->st_mode) ? 'd' : '-';
907                 psd->str.u.cStr[1] = (pStatStruct->st_mode & S_IRUSR) ? 'r' : '-';
908                 psd->str.u.cStr[2] = (pStatStruct->st_mode & S_IWUSR) ? 'w' : '-';
909                 psd->str.u.cStr[3] = (pStatStruct->st_mode & S_IXUSR) ? 'x' : '-';
910                 psd->str.u.cStr[4] = (pStatStruct->st_mode & S_IRGRP) ? 'r' : '-';
911                 psd->str.u.cStr[5] = (pStatStruct->st_mode & S_IWGRP) ? 'w' : '-';
912                 psd->str.u.cStr[6] = (pStatStruct->st_mode & S_IXGRP) ? 'x' : '-';
913                 psd->str.u.cStr[7] = (pStatStruct->st_mode & S_IROTH) ? 'r' : '-';
914                 psd->str.u.cStr[8] = (pStatStruct->st_mode & S_IWOTH) ? 'w' : '-';
915                 psd->str.u.cStr[9] = (pStatStruct->st_mode & S_IXOTH) ? 'x' : '-';
916                 psd->str.u.cStr[10] = '\0';
917                 break;
918             case 2:
919                 pPasswd = getpwuid(pStatStruct->st_uid);
920                 if (pPasswd) strcpy(psd->str.u.cStr, pPasswd->pw_name);
921                 break;
922             case 3:
923                 pGroup = getgrgid(pStatStruct->st_gid);
924                 if (pGroup) strcpy(psd->str.u.cStr, pGroup->gr_name);
925                 break;
926             case 4:
927                 _ILGetFileSize(pidl, psd->str.u.cStr, MAX_PATH);
928                 break;
929             case 5:
930                 _ILGetFileDate(pidl, psd->str.u.cStr, MAX_PATH);
931                 break;
932         }
933     }
934     
935     return hr;
936 }
937
938 static HRESULT WINAPI UnixFolder_IShellFolder2_MapColumnToSCID(IShellFolder2* iface, UINT iColumn,
939     SHCOLUMNID *pscid) 
940 {
941     TRACE("stub\n");
942     return E_NOTIMPL;
943 }
944
945 /* VTable for UnixFolder's IShellFolder2 interface.
946  */
947 static const IShellFolder2Vtbl UnixFolder_IShellFolder2_Vtbl = {
948     UnixFolder_IShellFolder2_QueryInterface,
949     UnixFolder_IShellFolder2_AddRef,
950     UnixFolder_IShellFolder2_Release,
951     UnixFolder_IShellFolder2_ParseDisplayName,
952     UnixFolder_IShellFolder2_EnumObjects,
953     UnixFolder_IShellFolder2_BindToObject,
954     UnixFolder_IShellFolder2_BindToStorage,
955     UnixFolder_IShellFolder2_CompareIDs,
956     UnixFolder_IShellFolder2_CreateViewObject,
957     UnixFolder_IShellFolder2_GetAttributesOf,
958     UnixFolder_IShellFolder2_GetUIObjectOf,
959     UnixFolder_IShellFolder2_GetDisplayNameOf,
960     UnixFolder_IShellFolder2_SetNameOf,
961     UnixFolder_IShellFolder2_GetDefaultSearchGUID,
962     UnixFolder_IShellFolder2_EnumSearches,
963     UnixFolder_IShellFolder2_GetDefaultColumn,
964     UnixFolder_IShellFolder2_GetDefaultColumnState,
965     UnixFolder_IShellFolder2_GetDetailsEx,
966     UnixFolder_IShellFolder2_GetDetailsOf,
967     UnixFolder_IShellFolder2_MapColumnToSCID
968 };
969
970 static HRESULT WINAPI UnixFolder_IPersistFolder_QueryInterface(IPersistFolder* This, REFIID riid, 
971     void** ppvObject)
972 {
973     return UnixFolder_IShellFolder2_QueryInterface(
974                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder, This), riid, ppvObject);
975 }
976
977 static ULONG WINAPI UnixFolder_IPersistFolder_AddRef(IPersistFolder* This)
978 {
979     return UnixFolder_IShellFolder2_AddRef(
980                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder, This));
981 }
982
983 static ULONG WINAPI UnixFolder_IPersistFolder_Release(IPersistFolder* This)
984 {
985     return UnixFolder_IShellFolder2_Release(
986                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder, This));
987 }
988
989 static HRESULT WINAPI UnixFolder_IPersistFolder_GetClassID(IPersistFolder* This, CLSID* pClassID)
990 {
991     TRACE("stub\n");
992     return E_NOTIMPL;
993 }
994
995 static HRESULT WINAPI UnixFolder_IPersistFolder_Initialize(IPersistFolder* iface, LPCITEMIDLIST pidl)
996 {
997     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder, iface);
998     
999     TRACE("(iface=%p, pidl=%p)\n", iface, pidl);
1000
1001     This->m_pidlLocation = ILClone(pidl);
1002     UNIXFS_pidl_to_path(pidl, &This->m_pszPath);
1003     UNIXFS_build_subfolder_pidls(This->m_pszPath, &This->m_apidlSubDirs, &This->m_cSubDirs);
1004     
1005     return S_OK;
1006 }
1007
1008 /* VTable for UnixFolder's IPersistFolder interface.
1009  */
1010 static const IPersistFolderVtbl UnixFolder_IPersistFolder_Vtbl = {
1011     UnixFolder_IPersistFolder_QueryInterface,
1012     UnixFolder_IPersistFolder_AddRef,
1013     UnixFolder_IPersistFolder_Release,
1014     UnixFolder_IPersistFolder_GetClassID,
1015     UnixFolder_IPersistFolder_Initialize
1016 };
1017
1018 /******************************************************************************
1019  * UnixFolder_Constructor [Internal]
1020  *
1021  * PARAMS
1022  *  pUnkOuter [I] Outer class for aggregation. Currently ignored.
1023  *  riid      [I] Interface asked for by the client.
1024  *  ppv       [O] Pointer to an riid interface to the UnixFolder object.
1025  *
1026  * NOTES
1027  *  This is the only function exported from shfldr_unixfs.c. It's called from
1028  *  shellole.c's default class factory and thus has to exhibit a LPFNCREATEINSTANCE
1029  *  compatible signature.
1030  */
1031 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1032     HRESULT hr;
1033     UnixFolder *pUnixFolder;
1034     
1035     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1036
1037     pUnixFolder = SHAlloc((ULONG)sizeof(UnixFolder));
1038     pUnixFolder->lpIShellFolder2Vtbl = &UnixFolder_IShellFolder2_Vtbl;
1039     pUnixFolder->lpIPersistFolderVtbl = &UnixFolder_IPersistFolder_Vtbl;
1040     pUnixFolder->m_cRef = 0;
1041     pUnixFolder->m_pszPath = NULL;
1042     pUnixFolder->m_apidlSubDirs = NULL;
1043     pUnixFolder->m_cSubDirs = 0;
1044
1045     UnixFolder_IShellFolder2_AddRef(STATIC_CAST(IShellFolder2, pUnixFolder));
1046     hr = UnixFolder_IShellFolder2_QueryInterface(STATIC_CAST(IShellFolder2, pUnixFolder), riid, ppv);
1047     UnixFolder_IShellFolder2_Release(STATIC_CAST(IShellFolder2, pUnixFolder));
1048     return hr;
1049 }
1050
1051 /******************************************************************************
1052  * UnixSubFolderIterator
1053  *
1054  * Class whose heap based objects represent iterators over the sub-directories
1055  * of a given UnixFolder object. 
1056  */
1057
1058 /* UnixSubFolderIterator object layout and typedef.
1059  */
1060 typedef struct _UnixSubFolderIterator {
1061     const IEnumIDListVtbl *lpIEnumIDListVtbl;
1062     ULONG m_cRef;
1063     UnixFolder *m_pUnixFolder;
1064     ULONG m_cIdx;
1065     SHCONTF m_fFilter;
1066 } UnixSubFolderIterator;
1067
1068 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
1069     TRACE("(iterator=%p)\n", iterator);
1070         
1071     UnixFolder_IShellFolder2_Release((IShellFolder2*)iterator->m_pUnixFolder);
1072     SHFree(iterator);
1073 }
1074
1075 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface, 
1076     REFIID riid, void** ppv)
1077 {
1078     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
1079     
1080     if (!ppv) return E_INVALIDARG;
1081     
1082     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
1083         *ppv = iface;
1084     } else {
1085         *ppv = NULL;
1086         return E_NOINTERFACE;
1087     }
1088
1089     IEnumIDList_AddRef(iface);
1090     return S_OK;
1091 }
1092                             
1093 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
1094 {
1095     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1096
1097     TRACE("(iface=%p)\n", iface);
1098    
1099     return InterlockedIncrement(&This->m_cRef);
1100 }
1101
1102 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
1103 {
1104     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1105     ULONG cRef;
1106     
1107     TRACE("(iface=%p)\n", iface);
1108
1109     cRef = InterlockedDecrement(&This->m_cRef);
1110     
1111     if (!cRef) 
1112         UnixSubFolderIterator_Destroy(This);
1113
1114     return cRef;
1115 }
1116
1117 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt, 
1118     LPITEMIDLIST* rgelt, ULONG* pceltFetched)
1119 {
1120     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1121     ULONG i;
1122
1123     TRACE("(iface=%p, celt=%ld, rgelt=%p, pceltFetched=%p)\n", iface, celt, rgelt, pceltFetched);
1124    
1125     for (i=0; (i < celt) && (This->m_cIdx < This->m_pUnixFolder->m_cSubDirs); This->m_cIdx++) {
1126         LPITEMIDLIST pCurrent = This->m_pUnixFolder->m_apidlSubDirs[This->m_cIdx];
1127         if (UNIXFS_is_pidl_of_type(pCurrent, This->m_fFilter)) {
1128             rgelt[i] = ILClone(pCurrent);
1129             i++;
1130         }
1131     }
1132
1133     if (pceltFetched)
1134         *pceltFetched = i;
1135
1136     return i == celt ? S_OK : S_FALSE;
1137 }
1138
1139 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
1140 {
1141     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1142     ULONG i;
1143     
1144     TRACE("(iface=%p, celt=%ld)\n", iface, celt);
1145
1146     for (i=0; i < celt; i++) {
1147         while (This->m_cIdx < This->m_pUnixFolder->m_cSubDirs &&
1148                !UNIXFS_is_pidl_of_type(This->m_pUnixFolder->m_apidlSubDirs[This->m_cIdx], This->m_fFilter)) 
1149         {
1150             This->m_cIdx++;
1151         }
1152         This->m_cIdx++;
1153     }
1154     
1155     if (This->m_cIdx > This->m_pUnixFolder->m_cSubDirs) {
1156         This->m_cIdx = This->m_pUnixFolder->m_cSubDirs;
1157         return S_FALSE;
1158     } else {
1159         return S_OK;
1160     }
1161 }
1162
1163 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
1164 {
1165     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1166         
1167     TRACE("(iface=%p)\n", iface);
1168
1169     This->m_cIdx = 0;
1170     
1171     return S_OK;
1172 }
1173
1174 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This, 
1175     IEnumIDList** ppenum)
1176 {
1177     TRACE("stub\n");
1178     return E_NOTIMPL;
1179 }
1180
1181 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
1182  */
1183 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
1184     UnixSubFolderIterator_IEnumIDList_QueryInterface,
1185     UnixSubFolderIterator_IEnumIDList_AddRef,
1186     UnixSubFolderIterator_IEnumIDList_Release,
1187     UnixSubFolderIterator_IEnumIDList_Next,
1188     UnixSubFolderIterator_IEnumIDList_Skip,
1189     UnixSubFolderIterator_IEnumIDList_Reset,
1190     UnixSubFolderIterator_IEnumIDList_Clone
1191 };
1192
1193 static IUnknown *UnixSubFolderIterator_Construct(UnixFolder *pUnixFolder, SHCONTF fFilter) {
1194     UnixSubFolderIterator *iterator;
1195
1196     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
1197     
1198     iterator = SHAlloc((ULONG)sizeof(UnixSubFolderIterator));
1199     iterator->lpIEnumIDListVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
1200     iterator->m_cRef = 0;
1201     iterator->m_cIdx = 0;
1202     iterator->m_pUnixFolder = pUnixFolder;
1203     iterator->m_fFilter = fFilter;
1204
1205     UnixSubFolderIterator_IEnumIDList_AddRef((IEnumIDList*)iterator);
1206     UnixFolder_IShellFolder2_AddRef((IShellFolder2*)pUnixFolder);
1207     
1208     return (IUnknown*)iterator;
1209 }