winedbg: Don't dereference possibly NULL thread pointer.
[wine/zf.git] / dlls / shell32 / shfldr_unixfs.c
blob48d5fe4ecb6b755196b55ef167eff914dd407ac3
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 its 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 its 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 #include <errno.h>
132 #ifdef HAVE_DIRENT_H
133 # include <dirent.h>
134 #endif
135 #include <stdlib.h>
136 #ifdef HAVE_UNISTD_H
137 # include <unistd.h>
138 #endif
139 #ifdef HAVE_SYS_STAT_H
140 # include <sys/stat.h>
141 #endif
142 #ifdef HAVE_PWD_H
143 # include <pwd.h>
144 #endif
145 #ifdef HAVE_GRP_H
146 # include <grp.h>
147 #endif
149 #define COBJMACROS
150 #define NONAMELESSUNION
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 #if !defined(__MINGW32__) && !defined(_MSC_VER)
170 WINE_DEFAULT_DEBUG_CHANNEL(shell);
172 #define LEN_SHITEMID_FIXED_PART ((USHORT) \
173 ( sizeof(USHORT) /* SHITEMID's cb field. */ \
174 + sizeof(PIDLTYPE) /* PIDLDATA's type field. */ \
175 + sizeof(FileStruct) /* Well, the FileStruct. */ \
176 - sizeof(char) /* One char too much in FileStruct. */ \
177 + sizeof(FileStructW) /* You name it. */ \
178 - sizeof(WCHAR) /* One WCHAR too much in FileStructW. */ \
179 + sizeof(WORD) )) /* Offset of FileStructW field in PIDL. */
181 #define PATHMODE_UNIX 0
182 #define PATHMODE_DOS 1
184 static const WCHAR wFileSystemBindData[] = {
185 'F','i','l','e',' ','S','y','s','t','e','m',' ','B','i','n','d',' ','D','a','t','a',0};
187 typedef struct {
188 IShellFolder2 IShellFolder2_iface;
189 IPersistFolder3 IPersistFolder3_iface;
190 IPersistPropertyBag IPersistPropertyBag_iface;
191 IDropTarget IDropTarget_iface;
192 ISFHelper ISFHelper_iface;
194 LONG ref;
195 CHAR *m_pszPath; /* Target path of the shell folder (CP_UNIXCP) */
196 LPITEMIDLIST m_pidlLocation; /* Location in the shell namespace */
197 DWORD m_dwPathMode;
198 DWORD m_dwAttributes;
199 const CLSID *m_pCLSID;
200 DWORD m_dwDropEffectsMask;
201 } UnixFolder;
203 static inline UnixFolder *impl_from_IShellFolder2(IShellFolder2 *iface)
205 return CONTAINING_RECORD(iface, UnixFolder, IShellFolder2_iface);
208 static inline UnixFolder *impl_from_IPersistFolder3(IPersistFolder3 *iface)
210 return CONTAINING_RECORD(iface, UnixFolder, IPersistFolder3_iface);
213 static inline UnixFolder *impl_from_IPersistPropertyBag(IPersistPropertyBag *iface)
215 return CONTAINING_RECORD(iface, UnixFolder, IPersistPropertyBag_iface);
218 static inline UnixFolder *impl_from_ISFHelper(ISFHelper *iface)
220 return CONTAINING_RECORD(iface, UnixFolder, ISFHelper_iface);
223 static inline UnixFolder *impl_from_IDropTarget(IDropTarget *iface)
225 return CONTAINING_RECORD(iface, UnixFolder, IDropTarget_iface);
228 /* Will hold the registered clipboard format identifier for ITEMIDLISTS. */
229 static UINT cfShellIDList = 0;
231 /******************************************************************************
232 * UNIXFS_filename_from_shitemid [Internal]
234 * Get CP_UNIXCP encoded filename corresponding to the first item of a pidl
236 * PARAMS
237 * pidl [I] A simple SHITEMID
238 * pszPathElement [O] Filename in CP_UNIXCP encoding will be stored here
240 * RETURNS
241 * Success: Number of bytes necessary to store the CP_UNIXCP encoded filename
242 * _without_ the terminating NUL.
243 * Failure: 0
245 * NOTES
246 * Size of the buffer at pszPathElement has to be FILENAME_MAX. pszPathElement
247 * may be NULL, if you are only interested in the return value.
249 static int UNIXFS_filename_from_shitemid(LPCITEMIDLIST pidl, char* pszPathElement) {
250 FileStructW *pFileStructW = _ILGetFileStructW(pidl);
251 int cLen = 0;
253 if (pFileStructW) {
254 cLen = WideCharToMultiByte(CP_UNIXCP, 0, pFileStructW->wszName, -1, pszPathElement,
255 pszPathElement ? FILENAME_MAX : 0, 0, 0);
256 } else {
257 /* There might be pidls slipping in from shfldr_fs.c, which don't contain the
258 * FileStructW field. In this case, we have to convert from CP_ACP to CP_UNIXCP. */
259 char *pszText = _ILGetTextPointer(pidl);
260 WCHAR *pwszPathElement = NULL;
261 int cWideChars;
263 cWideChars = MultiByteToWideChar(CP_ACP, 0, pszText, -1, NULL, 0);
264 if (!cWideChars) goto cleanup;
266 pwszPathElement = SHAlloc(cWideChars * sizeof(WCHAR));
267 if (!pwszPathElement) goto cleanup;
269 cWideChars = MultiByteToWideChar(CP_ACP, 0, pszText, -1, pwszPathElement, cWideChars);
270 if (!cWideChars) goto cleanup;
272 cLen = WideCharToMultiByte(CP_UNIXCP, 0, pwszPathElement, -1, pszPathElement,
273 pszPathElement ? FILENAME_MAX : 0, 0, 0);
275 cleanup:
276 SHFree(pwszPathElement);
279 if (cLen) cLen--; /* Don't count terminating NUL! */
280 return cLen;
283 /******************************************************************************
284 * UNIXFS_shitemid_len_from_filename [Internal]
286 * Computes the necessary length of a pidl to hold a path element
288 * PARAMS
289 * szPathElement [I] The path element string in CP_UNIXCP encoding.
290 * ppszPathElement [O] Path element string in CP_ACP encoding.
291 * ppwszPathElement [O] Path element string as WCHAR string.
293 * RETURNS
294 * Success: Length in bytes of a SHITEMID representing szPathElement
295 * Failure: 0
297 * NOTES
298 * Provide NULL values if not interested in pp(w)szPathElement. Otherwise
299 * caller is responsible to free ppszPathElement and ppwszPathElement with
300 * SHFree.
302 static USHORT UNIXFS_shitemid_len_from_filename(
303 const char *szPathElement, char **ppszPathElement, WCHAR **ppwszPathElement)
305 USHORT cbPidlLen = 0;
306 WCHAR *pwszPathElement = NULL;
307 char *pszPathElement = NULL;
308 int cWideChars, cChars;
310 /* There and Back Again: A Hobbit's Holiday. CP_UNIXCP might be some ANSI
311 * codepage or it might be a real multi-byte encoding like utf-8. There is no
312 * other way to figure out the length of the corresponding WCHAR and CP_ACP
313 * strings without actually doing the full CP_UNIXCP -> WCHAR -> CP_ACP cycle. */
315 cWideChars = MultiByteToWideChar(CP_UNIXCP, 0, szPathElement, -1, NULL, 0);
316 if (!cWideChars) goto cleanup;
318 pwszPathElement = SHAlloc(cWideChars * sizeof(WCHAR));
319 if (!pwszPathElement) goto cleanup;
321 cWideChars = MultiByteToWideChar(CP_UNIXCP, 0, szPathElement, -1, pwszPathElement, cWideChars);
322 if (!cWideChars) goto cleanup;
324 cChars = WideCharToMultiByte(CP_ACP, 0, pwszPathElement, -1, NULL, 0, 0, 0);
325 if (!cChars) goto cleanup;
327 pszPathElement = SHAlloc(cChars);
328 if (!pszPathElement) goto cleanup;
330 cChars = WideCharToMultiByte(CP_ACP, 0, pwszPathElement, -1, pszPathElement, cChars, 0, 0);
331 if (!cChars) goto cleanup;
333 /* (cChars & 0x1) is for the potential alignment byte */
334 cbPidlLen = LEN_SHITEMID_FIXED_PART + cChars + (cChars & 0x1) + cWideChars * sizeof(WCHAR);
336 cleanup:
337 if (cbPidlLen && ppszPathElement)
338 *ppszPathElement = pszPathElement;
339 else
340 SHFree(pszPathElement);
342 if (cbPidlLen && ppwszPathElement)
343 *ppwszPathElement = pwszPathElement;
344 else
345 SHFree(pwszPathElement);
347 return cbPidlLen;
350 /******************************************************************************
351 * UNIXFS_is_pidl_of_type [Internal]
353 * Checks for the first SHITEMID of an ITEMIDLIST if it passes a filter.
355 * PARAMS
356 * pIDL [I] The ITEMIDLIST to be checked.
357 * fFilter [I] Shell condition flags, which specify the filter.
359 * RETURNS
360 * TRUE, if pIDL is accepted by fFilter
361 * FALSE, otherwise
363 static inline BOOL UNIXFS_is_pidl_of_type(LPCITEMIDLIST pIDL, SHCONTF fFilter) {
364 const PIDLDATA *pIDLData = _ILGetDataPointer(pIDL);
365 if (!(fFilter & SHCONTF_INCLUDEHIDDEN) && pIDLData &&
366 (pIDLData->u.file.uFileAttribs & FILE_ATTRIBUTE_HIDDEN))
368 return FALSE;
370 if (_ILIsFolder(pIDL) && (fFilter & SHCONTF_FOLDERS)) return TRUE;
371 if (_ILIsValue(pIDL) && (fFilter & SHCONTF_NONFOLDERS)) return TRUE;
372 return FALSE;
375 /******************************************************************************
376 * UNIXFS_get_unix_path [Internal]
378 * Convert an absolute dos path to an absolute unix path.
379 * Evaluate "/.", "/.." and the symbolic links in $WINEPREFIX/dosdevices.
381 * PARAMS
382 * pszDosPath [I] An absolute dos path
383 * pszCanonicalPath [O] Buffer of length FILENAME_MAX. Will receive the canonical path.
385 * RETURNS
386 * Success, TRUE
387 * Failure, FALSE - Nonexistent path, too long, insufficient rights, too many symlinks
389 static BOOL UNIXFS_get_unix_path(LPCWSTR pszDosPath, char *pszCanonicalPath)
391 char *pPathTail, *pElement, *pCanonicalTail, szPath[FILENAME_MAX], *pszUnixPath, mb_path[FILENAME_MAX];
392 BOOL has_failed = FALSE;
393 WCHAR wszDrive[] = { '?', ':', '\\', 0 }, dospath[MAX_PATH], *dospath_end;
394 int cDriveSymlinkLen;
395 void *redir;
397 TRACE("(pszDosPath=%s, pszCanonicalPath=%p)\n", debugstr_w(pszDosPath), pszCanonicalPath);
399 if (!pszDosPath || pszDosPath[1] != ':')
400 return FALSE;
402 /* Get the canonicalized unix path corresponding to the drive letter. */
403 wszDrive[0] = pszDosPath[0];
404 pszUnixPath = wine_get_unix_file_name(wszDrive);
405 if (!pszUnixPath) return FALSE;
406 cDriveSymlinkLen = strlen(pszUnixPath);
407 pElement = realpath(pszUnixPath, szPath);
408 heap_free(pszUnixPath);
409 if (!pElement) return FALSE;
410 if (szPath[strlen(szPath)-1] != '/') strcat(szPath, "/");
412 /* Append the part relative to the drive symbolic link target. */
413 lstrcpyW(dospath, pszDosPath);
414 dospath_end = dospath + lstrlenW(dospath);
415 /* search for the most valid UNIX path possible, then append missing
416 * path parts */
417 Wow64DisableWow64FsRedirection(&redir);
418 while(!(pszUnixPath = wine_get_unix_file_name(dospath))){
419 if(has_failed){
420 *dospath_end = '/';
421 --dospath_end;
422 }else
423 has_failed = TRUE;
424 while(*dospath_end != '\\' && *dospath_end != '/'){
425 --dospath_end;
426 if(dospath_end < dospath)
427 break;
429 *dospath_end = '\0';
431 Wow64RevertWow64FsRedirection(redir);
432 if(dospath_end < dospath)
433 return FALSE;
434 strcat(szPath, pszUnixPath + cDriveSymlinkLen);
435 heap_free(pszUnixPath);
437 if(has_failed && WideCharToMultiByte(CP_UNIXCP, 0, dospath_end + 1, -1,
438 mb_path, FILENAME_MAX, NULL, NULL) > 0){
439 strcat(szPath, "/");
440 strcat(szPath, mb_path);
443 /* pCanonicalTail always points to the end of the canonical path constructed
444 * thus far. pPathTail points to the still to be processed part of the input
445 * path. pElement points to the path element currently investigated.
447 *pszCanonicalPath = '\0';
448 pCanonicalTail = pszCanonicalPath;
449 pPathTail = szPath;
451 do {
452 char cTemp;
454 pElement = pPathTail;
455 pPathTail = strchr(pPathTail+1, '/');
456 if (!pPathTail) /* Last path element may not be terminated by '/'. */
457 pPathTail = pElement + strlen(pElement);
458 /* Temporarily terminate the current path element. Will be restored later. */
459 cTemp = *pPathTail;
460 *pPathTail = '\0';
462 /* Skip "/." path elements */
463 if (!strcmp("/.", pElement)) {
464 *pPathTail = cTemp;
465 } else if (!strcmp("/..", pElement)) {
466 /* Remove last element in canonical path for "/.." elements, then skip. */
467 char *pTemp = strrchr(pszCanonicalPath, '/');
468 if (pTemp)
469 pCanonicalTail = pTemp;
470 *pCanonicalTail = '\0';
471 *pPathTail = cTemp;
472 } else {
473 /* Directory or file. Copy to canonical path */
474 if (pCanonicalTail - pszCanonicalPath + pPathTail - pElement + 1 > FILENAME_MAX)
475 return FALSE;
477 memcpy(pCanonicalTail, pElement, pPathTail - pElement + 1);
478 pCanonicalTail += pPathTail - pElement;
479 *pPathTail = cTemp;
481 } while (pPathTail[0] == '/');
483 TRACE("--> %s\n", debugstr_a(pszCanonicalPath));
485 return TRUE;
488 /******************************************************************************
489 * UNIXFS_build_shitemid [Internal]
491 * Constructs a new SHITEMID for the last component of path 'pszUnixPath' into
492 * buffer 'pIDL'.
494 * PARAMS
495 * pszUnixPath [I] An absolute path. The SHITEMID will be built for the last component.
496 * pbc [I] Bind context for this action, used to determine if the file must exist
497 * pIDL [O] SHITEMID will be constructed here.
499 * RETURNS
500 * Success: A pointer to the terminating '\0' character of path.
501 * Failure: NULL
503 * NOTES
504 * Minimum size of pIDL is SHITEMID_LEN_FROM_NAME_LEN(strlen(last_component_of_path)).
505 * If what you need is a PIDLLIST with a single SHITEMID, don't forget to append
506 * a 0 USHORT value.
508 static char* UNIXFS_build_shitemid(char *pszUnixPath, BOOL bMustExist, WIN32_FIND_DATAW *pFindData, void *pIDL) {
509 LPPIDLDATA pIDLData;
510 struct stat fileStat;
511 WIN32_FIND_DATAW findData;
512 char *pszComponentU, *pszComponentA;
513 WCHAR *pwszComponentW;
514 int cComponentULen, cComponentALen;
515 USHORT cbLen;
516 FileStructW *pFileStructW;
517 WORD uOffsetW, *pOffsetW;
519 TRACE("(pszUnixPath=%s, bMustExist=%s, pFindData=%p, pIDL=%p)\n",
520 debugstr_a(pszUnixPath), bMustExist ? "T" : "F", pFindData, pIDL);
522 if (pFindData)
523 memcpy(&findData, pFindData, sizeof(WIN32_FIND_DATAW));
524 else {
525 memset(&findData, 0, sizeof(WIN32_FIND_DATAW));
526 findData.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
529 /* We are only interested in regular files and directories. */
530 if (stat(pszUnixPath, &fileStat)){
531 if (bMustExist || errno != ENOENT)
532 return NULL;
533 } else {
534 LARGE_INTEGER time;
536 if (S_ISDIR(fileStat.st_mode))
537 findData.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
538 else if (S_ISREG(fileStat.st_mode))
539 findData.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
540 else
541 return NULL;
543 findData.nFileSizeLow = (DWORD)fileStat.st_size;
544 findData.nFileSizeHigh = fileStat.st_size >> 32;
546 RtlSecondsSince1970ToTime(fileStat.st_mtime, &time);
547 findData.ftLastWriteTime.dwLowDateTime = time.u.LowPart;
548 findData.ftLastWriteTime.dwHighDateTime = time.u.HighPart;
549 RtlSecondsSince1970ToTime(fileStat.st_atime, &time);
550 findData.ftLastAccessTime.dwLowDateTime = time.u.LowPart;
551 findData.ftLastAccessTime.dwHighDateTime = time.u.HighPart;
554 /* Compute the SHITEMID's length and wipe it. */
555 pszComponentU = strrchr(pszUnixPath, '/') + 1;
556 cComponentULen = strlen(pszComponentU);
557 cbLen = UNIXFS_shitemid_len_from_filename(pszComponentU, &pszComponentA, &pwszComponentW);
558 if (!cbLen) return NULL;
559 memset(pIDL, 0, cbLen);
560 ((LPSHITEMID)pIDL)->cb = cbLen;
562 /* Set shell32's standard SHITEMID data fields. */
563 pIDLData = _ILGetDataPointer(pIDL);
564 pIDLData->type = (findData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) ? PT_FOLDER : PT_VALUE;
565 pIDLData->u.file.dwFileSize = findData.nFileSizeLow;
566 FileTimeToDosDateTime(&findData.ftLastWriteTime, &pIDLData->u.file.uFileDate,
567 &pIDLData->u.file.uFileTime);
568 pIDLData->u.file.uFileAttribs = 0;
569 pIDLData->u.file.uFileAttribs |= findData.dwFileAttributes;
570 if (pszComponentU[0] == '.') pIDLData->u.file.uFileAttribs |= FILE_ATTRIBUTE_HIDDEN;
571 cComponentALen = lstrlenA(pszComponentA) + 1;
572 memcpy(pIDLData->u.file.szNames, pszComponentA, cComponentALen);
574 pFileStructW = (FileStructW*)(pIDLData->u.file.szNames + cComponentALen + (cComponentALen & 0x1));
575 uOffsetW = (WORD)(((LPBYTE)pFileStructW) - ((LPBYTE)pIDL));
576 pFileStructW->cbLen = cbLen - uOffsetW;
577 FileTimeToDosDateTime(&findData.ftLastWriteTime, &pFileStructW->uCreationDate,
578 &pFileStructW->uCreationTime);
579 FileTimeToDosDateTime(&findData.ftLastAccessTime, &pFileStructW->uLastAccessDate,
580 &pFileStructW->uLastAccessTime);
581 lstrcpyW(pFileStructW->wszName, pwszComponentW);
583 pOffsetW = (WORD*)(((LPBYTE)pIDL) + cbLen - sizeof(WORD));
584 *pOffsetW = uOffsetW;
586 SHFree(pszComponentA);
587 SHFree(pwszComponentW);
589 return pszComponentU + cComponentULen;
592 /******************************************************************************
593 * UNIXFS_path_to_pidl [Internal]
595 * PARAMS
596 * pUnixFolder [I] If path is relative, pUnixFolder represents the base path
597 * path [I] An absolute unix or dos path or a path relative to pUnixFolder
598 * ppidl [O] The corresponding ITEMIDLIST. Release with SHFree/ILFree
600 * RETURNS
601 * Success: S_OK
602 * Failure: Error code, invalid params or out of memory
604 * NOTES
605 * pUnixFolder also carries the information if the path is expected to be unix or dos.
607 static HRESULT UNIXFS_path_to_pidl(UnixFolder *pUnixFolder, LPBC pbc, const WCHAR *path,
608 LPITEMIDLIST *ppidl) {
609 LPITEMIDLIST pidl;
610 int cPidlLen, cPathLen;
611 char *pSlash, *pNextSlash, szCompletePath[FILENAME_MAX], *pNextPathElement, *pszAPath;
612 WCHAR *pwszPath;
613 WIN32_FIND_DATAW find_data;
614 BOOL must_exist = TRUE;
616 TRACE("pUnixFolder=%p, pbc=%p, path=%s, ppidl=%p\n", pUnixFolder, pbc, debugstr_w(path), ppidl);
618 if (!ppidl || !path)
619 return E_INVALIDARG;
621 /* Build an absolute path and let pNextPathElement point to the interesting
622 * relative sub-path. We need the absolute path to call 'stat', but the pidl
623 * will only contain the relative part.
625 if ((pUnixFolder->m_dwPathMode == PATHMODE_DOS) && (path[1] == ':'))
627 /* Absolute dos path. Convert to unix */
628 if (!UNIXFS_get_unix_path(path, szCompletePath))
629 return E_FAIL;
630 pNextPathElement = szCompletePath;
632 else if ((pUnixFolder->m_dwPathMode == PATHMODE_UNIX) && (path[0] == '/'))
634 /* Absolute unix path. Just convert to ANSI. */
635 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath, FILENAME_MAX, NULL, NULL);
636 pNextPathElement = szCompletePath;
638 else
640 /* Relative dos or unix path. Concat with this folder's path */
641 int cBasePathLen = strlen(pUnixFolder->m_pszPath);
642 memcpy(szCompletePath, pUnixFolder->m_pszPath, cBasePathLen);
643 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath + cBasePathLen,
644 FILENAME_MAX - cBasePathLen, NULL, NULL);
645 pNextPathElement = szCompletePath + cBasePathLen - 1;
647 /* If in dos mode, replace '\' with '/' */
648 if (pUnixFolder->m_dwPathMode == PATHMODE_DOS) {
649 char *pBackslash = strchr(pNextPathElement, '\\');
650 while (pBackslash) {
651 *pBackslash = '/';
652 pBackslash = strchr(pBackslash, '\\');
657 /* Special case for the root folder. */
658 if (!strcmp(szCompletePath, "/")) {
659 *ppidl = pidl = SHAlloc(sizeof(USHORT));
660 if (!pidl) return E_FAIL;
661 pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
662 return S_OK;
665 /* Remove trailing slash, if present */
666 cPathLen = strlen(szCompletePath);
667 if (szCompletePath[cPathLen-1] == '/')
668 szCompletePath[cPathLen-1] = '\0';
670 if ((szCompletePath[0] != '/') || (pNextPathElement[0] != '/')) {
671 ERR("szCompletePath: %s, pNextPathElement: %s\n", szCompletePath, pNextPathElement);
672 return E_FAIL;
675 /* At this point, we have an absolute unix path in szCompletePath
676 * and the relative portion of it in pNextPathElement. Both starting with '/'
677 * and _not_ terminated by a '/'. */
678 TRACE("complete path: %s, relative path: %s\n", szCompletePath, pNextPathElement);
680 /* Convert to CP_ACP and WCHAR */
681 if (!UNIXFS_shitemid_len_from_filename(pNextPathElement, &pszAPath, &pwszPath))
682 return E_FAIL;
684 /* Compute the length of the complete ITEMIDLIST */
685 cPidlLen = 0;
686 pSlash = pszAPath;
687 while (pSlash) {
688 pNextSlash = strchr(pSlash+1, '/');
689 cPidlLen += LEN_SHITEMID_FIXED_PART + /* Fixed part length plus potential alignment byte. */
690 (pNextSlash ? (pNextSlash - pSlash) & 0x1 : lstrlenA(pSlash) & 0x1);
691 pSlash = pNextSlash;
694 /* The USHORT is for the ITEMIDLIST terminator. The NUL terminators for the sub-path-strings
695 * are accounted for by the '/' separators, which are not stored in the SHITEMIDs. Above we
696 * have ensured that the number of '/'s exactly matches the number of sub-path-strings. */
697 cPidlLen += lstrlenA(pszAPath) + lstrlenW(pwszPath) * sizeof(WCHAR) + sizeof(USHORT);
699 SHFree(pszAPath);
700 SHFree(pwszPath);
702 *ppidl = pidl = SHAlloc(cPidlLen);
703 if (!pidl) return E_FAIL;
705 if (pbc) {
706 IUnknown *unk;
707 IFileSystemBindData *fsb;
708 HRESULT hr;
710 hr = IBindCtx_GetObjectParam(pbc, (LPOLESTR)wFileSystemBindData, &unk);
711 if (SUCCEEDED(hr)) {
712 hr = IUnknown_QueryInterface(unk, &IID_IFileSystemBindData, (LPVOID*)&fsb);
713 if (SUCCEEDED(hr)) {
714 hr = IFileSystemBindData_GetFindData(fsb, &find_data);
715 if (FAILED(hr))
716 memset(&find_data, 0, sizeof(WIN32_FIND_DATAW));
718 must_exist = FALSE;
719 IFileSystemBindData_Release(fsb);
721 IUnknown_Release(unk);
725 /* Concatenate the SHITEMIDs of the sub-directories. */
726 while (*pNextPathElement) {
727 pSlash = strchr(pNextPathElement+1, '/');
728 if (pSlash) *pSlash = '\0';
729 pNextPathElement = UNIXFS_build_shitemid(szCompletePath, must_exist,
730 must_exist&&!pSlash ? &find_data : NULL, pidl);
731 if (pSlash) *pSlash = '/';
733 if (!pNextPathElement) {
734 SHFree(*ppidl);
735 *ppidl = NULL;
736 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
738 pidl = ILGetNext(pidl);
740 pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
742 if ((char *)pidl-(char *)*ppidl+sizeof(USHORT) != cPidlLen) /* We've corrupted the heap :( */
743 ERR("Computed length of pidl incorrect. Please report.\n");
745 return S_OK;
748 /******************************************************************************
749 * UNIXFS_initialize_target_folder [Internal]
751 * Initialize the m_pszPath member of an UnixFolder, given an absolute unix
752 * base path and a relative ITEMIDLIST. Leave the m_pidlLocation member, which
753 * specifies the location in the shell namespace alone.
755 * PARAMS
756 * This [IO] The UnixFolder, whose target path is to be initialized
757 * szBasePath [I] The absolute base path
758 * pidlSubFolder [I] Relative part of the path, given as an ITEMIDLIST
759 * dwAttributes [I] Attributes to add to the Folders m_dwAttributes member
760 * (Used to pass the SFGAO_FILESYSTEM flag down the path)
761 * RETURNS
762 * Success: S_OK,
763 * Failure: E_FAIL
765 static HRESULT UNIXFS_initialize_target_folder(UnixFolder *This, const char *szBasePath,
766 LPCITEMIDLIST pidlSubFolder, DWORD dwAttributes)
768 LPCITEMIDLIST current = pidlSubFolder;
769 DWORD dwPathLen = strlen(szBasePath)+1;
770 char *pNextDir;
771 WCHAR *dos_name;
773 /* Determine the path's length bytes */
774 while (!_ILIsEmpty(current)) {
775 dwPathLen += UNIXFS_filename_from_shitemid(current, NULL) + 1; /* For the '/' */
776 current = ILGetNext(current);
779 /* Build the path and compute the attributes */
780 This->m_dwAttributes =
781 dwAttributes|SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME;
782 This->m_pszPath = pNextDir = SHAlloc(dwPathLen);
783 if (!This->m_pszPath) {
784 WARN("SHAlloc failed!\n");
785 return E_FAIL;
787 current = pidlSubFolder;
788 strcpy(pNextDir, szBasePath);
789 pNextDir += strlen(szBasePath);
790 if (This->m_dwPathMode == PATHMODE_UNIX || IsEqualCLSID(&CLSID_MyDocuments, This->m_pCLSID))
791 This->m_dwAttributes |= SFGAO_FILESYSTEM;
792 while (!_ILIsEmpty(current)) {
793 pNextDir += UNIXFS_filename_from_shitemid(current, pNextDir);
794 *pNextDir++ = '/';
795 current = ILGetNext(current);
797 *pNextDir='\0';
799 if (!(This->m_dwAttributes & SFGAO_FILESYSTEM) &&
800 ((dos_name = wine_get_dos_file_name(This->m_pszPath))))
802 This->m_dwAttributes |= SFGAO_FILESYSTEM;
803 heap_free( dos_name );
806 return S_OK;
809 /******************************************************************************
810 * UNIXFS_copy [Internal]
812 * Copy pwszDosSrc to pwszDosDst.
814 * PARAMS
815 * pwszDosSrc [I] absolute path of the source
816 * pwszDosDst [I] absolute path of the destination
818 * RETURNS
819 * Success: S_OK,
820 * Failure: E_FAIL
822 static HRESULT UNIXFS_copy(LPCWSTR pwszDosSrc, LPCWSTR pwszDosDst)
824 SHFILEOPSTRUCTW op;
825 LPWSTR pwszSrc, pwszDst;
826 HRESULT res = E_OUTOFMEMORY;
827 UINT iSrcLen, iDstLen;
829 if (!pwszDosSrc || !pwszDosDst)
830 return E_FAIL;
832 iSrcLen = lstrlenW(pwszDosSrc);
833 iDstLen = lstrlenW(pwszDosDst);
834 pwszSrc = heap_alloc((iSrcLen + 2) * sizeof(WCHAR));
835 pwszDst = heap_alloc((iDstLen + 2) * sizeof(WCHAR));
837 if (pwszSrc && pwszDst) {
838 lstrcpyW(pwszSrc, pwszDosSrc);
839 lstrcpyW(pwszDst, pwszDosDst);
840 /* double null termination */
841 pwszSrc[iSrcLen + 1] = 0;
842 pwszDst[iDstLen + 1] = 0;
844 ZeroMemory(&op, sizeof(op));
845 op.hwnd = GetActiveWindow();
846 op.wFunc = FO_COPY;
847 op.pFrom = pwszSrc;
848 op.pTo = pwszDst;
849 op.fFlags = FOF_ALLOWUNDO;
850 if (SHFileOperationW(&op))
852 WARN("SHFileOperationW failed\n");
853 res = E_FAIL;
855 else
856 res = S_OK;
859 heap_free(pwszSrc);
860 heap_free(pwszDst);
861 return res;
864 /******************************************************************************
865 * UnixFolder
867 * Class whose heap based instances represent unix filesystem directories.
870 static void UnixFolder_Destroy(UnixFolder *pUnixFolder) {
871 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
873 SHFree(pUnixFolder->m_pszPath);
874 ILFree(pUnixFolder->m_pidlLocation);
875 SHFree(pUnixFolder);
878 static HRESULT WINAPI ShellFolder2_QueryInterface(IShellFolder2 *iface, REFIID riid,
879 void **ppv)
881 UnixFolder *This = impl_from_IShellFolder2(iface);
883 TRACE("(%p)->(%s %p)\n", This, shdebugstr_guid(riid), ppv);
885 if (!ppv) return E_INVALIDARG;
887 if (IsEqualIID(&IID_IUnknown, riid) ||
888 IsEqualIID(&IID_IShellFolder, riid) ||
889 IsEqualIID(&IID_IShellFolder2, riid))
891 *ppv = &This->IShellFolder2_iface;
892 } else if (IsEqualIID(&IID_IPersistFolder3, riid) ||
893 IsEqualIID(&IID_IPersistFolder2, riid) ||
894 IsEqualIID(&IID_IPersistFolder, riid) ||
895 IsEqualIID(&IID_IPersist, riid))
897 *ppv = &This->IPersistFolder3_iface;
898 } else if (IsEqualIID(&IID_IPersistPropertyBag, riid)) {
899 *ppv = &This->IPersistPropertyBag_iface;
900 } else if (IsEqualIID(&IID_ISFHelper, riid)) {
901 *ppv = &This->ISFHelper_iface;
902 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
903 *ppv = &This->IDropTarget_iface;
904 if (!cfShellIDList)
905 cfShellIDList = RegisterClipboardFormatW(CFSTR_SHELLIDLISTW);
906 } else {
907 *ppv = NULL;
908 TRACE("Unimplemented interface %s\n", shdebugstr_guid(riid));
909 return E_NOINTERFACE;
912 IUnknown_AddRef((IUnknown*)*ppv);
913 return S_OK;
916 static ULONG WINAPI ShellFolder2_AddRef(IShellFolder2 *iface)
918 UnixFolder *This = impl_from_IShellFolder2(iface);
919 ULONG ref = InterlockedIncrement(&This->ref);
920 TRACE("(%p)->(%u)\n", This, ref);
921 return ref;
924 static ULONG WINAPI ShellFolder2_Release(IShellFolder2 *iface)
926 UnixFolder *This = impl_from_IShellFolder2(iface);
927 ULONG ref = InterlockedDecrement(&This->ref);
929 TRACE("(%p)->(%u)\n", This, ref);
931 if (!ref)
932 UnixFolder_Destroy(This);
934 return ref;
937 static HRESULT WINAPI ShellFolder2_ParseDisplayName(IShellFolder2* iface, HWND hwndOwner,
938 LPBC pbc, LPOLESTR display_name, ULONG* pchEaten, LPITEMIDLIST* ppidl,
939 ULONG* attrs)
941 UnixFolder *This = impl_from_IShellFolder2(iface);
942 HRESULT result;
944 TRACE("(%p)->(%p %p %s %p %p %p)\n", This, hwndOwner, pbc, debugstr_w(display_name),
945 pchEaten, ppidl, attrs);
947 result = UNIXFS_path_to_pidl(This, pbc, display_name, ppidl);
948 if (SUCCEEDED(result) && attrs && *attrs)
950 IShellFolder *parent;
951 LPCITEMIDLIST pidlLast;
952 LPITEMIDLIST pidlComplete = ILCombine(This->m_pidlLocation, *ppidl);
953 HRESULT hr;
955 hr = SHBindToParent(pidlComplete, &IID_IShellFolder, (void**)&parent, &pidlLast);
956 if (FAILED(hr)) {
957 FIXME("SHBindToParent failed! hr = 0x%08x\n", hr);
958 ILFree(pidlComplete);
959 return E_FAIL;
961 IShellFolder_GetAttributesOf(parent, 1, &pidlLast, attrs);
962 IShellFolder_Release(parent);
963 ILFree(pidlComplete);
966 if (FAILED(result)) TRACE("FAILED!\n");
967 return result;
970 static IEnumIDList *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter);
972 static HRESULT WINAPI ShellFolder2_EnumObjects(IShellFolder2* iface, HWND hwndOwner,
973 SHCONTF grfFlags, IEnumIDList** ppEnumIDList)
975 UnixFolder *This = impl_from_IShellFolder2(iface);
977 TRACE("(%p)->(%p 0x%08x %p)\n", This, hwndOwner, grfFlags, ppEnumIDList);
979 if (!This->m_pszPath) {
980 WARN("EnumObjects called on uninitialized UnixFolder-object!\n");
981 return E_UNEXPECTED;
984 *ppEnumIDList = UnixSubFolderIterator_Constructor(This, grfFlags);
985 return S_OK;
988 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, const CLSID *pCLSID);
990 static HRESULT WINAPI ShellFolder2_BindToObject(IShellFolder2* iface, LPCITEMIDLIST pidl,
991 LPBC pbcReserved, REFIID riid, void** ppvOut)
993 UnixFolder *This = impl_from_IShellFolder2(iface);
994 IPersistFolder3 *persistFolder;
995 const CLSID *clsidChild;
996 HRESULT hr;
998 TRACE("(%p)->(%p %p %s %p)\n", This, pidl, pbcReserved, debugstr_guid(riid), ppvOut);
1000 if (_ILIsEmpty(pidl))
1001 return E_INVALIDARG;
1003 /* Don't bind to files */
1004 if (_ILIsValue(ILFindLastID(pidl)))
1005 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1007 if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
1008 /* Children of FolderShortcuts are ShellFSFolders on Windows.
1009 * Unixfs' counterpart is UnixDosFolder. */
1010 clsidChild = &CLSID_UnixDosFolder;
1011 } else {
1012 clsidChild = This->m_pCLSID;
1015 hr = CreateUnixFolder(NULL, &IID_IPersistFolder3, (void**)&persistFolder, clsidChild);
1016 if (FAILED(hr)) return hr;
1017 hr = IPersistFolder3_QueryInterface(persistFolder, riid, ppvOut);
1019 if (SUCCEEDED(hr)) {
1020 UnixFolder *subfolder = impl_from_IPersistFolder3(persistFolder);
1021 subfolder->m_pidlLocation = ILCombine(This->m_pidlLocation, pidl);
1022 hr = UNIXFS_initialize_target_folder(subfolder, This->m_pszPath, pidl,
1023 This->m_dwAttributes & SFGAO_FILESYSTEM);
1026 IPersistFolder3_Release(persistFolder);
1028 return hr;
1031 static HRESULT WINAPI ShellFolder2_BindToStorage(IShellFolder2* iface, LPCITEMIDLIST pidl,
1032 LPBC pbcReserved, REFIID riid, void** ppvObj)
1034 UnixFolder *This = impl_from_IShellFolder2(iface);
1035 FIXME("(%p)->(%p %p %s %p): stub\n", This, pidl, pbcReserved, debugstr_guid(riid), ppvObj);
1036 return E_NOTIMPL;
1039 static HRESULT WINAPI ShellFolder2_CompareIDs(IShellFolder2* iface, LPARAM lParam,
1040 LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
1042 UnixFolder *This = impl_from_IShellFolder2(iface);
1043 BOOL isEmpty1, isEmpty2;
1044 HRESULT hr = E_FAIL;
1045 LPCITEMIDLIST firstpidl;
1046 IShellFolder2 *psf;
1047 int compare;
1049 TRACE("(%p)->(%ld %p %p)\n", This, lParam, pidl1, pidl2);
1051 isEmpty1 = _ILIsEmpty(pidl1);
1052 isEmpty2 = _ILIsEmpty(pidl2);
1054 if (isEmpty1 && isEmpty2)
1055 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
1056 else if (isEmpty1)
1057 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1058 else if (isEmpty2)
1059 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1061 compare = CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
1062 _ILGetTextPointer(pidl1), -1,
1063 _ILGetTextPointer(pidl2), -1);
1065 if ((compare != CSTR_EQUAL) && _ILIsFolder(pidl1) && !_ILIsFolder(pidl2))
1066 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1067 if ((compare != CSTR_EQUAL) && !_ILIsFolder(pidl1) && _ILIsFolder(pidl2))
1068 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1070 if ((compare == CSTR_LESS_THAN) || (compare == CSTR_GREATER_THAN))
1071 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)((compare == CSTR_LESS_THAN)?-1:1));
1073 if (pidl1->mkid.cb < pidl2->mkid.cb)
1074 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1075 else if (pidl1->mkid.cb > pidl2->mkid.cb)
1076 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1078 firstpidl = pidl1;
1079 pidl1 = ILGetNext(pidl1);
1080 pidl2 = ILGetNext(pidl2);
1082 isEmpty1 = _ILIsEmpty(pidl1);
1083 isEmpty2 = _ILIsEmpty(pidl2);
1085 if (isEmpty1 && isEmpty2)
1086 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
1087 else if (isEmpty1)
1088 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1089 else if (isEmpty2)
1090 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1091 else if (SUCCEEDED(IShellFolder2_BindToObject(iface, firstpidl, NULL, &IID_IShellFolder, (void**)&psf))) {
1092 hr = IShellFolder2_CompareIDs(psf, lParam, pidl1, pidl2);
1093 IShellFolder2_Release(psf);
1096 return hr;
1099 static HRESULT WINAPI ShellFolder2_CreateViewObject(IShellFolder2* iface, HWND hwndOwner,
1100 REFIID riid, void** ppv)
1102 UnixFolder *This = impl_from_IShellFolder2(iface);
1103 HRESULT hr = E_INVALIDARG;
1105 TRACE("(%p)->(%p %s %p)\n", This, hwndOwner, debugstr_guid(riid), ppv);
1107 if (!ppv) return E_INVALIDARG;
1108 *ppv = NULL;
1110 if (IsEqualIID(&IID_IShellView, riid)) {
1111 IShellView *view;
1113 view = IShellView_Constructor((IShellFolder*)iface);
1114 if (view) {
1115 hr = IShellView_QueryInterface(view, riid, ppv);
1116 IShellView_Release(view);
1118 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
1119 hr = IShellFolder2_QueryInterface(iface, &IID_IDropTarget, ppv);
1122 return hr;
1125 static HRESULT WINAPI ShellFolder2_GetAttributesOf(IShellFolder2* iface, UINT cidl,
1126 LPCITEMIDLIST* apidl, SFGAOF* attrs)
1128 UnixFolder *This = impl_from_IShellFolder2(iface);
1129 HRESULT hr = S_OK;
1131 TRACE("(%p)->(%u %p %p)\n", This, cidl, apidl, attrs);
1133 if (!attrs || (cidl && !apidl))
1134 return E_INVALIDARG;
1136 if (cidl == 0) {
1137 *attrs &= This->m_dwAttributes;
1138 } else {
1139 char szAbsolutePath[FILENAME_MAX], *pszRelativePath;
1140 UINT i;
1142 *attrs = SFGAO_CANCOPY | SFGAO_CANMOVE | SFGAO_CANLINK | SFGAO_CANRENAME | SFGAO_CANDELETE |
1143 SFGAO_HASPROPSHEET | SFGAO_DROPTARGET | SFGAO_FILESYSTEM | SFGAO_LINK;
1144 lstrcpyA(szAbsolutePath, This->m_pszPath);
1145 pszRelativePath = szAbsolutePath + lstrlenA(szAbsolutePath);
1146 for (i=0; i<cidl; i++) {
1147 if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
1148 WCHAR *dos_name;
1149 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelativePath))
1150 return E_INVALIDARG;
1151 if (!(dos_name = wine_get_dos_file_name( szAbsolutePath )))
1152 *attrs &= ~SFGAO_FILESYSTEM;
1153 else
1154 heap_free( dos_name );
1156 if (_ILIsFolder(apidl[i]))
1157 *attrs |= SFGAO_FOLDER | SFGAO_HASSUBFOLDER | SFGAO_FILESYSANCESTOR |
1158 SFGAO_STORAGEANCESTOR | SFGAO_STORAGE;
1159 else
1160 *attrs |= SFGAO_STREAM;
1161 if ((*attrs & SFGAO_LINK))
1163 char ext[MAX_PATH];
1165 if (!_ILGetExtension(apidl[i], ext, MAX_PATH) || lstrcmpiA(ext, "lnk"))
1166 *attrs &= ~SFGAO_LINK;
1171 return hr;
1174 static HRESULT WINAPI ShellFolder2_GetUIObjectOf(IShellFolder2* iface, HWND hwndOwner,
1175 UINT cidl, LPCITEMIDLIST* apidl, REFIID riid, UINT* prgfInOut, void** ppvOut)
1177 UnixFolder *This = impl_from_IShellFolder2(iface);
1178 HRESULT hr;
1179 UINT i;
1181 TRACE("(%p)->(%p %d %p riid=%s %p %p)\n",
1182 This, hwndOwner, cidl, apidl, debugstr_guid(riid), prgfInOut, ppvOut);
1184 if (!cidl || !apidl || !riid || !ppvOut)
1185 return E_INVALIDARG;
1187 for (i=0; i<cidl; i++)
1188 if (!apidl[i])
1189 return E_INVALIDARG;
1191 if(cidl == 1) {
1192 hr = SHELL32_CreateExtensionUIObject(iface, *apidl, riid, ppvOut);
1193 if(hr != S_FALSE)
1194 return hr;
1197 if (IsEqualIID(&IID_IContextMenu, riid)) {
1198 return ItemMenu_Constructor((IShellFolder*)iface, This->m_pidlLocation, apidl, cidl, riid, ppvOut);
1199 } else if (IsEqualIID(&IID_IDataObject, riid)) {
1200 *ppvOut = IDataObject_Constructor(hwndOwner, This->m_pidlLocation, apidl, cidl);
1201 return S_OK;
1202 } else if (IsEqualIID(&IID_IExtractIconA, riid)) {
1203 LPITEMIDLIST pidl;
1204 if (cidl != 1) return E_INVALIDARG;
1205 pidl = ILCombine(This->m_pidlLocation, apidl[0]);
1206 *ppvOut = IExtractIconA_Constructor(pidl);
1207 SHFree(pidl);
1208 return S_OK;
1209 } else if (IsEqualIID(&IID_IExtractIconW, riid)) {
1210 LPITEMIDLIST pidl;
1211 if (cidl != 1) return E_INVALIDARG;
1212 pidl = ILCombine(This->m_pidlLocation, apidl[0]);
1213 *ppvOut = IExtractIconW_Constructor(pidl);
1214 SHFree(pidl);
1215 return S_OK;
1216 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
1217 if (cidl != 1) return E_INVALIDARG;
1218 return IShellFolder2_BindToObject(iface, apidl[0], NULL, &IID_IDropTarget, ppvOut);
1219 } else if (IsEqualIID(&IID_IShellLinkW, riid)) {
1220 FIXME("IShellLinkW\n");
1221 return E_FAIL;
1222 } else if (IsEqualIID(&IID_IShellLinkA, riid)) {
1223 FIXME("IShellLinkA\n");
1224 return E_FAIL;
1225 } else {
1226 FIXME("Unknown interface %s in GetUIObjectOf\n", debugstr_guid(riid));
1227 return E_NOINTERFACE;
1231 static HRESULT WINAPI ShellFolder2_GetDisplayNameOf(IShellFolder2* iface,
1232 LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET* lpName)
1234 UnixFolder *This = impl_from_IShellFolder2(iface);
1235 SHITEMID emptyIDL = { 0, { 0 } };
1236 HRESULT hr = S_OK;
1238 TRACE("(%p)->(%p 0x%x %p)\n", This, pidl, uFlags, lpName);
1240 if ((GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) &&
1241 (GET_SHGDN_RELATION(uFlags) != SHGDN_INFOLDER))
1243 if (_ILIsEmpty(pidl)) {
1244 lpName->uType = STRRET_WSTR;
1245 if (This->m_dwPathMode == PATHMODE_UNIX) {
1246 UINT len = MultiByteToWideChar(CP_UNIXCP, 0, This->m_pszPath, -1, NULL, 0);
1247 lpName->u.pOleStr = SHAlloc(len * sizeof(WCHAR));
1248 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1249 MultiByteToWideChar(CP_UNIXCP, 0, This->m_pszPath, -1, lpName->u.pOleStr, len);
1250 } else {
1251 LPWSTR pwszDosFileName = wine_get_dos_file_name(This->m_pszPath);
1252 if (!pwszDosFileName) return HRESULT_FROM_WIN32(GetLastError());
1253 lpName->u.pOleStr = SHAlloc((lstrlenW(pwszDosFileName) + 1) * sizeof(WCHAR));
1254 if (!lpName->u.pOleStr) {
1255 heap_free(pwszDosFileName);
1256 return HRESULT_FROM_WIN32(GetLastError());
1258 lstrcpyW(lpName->u.pOleStr, pwszDosFileName);
1259 PathRemoveBackslashW(lpName->u.pOleStr);
1260 heap_free(pwszDosFileName);
1262 } else if (_ILIsValue(pidl)) {
1263 STRRET str;
1264 PWSTR path, file;
1266 /* We are looking for the complete path to a file */
1268 /* Get the complete path for the current folder object */
1269 hr = IShellFolder2_GetDisplayNameOf(iface, (LPITEMIDLIST)&emptyIDL, uFlags, &str);
1270 if (SUCCEEDED(hr)) {
1271 hr = StrRetToStrW(&str, NULL, &path);
1272 if (SUCCEEDED(hr)) {
1274 /* Get the child filename */
1275 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_FORPARSING | SHGDN_INFOLDER, &str);
1276 if (SUCCEEDED(hr)) {
1277 hr = StrRetToStrW(&str, NULL, &file);
1278 if (SUCCEEDED(hr)) {
1279 static const WCHAR slashW = '/';
1280 UINT len_path = strlenW(path), len_file = strlenW(file);
1282 /* Now, combine them */
1283 lpName->uType = STRRET_WSTR;
1284 lpName->u.pOleStr = SHAlloc( (len_path + len_file + 2)*sizeof(WCHAR) );
1285 lstrcpyW(lpName->u.pOleStr, path);
1286 if (This->m_dwPathMode == PATHMODE_UNIX &&
1287 lpName->u.pOleStr[len_path-1] != slashW) {
1288 lpName->u.pOleStr[len_path] = slashW;
1289 lpName->u.pOleStr[len_path+1] = '\0';
1290 } else
1291 PathAddBackslashW(lpName->u.pOleStr);
1292 lstrcatW(lpName->u.pOleStr, file);
1294 CoTaskMemFree(file);
1295 } else
1296 WARN("Failed to convert strret (file)\n");
1298 CoTaskMemFree(path);
1299 } else
1300 WARN("Failed to convert strret (path)\n");
1302 } else {
1303 IShellFolder *pSubFolder;
1305 hr = IShellFolder2_BindToObject(iface, pidl, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1306 if (SUCCEEDED(hr)) {
1307 hr = IShellFolder_GetDisplayNameOf(pSubFolder, (LPITEMIDLIST)&emptyIDL, uFlags, lpName);
1308 IShellFolder_Release(pSubFolder);
1309 } else if (FAILED(hr) && !_ILIsPidlSimple(pidl)) {
1310 LPITEMIDLIST pidl_parent = ILClone(pidl);
1311 LPITEMIDLIST pidl_child = ILFindLastID(pidl);
1313 /* Might be a file, try binding to its parent */
1314 ILRemoveLastID(pidl_parent);
1315 hr = IShellFolder2_BindToObject(iface, pidl_parent, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1316 if (SUCCEEDED(hr)) {
1317 hr = IShellFolder_GetDisplayNameOf(pSubFolder, pidl_child, uFlags, lpName);
1318 IShellFolder_Release(pSubFolder);
1320 ILFree(pidl_parent);
1323 } else {
1324 WCHAR wszFileName[MAX_PATH];
1325 if (!_ILSimpleGetTextW(pidl, wszFileName, MAX_PATH)) return E_INVALIDARG;
1326 lpName->uType = STRRET_WSTR;
1327 lpName->u.pOleStr = SHAlloc((lstrlenW(wszFileName)+1)*sizeof(WCHAR));
1328 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1329 lstrcpyW(lpName->u.pOleStr, wszFileName);
1330 if (!(GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) && This->m_dwPathMode == PATHMODE_DOS &&
1331 !_ILIsFolder(pidl) && wszFileName[0] != '.' && SHELL_FS_HideExtension(wszFileName))
1333 PathRemoveExtensionW(lpName->u.pOleStr);
1337 TRACE("--> %s\n", debugstr_w(lpName->u.pOleStr));
1339 return hr;
1342 static HRESULT WINAPI ShellFolder2_SetNameOf(IShellFolder2* iface, HWND hwnd,
1343 LPCITEMIDLIST pidl, LPCOLESTR lpcwszName, SHGDNF uFlags, LPITEMIDLIST* ppidlOut)
1345 UnixFolder *This = impl_from_IShellFolder2(iface);
1347 static const WCHAR awcInvalidChars[] = { '\\', '/', ':', '*', '?', '"', '<', '>', '|' };
1348 char szSrc[FILENAME_MAX], szDest[FILENAME_MAX];
1349 WCHAR wszSrcRelative[MAX_PATH], *pwszExt = NULL;
1350 unsigned int i;
1351 int cBasePathLen = lstrlenA(This->m_pszPath), cNameLen;
1352 struct stat statDest;
1353 LPITEMIDLIST pidlSrc, pidlDest, pidlRelativeDest;
1354 LPOLESTR lpwszName;
1355 HRESULT hr;
1357 TRACE("(%p)->(%p %p %s 0x%08x %p)\n", This, hwnd, pidl, debugstr_w(lpcwszName), uFlags, ppidlOut);
1359 /* prepare to fail */
1360 if (ppidlOut)
1361 *ppidlOut = NULL;
1363 /* pidl has to contain a single non-empty SHITEMID */
1364 if (_ILIsDesktop(pidl) || !_ILIsPidlSimple(pidl) || !_ILGetTextPointer(pidl))
1365 return E_INVALIDARG;
1367 /* check for invalid characters in lpcwszName. */
1368 for (i=0; i < ARRAY_SIZE(awcInvalidChars); i++)
1369 if (StrChrW(lpcwszName, awcInvalidChars[i]))
1370 return HRESULT_FROM_WIN32(ERROR_CANCELLED);
1372 /* build source path */
1373 memcpy(szSrc, This->m_pszPath, cBasePathLen);
1374 UNIXFS_filename_from_shitemid(pidl, szSrc + cBasePathLen);
1376 /* build destination path */
1377 memcpy(szDest, This->m_pszPath, cBasePathLen);
1378 WideCharToMultiByte(CP_UNIXCP, 0, lpcwszName, -1, szDest+cBasePathLen,
1379 FILENAME_MAX-cBasePathLen, NULL, NULL);
1381 /* If the filename's extension is hidden to the user, we have to append it. */
1382 if (!(uFlags & SHGDN_FORPARSING) &&
1383 _ILSimpleGetTextW(pidl, wszSrcRelative, MAX_PATH) &&
1384 SHELL_FS_HideExtension(wszSrcRelative))
1386 int cLenDest = strlen(szDest);
1387 pwszExt = PathFindExtensionW(wszSrcRelative);
1388 WideCharToMultiByte(CP_UNIXCP, 0, pwszExt, -1, szDest + cLenDest,
1389 FILENAME_MAX - cLenDest, NULL, NULL);
1392 TRACE("src=%s dest=%s\n", szSrc, szDest);
1394 /* Fail, if destination does already exist */
1395 if (!stat(szDest, &statDest))
1396 return E_FAIL;
1398 /* Rename the file */
1399 if (rename(szSrc, szDest))
1400 return E_FAIL;
1402 /* Build a pidl for the path of the renamed file */
1403 cNameLen = lstrlenW(lpcwszName) + 1;
1404 if(pwszExt)
1405 cNameLen += lstrlenW(pwszExt);
1406 lpwszName = SHAlloc(cNameLen*sizeof(WCHAR)); /* due to const correctness. */
1407 lstrcpyW(lpwszName, lpcwszName);
1408 if(pwszExt)
1409 lstrcatW(lpwszName, pwszExt);
1411 hr = IShellFolder2_ParseDisplayName(iface, NULL, NULL, lpwszName, NULL, &pidlRelativeDest, NULL);
1412 SHFree(lpwszName);
1413 if (FAILED(hr)) {
1414 rename(szDest, szSrc); /* Undo the renaming */
1415 return E_FAIL;
1417 pidlDest = ILCombine(This->m_pidlLocation, pidlRelativeDest);
1418 ILFree(pidlRelativeDest);
1419 pidlSrc = ILCombine(This->m_pidlLocation, pidl);
1421 /* Inform the shell */
1422 if (_ILIsFolder(ILFindLastID(pidlDest)))
1423 SHChangeNotify(SHCNE_RENAMEFOLDER, SHCNF_IDLIST, pidlSrc, pidlDest);
1424 else
1425 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_IDLIST, pidlSrc, pidlDest);
1427 if (ppidlOut)
1428 *ppidlOut = ILClone(ILFindLastID(pidlDest));
1430 ILFree(pidlSrc);
1431 ILFree(pidlDest);
1433 return S_OK;
1436 static HRESULT WINAPI ShellFolder2_EnumSearches(IShellFolder2* iface, IEnumExtraSearch **ppEnum)
1438 UnixFolder *This = impl_from_IShellFolder2(iface);
1439 FIXME("(%p)->(%p): stub\n", This, ppEnum);
1440 return E_NOTIMPL;
1443 static HRESULT WINAPI ShellFolder2_GetDefaultColumn(IShellFolder2* iface, DWORD reserved, ULONG *sort, ULONG *display)
1445 UnixFolder *This = impl_from_IShellFolder2(iface);
1447 TRACE("(%p)->(%#x, %p, %p)\n", This, reserved, sort, display);
1449 return E_NOTIMPL;
1452 static HRESULT WINAPI ShellFolder2_GetDefaultColumnState(IShellFolder2* iface,
1453 UINT column, SHCOLSTATEF *flags)
1455 UnixFolder *This = impl_from_IShellFolder2(iface);
1456 FIXME("(%p)->(%u %p): stub\n", This, column, flags);
1457 return E_NOTIMPL;
1460 static HRESULT WINAPI ShellFolder2_GetDefaultSearchGUID(IShellFolder2* iface, GUID *guid)
1462 UnixFolder *This = impl_from_IShellFolder2(iface);
1463 TRACE("(%p)->(%p)\n", This, guid);
1464 return E_NOTIMPL;
1467 static HRESULT WINAPI ShellFolder2_GetDetailsEx(IShellFolder2* iface,
1468 LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
1470 UnixFolder *This = impl_from_IShellFolder2(iface);
1471 FIXME("(%p)->(%p %p %p): stub\n", This, pidl, pscid, pv);
1472 return E_NOTIMPL;
1475 #define SHELLVIEWCOLUMNS 7
1476 static const shvheader unixfs_header[SHELLVIEWCOLUMNS] = {
1477 { &FMTID_Storage, PID_STG_NAME, IDS_SHV_COLUMN1, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15 },
1478 { &FMTID_Storage, PID_STG_SIZE, IDS_SHV_COLUMN2, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10 },
1479 { &FMTID_Storage, PID_STG_STORAGETYPE, IDS_SHV_COLUMN3, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10 },
1480 { &FMTID_Storage, PID_STG_WRITETIME, IDS_SHV_COLUMN4, SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12 },
1481 { NULL, 0, IDS_SHV_COLUMN5, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 9 },
1482 { NULL, 0, IDS_SHV_COLUMN10, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7 },
1483 { NULL, 0, IDS_SHV_COLUMN11, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7 },
1486 static HRESULT WINAPI ShellFolder2_GetDetailsOf(IShellFolder2* iface,
1487 LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
1489 UnixFolder *This = impl_from_IShellFolder2(iface);
1490 struct passwd *pPasswd;
1491 struct group *pGroup;
1492 struct stat statItem;
1493 HRESULT hr = S_OK;
1495 TRACE("(%p)->(%p %d %p)\n", This, pidl, iColumn, psd);
1497 if (!psd || iColumn >= SHELLVIEWCOLUMNS)
1498 return E_INVALIDARG;
1500 if (!pidl)
1501 return SHELL32_GetColumnDetails(unixfs_header, iColumn, psd);
1503 if (iColumn == 4 || iColumn == 5 || iColumn == 6) {
1504 char szPath[FILENAME_MAX];
1505 strcpy(szPath, This->m_pszPath);
1506 if (!UNIXFS_filename_from_shitemid(pidl, szPath + strlen(szPath)))
1507 return E_INVALIDARG;
1508 if (stat(szPath, &statItem))
1509 return E_INVALIDARG;
1512 psd->str.u.cStr[0] = '\0';
1513 psd->str.uType = STRRET_CSTR;
1515 switch (iColumn) {
1516 case 0:
1517 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL|SHGDN_INFOLDER, &psd->str);
1518 break;
1519 case 1:
1520 _ILGetFileSize(pidl, psd->str.u.cStr, MAX_PATH);
1521 break;
1522 case 2:
1523 _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
1524 break;
1525 case 3:
1526 _ILGetFileDate(pidl, psd->str.u.cStr, MAX_PATH);
1527 break;
1528 case 4:
1529 psd->str.u.cStr[0] = S_ISDIR(statItem.st_mode) ? 'd' : '-';
1530 psd->str.u.cStr[1] = (statItem.st_mode & S_IRUSR) ? 'r' : '-';
1531 psd->str.u.cStr[2] = (statItem.st_mode & S_IWUSR) ? 'w' : '-';
1532 psd->str.u.cStr[3] = (statItem.st_mode & S_IXUSR) ? 'x' : '-';
1533 psd->str.u.cStr[4] = (statItem.st_mode & S_IRGRP) ? 'r' : '-';
1534 psd->str.u.cStr[5] = (statItem.st_mode & S_IWGRP) ? 'w' : '-';
1535 psd->str.u.cStr[6] = (statItem.st_mode & S_IXGRP) ? 'x' : '-';
1536 psd->str.u.cStr[7] = (statItem.st_mode & S_IROTH) ? 'r' : '-';
1537 psd->str.u.cStr[8] = (statItem.st_mode & S_IWOTH) ? 'w' : '-';
1538 psd->str.u.cStr[9] = (statItem.st_mode & S_IXOTH) ? 'x' : '-';
1539 psd->str.u.cStr[10] = '\0';
1540 break;
1541 case 5:
1542 pPasswd = getpwuid(statItem.st_uid);
1543 if (pPasswd) strcpy(psd->str.u.cStr, pPasswd->pw_name);
1544 break;
1545 case 6:
1546 pGroup = getgrgid(statItem.st_gid);
1547 if (pGroup) strcpy(psd->str.u.cStr, pGroup->gr_name);
1548 break;
1551 return hr;
1554 static HRESULT WINAPI ShellFolder2_MapColumnToSCID(IShellFolder2* iface, UINT column, SHCOLUMNID *scid)
1556 UnixFolder *This = impl_from_IShellFolder2(iface);
1558 TRACE("(%p)->(%u %p)\n", This, column, scid);
1560 if (column >= SHELLVIEWCOLUMNS)
1561 return E_INVALIDARG;
1563 return shellfolder_map_column_to_scid(unixfs_header, column, scid);
1566 static const IShellFolder2Vtbl ShellFolder2Vtbl = {
1567 ShellFolder2_QueryInterface,
1568 ShellFolder2_AddRef,
1569 ShellFolder2_Release,
1570 ShellFolder2_ParseDisplayName,
1571 ShellFolder2_EnumObjects,
1572 ShellFolder2_BindToObject,
1573 ShellFolder2_BindToStorage,
1574 ShellFolder2_CompareIDs,
1575 ShellFolder2_CreateViewObject,
1576 ShellFolder2_GetAttributesOf,
1577 ShellFolder2_GetUIObjectOf,
1578 ShellFolder2_GetDisplayNameOf,
1579 ShellFolder2_SetNameOf,
1580 ShellFolder2_GetDefaultSearchGUID,
1581 ShellFolder2_EnumSearches,
1582 ShellFolder2_GetDefaultColumn,
1583 ShellFolder2_GetDefaultColumnState,
1584 ShellFolder2_GetDetailsEx,
1585 ShellFolder2_GetDetailsOf,
1586 ShellFolder2_MapColumnToSCID
1589 static HRESULT WINAPI PersistFolder3_QueryInterface(IPersistFolder3* iface, REFIID riid,
1590 void** ppvObject)
1592 UnixFolder *This = impl_from_IPersistFolder3(iface);
1593 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppvObject);
1596 static ULONG WINAPI PersistFolder3_AddRef(IPersistFolder3* iface)
1598 UnixFolder *This = impl_from_IPersistFolder3(iface);
1599 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
1602 static ULONG WINAPI PersistFolder3_Release(IPersistFolder3* iface)
1604 UnixFolder *This = impl_from_IPersistFolder3(iface);
1605 return IShellFolder2_Release(&This->IShellFolder2_iface);
1608 static HRESULT WINAPI PersistFolder3_GetClassID(IPersistFolder3* iface, CLSID* pClassID)
1610 UnixFolder *This = impl_from_IPersistFolder3(iface);
1612 TRACE("(%p)->(%p)\n", This, pClassID);
1614 if (!pClassID)
1615 return E_INVALIDARG;
1617 *pClassID = *This->m_pCLSID;
1618 return S_OK;
1621 static HRESULT WINAPI PersistFolder3_Initialize(IPersistFolder3* iface, LPCITEMIDLIST pidl)
1623 UnixFolder *This = impl_from_IPersistFolder3(iface);
1624 LPCITEMIDLIST current = pidl;
1625 char szBasePath[FILENAME_MAX] = "/";
1627 TRACE("(%p)->(%p)\n", This, pidl);
1629 /* Find the UnixFolderClass root */
1630 while (current->mkid.cb) {
1631 if ((_ILIsDrive(current) && IsEqualCLSID(This->m_pCLSID, &CLSID_ShellFSFolder)) ||
1632 (_ILIsSpecialFolder(current) && IsEqualCLSID(This->m_pCLSID, _ILGetGUIDPointer(current))))
1634 break;
1636 current = ILGetNext(current);
1639 if (current->mkid.cb) {
1640 if (_ILIsDrive(current)) {
1641 WCHAR wszDrive[] = { '?', ':', '\\', 0 };
1642 wszDrive[0] = (WCHAR)*_ILGetTextPointer(current);
1643 if (!UNIXFS_get_unix_path(wszDrive, szBasePath))
1644 return E_FAIL;
1645 } else if (IsEqualIID(&CLSID_MyDocuments, _ILGetGUIDPointer(current))) {
1646 WCHAR wszMyDocumentsPath[MAX_PATH];
1647 if (!SHGetSpecialFolderPathW(0, wszMyDocumentsPath, CSIDL_PERSONAL, FALSE))
1648 return E_FAIL;
1649 PathAddBackslashW(wszMyDocumentsPath);
1650 if (!UNIXFS_get_unix_path(wszMyDocumentsPath, szBasePath))
1651 return E_FAIL;
1653 current = ILGetNext(current);
1654 } else if (_ILIsDesktop(pidl) || _ILIsValue(pidl) || _ILIsFolder(pidl)) {
1655 /* Path rooted at Desktop */
1656 WCHAR wszDesktopPath[MAX_PATH];
1657 if (!SHGetSpecialFolderPathW(0, wszDesktopPath, CSIDL_DESKTOPDIRECTORY, FALSE))
1658 return E_FAIL;
1659 PathAddBackslashW(wszDesktopPath);
1660 if (!UNIXFS_get_unix_path(wszDesktopPath, szBasePath))
1661 return E_FAIL;
1662 current = pidl;
1663 } else if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
1664 /* FolderShortcuts' Initialize method only sets the ITEMIDLIST, which
1665 * specifies the location in the shell namespace, but leaves the
1666 * target folder (m_pszPath) alone. See unit tests in tests/shlfolder.c */
1667 This->m_pidlLocation = ILClone(pidl);
1668 return S_OK;
1669 } else {
1670 ERR("Unknown pidl type!\n");
1671 pdump(pidl);
1672 return E_INVALIDARG;
1675 This->m_pidlLocation = ILClone(pidl);
1676 return UNIXFS_initialize_target_folder(This, szBasePath, current, 0);
1679 static HRESULT WINAPI PersistFolder3_GetCurFolder(IPersistFolder3* iface, LPITEMIDLIST* ppidl)
1681 UnixFolder *This = impl_from_IPersistFolder3(iface);
1683 TRACE ("(iface=%p, ppidl=%p)\n", iface, ppidl);
1685 if (!ppidl)
1686 return E_POINTER;
1687 *ppidl = ILClone (This->m_pidlLocation);
1688 return S_OK;
1691 static HRESULT WINAPI PersistFolder3_InitializeEx(IPersistFolder3 *iface, IBindCtx *pbc,
1692 LPCITEMIDLIST pidlRoot, const PERSIST_FOLDER_TARGET_INFO *ppfti)
1694 UnixFolder *This = impl_from_IPersistFolder3(iface);
1695 WCHAR wszTargetDosPath[MAX_PATH];
1696 char szTargetPath[FILENAME_MAX] = "";
1698 TRACE("(%p)->(%p %p %p)\n", This, pbc, pidlRoot, ppfti);
1700 /* If no PERSIST_FOLDER_TARGET_INFO is given InitializeEx is equivalent to Initialize. */
1701 if (!ppfti)
1702 return IPersistFolder3_Initialize(iface, pidlRoot);
1704 if (ppfti->csidl != -1) {
1705 if (FAILED(SHGetFolderPathW(0, ppfti->csidl, NULL, 0, wszTargetDosPath)) ||
1706 !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1708 return E_FAIL;
1710 } else if (*ppfti->szTargetParsingName) {
1711 lstrcpyW(wszTargetDosPath, ppfti->szTargetParsingName);
1712 PathAddBackslashW(wszTargetDosPath);
1713 if (!UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath)) {
1714 return E_FAIL;
1716 } else if (ppfti->pidlTargetFolder) {
1717 if (!SHGetPathFromIDListW(ppfti->pidlTargetFolder, wszTargetDosPath) ||
1718 !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1720 return E_FAIL;
1722 } else {
1723 return E_FAIL;
1726 This->m_pszPath = SHAlloc(lstrlenA(szTargetPath)+1);
1727 if (!This->m_pszPath)
1728 return E_FAIL;
1729 lstrcpyA(This->m_pszPath, szTargetPath);
1730 This->m_pidlLocation = ILClone(pidlRoot);
1731 This->m_dwAttributes = (ppfti->dwAttributes != -1) ? ppfti->dwAttributes :
1732 (SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME|SFGAO_FILESYSTEM);
1734 return S_OK;
1737 static HRESULT WINAPI PersistFolder3_GetFolderTargetInfo(IPersistFolder3 *iface,
1738 PERSIST_FOLDER_TARGET_INFO *ppfti)
1740 UnixFolder *This = impl_from_IPersistFolder3(iface);
1741 FIXME("(%p)->(%p): stub\n", This, ppfti);
1742 return E_NOTIMPL;
1745 static const IPersistFolder3Vtbl PersistFolder3Vtbl = {
1746 PersistFolder3_QueryInterface,
1747 PersistFolder3_AddRef,
1748 PersistFolder3_Release,
1749 PersistFolder3_GetClassID,
1750 PersistFolder3_Initialize,
1751 PersistFolder3_GetCurFolder,
1752 PersistFolder3_InitializeEx,
1753 PersistFolder3_GetFolderTargetInfo
1756 static HRESULT WINAPI PersistPropertyBag_QueryInterface(IPersistPropertyBag* iface,
1757 REFIID riid, void** ppv)
1759 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1760 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppv);
1763 static ULONG WINAPI PersistPropertyBag_AddRef(IPersistPropertyBag* iface)
1765 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1766 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
1769 static ULONG WINAPI PersistPropertyBag_Release(IPersistPropertyBag* iface)
1771 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1772 return IShellFolder2_Release(&This->IShellFolder2_iface);
1775 static HRESULT WINAPI PersistPropertyBag_GetClassID(IPersistPropertyBag* iface, CLSID* pClassID)
1777 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1778 return IPersistFolder3_GetClassID(&This->IPersistFolder3_iface, pClassID);
1781 static HRESULT WINAPI PersistPropertyBag_InitNew(IPersistPropertyBag* iface)
1783 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1784 FIXME("(%p): stub\n", This);
1785 return E_NOTIMPL;
1788 static HRESULT WINAPI PersistPropertyBag_Load(IPersistPropertyBag *iface,
1789 IPropertyBag *pPropertyBag, IErrorLog *pErrorLog)
1791 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1793 static const WCHAR wszTarget[] = { 'T','a','r','g','e','t', 0 }, wszNull[] = { 0 };
1794 PERSIST_FOLDER_TARGET_INFO pftiTarget;
1795 VARIANT var;
1796 HRESULT hr;
1798 TRACE("(%p)->(%p %p)\n", This, pPropertyBag, pErrorLog);
1800 if (!pPropertyBag)
1801 return E_POINTER;
1803 /* Get 'Target' property from the property bag. */
1804 V_VT(&var) = VT_BSTR;
1805 hr = IPropertyBag_Read(pPropertyBag, wszTarget, &var, NULL);
1806 if (FAILED(hr))
1807 return E_FAIL;
1808 lstrcpyW(pftiTarget.szTargetParsingName, V_BSTR(&var));
1809 SysFreeString(V_BSTR(&var));
1811 pftiTarget.pidlTargetFolder = NULL;
1812 lstrcpyW(pftiTarget.szNetworkProvider, wszNull);
1813 pftiTarget.dwAttributes = -1;
1814 pftiTarget.csidl = -1;
1816 return IPersistFolder3_InitializeEx(&This->IPersistFolder3_iface, NULL, NULL, &pftiTarget);
1819 static HRESULT WINAPI PersistPropertyBag_Save(IPersistPropertyBag *iface,
1820 IPropertyBag *pPropertyBag, BOOL fClearDirty, BOOL fSaveAllProperties)
1822 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1823 FIXME("(%p): stub\n", This);
1824 return E_NOTIMPL;
1827 static const IPersistPropertyBagVtbl PersistPropertyBagVtbl = {
1828 PersistPropertyBag_QueryInterface,
1829 PersistPropertyBag_AddRef,
1830 PersistPropertyBag_Release,
1831 PersistPropertyBag_GetClassID,
1832 PersistPropertyBag_InitNew,
1833 PersistPropertyBag_Load,
1834 PersistPropertyBag_Save
1837 static HRESULT WINAPI SFHelper_QueryInterface(ISFHelper* iface, REFIID riid, void** ppvObject)
1839 UnixFolder *This = impl_from_ISFHelper(iface);
1840 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppvObject);
1843 static ULONG WINAPI SFHelper_AddRef(ISFHelper* iface)
1845 UnixFolder *This = impl_from_ISFHelper(iface);
1846 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
1849 static ULONG WINAPI SFHelper_Release(ISFHelper* iface)
1851 UnixFolder *This = impl_from_ISFHelper(iface);
1852 return IShellFolder2_Release(&This->IShellFolder2_iface);
1855 static HRESULT WINAPI SFHelper_GetUniqueName(ISFHelper* iface, LPWSTR pwszName, UINT uLen)
1857 UnixFolder *This = impl_from_ISFHelper(iface);
1858 IEnumIDList *pEnum;
1859 HRESULT hr;
1860 LPITEMIDLIST pidlElem;
1861 DWORD dwFetched;
1862 int i;
1863 WCHAR wszNewFolder[25];
1864 static const WCHAR wszFormat[] = { '%','s',' ','%','d',0 };
1866 TRACE("(%p)->(%p %u)\n", This, pwszName, uLen);
1868 LoadStringW(shell32_hInstance, IDS_NEWFOLDER, wszNewFolder, ARRAY_SIZE(wszNewFolder));
1870 if (uLen < ARRAY_SIZE(wszNewFolder) + 3)
1871 return E_INVALIDARG;
1873 hr = IShellFolder2_EnumObjects(&This->IShellFolder2_iface, 0,
1874 SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN, &pEnum);
1875 if (SUCCEEDED(hr)) {
1876 lstrcpynW(pwszName, wszNewFolder, uLen);
1877 IEnumIDList_Reset(pEnum);
1878 i = 2;
1879 while ((IEnumIDList_Next(pEnum, 1, &pidlElem, &dwFetched) == S_OK) && (dwFetched == 1)) {
1880 WCHAR wszTemp[MAX_PATH];
1881 _ILSimpleGetTextW(pidlElem, wszTemp, MAX_PATH);
1882 if (!lstrcmpiW(wszTemp, pwszName)) {
1883 IEnumIDList_Reset(pEnum);
1884 snprintfW(pwszName, uLen, wszFormat, wszNewFolder, i++);
1885 if (i > 99) {
1886 hr = E_FAIL;
1887 break;
1891 IEnumIDList_Release(pEnum);
1893 return hr;
1896 static HRESULT WINAPI SFHelper_AddFolder(ISFHelper* iface, HWND hwnd, LPCWSTR pwszName,
1897 LPITEMIDLIST* ppidlOut)
1899 UnixFolder *This = impl_from_ISFHelper(iface);
1900 char szNewDir[FILENAME_MAX];
1901 int cBaseLen;
1903 TRACE("(%p)->(%p %s %p)\n", This, hwnd, debugstr_w(pwszName), ppidlOut);
1905 if (ppidlOut)
1906 *ppidlOut = NULL;
1908 if (!This->m_pszPath || !(This->m_dwAttributes & SFGAO_FILESYSTEM))
1909 return E_FAIL;
1911 lstrcpynA(szNewDir, This->m_pszPath, FILENAME_MAX);
1912 cBaseLen = lstrlenA(szNewDir);
1913 WideCharToMultiByte(CP_UNIXCP, 0, pwszName, -1, szNewDir+cBaseLen, FILENAME_MAX-cBaseLen, 0, 0);
1915 if (mkdir(szNewDir, 0777)) {
1916 char szMessage[256 + FILENAME_MAX];
1917 char szCaption[256];
1919 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_DENIED, szCaption, ARRAY_SIZE(szCaption));
1920 sprintf(szMessage, szCaption, szNewDir);
1921 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_CAPTION, szCaption, ARRAY_SIZE(szCaption));
1922 MessageBoxA(hwnd, szMessage, szCaption, MB_OK | MB_ICONEXCLAMATION);
1924 return E_FAIL;
1925 } else {
1926 LPITEMIDLIST pidlRelative;
1928 /* Inform the shell */
1929 if (SUCCEEDED(UNIXFS_path_to_pidl(This, NULL, pwszName, &pidlRelative))) {
1930 LPITEMIDLIST pidlAbsolute = ILCombine(This->m_pidlLocation, pidlRelative);
1931 if (ppidlOut)
1932 *ppidlOut = pidlRelative;
1933 else
1934 ILFree(pidlRelative);
1935 SHChangeNotify(SHCNE_MKDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1936 ILFree(pidlAbsolute);
1937 } else return E_FAIL;
1938 return S_OK;
1943 * Delete specified files by converting the path to DOS paths and calling
1944 * SHFileOperationW. If an error occurs it returns an error code. If the paths can't
1945 * be converted, S_FALSE is returned. In such situation DeleteItems will try to delete
1946 * the files using syscalls
1948 static HRESULT UNIXFS_delete_with_shfileop(UnixFolder *This, UINT cidl, const LPCITEMIDLIST *apidl)
1950 char szAbsolute[FILENAME_MAX], *pszRelative;
1951 LPWSTR wszPathsList, wszListPos;
1952 SHFILEOPSTRUCTW op;
1953 HRESULT ret;
1954 UINT i;
1956 lstrcpyA(szAbsolute, This->m_pszPath);
1957 pszRelative = szAbsolute + lstrlenA(szAbsolute);
1959 wszListPos = wszPathsList = heap_alloc(cidl*MAX_PATH*sizeof(WCHAR)+1);
1960 if (wszPathsList == NULL)
1961 return E_OUTOFMEMORY;
1962 for (i=0; i<cidl; i++) {
1963 LPWSTR wszDosPath;
1965 if (!_ILIsFolder(apidl[i]) && !_ILIsValue(apidl[i]))
1966 continue;
1967 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
1969 heap_free(wszPathsList);
1970 return E_INVALIDARG;
1972 wszDosPath = wine_get_dos_file_name(szAbsolute);
1973 if (wszDosPath == NULL || lstrlenW(wszDosPath) >= MAX_PATH)
1975 heap_free(wszPathsList);
1976 heap_free(wszDosPath);
1977 return S_FALSE;
1979 lstrcpyW(wszListPos, wszDosPath);
1980 wszListPos += lstrlenW(wszListPos)+1;
1981 heap_free(wszDosPath);
1983 *wszListPos = 0;
1985 ZeroMemory(&op, sizeof(op));
1986 op.hwnd = GetActiveWindow();
1987 op.wFunc = FO_DELETE;
1988 op.pFrom = wszPathsList;
1989 op.fFlags = FOF_ALLOWUNDO;
1990 if (SHFileOperationW(&op))
1992 WARN("SHFileOperationW failed\n");
1993 ret = E_FAIL;
1995 else
1996 ret = S_OK;
1998 heap_free(wszPathsList);
1999 return ret;
2002 static HRESULT UNIXFS_delete_with_syscalls(UnixFolder *This, UINT cidl, const LPCITEMIDLIST *apidl)
2004 char szAbsolute[FILENAME_MAX], *pszRelative;
2005 static const WCHAR empty[] = {0};
2006 UINT i;
2008 if (!SHELL_ConfirmYesNoW(GetActiveWindow(), ASK_DELETE_SELECTED, empty))
2009 return S_OK;
2011 lstrcpyA(szAbsolute, This->m_pszPath);
2012 pszRelative = szAbsolute + lstrlenA(szAbsolute);
2014 for (i=0; i<cidl; i++) {
2015 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
2016 return E_INVALIDARG;
2017 if (_ILIsFolder(apidl[i])) {
2018 if (rmdir(szAbsolute))
2019 return E_FAIL;
2020 } else if (_ILIsValue(apidl[i])) {
2021 if (unlink(szAbsolute))
2022 return E_FAIL;
2025 return S_OK;
2028 static HRESULT WINAPI SFHelper_DeleteItems(ISFHelper* iface, UINT cidl, LPCITEMIDLIST* apidl)
2030 UnixFolder *This = impl_from_ISFHelper(iface);
2031 char szAbsolute[FILENAME_MAX], *pszRelative;
2032 LPITEMIDLIST pidlAbsolute;
2033 HRESULT hr = S_OK;
2034 UINT i;
2035 struct stat st;
2037 TRACE("(%p)->(%d %p)\n", This, cidl, apidl);
2039 hr = UNIXFS_delete_with_shfileop(This, cidl, apidl);
2040 if (hr == S_FALSE)
2041 hr = UNIXFS_delete_with_syscalls(This, cidl, apidl);
2043 lstrcpyA(szAbsolute, This->m_pszPath);
2044 pszRelative = szAbsolute + lstrlenA(szAbsolute);
2046 /* we need to manually send the notifies if the files doesn't exist */
2047 for (i=0; i<cidl; i++) {
2048 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
2049 continue;
2050 pidlAbsolute = ILCombine(This->m_pidlLocation, apidl[i]);
2051 if (stat(szAbsolute, &st))
2053 if (_ILIsFolder(apidl[i])) {
2054 SHChangeNotify(SHCNE_RMDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
2055 } else if (_ILIsValue(apidl[i])) {
2056 SHChangeNotify(SHCNE_DELETE, SHCNF_IDLIST, pidlAbsolute, NULL);
2059 ILFree(pidlAbsolute);
2062 return hr;
2065 static HRESULT WINAPI SFHelper_CopyItems(ISFHelper* iface, IShellFolder *psfFrom,
2066 UINT cidl, LPCITEMIDLIST *apidl)
2068 UnixFolder *This = impl_from_ISFHelper(iface);
2069 DWORD dwAttributes;
2070 UINT i;
2071 HRESULT hr;
2072 char szAbsoluteDst[FILENAME_MAX], *pszRelativeDst;
2074 TRACE("(%p)->(%p %d %p)\n", This, psfFrom, cidl, apidl);
2076 if (!psfFrom || !cidl || !apidl)
2077 return E_INVALIDARG;
2079 /* All source items have to be filesystem items. */
2080 dwAttributes = SFGAO_FILESYSTEM;
2081 hr = IShellFolder_GetAttributesOf(psfFrom, cidl, apidl, &dwAttributes);
2082 if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM))
2083 return E_INVALIDARG;
2085 lstrcpyA(szAbsoluteDst, This->m_pszPath);
2086 pszRelativeDst = szAbsoluteDst + strlen(szAbsoluteDst);
2088 for (i=0; i<cidl; i++) {
2089 WCHAR wszSrc[MAX_PATH];
2090 char szSrc[FILENAME_MAX];
2091 STRRET strret;
2092 HRESULT res;
2093 WCHAR *pwszDosSrc, *pwszDosDst;
2095 /* Build the unix path of the current source item. */
2096 if (FAILED(IShellFolder_GetDisplayNameOf(psfFrom, apidl[i], SHGDN_FORPARSING, &strret)))
2097 return E_FAIL;
2098 if (FAILED(StrRetToBufW(&strret, apidl[i], wszSrc, MAX_PATH)))
2099 return E_FAIL;
2100 if (!UNIXFS_get_unix_path(wszSrc, szSrc))
2101 return E_FAIL;
2103 /* Build the unix path of the current destination item */
2104 UNIXFS_filename_from_shitemid(apidl[i], pszRelativeDst);
2106 pwszDosSrc = wine_get_dos_file_name(szSrc);
2107 pwszDosDst = wine_get_dos_file_name(szAbsoluteDst);
2109 if (pwszDosSrc && pwszDosDst)
2110 res = UNIXFS_copy(pwszDosSrc, pwszDosDst);
2111 else
2112 res = E_OUTOFMEMORY;
2114 heap_free(pwszDosSrc);
2115 heap_free(pwszDosDst);
2117 if (res != S_OK)
2118 return res;
2120 return S_OK;
2123 static const ISFHelperVtbl SFHelperVtbl = {
2124 SFHelper_QueryInterface,
2125 SFHelper_AddRef,
2126 SFHelper_Release,
2127 SFHelper_GetUniqueName,
2128 SFHelper_AddFolder,
2129 SFHelper_DeleteItems,
2130 SFHelper_CopyItems
2133 static HRESULT WINAPI DropTarget_QueryInterface(IDropTarget* iface, REFIID riid, void** ppvObject)
2135 UnixFolder *This = impl_from_IDropTarget(iface);
2136 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppvObject);
2139 static ULONG WINAPI DropTarget_AddRef(IDropTarget* iface)
2141 UnixFolder *This = impl_from_IDropTarget(iface);
2142 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
2145 static ULONG WINAPI DropTarget_Release(IDropTarget* iface)
2147 UnixFolder *This = impl_from_IDropTarget(iface);
2148 return IShellFolder2_Release(&This->IShellFolder2_iface);
2151 #define HIDA_GetPIDLFolder(pida) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[0])
2152 #define HIDA_GetPIDLItem(pida, i) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[i+1])
2154 static HRESULT WINAPI DropTarget_DragEnter(IDropTarget *iface, IDataObject *pDataObject,
2155 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2157 UnixFolder *This = impl_from_IDropTarget(iface);
2158 FORMATETC format;
2159 STGMEDIUM medium;
2161 TRACE("(%p)->(%p 0x%08x {.x=%d, .y=%d} %p)\n", This, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
2163 if (!pdwEffect || !pDataObject)
2164 return E_INVALIDARG;
2166 /* Compute a mask of supported drop-effects for this shellfolder object and the given data
2167 * object. Dropping is only supported on folders, which represent filesystem locations. One
2168 * can't drop on file objects. And the 'move' drop effect is only supported, if the source
2169 * folder is not identical to the target folder. */
2170 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2171 InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
2172 if ((This->m_dwAttributes & SFGAO_FILESYSTEM) && /* Only drop to filesystem folders */
2173 _ILIsFolder(ILFindLastID(This->m_pidlLocation)) && /* Only drop to folders, not to files */
2174 SUCCEEDED(IDataObject_GetData(pDataObject, &format, &medium))) /* Only ShellIDList format */
2176 LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
2177 This->m_dwDropEffectsMask |= DROPEFFECT_COPY|DROPEFFECT_LINK;
2179 if (pidaShellIDList) { /* Files can only be moved between two different folders */
2180 if (!ILIsEqual(HIDA_GetPIDLFolder(pidaShellIDList), This->m_pidlLocation))
2181 This->m_dwDropEffectsMask |= DROPEFFECT_MOVE;
2182 GlobalUnlock(medium.u.hGlobal);
2186 *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
2188 return S_OK;
2191 static HRESULT WINAPI DropTarget_DragOver(IDropTarget *iface, DWORD dwKeyState,
2192 POINTL pt, DWORD *pdwEffect)
2194 UnixFolder *This = impl_from_IDropTarget(iface);
2196 TRACE("(%p)->(0x%08x {.x=%d, .y=%d} %p)\n", This, dwKeyState, pt.x, pt.y, pdwEffect);
2198 if (!pdwEffect)
2199 return E_INVALIDARG;
2201 *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
2203 return S_OK;
2206 static HRESULT WINAPI DropTarget_DragLeave(IDropTarget *iface)
2208 UnixFolder *This = impl_from_IDropTarget(iface);
2210 TRACE("(%p)\n", This);
2212 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2213 return S_OK;
2216 static HRESULT WINAPI DropTarget_Drop(IDropTarget *iface, IDataObject *pDataObject,
2217 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2219 UnixFolder *This = impl_from_IDropTarget(iface);
2220 FORMATETC format;
2221 STGMEDIUM medium;
2222 HRESULT hr;
2224 TRACE("(%p)->(%p %d {.x=%d, .y=%d} %p) semi-stub\n",
2225 This, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
2227 InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
2228 hr = IDataObject_GetData(pDataObject, &format, &medium);
2229 if (FAILED(hr))
2230 return hr;
2232 if (medium.tymed == TYMED_HGLOBAL) {
2233 IShellFolder *psfSourceFolder, *psfDesktopFolder;
2234 LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
2235 STRRET strret;
2236 UINT i;
2238 if (!pidaShellIDList)
2239 return HRESULT_FROM_WIN32(GetLastError());
2241 hr = SHGetDesktopFolder(&psfDesktopFolder);
2242 if (FAILED(hr)) {
2243 GlobalUnlock(medium.u.hGlobal);
2244 return hr;
2247 hr = IShellFolder_BindToObject(psfDesktopFolder, HIDA_GetPIDLFolder(pidaShellIDList), NULL,
2248 &IID_IShellFolder, (LPVOID*)&psfSourceFolder);
2249 IShellFolder_Release(psfDesktopFolder);
2250 if (FAILED(hr)) {
2251 GlobalUnlock(medium.u.hGlobal);
2252 return hr;
2255 for (i = 0; i < pidaShellIDList->cidl; i++) {
2256 WCHAR wszSourcePath[MAX_PATH];
2258 hr = IShellFolder_GetDisplayNameOf(psfSourceFolder, HIDA_GetPIDLItem(pidaShellIDList, i),
2259 SHGDN_FORPARSING, &strret);
2260 if (FAILED(hr))
2261 break;
2263 hr = StrRetToBufW(&strret, NULL, wszSourcePath, MAX_PATH);
2264 if (FAILED(hr))
2265 break;
2267 switch (*pdwEffect) {
2268 case DROPEFFECT_MOVE:
2269 FIXME("Move %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2270 break;
2271 case DROPEFFECT_COPY:
2272 FIXME("Copy %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2273 break;
2274 case DROPEFFECT_LINK:
2275 FIXME("Link %s from %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2276 break;
2280 IShellFolder_Release(psfSourceFolder);
2281 GlobalUnlock(medium.u.hGlobal);
2282 return hr;
2285 return E_NOTIMPL;
2288 static const IDropTargetVtbl DropTargetVtbl = {
2289 DropTarget_QueryInterface,
2290 DropTarget_AddRef,
2291 DropTarget_Release,
2292 DropTarget_DragEnter,
2293 DropTarget_DragOver,
2294 DropTarget_DragLeave,
2295 DropTarget_Drop
2298 /******************************************************************************
2299 * Unix[Dos]Folder_Constructor [Internal]
2301 * PARAMS
2302 * pUnkOuter [I] Outer class for aggregation. Currently ignored.
2303 * riid [I] Interface asked for by the client.
2304 * ppv [O] Pointer to an riid interface to the UnixFolder object.
2306 * NOTES
2307 * Those are the only functions exported from shfldr_unixfs.c. They are called from
2308 * shellole.c's default class factory and thus have to exhibit a LPFNCREATEINSTANCE
2309 * compatible signature.
2311 * The UnixDosFolder_Constructor sets the dwPathMode member to PATHMODE_DOS. This
2312 * means that paths are converted from dos to unix and back at the interfaces.
2314 static HRESULT CreateUnixFolder(IUnknown *outer, REFIID riid, void **ppv, const CLSID *clsid)
2316 UnixFolder *This;
2317 HRESULT hr;
2319 if (outer) {
2320 FIXME("Aggregation not yet implemented!\n");
2321 return CLASS_E_NOAGGREGATION;
2324 This = SHAlloc((ULONG)sizeof(UnixFolder));
2325 if (!This) return E_OUTOFMEMORY;
2327 This->IShellFolder2_iface.lpVtbl = &ShellFolder2Vtbl;
2328 This->IPersistFolder3_iface.lpVtbl = &PersistFolder3Vtbl;
2329 This->IPersistPropertyBag_iface.lpVtbl = &PersistPropertyBagVtbl;
2330 This->ISFHelper_iface.lpVtbl = &SFHelperVtbl;
2331 This->IDropTarget_iface.lpVtbl = &DropTargetVtbl;
2332 This->ref = 1;
2333 This->m_pszPath = NULL;
2334 This->m_pidlLocation = NULL;
2335 This->m_dwPathMode = IsEqualCLSID(&CLSID_UnixFolder, clsid) ? PATHMODE_UNIX : PATHMODE_DOS;
2336 This->m_dwAttributes = 0;
2337 This->m_pCLSID = clsid;
2338 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2340 hr = IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppv);
2341 IShellFolder2_Release(&This->IShellFolder2_iface);
2343 return hr;
2346 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2347 TRACE("(pUnkOuter=%p, riid=%s, ppv=%p)\n", pUnkOuter, debugstr_guid(riid), ppv);
2348 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixFolder);
2351 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2352 TRACE("(pUnkOuter=%p, riid=%s, ppv=%p)\n", pUnkOuter, debugstr_guid(riid), ppv);
2353 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixDosFolder);
2356 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2357 TRACE("(pUnkOuter=%p, riid=%s, ppv=%p)\n", pUnkOuter, debugstr_guid(riid), ppv);
2358 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_FolderShortcut);
2361 HRESULT WINAPI MyDocuments_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2362 TRACE("(pUnkOuter=%p, riid=%s, ppv=%p)\n", pUnkOuter, debugstr_guid(riid), ppv);
2363 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_MyDocuments);
2366 /******************************************************************************
2367 * UnixSubFolderIterator
2369 * Class whose heap based objects represent iterators over the sub-directories
2370 * of a given UnixFolder object.
2373 /* UnixSubFolderIterator object layout and typedef.
2375 typedef struct _UnixSubFolderIterator {
2376 IEnumIDList IEnumIDList_iface;
2377 LONG ref;
2378 SHCONTF m_fFilter;
2379 DIR *m_dirFolder;
2380 char m_szFolder[FILENAME_MAX];
2381 } UnixSubFolderIterator;
2383 static inline UnixSubFolderIterator *impl_from_IEnumIDList(IEnumIDList *iface)
2385 return CONTAINING_RECORD(iface, UnixSubFolderIterator, IEnumIDList_iface);
2388 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
2389 TRACE("(iterator=%p)\n", iterator);
2391 if (iterator->m_dirFolder)
2392 closedir(iterator->m_dirFolder);
2393 SHFree(iterator);
2396 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface,
2397 REFIID riid, void** ppv)
2399 TRACE("(iface=%p, riid=%s, ppv=%p)\n", iface, debugstr_guid(riid), ppv);
2401 if (!ppv) return E_INVALIDARG;
2403 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
2404 *ppv = iface;
2405 } else {
2406 *ppv = NULL;
2407 return E_NOINTERFACE;
2410 IEnumIDList_AddRef(iface);
2411 return S_OK;
2414 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
2416 UnixSubFolderIterator *This = impl_from_IEnumIDList(iface);
2417 ULONG ref = InterlockedIncrement(&This->ref);
2419 TRACE("(%p) ref=%d\n", This, ref);
2421 return ref;
2424 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
2426 UnixSubFolderIterator *This = impl_from_IEnumIDList(iface);
2427 ULONG ref = InterlockedDecrement(&This->ref);
2429 TRACE("(%p) ref=%d\n", This, ref);
2431 if (!ref)
2432 UnixSubFolderIterator_Destroy(This);
2434 return ref;
2437 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt,
2438 LPITEMIDLIST* rgelt, ULONG* pceltFetched)
2440 UnixSubFolderIterator *This = impl_from_IEnumIDList(iface);
2441 ULONG i = 0;
2443 /* This->m_dirFolder will be NULL if the user doesn't have access rights for the dir. */
2444 if (This->m_dirFolder) {
2445 char *pszRelativePath = This->m_szFolder + lstrlenA(This->m_szFolder);
2446 struct dirent *pDirEntry;
2448 while (i < celt) {
2449 pDirEntry = readdir(This->m_dirFolder);
2450 if (!pDirEntry) break; /* No more entries */
2451 if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
2453 /* Temporarily build absolute path in This->m_szFolder. Then construct a pidl
2454 * and see if it passes the filter.
2456 lstrcpyA(pszRelativePath, pDirEntry->d_name);
2457 rgelt[i] = SHAlloc(
2458 UNIXFS_shitemid_len_from_filename(pszRelativePath, NULL, NULL)+sizeof(USHORT));
2459 if (!UNIXFS_build_shitemid(This->m_szFolder, TRUE, NULL, rgelt[i]) ||
2460 !UNIXFS_is_pidl_of_type(rgelt[i], This->m_fFilter))
2462 SHFree(rgelt[i]);
2463 rgelt[i] = NULL;
2464 continue;
2466 memset(((PBYTE)rgelt[i])+rgelt[i]->mkid.cb, 0, sizeof(USHORT));
2467 i++;
2469 *pszRelativePath = '\0'; /* Restore the original path in This->m_szFolder. */
2472 if (pceltFetched)
2473 *pceltFetched = i;
2475 return (i == 0) ? S_FALSE : S_OK;
2478 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
2480 LPITEMIDLIST *apidl;
2481 ULONG cFetched;
2482 HRESULT hr;
2484 TRACE("(iface=%p, celt=%d)\n", iface, celt);
2486 /* Call IEnumIDList::Next and delete the resulting pidls. */
2487 apidl = SHAlloc(celt * sizeof(LPITEMIDLIST));
2488 hr = IEnumIDList_Next(iface, celt, apidl, &cFetched);
2489 if (SUCCEEDED(hr))
2490 while (cFetched--)
2491 SHFree(apidl[cFetched]);
2492 SHFree(apidl);
2494 return hr;
2497 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
2499 UnixSubFolderIterator *This = impl_from_IEnumIDList(iface);
2501 TRACE("(iface=%p)\n", iface);
2503 if (This->m_dirFolder)
2504 rewinddir(This->m_dirFolder);
2506 return S_OK;
2509 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This,
2510 IEnumIDList** ppenum)
2512 FIXME("stub\n");
2513 return E_NOTIMPL;
2516 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
2518 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
2519 UnixSubFolderIterator_IEnumIDList_QueryInterface,
2520 UnixSubFolderIterator_IEnumIDList_AddRef,
2521 UnixSubFolderIterator_IEnumIDList_Release,
2522 UnixSubFolderIterator_IEnumIDList_Next,
2523 UnixSubFolderIterator_IEnumIDList_Skip,
2524 UnixSubFolderIterator_IEnumIDList_Reset,
2525 UnixSubFolderIterator_IEnumIDList_Clone
2528 static IEnumIDList *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter)
2530 UnixSubFolderIterator *iterator;
2532 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
2534 iterator = SHAlloc(sizeof(*iterator));
2535 iterator->IEnumIDList_iface.lpVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
2536 iterator->ref = 1;
2537 iterator->m_fFilter = fFilter;
2538 iterator->m_dirFolder = opendir(pUnixFolder->m_pszPath);
2539 lstrcpyA(iterator->m_szFolder, pUnixFolder->m_pszPath);
2541 return &iterator->IEnumIDList_iface;
2544 #else /* __MINGW32__ || _MSC_VER */
2546 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2548 return E_NOTIMPL;
2551 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2553 return E_NOTIMPL;
2556 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2558 return E_NOTIMPL;
2561 HRESULT WINAPI MyDocuments_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2563 return E_NOTIMPL;
2566 #endif /* __MINGW32__ || _MSC_VER */
2568 /******************************************************************************
2569 * UNIXFS_is_rooted_at_desktop [Internal]
2571 * Checks if the unixfs namespace extension is rooted at desktop level.
2573 * RETURNS
2574 * TRUE, if unixfs is rooted at desktop level
2575 * FALSE, if not.
2577 BOOL UNIXFS_is_rooted_at_desktop(void) {
2578 HKEY hKey;
2579 WCHAR wszRootedAtDesktop[69 + CHARS_IN_GUID] = {
2580 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2581 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2582 'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
2583 'N','a','m','e','S','p','a','c','e','\\',0 };
2585 if (StringFromGUID2(&CLSID_UnixDosFolder, wszRootedAtDesktop + 69, CHARS_IN_GUID) &&
2586 RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszRootedAtDesktop, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
2588 RegCloseKey(hKey);
2589 return TRUE;
2591 return FALSE;