push 091faec692ac84fbc736a6b002e76f7e78d28f7b
[wine/hacks.git] / dlls / shell32 / shfldr_unixfs.c
blob9c9f94afad6309a9ec29b78e77075161e5ec3369
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 apart 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 shouldn't 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 safely 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 #ifdef HAVE_DIRENT_H
132 # include <dirent.h>
133 #endif
134 #include <stdlib.h>
135 #ifdef HAVE_UNISTD_H
136 # include <unistd.h>
137 #endif
138 #ifdef HAVE_SYS_STAT_H
139 # include <sys/stat.h>
140 #endif
141 #ifdef HAVE_PWD_H
142 # include <pwd.h>
143 #endif
144 #ifdef HAVE_GRP_H
145 # include <grp.h>
146 #endif
148 #define COBJMACROS
149 #define NONAMELESSUNION
150 #define NONAMELESSSTRUCT
152 #include "windef.h"
153 #include "winbase.h"
154 #include "winuser.h"
155 #include "objbase.h"
156 #include "winreg.h"
157 #include "shlwapi.h"
158 #include "winternl.h"
159 #include "wine/debug.h"
161 #include "shell32_main.h"
162 #include "shellfolder.h"
163 #include "shfldr.h"
164 #include "shresdef.h"
165 #include "pidl.h"
166 #include "debughlp.h"
168 WINE_DEFAULT_DEBUG_CHANNEL(shell);
170 #if !defined(__MINGW32__) && !defined(_MSC_VER)
172 #define ADJUST_THIS(c,m,p) ((c*)(((long)p)-(long)&(((c*)0)->lp##m##Vtbl)))
173 #define STATIC_CAST(i,p) ((i*)&p->lp##i##Vtbl)
175 #define LEN_SHITEMID_FIXED_PART ((USHORT) \
176 ( sizeof(USHORT) /* SHITEMID's cb field. */ \
177 + sizeof(PIDLTYPE) /* PIDLDATA's type field. */ \
178 + sizeof(FileStruct) /* Well, the FileStruct. */ \
179 - sizeof(char) /* One char too much in FileStruct. */ \
180 + sizeof(FileStructW) /* You name it. */ \
181 - sizeof(WCHAR) /* One WCHAR too much in FileStructW. */ \
182 + sizeof(WORD) )) /* Offset of FileStructW field in PIDL. */
184 #define PATHMODE_UNIX 0
185 #define PATHMODE_DOS 1
187 /* UnixFolder object layout and typedef.
189 typedef struct _UnixFolder {
190 const IShellFolder2Vtbl *lpIShellFolder2Vtbl;
191 const IPersistFolder3Vtbl *lpIPersistFolder3Vtbl;
192 const IPersistPropertyBagVtbl *lpIPersistPropertyBagVtbl;
193 const IDropTargetVtbl *lpIDropTargetVtbl;
194 const ISFHelperVtbl *lpISFHelperVtbl;
195 LONG m_cRef;
196 CHAR *m_pszPath; /* Target path of the shell folder (CP_UNIXCP) */
197 LPITEMIDLIST m_pidlLocation; /* Location in the shell namespace */
198 DWORD m_dwPathMode;
199 DWORD m_dwAttributes;
200 const CLSID *m_pCLSID;
201 DWORD m_dwDropEffectsMask;
202 } UnixFolder;
204 /* Will hold the registered clipboard format identifier for ITEMIDLISTS. */
205 static UINT cfShellIDList = 0;
207 /******************************************************************************
208 * UNIXFS_filename_from_shitemid [Internal]
210 * Get CP_UNIXCP encoded filename corresponding to the first item of a pidl
212 * PARAMS
213 * pidl [I] A simple SHITEMID
214 * pszPathElement [O] Filename in CP_UNIXCP encoding will be stored here
216 * RETURNS
217 * Success: Number of bytes necessary to store the CP_UNIXCP encoded filename
218 * _without_ the terminating NUL.
219 * Failure: 0
221 * NOTES
222 * Size of the buffer at pszPathElement has to be FILENAME_MAX. pszPathElement
223 * may be NULL, if you are only interested in the return value.
225 static int UNIXFS_filename_from_shitemid(LPCITEMIDLIST pidl, char* pszPathElement) {
226 FileStructW *pFileStructW = _ILGetFileStructW(pidl);
227 int cLen = 0;
229 if (pFileStructW) {
230 cLen = WideCharToMultiByte(CP_UNIXCP, 0, pFileStructW->wszName, -1, pszPathElement,
231 pszPathElement ? FILENAME_MAX : 0, 0, 0);
232 } else {
233 /* There might be pidls slipping in from shfldr_fs.c, which don't contain the
234 * FileStructW field. In this case, we have to convert from CP_ACP to CP_UNIXCP. */
235 char *pszText = _ILGetTextPointer(pidl);
236 WCHAR *pwszPathElement = NULL;
237 int cWideChars;
239 cWideChars = MultiByteToWideChar(CP_ACP, 0, pszText, -1, NULL, 0);
240 if (!cWideChars) goto cleanup;
242 pwszPathElement = SHAlloc(cWideChars * sizeof(WCHAR));
243 if (!pwszPathElement) goto cleanup;
245 cWideChars = MultiByteToWideChar(CP_ACP, 0, pszText, -1, pwszPathElement, cWideChars);
246 if (!cWideChars) goto cleanup;
248 cLen = WideCharToMultiByte(CP_UNIXCP, 0, pwszPathElement, -1, pszPathElement,
249 pszPathElement ? FILENAME_MAX : 0, 0, 0);
251 cleanup:
252 SHFree(pwszPathElement);
255 if (cLen) cLen--; /* Don't count terminating NUL! */
256 return cLen;
259 /******************************************************************************
260 * UNIXFS_shitemid_len_from_filename [Internal]
262 * Computes the necessary length of a pidl to hold a path element
264 * PARAMS
265 * szPathElement [I] The path element string in CP_UNIXCP encoding.
266 * ppszPathElement [O] Path element string in CP_ACP encoding.
267 * ppwszPathElement [O] Path element string as WCHAR string.
269 * RETURNS
270 * Success: Length in bytes of a SHITEMID representing szPathElement
271 * Failure: 0
273 * NOTES
274 * Provide NULL values if not interested in pp(w)szPathElement. Otherwise
275 * caller is responsible to free ppszPathElement and ppwszPathElement with
276 * SHFree.
278 static USHORT UNIXFS_shitemid_len_from_filename(
279 const char *szPathElement, char **ppszPathElement, WCHAR **ppwszPathElement)
281 USHORT cbPidlLen = 0;
282 WCHAR *pwszPathElement = NULL;
283 char *pszPathElement = NULL;
284 int cWideChars, cChars;
286 /* There and Back Again: A Hobbit's Holiday. CP_UNIXCP might be some ANSI
287 * codepage or it might be a real multi-byte encoding like utf-8. There is no
288 * other way to figure out the length of the corresponding WCHAR and CP_ACP
289 * strings without actually doing the full CP_UNIXCP -> WCHAR -> CP_ACP cycle. */
291 cWideChars = MultiByteToWideChar(CP_UNIXCP, 0, szPathElement, -1, NULL, 0);
292 if (!cWideChars) goto cleanup;
294 pwszPathElement = SHAlloc(cWideChars * sizeof(WCHAR));
295 if (!pwszPathElement) goto cleanup;
297 cWideChars = MultiByteToWideChar(CP_UNIXCP, 0, szPathElement, -1, pwszPathElement, cWideChars);
298 if (!cWideChars) goto cleanup;
300 cChars = WideCharToMultiByte(CP_ACP, 0, pwszPathElement, -1, NULL, 0, 0, 0);
301 if (!cChars) goto cleanup;
303 pszPathElement = SHAlloc(cChars);
304 if (!pszPathElement) goto cleanup;
306 cChars = WideCharToMultiByte(CP_ACP, 0, pwszPathElement, -1, pszPathElement, cChars, 0, 0);
307 if (!cChars) goto cleanup;
309 /* (cChars & 0x1) is for the potential alignment byte */
310 cbPidlLen = LEN_SHITEMID_FIXED_PART + cChars + (cChars & 0x1) + cWideChars * sizeof(WCHAR);
312 cleanup:
313 if (cbPidlLen && ppszPathElement)
314 *ppszPathElement = pszPathElement;
315 else
316 SHFree(pszPathElement);
318 if (cbPidlLen && ppwszPathElement)
319 *ppwszPathElement = pwszPathElement;
320 else
321 SHFree(pwszPathElement);
323 return cbPidlLen;
326 /******************************************************************************
327 * UNIXFS_is_pidl_of_type [Internal]
329 * Checks for the first SHITEMID of an ITEMIDLIST if it passes a filter.
331 * PARAMS
332 * pIDL [I] The ITEMIDLIST to be checked.
333 * fFilter [I] Shell condition flags, which specify the filter.
335 * RETURNS
336 * TRUE, if pIDL is accepted by fFilter
337 * FALSE, otherwise
339 static inline BOOL UNIXFS_is_pidl_of_type(LPCITEMIDLIST pIDL, SHCONTF fFilter) {
340 const PIDLDATA *pIDLData = _ILGetDataPointer(pIDL);
341 if (!(fFilter & SHCONTF_INCLUDEHIDDEN) && pIDLData &&
342 (pIDLData->u.file.uFileAttribs & FILE_ATTRIBUTE_HIDDEN))
344 return FALSE;
346 if (_ILIsFolder(pIDL) && (fFilter & SHCONTF_FOLDERS)) return TRUE;
347 if (_ILIsValue(pIDL) && (fFilter & SHCONTF_NONFOLDERS)) return TRUE;
348 return FALSE;
351 /******************************************************************************
352 * UNIXFS_get_unix_path [Internal]
354 * Convert an absolute dos path to an absolute unix path.
355 * Evaluate "/.", "/.." and the symbolic links in $WINEPREFIX/dosdevices.
357 * PARAMS
358 * pszDosPath [I] An absolute dos path
359 * pszCanonicalPath [O] Buffer of length FILENAME_MAX. Will receive the canonical path.
361 * RETURNS
362 * Success, TRUE
363 * Failure, FALSE - Path not existent, too long, insufficient rights, to many symlinks
365 static BOOL UNIXFS_get_unix_path(LPCWSTR pszDosPath, char *pszCanonicalPath)
367 char *pPathTail, *pElement, *pCanonicalTail, szPath[FILENAME_MAX], *pszUnixPath;
368 WCHAR wszDrive[] = { '?', ':', '\\', 0 };
369 int cDriveSymlinkLen;
371 TRACE("(pszDosPath=%s, pszCanonicalPath=%p)\n", debugstr_w(pszDosPath), pszCanonicalPath);
373 if (!pszDosPath || pszDosPath[1] != ':')
374 return FALSE;
376 /* Get the canonicalized unix path corresponding to the drive letter. */
377 wszDrive[0] = pszDosPath[0];
378 pszUnixPath = wine_get_unix_file_name(wszDrive);
379 if (!pszUnixPath) return FALSE;
380 cDriveSymlinkLen = strlen(pszUnixPath);
381 pElement = realpath(pszUnixPath, szPath);
382 HeapFree(GetProcessHeap(), 0, pszUnixPath);
383 if (!pElement) return FALSE;
384 if (szPath[strlen(szPath)-1] != '/') strcat(szPath, "/");
386 /* Append the part relative to the drive symbolic link target. */
387 pszUnixPath = wine_get_unix_file_name(pszDosPath);
388 if (!pszUnixPath) return FALSE;
389 strcat(szPath, pszUnixPath + cDriveSymlinkLen);
390 HeapFree(GetProcessHeap(), 0, pszUnixPath);
392 /* pCanonicalTail always points to the end of the canonical path constructed
393 * thus far. pPathTail points to the still to be processed part of the input
394 * path. pElement points to the path element currently investigated.
396 *pszCanonicalPath = '\0';
397 pCanonicalTail = pszCanonicalPath;
398 pPathTail = szPath;
400 do {
401 char cTemp;
403 pElement = pPathTail;
404 pPathTail = strchr(pPathTail+1, '/');
405 if (!pPathTail) /* Last path element may not be terminated by '/'. */
406 pPathTail = pElement + strlen(pElement);
407 /* Temporarily terminate the current path element. Will be restored later. */
408 cTemp = *pPathTail;
409 *pPathTail = '\0';
411 /* Skip "/." path elements */
412 if (!strcmp("/.", pElement)) {
413 *pPathTail = cTemp;
414 } else if (!strcmp("/..", pElement)) {
415 /* Remove last element in canonical path for "/.." elements, then skip. */
416 char *pTemp = strrchr(pszCanonicalPath, '/');
417 if (pTemp)
418 pCanonicalTail = pTemp;
419 *pCanonicalTail = '\0';
420 *pPathTail = cTemp;
421 } else {
422 /* Directory or file. Copy to canonical path */
423 if (pCanonicalTail - pszCanonicalPath + pPathTail - pElement + 1 > FILENAME_MAX)
424 return FALSE;
426 memcpy(pCanonicalTail, pElement, pPathTail - pElement + 1);
427 pCanonicalTail += pPathTail - pElement;
428 *pPathTail = cTemp;
430 } while (pPathTail[0] == '/');
432 TRACE("--> %s\n", debugstr_a(pszCanonicalPath));
434 return TRUE;
437 /******************************************************************************
438 * UNIXFS_seconds_since_1970_to_dos_date_time [Internal]
440 * Convert unix time to FAT time
442 * PARAMS
443 * ss1970 [I] Unix time (seconds since 1970)
444 * pDate [O] Corresponding FAT date
445 * pTime [O] Corresponding FAT time
447 static inline void UNIXFS_seconds_since_1970_to_dos_date_time(
448 time_t ss1970, LPWORD pDate, LPWORD pTime)
450 LARGE_INTEGER time;
451 FILETIME fileTime;
453 RtlSecondsSince1970ToTime( ss1970, &time );
454 fileTime.dwLowDateTime = time.u.LowPart;
455 fileTime.dwHighDateTime = time.u.HighPart;
456 FileTimeToDosDateTime(&fileTime, pDate, pTime);
459 /******************************************************************************
460 * UNIXFS_build_shitemid [Internal]
462 * Constructs a new SHITEMID for the last component of path 'pszUnixPath' into
463 * buffer 'pIDL'.
465 * PARAMS
466 * pszUnixPath [I] An absolute path. The SHITEMID will be built for the last component.
467 * pIDL [O] SHITEMID will be constructed here.
469 * RETURNS
470 * Success: A pointer to the terminating '\0' character of path.
471 * Failure: NULL
473 * NOTES
474 * Minimum size of pIDL is SHITEMID_LEN_FROM_NAME_LEN(strlen(last_component_of_path)).
475 * If what you need is a PIDLLIST with a single SHITEMID, don't forget to append
476 * a 0 USHORT value.
478 static char* UNIXFS_build_shitemid(char *pszUnixPath, void *pIDL) {
479 LPPIDLDATA pIDLData;
480 struct stat fileStat;
481 char *pszComponentU, *pszComponentA;
482 WCHAR *pwszComponentW;
483 int cComponentULen, cComponentALen;
484 USHORT cbLen;
485 FileStructW *pFileStructW;
486 WORD uOffsetW, *pOffsetW;
488 TRACE("(pszUnixPath=%s, pIDL=%p)\n", debugstr_a(pszUnixPath), pIDL);
490 /* We are only interested in regular files and directories. */
491 if (stat(pszUnixPath, &fileStat)) return NULL;
492 if (!S_ISDIR(fileStat.st_mode) && !S_ISREG(fileStat.st_mode)) return NULL;
494 /* Compute the SHITEMID's length and wipe it. */
495 pszComponentU = strrchr(pszUnixPath, '/') + 1;
496 cComponentULen = strlen(pszComponentU);
497 cbLen = UNIXFS_shitemid_len_from_filename(pszComponentU, &pszComponentA, &pwszComponentW);
498 if (!cbLen) return NULL;
499 memset(pIDL, 0, cbLen);
500 ((LPSHITEMID)pIDL)->cb = cbLen;
502 /* Set shell32's standard SHITEMID data fields. */
503 pIDLData = _ILGetDataPointer(pIDL);
504 pIDLData->type = S_ISDIR(fileStat.st_mode) ? PT_FOLDER : PT_VALUE;
505 pIDLData->u.file.dwFileSize = (DWORD)fileStat.st_size;
506 UNIXFS_seconds_since_1970_to_dos_date_time(fileStat.st_mtime, &pIDLData->u.file.uFileDate,
507 &pIDLData->u.file.uFileTime);
508 pIDLData->u.file.uFileAttribs = 0;
509 if (S_ISDIR(fileStat.st_mode)) pIDLData->u.file.uFileAttribs |= FILE_ATTRIBUTE_DIRECTORY;
510 if (pszComponentU[0] == '.') pIDLData->u.file.uFileAttribs |= FILE_ATTRIBUTE_HIDDEN;
511 cComponentALen = lstrlenA(pszComponentA) + 1;
512 memcpy(pIDLData->u.file.szNames, pszComponentA, cComponentALen);
514 pFileStructW = (FileStructW*)(pIDLData->u.file.szNames + cComponentALen + (cComponentALen & 0x1));
515 uOffsetW = (WORD)(((LPBYTE)pFileStructW) - ((LPBYTE)pIDL));
516 pFileStructW->cbLen = cbLen - uOffsetW;
517 UNIXFS_seconds_since_1970_to_dos_date_time(fileStat.st_mtime, &pFileStructW->uCreationDate,
518 &pFileStructW->uCreationTime);
519 UNIXFS_seconds_since_1970_to_dos_date_time(fileStat.st_atime, &pFileStructW->uLastAccessDate,
520 &pFileStructW->uLastAccessTime);
521 lstrcpyW(pFileStructW->wszName, pwszComponentW);
523 pOffsetW = (WORD*)(((LPBYTE)pIDL) + cbLen - sizeof(WORD));
524 *pOffsetW = uOffsetW;
526 SHFree(pszComponentA);
527 SHFree(pwszComponentW);
529 return pszComponentU + cComponentULen;
532 /******************************************************************************
533 * UNIXFS_path_to_pidl [Internal]
535 * PARAMS
536 * pUnixFolder [I] If path is relative, pUnixFolder represents the base path
537 * path [I] An absolute unix or dos path or a path relative to pUnixFolder
538 * ppidl [O] The corresponding ITEMIDLIST. Release with SHFree/ILFree
540 * RETURNS
541 * Success: S_OK
542 * Failure: Error code, invalid params or out of memory
544 * NOTES
545 * pUnixFolder also carries the information if the path is expected to be unix or dos.
547 static HRESULT UNIXFS_path_to_pidl(UnixFolder *pUnixFolder, const WCHAR *path, LPITEMIDLIST *ppidl) {
548 LPITEMIDLIST pidl;
549 int cPidlLen, cPathLen;
550 char *pSlash, *pNextSlash, szCompletePath[FILENAME_MAX], *pNextPathElement, *pszAPath;
551 WCHAR *pwszPath;
553 TRACE("pUnixFolder=%p, path=%s, ppidl=%p\n", pUnixFolder, debugstr_w(path), ppidl);
555 if (!ppidl || !path)
556 return E_INVALIDARG;
558 /* Build an absolute path and let pNextPathElement point to the interesting
559 * relative sub-path. We need the absolute path to call 'stat', but the pidl
560 * will only contain the relative part.
562 if ((pUnixFolder->m_dwPathMode == PATHMODE_DOS) && (path[1] == ':'))
564 /* Absolute dos path. Convert to unix */
565 if (!UNIXFS_get_unix_path(path, szCompletePath))
566 return E_FAIL;
567 pNextPathElement = szCompletePath;
569 else if ((pUnixFolder->m_dwPathMode == PATHMODE_UNIX) && (path[0] == '/'))
571 /* Absolute unix path. Just convert to ANSI. */
572 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath, FILENAME_MAX, NULL, NULL);
573 pNextPathElement = szCompletePath;
575 else
577 /* Relative dos or unix path. Concat with this folder's path */
578 int cBasePathLen = strlen(pUnixFolder->m_pszPath);
579 memcpy(szCompletePath, pUnixFolder->m_pszPath, cBasePathLen);
580 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath + cBasePathLen,
581 FILENAME_MAX - cBasePathLen, NULL, NULL);
582 pNextPathElement = szCompletePath + cBasePathLen - 1;
584 /* If in dos mode, replace '\' with '/' */
585 if (pUnixFolder->m_dwPathMode == PATHMODE_DOS) {
586 char *pBackslash = strchr(pNextPathElement, '\\');
587 while (pBackslash) {
588 *pBackslash = '/';
589 pBackslash = strchr(pBackslash, '\\');
594 /* Special case for the root folder. */
595 if (!strcmp(szCompletePath, "/")) {
596 *ppidl = pidl = SHAlloc(sizeof(USHORT));
597 if (!pidl) return E_FAIL;
598 pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
599 return S_OK;
602 /* Remove trailing slash, if present */
603 cPathLen = strlen(szCompletePath);
604 if (szCompletePath[cPathLen-1] == '/')
605 szCompletePath[cPathLen-1] = '\0';
607 if ((szCompletePath[0] != '/') || (pNextPathElement[0] != '/')) {
608 ERR("szCompletePath: %s, pNextPathElment: %s\n", szCompletePath, pNextPathElement);
609 return E_FAIL;
612 /* At this point, we have an absolute unix path in szCompletePath
613 * and the relative portion of it in pNextPathElement. Both starting with '/'
614 * and _not_ terminated by a '/'. */
615 TRACE("complete path: %s, relative path: %s\n", szCompletePath, pNextPathElement);
617 /* Convert to CP_ACP and WCHAR */
618 if (!UNIXFS_shitemid_len_from_filename(pNextPathElement, &pszAPath, &pwszPath))
619 return E_FAIL;
621 /* Compute the length of the complete ITEMIDLIST */
622 cPidlLen = 0;
623 pSlash = pszAPath;
624 while (pSlash) {
625 pNextSlash = strchr(pSlash+1, '/');
626 cPidlLen += LEN_SHITEMID_FIXED_PART + /* Fixed part length plus potential alignment byte. */
627 (pNextSlash ? (pNextSlash - pSlash) & 0x1 : lstrlenA(pSlash) & 0x1);
628 pSlash = pNextSlash;
631 /* The USHORT is for the ITEMIDLIST terminator. The NUL terminators for the sub-path-strings
632 * are accounted for by the '/' separators, which are not stored in the SHITEMIDs. Above we
633 * have ensured that the number of '/'s exactly matches the number of sub-path-strings. */
634 cPidlLen += lstrlenA(pszAPath) + lstrlenW(pwszPath) * sizeof(WCHAR) + sizeof(USHORT);
636 SHFree(pszAPath);
637 SHFree(pwszPath);
639 *ppidl = pidl = SHAlloc(cPidlLen);
640 if (!pidl) return E_FAIL;
642 /* Concatenate the SHITEMIDs of the sub-directories. */
643 while (*pNextPathElement) {
644 pSlash = strchr(pNextPathElement+1, '/');
645 if (pSlash) *pSlash = '\0';
646 pNextPathElement = UNIXFS_build_shitemid(szCompletePath, pidl);
647 if (pSlash) *pSlash = '/';
649 if (!pNextPathElement) {
650 SHFree(*ppidl);
651 *ppidl = NULL;
652 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
654 pidl = ILGetNext(pidl);
656 pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
658 if ((char *)pidl-(char *)*ppidl+sizeof(USHORT) != cPidlLen) /* We've corrupted the heap :( */
659 ERR("Computed length of pidl incorrect. Please report.\n");
661 return S_OK;
664 /******************************************************************************
665 * UNIXFS_initialize_target_folder [Internal]
667 * Initialize the m_pszPath member of an UnixFolder, given an absolute unix
668 * base path and a relative ITEMIDLIST. Leave the m_pidlLocation member, which
669 * specifies the location in the shell namespace alone.
671 * PARAMS
672 * This [IO] The UnixFolder, whose target path is to be initialized
673 * szBasePath [I] The absolute base path
674 * pidlSubFolder [I] Relative part of the path, given as an ITEMIDLIST
675 * dwAttributes [I] Attributes to add to the Folders m_dwAttributes member
676 * (Used to pass the SFGAO_FILESYSTEM flag down the path)
677 * RETURNS
678 * Success: S_OK,
679 * Failure: E_FAIL
681 static HRESULT UNIXFS_initialize_target_folder(UnixFolder *This, const char *szBasePath,
682 LPCITEMIDLIST pidlSubFolder, DWORD dwAttributes)
684 LPCITEMIDLIST current = pidlSubFolder;
685 DWORD dwPathLen = strlen(szBasePath)+1;
686 char *pNextDir;
687 WCHAR *dos_name;
689 /* Determine the path's length bytes */
690 while (current && current->mkid.cb) {
691 dwPathLen += UNIXFS_filename_from_shitemid(current, NULL) + 1; /* For the '/' */
692 current = ILGetNext(current);
695 /* Build the path and compute the attributes*/
696 This->m_dwAttributes =
697 dwAttributes|SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME;
698 This->m_pszPath = pNextDir = SHAlloc(dwPathLen);
699 if (!This->m_pszPath) {
700 WARN("SHAlloc failed!\n");
701 return E_FAIL;
703 current = pidlSubFolder;
704 strcpy(pNextDir, szBasePath);
705 pNextDir += strlen(szBasePath);
706 if (This->m_dwPathMode == PATHMODE_UNIX || IsEqualCLSID(&CLSID_MyDocuments, This->m_pCLSID))
707 This->m_dwAttributes |= SFGAO_FILESYSTEM;
708 while (current && current->mkid.cb) {
709 pNextDir += UNIXFS_filename_from_shitemid(current, pNextDir);
710 *pNextDir++ = '/';
711 current = ILGetNext(current);
713 *pNextDir='\0';
715 if (!(This->m_dwAttributes & SFGAO_FILESYSTEM) &&
716 ((dos_name = wine_get_dos_file_name(This->m_pszPath))))
718 This->m_dwAttributes |= SFGAO_FILESYSTEM;
719 HeapFree( GetProcessHeap(), 0, dos_name );
722 return S_OK;
725 /******************************************************************************
726 * UNIXFS_copy [Internal]
728 * Copy pwszDosSrc to pwszDosDst.
730 * PARAMS
731 * pwszDosSrc [I] absolute path of the source
732 * pwszDosDst [I] absolute path of the destination
734 * RETURNS
735 * Success: S_OK,
736 * Failure: E_FAIL
738 static HRESULT UNIXFS_copy(LPCWSTR pwszDosSrc, LPCWSTR pwszDosDst)
740 SHFILEOPSTRUCTW op;
741 LPWSTR pwszSrc, pwszDst;
742 HRESULT res = E_OUTOFMEMORY;
743 UINT iSrcLen, iDstLen;
745 if (!pwszDosSrc || !pwszDosDst)
746 return E_FAIL;
748 iSrcLen = lstrlenW(pwszDosSrc);
749 iDstLen = lstrlenW(pwszDosDst);
750 pwszSrc = HeapAlloc(GetProcessHeap(), 0, (iSrcLen + 2) * sizeof(WCHAR));
751 pwszDst = HeapAlloc(GetProcessHeap(), 0, (iDstLen + 2) * sizeof(WCHAR));
753 if (pwszSrc && pwszDst) {
754 lstrcpyW(pwszSrc, pwszDosSrc);
755 lstrcpyW(pwszDst, pwszDosDst);
756 /* double null termination */
757 pwszSrc[iSrcLen + 1] = 0;
758 pwszDst[iDstLen + 1] = 0;
760 ZeroMemory(&op, sizeof(op));
761 op.hwnd = GetActiveWindow();
762 op.wFunc = FO_COPY;
763 op.pFrom = pwszSrc;
764 op.pTo = pwszDst;
765 op.fFlags = FOF_ALLOWUNDO;
766 if (!SHFileOperationW(&op))
768 WARN("SHFileOperationW failed\n");
769 res = E_FAIL;
771 else
772 res = S_OK;
775 HeapFree(GetProcessHeap(), 0, pwszSrc);
776 HeapFree(GetProcessHeap(), 0, pwszDst);
777 return res;
780 /******************************************************************************
781 * UnixFolder
783 * Class whose heap based instances represent unix filesystem directories.
786 static void UnixFolder_Destroy(UnixFolder *pUnixFolder) {
787 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
789 SHFree(pUnixFolder->m_pszPath);
790 ILFree(pUnixFolder->m_pidlLocation);
791 SHFree(pUnixFolder);
794 static HRESULT WINAPI UnixFolder_IShellFolder2_QueryInterface(IShellFolder2 *iface, REFIID riid,
795 void **ppv)
797 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
799 TRACE("(iface=%p, riid=%s, ppv=%p)\n", iface, shdebugstr_guid(riid), ppv);
801 if (!ppv) return E_INVALIDARG;
803 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IShellFolder, riid) ||
804 IsEqualIID(&IID_IShellFolder2, riid))
806 *ppv = STATIC_CAST(IShellFolder2, This);
807 } else if (IsEqualIID(&IID_IPersistFolder3, riid) || IsEqualIID(&IID_IPersistFolder2, riid) ||
808 IsEqualIID(&IID_IPersistFolder, riid) || IsEqualIID(&IID_IPersist, riid))
810 *ppv = STATIC_CAST(IPersistFolder3, This);
811 } else if (IsEqualIID(&IID_IPersistPropertyBag, riid)) {
812 *ppv = STATIC_CAST(IPersistPropertyBag, This);
813 } else if (IsEqualIID(&IID_ISFHelper, riid)) {
814 *ppv = STATIC_CAST(ISFHelper, This);
815 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
816 *ppv = STATIC_CAST(IDropTarget, This);
817 if (!cfShellIDList)
818 cfShellIDList = RegisterClipboardFormatW(CFSTR_SHELLIDLISTW);
819 } else {
820 *ppv = NULL;
821 FIXME("Unimplemented interface %s\n", shdebugstr_guid(riid));
822 return E_NOINTERFACE;
825 IUnknown_AddRef((IUnknown*)*ppv);
826 return S_OK;
829 static ULONG WINAPI UnixFolder_IShellFolder2_AddRef(IShellFolder2 *iface) {
830 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
832 TRACE("(iface=%p)\n", iface);
834 return InterlockedIncrement(&This->m_cRef);
837 static ULONG WINAPI UnixFolder_IShellFolder2_Release(IShellFolder2 *iface) {
838 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
839 ULONG cRef;
841 TRACE("(iface=%p)\n", iface);
843 cRef = InterlockedDecrement(&This->m_cRef);
845 if (!cRef)
846 UnixFolder_Destroy(This);
848 return cRef;
851 static HRESULT WINAPI UnixFolder_IShellFolder2_ParseDisplayName(IShellFolder2* iface, HWND hwndOwner,
852 LPBC pbcReserved, LPOLESTR lpszDisplayName, ULONG* pchEaten, LPITEMIDLIST* ppidl,
853 ULONG* pdwAttributes)
855 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
856 HRESULT result;
858 TRACE("(iface=%p, hwndOwner=%p, pbcReserved=%p, lpszDisplayName=%s, pchEaten=%p, ppidl=%p, "
859 "pdwAttributes=%p) stub\n", iface, hwndOwner, pbcReserved, debugstr_w(lpszDisplayName),
860 pchEaten, ppidl, pdwAttributes);
862 result = UNIXFS_path_to_pidl(This, lpszDisplayName, ppidl);
863 if (SUCCEEDED(result) && pdwAttributes && *pdwAttributes)
865 IShellFolder *pParentSF;
866 LPCITEMIDLIST pidlLast;
867 LPITEMIDLIST pidlComplete = ILCombine(This->m_pidlLocation, *ppidl);
868 HRESULT hr;
870 hr = SHBindToParent(pidlComplete, &IID_IShellFolder, (LPVOID*)&pParentSF, &pidlLast);
871 if (FAILED(hr)) {
872 FIXME("SHBindToParent failed! hr = %08x\n", hr);
873 ILFree(pidlComplete);
874 return E_FAIL;
876 IShellFolder_GetAttributesOf(pParentSF, 1, &pidlLast, pdwAttributes);
877 IShellFolder_Release(pParentSF);
878 ILFree(pidlComplete);
881 if (FAILED(result)) TRACE("FAILED!\n");
882 return result;
885 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter);
887 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumObjects(IShellFolder2* iface, HWND hwndOwner,
888 SHCONTF grfFlags, IEnumIDList** ppEnumIDList)
890 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
891 IUnknown *newIterator;
892 HRESULT hr;
894 TRACE("(iface=%p, hwndOwner=%p, grfFlags=%08x, ppEnumIDList=%p)\n",
895 iface, hwndOwner, grfFlags, ppEnumIDList);
897 if (!This->m_pszPath) {
898 WARN("EnumObjects called on uninitialized UnixFolder-object!\n");
899 return E_UNEXPECTED;
902 newIterator = UnixSubFolderIterator_Constructor(This, grfFlags);
903 hr = IUnknown_QueryInterface(newIterator, &IID_IEnumIDList, (void**)ppEnumIDList);
904 IUnknown_Release(newIterator);
906 return hr;
909 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, const CLSID *pCLSID);
911 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToObject(IShellFolder2* iface, LPCITEMIDLIST pidl,
912 LPBC pbcReserved, REFIID riid, void** ppvOut)
914 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
915 IPersistFolder3 *persistFolder;
916 HRESULT hr;
917 const CLSID *clsidChild;
919 TRACE("(iface=%p, pidl=%p, pbcReserver=%p, riid=%p, ppvOut=%p)\n",
920 iface, pidl, pbcReserved, riid, ppvOut);
922 if (!pidl || !pidl->mkid.cb)
923 return E_INVALIDARG;
925 if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
926 /* Children of FolderShortcuts are ShellFSFolders on Windows.
927 * Unixfs' counterpart is UnixDosFolder. */
928 clsidChild = &CLSID_UnixDosFolder;
929 } else {
930 clsidChild = This->m_pCLSID;
933 hr = CreateUnixFolder(NULL, &IID_IPersistFolder3, (void**)&persistFolder, clsidChild);
934 if (FAILED(hr)) return hr;
935 hr = IPersistFolder_QueryInterface(persistFolder, riid, ppvOut);
937 if (SUCCEEDED(hr)) {
938 UnixFolder *subfolder = ADJUST_THIS(UnixFolder, IPersistFolder3, persistFolder);
939 subfolder->m_pidlLocation = ILCombine(This->m_pidlLocation, pidl);
940 hr = UNIXFS_initialize_target_folder(subfolder, This->m_pszPath, pidl,
941 This->m_dwAttributes & SFGAO_FILESYSTEM);
944 IPersistFolder3_Release(persistFolder);
946 return hr;
949 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToStorage(IShellFolder2* This, LPCITEMIDLIST pidl,
950 LPBC pbcReserved, REFIID riid, void** ppvObj)
952 FIXME("stub\n");
953 return E_NOTIMPL;
956 static HRESULT WINAPI UnixFolder_IShellFolder2_CompareIDs(IShellFolder2* iface, LPARAM lParam,
957 LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
959 BOOL isEmpty1, isEmpty2;
960 HRESULT hr = E_FAIL;
961 LPITEMIDLIST firstpidl;
962 IShellFolder2 *psf;
963 int compare;
965 TRACE("(iface=%p, lParam=%ld, pidl1=%p, pidl2=%p)\n", iface, lParam, pidl1, pidl2);
967 isEmpty1 = !pidl1 || !pidl1->mkid.cb;
968 isEmpty2 = !pidl2 || !pidl2->mkid.cb;
970 if (isEmpty1 && isEmpty2)
971 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
972 else if (isEmpty1)
973 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
974 else if (isEmpty2)
975 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
977 if (_ILIsFolder(pidl1) && !_ILIsFolder(pidl2))
978 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
979 if (!_ILIsFolder(pidl1) && _ILIsFolder(pidl2))
980 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
982 compare = CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
983 _ILGetTextPointer(pidl1), -1,
984 _ILGetTextPointer(pidl2), -1);
986 if ((compare == CSTR_LESS_THAN) || (compare == CSTR_GREATER_THAN))
987 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)((compare == CSTR_LESS_THAN)?-1:1));
989 if (pidl1->mkid.cb < pidl2->mkid.cb)
990 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
991 else if (pidl1->mkid.cb > pidl2->mkid.cb)
992 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
994 firstpidl = ILCloneFirst(pidl1);
995 pidl1 = ILGetNext(pidl1);
996 pidl2 = ILGetNext(pidl2);
998 hr = IShellFolder2_BindToObject(iface, firstpidl, NULL, &IID_IShellFolder, (LPVOID*)&psf);
999 if (SUCCEEDED(hr)) {
1000 hr = IShellFolder_CompareIDs(psf, lParam, pidl1, pidl2);
1001 IShellFolder2_Release(psf);
1004 ILFree(firstpidl);
1005 return hr;
1008 static HRESULT WINAPI UnixFolder_IShellFolder2_CreateViewObject(IShellFolder2* iface, HWND hwndOwner,
1009 REFIID riid, void** ppv)
1011 HRESULT hr = E_INVALIDARG;
1013 TRACE("(iface=%p, hwndOwner=%p, riid=%p, ppv=%p) stub\n", iface, hwndOwner, riid, ppv);
1015 if (!ppv) return E_INVALIDARG;
1016 *ppv = NULL;
1018 if (IsEqualIID(&IID_IShellView, riid)) {
1019 LPSHELLVIEW pShellView;
1021 pShellView = IShellView_Constructor((IShellFolder*)iface);
1022 if (pShellView) {
1023 hr = IShellView_QueryInterface(pShellView, riid, ppv);
1024 IShellView_Release(pShellView);
1026 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
1027 hr = IShellFolder2_QueryInterface(iface, &IID_IDropTarget, ppv);
1030 return hr;
1033 static HRESULT WINAPI UnixFolder_IShellFolder2_GetAttributesOf(IShellFolder2* iface, UINT cidl,
1034 LPCITEMIDLIST* apidl, SFGAOF* rgfInOut)
1036 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1037 HRESULT hr = S_OK;
1039 TRACE("(iface=%p, cidl=%u, apidl=%p, rgfInOut=%p)\n", iface, cidl, apidl, rgfInOut);
1041 if (!rgfInOut || (cidl && !apidl))
1042 return E_INVALIDARG;
1044 if (cidl == 0) {
1045 *rgfInOut &= This->m_dwAttributes;
1046 } else {
1047 char szAbsolutePath[FILENAME_MAX], *pszRelativePath;
1048 UINT i;
1050 *rgfInOut = SFGAO_CANCOPY|SFGAO_CANMOVE|SFGAO_CANLINK|SFGAO_CANRENAME|SFGAO_CANDELETE|
1051 SFGAO_HASPROPSHEET|SFGAO_DROPTARGET|SFGAO_FILESYSTEM;
1052 lstrcpyA(szAbsolutePath, This->m_pszPath);
1053 pszRelativePath = szAbsolutePath + lstrlenA(szAbsolutePath);
1054 for (i=0; i<cidl; i++) {
1055 if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
1056 WCHAR *dos_name;
1057 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelativePath))
1058 return E_INVALIDARG;
1059 if (!(dos_name = wine_get_dos_file_name( szAbsolutePath )))
1060 *rgfInOut &= ~SFGAO_FILESYSTEM;
1061 else
1062 HeapFree( GetProcessHeap(), 0, dos_name );
1064 if (_ILIsFolder(apidl[i]))
1065 *rgfInOut |= SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR;
1069 return hr;
1072 static HRESULT WINAPI UnixFolder_IShellFolder2_GetUIObjectOf(IShellFolder2* iface, HWND hwndOwner,
1073 UINT cidl, LPCITEMIDLIST* apidl, REFIID riid, UINT* prgfInOut, void** ppvOut)
1075 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1076 UINT i;
1078 TRACE("(iface=%p, hwndOwner=%p, cidl=%d, apidl=%p, riid=%s, prgfInOut=%p, ppv=%p)\n",
1079 iface, hwndOwner, cidl, apidl, debugstr_guid(riid), prgfInOut, ppvOut);
1081 if (!cidl || !apidl || !riid || !ppvOut)
1082 return E_INVALIDARG;
1084 for (i=0; i<cidl; i++)
1085 if (!apidl[i])
1086 return E_INVALIDARG;
1088 if (IsEqualIID(&IID_IContextMenu, riid)) {
1089 *ppvOut = ISvItemCm_Constructor((IShellFolder*)iface, This->m_pidlLocation, apidl, cidl);
1090 return S_OK;
1091 } else if (IsEqualIID(&IID_IDataObject, riid)) {
1092 *ppvOut = IDataObject_Constructor(hwndOwner, This->m_pidlLocation, apidl, cidl);
1093 return S_OK;
1094 } else if (IsEqualIID(&IID_IExtractIconA, riid)) {
1095 LPITEMIDLIST pidl;
1096 if (cidl != 1) return E_INVALIDARG;
1097 pidl = ILCombine(This->m_pidlLocation, apidl[0]);
1098 *ppvOut = IExtractIconA_Constructor(pidl);
1099 SHFree(pidl);
1100 return S_OK;
1101 } else if (IsEqualIID(&IID_IExtractIconW, riid)) {
1102 LPITEMIDLIST pidl;
1103 if (cidl != 1) return E_INVALIDARG;
1104 pidl = ILCombine(This->m_pidlLocation, apidl[0]);
1105 *ppvOut = IExtractIconW_Constructor(pidl);
1106 SHFree(pidl);
1107 return S_OK;
1108 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
1109 if (cidl != 1) return E_INVALIDARG;
1110 return IShellFolder2_BindToObject(iface, apidl[0], NULL, &IID_IDropTarget, ppvOut);
1111 } else if (IsEqualIID(&IID_IShellLinkW, riid)) {
1112 FIXME("IShellLinkW\n");
1113 return E_FAIL;
1114 } else if (IsEqualIID(&IID_IShellLinkA, riid)) {
1115 FIXME("IShellLinkA\n");
1116 return E_FAIL;
1117 } else {
1118 FIXME("Unknown interface %s in GetUIObjectOf\n", debugstr_guid(riid));
1119 return E_NOINTERFACE;
1123 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDisplayNameOf(IShellFolder2* iface,
1124 LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET* lpName)
1126 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1127 HRESULT hr = S_OK;
1129 TRACE("(iface=%p, pidl=%p, uFlags=%x, lpName=%p)\n", iface, pidl, uFlags, lpName);
1131 if ((GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) &&
1132 (GET_SHGDN_RELATION(uFlags) != SHGDN_INFOLDER))
1134 if (!pidl || !pidl->mkid.cb) {
1135 lpName->uType = STRRET_WSTR;
1136 if (This->m_dwPathMode == PATHMODE_UNIX) {
1137 UINT len = MultiByteToWideChar(CP_UNIXCP, 0, This->m_pszPath, -1, NULL, 0);
1138 lpName->u.pOleStr = SHAlloc(len * sizeof(WCHAR));
1139 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1140 MultiByteToWideChar(CP_UNIXCP, 0, This->m_pszPath, -1, lpName->u.pOleStr, len);
1141 } else {
1142 LPWSTR pwszDosFileName = wine_get_dos_file_name(This->m_pszPath);
1143 if (!pwszDosFileName) return HRESULT_FROM_WIN32(GetLastError());
1144 lpName->u.pOleStr = SHAlloc((lstrlenW(pwszDosFileName) + 1) * sizeof(WCHAR));
1145 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1146 lstrcpyW(lpName->u.pOleStr, pwszDosFileName);
1147 PathRemoveBackslashW(lpName->u.pOleStr);
1148 HeapFree(GetProcessHeap(), 0, pwszDosFileName);
1150 } else {
1151 IShellFolder *pSubFolder;
1152 SHITEMID emptyIDL = { 0, { 0 } };
1154 hr = IShellFolder_BindToObject(iface, pidl, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1155 if (FAILED(hr)) return hr;
1157 hr = IShellFolder_GetDisplayNameOf(pSubFolder, (LPITEMIDLIST)&emptyIDL, uFlags, lpName);
1158 IShellFolder_Release(pSubFolder);
1160 } else {
1161 WCHAR wszFileName[MAX_PATH];
1162 if (!_ILSimpleGetTextW(pidl, wszFileName, MAX_PATH)) return E_INVALIDARG;
1163 lpName->uType = STRRET_WSTR;
1164 lpName->u.pOleStr = SHAlloc((lstrlenW(wszFileName)+1)*sizeof(WCHAR));
1165 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1166 lstrcpyW(lpName->u.pOleStr, wszFileName);
1167 if (!(GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) && This->m_dwPathMode == PATHMODE_DOS &&
1168 !_ILIsFolder(pidl) && wszFileName[0] != '.' && SHELL_FS_HideExtension(wszFileName))
1170 PathRemoveExtensionW(lpName->u.pOleStr);
1174 TRACE("--> %s\n", debugstr_w(lpName->u.pOleStr));
1176 return hr;
1179 static HRESULT WINAPI UnixFolder_IShellFolder2_SetNameOf(IShellFolder2* iface, HWND hwnd,
1180 LPCITEMIDLIST pidl, LPCOLESTR lpcwszName, SHGDNF uFlags, LPITEMIDLIST* ppidlOut)
1182 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1184 static const WCHAR awcInvalidChars[] = { '\\', '/', ':', '*', '?', '"', '<', '>', '|' };
1185 char szSrc[FILENAME_MAX], szDest[FILENAME_MAX];
1186 WCHAR wszSrcRelative[MAX_PATH], *pwszExt = NULL;
1187 unsigned int i;
1188 int cBasePathLen = lstrlenA(This->m_pszPath), cNameLen;
1189 struct stat statDest;
1190 LPITEMIDLIST pidlSrc, pidlDest, pidlRelativeDest;
1191 LPOLESTR lpwszName;
1192 HRESULT hr;
1194 TRACE("(iface=%p, hwnd=%p, pidl=%p, lpcwszName=%s, uFlags=0x%08x, ppidlOut=%p)\n",
1195 iface, hwnd, pidl, debugstr_w(lpcwszName), uFlags, ppidlOut);
1197 /* prepare to fail */
1198 if (ppidlOut)
1199 *ppidlOut = NULL;
1201 /* pidl has to contain a single non-empty SHITEMID */
1202 if (_ILIsDesktop(pidl) || !_ILIsPidlSimple(pidl) || !_ILGetTextPointer(pidl))
1203 return E_INVALIDARG;
1205 /* check for invalid characters in lpcwszName. */
1206 for (i=0; i < sizeof(awcInvalidChars)/sizeof(*awcInvalidChars); i++)
1207 if (StrChrW(lpcwszName, awcInvalidChars[i]))
1208 return HRESULT_FROM_WIN32(ERROR_CANCELLED);
1210 /* build source path */
1211 memcpy(szSrc, This->m_pszPath, cBasePathLen);
1212 UNIXFS_filename_from_shitemid(pidl, szSrc + cBasePathLen);
1214 /* build destination path */
1215 memcpy(szDest, This->m_pszPath, cBasePathLen);
1216 WideCharToMultiByte(CP_UNIXCP, 0, lpcwszName, -1, szDest+cBasePathLen,
1217 FILENAME_MAX-cBasePathLen, NULL, NULL);
1219 /* If the filename's extension is hidden to the user, we have to append it. */
1220 if (!(uFlags & SHGDN_FORPARSING) &&
1221 _ILSimpleGetTextW(pidl, wszSrcRelative, MAX_PATH) &&
1222 SHELL_FS_HideExtension(wszSrcRelative))
1224 int cLenDest = strlen(szDest);
1225 pwszExt = PathFindExtensionW(wszSrcRelative);
1226 WideCharToMultiByte(CP_UNIXCP, 0, pwszExt, -1, szDest + cLenDest,
1227 FILENAME_MAX - cLenDest, NULL, NULL);
1230 TRACE("src=%s dest=%s\n", szSrc, szDest);
1232 /* Fail, if destination does already exist */
1233 if (!stat(szDest, &statDest))
1234 return E_FAIL;
1236 /* Rename the file */
1237 if (rename(szSrc, szDest))
1238 return E_FAIL;
1240 /* Build a pidl for the path of the renamed file */
1241 cNameLen = lstrlenW(lpcwszName) + 1;
1242 if(pwszExt)
1243 cNameLen += lstrlenW(pwszExt);
1244 lpwszName = SHAlloc(cNameLen*sizeof(WCHAR)); /* due to const correctness. */
1245 lstrcpyW(lpwszName, lpcwszName);
1246 if(pwszExt)
1247 lstrcatW(lpwszName, pwszExt);
1249 hr = IShellFolder2_ParseDisplayName(iface, NULL, NULL, lpwszName, NULL, &pidlRelativeDest, NULL);
1250 SHFree(lpwszName);
1251 if (FAILED(hr)) {
1252 rename(szDest, szSrc); /* Undo the renaming */
1253 return E_FAIL;
1255 pidlDest = ILCombine(This->m_pidlLocation, pidlRelativeDest);
1256 ILFree(pidlRelativeDest);
1257 pidlSrc = ILCombine(This->m_pidlLocation, pidl);
1259 /* Inform the shell */
1260 if (_ILIsFolder(ILFindLastID(pidlDest)))
1261 SHChangeNotify(SHCNE_RENAMEFOLDER, SHCNF_IDLIST, pidlSrc, pidlDest);
1262 else
1263 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_IDLIST, pidlSrc, pidlDest);
1265 if (ppidlOut)
1266 *ppidlOut = ILClone(ILFindLastID(pidlDest));
1268 ILFree(pidlSrc);
1269 ILFree(pidlDest);
1271 return S_OK;
1274 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumSearches(IShellFolder2* iface,
1275 IEnumExtraSearch **ppEnum)
1277 FIXME("stub\n");
1278 return E_NOTIMPL;
1281 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumn(IShellFolder2* iface,
1282 DWORD dwReserved, ULONG *pSort, ULONG *pDisplay)
1284 TRACE("(iface=%p,dwReserved=%x,pSort=%p,pDisplay=%p)\n", iface, dwReserved, pSort, pDisplay);
1286 if (pSort)
1287 *pSort = 0;
1288 if (pDisplay)
1289 *pDisplay = 0;
1291 return S_OK;
1294 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumnState(IShellFolder2* iface,
1295 UINT iColumn, SHCOLSTATEF *pcsFlags)
1297 FIXME("stub\n");
1298 return E_NOTIMPL;
1301 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultSearchGUID(IShellFolder2* iface,
1302 GUID *pguid)
1304 FIXME("stub\n");
1305 return E_NOTIMPL;
1308 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsEx(IShellFolder2* iface,
1309 LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
1311 FIXME("stub\n");
1312 return E_NOTIMPL;
1315 #define SHELLVIEWCOLUMNS 7
1317 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsOf(IShellFolder2* iface,
1318 LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
1320 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1321 HRESULT hr = E_FAIL;
1322 struct passwd *pPasswd;
1323 struct group *pGroup;
1324 struct stat statItem;
1326 static const shvheader unixfs_header[SHELLVIEWCOLUMNS] = {
1327 {IDS_SHV_COLUMN1, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
1328 {IDS_SHV_COLUMN2, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1329 {IDS_SHV_COLUMN3, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1330 {IDS_SHV_COLUMN4, SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12},
1331 {IDS_SHV_COLUMN5, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 9},
1332 {IDS_SHV_COLUMN10, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
1333 {IDS_SHV_COLUMN11, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7}
1336 TRACE("(iface=%p, pidl=%p, iColumn=%d, psd=%p) stub\n", iface, pidl, iColumn, psd);
1338 if (!psd || iColumn >= SHELLVIEWCOLUMNS)
1339 return E_INVALIDARG;
1341 if (!pidl)
1342 return SHELL32_GetColumnDetails(unixfs_header, iColumn, psd);
1344 if (iColumn == 4 || iColumn == 5 || iColumn == 6) {
1345 char szPath[FILENAME_MAX];
1346 strcpy(szPath, This->m_pszPath);
1347 if (!UNIXFS_filename_from_shitemid(pidl, szPath + strlen(szPath)))
1348 return E_INVALIDARG;
1349 if (stat(szPath, &statItem))
1350 return E_INVALIDARG;
1353 psd->str.u.cStr[0] = '\0';
1354 psd->str.uType = STRRET_CSTR;
1356 switch (iColumn) {
1357 case 0:
1358 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL|SHGDN_INFOLDER, &psd->str);
1359 break;
1360 case 1:
1361 _ILGetFileSize(pidl, psd->str.u.cStr, MAX_PATH);
1362 break;
1363 case 2:
1364 _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
1365 break;
1366 case 3:
1367 _ILGetFileDate(pidl, psd->str.u.cStr, MAX_PATH);
1368 break;
1369 case 4:
1370 psd->str.u.cStr[0] = S_ISDIR(statItem.st_mode) ? 'd' : '-';
1371 psd->str.u.cStr[1] = (statItem.st_mode & S_IRUSR) ? 'r' : '-';
1372 psd->str.u.cStr[2] = (statItem.st_mode & S_IWUSR) ? 'w' : '-';
1373 psd->str.u.cStr[3] = (statItem.st_mode & S_IXUSR) ? 'x' : '-';
1374 psd->str.u.cStr[4] = (statItem.st_mode & S_IRGRP) ? 'r' : '-';
1375 psd->str.u.cStr[5] = (statItem.st_mode & S_IWGRP) ? 'w' : '-';
1376 psd->str.u.cStr[6] = (statItem.st_mode & S_IXGRP) ? 'x' : '-';
1377 psd->str.u.cStr[7] = (statItem.st_mode & S_IROTH) ? 'r' : '-';
1378 psd->str.u.cStr[8] = (statItem.st_mode & S_IWOTH) ? 'w' : '-';
1379 psd->str.u.cStr[9] = (statItem.st_mode & S_IXOTH) ? 'x' : '-';
1380 psd->str.u.cStr[10] = '\0';
1381 break;
1382 case 5:
1383 pPasswd = getpwuid(statItem.st_uid);
1384 if (pPasswd) strcpy(psd->str.u.cStr, pPasswd->pw_name);
1385 break;
1386 case 6:
1387 pGroup = getgrgid(statItem.st_gid);
1388 if (pGroup) strcpy(psd->str.u.cStr, pGroup->gr_name);
1389 break;
1392 return hr;
1395 static HRESULT WINAPI UnixFolder_IShellFolder2_MapColumnToSCID(IShellFolder2* iface, UINT iColumn,
1396 SHCOLUMNID *pscid)
1398 FIXME("stub\n");
1399 return E_NOTIMPL;
1402 /* VTable for UnixFolder's IShellFolder2 interface.
1404 static const IShellFolder2Vtbl UnixFolder_IShellFolder2_Vtbl = {
1405 UnixFolder_IShellFolder2_QueryInterface,
1406 UnixFolder_IShellFolder2_AddRef,
1407 UnixFolder_IShellFolder2_Release,
1408 UnixFolder_IShellFolder2_ParseDisplayName,
1409 UnixFolder_IShellFolder2_EnumObjects,
1410 UnixFolder_IShellFolder2_BindToObject,
1411 UnixFolder_IShellFolder2_BindToStorage,
1412 UnixFolder_IShellFolder2_CompareIDs,
1413 UnixFolder_IShellFolder2_CreateViewObject,
1414 UnixFolder_IShellFolder2_GetAttributesOf,
1415 UnixFolder_IShellFolder2_GetUIObjectOf,
1416 UnixFolder_IShellFolder2_GetDisplayNameOf,
1417 UnixFolder_IShellFolder2_SetNameOf,
1418 UnixFolder_IShellFolder2_GetDefaultSearchGUID,
1419 UnixFolder_IShellFolder2_EnumSearches,
1420 UnixFolder_IShellFolder2_GetDefaultColumn,
1421 UnixFolder_IShellFolder2_GetDefaultColumnState,
1422 UnixFolder_IShellFolder2_GetDetailsEx,
1423 UnixFolder_IShellFolder2_GetDetailsOf,
1424 UnixFolder_IShellFolder2_MapColumnToSCID
1427 static HRESULT WINAPI UnixFolder_IPersistFolder3_QueryInterface(IPersistFolder3* iface, REFIID riid,
1428 void** ppvObject)
1430 return UnixFolder_IShellFolder2_QueryInterface(
1431 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistFolder3, iface)), riid, ppvObject);
1434 static ULONG WINAPI UnixFolder_IPersistFolder3_AddRef(IPersistFolder3* iface)
1436 return UnixFolder_IShellFolder2_AddRef(
1437 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistFolder3, iface)));
1440 static ULONG WINAPI UnixFolder_IPersistFolder3_Release(IPersistFolder3* iface)
1442 return UnixFolder_IShellFolder2_Release(
1443 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistFolder3, iface)));
1446 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetClassID(IPersistFolder3* iface, CLSID* pClassID)
1448 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1450 TRACE("(iface=%p, pClassId=%p)\n", iface, pClassID);
1452 if (!pClassID)
1453 return E_INVALIDARG;
1455 *pClassID = *This->m_pCLSID;
1456 return S_OK;
1459 static HRESULT WINAPI UnixFolder_IPersistFolder3_Initialize(IPersistFolder3* iface, LPCITEMIDLIST pidl)
1461 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1462 LPCITEMIDLIST current = pidl;
1463 char szBasePath[FILENAME_MAX] = "/";
1465 TRACE("(iface=%p, pidl=%p)\n", iface, pidl);
1467 /* Find the UnixFolderClass root */
1468 while (current->mkid.cb) {
1469 if ((_ILIsDrive(current) && IsEqualCLSID(This->m_pCLSID, &CLSID_ShellFSFolder)) ||
1470 (_ILIsSpecialFolder(current) && IsEqualCLSID(This->m_pCLSID, _ILGetGUIDPointer(current))))
1472 break;
1474 current = ILGetNext(current);
1477 if (current->mkid.cb) {
1478 if (_ILIsDrive(current)) {
1479 WCHAR wszDrive[4] = { '?', ':', '\\', 0 };
1480 wszDrive[0] = (WCHAR)*_ILGetTextPointer(current);
1481 if (!UNIXFS_get_unix_path(wszDrive, szBasePath))
1482 return E_FAIL;
1483 } else if (IsEqualIID(&CLSID_MyDocuments, _ILGetGUIDPointer(current))) {
1484 WCHAR wszMyDocumentsPath[MAX_PATH];
1485 if (!SHGetSpecialFolderPathW(0, wszMyDocumentsPath, CSIDL_PERSONAL, FALSE))
1486 return E_FAIL;
1487 PathAddBackslashW(wszMyDocumentsPath);
1488 if (!UNIXFS_get_unix_path(wszMyDocumentsPath, szBasePath))
1489 return E_FAIL;
1491 current = ILGetNext(current);
1492 } else if (_ILIsDesktop(pidl) || _ILIsValue(pidl) || _ILIsFolder(pidl)) {
1493 /* Path rooted at Desktop */
1494 WCHAR wszDesktopPath[MAX_PATH];
1495 if (!SHGetSpecialFolderPathW(0, wszDesktopPath, CSIDL_DESKTOPDIRECTORY, FALSE))
1496 return E_FAIL;
1497 PathAddBackslashW(wszDesktopPath);
1498 if (!UNIXFS_get_unix_path(wszDesktopPath, szBasePath))
1499 return E_FAIL;
1500 current = pidl;
1501 } else if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
1502 /* FolderShortcuts' Initialize method only sets the ITEMIDLIST, which
1503 * specifies the location in the shell namespace, but leaves the
1504 * target folder (m_pszPath) alone. See unit tests in tests/shlfolder.c */
1505 This->m_pidlLocation = ILClone(pidl);
1506 return S_OK;
1507 } else {
1508 ERR("Unknown pidl type!\n");
1509 pdump(pidl);
1510 return E_INVALIDARG;
1513 This->m_pidlLocation = ILClone(pidl);
1514 return UNIXFS_initialize_target_folder(This, szBasePath, current, 0);
1517 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetCurFolder(IPersistFolder3* iface, LPITEMIDLIST* ppidl)
1519 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1521 TRACE ("(iface=%p, ppidl=%p)\n", iface, ppidl);
1523 if (!ppidl)
1524 return E_POINTER;
1525 *ppidl = ILClone (This->m_pidlLocation);
1526 return S_OK;
1529 static HRESULT WINAPI UnixFolder_IPersistFolder3_InitializeEx(IPersistFolder3 *iface, IBindCtx *pbc,
1530 LPCITEMIDLIST pidlRoot, const PERSIST_FOLDER_TARGET_INFO *ppfti)
1532 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1533 WCHAR wszTargetDosPath[MAX_PATH];
1534 char szTargetPath[FILENAME_MAX] = "";
1536 TRACE("(iface=%p, pbc=%p, pidlRoot=%p, ppfti=%p)\n", iface, pbc, pidlRoot, ppfti);
1538 /* If no PERSIST_FOLDER_TARGET_INFO is given InitializeEx is equivalent to Initialize. */
1539 if (!ppfti)
1540 return IPersistFolder3_Initialize(iface, pidlRoot);
1542 if (ppfti->csidl != -1) {
1543 if (FAILED(SHGetFolderPathW(0, ppfti->csidl, NULL, 0, wszTargetDosPath)) ||
1544 !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1546 return E_FAIL;
1548 } else if (*ppfti->szTargetParsingName) {
1549 lstrcpyW(wszTargetDosPath, ppfti->szTargetParsingName);
1550 PathAddBackslashW(wszTargetDosPath);
1551 if (!UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath)) {
1552 return E_FAIL;
1554 } else if (ppfti->pidlTargetFolder) {
1555 if (!SHGetPathFromIDListW(ppfti->pidlTargetFolder, wszTargetDosPath) ||
1556 !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1558 return E_FAIL;
1560 } else {
1561 return E_FAIL;
1564 This->m_pszPath = SHAlloc(lstrlenA(szTargetPath)+1);
1565 if (!This->m_pszPath)
1566 return E_FAIL;
1567 lstrcpyA(This->m_pszPath, szTargetPath);
1568 This->m_pidlLocation = ILClone(pidlRoot);
1569 This->m_dwAttributes = (ppfti->dwAttributes != -1) ? ppfti->dwAttributes :
1570 (SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME|SFGAO_FILESYSTEM);
1572 return S_OK;
1575 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetFolderTargetInfo(IPersistFolder3 *iface,
1576 PERSIST_FOLDER_TARGET_INFO *ppfti)
1578 FIXME("(iface=%p, ppfti=%p) stub\n", iface, ppfti);
1579 return E_NOTIMPL;
1582 /* VTable for UnixFolder's IPersistFolder interface.
1584 static const IPersistFolder3Vtbl UnixFolder_IPersistFolder3_Vtbl = {
1585 UnixFolder_IPersistFolder3_QueryInterface,
1586 UnixFolder_IPersistFolder3_AddRef,
1587 UnixFolder_IPersistFolder3_Release,
1588 UnixFolder_IPersistFolder3_GetClassID,
1589 UnixFolder_IPersistFolder3_Initialize,
1590 UnixFolder_IPersistFolder3_GetCurFolder,
1591 UnixFolder_IPersistFolder3_InitializeEx,
1592 UnixFolder_IPersistFolder3_GetFolderTargetInfo
1595 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_QueryInterface(IPersistPropertyBag* iface,
1596 REFIID riid, void** ppv)
1598 return UnixFolder_IShellFolder2_QueryInterface(
1599 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)), riid, ppv);
1602 static ULONG WINAPI UnixFolder_IPersistPropertyBag_AddRef(IPersistPropertyBag* iface)
1604 return UnixFolder_IShellFolder2_AddRef(
1605 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)));
1608 static ULONG WINAPI UnixFolder_IPersistPropertyBag_Release(IPersistPropertyBag* iface)
1610 return UnixFolder_IShellFolder2_Release(
1611 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)));
1614 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_GetClassID(IPersistPropertyBag* iface,
1615 CLSID* pClassID)
1617 return UnixFolder_IPersistFolder3_GetClassID(
1618 STATIC_CAST(IPersistFolder3, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)), pClassID);
1621 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_InitNew(IPersistPropertyBag* iface)
1623 FIXME("() stub\n");
1624 return E_NOTIMPL;
1627 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_Load(IPersistPropertyBag *iface,
1628 IPropertyBag *pPropertyBag, IErrorLog *pErrorLog)
1630 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface);
1631 static const WCHAR wszTarget[] = { 'T','a','r','g','e','t', 0 }, wszNull[] = { 0 };
1632 PERSIST_FOLDER_TARGET_INFO pftiTarget;
1633 VARIANT var;
1634 HRESULT hr;
1636 TRACE("(iface=%p, pPropertyBag=%p, pErrorLog=%p)\n", iface, pPropertyBag, pErrorLog);
1638 if (!pPropertyBag)
1639 return E_POINTER;
1641 /* Get 'Target' property from the property bag. */
1642 V_VT(&var) = VT_BSTR;
1643 hr = IPropertyBag_Read(pPropertyBag, wszTarget, &var, NULL);
1644 if (FAILED(hr))
1645 return E_FAIL;
1646 lstrcpyW(pftiTarget.szTargetParsingName, V_BSTR(&var));
1647 SysFreeString(V_BSTR(&var));
1649 pftiTarget.pidlTargetFolder = NULL;
1650 lstrcpyW(pftiTarget.szNetworkProvider, wszNull);
1651 pftiTarget.dwAttributes = -1;
1652 pftiTarget.csidl = -1;
1654 return UnixFolder_IPersistFolder3_InitializeEx(
1655 STATIC_CAST(IPersistFolder3, This), NULL, NULL, &pftiTarget);
1658 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_Save(IPersistPropertyBag *iface,
1659 IPropertyBag *pPropertyBag, BOOL fClearDirty, BOOL fSaveAllProperties)
1661 FIXME("() stub\n");
1662 return E_NOTIMPL;
1665 /* VTable for UnixFolder's IPersistPropertyBag interface.
1667 static const IPersistPropertyBagVtbl UnixFolder_IPersistPropertyBag_Vtbl = {
1668 UnixFolder_IPersistPropertyBag_QueryInterface,
1669 UnixFolder_IPersistPropertyBag_AddRef,
1670 UnixFolder_IPersistPropertyBag_Release,
1671 UnixFolder_IPersistPropertyBag_GetClassID,
1672 UnixFolder_IPersistPropertyBag_InitNew,
1673 UnixFolder_IPersistPropertyBag_Load,
1674 UnixFolder_IPersistPropertyBag_Save
1677 static HRESULT WINAPI UnixFolder_ISFHelper_QueryInterface(ISFHelper* iface, REFIID riid,
1678 void** ppvObject)
1680 return UnixFolder_IShellFolder2_QueryInterface(
1681 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, ISFHelper, iface)), riid, ppvObject);
1684 static ULONG WINAPI UnixFolder_ISFHelper_AddRef(ISFHelper* iface)
1686 return UnixFolder_IShellFolder2_AddRef(
1687 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, ISFHelper, iface)));
1690 static ULONG WINAPI UnixFolder_ISFHelper_Release(ISFHelper* iface)
1692 return UnixFolder_IShellFolder2_Release(
1693 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, ISFHelper, iface)));
1696 static HRESULT WINAPI UnixFolder_ISFHelper_GetUniqueName(ISFHelper* iface, LPWSTR pwszName, UINT uLen)
1698 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1699 IEnumIDList *pEnum;
1700 HRESULT hr;
1701 LPITEMIDLIST pidlElem;
1702 DWORD dwFetched;
1703 int i;
1704 WCHAR wszNewFolder[25];
1705 static const WCHAR wszFormat[] = { '%','s',' ','%','d',0 };
1707 TRACE("(iface=%p, pwszName=%p, uLen=%u)\n", iface, pwszName, uLen);
1709 LoadStringW(shell32_hInstance, IDS_NEWFOLDER, wszNewFolder, sizeof(wszNewFolder)/sizeof(WCHAR));
1711 if (uLen < sizeof(wszNewFolder)/sizeof(WCHAR)+3)
1712 return E_INVALIDARG;
1714 hr = IShellFolder2_EnumObjects(STATIC_CAST(IShellFolder2, This), 0,
1715 SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN, &pEnum);
1716 if (SUCCEEDED(hr)) {
1717 lstrcpynW(pwszName, wszNewFolder, uLen);
1718 IEnumIDList_Reset(pEnum);
1719 i = 2;
1720 while ((IEnumIDList_Next(pEnum, 1, &pidlElem, &dwFetched) == S_OK) && (dwFetched == 1)) {
1721 WCHAR wszTemp[MAX_PATH];
1722 _ILSimpleGetTextW(pidlElem, wszTemp, MAX_PATH);
1723 if (!lstrcmpiW(wszTemp, pwszName)) {
1724 IEnumIDList_Reset(pEnum);
1725 snprintfW(pwszName, uLen, wszFormat, wszNewFolder, i++);
1726 if (i > 99) {
1727 hr = E_FAIL;
1728 break;
1732 IEnumIDList_Release(pEnum);
1734 return hr;
1737 static HRESULT WINAPI UnixFolder_ISFHelper_AddFolder(ISFHelper* iface, HWND hwnd, LPCWSTR pwszName,
1738 LPITEMIDLIST* ppidlOut)
1740 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1741 char szNewDir[FILENAME_MAX];
1742 int cBaseLen;
1744 TRACE("(iface=%p, hwnd=%p, pwszName=%s, ppidlOut=%p)\n",
1745 iface, hwnd, debugstr_w(pwszName), ppidlOut);
1747 if (ppidlOut)
1748 *ppidlOut = NULL;
1750 if (!This->m_pszPath || !(This->m_dwAttributes & SFGAO_FILESYSTEM))
1751 return E_FAIL;
1753 lstrcpynA(szNewDir, This->m_pszPath, FILENAME_MAX);
1754 cBaseLen = lstrlenA(szNewDir);
1755 WideCharToMultiByte(CP_UNIXCP, 0, pwszName, -1, szNewDir+cBaseLen, FILENAME_MAX-cBaseLen, 0, 0);
1757 if (mkdir(szNewDir, 0777)) {
1758 char szMessage[256 + FILENAME_MAX];
1759 char szCaption[256];
1761 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_DENIED, szCaption, sizeof(szCaption));
1762 sprintf(szMessage, szCaption, szNewDir);
1763 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_CAPTION, szCaption, sizeof(szCaption));
1764 MessageBoxA(hwnd, szMessage, szCaption, MB_OK | MB_ICONEXCLAMATION);
1766 return E_FAIL;
1767 } else {
1768 LPITEMIDLIST pidlRelative;
1770 /* Inform the shell */
1771 if (SUCCEEDED(UNIXFS_path_to_pidl(This, pwszName, &pidlRelative))) {
1772 LPITEMIDLIST pidlAbsolute = ILCombine(This->m_pidlLocation, pidlRelative);
1773 if (ppidlOut)
1774 *ppidlOut = pidlRelative;
1775 else
1776 ILFree(pidlRelative);
1777 SHChangeNotify(SHCNE_MKDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1778 ILFree(pidlAbsolute);
1779 } else return E_FAIL;
1780 return S_OK;
1785 * Delete specified files by converting the path to DOS paths and calling
1786 * SHFileOperationW. If an error occurs it returns an error code. If the paths can't
1787 * be converted, S_FALSE is returned. In such situation DeleteItems will try to delete
1788 * the files using syscalls
1790 static HRESULT UNIXFS_delete_with_shfileop(UnixFolder *This, UINT cidl, const LPCITEMIDLIST *apidl)
1792 char szAbsolute[FILENAME_MAX], *pszRelative;
1793 LPWSTR wszPathsList, wszListPos;
1794 SHFILEOPSTRUCTW op;
1795 HRESULT ret;
1796 UINT i;
1798 lstrcpyA(szAbsolute, This->m_pszPath);
1799 pszRelative = szAbsolute + lstrlenA(szAbsolute);
1801 wszListPos = wszPathsList = HeapAlloc(GetProcessHeap(), 0, cidl*MAX_PATH*sizeof(WCHAR)+1);
1802 if (wszPathsList == NULL)
1803 return E_OUTOFMEMORY;
1804 for (i=0; i<cidl; i++) {
1805 LPWSTR wszDosPath;
1807 if (!_ILIsFolder(apidl[i]) && !_ILIsValue(apidl[i]))
1808 continue;
1809 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
1811 HeapFree(GetProcessHeap(), 0, wszPathsList);
1812 return E_INVALIDARG;
1814 wszDosPath = wine_get_dos_file_name(szAbsolute);
1815 if (wszDosPath == NULL || lstrlenW(wszDosPath) >= MAX_PATH)
1817 HeapFree(GetProcessHeap(), 0, wszPathsList);
1818 HeapFree(GetProcessHeap(), 0, wszDosPath);
1819 return S_FALSE;
1821 lstrcpyW(wszListPos, wszDosPath);
1822 wszListPos += lstrlenW(wszListPos)+1;
1823 HeapFree(GetProcessHeap(), 0, wszDosPath);
1825 *wszListPos = 0;
1827 ZeroMemory(&op, sizeof(op));
1828 op.hwnd = GetActiveWindow();
1829 op.wFunc = FO_DELETE;
1830 op.pFrom = wszPathsList;
1831 op.fFlags = FOF_ALLOWUNDO;
1832 if (!SHFileOperationW(&op))
1834 WARN("SHFileOperationW failed\n");
1835 ret = E_FAIL;
1837 else
1838 ret = S_OK;
1840 HeapFree(GetProcessHeap(), 0, wszPathsList);
1841 return ret;
1844 static HRESULT UNIXFS_delete_with_syscalls(UnixFolder *This, UINT cidl, const LPCITEMIDLIST *apidl)
1846 char szAbsolute[FILENAME_MAX], *pszRelative;
1847 static const WCHAR empty[] = {0};
1848 UINT i;
1850 if (!SHELL_ConfirmYesNoW(GetActiveWindow(), ASK_DELETE_SELECTED, empty))
1851 return S_OK;
1853 lstrcpyA(szAbsolute, This->m_pszPath);
1854 pszRelative = szAbsolute + lstrlenA(szAbsolute);
1856 for (i=0; i<cidl; i++) {
1857 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
1858 return E_INVALIDARG;
1859 if (_ILIsFolder(apidl[i])) {
1860 if (rmdir(szAbsolute))
1861 return E_FAIL;
1862 } else if (_ILIsValue(apidl[i])) {
1863 if (unlink(szAbsolute))
1864 return E_FAIL;
1867 return S_OK;
1870 static HRESULT WINAPI UnixFolder_ISFHelper_DeleteItems(ISFHelper* iface, UINT cidl,
1871 LPCITEMIDLIST* apidl)
1873 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1874 char szAbsolute[FILENAME_MAX], *pszRelative;
1875 LPITEMIDLIST pidlAbsolute;
1876 HRESULT hr = S_OK;
1877 UINT i;
1878 struct stat st;
1880 TRACE("(iface=%p, cidl=%d, apidl=%p)\n", iface, cidl, apidl);
1882 hr = UNIXFS_delete_with_shfileop(This, cidl, apidl);
1883 if (hr == S_FALSE)
1884 hr = UNIXFS_delete_with_syscalls(This, cidl, apidl);
1886 lstrcpyA(szAbsolute, This->m_pszPath);
1887 pszRelative = szAbsolute + lstrlenA(szAbsolute);
1889 /* we need to manually send the notifies if the files doesn't exist */
1890 for (i=0; i<cidl; i++) {
1891 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
1892 continue;
1893 pidlAbsolute = ILCombine(This->m_pidlLocation, apidl[i]);
1894 if (stat(szAbsolute, &st))
1896 if (_ILIsFolder(apidl[i])) {
1897 SHChangeNotify(SHCNE_RMDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1898 } else if (_ILIsValue(apidl[i])) {
1899 SHChangeNotify(SHCNE_DELETE, SHCNF_IDLIST, pidlAbsolute, NULL);
1902 ILFree(pidlAbsolute);
1905 return hr;
1908 static HRESULT WINAPI UnixFolder_ISFHelper_CopyItems(ISFHelper* iface, IShellFolder *psfFrom,
1909 UINT cidl, LPCITEMIDLIST *apidl)
1911 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1912 DWORD dwAttributes;
1913 UINT i;
1914 HRESULT hr;
1915 char szAbsoluteDst[FILENAME_MAX], *pszRelativeDst;
1917 TRACE("(iface=%p, psfFrom=%p, cidl=%d, apidl=%p)\n", iface, psfFrom, cidl, apidl);
1919 if (!psfFrom || !cidl || !apidl)
1920 return E_INVALIDARG;
1922 /* All source items have to be filesystem items. */
1923 dwAttributes = SFGAO_FILESYSTEM;
1924 hr = IShellFolder_GetAttributesOf(psfFrom, cidl, apidl, &dwAttributes);
1925 if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM))
1926 return E_INVALIDARG;
1928 lstrcpyA(szAbsoluteDst, This->m_pszPath);
1929 pszRelativeDst = szAbsoluteDst + strlen(szAbsoluteDst);
1931 for (i=0; i<cidl; i++) {
1932 WCHAR wszSrc[MAX_PATH];
1933 char szSrc[FILENAME_MAX];
1934 STRRET strret;
1935 HRESULT res;
1936 WCHAR *pwszDosSrc, *pwszDosDst;
1938 /* Build the unix path of the current source item. */
1939 if (FAILED(IShellFolder_GetDisplayNameOf(psfFrom, apidl[i], SHGDN_FORPARSING, &strret)))
1940 return E_FAIL;
1941 if (FAILED(StrRetToBufW(&strret, apidl[i], wszSrc, MAX_PATH)))
1942 return E_FAIL;
1943 if (!UNIXFS_get_unix_path(wszSrc, szSrc))
1944 return E_FAIL;
1946 /* Build the unix path of the current destination item */
1947 UNIXFS_filename_from_shitemid(apidl[i], pszRelativeDst);
1949 pwszDosSrc = wine_get_dos_file_name(szSrc);
1950 pwszDosDst = wine_get_dos_file_name(szAbsoluteDst);
1952 if (pwszDosSrc && pwszDosDst)
1953 res = UNIXFS_copy(pwszDosSrc, pwszDosDst);
1954 else
1955 res = E_OUTOFMEMORY;
1957 HeapFree(GetProcessHeap(), 0, pwszDosSrc);
1958 HeapFree(GetProcessHeap(), 0, pwszDosDst);
1960 if (res != S_OK)
1961 return res;
1963 return S_OK;
1966 /* VTable for UnixFolder's ISFHelper interface
1968 static const ISFHelperVtbl UnixFolder_ISFHelper_Vtbl = {
1969 UnixFolder_ISFHelper_QueryInterface,
1970 UnixFolder_ISFHelper_AddRef,
1971 UnixFolder_ISFHelper_Release,
1972 UnixFolder_ISFHelper_GetUniqueName,
1973 UnixFolder_ISFHelper_AddFolder,
1974 UnixFolder_ISFHelper_DeleteItems,
1975 UnixFolder_ISFHelper_CopyItems
1978 static HRESULT WINAPI UnixFolder_IDropTarget_QueryInterface(IDropTarget* iface, REFIID riid,
1979 void** ppvObject)
1981 return UnixFolder_IShellFolder2_QueryInterface(
1982 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IDropTarget, iface)), riid, ppvObject);
1985 static ULONG WINAPI UnixFolder_IDropTarget_AddRef(IDropTarget* iface)
1987 return UnixFolder_IShellFolder2_AddRef(
1988 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IDropTarget, iface)));
1991 static ULONG WINAPI UnixFolder_IDropTarget_Release(IDropTarget* iface)
1993 return UnixFolder_IShellFolder2_Release(
1994 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IDropTarget, iface)));
1997 #define HIDA_GetPIDLFolder(pida) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[0])
1998 #define HIDA_GetPIDLItem(pida, i) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[i+1])
2000 static HRESULT WINAPI UnixFolder_IDropTarget_DragEnter(IDropTarget *iface, IDataObject *pDataObject,
2001 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2003 UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
2004 FORMATETC format;
2005 STGMEDIUM medium;
2007 TRACE("(iface=%p, pDataObject=%p, dwKeyState=%08x, pt={.x=%d, .y=%d}, pdwEffect=%p)\n",
2008 iface, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
2010 if (!pdwEffect || !pDataObject)
2011 return E_INVALIDARG;
2013 /* Compute a mask of supported drop-effects for this shellfolder object and the given data
2014 * object. Dropping is only supported on folders, which represent filesystem locations. One
2015 * can't drop on file objects. And the 'move' drop effect is only supported, if the source
2016 * folder is not identical to the target folder. */
2017 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2018 InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
2019 if ((This->m_dwAttributes & SFGAO_FILESYSTEM) && /* Only drop to filesystem folders */
2020 _ILIsFolder(ILFindLastID(This->m_pidlLocation)) && /* Only drop to folders, not to files */
2021 SUCCEEDED(IDataObject_GetData(pDataObject, &format, &medium))) /* Only ShellIDList format */
2023 LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
2024 This->m_dwDropEffectsMask |= DROPEFFECT_COPY|DROPEFFECT_LINK;
2026 if (pidaShellIDList) { /* Files can only be moved between two different folders */
2027 if (!ILIsEqual(HIDA_GetPIDLFolder(pidaShellIDList), This->m_pidlLocation))
2028 This->m_dwDropEffectsMask |= DROPEFFECT_MOVE;
2029 GlobalUnlock(medium.u.hGlobal);
2033 *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
2035 return S_OK;
2038 static HRESULT WINAPI UnixFolder_IDropTarget_DragOver(IDropTarget *iface, DWORD dwKeyState,
2039 POINTL pt, DWORD *pdwEffect)
2041 UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
2043 TRACE("(iface=%p, dwKeyState=%08x, pt={.x=%d, .y=%d}, pdwEffect=%p)\n", iface, dwKeyState,
2044 pt.x, pt.y, pdwEffect);
2046 if (!pdwEffect)
2047 return E_INVALIDARG;
2049 *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
2051 return S_OK;
2054 static HRESULT WINAPI UnixFolder_IDropTarget_DragLeave(IDropTarget *iface) {
2055 UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
2057 TRACE("(iface=%p)\n", iface);
2059 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2061 return S_OK;
2064 static HRESULT WINAPI UnixFolder_IDropTarget_Drop(IDropTarget *iface, IDataObject *pDataObject,
2065 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2067 UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
2068 FORMATETC format;
2069 STGMEDIUM medium;
2070 HRESULT hr;
2072 TRACE("(iface=%p, pDataObject=%p, dwKeyState=%d, pt={.x=%d, .y=%d}, pdwEffect=%p) semi-stub\n",
2073 iface, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
2075 InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
2076 hr = IDataObject_GetData(pDataObject, &format, &medium);
2077 if (FAILED(hr))
2078 return hr;
2080 if (medium.tymed == TYMED_HGLOBAL) {
2081 IShellFolder *psfSourceFolder, *psfDesktopFolder;
2082 LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
2083 STRRET strret;
2084 UINT i;
2086 if (!pidaShellIDList)
2087 return HRESULT_FROM_WIN32(GetLastError());
2089 hr = SHGetDesktopFolder(&psfDesktopFolder);
2090 if (FAILED(hr)) {
2091 GlobalUnlock(medium.u.hGlobal);
2092 return hr;
2095 hr = IShellFolder_BindToObject(psfDesktopFolder, HIDA_GetPIDLFolder(pidaShellIDList), NULL,
2096 &IID_IShellFolder, (LPVOID*)&psfSourceFolder);
2097 IShellFolder_Release(psfDesktopFolder);
2098 if (FAILED(hr)) {
2099 GlobalUnlock(medium.u.hGlobal);
2100 return hr;
2103 for (i = 0; i < pidaShellIDList->cidl; i++) {
2104 WCHAR wszSourcePath[MAX_PATH];
2106 hr = IShellFolder_GetDisplayNameOf(psfSourceFolder, HIDA_GetPIDLItem(pidaShellIDList, i),
2107 SHGDN_FORPARSING, &strret);
2108 if (FAILED(hr))
2109 break;
2111 hr = StrRetToBufW(&strret, NULL, wszSourcePath, MAX_PATH);
2112 if (FAILED(hr))
2113 break;
2115 switch (*pdwEffect) {
2116 case DROPEFFECT_MOVE:
2117 FIXME("Move %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2118 break;
2119 case DROPEFFECT_COPY:
2120 FIXME("Copy %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2121 break;
2122 case DROPEFFECT_LINK:
2123 FIXME("Link %s from %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2124 break;
2128 IShellFolder_Release(psfSourceFolder);
2129 GlobalUnlock(medium.u.hGlobal);
2130 return hr;
2133 return E_NOTIMPL;
2136 /* VTable for UnixFolder's IDropTarget interface
2138 static const IDropTargetVtbl UnixFolder_IDropTarget_Vtbl = {
2139 UnixFolder_IDropTarget_QueryInterface,
2140 UnixFolder_IDropTarget_AddRef,
2141 UnixFolder_IDropTarget_Release,
2142 UnixFolder_IDropTarget_DragEnter,
2143 UnixFolder_IDropTarget_DragOver,
2144 UnixFolder_IDropTarget_DragLeave,
2145 UnixFolder_IDropTarget_Drop
2148 /******************************************************************************
2149 * Unix[Dos]Folder_Constructor [Internal]
2151 * PARAMS
2152 * pUnkOuter [I] Outer class for aggregation. Currently ignored.
2153 * riid [I] Interface asked for by the client.
2154 * ppv [O] Pointer to an riid interface to the UnixFolder object.
2156 * NOTES
2157 * Those are the only functions exported from shfldr_unixfs.c. They are called from
2158 * shellole.c's default class factory and thus have to exhibit a LPFNCREATEINSTANCE
2159 * compatible signature.
2161 * The UnixDosFolder_Constructor sets the dwPathMode member to PATHMODE_DOS. This
2162 * means that paths are converted from dos to unix and back at the interfaces.
2164 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, const CLSID *pCLSID)
2166 HRESULT hr = E_FAIL;
2167 UnixFolder *pUnixFolder;
2169 if (pUnkOuter) {
2170 FIXME("Aggregation not yet implemented!\n");
2171 return CLASS_E_NOAGGREGATION;
2174 pUnixFolder = SHAlloc((ULONG)sizeof(UnixFolder));
2176 if(pUnixFolder) {
2177 pUnixFolder->lpIShellFolder2Vtbl = &UnixFolder_IShellFolder2_Vtbl;
2178 pUnixFolder->lpIPersistFolder3Vtbl = &UnixFolder_IPersistFolder3_Vtbl;
2179 pUnixFolder->lpIPersistPropertyBagVtbl = &UnixFolder_IPersistPropertyBag_Vtbl;
2180 pUnixFolder->lpISFHelperVtbl = &UnixFolder_ISFHelper_Vtbl;
2181 pUnixFolder->lpIDropTargetVtbl = &UnixFolder_IDropTarget_Vtbl;
2182 pUnixFolder->m_cRef = 0;
2183 pUnixFolder->m_pszPath = NULL;
2184 pUnixFolder->m_pidlLocation = NULL;
2185 pUnixFolder->m_dwPathMode = IsEqualCLSID(&CLSID_UnixFolder, pCLSID) ? PATHMODE_UNIX : PATHMODE_DOS;
2186 pUnixFolder->m_dwAttributes = 0;
2187 pUnixFolder->m_pCLSID = pCLSID;
2188 pUnixFolder->m_dwDropEffectsMask = DROPEFFECT_NONE;
2190 UnixFolder_IShellFolder2_AddRef(STATIC_CAST(IShellFolder2, pUnixFolder));
2191 hr = UnixFolder_IShellFolder2_QueryInterface(STATIC_CAST(IShellFolder2, pUnixFolder), riid, ppv);
2192 UnixFolder_IShellFolder2_Release(STATIC_CAST(IShellFolder2, pUnixFolder));
2194 return hr;
2197 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2198 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2199 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixFolder);
2202 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2203 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2204 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixDosFolder);
2207 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2208 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2209 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_FolderShortcut);
2212 HRESULT WINAPI MyDocuments_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2213 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2214 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_MyDocuments);
2217 /******************************************************************************
2218 * UnixSubFolderIterator
2220 * Class whose heap based objects represent iterators over the sub-directories
2221 * of a given UnixFolder object.
2224 /* UnixSubFolderIterator object layout and typedef.
2226 typedef struct _UnixSubFolderIterator {
2227 const IEnumIDListVtbl *lpIEnumIDListVtbl;
2228 LONG m_cRef;
2229 SHCONTF m_fFilter;
2230 DIR *m_dirFolder;
2231 char m_szFolder[FILENAME_MAX];
2232 } UnixSubFolderIterator;
2234 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
2235 TRACE("(iterator=%p)\n", iterator);
2237 if (iterator->m_dirFolder)
2238 closedir(iterator->m_dirFolder);
2239 SHFree(iterator);
2242 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface,
2243 REFIID riid, void** ppv)
2245 TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
2247 if (!ppv) return E_INVALIDARG;
2249 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
2250 *ppv = iface;
2251 } else {
2252 *ppv = NULL;
2253 return E_NOINTERFACE;
2256 IEnumIDList_AddRef(iface);
2257 return S_OK;
2260 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
2262 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2264 TRACE("(iface=%p)\n", iface);
2266 return InterlockedIncrement(&This->m_cRef);
2269 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
2271 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2272 ULONG cRef;
2274 TRACE("(iface=%p)\n", iface);
2276 cRef = InterlockedDecrement(&This->m_cRef);
2278 if (!cRef)
2279 UnixSubFolderIterator_Destroy(This);
2281 return cRef;
2284 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt,
2285 LPITEMIDLIST* rgelt, ULONG* pceltFetched)
2287 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2288 ULONG i = 0;
2290 /* This->m_dirFolder will be NULL if the user doesn't have access rights for the dir. */
2291 if (This->m_dirFolder) {
2292 char *pszRelativePath = This->m_szFolder + lstrlenA(This->m_szFolder);
2293 struct dirent *pDirEntry;
2295 while (i < celt) {
2296 pDirEntry = readdir(This->m_dirFolder);
2297 if (!pDirEntry) break; /* No more entries */
2298 if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
2300 /* Temporarily build absolute path in This->m_szFolder. Then construct a pidl
2301 * and see if it passes the filter.
2303 lstrcpyA(pszRelativePath, pDirEntry->d_name);
2304 rgelt[i] = SHAlloc(
2305 UNIXFS_shitemid_len_from_filename(pszRelativePath, NULL, NULL)+sizeof(USHORT));
2306 if (!UNIXFS_build_shitemid(This->m_szFolder, rgelt[i]) ||
2307 !UNIXFS_is_pidl_of_type(rgelt[i], This->m_fFilter))
2309 SHFree(rgelt[i]);
2310 continue;
2312 memset(((PBYTE)rgelt[i])+rgelt[i]->mkid.cb, 0, sizeof(USHORT));
2313 i++;
2315 *pszRelativePath = '\0'; /* Restore the original path in This->m_szFolder. */
2318 if (pceltFetched)
2319 *pceltFetched = i;
2321 return (i == 0) ? S_FALSE : S_OK;
2324 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
2326 LPITEMIDLIST *apidl;
2327 ULONG cFetched;
2328 HRESULT hr;
2330 TRACE("(iface=%p, celt=%d)\n", iface, celt);
2332 /* Call IEnumIDList::Next and delete the resulting pidls. */
2333 apidl = SHAlloc(celt * sizeof(LPITEMIDLIST));
2334 hr = IEnumIDList_Next(iface, celt, apidl, &cFetched);
2335 if (SUCCEEDED(hr))
2336 while (cFetched--)
2337 SHFree(apidl[cFetched]);
2338 SHFree(apidl);
2340 return hr;
2343 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
2345 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2347 TRACE("(iface=%p)\n", iface);
2349 if (This->m_dirFolder)
2350 rewinddir(This->m_dirFolder);
2352 return S_OK;
2355 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This,
2356 IEnumIDList** ppenum)
2358 FIXME("stub\n");
2359 return E_NOTIMPL;
2362 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
2364 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
2365 UnixSubFolderIterator_IEnumIDList_QueryInterface,
2366 UnixSubFolderIterator_IEnumIDList_AddRef,
2367 UnixSubFolderIterator_IEnumIDList_Release,
2368 UnixSubFolderIterator_IEnumIDList_Next,
2369 UnixSubFolderIterator_IEnumIDList_Skip,
2370 UnixSubFolderIterator_IEnumIDList_Reset,
2371 UnixSubFolderIterator_IEnumIDList_Clone
2374 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter) {
2375 UnixSubFolderIterator *iterator;
2377 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
2379 iterator = SHAlloc((ULONG)sizeof(UnixSubFolderIterator));
2380 iterator->lpIEnumIDListVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
2381 iterator->m_cRef = 0;
2382 iterator->m_fFilter = fFilter;
2383 iterator->m_dirFolder = opendir(pUnixFolder->m_pszPath);
2384 lstrcpyA(iterator->m_szFolder, pUnixFolder->m_pszPath);
2386 UnixSubFolderIterator_IEnumIDList_AddRef((IEnumIDList*)iterator);
2388 return (IUnknown*)iterator;
2391 #else /* __MINGW32__ || _MSC_VER */
2393 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2395 return E_NOTIMPL;
2398 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2400 return E_NOTIMPL;
2403 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2405 return E_NOTIMPL;
2408 HRESULT WINAPI MyDocuments_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2410 return E_NOTIMPL;
2413 #endif /* __MINGW32__ || _MSC_VER */
2415 /******************************************************************************
2416 * UNIXFS_is_rooted_at_desktop [Internal]
2418 * Checks if the unixfs namespace extension is rooted at desktop level.
2420 * RETURNS
2421 * TRUE, if unixfs is rooted at desktop level
2422 * FALSE, if not.
2424 BOOL UNIXFS_is_rooted_at_desktop(void) {
2425 HKEY hKey;
2426 WCHAR wszRootedAtDesktop[69 + CHARS_IN_GUID] = {
2427 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2428 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2429 'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
2430 'N','a','m','e','S','p','a','c','e','\\',0 };
2432 if (StringFromGUID2(&CLSID_UnixDosFolder, wszRootedAtDesktop + 69, CHARS_IN_GUID) &&
2433 RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszRootedAtDesktop, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
2435 RegCloseKey(hKey);
2436 return TRUE;
2438 return FALSE;