Added stubbed implementation of IPersistPropertyBag interface.
[wine/hacks.git] / dlls / shell32 / shfldr_unixfs.c
bloba10c67d2ce34bd2de24c97a4f8b70bfa8c759cd2
1 /*
2 * UNIXFS - Shell namespace extension for the unix filesystem
4 * Copyright (C) 2005 Michael Jung
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <limits.h>
27 #include <dirent.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
33 #endif
34 #ifdef HAVE_PWD_H
35 # include <pwd.h>
36 #endif
37 #include <grp.h>
38 #include <limits.h>
40 #define COBJMACROS
41 #define NONAMELESSUNION
42 #define NONAMELESSSTRUCT
44 #include "windef.h"
45 #include "winbase.h"
46 #include "winuser.h"
47 #include "objbase.h"
48 #include "winreg.h"
49 #include "shlwapi.h"
50 #include "winternl.h"
51 #include "wine/debug.h"
53 #include "shell32_main.h"
54 #include "shellfolder.h"
55 #include "shfldr.h"
56 #include "shresdef.h"
57 #include "pidl.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(shell);
61 const GUID CLSID_UnixFolder = {0xcc702eb2, 0x7dc5, 0x11d9, {0xc6, 0x87, 0x00, 0x04, 0x23, 0x8a, 0x01, 0xcd}};
62 const GUID CLSID_UnixDosFolder = {0x9d20aae8, 0x0625, 0x44b0, {0x9c, 0xa7, 0x71, 0x88, 0x9c, 0x22, 0x54, 0xd9}};
64 #define ADJUST_THIS(c,m,p) ((c*)(((long)p)-(long)&(((c*)0)->lp##m##Vtbl)))
65 #define STATIC_CAST(i,p) ((i*)&p->lp##i##Vtbl)
67 /* FileStruct reserves one byte for szNames, thus we don't have to
68 * alloc a byte for the terminating '\0' of 'name'. Two of the
69 * additional bytes are for SHITEMID's cb field. One is for IDLDATA's
70 * type field. One is for FileStruct's szNames field, to terminate
71 * the alternate DOS name, which we don't use here.
73 #define SHITEMID_LEN_FROM_NAME_LEN(n) \
74 (sizeof(USHORT)+sizeof(PIDLTYPE)+sizeof(FileStruct)+(n)+sizeof(char))
75 #define NAME_LEN_FROM_LPSHITEMID(s) \
76 (((LPSHITEMID)s)->cb-sizeof(USHORT)-sizeof(PIDLTYPE)-sizeof(FileStruct)-sizeof(char))
78 #define PATHMODE_UNIX 0
79 #define PATHMODE_DOS 1
81 /* UnixFolder object layout and typedef.
83 typedef struct _UnixFolder {
84 const IShellFolder2Vtbl *lpIShellFolder2Vtbl;
85 const IPersistFolder3Vtbl *lpIPersistFolder3Vtbl;
86 const IPersistPropertyBagVtbl *lpIPersistPropertyBagVtbl;
87 const ISFHelperVtbl *lpISFHelperVtbl;
88 LONG m_cRef;
89 CHAR *m_pszPath;
90 LPITEMIDLIST m_pidlLocation;
91 DWORD m_dwPathMode;
92 DWORD m_dwAttributes;
93 const CLSID *m_pCLSID;
94 } UnixFolder;
96 /******************************************************************************
97 * UNIXFS_is_rooted_at_desktop [Internal]
99 * Checks if the unixfs namespace extension is rooted at desktop level.
101 * RETURNS
102 * TRUE, if unixfs is rooted at desktop level
103 * FALSE, if not.
105 BOOL UNIXFS_is_rooted_at_desktop(void) {
106 HKEY hKey;
107 WCHAR *pwszCLSID_UnixDosFolder, wszRootedAtDesktop[69 + CHARS_IN_GUID] = {
108 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
109 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
110 'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
111 'N','a','m','e','S','p','a','c','e','\\',0 };
113 if (FAILED(StringFromCLSID(&CLSID_UnixDosFolder, &pwszCLSID_UnixDosFolder)))
114 return FALSE;
116 lstrcatW(wszRootedAtDesktop, pwszCLSID_UnixDosFolder);
117 CoTaskMemFree(pwszCLSID_UnixDosFolder);
119 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszRootedAtDesktop, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
120 return FALSE;
122 RegCloseKey(hKey);
123 return TRUE;
126 /******************************************************************************
127 * UNIXFS_is_pidl_of_type [INTERNAL]
129 * Checks for the first SHITEMID of an ITEMIDLIST if it passes a filter.
131 * PARAMS
132 * pIDL [I] The ITEMIDLIST to be checked.
133 * fFilter [I] Shell condition flags, which specify the filter.
135 * RETURNS
136 * TRUE, if pIDL is accepted by fFilter
137 * FALSE, otherwise
139 static inline BOOL UNIXFS_is_pidl_of_type(LPITEMIDLIST pIDL, SHCONTF fFilter) {
140 LPPIDLDATA pIDLData = _ILGetDataPointer(pIDL);
141 if (!(fFilter & SHCONTF_INCLUDEHIDDEN) && pIDLData &&
142 (pIDLData->u.file.uFileAttribs & FILE_ATTRIBUTE_HIDDEN))
144 return FALSE;
146 if (_ILIsFolder(pIDL) && (fFilter & SHCONTF_FOLDERS)) return TRUE;
147 if (_ILIsValue(pIDL) && (fFilter & SHCONTF_NONFOLDERS)) return TRUE;
148 return FALSE;
151 /******************************************************************************
152 * UNIXFS_is_dos_device [Internal]
154 * Determines if a unix directory corresponds to any dos device.
156 * PARAMS
157 * statPath [I] The stat struct of the directory, as returned by stat(2).
159 * RETURNS
160 * TRUE, if statPath corresponds to any dos drive letter
161 * FALSE, otherwise
163 static BOOL UNIXFS_is_dos_device(const struct stat *statPath) {
164 struct stat statDrive;
165 char *pszDrivePath;
166 DWORD dwDriveMap;
167 WCHAR wszDosDevice[4] = { 'A', ':', '\\', 0 };
169 for (dwDriveMap = GetLogicalDrives(); dwDriveMap; dwDriveMap >>= 1, wszDosDevice[0]++) {
170 if (!(dwDriveMap & 0x1)) continue;
171 pszDrivePath = wine_get_unix_file_name(wszDosDevice);
172 if (pszDrivePath && !stat(pszDrivePath, &statDrive)) {
173 HeapFree(GetProcessHeap(), 0, pszDrivePath);
174 if ((statPath->st_dev == statDrive.st_dev) && (statPath->st_ino == statDrive.st_ino))
175 return TRUE;
178 return FALSE;
181 /******************************************************************************
182 * UNIXFS_get_unix_path [Internal]
184 * Convert an absolute dos path to an absolute canonicalized unix path.
185 * Evaluate "/.", "/.." and symbolic links.
187 * PARAMS
188 * pszDosPath [I] An absolute dos path
189 * pszCanonicalPath [O] Buffer of length FILENAME_MAX. Will receive the canonical path.
191 * RETURNS
192 * Success, TRUE
193 * Failure, FALSE - Path not existent, too long, insufficient rights, to many symlinks
195 static BOOL UNIXFS_get_unix_path(LPCWSTR pszDosPath, char *pszCanonicalPath)
197 char *pPathTail, *pElement, *pCanonicalTail, szPath[FILENAME_MAX], *pszUnixPath;
198 struct stat fileStat;
200 TRACE("(pszDosPath=%s, pszCanonicalPath=%p)\n", debugstr_w(pszDosPath), pszCanonicalPath);
202 if (!pszDosPath || pszDosPath[1] != ':')
203 return FALSE;
205 pszUnixPath = wine_get_unix_file_name(pszDosPath);
206 if (!pszUnixPath) return FALSE;
207 strcpy(szPath, pszUnixPath);
208 HeapFree(GetProcessHeap(), 0, pszUnixPath);
210 /* pCanonicalTail always points to the end of the canonical path constructed
211 * thus far. pPathTail points to the still to be processed part of the input
212 * path. pElement points to the path element currently investigated.
214 *pszCanonicalPath = '\0';
215 pCanonicalTail = pszCanonicalPath;
216 pPathTail = szPath;
218 do {
219 char cTemp;
220 int cLinks = 0;
222 pElement = pPathTail;
223 pPathTail = strchr(pPathTail+1, '/');
224 if (!pPathTail) /* Last path element may not be terminated by '/'. */
225 pPathTail = pElement + strlen(pElement);
226 /* Temporarily terminate the current path element. Will be restored later. */
227 cTemp = *pPathTail;
228 *pPathTail = '\0';
230 /* Skip "/." path elements */
231 if (!strcmp("/.", pElement)) {
232 *pPathTail = cTemp;
233 continue;
236 /* Remove last element in canonical path for "/.." elements, then skip. */
237 if (!strcmp("/..", pElement)) {
238 char *pTemp = strrchr(pszCanonicalPath, '/');
239 if (pTemp)
240 pCanonicalTail = pTemp;
241 *pCanonicalTail = '\0';
242 *pPathTail = cTemp;
243 continue;
246 /* lstat returns zero on success. */
247 if (lstat(szPath, &fileStat))
248 return FALSE;
250 if (S_ISLNK(fileStat.st_mode)) {
251 char szSymlink[FILENAME_MAX];
252 int cLinkLen, cTailLen;
254 /* Avoid infinite loop for recursive links. */
255 if (++cLinks > 64)
256 return FALSE;
258 cLinkLen = readlink(szPath, szSymlink, FILENAME_MAX);
259 if (cLinkLen < 0)
260 return FALSE;
262 *pPathTail = cTemp;
263 cTailLen = strlen(pPathTail);
265 if (szSymlink[0] == '/') {
266 /* Absolute link. Copy to szPath, concat remaining path and start all over. */
267 if (cLinkLen + cTailLen + 1 > FILENAME_MAX)
268 return FALSE;
270 /* Avoid double slashes. */
271 if (szSymlink[cLinkLen-1] == '/' && pPathTail[0] == '/') {
272 szSymlink[cLinkLen-1] = '\0';
273 cLinkLen--;
276 memcpy(szSymlink + cLinkLen, pPathTail, cTailLen + 1);
277 memcpy(szPath, szSymlink, cLinkLen + cTailLen + 1);
278 *pszCanonicalPath = '\0';
279 pCanonicalTail = pszCanonicalPath;
280 pPathTail = szPath;
281 } else {
282 /* Relative link. Expand into szPath and continue. */
283 char szTemp[FILENAME_MAX];
284 int cTailLen = strlen(pPathTail);
286 if (pElement - szPath + 1 + cLinkLen + cTailLen + 1 > FILENAME_MAX)
287 return FALSE;
289 memcpy(szTemp, pPathTail, cTailLen + 1);
290 memcpy(pElement + 1, szSymlink, cLinkLen);
291 memcpy(pElement + 1 + cLinkLen, szTemp, cTailLen + 1);
292 pPathTail = pElement;
294 } else {
295 /* Regular directory or file. Copy to canonical path */
296 if (pCanonicalTail - pszCanonicalPath + pPathTail - pElement + 1 > FILENAME_MAX)
297 return FALSE;
299 memcpy(pCanonicalTail, pElement, pPathTail - pElement + 1);
300 pCanonicalTail += pPathTail - pElement;
301 *pPathTail = cTemp;
303 } while (pPathTail[0] == '/');
305 TRACE("--> %s\n", debugstr_a(pszCanonicalPath));
307 return TRUE;
310 /******************************************************************************
311 * UNIXFS_build_shitemid [Internal]
313 * Constructs a new SHITEMID for the last component of path 'pszUnixPath' into
314 * buffer 'pIDL'.
316 * PARAMS
317 * pszUnixPath [I] An absolute path. The SHITEMID will be build for the last component.
318 * pIDL [O] SHITEMID will be constructed here.
320 * RETURNS
321 * Success: A pointer to the terminating '\0' character of path.
322 * Failure: NULL
324 * NOTES
325 * Minimum size of pIDL is SHITEMID_LEN_FROM_NAME_LEN(strlen(last_component_of_path)).
326 * If what you need is a PIDLLIST with a single SHITEMID, don't forget to append
327 * a 0 USHORT value.
329 static char* UNIXFS_build_shitemid(char *pszUnixPath, void *pIDL) {
330 LARGE_INTEGER time;
331 FILETIME fileTime;
332 LPPIDLDATA pIDLData;
333 struct stat fileStat;
334 char *pszComponent;
335 int cComponentLen;
337 TRACE("(pszUnixPath=%s, pIDL=%p)\n", debugstr_a(pszUnixPath), pIDL);
339 /* Compute the SHITEMID's length and wipe it. */
340 pszComponent = strrchr(pszUnixPath, '/') + 1;
341 cComponentLen = strlen(pszComponent);
342 memset(pIDL, 0, SHITEMID_LEN_FROM_NAME_LEN(cComponentLen));
343 ((LPSHITEMID)pIDL)->cb = SHITEMID_LEN_FROM_NAME_LEN(cComponentLen) ;
345 /* We are only interested in regular files and directories. */
346 if (stat(pszUnixPath, &fileStat)) return NULL;
347 if (!S_ISDIR(fileStat.st_mode) && !S_ISREG(fileStat.st_mode)) return NULL;
349 /* Set shell32's standard SHITEMID data fields. */
350 pIDLData = _ILGetDataPointer((LPCITEMIDLIST)pIDL);
351 pIDLData->type = S_ISDIR(fileStat.st_mode) ? PT_FOLDER : PT_VALUE;
352 pIDLData->u.file.dwFileSize = (DWORD)fileStat.st_size;
353 RtlSecondsSince1970ToTime( fileStat.st_mtime, &time );
354 fileTime.dwLowDateTime = time.u.LowPart;
355 fileTime.dwHighDateTime = time.u.HighPart;
356 FileTimeToDosDateTime(&fileTime, &pIDLData->u.file.uFileDate, &pIDLData->u.file.uFileTime);
357 pIDLData->u.file.uFileAttribs = 0;
358 if (S_ISDIR(fileStat.st_mode)) pIDLData->u.file.uFileAttribs |= FILE_ATTRIBUTE_DIRECTORY;
359 if (pszComponent[0] == '.') pIDLData->u.file.uFileAttribs |= FILE_ATTRIBUTE_HIDDEN;
360 memcpy(pIDLData->u.file.szNames, pszComponent, cComponentLen);
362 return pszComponent + cComponentLen;
365 /******************************************************************************
366 * UNIXFS_path_to_pidl [Internal]
368 * PARAMS
369 * pUnixFolder [I] If path is relative, pUnixFolder represents the base path
370 * path [I] An absolute unix or dos path or a path relativ to pUnixFolder
371 * ppidl [O] The corresponding ITEMIDLIST. Release with SHFree/ILFree
373 * RETURNS
374 * Success: TRUE
375 * Failure: FALSE, invalid params or out of memory
377 * NOTES
378 * pUnixFolder also carries the information if the path is expected to be unix or dos.
380 static BOOL UNIXFS_path_to_pidl(UnixFolder *pUnixFolder, const WCHAR *path, LPITEMIDLIST *ppidl) {
381 LPITEMIDLIST pidl;
382 int cSubDirs, cPidlLen, cPathLen;
383 char *pSlash, szCompletePath[FILENAME_MAX], *pNextPathElement;
385 TRACE("pUnixFolder=%p, path=%s, ppidl=%p\n", pUnixFolder, debugstr_w(path), ppidl);
387 if (!ppidl || !path)
388 return FALSE;
390 /* Build an absolute path and let pNextPathElement point to the interesting
391 * relative sub-path. We need the absolute path to call 'stat', but the pidl
392 * will only contain the relative part.
394 if ((pUnixFolder->m_dwPathMode == PATHMODE_DOS) && (path[1] == ':'))
396 /* Absolute dos path. Convert to unix */
397 if (!UNIXFS_get_unix_path(path, szCompletePath))
398 return FALSE;
399 pNextPathElement = szCompletePath;
401 else if ((pUnixFolder->m_dwPathMode == PATHMODE_UNIX) && (path[0] == '/'))
403 /* Absolute unix path. Just convert to ANSI. */
404 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath, FILENAME_MAX, NULL, NULL);
405 pNextPathElement = szCompletePath;
407 else
409 /* Relative dos or unix path. Concat with this folder's path */
410 int cBasePathLen = strlen(pUnixFolder->m_pszPath);
411 memcpy(szCompletePath, pUnixFolder->m_pszPath, cBasePathLen);
412 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath + cBasePathLen,
413 FILENAME_MAX - cBasePathLen, NULL, NULL);
414 pNextPathElement = szCompletePath + cBasePathLen - 1;
416 /* If in dos mode, replace '\' with '/' */
417 if (pUnixFolder->m_dwPathMode == PATHMODE_DOS) {
418 char *pBackslash = strchr(pNextPathElement, '\\');
419 while (pBackslash) {
420 *pBackslash = '/';
421 pBackslash = strchr(pBackslash, '\\');
426 /* Special case for the root folder. */
427 if (!strcmp(szCompletePath, "/")) {
428 *ppidl = pidl = (LPITEMIDLIST)SHAlloc(sizeof(USHORT));
429 if (!pidl) return FALSE;
430 pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
431 return TRUE;
434 /* Remove trailing slash, if present */
435 cPathLen = strlen(szCompletePath);
436 if (szCompletePath[cPathLen-1] == '/')
437 szCompletePath[cPathLen-1] = '\0';
439 if ((szCompletePath[0] != '/') || (pNextPathElement[0] != '/')) {
440 ERR("szCompletePath: %s, pNextPathElment: %s\n", szCompletePath, pNextPathElement);
441 return FALSE;
444 /* At this point, we have an absolute unix path in szCompletePath
445 * and the relative portion of it in pNextPathElement. Both starting with '/'
446 * and _not_ terminated by a '/'. */
447 TRACE("complete path: %s, relative path: %s\n", szCompletePath, pNextPathElement);
449 /* Count the number of sub-directories in the path */
450 cSubDirs = 0;
451 pSlash = pNextPathElement;
452 while (pSlash) {
453 cSubDirs++;
454 pSlash = strchr(pSlash+1, '/');
457 /* Allocate enough memory to hold the path. The -cSubDirs is for the '/'
458 * characters, which are not stored in the ITEMIDLIST. */
459 cPidlLen = strlen(pNextPathElement) - cSubDirs + cSubDirs * SHITEMID_LEN_FROM_NAME_LEN(0) + sizeof(USHORT);
460 *ppidl = pidl = (LPITEMIDLIST)SHAlloc(cPidlLen);
461 if (!pidl) return FALSE;
463 /* Concatenate the SHITEMIDs of the sub-directories. */
464 while (*pNextPathElement) {
465 pSlash = strchr(pNextPathElement+1, '/');
466 if (pSlash) *pSlash = '\0';
467 pNextPathElement = UNIXFS_build_shitemid(szCompletePath, pidl);
468 if (pSlash) *pSlash = '/';
470 if (!pNextPathElement) {
471 SHFree(pidl);
472 return FALSE;
474 pidl = ILGetNext(pidl);
476 pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
478 if ((int)pidl-(int)*ppidl+sizeof(USHORT) != cPidlLen) /* We've corrupted the heap :( */
479 ERR("Computed length of pidl incorrect. Please report.\n");
481 return TRUE;
484 /******************************************************************************
485 * UnixFolder
487 * Class whose heap based instances represent unix filesystem directories.
490 static void UnixFolder_Destroy(UnixFolder *pUnixFolder) {
491 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
493 SHFree(pUnixFolder->m_pszPath);
494 ILFree(pUnixFolder->m_pidlLocation);
495 SHFree(pUnixFolder);
498 static HRESULT WINAPI UnixFolder_IShellFolder2_QueryInterface(IShellFolder2 *iface, REFIID riid,
499 void **ppv)
501 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
503 TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
505 if (!ppv) return E_INVALIDARG;
507 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IShellFolder, riid) ||
508 IsEqualIID(&IID_IShellFolder2, riid))
510 *ppv = &This->lpIShellFolder2Vtbl;
511 } else if (IsEqualIID(&IID_IPersistFolder3, riid) || IsEqualIID(&IID_IPersistFolder2, riid) ||
512 IsEqualIID(&IID_IPersistFolder, riid) || IsEqualIID(&IID_IPersist, riid))
514 *ppv = &This->lpIPersistFolder3Vtbl;
515 } else if (IsEqualIID(&IID_IPersistPropertyBag, riid)) {
516 *ppv = &This->lpIPersistPropertyBagVtbl;
517 } else if (IsEqualIID(&IID_ISFHelper, riid)) {
518 *ppv = &This->lpISFHelperVtbl;
519 } else {
520 *ppv = NULL;
521 return E_NOINTERFACE;
524 IUnknown_AddRef((IUnknown*)*ppv);
525 return S_OK;
528 static ULONG WINAPI UnixFolder_IShellFolder2_AddRef(IShellFolder2 *iface) {
529 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
531 TRACE("(iface=%p)\n", iface);
533 return InterlockedIncrement(&This->m_cRef);
536 static ULONG WINAPI UnixFolder_IShellFolder2_Release(IShellFolder2 *iface) {
537 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
538 ULONG cRef;
540 TRACE("(iface=%p)\n", iface);
542 cRef = InterlockedDecrement(&This->m_cRef);
544 if (!cRef)
545 UnixFolder_Destroy(This);
547 return cRef;
550 static HRESULT WINAPI UnixFolder_IShellFolder2_ParseDisplayName(IShellFolder2* iface, HWND hwndOwner,
551 LPBC pbcReserved, LPOLESTR lpszDisplayName, ULONG* pchEaten, LPITEMIDLIST* ppidl,
552 ULONG* pdwAttributes)
554 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
555 BOOL result;
557 TRACE("(iface=%p, hwndOwner=%p, pbcReserved=%p, lpszDisplayName=%s, pchEaten=%p, ppidl=%p, "
558 "pdwAttributes=%p) stub\n", iface, hwndOwner, pbcReserved, debugstr_w(lpszDisplayName),
559 pchEaten, ppidl, pdwAttributes);
561 result = UNIXFS_path_to_pidl(This, lpszDisplayName, ppidl);
562 if (result && pdwAttributes && *pdwAttributes)
564 IShellFolder *pParentSF;
565 LPCITEMIDLIST pidlLast;
566 HRESULT hr;
568 hr = SHBindToParent(*ppidl, &IID_IShellFolder, (LPVOID*)&pParentSF, &pidlLast);
569 if (FAILED(hr)) return E_FAIL;
570 IShellFolder_GetAttributesOf(pParentSF, 1, &pidlLast, pdwAttributes);
571 IShellFolder_Release(pParentSF);
574 if (!result) TRACE("FAILED!\n");
575 return result ? S_OK : E_FAIL;
578 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter);
580 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumObjects(IShellFolder2* iface, HWND hwndOwner,
581 SHCONTF grfFlags, IEnumIDList** ppEnumIDList)
583 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
584 IUnknown *newIterator;
585 HRESULT hr;
587 TRACE("(iface=%p, hwndOwner=%p, grfFlags=%08lx, ppEnumIDList=%p)\n",
588 iface, hwndOwner, grfFlags, ppEnumIDList);
590 newIterator = UnixSubFolderIterator_Constructor(This, grfFlags);
591 hr = IUnknown_QueryInterface(newIterator, &IID_IEnumIDList, (void**)ppEnumIDList);
592 IUnknown_Release(newIterator);
594 return hr;
597 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToObject(IShellFolder2* iface, LPCITEMIDLIST pidl,
598 LPBC pbcReserved, REFIID riid, void** ppvOut)
600 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
601 IPersistFolder3 *persistFolder;
602 LPITEMIDLIST pidlSubFolder;
603 HRESULT hr;
605 TRACE("(iface=%p, pidl=%p, pbcReserver=%p, riid=%p, ppvOut=%p)\n",
606 iface, pidl, pbcReserved, riid, ppvOut);
608 if (!pidl || !pidl->mkid.cb)
609 return E_INVALIDARG;
611 if (This->m_dwPathMode == PATHMODE_DOS)
612 hr = UnixDosFolder_Constructor(NULL, &IID_IPersistFolder3, (void**)&persistFolder);
613 else
614 hr = UnixFolder_Constructor(NULL, &IID_IPersistFolder3, (void**)&persistFolder);
616 if (!SUCCEEDED(hr)) return hr;
617 hr = IPersistFolder_QueryInterface(persistFolder, riid, (void**)ppvOut);
619 pidlSubFolder = ILCombine(This->m_pidlLocation, pidl);
620 IPersistFolder3_Initialize(persistFolder, pidlSubFolder);
621 IPersistFolder3_Release(persistFolder);
622 ILFree(pidlSubFolder);
624 return hr;
627 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToStorage(IShellFolder2* This, LPCITEMIDLIST pidl,
628 LPBC pbcReserved, REFIID riid, void** ppvObj)
630 TRACE("stub\n");
631 return E_NOTIMPL;
634 static HRESULT WINAPI UnixFolder_IShellFolder2_CompareIDs(IShellFolder2* iface, LPARAM lParam,
635 LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
637 BOOL isEmpty1, isEmpty2;
638 HRESULT hr = E_FAIL;
639 LPITEMIDLIST firstpidl;
640 IShellFolder2 *psf;
641 int compare;
643 TRACE("(iface=%p, lParam=%ld, pidl1=%p, pidl2=%p)\n", iface, lParam, pidl1, pidl2);
645 isEmpty1 = !pidl1 || !pidl1->mkid.cb;
646 isEmpty2 = !pidl2 || !pidl2->mkid.cb;
648 if (isEmpty1 && isEmpty2)
649 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
650 else if (isEmpty1)
651 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
652 else if (isEmpty2)
653 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
655 if (_ILIsFolder(pidl1) && !_ILIsFolder(pidl2))
656 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
657 if (!_ILIsFolder(pidl1) && _ILIsFolder(pidl2))
658 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
660 compare = CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
661 _ILGetTextPointer(pidl1), NAME_LEN_FROM_LPSHITEMID(pidl1),
662 _ILGetTextPointer(pidl2), NAME_LEN_FROM_LPSHITEMID(pidl2));
664 if ((compare == CSTR_LESS_THAN) || (compare == CSTR_GREATER_THAN))
665 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)((compare == CSTR_LESS_THAN)?-1:1));
667 if (pidl1->mkid.cb < pidl2->mkid.cb)
668 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
669 else if (pidl1->mkid.cb > pidl2->mkid.cb)
670 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
672 firstpidl = ILCloneFirst(pidl1);
673 pidl1 = ILGetNext(pidl1);
674 pidl2 = ILGetNext(pidl2);
676 hr = IShellFolder2_BindToObject(iface, firstpidl, NULL, &IID_IShellFolder, (LPVOID*)&psf);
677 if (SUCCEEDED(hr)) {
678 hr = IShellFolder_CompareIDs(psf, lParam, pidl1, pidl2);
679 IShellFolder2_Release(psf);
682 ILFree(firstpidl);
683 return hr;
686 static HRESULT WINAPI UnixFolder_IShellFolder2_CreateViewObject(IShellFolder2* iface, HWND hwndOwner,
687 REFIID riid, void** ppv)
689 HRESULT hr = E_INVALIDARG;
691 TRACE("(iface=%p, hwndOwner=%p, riid=%p, ppv=%p) stub\n", iface, hwndOwner, riid, ppv);
693 if (!ppv) return E_INVALIDARG;
694 *ppv = NULL;
696 if (IsEqualIID(&IID_IShellView, riid)) {
697 LPSHELLVIEW pShellView;
699 pShellView = IShellView_Constructor((IShellFolder*)iface);
700 if (pShellView) {
701 hr = IShellView_QueryInterface(pShellView, riid, ppv);
702 IShellView_Release(pShellView);
706 return hr;
709 static HRESULT WINAPI UnixFolder_IShellFolder2_GetAttributesOf(IShellFolder2* iface, UINT cidl,
710 LPCITEMIDLIST* apidl, SFGAOF* rgfInOut)
712 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
713 HRESULT hr = S_OK;
715 TRACE("(iface=%p, cidl=%u, apidl=%p, rgfInOut=%p)\n", iface, cidl, apidl, rgfInOut);
717 if (!rgfInOut || (cidl && !apidl))
718 return E_INVALIDARG;
720 if (cidl == 0) {
721 *rgfInOut &= This->m_dwAttributes;
722 } else {
723 char szAbsolutePath[FILENAME_MAX], *pszRelativePath;
724 UINT i;
726 *rgfInOut &= SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME|
727 SFGAO_FILESYSTEM;
728 lstrcpyA(szAbsolutePath, This->m_pszPath);
729 pszRelativePath = szAbsolutePath + lstrlenA(szAbsolutePath);
730 for (i=0; i<cidl; i++) {
731 if ((*rgfInOut & SFGAO_FILESYSTEM) && !(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
732 struct stat fileStat;
733 char *pszName = _ILGetTextPointer(apidl[i]);
734 if (!pszName) return E_INVALIDARG;
735 lstrcpyA(pszRelativePath, pszName);
736 if (stat(szAbsolutePath, &fileStat) || !UNIXFS_is_dos_device(&fileStat))
737 *rgfInOut &= ~SFGAO_FILESYSTEM;
739 if (!_ILIsFolder(apidl[i]))
740 *rgfInOut &= ~(SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR);
744 return hr;
747 static HRESULT WINAPI UnixFolder_IShellFolder2_GetUIObjectOf(IShellFolder2* iface, HWND hwndOwner,
748 UINT cidl, LPCITEMIDLIST* apidl, REFIID riid, UINT* prgfInOut, void** ppvOut)
750 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
752 TRACE("(iface=%p, hwndOwner=%p, cidl=%d, apidl=%p, riid=%s, prgfInOut=%p, ppv=%p)\n",
753 iface, hwndOwner, cidl, apidl, debugstr_guid(riid), prgfInOut, ppvOut);
755 if (IsEqualIID(&IID_IContextMenu, riid)) {
756 *ppvOut = ISvItemCm_Constructor((IShellFolder*)iface, This->m_pidlLocation, apidl, cidl);
757 return S_OK;
758 } else if (IsEqualIID(&IID_IDataObject, riid)) {
759 *ppvOut = IDataObject_Constructor(hwndOwner, This->m_pidlLocation, apidl, cidl);
760 return S_OK;
761 } else if (IsEqualIID(&IID_IExtractIconA, riid)) {
762 LPITEMIDLIST pidl;
763 if (cidl != 1) return E_FAIL;
764 pidl = ILCombine(This->m_pidlLocation, apidl[0]);
765 *ppvOut = (LPVOID)IExtractIconA_Constructor(pidl);
766 SHFree(pidl);
767 return S_OK;
768 } else if (IsEqualIID(&IID_IExtractIconW, riid)) {
769 LPITEMIDLIST pidl;
770 if (cidl != 1) return E_FAIL;
771 pidl = ILCombine(This->m_pidlLocation, apidl[0]);
772 *ppvOut = (LPVOID)IExtractIconW_Constructor(pidl);
773 SHFree(pidl);
774 return S_OK;
775 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
776 FIXME("IDropTarget\n");
777 return E_FAIL;
778 } else if (IsEqualIID(&IID_IShellLinkW, riid)) {
779 FIXME("IShellLinkW\n");
780 return E_FAIL;
781 } else if (IsEqualIID(&IID_IShellLinkA, riid)) {
782 FIXME("IShellLinkA\n");
783 return E_FAIL;
784 } else {
785 FIXME("Unknown interface %s in GetUIObjectOf\n", debugstr_guid(riid));
786 return E_NOINTERFACE;
790 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDisplayNameOf(IShellFolder2* iface,
791 LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET* lpName)
793 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
794 HRESULT hr = S_OK;
796 TRACE("(iface=%p, pidl=%p, uFlags=%lx, lpName=%p)\n", iface, pidl, uFlags, lpName);
798 if ((GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) &&
799 (GET_SHGDN_RELATION(uFlags) != SHGDN_INFOLDER))
801 if (!pidl->mkid.cb) {
802 lpName->uType = STRRET_CSTR;
803 if (This->m_dwPathMode == PATHMODE_UNIX) {
804 strcpy(lpName->u.cStr, This->m_pszPath);
805 } else {
806 WCHAR *pwszDosPath = wine_get_dos_file_name(This->m_pszPath);
807 if (!pwszDosPath)
808 return HRESULT_FROM_WIN32(GetLastError());
809 PathRemoveBackslashW(pwszDosPath);
810 WideCharToMultiByte(CP_UNIXCP, 0, pwszDosPath, -1, lpName->u.cStr, MAX_PATH, NULL, NULL);
811 HeapFree(GetProcessHeap(), 0, pwszDosPath);
813 } else {
814 IShellFolder *pSubFolder;
815 USHORT emptyIDL = 0;
817 hr = IShellFolder_BindToObject(iface, pidl, NULL, &IID_IShellFolder, (void**)&pSubFolder);
818 if (!SUCCEEDED(hr)) return hr;
820 hr = IShellFolder_GetDisplayNameOf(pSubFolder, (LPITEMIDLIST)&emptyIDL, uFlags, lpName);
821 IShellFolder_Release(pSubFolder);
823 } else {
824 char *pszFileName = _ILGetTextPointer(pidl);
825 lpName->uType = STRRET_CSTR;
826 strcpy(lpName->u.cStr, pszFileName ? pszFileName : "");
829 /* If in dos mode, do some post-processing on the path.
830 * (e.g. remove filename extension, if uFlags & SHGDN_FOREDITING)
832 if (SUCCEEDED(hr) && This->m_dwPathMode == PATHMODE_DOS && !_ILIsFolder(pidl))
833 SHELL_FS_ProcessDisplayFilename(lpName->u.cStr, uFlags);
835 TRACE("--> %s\n", lpName->u.cStr);
837 return hr;
840 static HRESULT WINAPI UnixFolder_IShellFolder2_SetNameOf(IShellFolder2* iface, HWND hwnd,
841 LPCITEMIDLIST pidl, LPCOLESTR lpszName, SHGDNF uFlags, LPITEMIDLIST* ppidlOut)
843 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
845 char szSrc[FILENAME_MAX], szDest[FILENAME_MAX];
846 WCHAR *pwszDosDest;
847 int cBasePathLen = lstrlenA(This->m_pszPath);
848 struct stat statDest;
849 LPITEMIDLIST pidlSrc, pidlDest;
851 TRACE("(iface=%p, hwnd=%p, pidl=%p, lpszName=%s, uFlags=0x%08lx, ppidlOut=%p)\n",
852 iface, hwnd, pidl, debugstr_w(lpszName), uFlags, ppidlOut);
854 /* pidl has to contain a single non-empty SHITEMID */
855 if (_ILIsDesktop(pidl) || !_ILIsPidlSimple(pidl) || !_ILGetTextPointer(pidl))
856 return E_INVALIDARG;
858 if (ppidlOut)
859 *ppidlOut = NULL;
861 /* build source path */
862 memcpy(szSrc, This->m_pszPath, cBasePathLen);
863 lstrcpyA(szSrc+cBasePathLen, _ILGetTextPointer(pidl));
865 /* build destination path */
866 if (uFlags & SHGDN_FORPARSING) { /* absolute path in lpszName */
867 WideCharToMultiByte(CP_UNIXCP, 0, lpszName, -1, szDest, FILENAME_MAX, NULL, NULL);
868 } else {
869 WCHAR wszSrcRelative[MAX_PATH];
870 memcpy(szDest, This->m_pszPath, cBasePathLen);
871 WideCharToMultiByte(CP_UNIXCP, 0, lpszName, -1, szDest+cBasePathLen,
872 FILENAME_MAX-cBasePathLen, NULL, NULL);
874 /* uFlags is SHGDN_FOREDITING of SHGDN_FORADDRESSBAR. If the filename's
875 * extension is hidden to the user, we have to append it. */
876 if (_ILSimpleGetTextW(pidl, wszSrcRelative, MAX_PATH) &&
877 SHELL_FS_HideExtension(wszSrcRelative))
879 char *pszExt = PathFindExtensionA(_ILGetTextPointer(pidl));
880 lstrcatA(szDest, pszExt);
884 TRACE("src=%s dest=%s\n", szSrc, szDest);
886 /* Fail, if destination does already exist */
887 if (!stat(szDest, &statDest))
888 return E_FAIL;
890 /* Rename the file */
891 if (rename(szSrc, szDest))
892 return E_FAIL;
894 /* Build a pidl for the path of the renamed file */
895 pwszDosDest = wine_get_dos_file_name(szDest);
896 if (!pwszDosDest || !UNIXFS_path_to_pidl(This, pwszDosDest, &pidlDest)) {
897 HeapFree(GetProcessHeap(), 0, pwszDosDest);
898 rename(szDest, szSrc); /* Undo the renaming */
899 return E_FAIL;
902 /* Inform the shell */
903 pidlSrc = ILCombine(This->m_pidlLocation, pidl);
904 if (_ILIsFolder(ILFindLastID(pidlDest)))
905 SHChangeNotify(SHCNE_RENAMEFOLDER, SHCNF_IDLIST, pidlSrc, pidlDest);
906 else
907 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_IDLIST, pidlSrc, pidlDest);
908 ILFree(pidlSrc);
909 ILFree(pidlDest);
911 if (ppidlOut)
912 _ILCreateFromPathW(pwszDosDest, ppidlOut);
914 HeapFree(GetProcessHeap(), 0, pwszDosDest);
915 return S_OK;
918 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumSearches(IShellFolder2* iface,
919 IEnumExtraSearch **ppEnum)
921 TRACE("stub\n");
922 return E_NOTIMPL;
925 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumn(IShellFolder2* iface,
926 DWORD dwReserved, ULONG *pSort, ULONG *pDisplay)
928 TRACE("stub\n");
929 return E_NOTIMPL;
932 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumnState(IShellFolder2* iface,
933 UINT iColumn, SHCOLSTATEF *pcsFlags)
935 TRACE("stub\n");
936 return E_NOTIMPL;
939 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultSearchGUID(IShellFolder2* iface,
940 GUID *pguid)
942 TRACE("stub\n");
943 return E_NOTIMPL;
946 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsEx(IShellFolder2* iface,
947 LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
949 TRACE("stub\n");
950 return E_NOTIMPL;
953 #define SHELLVIEWCOLUMNS 7
955 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsOf(IShellFolder2* iface,
956 LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
958 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
959 HRESULT hr = E_FAIL;
960 struct passwd *pPasswd;
961 struct group *pGroup;
962 static const shvheader SFHeader[SHELLVIEWCOLUMNS] = {
963 {IDS_SHV_COLUMN1, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
964 {IDS_SHV_COLUMN2, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
965 {IDS_SHV_COLUMN3, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
966 {IDS_SHV_COLUMN4, SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12},
967 {IDS_SHV_COLUMN5, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 9},
968 {IDS_SHV_COLUMN10, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
969 {IDS_SHV_COLUMN11, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7}
972 TRACE("(iface=%p, pidl=%p, iColumn=%d, psd=%p) stub\n", iface, pidl, iColumn, psd);
974 if (!psd || iColumn >= SHELLVIEWCOLUMNS)
975 return E_INVALIDARG;
977 if (!pidl) {
978 psd->fmt = SFHeader[iColumn].fmt;
979 psd->cxChar = SFHeader[iColumn].cxChar;
980 psd->str.uType = STRRET_CSTR;
981 LoadStringA(shell32_hInstance, SFHeader[iColumn].colnameid, psd->str.u.cStr, MAX_PATH);
982 return S_OK;
983 } else {
984 struct stat statItem;
985 if (iColumn == 4 || iColumn == 5 || iColumn == 6) {
986 char szPath[FILENAME_MAX], *pszFile = _ILGetTextPointer(pidl);
987 if (!pszFile)
988 return E_INVALIDARG;
989 lstrcpyA(szPath, This->m_pszPath);
990 lstrcatA(szPath, pszFile);
991 if (stat(szPath, &statItem))
992 return E_INVALIDARG;
994 psd->str.u.cStr[0] = '\0';
995 psd->str.uType = STRRET_CSTR;
996 switch (iColumn) {
997 case 0:
998 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL|SHGDN_INFOLDER, &psd->str);
999 break;
1000 case 1:
1001 _ILGetFileSize(pidl, psd->str.u.cStr, MAX_PATH);
1002 break;
1003 case 2:
1004 _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
1005 break;
1006 case 3:
1007 _ILGetFileDate(pidl, psd->str.u.cStr, MAX_PATH);
1008 break;
1009 case 4:
1010 psd->str.u.cStr[0] = S_ISDIR(statItem.st_mode) ? 'd' : '-';
1011 psd->str.u.cStr[1] = (statItem.st_mode & S_IRUSR) ? 'r' : '-';
1012 psd->str.u.cStr[2] = (statItem.st_mode & S_IWUSR) ? 'w' : '-';
1013 psd->str.u.cStr[3] = (statItem.st_mode & S_IXUSR) ? 'x' : '-';
1014 psd->str.u.cStr[4] = (statItem.st_mode & S_IRGRP) ? 'r' : '-';
1015 psd->str.u.cStr[5] = (statItem.st_mode & S_IWGRP) ? 'w' : '-';
1016 psd->str.u.cStr[6] = (statItem.st_mode & S_IXGRP) ? 'x' : '-';
1017 psd->str.u.cStr[7] = (statItem.st_mode & S_IROTH) ? 'r' : '-';
1018 psd->str.u.cStr[8] = (statItem.st_mode & S_IWOTH) ? 'w' : '-';
1019 psd->str.u.cStr[9] = (statItem.st_mode & S_IXOTH) ? 'x' : '-';
1020 psd->str.u.cStr[10] = '\0';
1021 break;
1022 case 5:
1023 pPasswd = getpwuid(statItem.st_uid);
1024 if (pPasswd) strcpy(psd->str.u.cStr, pPasswd->pw_name);
1025 break;
1026 case 6:
1027 pGroup = getgrgid(statItem.st_gid);
1028 if (pGroup) strcpy(psd->str.u.cStr, pGroup->gr_name);
1029 break;
1033 return hr;
1036 static HRESULT WINAPI UnixFolder_IShellFolder2_MapColumnToSCID(IShellFolder2* iface, UINT iColumn,
1037 SHCOLUMNID *pscid)
1039 TRACE("stub\n");
1040 return E_NOTIMPL;
1043 /* VTable for UnixFolder's IShellFolder2 interface.
1045 static const IShellFolder2Vtbl UnixFolder_IShellFolder2_Vtbl = {
1046 UnixFolder_IShellFolder2_QueryInterface,
1047 UnixFolder_IShellFolder2_AddRef,
1048 UnixFolder_IShellFolder2_Release,
1049 UnixFolder_IShellFolder2_ParseDisplayName,
1050 UnixFolder_IShellFolder2_EnumObjects,
1051 UnixFolder_IShellFolder2_BindToObject,
1052 UnixFolder_IShellFolder2_BindToStorage,
1053 UnixFolder_IShellFolder2_CompareIDs,
1054 UnixFolder_IShellFolder2_CreateViewObject,
1055 UnixFolder_IShellFolder2_GetAttributesOf,
1056 UnixFolder_IShellFolder2_GetUIObjectOf,
1057 UnixFolder_IShellFolder2_GetDisplayNameOf,
1058 UnixFolder_IShellFolder2_SetNameOf,
1059 UnixFolder_IShellFolder2_GetDefaultSearchGUID,
1060 UnixFolder_IShellFolder2_EnumSearches,
1061 UnixFolder_IShellFolder2_GetDefaultColumn,
1062 UnixFolder_IShellFolder2_GetDefaultColumnState,
1063 UnixFolder_IShellFolder2_GetDetailsEx,
1064 UnixFolder_IShellFolder2_GetDetailsOf,
1065 UnixFolder_IShellFolder2_MapColumnToSCID
1068 static HRESULT WINAPI UnixFolder_IPersistFolder3_QueryInterface(IPersistFolder3* This, REFIID riid,
1069 void** ppvObject)
1071 return UnixFolder_IShellFolder2_QueryInterface(
1072 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder3, This), riid, ppvObject);
1075 static ULONG WINAPI UnixFolder_IPersistFolder3_AddRef(IPersistFolder3* This)
1077 return UnixFolder_IShellFolder2_AddRef(
1078 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder3, This));
1081 static ULONG WINAPI UnixFolder_IPersistFolder3_Release(IPersistFolder3* This)
1083 return UnixFolder_IShellFolder2_Release(
1084 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder3, This));
1087 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetClassID(IPersistFolder3* iface, CLSID* pClassID)
1089 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1091 TRACE("(iface=%p, pClassId=%p)\n", iface, pClassID);
1093 if (!pClassID)
1094 return E_INVALIDARG;
1096 memcpy(pClassID, This->m_pCLSID, sizeof(CLSID));
1097 return S_OK;
1100 static HRESULT WINAPI UnixFolder_IPersistFolder3_Initialize(IPersistFolder3* iface, LPCITEMIDLIST pidl)
1102 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1103 struct stat statPrefix;
1104 LPCITEMIDLIST current = pidl, root;
1105 DWORD dwPathLen;
1106 char *pNextDir, szBasePath[FILENAME_MAX] = "/";
1108 TRACE("(iface=%p, pidl=%p)\n", iface, pidl);
1110 /* Find the UnixFolderClass root */
1111 while (current->mkid.cb) {
1112 if (_ILIsSpecialFolder(current) &&
1113 (IsEqualIID(&CLSID_UnixFolder, _ILGetGUIDPointer(current)) ||
1114 IsEqualIID(&CLSID_UnixDosFolder, _ILGetGUIDPointer(current))))
1116 break;
1118 current = ILGetNext(current);
1121 if (current && current->mkid.cb) {
1122 root = current = ILGetNext(current);
1123 dwPathLen = 2; /* For the '/' prefix and the terminating '\0' */
1124 } else if (_ILIsDesktop(pidl) || _ILIsValue(pidl) || _ILIsFolder(pidl)) {
1125 /* Path rooted at Desktop */
1126 WCHAR wszDesktopPath[MAX_PATH];
1127 if (FAILED(SHGetSpecialFolderPathW(0, wszDesktopPath, CSIDL_DESKTOP, FALSE)))
1128 return E_FAIL;
1129 PathAddBackslashW(wszDesktopPath);
1130 if (!UNIXFS_get_unix_path(wszDesktopPath, szBasePath))
1131 return E_FAIL;
1132 dwPathLen = strlen(szBasePath) + 1;
1133 root = current = pidl;
1134 } else {
1135 ERR("Unknown pidl type!\n");
1136 pdump(pidl);
1137 return E_INVALIDARG;
1140 /* Determine the path's length bytes */
1141 while (current && current->mkid.cb) {
1142 dwPathLen += NAME_LEN_FROM_LPSHITEMID(current) + 1; /* For the '/' */
1143 current = ILGetNext(current);
1146 /* Build the path */
1147 This->m_dwAttributes = SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME;
1148 This->m_pidlLocation = ILClone(pidl);
1149 This->m_pszPath = pNextDir = SHAlloc(dwPathLen);
1150 if (!This->m_pszPath || !This->m_pidlLocation) {
1151 WARN("SHAlloc failed!\n");
1152 return E_FAIL;
1154 current = root;
1155 strcpy(pNextDir, szBasePath);
1156 pNextDir += strlen(szBasePath);
1157 if (This->m_dwPathMode == PATHMODE_UNIX)
1158 This->m_dwAttributes |= SFGAO_FILESYSTEM;
1159 if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
1160 *pNextDir = '\0';
1161 if (!stat(This->m_pszPath, &statPrefix) && UNIXFS_is_dos_device(&statPrefix))
1162 This->m_dwAttributes |= SFGAO_FILESYSTEM;
1164 while (current && current->mkid.cb) {
1165 memcpy(pNextDir, _ILGetTextPointer(current), NAME_LEN_FROM_LPSHITEMID(current));
1166 pNextDir += NAME_LEN_FROM_LPSHITEMID(current);
1167 if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
1168 *pNextDir = '\0';
1169 if (!stat(This->m_pszPath, &statPrefix) && UNIXFS_is_dos_device(&statPrefix))
1170 This->m_dwAttributes |= SFGAO_FILESYSTEM;
1172 *pNextDir++ = '/';
1173 current = ILGetNext(current);
1175 *pNextDir='\0';
1177 return S_OK;
1180 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetCurFolder(IPersistFolder3* iface, LPITEMIDLIST* ppidl)
1182 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1184 TRACE ("(iface=%p, ppidl=%p)\n", iface, ppidl);
1186 if (!ppidl)
1187 return E_POINTER;
1188 *ppidl = ILClone (This->m_pidlLocation);
1189 return S_OK;
1192 static HRESULT WINAPI UnixFolder_IPersistFolder3_InitializeEx(IPersistFolder3 *iface, IBindCtx *pbc,
1193 LPCITEMIDLIST pidlRoot, const PERSIST_FOLDER_TARGET_INFO *ppfti)
1195 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1196 WCHAR wszTargetDosPath[MAX_PATH];
1197 char szTargetPath[FILENAME_MAX] = "";
1199 TRACE("(iface=%p, pbc=%p, pidlRoot=%p, ppfti=%p)\n", iface, pbc, pidlRoot, ppfti);
1201 /* If no PERSIST_FOLDER_TARGET_INFO is given InitializeEx is equivalent to Initialize. */
1202 if (!ppfti)
1203 return IPersistFolder3_Initialize(iface, pidlRoot);
1205 if (ppfti->csidl != -1) {
1206 if (SUCCEEDED(SHGetFolderPathW(0, ppfti->csidl, NULL, 0, wszTargetDosPath))) {
1207 UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath);
1209 } else if (*ppfti->szTargetParsingName) {
1210 UNIXFS_get_unix_path(ppfti->szTargetParsingName, szTargetPath);
1211 } else if (ppfti->pidlTargetFolder) {
1212 if (SHGetPathFromIDListW(ppfti->pidlTargetFolder, wszTargetDosPath)) {
1213 UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath);
1217 This->m_pszPath = SHAlloc(lstrlenA(szTargetPath)+1);
1218 if (!This->m_pszPath)
1219 return E_FAIL;
1220 lstrcpyA(This->m_pszPath, szTargetPath);
1221 This->m_pidlLocation = ILClone(pidlRoot);
1222 This->m_dwAttributes = (ppfti->dwAttributes == -1) ? ppfti->dwAttributes :
1223 SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME|SFGAO_FILESYSTEM;
1225 return S_OK;
1228 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetFolderTargetInfo(IPersistFolder3 *iface,
1229 PERSIST_FOLDER_TARGET_INFO *ppfti)
1231 FIXME("(iface=%p, ppfti=%p) stub\n", iface, ppfti);
1232 return E_NOTIMPL;
1235 /* VTable for UnixFolder's IPersistFolder interface.
1237 static const IPersistFolder3Vtbl UnixFolder_IPersistFolder3_Vtbl = {
1238 UnixFolder_IPersistFolder3_QueryInterface,
1239 UnixFolder_IPersistFolder3_AddRef,
1240 UnixFolder_IPersistFolder3_Release,
1241 UnixFolder_IPersistFolder3_GetClassID,
1242 UnixFolder_IPersistFolder3_Initialize,
1243 UnixFolder_IPersistFolder3_GetCurFolder,
1244 UnixFolder_IPersistFolder3_InitializeEx,
1245 UnixFolder_IPersistFolder3_GetFolderTargetInfo
1248 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_QueryInterface(IPersistPropertyBag* This,
1249 REFIID riid, void** ppvObject)
1251 return UnixFolder_IShellFolder2_QueryInterface(
1252 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistPropertyBag, This), riid, ppvObject);
1255 static ULONG WINAPI UnixFolder_IPersistPropertyBag_AddRef(IPersistPropertyBag* This)
1257 return UnixFolder_IShellFolder2_AddRef(
1258 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistPropertyBag, This));
1261 static ULONG WINAPI UnixFolder_IPersistPropertyBag_Release(IPersistPropertyBag* This)
1263 return UnixFolder_IShellFolder2_Release(
1264 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistPropertyBag, This));
1267 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_GetClassID(IPersistPropertyBag* iface,
1268 CLSID* pClassID)
1270 return UnixFolder_IPersistFolder3_GetClassID(
1271 (IPersistFolder3*)&ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)->lpIPersistFolder3Vtbl,
1272 pClassID);
1275 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_InitNew(IPersistPropertyBag* iface)
1277 TRACE("() stub\n");
1278 return E_NOTIMPL;
1281 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_Load(IPersistPropertyBag *iface,
1282 IPropertyBag *pPropertyBag, IErrorLog *pErrorLog)
1284 TRACE("() stub\n");
1285 return E_NOTIMPL;
1288 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_Save(IPersistPropertyBag *iface,
1289 IPropertyBag *pPropertyBag, BOOL fClearDirty, BOOL fSaveAllProperties)
1291 TRACE("() stub\n");
1292 return E_NOTIMPL;
1295 /* VTable for UnixFolder's IPersistPropertyBag interface.
1297 static const IPersistPropertyBagVtbl UnixFolder_IPersistPropertyBag_Vtbl = {
1298 UnixFolder_IPersistPropertyBag_QueryInterface,
1299 UnixFolder_IPersistPropertyBag_AddRef,
1300 UnixFolder_IPersistPropertyBag_Release,
1301 UnixFolder_IPersistPropertyBag_GetClassID,
1302 UnixFolder_IPersistPropertyBag_InitNew,
1303 UnixFolder_IPersistPropertyBag_Load,
1304 UnixFolder_IPersistPropertyBag_Save
1307 static HRESULT WINAPI UnixFolder_ISFHelper_QueryInterface(ISFHelper* iface, REFIID riid,
1308 void** ppvObject)
1310 return UnixFolder_IShellFolder2_QueryInterface(
1311 (IShellFolder2*)ADJUST_THIS(UnixFolder, ISFHelper, iface), riid, ppvObject);
1314 static ULONG WINAPI UnixFolder_ISFHelper_AddRef(ISFHelper* iface)
1316 return UnixFolder_IShellFolder2_AddRef(
1317 (IShellFolder2*)ADJUST_THIS(UnixFolder, ISFHelper, iface));
1320 static ULONG WINAPI UnixFolder_ISFHelper_Release(ISFHelper* iface)
1322 return UnixFolder_IShellFolder2_Release(
1323 (IShellFolder2*)ADJUST_THIS(UnixFolder, ISFHelper, iface));
1326 static HRESULT WINAPI UnixFolder_ISFHelper_GetUniqueName(ISFHelper* iface, LPSTR lpName, UINT uLen)
1328 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1329 IEnumIDList *pEnum;
1330 HRESULT hr;
1331 LPITEMIDLIST pidlElem;
1332 DWORD dwFetched;
1333 int i;
1334 static const char szNewFolder[] = "New Folder";
1336 TRACE("(iface=%p, lpName=%p, uLen=%u)\n", iface, lpName, uLen);
1338 if (uLen < sizeof(szNewFolder)+3)
1339 return E_INVALIDARG;
1341 hr = IShellFolder2_EnumObjects(STATIC_CAST(IShellFolder2, This), 0,
1342 SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN, &pEnum);
1343 if (SUCCEEDED(hr)) {
1344 lstrcpyA(lpName, szNewFolder);
1345 IEnumIDList_Reset(pEnum);
1346 i = 2;
1347 while ((IEnumIDList_Next(pEnum, 1, &pidlElem, &dwFetched) == S_OK) && (dwFetched == 1)) {
1348 if (!strcasecmp(_ILGetTextPointer(pidlElem), lpName)) {
1349 IEnumIDList_Reset(pEnum);
1350 sprintf(lpName, "%s %d", szNewFolder, i++);
1351 if (i > 99) {
1352 hr = E_FAIL;
1353 break;
1357 IEnumIDList_Release(pEnum);
1359 return hr;
1362 static HRESULT WINAPI UnixFolder_ISFHelper_AddFolder(ISFHelper* iface, HWND hwnd, LPCSTR pszName,
1363 LPITEMIDLIST* ppidlOut)
1365 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1366 char szNewDir[FILENAME_MAX];
1368 TRACE("(iface=%p, hwnd=%p, pszName=%s, ppidlOut=%p)\n", iface, hwnd, pszName, ppidlOut);
1370 if (ppidlOut)
1371 *ppidlOut = NULL;
1373 lstrcpyA(szNewDir, This->m_pszPath);
1374 lstrcatA(szNewDir, pszName);
1376 if (mkdir(szNewDir, 0755)) {
1377 char szMessage[256 + FILENAME_MAX];
1378 char szCaption[256];
1380 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_DENIED, szCaption, sizeof(szCaption));
1381 sprintf(szMessage, szCaption, szNewDir);
1382 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_CAPTION, szCaption, sizeof(szCaption));
1383 MessageBoxA(hwnd, szMessage, szCaption, MB_OK | MB_ICONEXCLAMATION);
1385 return E_FAIL;
1386 } else {
1387 LPITEMIDLIST pidlRelative;
1388 WCHAR wszName[MAX_PATH];
1390 /* Inform the shell */
1391 MultiByteToWideChar(CP_UNIXCP, 0, pszName, -1, wszName, MAX_PATH);
1392 if (UNIXFS_path_to_pidl(This, wszName, &pidlRelative)) {
1393 LPITEMIDLIST pidlAbsolute = ILCombine(This->m_pidlLocation, pidlRelative);
1394 if (ppidlOut)
1395 *ppidlOut = pidlRelative;
1396 else
1397 ILFree(pidlRelative);
1398 SHChangeNotify(SHCNE_MKDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1399 ILFree(pidlAbsolute);
1401 return S_OK;
1405 static HRESULT WINAPI UnixFolder_ISFHelper_DeleteItems(ISFHelper* iface, UINT cidl,
1406 LPCITEMIDLIST* apidl)
1408 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1409 char szAbsolute[FILENAME_MAX], *pszRelative;
1410 LPITEMIDLIST pidlAbsolute;
1411 HRESULT hr = S_OK;
1412 UINT i;
1414 TRACE("(iface=%p, cidl=%d, apidl=%p)\n", iface, cidl, apidl);
1416 lstrcpyA(szAbsolute, This->m_pszPath);
1417 pszRelative = szAbsolute + lstrlenA(szAbsolute);
1419 for (i=0; i<cidl && SUCCEEDED(hr); i++) {
1420 lstrcpyA(pszRelative, _ILGetTextPointer(apidl[i]));
1421 pidlAbsolute = ILCombine(This->m_pidlLocation, apidl[i]);
1422 if (_ILIsFolder(apidl[i])) {
1423 if (rmdir(szAbsolute)) {
1424 hr = E_FAIL;
1425 } else {
1426 SHChangeNotify(SHCNE_RMDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1428 } else if (_ILIsValue(apidl[i])) {
1429 if (unlink(szAbsolute)) {
1430 hr = E_FAIL;
1431 } else {
1432 SHChangeNotify(SHCNE_DELETE, SHCNF_IDLIST, pidlAbsolute, NULL);
1435 ILFree(pidlAbsolute);
1438 return hr;
1441 static HRESULT WINAPI UnixFolder_ISFHelper_CopyItems(ISFHelper* iface, IShellFolder *psfFrom,
1442 UINT cidl, LPCITEMIDLIST *apidl)
1444 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1445 DWORD dwAttributes;
1446 UINT i;
1447 HRESULT hr;
1448 char szAbsoluteDst[FILENAME_MAX], *pszRelativeDst;
1450 TRACE("(iface=%p, psfFrom=%p, cidl=%d, apidl=%p): semi-stub\n", iface, psfFrom, cidl, apidl);
1452 if (!psfFrom || !cidl || !apidl)
1453 return E_INVALIDARG;
1455 /* All source items have to be filesystem items. */
1456 dwAttributes = SFGAO_FILESYSTEM;
1457 hr = IShellFolder_GetAttributesOf(psfFrom, cidl, apidl, &dwAttributes);
1458 if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM))
1459 return E_INVALIDARG;
1461 lstrcpyA(szAbsoluteDst, This->m_pszPath);
1462 pszRelativeDst = szAbsoluteDst + strlen(szAbsoluteDst);
1464 for (i=0; i<cidl; i++) {
1465 WCHAR wszSrc[MAX_PATH];
1466 char szSrc[FILENAME_MAX];
1467 STRRET strret;
1469 /* Build the unix path of the current source item. */
1470 if (FAILED(IShellFolder_GetDisplayNameOf(psfFrom, apidl[i], SHGDN_FORPARSING, &strret)))
1471 return E_FAIL;
1472 if (FAILED(StrRetToBufW(&strret, apidl[i], wszSrc, MAX_PATH)))
1473 return E_FAIL;
1474 if (!UNIXFS_get_unix_path(wszSrc, szSrc))
1475 return E_FAIL;
1477 /* Build the unix path of the current destination item */
1478 lstrcpyA(pszRelativeDst, _ILGetTextPointer(apidl[i]));
1480 FIXME("Would copy %s to %s. Not yet implemented.\n", szSrc, szAbsoluteDst);
1482 return S_OK;
1485 /* VTable for UnixFolder's ISFHelper interface
1487 static const ISFHelperVtbl UnixFolder_ISFHelper_Vtbl = {
1488 UnixFolder_ISFHelper_QueryInterface,
1489 UnixFolder_ISFHelper_AddRef,
1490 UnixFolder_ISFHelper_Release,
1491 UnixFolder_ISFHelper_GetUniqueName,
1492 UnixFolder_ISFHelper_AddFolder,
1493 UnixFolder_ISFHelper_DeleteItems,
1494 UnixFolder_ISFHelper_CopyItems
1497 /******************************************************************************
1498 * Unix[Dos]Folder_Constructor [Internal]
1500 * PARAMS
1501 * pUnkOuter [I] Outer class for aggregation. Currently ignored.
1502 * riid [I] Interface asked for by the client.
1503 * ppv [O] Pointer to an riid interface to the UnixFolder object.
1505 * NOTES
1506 * Those are the only functions exported from shfldr_unixfs.c. They are called from
1507 * shellole.c's default class factory and thus have to exhibit a LPFNCREATEINSTANCE
1508 * compatible signature.
1510 * The UnixDosFolder_Constructor sets the dwPathMode member to PATHMODE_DOS. This
1511 * means that paths are converted from dos to unix and back at the interfaces.
1513 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, DWORD dwPathMode,
1514 const CLSID *pCLSID)
1516 HRESULT hr = E_FAIL;
1517 UnixFolder *pUnixFolder = SHAlloc((ULONG)sizeof(UnixFolder));
1519 if(pUnixFolder) {
1520 pUnixFolder->lpIShellFolder2Vtbl = &UnixFolder_IShellFolder2_Vtbl;
1521 pUnixFolder->lpIPersistFolder3Vtbl = &UnixFolder_IPersistFolder3_Vtbl;
1522 pUnixFolder->lpIPersistPropertyBagVtbl = &UnixFolder_IPersistPropertyBag_Vtbl;
1523 pUnixFolder->lpISFHelperVtbl = &UnixFolder_ISFHelper_Vtbl;
1524 pUnixFolder->m_cRef = 0;
1525 pUnixFolder->m_pszPath = NULL;
1526 pUnixFolder->m_dwPathMode = dwPathMode;
1527 pUnixFolder->m_pCLSID = pCLSID;
1529 UnixFolder_IShellFolder2_AddRef(STATIC_CAST(IShellFolder2, pUnixFolder));
1530 hr = UnixFolder_IShellFolder2_QueryInterface(STATIC_CAST(IShellFolder2, pUnixFolder), riid, ppv);
1531 UnixFolder_IShellFolder2_Release(STATIC_CAST(IShellFolder2, pUnixFolder));
1533 return hr;
1536 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1537 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1538 return CreateUnixFolder(pUnkOuter, riid, ppv, PATHMODE_UNIX, &CLSID_UnixFolder);
1541 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1542 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1543 return CreateUnixFolder(pUnkOuter, riid, ppv, PATHMODE_DOS, &CLSID_UnixDosFolder);
1546 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1547 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1548 return CreateUnixFolder(pUnkOuter, riid, ppv, PATHMODE_DOS, &CLSID_FolderShortcut);
1551 /******************************************************************************
1552 * UnixSubFolderIterator
1554 * Class whose heap based objects represent iterators over the sub-directories
1555 * of a given UnixFolder object.
1558 /* UnixSubFolderIterator object layout and typedef.
1560 typedef struct _UnixSubFolderIterator {
1561 const IEnumIDListVtbl *lpIEnumIDListVtbl;
1562 LONG m_cRef;
1563 SHCONTF m_fFilter;
1564 DIR *m_dirFolder;
1565 char m_szFolder[FILENAME_MAX];
1566 } UnixSubFolderIterator;
1568 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
1569 TRACE("(iterator=%p)\n", iterator);
1571 if (iterator->m_dirFolder)
1572 closedir(iterator->m_dirFolder);
1573 SHFree(iterator);
1576 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface,
1577 REFIID riid, void** ppv)
1579 TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
1581 if (!ppv) return E_INVALIDARG;
1583 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
1584 *ppv = iface;
1585 } else {
1586 *ppv = NULL;
1587 return E_NOINTERFACE;
1590 IEnumIDList_AddRef(iface);
1591 return S_OK;
1594 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
1596 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1598 TRACE("(iface=%p)\n", iface);
1600 return InterlockedIncrement(&This->m_cRef);
1603 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
1605 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1606 ULONG cRef;
1608 TRACE("(iface=%p)\n", iface);
1610 cRef = InterlockedDecrement(&This->m_cRef);
1612 if (!cRef)
1613 UnixSubFolderIterator_Destroy(This);
1615 return cRef;
1618 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt,
1619 LPITEMIDLIST* rgelt, ULONG* pceltFetched)
1621 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1622 ULONG i = 0;
1624 /* This->m_dirFolder will be NULL if the user doesn't have access rights for the dir. */
1625 if (This->m_dirFolder) {
1626 char *pszRelativePath = This->m_szFolder + lstrlenA(This->m_szFolder);
1627 struct dirent *pDirEntry;
1629 while (i < celt) {
1630 pDirEntry = readdir(This->m_dirFolder);
1631 if (!pDirEntry) break; /* No more entries */
1632 if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
1634 /* Temporarily build absolute path in This->m_szFolder. Then construct a pidl
1635 * and see if it passes the filter.
1637 lstrcpyA(pszRelativePath, pDirEntry->d_name);
1638 rgelt[i] = (LPITEMIDLIST)SHAlloc(SHITEMID_LEN_FROM_NAME_LEN(lstrlenA(pszRelativePath))+sizeof(USHORT));
1639 if (!UNIXFS_build_shitemid(This->m_szFolder, rgelt[i]) ||
1640 !UNIXFS_is_pidl_of_type(rgelt[i], This->m_fFilter))
1642 SHFree(rgelt[i]);
1643 continue;
1645 memset(((PBYTE)rgelt[i])+rgelt[i]->mkid.cb, 0, sizeof(USHORT));
1646 i++;
1648 *pszRelativePath = '\0'; /* Restore the original path in This->m_szFolder. */
1651 if (pceltFetched)
1652 *pceltFetched = i;
1654 return (i == 0) ? S_FALSE : S_OK;
1657 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
1659 LPITEMIDLIST *apidl;
1660 ULONG cFetched;
1661 HRESULT hr;
1663 TRACE("(iface=%p, celt=%ld)\n", iface, celt);
1665 /* Call IEnumIDList::Next and delete the resulting pidls. */
1666 apidl = (LPITEMIDLIST*)SHAlloc(celt * sizeof(LPITEMIDLIST));
1667 hr = IEnumIDList_Next(iface, celt, apidl, &cFetched);
1668 if (SUCCEEDED(hr))
1669 while (cFetched--)
1670 SHFree(apidl[cFetched]);
1671 SHFree(apidl);
1673 return hr;
1676 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
1678 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1680 TRACE("(iface=%p)\n", iface);
1682 if (This->m_dirFolder)
1683 rewinddir(This->m_dirFolder);
1685 return S_OK;
1688 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This,
1689 IEnumIDList** ppenum)
1691 TRACE("stub\n");
1692 return E_NOTIMPL;
1695 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
1697 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
1698 UnixSubFolderIterator_IEnumIDList_QueryInterface,
1699 UnixSubFolderIterator_IEnumIDList_AddRef,
1700 UnixSubFolderIterator_IEnumIDList_Release,
1701 UnixSubFolderIterator_IEnumIDList_Next,
1702 UnixSubFolderIterator_IEnumIDList_Skip,
1703 UnixSubFolderIterator_IEnumIDList_Reset,
1704 UnixSubFolderIterator_IEnumIDList_Clone
1707 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter) {
1708 UnixSubFolderIterator *iterator;
1710 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
1712 iterator = SHAlloc((ULONG)sizeof(UnixSubFolderIterator));
1713 iterator->lpIEnumIDListVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
1714 iterator->m_cRef = 0;
1715 iterator->m_fFilter = fFilter;
1716 iterator->m_dirFolder = opendir(pUnixFolder->m_pszPath);
1717 lstrcpyA(iterator->m_szFolder, pUnixFolder->m_pszPath);
1719 UnixSubFolderIterator_IEnumIDList_AddRef((IEnumIDList*)iterator);
1721 return (IUnknown*)iterator;