shell32/unixfs: Added unicode support in folder and file pidls.
[wine/multimedia.git] / dlls / shell32 / shfldr_unixfs.c
blob394a8c6c7498f4d5e8e452ecc04d46eea06aa50c
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
22 * As you know, windows and unix do have a different philosophy with regard to
23 * the question of how a filesystem should be laid out. While we unix geeks
24 * learned to love the 'one-tree-rooted-at-/' approach, windows has in fact
25 * a whole forest of filesystem trees, each of which is typically identified by
26 * a drive letter.
28 * We would like wine to integrate as smoothly as possible (that is without
29 * sacrificing win32 compatibility) into the unix environment. For the
30 * filesystem question, this means we really would like those windows
31 * applications to work with unix path- and file-names. Unfortunately, this
32 * seems to be impossible in general. Therefore we have those symbolic links
33 * in wine's 'dosdevices' directory, which are used to simulate drives
34 * to keep windows applications happy. And as a consequence, we have those
35 * drive letters show up now and then in GUI applications running under wine,
36 * which gets the unix hardcore fans all angry, shouting at us @#!&$%* wine
37 * hackers that we are seducing the big companies not to port their applications
38 * to unix.
40 * DOS paths do appear at various places in GUI applications. Sometimes, they
41 * show up in the title bar of an application's window. They tend to accumulate
42 * in the most-recently-used section of the file-menu. And I've even seen some
43 * in a configuration dialog's edit control. In those examples, wine can't do a
44 * lot about this, since path-names can't be told appart from ordinary strings
45 * here. That's different in the file dialogs, though.
47 * With the introduction of the 'shell' in win32, Microsoft established an
48 * abstraction layer on top of the filesystem, called the shell namespace (I was
49 * told that Gnome's virtual filesystem is conceptually similar). In the shell
50 * namespace, one doesn't use ascii- or unicode-strings to uniquely identify
51 * objects. Instead Microsoft introduced item-identifier-lists (The c type is
52 * called ITEMIDLIST) as an abstraction of path-names. As you probably would
53 * have guessed, an item-identifier-list is a list of item-identifiers (whose
54 * c type's funny name is SHITEMID), which are opaque binary objects. This means
55 * that no application (apart from Microsoft Office) should make any assumptions
56 * on the internal structure of these SHITEMIDs.
58 * Since the user prefers to be presented the good-old DOS file-names instead of
59 * binary ITEMIDLISTs, a translation method between string-based file-names and
60 * ITEMIDLISTs was established. At the core of this are the COM-Interface
61 * IShellFolder and especially it's methods ParseDisplayName and
62 * GetDisplayNameOf. Basically, you give a DOS-path (let's say C:\windows) to
63 * ParseDisplayName and get a SHITEMID similar to <Desktop|My Computer|C:|windows|>.
64 * Since it's opaque, you can't see the 'C', the 'windows' and the other stuff.
65 * You can only figure out that the ITEMIDLIST is composed of four SHITEMIDS.
66 * The file dialog applies IShellFolder's BindToObject method to bind to each of
67 * those four objects (Desktop, My Computer, C: and windows. All of them have to
68 * implement the IShellFolder interface.) and asks them how they would like to be
69 * displayed (basically their icon and the string displayed). If the file dialog
70 * asks <Desktop|My Computer|C:|windows> which sub-objects it contains (via
71 * EnumObjects) it gets a list of opaque SHITEMIDs, which can be concatenated to
72 * <Desktop|...|windows> to build a new ITEMIDLIST and browse, for instance,
73 * into <system32>. This means the file dialog browses the shell namespace by
74 * identifying objects via ITEMIDLISTs. Once the user has selected a location to
75 * save his valuable file, the file dialog calls IShellFolder's GetDisplayNameOf
76 * method to translate the ITEMIDLIST back to a DOS filename.
78 * It seems that one intention of the shell namespace concept was to make it
79 * possible to have objects in the namespace, which don't have any counterpart
80 * in the filesystem. The 'My Computer' shell folder object is one instance
81 * which comes to mind (Go try to save a file into 'My Computer' on windows.)
82 * So, to make matters a little more complex, before the file dialog asks a
83 * shell namespace object for it's DOS path, it asks if it actually has one.
84 * This is done via the IShellFolder::GetAttributesOf method, which sets the
85 * SFGAO_FILESYSTEM if - and only if - it has.
87 * The two things, described in the previous two paragraphs, are what unixfs is
88 * based on. So basically, if UnixDosFolder's ParseDisplayName method is called
89 * with a 'c:\windows' path-name, it doesn't return an
90 * <Desktop|My Computer|C:|windows|> ITEMIDLIST. Instead, it uses
91 * shell32's wine_get_unix_path_name and the _posix_ (which means not the win32)
92 * fileio api's to figure out that c: is mapped to - let's say -
93 * /home/mjung/.wine/drive_c and then constructs a
94 * <Desktop|/|home|mjung|.wine|drive_c> ITEMIDLIST. Which is what the file
95 * dialog uses to display the folder and file objects, which is why you see a
96 * unix path. When the user has found a nice place for his file and hits the
97 * save button, the ITEMIDLIST of the selected folder object is passed to
98 * GetDisplayNameOf, which returns a _DOS_ path name
99 * (like H:\home_of_my_new_file out of <|Desktop|/|home|mjung|home_of_my_new_file|>).
100 * Unixfs basically mounts your dos devices together in order to construct
101 * a copy of your unix filesystem structure.
103 * But what if none of the symbolic links in 'dosdevices' points to '/', you
104 * might ask ("And I don't want wine have access to my complete hard drive, you
105 * *%&1#!"). No problem, as I stated above, unixfs uses the _posix_ apis to
106 * construct the ITEMIDLISTs. Folders, which aren't accessible via a drive letter,
107 * don't have the SFGAO_FILESYSTEM flag set. So the file dialogs should'nt allow
108 * the user to select such a folder for file storage (And if it does anyhow, it
109 * will not be able to return a valid path, since there is none). Think of those
110 * folders as a hierarchy of 'My Computer'-like folders, which happen to be a
111 * shadow of your unix filesystem tree. And since all of this stuff doesn't
112 * change anything at all in wine's fileio api's, windows applications will have
113 * no more access rights as they had before.
115 * To sum it all up, you can still savely run wine with you root account (Just
116 * kidding, don't do it.)
118 * If you are now standing in front of your computer, shouting hotly
119 * "I am not convinced, Mr. Rumsfeld^H^H^H^H^H^H^H^H^H^H^H^H", fire up regedit
120 * and delete HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\
121 * Explorer\Desktop\Namespace\{9D20AAE8-0625-44B0-9CA7-71889C2254D9} and you
122 * will be back in the pre-unixfs days.
125 #include "config.h"
126 #include "wine/port.h"
128 #include <stdio.h>
129 #include <stdarg.h>
130 #include <limits.h>
131 #include <dirent.h>
132 #ifdef HAVE_UNISTD_H
133 # include <unistd.h>
134 #endif
135 #ifdef HAVE_SYS_STAT_H
136 # include <sys/stat.h>
137 #endif
138 #ifdef HAVE_PWD_H
139 # include <pwd.h>
140 #endif
141 #include <grp.h>
142 #include <limits.h>
144 #define COBJMACROS
145 #define NONAMELESSUNION
146 #define NONAMELESSSTRUCT
148 #include "windef.h"
149 #include "winbase.h"
150 #include "winuser.h"
151 #include "objbase.h"
152 #include "winreg.h"
153 #include "shlwapi.h"
154 #include "winternl.h"
155 #include "wine/debug.h"
157 #include "shell32_main.h"
158 #include "shellfolder.h"
159 #include "shfldr.h"
160 #include "shresdef.h"
161 #include "pidl.h"
163 WINE_DEFAULT_DEBUG_CHANNEL(shell);
165 const GUID CLSID_UnixFolder = {0xcc702eb2, 0x7dc5, 0x11d9, {0xc6, 0x87, 0x00, 0x04, 0x23, 0x8a, 0x01, 0xcd}};
166 const GUID CLSID_UnixDosFolder = {0x9d20aae8, 0x0625, 0x44b0, {0x9c, 0xa7, 0x71, 0x88, 0x9c, 0x22, 0x54, 0xd9}};
168 #define ADJUST_THIS(c,m,p) ((c*)(((long)p)-(long)&(((c*)0)->lp##m##Vtbl)))
169 #define STATIC_CAST(i,p) ((i*)&p->lp##i##Vtbl)
171 #define LEN_SHITEMID_FIXED_PART ((USHORT) \
172 ( sizeof(USHORT) /* SHITEMID's cb field. */ \
173 + sizeof(PIDLTYPE) /* PIDLDATA's type field. */ \
174 + sizeof(FileStruct) /* Well, the FileStruct. */ \
175 - sizeof(char) /* One char too much in FileStruct. */ \
176 + sizeof(FileStructW) /* You name it. */ \
177 - sizeof(WCHAR) /* One WCHAR too much in FileStructW. */ \
178 + sizeof(WORD) )) /* Offset of FileStructW field in PIDL. */
180 #define PATHMODE_UNIX 0
181 #define PATHMODE_DOS 1
183 /* UnixFolder object layout and typedef.
185 typedef struct _UnixFolder {
186 const IShellFolder2Vtbl *lpIShellFolder2Vtbl;
187 const IPersistFolder3Vtbl *lpIPersistFolder3Vtbl;
188 const IPersistPropertyBagVtbl *lpIPersistPropertyBagVtbl;
189 const IDropTargetVtbl *lpIDropTargetVtbl;
190 const ISFHelperVtbl *lpISFHelperVtbl;
191 LONG m_cRef;
192 CHAR *m_pszPath; /* Target path of the shell folder (CP_UNIXCP) */
193 LPITEMIDLIST m_pidlLocation; /* Location in the shell namespace */
194 DWORD m_dwPathMode;
195 DWORD m_dwAttributes;
196 const CLSID *m_pCLSID;
197 DWORD m_dwDropEffectsMask;
198 } UnixFolder;
200 /* Will hold the registered clipboard format identifier for ITEMIDLISTS. */
201 static UINT cfShellIDList = 0;
203 /******************************************************************************
204 * UNIXFS_is_rooted_at_desktop [Internal]
206 * Checks if the unixfs namespace extension is rooted at desktop level.
208 * RETURNS
209 * TRUE, if unixfs is rooted at desktop level
210 * FALSE, if not.
212 BOOL UNIXFS_is_rooted_at_desktop(void) {
213 HKEY hKey;
214 WCHAR wszRootedAtDesktop[69 + CHARS_IN_GUID] = {
215 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
216 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
217 'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
218 'N','a','m','e','S','p','a','c','e','\\',0 };
220 if (StringFromGUID2(&CLSID_UnixDosFolder, wszRootedAtDesktop + 69, CHARS_IN_GUID) &&
221 RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszRootedAtDesktop, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
223 RegCloseKey(hKey);
224 return TRUE;
226 return FALSE;
229 /******************************************************************************
230 * UNIXFS_filename_from_shitemid [Internal]
232 * Get CP_UNIXCP encoded filename corresponding to the first item of a pidl
234 * PARAMS
235 * pidl [I] A simple SHITEMID
236 * pszPathElement [O] Filename in CP_UNIXCP encoding will be stored here
238 * RETURNS
239 * Success: Number of bytes necessary to store the CP_UNIXCP encoded filename
240 * _without_ the terminating NUL.
241 * Failure: 0
243 * NOTES
244 * Size of the buffer at pszPathElement has to be FILENAME_MAX. pszPathElement
245 * may be NULL, if you are only interested in the return value.
247 static int UNIXFS_filename_from_shitemid(LPCITEMIDLIST pidl, char* pszPathElement) {
248 FileStructW *pFileStructW = _ILGetFileStructW(pidl);
249 int cLen = 0;
251 if (pFileStructW) {
252 cLen = WideCharToMultiByte(CP_UNIXCP, 0, pFileStructW->wszName, -1, pszPathElement,
253 pszPathElement ? FILENAME_MAX : 0, 0, 0);
254 } else {
255 /* There might be pidls slipping in from shfldr_fs.c, which don't contain the
256 * FileStructW field. In this case, we have to convert from CP_ACP to CP_UNIXCP. */
257 char *pszText = _ILGetTextPointer(pidl);
258 WCHAR *pwszPathElement = NULL;
259 int cWideChars;
261 cWideChars = MultiByteToWideChar(CP_ACP, 0, pszText, -1, NULL, 0);
262 if (!cWideChars) goto cleanup;
264 pwszPathElement = SHAlloc(cWideChars * sizeof(WCHAR));
265 if (!pwszPathElement) goto cleanup;
267 cWideChars = MultiByteToWideChar(CP_ACP, 0, pszText, -1, pwszPathElement, cWideChars);
268 if (!cWideChars) goto cleanup;
270 cLen = WideCharToMultiByte(CP_UNIXCP, 0, pwszPathElement, -1, pszPathElement,
271 pszPathElement ? FILENAME_MAX : 0, 0, 0);
273 cleanup:
274 SHFree(pwszPathElement);
277 if (cLen) cLen--; /* Don't count terminating NUL! */
278 return cLen;
281 /******************************************************************************
282 * UNIXFS_shitemid_len_from_filename [Internal]
284 * Computes the necessary length of a pidl to hold a path element
286 * PARAMS
287 * szPathElement [I] The path element string in CP_UNIXCP encoding.
288 * ppszPathElement [O] Path element string in CP_ACP encoding.
289 * ppwszPathElement [O] Path element string as WCHAR string.
291 * RETURNS
292 * Success: Length in bytes of a SHITEMID representing szPathElement
293 * Failure: 0
295 * NOTES
296 * Provide NULL values if not interested in pp(w)szPathElement. Otherwise
297 * caller is responsible to free ppszPathElement and ppwszPathElement with
298 * SHFree.
300 static USHORT UNIXFS_shitemid_len_from_filename(
301 const char *szPathElement, char **ppszPathElement, WCHAR **ppwszPathElement)
303 USHORT cbPidlLen = 0;
304 WCHAR *pwszPathElement = NULL;
305 char *pszPathElement = NULL;
306 int cWideChars, cChars;
308 /* There and Back Again: A Hobbit's Holiday. CP_UNIXCP might be some ANSI
309 * codepage or it might be a real multi-byte encoding like utf-8. There is no
310 * other way to figure out the length of the corresponding WCHAR and CP_ACP
311 * strings without actually doing the full CP_UNIXCP -> WCHAR -> CP_ACP cycle. */
313 cWideChars = MultiByteToWideChar(CP_UNIXCP, 0, szPathElement, -1, NULL, 0);
314 if (!cWideChars) goto cleanup;
316 pwszPathElement = SHAlloc(cWideChars * sizeof(WCHAR));
317 if (!pwszPathElement) goto cleanup;
319 cWideChars = MultiByteToWideChar(CP_UNIXCP, 0, szPathElement, -1, pwszPathElement, cWideChars);
320 if (!cWideChars) goto cleanup;
322 cChars = WideCharToMultiByte(CP_ACP, 0, pwszPathElement, -1, NULL, 0, 0, 0);
323 if (!cChars) goto cleanup;
325 pszPathElement = SHAlloc(cChars);
326 if (!pszPathElement) goto cleanup;
328 cChars = WideCharToMultiByte(CP_ACP, 0, pwszPathElement, -1, pszPathElement, cChars, 0, 0);
329 if (!cChars) goto cleanup;
331 /* (cChars & 0x1) is for the potential alignment byte */
332 cbPidlLen = LEN_SHITEMID_FIXED_PART + cChars + (cChars & 0x1) + cWideChars * sizeof(WCHAR);
334 cleanup:
335 if (cbPidlLen && ppszPathElement)
336 *ppszPathElement = pszPathElement;
337 else
338 SHFree(pszPathElement);
340 if (cbPidlLen && ppwszPathElement)
341 *ppwszPathElement = pwszPathElement;
342 else
343 SHFree(pwszPathElement);
345 return cbPidlLen;
348 /******************************************************************************
349 * UNIXFS_is_pidl_of_type [Internal]
351 * Checks for the first SHITEMID of an ITEMIDLIST if it passes a filter.
353 * PARAMS
354 * pIDL [I] The ITEMIDLIST to be checked.
355 * fFilter [I] Shell condition flags, which specify the filter.
357 * RETURNS
358 * TRUE, if pIDL is accepted by fFilter
359 * FALSE, otherwise
361 static inline BOOL UNIXFS_is_pidl_of_type(LPITEMIDLIST pIDL, SHCONTF fFilter) {
362 LPPIDLDATA pIDLData = _ILGetDataPointer(pIDL);
363 if (!(fFilter & SHCONTF_INCLUDEHIDDEN) && pIDLData &&
364 (pIDLData->u.file.uFileAttribs & FILE_ATTRIBUTE_HIDDEN))
366 return FALSE;
368 if (_ILIsFolder(pIDL) && (fFilter & SHCONTF_FOLDERS)) return TRUE;
369 if (_ILIsValue(pIDL) && (fFilter & SHCONTF_NONFOLDERS)) return TRUE;
370 return FALSE;
373 /******************************************************************************
374 * UNIXFS_is_dos_device [Internal]
376 * Determines if a unix directory corresponds to any dos device.
378 * PARAMS
379 * statPath [I] The stat struct of the directory, as returned by stat(2).
381 * RETURNS
382 * TRUE, if statPath corresponds to any dos drive letter
383 * FALSE, otherwise
385 static BOOL UNIXFS_is_dos_device(const struct stat *statPath) {
386 struct stat statDrive;
387 char *pszDrivePath;
388 DWORD dwDriveMap;
389 WCHAR wszDosDevice[4] = { 'A', ':', '\\', 0 };
391 for (dwDriveMap = GetLogicalDrives(); dwDriveMap; dwDriveMap >>= 1, wszDosDevice[0]++) {
392 if (!(dwDriveMap & 0x1)) continue;
393 pszDrivePath = wine_get_unix_file_name(wszDosDevice);
394 if (pszDrivePath && !stat(pszDrivePath, &statDrive)) {
395 HeapFree(GetProcessHeap(), 0, pszDrivePath);
396 if ((statPath->st_dev == statDrive.st_dev) && (statPath->st_ino == statDrive.st_ino))
397 return TRUE;
400 return FALSE;
403 /******************************************************************************
404 * UNIXFS_get_unix_path [Internal]
406 * Convert an absolute dos path to an absolute canonicalized unix path.
407 * Evaluate "/.", "/.." and symbolic links.
409 * PARAMS
410 * pszDosPath [I] An absolute dos path
411 * pszCanonicalPath [O] Buffer of length FILENAME_MAX. Will receive the canonical path.
413 * RETURNS
414 * Success, TRUE
415 * Failure, FALSE - Path not existent, too long, insufficient rights, to many symlinks
417 static BOOL UNIXFS_get_unix_path(LPCWSTR pszDosPath, char *pszCanonicalPath)
419 char *pPathTail, *pElement, *pCanonicalTail, szPath[FILENAME_MAX], *pszUnixPath;
420 struct stat fileStat;
422 TRACE("(pszDosPath=%s, pszCanonicalPath=%p)\n", debugstr_w(pszDosPath), pszCanonicalPath);
424 if (!pszDosPath || pszDosPath[1] != ':')
425 return FALSE;
427 pszUnixPath = wine_get_unix_file_name(pszDosPath);
428 if (!pszUnixPath) return FALSE;
429 strcpy(szPath, pszUnixPath);
430 HeapFree(GetProcessHeap(), 0, pszUnixPath);
432 /* pCanonicalTail always points to the end of the canonical path constructed
433 * thus far. pPathTail points to the still to be processed part of the input
434 * path. pElement points to the path element currently investigated.
436 *pszCanonicalPath = '\0';
437 pCanonicalTail = pszCanonicalPath;
438 pPathTail = szPath;
440 do {
441 char cTemp;
442 int cLinks = 0;
444 pElement = pPathTail;
445 pPathTail = strchr(pPathTail+1, '/');
446 if (!pPathTail) /* Last path element may not be terminated by '/'. */
447 pPathTail = pElement + strlen(pElement);
448 /* Temporarily terminate the current path element. Will be restored later. */
449 cTemp = *pPathTail;
450 *pPathTail = '\0';
452 /* Skip "/." path elements */
453 if (!strcmp("/.", pElement)) {
454 *pPathTail = cTemp;
455 continue;
458 /* Remove last element in canonical path for "/.." elements, then skip. */
459 if (!strcmp("/..", pElement)) {
460 char *pTemp = strrchr(pszCanonicalPath, '/');
461 if (pTemp)
462 pCanonicalTail = pTemp;
463 *pCanonicalTail = '\0';
464 *pPathTail = cTemp;
465 continue;
468 /* lstat returns zero on success. */
469 if (lstat(szPath, &fileStat))
470 return FALSE;
472 if (S_ISLNK(fileStat.st_mode)) {
473 char szSymlink[FILENAME_MAX];
474 int cLinkLen, cTailLen;
476 /* Avoid infinite loop for recursive links. */
477 if (++cLinks > 64)
478 return FALSE;
480 cLinkLen = readlink(szPath, szSymlink, FILENAME_MAX);
481 if (cLinkLen < 0)
482 return FALSE;
484 *pPathTail = cTemp;
485 cTailLen = strlen(pPathTail);
487 if (szSymlink[0] == '/') {
488 /* Absolute link. Copy to szPath, concat remaining path and start all over. */
489 if (cLinkLen + cTailLen + 1 > FILENAME_MAX)
490 return FALSE;
492 /* Avoid double slashes. */
493 if (szSymlink[cLinkLen-1] == '/' && pPathTail[0] == '/') {
494 szSymlink[cLinkLen-1] = '\0';
495 cLinkLen--;
498 memcpy(szSymlink + cLinkLen, pPathTail, cTailLen + 1);
499 memcpy(szPath, szSymlink, cLinkLen + cTailLen + 1);
500 *pszCanonicalPath = '\0';
501 pCanonicalTail = pszCanonicalPath;
502 pPathTail = szPath;
503 } else {
504 /* Relative link. Expand into szPath and continue. */
505 char szTemp[FILENAME_MAX];
506 int cTailLen = strlen(pPathTail);
508 if (pElement - szPath + 1 + cLinkLen + cTailLen + 1 > FILENAME_MAX)
509 return FALSE;
511 memcpy(szTemp, pPathTail, cTailLen + 1);
512 memcpy(pElement + 1, szSymlink, cLinkLen);
513 memcpy(pElement + 1 + cLinkLen, szTemp, cTailLen + 1);
514 pPathTail = pElement;
516 } else {
517 /* Regular directory or file. Copy to canonical path */
518 if (pCanonicalTail - pszCanonicalPath + pPathTail - pElement + 1 > FILENAME_MAX)
519 return FALSE;
521 memcpy(pCanonicalTail, pElement, pPathTail - pElement + 1);
522 pCanonicalTail += pPathTail - pElement;
523 *pPathTail = cTemp;
525 } while (pPathTail[0] == '/');
527 TRACE("--> %s\n", debugstr_a(pszCanonicalPath));
529 return TRUE;
532 /******************************************************************************
533 * UNIXFS_seconds_since_1970_to_dos_date_time [Internal]
535 * Convert unix time to FAT time
537 * PARAMS
538 * ss1970 [I] Unix time (seconds since 1970)
539 * pDate [O] Corresponding FAT date
540 * pTime [O] Corresponding FAT time
542 static inline void UNIXFS_seconds_since_1970_to_dos_date_time(
543 time_t ss1970, LPWORD pDate, LPWORD pTime)
545 LARGE_INTEGER time;
546 FILETIME fileTime;
548 RtlSecondsSince1970ToTime( ss1970, &time );
549 fileTime.dwLowDateTime = time.u.LowPart;
550 fileTime.dwHighDateTime = time.u.HighPart;
551 FileTimeToDosDateTime(&fileTime, pDate, pTime);
554 /******************************************************************************
555 * UNIXFS_build_shitemid [Internal]
557 * Constructs a new SHITEMID for the last component of path 'pszUnixPath' into
558 * buffer 'pIDL'.
560 * PARAMS
561 * pszUnixPath [I] An absolute path. The SHITEMID will be build for the last component.
562 * pIDL [O] SHITEMID will be constructed here.
564 * RETURNS
565 * Success: A pointer to the terminating '\0' character of path.
566 * Failure: NULL
568 * NOTES
569 * Minimum size of pIDL is SHITEMID_LEN_FROM_NAME_LEN(strlen(last_component_of_path)).
570 * If what you need is a PIDLLIST with a single SHITEMID, don't forget to append
571 * a 0 USHORT value.
573 static char* UNIXFS_build_shitemid(char *pszUnixPath, void *pIDL) {
574 LPPIDLDATA pIDLData;
575 struct stat fileStat;
576 char *pszComponentU, *pszComponentA;
577 WCHAR *pwszComponentW;
578 int cComponentULen, cComponentALen;
579 USHORT cbLen;
580 FileStructW *pFileStructW;
581 WORD uOffsetW, *pOffsetW;
583 TRACE("(pszUnixPath=%s, pIDL=%p)\n", debugstr_a(pszUnixPath), pIDL);
585 /* We are only interested in regular files and directories. */
586 if (stat(pszUnixPath, &fileStat)) return NULL;
587 if (!S_ISDIR(fileStat.st_mode) && !S_ISREG(fileStat.st_mode)) return NULL;
589 /* Compute the SHITEMID's length and wipe it. */
590 pszComponentU = strrchr(pszUnixPath, '/') + 1;
591 cComponentULen = strlen(pszComponentU);
592 cbLen = UNIXFS_shitemid_len_from_filename(pszComponentU, &pszComponentA, &pwszComponentW);
593 if (!cbLen) return NULL;
594 memset(pIDL, 0, cbLen);
595 ((LPSHITEMID)pIDL)->cb = cbLen;
597 /* Set shell32's standard SHITEMID data fields. */
598 pIDLData = _ILGetDataPointer((LPCITEMIDLIST)pIDL);
599 pIDLData->type = S_ISDIR(fileStat.st_mode) ? PT_FOLDER : PT_VALUE;
600 pIDLData->u.file.dwFileSize = (DWORD)fileStat.st_size;
601 UNIXFS_seconds_since_1970_to_dos_date_time(fileStat.st_mtime, &pIDLData->u.file.uFileDate,
602 &pIDLData->u.file.uFileTime);
603 pIDLData->u.file.uFileAttribs = 0;
604 if (S_ISDIR(fileStat.st_mode)) pIDLData->u.file.uFileAttribs |= FILE_ATTRIBUTE_DIRECTORY;
605 if (pszComponentU[0] == '.') pIDLData->u.file.uFileAttribs |= FILE_ATTRIBUTE_HIDDEN;
606 cComponentALen = lstrlenA(pszComponentA) + 1;
607 memcpy(pIDLData->u.file.szNames, pszComponentA, cComponentALen);
609 pFileStructW = (FileStructW*)(pIDLData->u.file.szNames + cComponentALen + (cComponentALen & 0x1));
610 uOffsetW = (WORD)(((LPBYTE)pFileStructW) - ((LPBYTE)pIDL));
611 pFileStructW->cbLen = cbLen - uOffsetW;
612 UNIXFS_seconds_since_1970_to_dos_date_time(fileStat.st_mtime, &pFileStructW->uCreationDate,
613 &pFileStructW->uCreationTime);
614 UNIXFS_seconds_since_1970_to_dos_date_time(fileStat.st_atime, &pFileStructW->uLastAccessDate,
615 &pFileStructW->uLastAccessTime);
616 lstrcpyW(pFileStructW->wszName, pwszComponentW);
618 pOffsetW = (WORD*)(((LPBYTE)pIDL) + cbLen - sizeof(WORD));
619 *pOffsetW = uOffsetW;
621 SHFree(pszComponentA);
622 SHFree(pwszComponentW);
624 return pszComponentU + cComponentULen;
627 /******************************************************************************
628 * UNIXFS_path_to_pidl [Internal]
630 * PARAMS
631 * pUnixFolder [I] If path is relative, pUnixFolder represents the base path
632 * path [I] An absolute unix or dos path or a path relativ to pUnixFolder
633 * ppidl [O] The corresponding ITEMIDLIST. Release with SHFree/ILFree
635 * RETURNS
636 * Success: TRUE
637 * Failure: FALSE, invalid params or out of memory
639 * NOTES
640 * pUnixFolder also carries the information if the path is expected to be unix or dos.
642 static BOOL UNIXFS_path_to_pidl(UnixFolder *pUnixFolder, const WCHAR *path, LPITEMIDLIST *ppidl) {
643 LPITEMIDLIST pidl;
644 int cPidlLen, cPathLen;
645 char *pSlash, *pNextSlash, szCompletePath[FILENAME_MAX], *pNextPathElement, *pszAPath;
646 WCHAR *pwszPath;
648 TRACE("pUnixFolder=%p, path=%s, ppidl=%p\n", pUnixFolder, debugstr_w(path), ppidl);
650 if (!ppidl || !path)
651 return FALSE;
653 /* Build an absolute path and let pNextPathElement point to the interesting
654 * relative sub-path. We need the absolute path to call 'stat', but the pidl
655 * will only contain the relative part.
657 if ((pUnixFolder->m_dwPathMode == PATHMODE_DOS) && (path[1] == ':'))
659 /* Absolute dos path. Convert to unix */
660 if (!UNIXFS_get_unix_path(path, szCompletePath))
661 return FALSE;
662 pNextPathElement = szCompletePath;
664 else if ((pUnixFolder->m_dwPathMode == PATHMODE_UNIX) && (path[0] == '/'))
666 /* Absolute unix path. Just convert to ANSI. */
667 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath, FILENAME_MAX, NULL, NULL);
668 pNextPathElement = szCompletePath;
670 else
672 /* Relative dos or unix path. Concat with this folder's path */
673 int cBasePathLen = strlen(pUnixFolder->m_pszPath);
674 memcpy(szCompletePath, pUnixFolder->m_pszPath, cBasePathLen);
675 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath + cBasePathLen,
676 FILENAME_MAX - cBasePathLen, NULL, NULL);
677 pNextPathElement = szCompletePath + cBasePathLen - 1;
679 /* If in dos mode, replace '\' with '/' */
680 if (pUnixFolder->m_dwPathMode == PATHMODE_DOS) {
681 char *pBackslash = strchr(pNextPathElement, '\\');
682 while (pBackslash) {
683 *pBackslash = '/';
684 pBackslash = strchr(pBackslash, '\\');
689 /* Special case for the root folder. */
690 if (!strcmp(szCompletePath, "/")) {
691 *ppidl = pidl = (LPITEMIDLIST)SHAlloc(sizeof(USHORT));
692 if (!pidl) return FALSE;
693 pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
694 return TRUE;
697 /* Remove trailing slash, if present */
698 cPathLen = strlen(szCompletePath);
699 if (szCompletePath[cPathLen-1] == '/')
700 szCompletePath[cPathLen-1] = '\0';
702 if ((szCompletePath[0] != '/') || (pNextPathElement[0] != '/')) {
703 ERR("szCompletePath: %s, pNextPathElment: %s\n", szCompletePath, pNextPathElement);
704 return FALSE;
707 /* At this point, we have an absolute unix path in szCompletePath
708 * and the relative portion of it in pNextPathElement. Both starting with '/'
709 * and _not_ terminated by a '/'. */
710 TRACE("complete path: %s, relative path: %s\n", szCompletePath, pNextPathElement);
712 /* Convert to CP_ACP and WCHAR */
713 if (!UNIXFS_shitemid_len_from_filename(pNextPathElement, &pszAPath, &pwszPath))
714 return 0;
716 /* Compute the length of the complete ITEMIDLIST */
717 cPidlLen = 0;
718 pSlash = pszAPath;
719 while (pSlash) {
720 pNextSlash = strchr(pSlash+1, '/');
721 cPidlLen += LEN_SHITEMID_FIXED_PART + /* Fixed part length plus potential alignment byte. */
722 (pNextSlash ? (pNextSlash - pSlash) & 0x1 : lstrlenA(pSlash) & 0x1);
723 pSlash = pNextSlash;
726 /* The USHORT is for the ITEMIDLIST terminator. The NUL terminators for the sub-path-strings
727 * are accounted for by the '/' separators, which are not stored in the SHITEMIDs. Above we
728 * have ensured that the number of '/'s exactly matches the number of sub-path-strings. */
729 cPidlLen += lstrlenA(pszAPath) + lstrlenW(pwszPath) * sizeof(WCHAR) + sizeof(USHORT);
731 SHFree(pszAPath);
732 SHFree(pwszPath);
734 *ppidl = pidl = (LPITEMIDLIST)SHAlloc(cPidlLen);
735 if (!pidl) return FALSE;
737 /* Concatenate the SHITEMIDs of the sub-directories. */
738 while (*pNextPathElement) {
739 pSlash = strchr(pNextPathElement+1, '/');
740 if (pSlash) *pSlash = '\0';
741 pNextPathElement = UNIXFS_build_shitemid(szCompletePath, pidl);
742 if (pSlash) *pSlash = '/';
744 if (!pNextPathElement) {
745 SHFree(*ppidl);
746 return FALSE;
748 pidl = ILGetNext(pidl);
750 pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
752 if ((char *)pidl-(char *)*ppidl+sizeof(USHORT) != cPidlLen) /* We've corrupted the heap :( */
753 ERR("Computed length of pidl incorrect. Please report.\n");
755 return TRUE;
758 /******************************************************************************
759 * UNIXFS_initialize_target_folder [Internal]
761 * Initialize the m_pszPath member of an UnixFolder, given an absolute unix
762 * base path and a relative ITEMIDLIST. Leave the m_pidlLocation member, which
763 * specifies the location in the shell namespace alone.
765 * PARAMS
766 * This [IO] The UnixFolder, whose target path is to be initialized
767 * szBasePath [I] The absolute base path
768 * pidlSubFolder [I] Relative part of the path, given as an ITEMIDLIST
769 * dwAttributes [I] Attributes to add to the Folders m_dwAttributes member
770 * (Used to pass the SFGAO_FILESYSTEM flag down the path)
771 * RETURNS
772 * Success: S_OK,
773 * Failure: E_FAIL
775 static HRESULT UNIXFS_initialize_target_folder(UnixFolder *This, const char *szBasePath,
776 LPCITEMIDLIST pidlSubFolder, DWORD dwAttributes)
778 LPCITEMIDLIST current = pidlSubFolder;
779 DWORD dwPathLen = strlen(szBasePath)+1;
780 struct stat statPrefix;
781 char *pNextDir;
783 /* Determine the path's length bytes */
784 while (current && current->mkid.cb) {
785 dwPathLen += UNIXFS_filename_from_shitemid(current, NULL) + 1; /* For the '/' */
786 current = ILGetNext(current);
789 /* Build the path and compute the attributes*/
790 This->m_dwAttributes =
791 dwAttributes|SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME;
792 This->m_pszPath = pNextDir = SHAlloc(dwPathLen);
793 if (!This->m_pszPath) {
794 WARN("SHAlloc failed!\n");
795 return E_FAIL;
797 current = pidlSubFolder;
798 strcpy(pNextDir, szBasePath);
799 pNextDir += strlen(szBasePath);
800 if (This->m_dwPathMode == PATHMODE_UNIX || IsEqualCLSID(&CLSID_MyDocuments, This->m_pCLSID))
801 This->m_dwAttributes |= SFGAO_FILESYSTEM;
802 if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
803 *pNextDir = '\0';
804 if (!stat(This->m_pszPath, &statPrefix) && UNIXFS_is_dos_device(&statPrefix))
805 This->m_dwAttributes |= SFGAO_FILESYSTEM;
807 while (current && current->mkid.cb) {
808 pNextDir += UNIXFS_filename_from_shitemid(current, pNextDir);
809 if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
810 *pNextDir = '\0';
811 if (!stat(This->m_pszPath, &statPrefix) && UNIXFS_is_dos_device(&statPrefix))
812 This->m_dwAttributes |= SFGAO_FILESYSTEM;
814 *pNextDir++ = '/';
815 current = ILGetNext(current);
817 *pNextDir='\0';
819 return S_OK;
822 /******************************************************************************
823 * UnixFolder
825 * Class whose heap based instances represent unix filesystem directories.
828 static void UnixFolder_Destroy(UnixFolder *pUnixFolder) {
829 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
831 SHFree(pUnixFolder->m_pszPath);
832 ILFree(pUnixFolder->m_pidlLocation);
833 SHFree(pUnixFolder);
836 static HRESULT WINAPI UnixFolder_IShellFolder2_QueryInterface(IShellFolder2 *iface, REFIID riid,
837 void **ppv)
839 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
841 TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
843 if (!ppv) return E_INVALIDARG;
845 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IShellFolder, riid) ||
846 IsEqualIID(&IID_IShellFolder2, riid))
848 *ppv = STATIC_CAST(IShellFolder2, This);
849 } else if (IsEqualIID(&IID_IPersistFolder3, riid) || IsEqualIID(&IID_IPersistFolder2, riid) ||
850 IsEqualIID(&IID_IPersistFolder, riid) || IsEqualIID(&IID_IPersist, riid))
852 *ppv = STATIC_CAST(IPersistFolder3, This);
853 } else if (IsEqualIID(&IID_IPersistPropertyBag, riid)) {
854 *ppv = STATIC_CAST(IPersistPropertyBag, This);
855 } else if (IsEqualIID(&IID_ISFHelper, riid)) {
856 *ppv = STATIC_CAST(ISFHelper, This);
857 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
858 *ppv = STATIC_CAST(IDropTarget, This);
859 if (!cfShellIDList)
860 cfShellIDList = RegisterClipboardFormatA(CFSTR_SHELLIDLIST);
861 } else {
862 *ppv = NULL;
863 return E_NOINTERFACE;
866 IUnknown_AddRef((IUnknown*)*ppv);
867 return S_OK;
870 static ULONG WINAPI UnixFolder_IShellFolder2_AddRef(IShellFolder2 *iface) {
871 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
873 TRACE("(iface=%p)\n", iface);
875 return InterlockedIncrement(&This->m_cRef);
878 static ULONG WINAPI UnixFolder_IShellFolder2_Release(IShellFolder2 *iface) {
879 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
880 ULONG cRef;
882 TRACE("(iface=%p)\n", iface);
884 cRef = InterlockedDecrement(&This->m_cRef);
886 if (!cRef)
887 UnixFolder_Destroy(This);
889 return cRef;
892 static HRESULT WINAPI UnixFolder_IShellFolder2_ParseDisplayName(IShellFolder2* iface, HWND hwndOwner,
893 LPBC pbcReserved, LPOLESTR lpszDisplayName, ULONG* pchEaten, LPITEMIDLIST* ppidl,
894 ULONG* pdwAttributes)
896 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
897 BOOL result;
899 TRACE("(iface=%p, hwndOwner=%p, pbcReserved=%p, lpszDisplayName=%s, pchEaten=%p, ppidl=%p, "
900 "pdwAttributes=%p) stub\n", iface, hwndOwner, pbcReserved, debugstr_w(lpszDisplayName),
901 pchEaten, ppidl, pdwAttributes);
903 result = UNIXFS_path_to_pidl(This, lpszDisplayName, ppidl);
904 if (result && pdwAttributes && *pdwAttributes)
906 IShellFolder *pParentSF;
907 LPCITEMIDLIST pidlLast;
908 HRESULT hr;
910 hr = SHBindToParent(*ppidl, &IID_IShellFolder, (LPVOID*)&pParentSF, &pidlLast);
911 if (FAILED(hr)) return E_FAIL;
912 IShellFolder_GetAttributesOf(pParentSF, 1, &pidlLast, pdwAttributes);
913 IShellFolder_Release(pParentSF);
916 if (!result) TRACE("FAILED!\n");
917 return result ? S_OK : E_FAIL;
920 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter);
922 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumObjects(IShellFolder2* iface, HWND hwndOwner,
923 SHCONTF grfFlags, IEnumIDList** ppEnumIDList)
925 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
926 IUnknown *newIterator;
927 HRESULT hr;
929 TRACE("(iface=%p, hwndOwner=%p, grfFlags=%08lx, ppEnumIDList=%p)\n",
930 iface, hwndOwner, grfFlags, ppEnumIDList);
932 if (!This->m_pszPath) {
933 WARN("EnumObjects called on uninitialized UnixFolder-object!\n");
934 return E_UNEXPECTED;
937 newIterator = UnixSubFolderIterator_Constructor(This, grfFlags);
938 hr = IUnknown_QueryInterface(newIterator, &IID_IEnumIDList, (void**)ppEnumIDList);
939 IUnknown_Release(newIterator);
941 return hr;
944 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, const CLSID *pCLSID);
946 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToObject(IShellFolder2* iface, LPCITEMIDLIST pidl,
947 LPBC pbcReserved, REFIID riid, void** ppvOut)
949 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
950 IPersistFolder3 *persistFolder;
951 HRESULT hr;
952 const CLSID *clsidChild;
954 TRACE("(iface=%p, pidl=%p, pbcReserver=%p, riid=%p, ppvOut=%p)\n",
955 iface, pidl, pbcReserved, riid, ppvOut);
957 if (!pidl || !pidl->mkid.cb)
958 return E_INVALIDARG;
960 if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
961 /* Children of FolderShortcuts are ShellFSFolders on Windows.
962 * Unixfs' counterpart is UnixDosFolder. */
963 clsidChild = &CLSID_UnixDosFolder;
964 } else {
965 clsidChild = This->m_pCLSID;
968 hr = CreateUnixFolder(NULL, &IID_IPersistFolder3, (void**)&persistFolder, clsidChild);
969 if (!SUCCEEDED(hr)) return hr;
970 hr = IPersistFolder_QueryInterface(persistFolder, riid, (void**)ppvOut);
972 if (SUCCEEDED(hr)) {
973 UnixFolder *subfolder = ADJUST_THIS(UnixFolder, IPersistFolder3, persistFolder);
974 subfolder->m_pidlLocation = ILCombine(This->m_pidlLocation, pidl);
975 hr = UNIXFS_initialize_target_folder(subfolder, This->m_pszPath, pidl,
976 This->m_dwAttributes & SFGAO_FILESYSTEM);
979 IPersistFolder3_Release(persistFolder);
981 return hr;
984 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToStorage(IShellFolder2* This, LPCITEMIDLIST pidl,
985 LPBC pbcReserved, REFIID riid, void** ppvObj)
987 FIXME("stub\n");
988 return E_NOTIMPL;
991 static HRESULT WINAPI UnixFolder_IShellFolder2_CompareIDs(IShellFolder2* iface, LPARAM lParam,
992 LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
994 BOOL isEmpty1, isEmpty2;
995 HRESULT hr = E_FAIL;
996 LPITEMIDLIST firstpidl;
997 IShellFolder2 *psf;
998 int compare;
1000 TRACE("(iface=%p, lParam=%ld, pidl1=%p, pidl2=%p)\n", iface, lParam, pidl1, pidl2);
1002 isEmpty1 = !pidl1 || !pidl1->mkid.cb;
1003 isEmpty2 = !pidl2 || !pidl2->mkid.cb;
1005 if (isEmpty1 && isEmpty2)
1006 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
1007 else if (isEmpty1)
1008 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1009 else if (isEmpty2)
1010 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1012 if (_ILIsFolder(pidl1) && !_ILIsFolder(pidl2))
1013 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1014 if (!_ILIsFolder(pidl1) && _ILIsFolder(pidl2))
1015 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1017 compare = CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
1018 _ILGetTextPointer(pidl1), -1,
1019 _ILGetTextPointer(pidl2), -1);
1021 if ((compare == CSTR_LESS_THAN) || (compare == CSTR_GREATER_THAN))
1022 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)((compare == CSTR_LESS_THAN)?-1:1));
1024 if (pidl1->mkid.cb < pidl2->mkid.cb)
1025 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1026 else if (pidl1->mkid.cb > pidl2->mkid.cb)
1027 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1029 firstpidl = ILCloneFirst(pidl1);
1030 pidl1 = ILGetNext(pidl1);
1031 pidl2 = ILGetNext(pidl2);
1033 hr = IShellFolder2_BindToObject(iface, firstpidl, NULL, &IID_IShellFolder, (LPVOID*)&psf);
1034 if (SUCCEEDED(hr)) {
1035 hr = IShellFolder_CompareIDs(psf, lParam, pidl1, pidl2);
1036 IShellFolder2_Release(psf);
1039 ILFree(firstpidl);
1040 return hr;
1043 static HRESULT WINAPI UnixFolder_IShellFolder2_CreateViewObject(IShellFolder2* iface, HWND hwndOwner,
1044 REFIID riid, void** ppv)
1046 HRESULT hr = E_INVALIDARG;
1048 TRACE("(iface=%p, hwndOwner=%p, riid=%p, ppv=%p) stub\n", iface, hwndOwner, riid, ppv);
1050 if (!ppv) return E_INVALIDARG;
1051 *ppv = NULL;
1053 if (IsEqualIID(&IID_IShellView, riid)) {
1054 LPSHELLVIEW pShellView;
1056 pShellView = IShellView_Constructor((IShellFolder*)iface);
1057 if (pShellView) {
1058 hr = IShellView_QueryInterface(pShellView, riid, ppv);
1059 IShellView_Release(pShellView);
1061 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
1062 hr = IShellFolder2_QueryInterface(iface, &IID_IDropTarget, ppv);
1065 return hr;
1068 static HRESULT WINAPI UnixFolder_IShellFolder2_GetAttributesOf(IShellFolder2* iface, UINT cidl,
1069 LPCITEMIDLIST* apidl, SFGAOF* rgfInOut)
1071 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1072 HRESULT hr = S_OK;
1074 TRACE("(iface=%p, cidl=%u, apidl=%p, rgfInOut=%p)\n", iface, cidl, apidl, rgfInOut);
1076 if (!rgfInOut || (cidl && !apidl))
1077 return E_INVALIDARG;
1079 if (cidl == 0) {
1080 *rgfInOut &= This->m_dwAttributes;
1081 } else {
1082 char szAbsolutePath[FILENAME_MAX], *pszRelativePath;
1083 UINT i;
1085 *rgfInOut = SFGAO_CANCOPY|SFGAO_CANMOVE|SFGAO_CANLINK|SFGAO_CANRENAME|SFGAO_CANDELETE|
1086 SFGAO_HASPROPSHEET|SFGAO_DROPTARGET|SFGAO_FILESYSTEM;
1087 lstrcpyA(szAbsolutePath, This->m_pszPath);
1088 pszRelativePath = szAbsolutePath + lstrlenA(szAbsolutePath);
1089 for (i=0; i<cidl; i++) {
1090 if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
1091 struct stat fileStat;
1092 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelativePath))
1093 return E_INVALIDARG;
1094 if (stat(szAbsolutePath, &fileStat) || !UNIXFS_is_dos_device(&fileStat))
1095 *rgfInOut &= ~SFGAO_FILESYSTEM;
1097 if (_ILIsFolder(apidl[i]))
1098 *rgfInOut |= SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR;
1102 return hr;
1105 static HRESULT WINAPI UnixFolder_IShellFolder2_GetUIObjectOf(IShellFolder2* iface, HWND hwndOwner,
1106 UINT cidl, LPCITEMIDLIST* apidl, REFIID riid, UINT* prgfInOut, void** ppvOut)
1108 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1109 UINT i;
1111 TRACE("(iface=%p, hwndOwner=%p, cidl=%d, apidl=%p, riid=%s, prgfInOut=%p, ppv=%p)\n",
1112 iface, hwndOwner, cidl, apidl, debugstr_guid(riid), prgfInOut, ppvOut);
1114 if (!cidl || !apidl || !riid || !ppvOut)
1115 return E_INVALIDARG;
1117 for (i=0; i<cidl; i++)
1118 if (!apidl[i])
1119 return E_INVALIDARG;
1121 if (IsEqualIID(&IID_IContextMenu, riid)) {
1122 *ppvOut = ISvItemCm_Constructor((IShellFolder*)iface, This->m_pidlLocation, apidl, cidl);
1123 return S_OK;
1124 } else if (IsEqualIID(&IID_IDataObject, riid)) {
1125 *ppvOut = IDataObject_Constructor(hwndOwner, This->m_pidlLocation, apidl, cidl);
1126 return S_OK;
1127 } else if (IsEqualIID(&IID_IExtractIconA, riid)) {
1128 LPITEMIDLIST pidl;
1129 if (cidl != 1) return E_INVALIDARG;
1130 pidl = ILCombine(This->m_pidlLocation, apidl[0]);
1131 *ppvOut = (LPVOID)IExtractIconA_Constructor(pidl);
1132 SHFree(pidl);
1133 return S_OK;
1134 } else if (IsEqualIID(&IID_IExtractIconW, riid)) {
1135 LPITEMIDLIST pidl;
1136 if (cidl != 1) return E_INVALIDARG;
1137 pidl = ILCombine(This->m_pidlLocation, apidl[0]);
1138 *ppvOut = (LPVOID)IExtractIconW_Constructor(pidl);
1139 SHFree(pidl);
1140 return S_OK;
1141 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
1142 if (cidl != 1) return E_INVALIDARG;
1143 return IShellFolder2_BindToObject(iface, apidl[0], NULL, &IID_IDropTarget, ppvOut);
1144 } else if (IsEqualIID(&IID_IShellLinkW, riid)) {
1145 FIXME("IShellLinkW\n");
1146 return E_FAIL;
1147 } else if (IsEqualIID(&IID_IShellLinkA, riid)) {
1148 FIXME("IShellLinkA\n");
1149 return E_FAIL;
1150 } else {
1151 FIXME("Unknown interface %s in GetUIObjectOf\n", debugstr_guid(riid));
1152 return E_NOINTERFACE;
1156 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDisplayNameOf(IShellFolder2* iface,
1157 LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET* lpName)
1159 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1160 HRESULT hr = S_OK;
1162 TRACE("(iface=%p, pidl=%p, uFlags=%lx, lpName=%p)\n", iface, pidl, uFlags, lpName);
1164 if ((GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) &&
1165 (GET_SHGDN_RELATION(uFlags) != SHGDN_INFOLDER))
1167 if (!pidl || !pidl->mkid.cb) {
1168 lpName->uType = STRRET_WSTR;
1169 if (This->m_dwPathMode == PATHMODE_UNIX) {
1170 UINT len = MultiByteToWideChar(CP_UNIXCP, 0, This->m_pszPath, -1, NULL, 0);
1171 lpName->u.pOleStr = SHAlloc(len * sizeof(WCHAR));
1172 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1173 MultiByteToWideChar(CP_UNIXCP, 0, This->m_pszPath, -1, lpName->u.pOleStr, len);
1174 } else {
1175 LPWSTR pwszDosFileName = wine_get_dos_file_name(This->m_pszPath);
1176 if (!pwszDosFileName) return HRESULT_FROM_WIN32(GetLastError());
1177 lpName->u.pOleStr = SHAlloc((lstrlenW(pwszDosFileName) + 1) * sizeof(WCHAR));
1178 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1179 lstrcpyW(lpName->u.pOleStr, pwszDosFileName);
1180 PathRemoveBackslashW(lpName->u.pOleStr);
1181 HeapFree(GetProcessHeap(), 0, pwszDosFileName);
1183 } else {
1184 IShellFolder *pSubFolder;
1185 SHITEMID emptyIDL = { 0, { 0 } };
1187 hr = IShellFolder_BindToObject(iface, pidl, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1188 if (!SUCCEEDED(hr)) return hr;
1190 hr = IShellFolder_GetDisplayNameOf(pSubFolder, (LPITEMIDLIST)&emptyIDL, uFlags, lpName);
1191 IShellFolder_Release(pSubFolder);
1193 } else {
1194 WCHAR wszFileName[MAX_PATH];
1195 if (!_ILSimpleGetTextW(pidl, wszFileName, MAX_PATH)) return E_INVALIDARG;
1196 lpName->uType = STRRET_WSTR;
1197 lpName->u.pOleStr = SHAlloc((lstrlenW(wszFileName)+1)*sizeof(WCHAR));
1198 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1199 lstrcpyW(lpName->u.pOleStr, wszFileName);
1200 if (!(GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) && This->m_dwPathMode == PATHMODE_DOS &&
1201 !_ILIsFolder(pidl) && wszFileName[0] != '.' && SHELL_FS_HideExtension(wszFileName))
1203 PathRemoveExtensionW(lpName->u.pOleStr);
1207 TRACE("--> %s\n", debugstr_w(lpName->u.pOleStr));
1209 return hr;
1212 static HRESULT WINAPI UnixFolder_IShellFolder2_SetNameOf(IShellFolder2* iface, HWND hwnd,
1213 LPCITEMIDLIST pidl, LPCOLESTR lpszName, SHGDNF uFlags, LPITEMIDLIST* ppidlOut)
1215 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1217 char szSrc[FILENAME_MAX], szDest[FILENAME_MAX];
1218 WCHAR *pwszDosDest;
1219 int cBasePathLen = lstrlenA(This->m_pszPath);
1220 struct stat statDest;
1221 LPITEMIDLIST pidlSrc, pidlDest;
1223 TRACE("(iface=%p, hwnd=%p, pidl=%p, lpszName=%s, uFlags=0x%08lx, ppidlOut=%p)\n",
1224 iface, hwnd, pidl, debugstr_w(lpszName), uFlags, ppidlOut);
1226 /* pidl has to contain a single non-empty SHITEMID */
1227 if (_ILIsDesktop(pidl) || !_ILIsPidlSimple(pidl) || !_ILGetTextPointer(pidl))
1228 return E_INVALIDARG;
1230 if (ppidlOut)
1231 *ppidlOut = NULL;
1233 /* build source path */
1234 memcpy(szSrc, This->m_pszPath, cBasePathLen);
1235 UNIXFS_filename_from_shitemid(pidl, szSrc + cBasePathLen);
1237 /* build destination path */
1238 if (uFlags & SHGDN_FORPARSING) { /* absolute path in lpszName */
1239 WideCharToMultiByte(CP_UNIXCP, 0, lpszName, -1, szDest, FILENAME_MAX, NULL, NULL);
1240 } else {
1241 WCHAR wszSrcRelative[MAX_PATH];
1242 memcpy(szDest, This->m_pszPath, cBasePathLen);
1243 WideCharToMultiByte(CP_UNIXCP, 0, lpszName, -1, szDest+cBasePathLen,
1244 FILENAME_MAX-cBasePathLen, NULL, NULL);
1246 /* uFlags is SHGDN_FOREDITING of SHGDN_FORADDRESSBAR. If the filename's
1247 * extension is hidden to the user, we have to append it. */
1248 if (_ILSimpleGetTextW(pidl, wszSrcRelative, MAX_PATH) &&
1249 SHELL_FS_HideExtension(wszSrcRelative))
1251 WCHAR *pwszExt = PathFindExtensionW(wszSrcRelative);
1252 int cLenDest = strlen(szDest);
1253 WideCharToMultiByte(CP_UNIXCP, 0, pwszExt, -1, szDest + cLenDest,
1254 FILENAME_MAX - cLenDest, NULL, NULL);
1258 TRACE("src=%s dest=%s\n", szSrc, szDest);
1260 /* Fail, if destination does already exist */
1261 if (!stat(szDest, &statDest))
1262 return E_FAIL;
1264 /* Rename the file */
1265 if (rename(szSrc, szDest))
1266 return E_FAIL;
1268 /* Build a pidl for the path of the renamed file */
1269 pwszDosDest = wine_get_dos_file_name(szDest);
1270 if (!pwszDosDest || !UNIXFS_path_to_pidl(This, pwszDosDest, &pidlDest)) {
1271 HeapFree(GetProcessHeap(), 0, pwszDosDest);
1272 rename(szDest, szSrc); /* Undo the renaming */
1273 return E_FAIL;
1276 /* Inform the shell */
1277 pidlSrc = ILCombine(This->m_pidlLocation, pidl);
1278 if (_ILIsFolder(ILFindLastID(pidlDest)))
1279 SHChangeNotify(SHCNE_RENAMEFOLDER, SHCNF_IDLIST, pidlSrc, pidlDest);
1280 else
1281 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_IDLIST, pidlSrc, pidlDest);
1282 ILFree(pidlSrc);
1283 ILFree(pidlDest);
1285 if (ppidlOut)
1286 _ILCreateFromPathW(pwszDosDest, ppidlOut);
1288 HeapFree(GetProcessHeap(), 0, pwszDosDest);
1289 return S_OK;
1292 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumSearches(IShellFolder2* iface,
1293 IEnumExtraSearch **ppEnum)
1295 FIXME("stub\n");
1296 return E_NOTIMPL;
1299 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumn(IShellFolder2* iface,
1300 DWORD dwReserved, ULONG *pSort, ULONG *pDisplay)
1302 FIXME("stub\n");
1303 return E_NOTIMPL;
1306 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumnState(IShellFolder2* iface,
1307 UINT iColumn, SHCOLSTATEF *pcsFlags)
1309 FIXME("stub\n");
1310 return E_NOTIMPL;
1313 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultSearchGUID(IShellFolder2* iface,
1314 GUID *pguid)
1316 FIXME("stub\n");
1317 return E_NOTIMPL;
1320 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsEx(IShellFolder2* iface,
1321 LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
1323 FIXME("stub\n");
1324 return E_NOTIMPL;
1327 #define SHELLVIEWCOLUMNS 7
1329 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsOf(IShellFolder2* iface,
1330 LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
1332 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1333 HRESULT hr = E_FAIL;
1334 struct passwd *pPasswd;
1335 struct group *pGroup;
1336 static const shvheader SFHeader[SHELLVIEWCOLUMNS] = {
1337 {IDS_SHV_COLUMN1, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
1338 {IDS_SHV_COLUMN2, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1339 {IDS_SHV_COLUMN3, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1340 {IDS_SHV_COLUMN4, SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12},
1341 {IDS_SHV_COLUMN5, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 9},
1342 {IDS_SHV_COLUMN10, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
1343 {IDS_SHV_COLUMN11, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7}
1346 TRACE("(iface=%p, pidl=%p, iColumn=%d, psd=%p) stub\n", iface, pidl, iColumn, psd);
1348 if (!psd || iColumn >= SHELLVIEWCOLUMNS)
1349 return E_INVALIDARG;
1351 if (!pidl) {
1352 psd->fmt = SFHeader[iColumn].fmt;
1353 psd->cxChar = SFHeader[iColumn].cxChar;
1354 psd->str.uType = STRRET_CSTR;
1355 LoadStringA(shell32_hInstance, SFHeader[iColumn].colnameid, psd->str.u.cStr, MAX_PATH);
1356 return S_OK;
1357 } else {
1358 struct stat statItem;
1359 if (iColumn == 4 || iColumn == 5 || iColumn == 6) {
1360 char szPath[FILENAME_MAX];
1361 strcpy(szPath, This->m_pszPath);
1362 if (!UNIXFS_filename_from_shitemid(pidl, szPath + strlen(szPath)))
1363 return E_INVALIDARG;
1364 if (stat(szPath, &statItem))
1365 return E_INVALIDARG;
1367 psd->str.u.cStr[0] = '\0';
1368 psd->str.uType = STRRET_CSTR;
1369 switch (iColumn) {
1370 case 0:
1371 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL|SHGDN_INFOLDER, &psd->str);
1372 break;
1373 case 1:
1374 _ILGetFileSize(pidl, psd->str.u.cStr, MAX_PATH);
1375 break;
1376 case 2:
1377 _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
1378 break;
1379 case 3:
1380 _ILGetFileDate(pidl, psd->str.u.cStr, MAX_PATH);
1381 break;
1382 case 4:
1383 psd->str.u.cStr[0] = S_ISDIR(statItem.st_mode) ? 'd' : '-';
1384 psd->str.u.cStr[1] = (statItem.st_mode & S_IRUSR) ? 'r' : '-';
1385 psd->str.u.cStr[2] = (statItem.st_mode & S_IWUSR) ? 'w' : '-';
1386 psd->str.u.cStr[3] = (statItem.st_mode & S_IXUSR) ? 'x' : '-';
1387 psd->str.u.cStr[4] = (statItem.st_mode & S_IRGRP) ? 'r' : '-';
1388 psd->str.u.cStr[5] = (statItem.st_mode & S_IWGRP) ? 'w' : '-';
1389 psd->str.u.cStr[6] = (statItem.st_mode & S_IXGRP) ? 'x' : '-';
1390 psd->str.u.cStr[7] = (statItem.st_mode & S_IROTH) ? 'r' : '-';
1391 psd->str.u.cStr[8] = (statItem.st_mode & S_IWOTH) ? 'w' : '-';
1392 psd->str.u.cStr[9] = (statItem.st_mode & S_IXOTH) ? 'x' : '-';
1393 psd->str.u.cStr[10] = '\0';
1394 break;
1395 case 5:
1396 pPasswd = getpwuid(statItem.st_uid);
1397 if (pPasswd) strcpy(psd->str.u.cStr, pPasswd->pw_name);
1398 break;
1399 case 6:
1400 pGroup = getgrgid(statItem.st_gid);
1401 if (pGroup) strcpy(psd->str.u.cStr, pGroup->gr_name);
1402 break;
1406 return hr;
1409 static HRESULT WINAPI UnixFolder_IShellFolder2_MapColumnToSCID(IShellFolder2* iface, UINT iColumn,
1410 SHCOLUMNID *pscid)
1412 FIXME("stub\n");
1413 return E_NOTIMPL;
1416 /* VTable for UnixFolder's IShellFolder2 interface.
1418 static const IShellFolder2Vtbl UnixFolder_IShellFolder2_Vtbl = {
1419 UnixFolder_IShellFolder2_QueryInterface,
1420 UnixFolder_IShellFolder2_AddRef,
1421 UnixFolder_IShellFolder2_Release,
1422 UnixFolder_IShellFolder2_ParseDisplayName,
1423 UnixFolder_IShellFolder2_EnumObjects,
1424 UnixFolder_IShellFolder2_BindToObject,
1425 UnixFolder_IShellFolder2_BindToStorage,
1426 UnixFolder_IShellFolder2_CompareIDs,
1427 UnixFolder_IShellFolder2_CreateViewObject,
1428 UnixFolder_IShellFolder2_GetAttributesOf,
1429 UnixFolder_IShellFolder2_GetUIObjectOf,
1430 UnixFolder_IShellFolder2_GetDisplayNameOf,
1431 UnixFolder_IShellFolder2_SetNameOf,
1432 UnixFolder_IShellFolder2_GetDefaultSearchGUID,
1433 UnixFolder_IShellFolder2_EnumSearches,
1434 UnixFolder_IShellFolder2_GetDefaultColumn,
1435 UnixFolder_IShellFolder2_GetDefaultColumnState,
1436 UnixFolder_IShellFolder2_GetDetailsEx,
1437 UnixFolder_IShellFolder2_GetDetailsOf,
1438 UnixFolder_IShellFolder2_MapColumnToSCID
1441 static HRESULT WINAPI UnixFolder_IPersistFolder3_QueryInterface(IPersistFolder3* iface, REFIID riid,
1442 void** ppvObject)
1444 return UnixFolder_IShellFolder2_QueryInterface(
1445 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistFolder3, iface)), riid, ppvObject);
1448 static ULONG WINAPI UnixFolder_IPersistFolder3_AddRef(IPersistFolder3* iface)
1450 return UnixFolder_IShellFolder2_AddRef(
1451 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistFolder3, iface)));
1454 static ULONG WINAPI UnixFolder_IPersistFolder3_Release(IPersistFolder3* iface)
1456 return UnixFolder_IShellFolder2_Release(
1457 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistFolder3, iface)));
1460 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetClassID(IPersistFolder3* iface, CLSID* pClassID)
1462 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1464 TRACE("(iface=%p, pClassId=%p)\n", iface, pClassID);
1466 if (!pClassID)
1467 return E_INVALIDARG;
1469 memcpy(pClassID, This->m_pCLSID, sizeof(CLSID));
1470 return S_OK;
1473 static HRESULT WINAPI UnixFolder_IPersistFolder3_Initialize(IPersistFolder3* iface, LPCITEMIDLIST pidl)
1475 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1476 LPCITEMIDLIST current = pidl;
1477 char szBasePath[FILENAME_MAX] = "/";
1479 TRACE("(iface=%p, pidl=%p)\n", iface, pidl);
1481 /* Find the UnixFolderClass root */
1482 while (current->mkid.cb) {
1483 if (_ILIsSpecialFolder(current) && IsEqualIID(This->m_pCLSID, _ILGetGUIDPointer(current)))
1484 break;
1485 current = ILGetNext(current);
1488 if (current && current->mkid.cb) {
1489 if (IsEqualIID(&CLSID_MyDocuments, _ILGetGUIDPointer(current))) {
1490 WCHAR wszMyDocumentsPath[MAX_PATH];
1491 if (!SHGetSpecialFolderPathW(0, wszMyDocumentsPath, CSIDL_PERSONAL, FALSE))
1492 return E_FAIL;
1493 PathAddBackslashW(wszMyDocumentsPath);
1494 if (!UNIXFS_get_unix_path(wszMyDocumentsPath, szBasePath))
1495 return E_FAIL;
1497 current = ILGetNext(current);
1498 } else if (_ILIsDesktop(pidl) || _ILIsValue(pidl) || _ILIsFolder(pidl)) {
1499 /* Path rooted at Desktop */
1500 WCHAR wszDesktopPath[MAX_PATH];
1501 if (!SHGetSpecialFolderPathW(0, wszDesktopPath, CSIDL_DESKTOPDIRECTORY, FALSE))
1502 return E_FAIL;
1503 PathAddBackslashW(wszDesktopPath);
1504 if (!UNIXFS_get_unix_path(wszDesktopPath, szBasePath))
1505 return E_FAIL;
1506 current = pidl;
1507 } else if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
1508 /* FolderShortcuts' Initialize method only sets the ITEMIDLIST, which
1509 * specifies the location in the shell namespace, but leaves the
1510 * target folder (m_pszPath) alone. See unit tests in tests/shlfolder.c */
1511 This->m_pidlLocation = ILClone(pidl);
1512 return S_OK;
1513 } else {
1514 ERR("Unknown pidl type!\n");
1515 pdump(pidl);
1516 return E_INVALIDARG;
1519 This->m_pidlLocation = ILClone(pidl);
1520 return UNIXFS_initialize_target_folder(This, szBasePath, current, 0);
1523 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetCurFolder(IPersistFolder3* iface, LPITEMIDLIST* ppidl)
1525 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1527 TRACE ("(iface=%p, ppidl=%p)\n", iface, ppidl);
1529 if (!ppidl)
1530 return E_POINTER;
1531 *ppidl = ILClone (This->m_pidlLocation);
1532 return S_OK;
1535 static HRESULT WINAPI UnixFolder_IPersistFolder3_InitializeEx(IPersistFolder3 *iface, IBindCtx *pbc,
1536 LPCITEMIDLIST pidlRoot, const PERSIST_FOLDER_TARGET_INFO *ppfti)
1538 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1539 WCHAR wszTargetDosPath[MAX_PATH];
1540 char szTargetPath[FILENAME_MAX] = "";
1542 TRACE("(iface=%p, pbc=%p, pidlRoot=%p, ppfti=%p)\n", iface, pbc, pidlRoot, ppfti);
1544 /* If no PERSIST_FOLDER_TARGET_INFO is given InitializeEx is equivalent to Initialize. */
1545 if (!ppfti)
1546 return IPersistFolder3_Initialize(iface, pidlRoot);
1548 if (ppfti->csidl != -1) {
1549 if (FAILED(SHGetFolderPathW(0, ppfti->csidl, NULL, 0, wszTargetDosPath)) ||
1550 !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1552 return E_FAIL;
1554 } else if (*ppfti->szTargetParsingName) {
1555 lstrcpyW(wszTargetDosPath, ppfti->szTargetParsingName);
1556 PathAddBackslashW(wszTargetDosPath);
1557 if (!UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath)) {
1558 return E_FAIL;
1560 } else if (ppfti->pidlTargetFolder) {
1561 if (!SHGetPathFromIDListW(ppfti->pidlTargetFolder, wszTargetDosPath) ||
1562 !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1564 return E_FAIL;
1566 } else {
1567 return E_FAIL;
1570 This->m_pszPath = SHAlloc(lstrlenA(szTargetPath)+1);
1571 if (!This->m_pszPath)
1572 return E_FAIL;
1573 lstrcpyA(This->m_pszPath, szTargetPath);
1574 This->m_pidlLocation = ILClone(pidlRoot);
1575 This->m_dwAttributes = (ppfti->dwAttributes != -1) ? ppfti->dwAttributes :
1576 (SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME|SFGAO_FILESYSTEM);
1578 return S_OK;
1581 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetFolderTargetInfo(IPersistFolder3 *iface,
1582 PERSIST_FOLDER_TARGET_INFO *ppfti)
1584 FIXME("(iface=%p, ppfti=%p) stub\n", iface, ppfti);
1585 return E_NOTIMPL;
1588 /* VTable for UnixFolder's IPersistFolder interface.
1590 static const IPersistFolder3Vtbl UnixFolder_IPersistFolder3_Vtbl = {
1591 UnixFolder_IPersistFolder3_QueryInterface,
1592 UnixFolder_IPersistFolder3_AddRef,
1593 UnixFolder_IPersistFolder3_Release,
1594 UnixFolder_IPersistFolder3_GetClassID,
1595 UnixFolder_IPersistFolder3_Initialize,
1596 UnixFolder_IPersistFolder3_GetCurFolder,
1597 UnixFolder_IPersistFolder3_InitializeEx,
1598 UnixFolder_IPersistFolder3_GetFolderTargetInfo
1601 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_QueryInterface(IPersistPropertyBag* iface,
1602 REFIID riid, void** ppv)
1604 return UnixFolder_IShellFolder2_QueryInterface(
1605 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)), riid, ppv);
1608 static ULONG WINAPI UnixFolder_IPersistPropertyBag_AddRef(IPersistPropertyBag* iface)
1610 return UnixFolder_IShellFolder2_AddRef(
1611 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)));
1614 static ULONG WINAPI UnixFolder_IPersistPropertyBag_Release(IPersistPropertyBag* iface)
1616 return UnixFolder_IShellFolder2_Release(
1617 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)));
1620 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_GetClassID(IPersistPropertyBag* iface,
1621 CLSID* pClassID)
1623 return UnixFolder_IPersistFolder3_GetClassID(
1624 STATIC_CAST(IPersistFolder3, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)), pClassID);
1627 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_InitNew(IPersistPropertyBag* iface)
1629 FIXME("() stub\n");
1630 return E_NOTIMPL;
1633 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_Load(IPersistPropertyBag *iface,
1634 IPropertyBag *pPropertyBag, IErrorLog *pErrorLog)
1636 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface);
1637 static const WCHAR wszTarget[] = { 'T','a','r','g','e','t', 0 }, wszNull[] = { 0 };
1638 PERSIST_FOLDER_TARGET_INFO pftiTarget;
1639 VARIANT var;
1640 HRESULT hr;
1642 TRACE("(iface=%p, pPropertyBag=%p, pErrorLog=%p)\n", iface, pPropertyBag, pErrorLog);
1644 if (!pPropertyBag)
1645 return E_POINTER;
1647 /* Get 'Target' property from the property bag. */
1648 V_VT(&var) = VT_BSTR;
1649 hr = IPropertyBag_Read(pPropertyBag, wszTarget, &var, NULL);
1650 if (FAILED(hr))
1651 return E_FAIL;
1652 lstrcpyW(pftiTarget.szTargetParsingName, V_BSTR(&var));
1653 SysFreeString(V_BSTR(&var));
1655 pftiTarget.pidlTargetFolder = NULL;
1656 lstrcpyW(pftiTarget.szNetworkProvider, wszNull);
1657 pftiTarget.dwAttributes = -1;
1658 pftiTarget.csidl = -1;
1660 return UnixFolder_IPersistFolder3_InitializeEx(
1661 STATIC_CAST(IPersistFolder3, This), NULL, NULL, &pftiTarget);
1664 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_Save(IPersistPropertyBag *iface,
1665 IPropertyBag *pPropertyBag, BOOL fClearDirty, BOOL fSaveAllProperties)
1667 FIXME("() stub\n");
1668 return E_NOTIMPL;
1671 /* VTable for UnixFolder's IPersistPropertyBag interface.
1673 static const IPersistPropertyBagVtbl UnixFolder_IPersistPropertyBag_Vtbl = {
1674 UnixFolder_IPersistPropertyBag_QueryInterface,
1675 UnixFolder_IPersistPropertyBag_AddRef,
1676 UnixFolder_IPersistPropertyBag_Release,
1677 UnixFolder_IPersistPropertyBag_GetClassID,
1678 UnixFolder_IPersistPropertyBag_InitNew,
1679 UnixFolder_IPersistPropertyBag_Load,
1680 UnixFolder_IPersistPropertyBag_Save
1683 static HRESULT WINAPI UnixFolder_ISFHelper_QueryInterface(ISFHelper* iface, REFIID riid,
1684 void** ppvObject)
1686 return UnixFolder_IShellFolder2_QueryInterface(
1687 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, ISFHelper, iface)), riid, ppvObject);
1690 static ULONG WINAPI UnixFolder_ISFHelper_AddRef(ISFHelper* iface)
1692 return UnixFolder_IShellFolder2_AddRef(
1693 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, ISFHelper, iface)));
1696 static ULONG WINAPI UnixFolder_ISFHelper_Release(ISFHelper* iface)
1698 return UnixFolder_IShellFolder2_Release(
1699 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, ISFHelper, iface)));
1702 static HRESULT WINAPI UnixFolder_ISFHelper_GetUniqueName(ISFHelper* iface, LPSTR lpName, UINT uLen)
1704 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1705 IEnumIDList *pEnum;
1706 HRESULT hr;
1707 LPITEMIDLIST pidlElem;
1708 DWORD dwFetched;
1709 int i;
1710 static const char szNewFolder[] = "New Folder";
1712 TRACE("(iface=%p, lpName=%p, uLen=%u)\n", iface, lpName, uLen);
1714 if (uLen < sizeof(szNewFolder)+3)
1715 return E_INVALIDARG;
1717 hr = IShellFolder2_EnumObjects(STATIC_CAST(IShellFolder2, This), 0,
1718 SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN, &pEnum);
1719 if (SUCCEEDED(hr)) {
1720 lstrcpyA(lpName, szNewFolder);
1721 IEnumIDList_Reset(pEnum);
1722 i = 2;
1723 while ((IEnumIDList_Next(pEnum, 1, &pidlElem, &dwFetched) == S_OK) && (dwFetched == 1)) {
1724 if (!strcasecmp(_ILGetTextPointer(pidlElem), lpName)) {
1725 IEnumIDList_Reset(pEnum);
1726 sprintf(lpName, "%s %d", szNewFolder, i++);
1727 if (i > 99) {
1728 hr = E_FAIL;
1729 break;
1733 IEnumIDList_Release(pEnum);
1735 return hr;
1738 static HRESULT WINAPI UnixFolder_ISFHelper_AddFolder(ISFHelper* iface, HWND hwnd, LPCSTR pszName,
1739 LPITEMIDLIST* ppidlOut)
1741 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1742 char szNewDir[FILENAME_MAX];
1744 TRACE("(iface=%p, hwnd=%p, pszName=%s, ppidlOut=%p)\n", iface, hwnd, pszName, ppidlOut);
1746 if (ppidlOut)
1747 *ppidlOut = NULL;
1749 lstrcpyA(szNewDir, This->m_pszPath);
1750 lstrcatA(szNewDir, pszName);
1752 if (mkdir(szNewDir, 0755)) {
1753 char szMessage[256 + FILENAME_MAX];
1754 char szCaption[256];
1756 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_DENIED, szCaption, sizeof(szCaption));
1757 sprintf(szMessage, szCaption, szNewDir);
1758 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_CAPTION, szCaption, sizeof(szCaption));
1759 MessageBoxA(hwnd, szMessage, szCaption, MB_OK | MB_ICONEXCLAMATION);
1761 return E_FAIL;
1762 } else {
1763 LPITEMIDLIST pidlRelative;
1764 WCHAR wszName[MAX_PATH];
1766 /* Inform the shell */
1767 MultiByteToWideChar(CP_UNIXCP, 0, pszName, -1, wszName, MAX_PATH);
1768 if (UNIXFS_path_to_pidl(This, wszName, &pidlRelative)) {
1769 LPITEMIDLIST pidlAbsolute = ILCombine(This->m_pidlLocation, pidlRelative);
1770 if (ppidlOut)
1771 *ppidlOut = pidlRelative;
1772 else
1773 ILFree(pidlRelative);
1774 SHChangeNotify(SHCNE_MKDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1775 ILFree(pidlAbsolute);
1777 return S_OK;
1781 static HRESULT WINAPI UnixFolder_ISFHelper_DeleteItems(ISFHelper* iface, UINT cidl,
1782 LPCITEMIDLIST* apidl)
1784 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1785 char szAbsolute[FILENAME_MAX], *pszRelative;
1786 LPITEMIDLIST pidlAbsolute;
1787 HRESULT hr = S_OK;
1788 UINT i;
1790 TRACE("(iface=%p, cidl=%d, apidl=%p)\n", iface, cidl, apidl);
1792 lstrcpyA(szAbsolute, This->m_pszPath);
1793 pszRelative = szAbsolute + lstrlenA(szAbsolute);
1795 for (i=0; i<cidl && SUCCEEDED(hr); i++) {
1796 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
1797 return E_INVALIDARG;
1798 pidlAbsolute = ILCombine(This->m_pidlLocation, apidl[i]);
1799 if (_ILIsFolder(apidl[i])) {
1800 if (rmdir(szAbsolute)) {
1801 hr = E_FAIL;
1802 } else {
1803 SHChangeNotify(SHCNE_RMDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1805 } else if (_ILIsValue(apidl[i])) {
1806 if (unlink(szAbsolute)) {
1807 hr = E_FAIL;
1808 } else {
1809 SHChangeNotify(SHCNE_DELETE, SHCNF_IDLIST, pidlAbsolute, NULL);
1812 ILFree(pidlAbsolute);
1815 return hr;
1818 static HRESULT WINAPI UnixFolder_ISFHelper_CopyItems(ISFHelper* iface, IShellFolder *psfFrom,
1819 UINT cidl, LPCITEMIDLIST *apidl)
1821 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1822 DWORD dwAttributes;
1823 UINT i;
1824 HRESULT hr;
1825 char szAbsoluteDst[FILENAME_MAX], *pszRelativeDst;
1827 TRACE("(iface=%p, psfFrom=%p, cidl=%d, apidl=%p): semi-stub\n", iface, psfFrom, cidl, apidl);
1829 if (!psfFrom || !cidl || !apidl)
1830 return E_INVALIDARG;
1832 /* All source items have to be filesystem items. */
1833 dwAttributes = SFGAO_FILESYSTEM;
1834 hr = IShellFolder_GetAttributesOf(psfFrom, cidl, apidl, &dwAttributes);
1835 if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM))
1836 return E_INVALIDARG;
1838 lstrcpyA(szAbsoluteDst, This->m_pszPath);
1839 pszRelativeDst = szAbsoluteDst + strlen(szAbsoluteDst);
1841 for (i=0; i<cidl; i++) {
1842 WCHAR wszSrc[MAX_PATH];
1843 char szSrc[FILENAME_MAX];
1844 STRRET strret;
1846 /* Build the unix path of the current source item. */
1847 if (FAILED(IShellFolder_GetDisplayNameOf(psfFrom, apidl[i], SHGDN_FORPARSING, &strret)))
1848 return E_FAIL;
1849 if (FAILED(StrRetToBufW(&strret, apidl[i], wszSrc, MAX_PATH)))
1850 return E_FAIL;
1851 if (!UNIXFS_get_unix_path(wszSrc, szSrc))
1852 return E_FAIL;
1854 /* Build the unix path of the current destination item */
1855 UNIXFS_filename_from_shitemid(apidl[i], pszRelativeDst);
1857 FIXME("Would copy %s to %s. Not yet implemented.\n", szSrc, szAbsoluteDst);
1859 return S_OK;
1862 /* VTable for UnixFolder's ISFHelper interface
1864 static const ISFHelperVtbl UnixFolder_ISFHelper_Vtbl = {
1865 UnixFolder_ISFHelper_QueryInterface,
1866 UnixFolder_ISFHelper_AddRef,
1867 UnixFolder_ISFHelper_Release,
1868 UnixFolder_ISFHelper_GetUniqueName,
1869 UnixFolder_ISFHelper_AddFolder,
1870 UnixFolder_ISFHelper_DeleteItems,
1871 UnixFolder_ISFHelper_CopyItems
1874 static HRESULT WINAPI UnixFolder_IDropTarget_QueryInterface(IDropTarget* iface, REFIID riid,
1875 void** ppvObject)
1877 return UnixFolder_IShellFolder2_QueryInterface(
1878 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IDropTarget, iface)), riid, ppvObject);
1881 static ULONG WINAPI UnixFolder_IDropTarget_AddRef(IDropTarget* iface)
1883 return UnixFolder_IShellFolder2_AddRef(
1884 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IDropTarget, iface)));
1887 static ULONG WINAPI UnixFolder_IDropTarget_Release(IDropTarget* iface)
1889 return UnixFolder_IShellFolder2_Release(
1890 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IDropTarget, iface)));
1893 #define HIDA_GetPIDLFolder(pida) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[0])
1894 #define HIDA_GetPIDLItem(pida, i) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[i+1])
1896 static HRESULT WINAPI UnixFolder_IDropTarget_DragEnter(IDropTarget *iface, IDataObject *pDataObject,
1897 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
1899 UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
1900 FORMATETC format;
1901 STGMEDIUM medium;
1903 TRACE("(iface=%p, pDataObject=%p, dwKeyState=%08lx, pt={.x=%ld, .y=%ld}, pdwEffect=%p)\n",
1904 iface, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
1906 if (!pdwEffect || !pDataObject)
1907 return E_INVALIDARG;
1909 /* Compute a mask of supported drop-effects for this shellfolder object and the given data
1910 * object. Dropping is only supported on folders, which represent filesystem locations. One
1911 * can't drop on file objects. And the 'move' drop effect is only supported, if the source
1912 * folder is not identical to the target folder. */
1913 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
1914 InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
1915 if ((This->m_dwAttributes & SFGAO_FILESYSTEM) && /* Only drop to filesystem folders */
1916 _ILIsFolder(ILFindLastID(This->m_pidlLocation)) && /* Only drop to folders, not to files */
1917 SUCCEEDED(IDataObject_GetData(pDataObject, &format, &medium))) /* Only ShellIDList format */
1919 LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
1920 This->m_dwDropEffectsMask |= DROPEFFECT_COPY|DROPEFFECT_LINK;
1922 if (pidaShellIDList) { /* Files can only be moved between two different folders */
1923 if (!ILIsEqual(HIDA_GetPIDLFolder(pidaShellIDList), This->m_pidlLocation))
1924 This->m_dwDropEffectsMask |= DROPEFFECT_MOVE;
1925 GlobalUnlock(medium.u.hGlobal);
1929 *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
1931 return S_OK;
1934 static HRESULT WINAPI UnixFolder_IDropTarget_DragOver(IDropTarget *iface, DWORD dwKeyState,
1935 POINTL pt, DWORD *pdwEffect)
1937 UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
1939 TRACE("(iface=%p, dwKeyState=%08lx, pt={.x=%ld, .y=%ld}, pdwEffect=%p)\n", iface, dwKeyState,
1940 pt.x, pt.y, pdwEffect);
1942 if (!pdwEffect)
1943 return E_INVALIDARG;
1945 *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
1947 return S_OK;
1950 static HRESULT WINAPI UnixFolder_IDropTarget_DragLeave(IDropTarget *iface) {
1951 UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
1953 TRACE("(iface=%p)\n", iface);
1955 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
1957 return S_OK;
1960 static HRESULT WINAPI UnixFolder_IDropTarget_Drop(IDropTarget *iface, IDataObject *pDataObject,
1961 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
1963 UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
1964 FORMATETC format;
1965 STGMEDIUM medium;
1966 HRESULT hr;
1968 TRACE("(iface=%p, pDataObject=%p, dwKeyState=%ld, pt={.x=%ld, .y=%ld}, pdwEffect=%p) semi-stub\n",
1969 iface, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
1971 InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
1972 hr = IDataObject_GetData(pDataObject, &format, &medium);
1973 if (!SUCCEEDED(hr))
1974 return hr;
1976 if (medium.tymed == TYMED_HGLOBAL) {
1977 IShellFolder *psfSourceFolder, *psfDesktopFolder;
1978 LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
1979 STRRET strret;
1980 UINT i;
1982 if (!pidaShellIDList)
1983 return HRESULT_FROM_WIN32(GetLastError());
1985 hr = SHGetDesktopFolder(&psfDesktopFolder);
1986 if (FAILED(hr)) {
1987 GlobalUnlock(medium.u.hGlobal);
1988 return hr;
1991 hr = IShellFolder_BindToObject(psfDesktopFolder, HIDA_GetPIDLFolder(pidaShellIDList), NULL,
1992 &IID_IShellFolder, (LPVOID*)&psfSourceFolder);
1993 IShellFolder_Release(psfDesktopFolder);
1994 if (FAILED(hr)) {
1995 GlobalUnlock(medium.u.hGlobal);
1996 return hr;
1999 for (i = 0; i < pidaShellIDList->cidl; i++) {
2000 WCHAR wszSourcePath[MAX_PATH];
2002 hr = IShellFolder_GetDisplayNameOf(psfSourceFolder, HIDA_GetPIDLItem(pidaShellIDList, i),
2003 SHGDN_FORPARSING, &strret);
2004 if (FAILED(hr))
2005 break;
2007 hr = StrRetToBufW(&strret, NULL, wszSourcePath, MAX_PATH);
2008 if (FAILED(hr))
2009 break;
2011 switch (*pdwEffect) {
2012 case DROPEFFECT_MOVE:
2013 FIXME("Move %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2014 break;
2015 case DROPEFFECT_COPY:
2016 FIXME("Copy %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2017 break;
2018 case DROPEFFECT_LINK:
2019 FIXME("Link %s from %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2020 break;
2024 IShellFolder_Release(psfSourceFolder);
2025 GlobalUnlock(medium.u.hGlobal);
2026 return hr;
2029 return E_NOTIMPL;
2032 /* VTable for UnixFolder's IDropTarget interface
2034 static const IDropTargetVtbl UnixFolder_IDropTarget_Vtbl = {
2035 UnixFolder_IDropTarget_QueryInterface,
2036 UnixFolder_IDropTarget_AddRef,
2037 UnixFolder_IDropTarget_Release,
2038 UnixFolder_IDropTarget_DragEnter,
2039 UnixFolder_IDropTarget_DragOver,
2040 UnixFolder_IDropTarget_DragLeave,
2041 UnixFolder_IDropTarget_Drop
2044 /******************************************************************************
2045 * Unix[Dos]Folder_Constructor [Internal]
2047 * PARAMS
2048 * pUnkOuter [I] Outer class for aggregation. Currently ignored.
2049 * riid [I] Interface asked for by the client.
2050 * ppv [O] Pointer to an riid interface to the UnixFolder object.
2052 * NOTES
2053 * Those are the only functions exported from shfldr_unixfs.c. They are called from
2054 * shellole.c's default class factory and thus have to exhibit a LPFNCREATEINSTANCE
2055 * compatible signature.
2057 * The UnixDosFolder_Constructor sets the dwPathMode member to PATHMODE_DOS. This
2058 * means that paths are converted from dos to unix and back at the interfaces.
2060 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, const CLSID *pCLSID)
2062 HRESULT hr = E_FAIL;
2063 UnixFolder *pUnixFolder = SHAlloc((ULONG)sizeof(UnixFolder));
2065 if (pUnkOuter) {
2066 FIXME("Aggregation not yet implemented!\n");
2067 return CLASS_E_NOAGGREGATION;
2070 if(pUnixFolder) {
2071 pUnixFolder->lpIShellFolder2Vtbl = &UnixFolder_IShellFolder2_Vtbl;
2072 pUnixFolder->lpIPersistFolder3Vtbl = &UnixFolder_IPersistFolder3_Vtbl;
2073 pUnixFolder->lpIPersistPropertyBagVtbl = &UnixFolder_IPersistPropertyBag_Vtbl;
2074 pUnixFolder->lpISFHelperVtbl = &UnixFolder_ISFHelper_Vtbl;
2075 pUnixFolder->lpIDropTargetVtbl = &UnixFolder_IDropTarget_Vtbl;
2076 pUnixFolder->m_cRef = 0;
2077 pUnixFolder->m_pszPath = NULL;
2078 pUnixFolder->m_pidlLocation = NULL;
2079 pUnixFolder->m_dwPathMode = IsEqualCLSID(&CLSID_UnixFolder, pCLSID) ? PATHMODE_UNIX : PATHMODE_DOS;
2080 pUnixFolder->m_dwAttributes = 0;
2081 pUnixFolder->m_pCLSID = pCLSID;
2082 pUnixFolder->m_dwDropEffectsMask = DROPEFFECT_NONE;
2084 UnixFolder_IShellFolder2_AddRef(STATIC_CAST(IShellFolder2, pUnixFolder));
2085 hr = UnixFolder_IShellFolder2_QueryInterface(STATIC_CAST(IShellFolder2, pUnixFolder), riid, ppv);
2086 UnixFolder_IShellFolder2_Release(STATIC_CAST(IShellFolder2, pUnixFolder));
2088 return hr;
2091 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2092 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2093 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixFolder);
2096 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2097 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2098 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixDosFolder);
2101 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2102 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2103 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_FolderShortcut);
2106 HRESULT WINAPI MyDocuments_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2107 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2108 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_MyDocuments);
2111 /******************************************************************************
2112 * UnixSubFolderIterator
2114 * Class whose heap based objects represent iterators over the sub-directories
2115 * of a given UnixFolder object.
2118 /* UnixSubFolderIterator object layout and typedef.
2120 typedef struct _UnixSubFolderIterator {
2121 const IEnumIDListVtbl *lpIEnumIDListVtbl;
2122 LONG m_cRef;
2123 SHCONTF m_fFilter;
2124 DIR *m_dirFolder;
2125 char m_szFolder[FILENAME_MAX];
2126 } UnixSubFolderIterator;
2128 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
2129 TRACE("(iterator=%p)\n", iterator);
2131 if (iterator->m_dirFolder)
2132 closedir(iterator->m_dirFolder);
2133 SHFree(iterator);
2136 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface,
2137 REFIID riid, void** ppv)
2139 TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
2141 if (!ppv) return E_INVALIDARG;
2143 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
2144 *ppv = iface;
2145 } else {
2146 *ppv = NULL;
2147 return E_NOINTERFACE;
2150 IEnumIDList_AddRef(iface);
2151 return S_OK;
2154 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
2156 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2158 TRACE("(iface=%p)\n", iface);
2160 return InterlockedIncrement(&This->m_cRef);
2163 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
2165 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2166 ULONG cRef;
2168 TRACE("(iface=%p)\n", iface);
2170 cRef = InterlockedDecrement(&This->m_cRef);
2172 if (!cRef)
2173 UnixSubFolderIterator_Destroy(This);
2175 return cRef;
2178 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt,
2179 LPITEMIDLIST* rgelt, ULONG* pceltFetched)
2181 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2182 ULONG i = 0;
2184 /* This->m_dirFolder will be NULL if the user doesn't have access rights for the dir. */
2185 if (This->m_dirFolder) {
2186 char *pszRelativePath = This->m_szFolder + lstrlenA(This->m_szFolder);
2187 struct dirent *pDirEntry;
2189 while (i < celt) {
2190 pDirEntry = readdir(This->m_dirFolder);
2191 if (!pDirEntry) break; /* No more entries */
2192 if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
2194 /* Temporarily build absolute path in This->m_szFolder. Then construct a pidl
2195 * and see if it passes the filter.
2197 lstrcpyA(pszRelativePath, pDirEntry->d_name);
2198 rgelt[i] = (LPITEMIDLIST)SHAlloc(
2199 UNIXFS_shitemid_len_from_filename(pszRelativePath, NULL, NULL)+sizeof(USHORT));
2200 if (!UNIXFS_build_shitemid(This->m_szFolder, rgelt[i]) ||
2201 !UNIXFS_is_pidl_of_type(rgelt[i], This->m_fFilter))
2203 SHFree(rgelt[i]);
2204 continue;
2206 memset(((PBYTE)rgelt[i])+rgelt[i]->mkid.cb, 0, sizeof(USHORT));
2207 i++;
2209 *pszRelativePath = '\0'; /* Restore the original path in This->m_szFolder. */
2212 if (pceltFetched)
2213 *pceltFetched = i;
2215 return (i == 0) ? S_FALSE : S_OK;
2218 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
2220 LPITEMIDLIST *apidl;
2221 ULONG cFetched;
2222 HRESULT hr;
2224 TRACE("(iface=%p, celt=%ld)\n", iface, celt);
2226 /* Call IEnumIDList::Next and delete the resulting pidls. */
2227 apidl = (LPITEMIDLIST*)SHAlloc(celt * sizeof(LPITEMIDLIST));
2228 hr = IEnumIDList_Next(iface, celt, apidl, &cFetched);
2229 if (SUCCEEDED(hr))
2230 while (cFetched--)
2231 SHFree(apidl[cFetched]);
2232 SHFree(apidl);
2234 return hr;
2237 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
2239 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2241 TRACE("(iface=%p)\n", iface);
2243 if (This->m_dirFolder)
2244 rewinddir(This->m_dirFolder);
2246 return S_OK;
2249 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This,
2250 IEnumIDList** ppenum)
2252 FIXME("stub\n");
2253 return E_NOTIMPL;
2256 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
2258 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
2259 UnixSubFolderIterator_IEnumIDList_QueryInterface,
2260 UnixSubFolderIterator_IEnumIDList_AddRef,
2261 UnixSubFolderIterator_IEnumIDList_Release,
2262 UnixSubFolderIterator_IEnumIDList_Next,
2263 UnixSubFolderIterator_IEnumIDList_Skip,
2264 UnixSubFolderIterator_IEnumIDList_Reset,
2265 UnixSubFolderIterator_IEnumIDList_Clone
2268 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter) {
2269 UnixSubFolderIterator *iterator;
2271 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
2273 iterator = SHAlloc((ULONG)sizeof(UnixSubFolderIterator));
2274 iterator->lpIEnumIDListVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
2275 iterator->m_cRef = 0;
2276 iterator->m_fFilter = fFilter;
2277 iterator->m_dirFolder = opendir(pUnixFolder->m_pszPath);
2278 lstrcpyA(iterator->m_szFolder, pUnixFolder->m_pszPath);
2280 UnixSubFolderIterator_IEnumIDList_AddRef((IEnumIDList*)iterator);
2282 return (IUnknown*)iterator;