ntdll: Add a helper for platform-specific threading initialization.
[wine.git] / dlls / shell32 / shfldr_unixfs.c
bloba37b1bf5881a16efcafd3446e6acbb53c7a6952b
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) return HRESULT_FROM_WIN32(GetLastError());
1255 lstrcpyW(lpName->u.pOleStr, pwszDosFileName);
1256 PathRemoveBackslashW(lpName->u.pOleStr);
1257 heap_free(pwszDosFileName);
1259 } else if (_ILIsValue(pidl)) {
1260 STRRET str;
1261 PWSTR path, file;
1263 /* We are looking for the complete path to a file */
1265 /* Get the complete path for the current folder object */
1266 hr = IShellFolder2_GetDisplayNameOf(iface, (LPITEMIDLIST)&emptyIDL, uFlags, &str);
1267 if (SUCCEEDED(hr)) {
1268 hr = StrRetToStrW(&str, NULL, &path);
1269 if (SUCCEEDED(hr)) {
1271 /* Get the child filename */
1272 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_FORPARSING | SHGDN_INFOLDER, &str);
1273 if (SUCCEEDED(hr)) {
1274 hr = StrRetToStrW(&str, NULL, &file);
1275 if (SUCCEEDED(hr)) {
1276 static const WCHAR slashW = '/';
1277 UINT len_path = strlenW(path), len_file = strlenW(file);
1279 /* Now, combine them */
1280 lpName->uType = STRRET_WSTR;
1281 lpName->u.pOleStr = SHAlloc( (len_path + len_file + 2)*sizeof(WCHAR) );
1282 lstrcpyW(lpName->u.pOleStr, path);
1283 if (This->m_dwPathMode == PATHMODE_UNIX &&
1284 lpName->u.pOleStr[len_path-1] != slashW) {
1285 lpName->u.pOleStr[len_path] = slashW;
1286 lpName->u.pOleStr[len_path+1] = '\0';
1287 } else
1288 PathAddBackslashW(lpName->u.pOleStr);
1289 lstrcatW(lpName->u.pOleStr, file);
1291 CoTaskMemFree(file);
1292 } else
1293 WARN("Failed to convert strret (file)\n");
1295 CoTaskMemFree(path);
1296 } else
1297 WARN("Failed to convert strret (path)\n");
1299 } else {
1300 IShellFolder *pSubFolder;
1302 hr = IShellFolder2_BindToObject(iface, pidl, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1303 if (SUCCEEDED(hr)) {
1304 hr = IShellFolder_GetDisplayNameOf(pSubFolder, (LPITEMIDLIST)&emptyIDL, uFlags, lpName);
1305 IShellFolder_Release(pSubFolder);
1306 } else if (FAILED(hr) && !_ILIsPidlSimple(pidl)) {
1307 LPITEMIDLIST pidl_parent = ILClone(pidl);
1308 LPITEMIDLIST pidl_child = ILFindLastID(pidl);
1310 /* Might be a file, try binding to its parent */
1311 ILRemoveLastID(pidl_parent);
1312 hr = IShellFolder2_BindToObject(iface, pidl_parent, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1313 if (SUCCEEDED(hr)) {
1314 hr = IShellFolder_GetDisplayNameOf(pSubFolder, pidl_child, uFlags, lpName);
1315 IShellFolder_Release(pSubFolder);
1317 ILFree(pidl_parent);
1320 } else {
1321 WCHAR wszFileName[MAX_PATH];
1322 if (!_ILSimpleGetTextW(pidl, wszFileName, MAX_PATH)) return E_INVALIDARG;
1323 lpName->uType = STRRET_WSTR;
1324 lpName->u.pOleStr = SHAlloc((lstrlenW(wszFileName)+1)*sizeof(WCHAR));
1325 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1326 lstrcpyW(lpName->u.pOleStr, wszFileName);
1327 if (!(GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) && This->m_dwPathMode == PATHMODE_DOS &&
1328 !_ILIsFolder(pidl) && wszFileName[0] != '.' && SHELL_FS_HideExtension(wszFileName))
1330 PathRemoveExtensionW(lpName->u.pOleStr);
1334 TRACE("--> %s\n", debugstr_w(lpName->u.pOleStr));
1336 return hr;
1339 static HRESULT WINAPI ShellFolder2_SetNameOf(IShellFolder2* iface, HWND hwnd,
1340 LPCITEMIDLIST pidl, LPCOLESTR lpcwszName, SHGDNF uFlags, LPITEMIDLIST* ppidlOut)
1342 UnixFolder *This = impl_from_IShellFolder2(iface);
1344 static const WCHAR awcInvalidChars[] = { '\\', '/', ':', '*', '?', '"', '<', '>', '|' };
1345 char szSrc[FILENAME_MAX], szDest[FILENAME_MAX];
1346 WCHAR wszSrcRelative[MAX_PATH], *pwszExt = NULL;
1347 unsigned int i;
1348 int cBasePathLen = lstrlenA(This->m_pszPath), cNameLen;
1349 struct stat statDest;
1350 LPITEMIDLIST pidlSrc, pidlDest, pidlRelativeDest;
1351 LPOLESTR lpwszName;
1352 HRESULT hr;
1354 TRACE("(%p)->(%p %p %s 0x%08x %p)\n", This, hwnd, pidl, debugstr_w(lpcwszName), uFlags, ppidlOut);
1356 /* prepare to fail */
1357 if (ppidlOut)
1358 *ppidlOut = NULL;
1360 /* pidl has to contain a single non-empty SHITEMID */
1361 if (_ILIsDesktop(pidl) || !_ILIsPidlSimple(pidl) || !_ILGetTextPointer(pidl))
1362 return E_INVALIDARG;
1364 /* check for invalid characters in lpcwszName. */
1365 for (i=0; i < ARRAY_SIZE(awcInvalidChars); i++)
1366 if (StrChrW(lpcwszName, awcInvalidChars[i]))
1367 return HRESULT_FROM_WIN32(ERROR_CANCELLED);
1369 /* build source path */
1370 memcpy(szSrc, This->m_pszPath, cBasePathLen);
1371 UNIXFS_filename_from_shitemid(pidl, szSrc + cBasePathLen);
1373 /* build destination path */
1374 memcpy(szDest, This->m_pszPath, cBasePathLen);
1375 WideCharToMultiByte(CP_UNIXCP, 0, lpcwszName, -1, szDest+cBasePathLen,
1376 FILENAME_MAX-cBasePathLen, NULL, NULL);
1378 /* If the filename's extension is hidden to the user, we have to append it. */
1379 if (!(uFlags & SHGDN_FORPARSING) &&
1380 _ILSimpleGetTextW(pidl, wszSrcRelative, MAX_PATH) &&
1381 SHELL_FS_HideExtension(wszSrcRelative))
1383 int cLenDest = strlen(szDest);
1384 pwszExt = PathFindExtensionW(wszSrcRelative);
1385 WideCharToMultiByte(CP_UNIXCP, 0, pwszExt, -1, szDest + cLenDest,
1386 FILENAME_MAX - cLenDest, NULL, NULL);
1389 TRACE("src=%s dest=%s\n", szSrc, szDest);
1391 /* Fail, if destination does already exist */
1392 if (!stat(szDest, &statDest))
1393 return E_FAIL;
1395 /* Rename the file */
1396 if (rename(szSrc, szDest))
1397 return E_FAIL;
1399 /* Build a pidl for the path of the renamed file */
1400 cNameLen = lstrlenW(lpcwszName) + 1;
1401 if(pwszExt)
1402 cNameLen += lstrlenW(pwszExt);
1403 lpwszName = SHAlloc(cNameLen*sizeof(WCHAR)); /* due to const correctness. */
1404 lstrcpyW(lpwszName, lpcwszName);
1405 if(pwszExt)
1406 lstrcatW(lpwszName, pwszExt);
1408 hr = IShellFolder2_ParseDisplayName(iface, NULL, NULL, lpwszName, NULL, &pidlRelativeDest, NULL);
1409 SHFree(lpwszName);
1410 if (FAILED(hr)) {
1411 rename(szDest, szSrc); /* Undo the renaming */
1412 return E_FAIL;
1414 pidlDest = ILCombine(This->m_pidlLocation, pidlRelativeDest);
1415 ILFree(pidlRelativeDest);
1416 pidlSrc = ILCombine(This->m_pidlLocation, pidl);
1418 /* Inform the shell */
1419 if (_ILIsFolder(ILFindLastID(pidlDest)))
1420 SHChangeNotify(SHCNE_RENAMEFOLDER, SHCNF_IDLIST, pidlSrc, pidlDest);
1421 else
1422 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_IDLIST, pidlSrc, pidlDest);
1424 if (ppidlOut)
1425 *ppidlOut = ILClone(ILFindLastID(pidlDest));
1427 ILFree(pidlSrc);
1428 ILFree(pidlDest);
1430 return S_OK;
1433 static HRESULT WINAPI ShellFolder2_EnumSearches(IShellFolder2* iface, IEnumExtraSearch **ppEnum)
1435 UnixFolder *This = impl_from_IShellFolder2(iface);
1436 FIXME("(%p)->(%p): stub\n", This, ppEnum);
1437 return E_NOTIMPL;
1440 static HRESULT WINAPI ShellFolder2_GetDefaultColumn(IShellFolder2* iface, DWORD reserved, ULONG *sort, ULONG *display)
1442 UnixFolder *This = impl_from_IShellFolder2(iface);
1444 TRACE("(%p)->(%#x, %p, %p)\n", This, reserved, sort, display);
1446 return E_NOTIMPL;
1449 static HRESULT WINAPI ShellFolder2_GetDefaultColumnState(IShellFolder2* iface,
1450 UINT column, SHCOLSTATEF *flags)
1452 UnixFolder *This = impl_from_IShellFolder2(iface);
1453 FIXME("(%p)->(%u %p): stub\n", This, column, flags);
1454 return E_NOTIMPL;
1457 static HRESULT WINAPI ShellFolder2_GetDefaultSearchGUID(IShellFolder2* iface, GUID *guid)
1459 UnixFolder *This = impl_from_IShellFolder2(iface);
1460 TRACE("(%p)->(%p)\n", This, guid);
1461 return E_NOTIMPL;
1464 static HRESULT WINAPI ShellFolder2_GetDetailsEx(IShellFolder2* iface,
1465 LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
1467 UnixFolder *This = impl_from_IShellFolder2(iface);
1468 FIXME("(%p)->(%p %p %p): stub\n", This, pidl, pscid, pv);
1469 return E_NOTIMPL;
1472 #define SHELLVIEWCOLUMNS 7
1473 static const shvheader unixfs_header[SHELLVIEWCOLUMNS] = {
1474 { &FMTID_Storage, PID_STG_NAME, IDS_SHV_COLUMN1, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15 },
1475 { &FMTID_Storage, PID_STG_SIZE, IDS_SHV_COLUMN2, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10 },
1476 { &FMTID_Storage, PID_STG_STORAGETYPE, IDS_SHV_COLUMN3, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10 },
1477 { &FMTID_Storage, PID_STG_WRITETIME, IDS_SHV_COLUMN4, SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12 },
1478 { NULL, 0, IDS_SHV_COLUMN5, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 9 },
1479 { NULL, 0, IDS_SHV_COLUMN10, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7 },
1480 { NULL, 0, IDS_SHV_COLUMN11, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7 },
1483 static HRESULT WINAPI ShellFolder2_GetDetailsOf(IShellFolder2* iface,
1484 LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
1486 UnixFolder *This = impl_from_IShellFolder2(iface);
1487 struct passwd *pPasswd;
1488 struct group *pGroup;
1489 struct stat statItem;
1490 HRESULT hr = S_OK;
1492 TRACE("(%p)->(%p %d %p)\n", This, pidl, iColumn, psd);
1494 if (!psd || iColumn >= SHELLVIEWCOLUMNS)
1495 return E_INVALIDARG;
1497 if (!pidl)
1498 return SHELL32_GetColumnDetails(unixfs_header, iColumn, psd);
1500 if (iColumn == 4 || iColumn == 5 || iColumn == 6) {
1501 char szPath[FILENAME_MAX];
1502 strcpy(szPath, This->m_pszPath);
1503 if (!UNIXFS_filename_from_shitemid(pidl, szPath + strlen(szPath)))
1504 return E_INVALIDARG;
1505 if (stat(szPath, &statItem))
1506 return E_INVALIDARG;
1509 psd->str.u.cStr[0] = '\0';
1510 psd->str.uType = STRRET_CSTR;
1512 switch (iColumn) {
1513 case 0:
1514 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL|SHGDN_INFOLDER, &psd->str);
1515 break;
1516 case 1:
1517 _ILGetFileSize(pidl, psd->str.u.cStr, MAX_PATH);
1518 break;
1519 case 2:
1520 _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
1521 break;
1522 case 3:
1523 _ILGetFileDate(pidl, psd->str.u.cStr, MAX_PATH);
1524 break;
1525 case 4:
1526 psd->str.u.cStr[0] = S_ISDIR(statItem.st_mode) ? 'd' : '-';
1527 psd->str.u.cStr[1] = (statItem.st_mode & S_IRUSR) ? 'r' : '-';
1528 psd->str.u.cStr[2] = (statItem.st_mode & S_IWUSR) ? 'w' : '-';
1529 psd->str.u.cStr[3] = (statItem.st_mode & S_IXUSR) ? 'x' : '-';
1530 psd->str.u.cStr[4] = (statItem.st_mode & S_IRGRP) ? 'r' : '-';
1531 psd->str.u.cStr[5] = (statItem.st_mode & S_IWGRP) ? 'w' : '-';
1532 psd->str.u.cStr[6] = (statItem.st_mode & S_IXGRP) ? 'x' : '-';
1533 psd->str.u.cStr[7] = (statItem.st_mode & S_IROTH) ? 'r' : '-';
1534 psd->str.u.cStr[8] = (statItem.st_mode & S_IWOTH) ? 'w' : '-';
1535 psd->str.u.cStr[9] = (statItem.st_mode & S_IXOTH) ? 'x' : '-';
1536 psd->str.u.cStr[10] = '\0';
1537 break;
1538 case 5:
1539 pPasswd = getpwuid(statItem.st_uid);
1540 if (pPasswd) strcpy(psd->str.u.cStr, pPasswd->pw_name);
1541 break;
1542 case 6:
1543 pGroup = getgrgid(statItem.st_gid);
1544 if (pGroup) strcpy(psd->str.u.cStr, pGroup->gr_name);
1545 break;
1548 return hr;
1551 static HRESULT WINAPI ShellFolder2_MapColumnToSCID(IShellFolder2* iface, UINT column, SHCOLUMNID *scid)
1553 UnixFolder *This = impl_from_IShellFolder2(iface);
1555 TRACE("(%p)->(%u %p)\n", This, column, scid);
1557 if (column >= SHELLVIEWCOLUMNS)
1558 return E_INVALIDARG;
1560 return shellfolder_map_column_to_scid(unixfs_header, column, scid);
1563 static const IShellFolder2Vtbl ShellFolder2Vtbl = {
1564 ShellFolder2_QueryInterface,
1565 ShellFolder2_AddRef,
1566 ShellFolder2_Release,
1567 ShellFolder2_ParseDisplayName,
1568 ShellFolder2_EnumObjects,
1569 ShellFolder2_BindToObject,
1570 ShellFolder2_BindToStorage,
1571 ShellFolder2_CompareIDs,
1572 ShellFolder2_CreateViewObject,
1573 ShellFolder2_GetAttributesOf,
1574 ShellFolder2_GetUIObjectOf,
1575 ShellFolder2_GetDisplayNameOf,
1576 ShellFolder2_SetNameOf,
1577 ShellFolder2_GetDefaultSearchGUID,
1578 ShellFolder2_EnumSearches,
1579 ShellFolder2_GetDefaultColumn,
1580 ShellFolder2_GetDefaultColumnState,
1581 ShellFolder2_GetDetailsEx,
1582 ShellFolder2_GetDetailsOf,
1583 ShellFolder2_MapColumnToSCID
1586 static HRESULT WINAPI PersistFolder3_QueryInterface(IPersistFolder3* iface, REFIID riid,
1587 void** ppvObject)
1589 UnixFolder *This = impl_from_IPersistFolder3(iface);
1590 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppvObject);
1593 static ULONG WINAPI PersistFolder3_AddRef(IPersistFolder3* iface)
1595 UnixFolder *This = impl_from_IPersistFolder3(iface);
1596 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
1599 static ULONG WINAPI PersistFolder3_Release(IPersistFolder3* iface)
1601 UnixFolder *This = impl_from_IPersistFolder3(iface);
1602 return IShellFolder2_Release(&This->IShellFolder2_iface);
1605 static HRESULT WINAPI PersistFolder3_GetClassID(IPersistFolder3* iface, CLSID* pClassID)
1607 UnixFolder *This = impl_from_IPersistFolder3(iface);
1609 TRACE("(%p)->(%p)\n", This, pClassID);
1611 if (!pClassID)
1612 return E_INVALIDARG;
1614 *pClassID = *This->m_pCLSID;
1615 return S_OK;
1618 static HRESULT WINAPI PersistFolder3_Initialize(IPersistFolder3* iface, LPCITEMIDLIST pidl)
1620 UnixFolder *This = impl_from_IPersistFolder3(iface);
1621 LPCITEMIDLIST current = pidl;
1622 char szBasePath[FILENAME_MAX] = "/";
1624 TRACE("(%p)->(%p)\n", This, pidl);
1626 /* Find the UnixFolderClass root */
1627 while (current->mkid.cb) {
1628 if ((_ILIsDrive(current) && IsEqualCLSID(This->m_pCLSID, &CLSID_ShellFSFolder)) ||
1629 (_ILIsSpecialFolder(current) && IsEqualCLSID(This->m_pCLSID, _ILGetGUIDPointer(current))))
1631 break;
1633 current = ILGetNext(current);
1636 if (current->mkid.cb) {
1637 if (_ILIsDrive(current)) {
1638 WCHAR wszDrive[] = { '?', ':', '\\', 0 };
1639 wszDrive[0] = (WCHAR)*_ILGetTextPointer(current);
1640 if (!UNIXFS_get_unix_path(wszDrive, szBasePath))
1641 return E_FAIL;
1642 } else if (IsEqualIID(&CLSID_MyDocuments, _ILGetGUIDPointer(current))) {
1643 WCHAR wszMyDocumentsPath[MAX_PATH];
1644 if (!SHGetSpecialFolderPathW(0, wszMyDocumentsPath, CSIDL_PERSONAL, FALSE))
1645 return E_FAIL;
1646 PathAddBackslashW(wszMyDocumentsPath);
1647 if (!UNIXFS_get_unix_path(wszMyDocumentsPath, szBasePath))
1648 return E_FAIL;
1650 current = ILGetNext(current);
1651 } else if (_ILIsDesktop(pidl) || _ILIsValue(pidl) || _ILIsFolder(pidl)) {
1652 /* Path rooted at Desktop */
1653 WCHAR wszDesktopPath[MAX_PATH];
1654 if (!SHGetSpecialFolderPathW(0, wszDesktopPath, CSIDL_DESKTOPDIRECTORY, FALSE))
1655 return E_FAIL;
1656 PathAddBackslashW(wszDesktopPath);
1657 if (!UNIXFS_get_unix_path(wszDesktopPath, szBasePath))
1658 return E_FAIL;
1659 current = pidl;
1660 } else if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
1661 /* FolderShortcuts' Initialize method only sets the ITEMIDLIST, which
1662 * specifies the location in the shell namespace, but leaves the
1663 * target folder (m_pszPath) alone. See unit tests in tests/shlfolder.c */
1664 This->m_pidlLocation = ILClone(pidl);
1665 return S_OK;
1666 } else {
1667 ERR("Unknown pidl type!\n");
1668 pdump(pidl);
1669 return E_INVALIDARG;
1672 This->m_pidlLocation = ILClone(pidl);
1673 return UNIXFS_initialize_target_folder(This, szBasePath, current, 0);
1676 static HRESULT WINAPI PersistFolder3_GetCurFolder(IPersistFolder3* iface, LPITEMIDLIST* ppidl)
1678 UnixFolder *This = impl_from_IPersistFolder3(iface);
1680 TRACE ("(iface=%p, ppidl=%p)\n", iface, ppidl);
1682 if (!ppidl)
1683 return E_POINTER;
1684 *ppidl = ILClone (This->m_pidlLocation);
1685 return S_OK;
1688 static HRESULT WINAPI PersistFolder3_InitializeEx(IPersistFolder3 *iface, IBindCtx *pbc,
1689 LPCITEMIDLIST pidlRoot, const PERSIST_FOLDER_TARGET_INFO *ppfti)
1691 UnixFolder *This = impl_from_IPersistFolder3(iface);
1692 WCHAR wszTargetDosPath[MAX_PATH];
1693 char szTargetPath[FILENAME_MAX] = "";
1695 TRACE("(%p)->(%p %p %p)\n", This, pbc, pidlRoot, ppfti);
1697 /* If no PERSIST_FOLDER_TARGET_INFO is given InitializeEx is equivalent to Initialize. */
1698 if (!ppfti)
1699 return IPersistFolder3_Initialize(iface, pidlRoot);
1701 if (ppfti->csidl != -1) {
1702 if (FAILED(SHGetFolderPathW(0, ppfti->csidl, NULL, 0, wszTargetDosPath)) ||
1703 !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1705 return E_FAIL;
1707 } else if (*ppfti->szTargetParsingName) {
1708 lstrcpyW(wszTargetDosPath, ppfti->szTargetParsingName);
1709 PathAddBackslashW(wszTargetDosPath);
1710 if (!UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath)) {
1711 return E_FAIL;
1713 } else if (ppfti->pidlTargetFolder) {
1714 if (!SHGetPathFromIDListW(ppfti->pidlTargetFolder, wszTargetDosPath) ||
1715 !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1717 return E_FAIL;
1719 } else {
1720 return E_FAIL;
1723 This->m_pszPath = SHAlloc(lstrlenA(szTargetPath)+1);
1724 if (!This->m_pszPath)
1725 return E_FAIL;
1726 lstrcpyA(This->m_pszPath, szTargetPath);
1727 This->m_pidlLocation = ILClone(pidlRoot);
1728 This->m_dwAttributes = (ppfti->dwAttributes != -1) ? ppfti->dwAttributes :
1729 (SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME|SFGAO_FILESYSTEM);
1731 return S_OK;
1734 static HRESULT WINAPI PersistFolder3_GetFolderTargetInfo(IPersistFolder3 *iface,
1735 PERSIST_FOLDER_TARGET_INFO *ppfti)
1737 UnixFolder *This = impl_from_IPersistFolder3(iface);
1738 FIXME("(%p)->(%p): stub\n", This, ppfti);
1739 return E_NOTIMPL;
1742 static const IPersistFolder3Vtbl PersistFolder3Vtbl = {
1743 PersistFolder3_QueryInterface,
1744 PersistFolder3_AddRef,
1745 PersistFolder3_Release,
1746 PersistFolder3_GetClassID,
1747 PersistFolder3_Initialize,
1748 PersistFolder3_GetCurFolder,
1749 PersistFolder3_InitializeEx,
1750 PersistFolder3_GetFolderTargetInfo
1753 static HRESULT WINAPI PersistPropertyBag_QueryInterface(IPersistPropertyBag* iface,
1754 REFIID riid, void** ppv)
1756 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1757 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppv);
1760 static ULONG WINAPI PersistPropertyBag_AddRef(IPersistPropertyBag* iface)
1762 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1763 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
1766 static ULONG WINAPI PersistPropertyBag_Release(IPersistPropertyBag* iface)
1768 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1769 return IShellFolder2_Release(&This->IShellFolder2_iface);
1772 static HRESULT WINAPI PersistPropertyBag_GetClassID(IPersistPropertyBag* iface, CLSID* pClassID)
1774 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1775 return IPersistFolder3_GetClassID(&This->IPersistFolder3_iface, pClassID);
1778 static HRESULT WINAPI PersistPropertyBag_InitNew(IPersistPropertyBag* iface)
1780 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1781 FIXME("(%p): stub\n", This);
1782 return E_NOTIMPL;
1785 static HRESULT WINAPI PersistPropertyBag_Load(IPersistPropertyBag *iface,
1786 IPropertyBag *pPropertyBag, IErrorLog *pErrorLog)
1788 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1790 static const WCHAR wszTarget[] = { 'T','a','r','g','e','t', 0 }, wszNull[] = { 0 };
1791 PERSIST_FOLDER_TARGET_INFO pftiTarget;
1792 VARIANT var;
1793 HRESULT hr;
1795 TRACE("(%p)->(%p %p)\n", This, pPropertyBag, pErrorLog);
1797 if (!pPropertyBag)
1798 return E_POINTER;
1800 /* Get 'Target' property from the property bag. */
1801 V_VT(&var) = VT_BSTR;
1802 hr = IPropertyBag_Read(pPropertyBag, wszTarget, &var, NULL);
1803 if (FAILED(hr))
1804 return E_FAIL;
1805 lstrcpyW(pftiTarget.szTargetParsingName, V_BSTR(&var));
1806 SysFreeString(V_BSTR(&var));
1808 pftiTarget.pidlTargetFolder = NULL;
1809 lstrcpyW(pftiTarget.szNetworkProvider, wszNull);
1810 pftiTarget.dwAttributes = -1;
1811 pftiTarget.csidl = -1;
1813 return IPersistFolder3_InitializeEx(&This->IPersistFolder3_iface, NULL, NULL, &pftiTarget);
1816 static HRESULT WINAPI PersistPropertyBag_Save(IPersistPropertyBag *iface,
1817 IPropertyBag *pPropertyBag, BOOL fClearDirty, BOOL fSaveAllProperties)
1819 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1820 FIXME("(%p): stub\n", This);
1821 return E_NOTIMPL;
1824 static const IPersistPropertyBagVtbl PersistPropertyBagVtbl = {
1825 PersistPropertyBag_QueryInterface,
1826 PersistPropertyBag_AddRef,
1827 PersistPropertyBag_Release,
1828 PersistPropertyBag_GetClassID,
1829 PersistPropertyBag_InitNew,
1830 PersistPropertyBag_Load,
1831 PersistPropertyBag_Save
1834 static HRESULT WINAPI SFHelper_QueryInterface(ISFHelper* iface, REFIID riid, void** ppvObject)
1836 UnixFolder *This = impl_from_ISFHelper(iface);
1837 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppvObject);
1840 static ULONG WINAPI SFHelper_AddRef(ISFHelper* iface)
1842 UnixFolder *This = impl_from_ISFHelper(iface);
1843 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
1846 static ULONG WINAPI SFHelper_Release(ISFHelper* iface)
1848 UnixFolder *This = impl_from_ISFHelper(iface);
1849 return IShellFolder2_Release(&This->IShellFolder2_iface);
1852 static HRESULT WINAPI SFHelper_GetUniqueName(ISFHelper* iface, LPWSTR pwszName, UINT uLen)
1854 UnixFolder *This = impl_from_ISFHelper(iface);
1855 IEnumIDList *pEnum;
1856 HRESULT hr;
1857 LPITEMIDLIST pidlElem;
1858 DWORD dwFetched;
1859 int i;
1860 WCHAR wszNewFolder[25];
1861 static const WCHAR wszFormat[] = { '%','s',' ','%','d',0 };
1863 TRACE("(%p)->(%p %u)\n", This, pwszName, uLen);
1865 LoadStringW(shell32_hInstance, IDS_NEWFOLDER, wszNewFolder, ARRAY_SIZE(wszNewFolder));
1867 if (uLen < ARRAY_SIZE(wszNewFolder) + 3)
1868 return E_INVALIDARG;
1870 hr = IShellFolder2_EnumObjects(&This->IShellFolder2_iface, 0,
1871 SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN, &pEnum);
1872 if (SUCCEEDED(hr)) {
1873 lstrcpynW(pwszName, wszNewFolder, uLen);
1874 IEnumIDList_Reset(pEnum);
1875 i = 2;
1876 while ((IEnumIDList_Next(pEnum, 1, &pidlElem, &dwFetched) == S_OK) && (dwFetched == 1)) {
1877 WCHAR wszTemp[MAX_PATH];
1878 _ILSimpleGetTextW(pidlElem, wszTemp, MAX_PATH);
1879 if (!lstrcmpiW(wszTemp, pwszName)) {
1880 IEnumIDList_Reset(pEnum);
1881 snprintfW(pwszName, uLen, wszFormat, wszNewFolder, i++);
1882 if (i > 99) {
1883 hr = E_FAIL;
1884 break;
1888 IEnumIDList_Release(pEnum);
1890 return hr;
1893 static HRESULT WINAPI SFHelper_AddFolder(ISFHelper* iface, HWND hwnd, LPCWSTR pwszName,
1894 LPITEMIDLIST* ppidlOut)
1896 UnixFolder *This = impl_from_ISFHelper(iface);
1897 char szNewDir[FILENAME_MAX];
1898 int cBaseLen;
1900 TRACE("(%p)->(%p %s %p)\n", This, hwnd, debugstr_w(pwszName), ppidlOut);
1902 if (ppidlOut)
1903 *ppidlOut = NULL;
1905 if (!This->m_pszPath || !(This->m_dwAttributes & SFGAO_FILESYSTEM))
1906 return E_FAIL;
1908 lstrcpynA(szNewDir, This->m_pszPath, FILENAME_MAX);
1909 cBaseLen = lstrlenA(szNewDir);
1910 WideCharToMultiByte(CP_UNIXCP, 0, pwszName, -1, szNewDir+cBaseLen, FILENAME_MAX-cBaseLen, 0, 0);
1912 if (mkdir(szNewDir, 0777)) {
1913 char szMessage[256 + FILENAME_MAX];
1914 char szCaption[256];
1916 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_DENIED, szCaption, ARRAY_SIZE(szCaption));
1917 sprintf(szMessage, szCaption, szNewDir);
1918 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_CAPTION, szCaption, ARRAY_SIZE(szCaption));
1919 MessageBoxA(hwnd, szMessage, szCaption, MB_OK | MB_ICONEXCLAMATION);
1921 return E_FAIL;
1922 } else {
1923 LPITEMIDLIST pidlRelative;
1925 /* Inform the shell */
1926 if (SUCCEEDED(UNIXFS_path_to_pidl(This, NULL, pwszName, &pidlRelative))) {
1927 LPITEMIDLIST pidlAbsolute = ILCombine(This->m_pidlLocation, pidlRelative);
1928 if (ppidlOut)
1929 *ppidlOut = pidlRelative;
1930 else
1931 ILFree(pidlRelative);
1932 SHChangeNotify(SHCNE_MKDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1933 ILFree(pidlAbsolute);
1934 } else return E_FAIL;
1935 return S_OK;
1940 * Delete specified files by converting the path to DOS paths and calling
1941 * SHFileOperationW. If an error occurs it returns an error code. If the paths can't
1942 * be converted, S_FALSE is returned. In such situation DeleteItems will try to delete
1943 * the files using syscalls
1945 static HRESULT UNIXFS_delete_with_shfileop(UnixFolder *This, UINT cidl, const LPCITEMIDLIST *apidl)
1947 char szAbsolute[FILENAME_MAX], *pszRelative;
1948 LPWSTR wszPathsList, wszListPos;
1949 SHFILEOPSTRUCTW op;
1950 HRESULT ret;
1951 UINT i;
1953 lstrcpyA(szAbsolute, This->m_pszPath);
1954 pszRelative = szAbsolute + lstrlenA(szAbsolute);
1956 wszListPos = wszPathsList = heap_alloc(cidl*MAX_PATH*sizeof(WCHAR)+1);
1957 if (wszPathsList == NULL)
1958 return E_OUTOFMEMORY;
1959 for (i=0; i<cidl; i++) {
1960 LPWSTR wszDosPath;
1962 if (!_ILIsFolder(apidl[i]) && !_ILIsValue(apidl[i]))
1963 continue;
1964 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
1966 heap_free(wszPathsList);
1967 return E_INVALIDARG;
1969 wszDosPath = wine_get_dos_file_name(szAbsolute);
1970 if (wszDosPath == NULL || lstrlenW(wszDosPath) >= MAX_PATH)
1972 heap_free(wszPathsList);
1973 heap_free(wszDosPath);
1974 return S_FALSE;
1976 lstrcpyW(wszListPos, wszDosPath);
1977 wszListPos += lstrlenW(wszListPos)+1;
1978 heap_free(wszDosPath);
1980 *wszListPos = 0;
1982 ZeroMemory(&op, sizeof(op));
1983 op.hwnd = GetActiveWindow();
1984 op.wFunc = FO_DELETE;
1985 op.pFrom = wszPathsList;
1986 op.fFlags = FOF_ALLOWUNDO;
1987 if (SHFileOperationW(&op))
1989 WARN("SHFileOperationW failed\n");
1990 ret = E_FAIL;
1992 else
1993 ret = S_OK;
1995 heap_free(wszPathsList);
1996 return ret;
1999 static HRESULT UNIXFS_delete_with_syscalls(UnixFolder *This, UINT cidl, const LPCITEMIDLIST *apidl)
2001 char szAbsolute[FILENAME_MAX], *pszRelative;
2002 static const WCHAR empty[] = {0};
2003 UINT i;
2005 if (!SHELL_ConfirmYesNoW(GetActiveWindow(), ASK_DELETE_SELECTED, empty))
2006 return S_OK;
2008 lstrcpyA(szAbsolute, This->m_pszPath);
2009 pszRelative = szAbsolute + lstrlenA(szAbsolute);
2011 for (i=0; i<cidl; i++) {
2012 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
2013 return E_INVALIDARG;
2014 if (_ILIsFolder(apidl[i])) {
2015 if (rmdir(szAbsolute))
2016 return E_FAIL;
2017 } else if (_ILIsValue(apidl[i])) {
2018 if (unlink(szAbsolute))
2019 return E_FAIL;
2022 return S_OK;
2025 static HRESULT WINAPI SFHelper_DeleteItems(ISFHelper* iface, UINT cidl, LPCITEMIDLIST* apidl)
2027 UnixFolder *This = impl_from_ISFHelper(iface);
2028 char szAbsolute[FILENAME_MAX], *pszRelative;
2029 LPITEMIDLIST pidlAbsolute;
2030 HRESULT hr = S_OK;
2031 UINT i;
2032 struct stat st;
2034 TRACE("(%p)->(%d %p)\n", This, cidl, apidl);
2036 hr = UNIXFS_delete_with_shfileop(This, cidl, apidl);
2037 if (hr == S_FALSE)
2038 hr = UNIXFS_delete_with_syscalls(This, cidl, apidl);
2040 lstrcpyA(szAbsolute, This->m_pszPath);
2041 pszRelative = szAbsolute + lstrlenA(szAbsolute);
2043 /* we need to manually send the notifies if the files doesn't exist */
2044 for (i=0; i<cidl; i++) {
2045 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
2046 continue;
2047 pidlAbsolute = ILCombine(This->m_pidlLocation, apidl[i]);
2048 if (stat(szAbsolute, &st))
2050 if (_ILIsFolder(apidl[i])) {
2051 SHChangeNotify(SHCNE_RMDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
2052 } else if (_ILIsValue(apidl[i])) {
2053 SHChangeNotify(SHCNE_DELETE, SHCNF_IDLIST, pidlAbsolute, NULL);
2056 ILFree(pidlAbsolute);
2059 return hr;
2062 static HRESULT WINAPI SFHelper_CopyItems(ISFHelper* iface, IShellFolder *psfFrom,
2063 UINT cidl, LPCITEMIDLIST *apidl)
2065 UnixFolder *This = impl_from_ISFHelper(iface);
2066 DWORD dwAttributes;
2067 UINT i;
2068 HRESULT hr;
2069 char szAbsoluteDst[FILENAME_MAX], *pszRelativeDst;
2071 TRACE("(%p)->(%p %d %p)\n", This, psfFrom, cidl, apidl);
2073 if (!psfFrom || !cidl || !apidl)
2074 return E_INVALIDARG;
2076 /* All source items have to be filesystem items. */
2077 dwAttributes = SFGAO_FILESYSTEM;
2078 hr = IShellFolder_GetAttributesOf(psfFrom, cidl, apidl, &dwAttributes);
2079 if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM))
2080 return E_INVALIDARG;
2082 lstrcpyA(szAbsoluteDst, This->m_pszPath);
2083 pszRelativeDst = szAbsoluteDst + strlen(szAbsoluteDst);
2085 for (i=0; i<cidl; i++) {
2086 WCHAR wszSrc[MAX_PATH];
2087 char szSrc[FILENAME_MAX];
2088 STRRET strret;
2089 HRESULT res;
2090 WCHAR *pwszDosSrc, *pwszDosDst;
2092 /* Build the unix path of the current source item. */
2093 if (FAILED(IShellFolder_GetDisplayNameOf(psfFrom, apidl[i], SHGDN_FORPARSING, &strret)))
2094 return E_FAIL;
2095 if (FAILED(StrRetToBufW(&strret, apidl[i], wszSrc, MAX_PATH)))
2096 return E_FAIL;
2097 if (!UNIXFS_get_unix_path(wszSrc, szSrc))
2098 return E_FAIL;
2100 /* Build the unix path of the current destination item */
2101 UNIXFS_filename_from_shitemid(apidl[i], pszRelativeDst);
2103 pwszDosSrc = wine_get_dos_file_name(szSrc);
2104 pwszDosDst = wine_get_dos_file_name(szAbsoluteDst);
2106 if (pwszDosSrc && pwszDosDst)
2107 res = UNIXFS_copy(pwszDosSrc, pwszDosDst);
2108 else
2109 res = E_OUTOFMEMORY;
2111 heap_free(pwszDosSrc);
2112 heap_free(pwszDosDst);
2114 if (res != S_OK)
2115 return res;
2117 return S_OK;
2120 static const ISFHelperVtbl SFHelperVtbl = {
2121 SFHelper_QueryInterface,
2122 SFHelper_AddRef,
2123 SFHelper_Release,
2124 SFHelper_GetUniqueName,
2125 SFHelper_AddFolder,
2126 SFHelper_DeleteItems,
2127 SFHelper_CopyItems
2130 static HRESULT WINAPI DropTarget_QueryInterface(IDropTarget* iface, REFIID riid, void** ppvObject)
2132 UnixFolder *This = impl_from_IDropTarget(iface);
2133 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppvObject);
2136 static ULONG WINAPI DropTarget_AddRef(IDropTarget* iface)
2138 UnixFolder *This = impl_from_IDropTarget(iface);
2139 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
2142 static ULONG WINAPI DropTarget_Release(IDropTarget* iface)
2144 UnixFolder *This = impl_from_IDropTarget(iface);
2145 return IShellFolder2_Release(&This->IShellFolder2_iface);
2148 #define HIDA_GetPIDLFolder(pida) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[0])
2149 #define HIDA_GetPIDLItem(pida, i) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[i+1])
2151 static HRESULT WINAPI DropTarget_DragEnter(IDropTarget *iface, IDataObject *pDataObject,
2152 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2154 UnixFolder *This = impl_from_IDropTarget(iface);
2155 FORMATETC format;
2156 STGMEDIUM medium;
2158 TRACE("(%p)->(%p 0x%08x {.x=%d, .y=%d} %p)\n", This, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
2160 if (!pdwEffect || !pDataObject)
2161 return E_INVALIDARG;
2163 /* Compute a mask of supported drop-effects for this shellfolder object and the given data
2164 * object. Dropping is only supported on folders, which represent filesystem locations. One
2165 * can't drop on file objects. And the 'move' drop effect is only supported, if the source
2166 * folder is not identical to the target folder. */
2167 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2168 InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
2169 if ((This->m_dwAttributes & SFGAO_FILESYSTEM) && /* Only drop to filesystem folders */
2170 _ILIsFolder(ILFindLastID(This->m_pidlLocation)) && /* Only drop to folders, not to files */
2171 SUCCEEDED(IDataObject_GetData(pDataObject, &format, &medium))) /* Only ShellIDList format */
2173 LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
2174 This->m_dwDropEffectsMask |= DROPEFFECT_COPY|DROPEFFECT_LINK;
2176 if (pidaShellIDList) { /* Files can only be moved between two different folders */
2177 if (!ILIsEqual(HIDA_GetPIDLFolder(pidaShellIDList), This->m_pidlLocation))
2178 This->m_dwDropEffectsMask |= DROPEFFECT_MOVE;
2179 GlobalUnlock(medium.u.hGlobal);
2183 *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
2185 return S_OK;
2188 static HRESULT WINAPI DropTarget_DragOver(IDropTarget *iface, DWORD dwKeyState,
2189 POINTL pt, DWORD *pdwEffect)
2191 UnixFolder *This = impl_from_IDropTarget(iface);
2193 TRACE("(%p)->(0x%08x {.x=%d, .y=%d} %p)\n", This, dwKeyState, pt.x, pt.y, pdwEffect);
2195 if (!pdwEffect)
2196 return E_INVALIDARG;
2198 *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
2200 return S_OK;
2203 static HRESULT WINAPI DropTarget_DragLeave(IDropTarget *iface)
2205 UnixFolder *This = impl_from_IDropTarget(iface);
2207 TRACE("(%p)\n", This);
2209 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2210 return S_OK;
2213 static HRESULT WINAPI DropTarget_Drop(IDropTarget *iface, IDataObject *pDataObject,
2214 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2216 UnixFolder *This = impl_from_IDropTarget(iface);
2217 FORMATETC format;
2218 STGMEDIUM medium;
2219 HRESULT hr;
2221 TRACE("(%p)->(%p %d {.x=%d, .y=%d} %p) semi-stub\n",
2222 This, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
2224 InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
2225 hr = IDataObject_GetData(pDataObject, &format, &medium);
2226 if (FAILED(hr))
2227 return hr;
2229 if (medium.tymed == TYMED_HGLOBAL) {
2230 IShellFolder *psfSourceFolder, *psfDesktopFolder;
2231 LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
2232 STRRET strret;
2233 UINT i;
2235 if (!pidaShellIDList)
2236 return HRESULT_FROM_WIN32(GetLastError());
2238 hr = SHGetDesktopFolder(&psfDesktopFolder);
2239 if (FAILED(hr)) {
2240 GlobalUnlock(medium.u.hGlobal);
2241 return hr;
2244 hr = IShellFolder_BindToObject(psfDesktopFolder, HIDA_GetPIDLFolder(pidaShellIDList), NULL,
2245 &IID_IShellFolder, (LPVOID*)&psfSourceFolder);
2246 IShellFolder_Release(psfDesktopFolder);
2247 if (FAILED(hr)) {
2248 GlobalUnlock(medium.u.hGlobal);
2249 return hr;
2252 for (i = 0; i < pidaShellIDList->cidl; i++) {
2253 WCHAR wszSourcePath[MAX_PATH];
2255 hr = IShellFolder_GetDisplayNameOf(psfSourceFolder, HIDA_GetPIDLItem(pidaShellIDList, i),
2256 SHGDN_FORPARSING, &strret);
2257 if (FAILED(hr))
2258 break;
2260 hr = StrRetToBufW(&strret, NULL, wszSourcePath, MAX_PATH);
2261 if (FAILED(hr))
2262 break;
2264 switch (*pdwEffect) {
2265 case DROPEFFECT_MOVE:
2266 FIXME("Move %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2267 break;
2268 case DROPEFFECT_COPY:
2269 FIXME("Copy %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2270 break;
2271 case DROPEFFECT_LINK:
2272 FIXME("Link %s from %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2273 break;
2277 IShellFolder_Release(psfSourceFolder);
2278 GlobalUnlock(medium.u.hGlobal);
2279 return hr;
2282 return E_NOTIMPL;
2285 static const IDropTargetVtbl DropTargetVtbl = {
2286 DropTarget_QueryInterface,
2287 DropTarget_AddRef,
2288 DropTarget_Release,
2289 DropTarget_DragEnter,
2290 DropTarget_DragOver,
2291 DropTarget_DragLeave,
2292 DropTarget_Drop
2295 /******************************************************************************
2296 * Unix[Dos]Folder_Constructor [Internal]
2298 * PARAMS
2299 * pUnkOuter [I] Outer class for aggregation. Currently ignored.
2300 * riid [I] Interface asked for by the client.
2301 * ppv [O] Pointer to an riid interface to the UnixFolder object.
2303 * NOTES
2304 * Those are the only functions exported from shfldr_unixfs.c. They are called from
2305 * shellole.c's default class factory and thus have to exhibit a LPFNCREATEINSTANCE
2306 * compatible signature.
2308 * The UnixDosFolder_Constructor sets the dwPathMode member to PATHMODE_DOS. This
2309 * means that paths are converted from dos to unix and back at the interfaces.
2311 static HRESULT CreateUnixFolder(IUnknown *outer, REFIID riid, void **ppv, const CLSID *clsid)
2313 UnixFolder *This;
2314 HRESULT hr;
2316 if (outer) {
2317 FIXME("Aggregation not yet implemented!\n");
2318 return CLASS_E_NOAGGREGATION;
2321 This = SHAlloc((ULONG)sizeof(UnixFolder));
2322 if (!This) return E_OUTOFMEMORY;
2324 This->IShellFolder2_iface.lpVtbl = &ShellFolder2Vtbl;
2325 This->IPersistFolder3_iface.lpVtbl = &PersistFolder3Vtbl;
2326 This->IPersistPropertyBag_iface.lpVtbl = &PersistPropertyBagVtbl;
2327 This->ISFHelper_iface.lpVtbl = &SFHelperVtbl;
2328 This->IDropTarget_iface.lpVtbl = &DropTargetVtbl;
2329 This->ref = 1;
2330 This->m_pszPath = NULL;
2331 This->m_pidlLocation = NULL;
2332 This->m_dwPathMode = IsEqualCLSID(&CLSID_UnixFolder, clsid) ? PATHMODE_UNIX : PATHMODE_DOS;
2333 This->m_dwAttributes = 0;
2334 This->m_pCLSID = clsid;
2335 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2337 hr = IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppv);
2338 IShellFolder2_Release(&This->IShellFolder2_iface);
2340 return hr;
2343 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2344 TRACE("(pUnkOuter=%p, riid=%s, ppv=%p)\n", pUnkOuter, debugstr_guid(riid), ppv);
2345 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixFolder);
2348 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2349 TRACE("(pUnkOuter=%p, riid=%s, ppv=%p)\n", pUnkOuter, debugstr_guid(riid), ppv);
2350 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixDosFolder);
2353 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2354 TRACE("(pUnkOuter=%p, riid=%s, ppv=%p)\n", pUnkOuter, debugstr_guid(riid), ppv);
2355 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_FolderShortcut);
2358 HRESULT WINAPI MyDocuments_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2359 TRACE("(pUnkOuter=%p, riid=%s, ppv=%p)\n", pUnkOuter, debugstr_guid(riid), ppv);
2360 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_MyDocuments);
2363 /******************************************************************************
2364 * UnixSubFolderIterator
2366 * Class whose heap based objects represent iterators over the sub-directories
2367 * of a given UnixFolder object.
2370 /* UnixSubFolderIterator object layout and typedef.
2372 typedef struct _UnixSubFolderIterator {
2373 IEnumIDList IEnumIDList_iface;
2374 LONG ref;
2375 SHCONTF m_fFilter;
2376 DIR *m_dirFolder;
2377 char m_szFolder[FILENAME_MAX];
2378 } UnixSubFolderIterator;
2380 static inline UnixSubFolderIterator *impl_from_IEnumIDList(IEnumIDList *iface)
2382 return CONTAINING_RECORD(iface, UnixSubFolderIterator, IEnumIDList_iface);
2385 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
2386 TRACE("(iterator=%p)\n", iterator);
2388 if (iterator->m_dirFolder)
2389 closedir(iterator->m_dirFolder);
2390 SHFree(iterator);
2393 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface,
2394 REFIID riid, void** ppv)
2396 TRACE("(iface=%p, riid=%s, ppv=%p)\n", iface, debugstr_guid(riid), ppv);
2398 if (!ppv) return E_INVALIDARG;
2400 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
2401 *ppv = iface;
2402 } else {
2403 *ppv = NULL;
2404 return E_NOINTERFACE;
2407 IEnumIDList_AddRef(iface);
2408 return S_OK;
2411 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
2413 UnixSubFolderIterator *This = impl_from_IEnumIDList(iface);
2414 ULONG ref = InterlockedIncrement(&This->ref);
2416 TRACE("(%p) ref=%d\n", This, ref);
2418 return ref;
2421 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
2423 UnixSubFolderIterator *This = impl_from_IEnumIDList(iface);
2424 ULONG ref = InterlockedDecrement(&This->ref);
2426 TRACE("(%p) ref=%d\n", This, ref);
2428 if (!ref)
2429 UnixSubFolderIterator_Destroy(This);
2431 return ref;
2434 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt,
2435 LPITEMIDLIST* rgelt, ULONG* pceltFetched)
2437 UnixSubFolderIterator *This = impl_from_IEnumIDList(iface);
2438 ULONG i = 0;
2440 /* This->m_dirFolder will be NULL if the user doesn't have access rights for the dir. */
2441 if (This->m_dirFolder) {
2442 char *pszRelativePath = This->m_szFolder + lstrlenA(This->m_szFolder);
2443 struct dirent *pDirEntry;
2445 while (i < celt) {
2446 pDirEntry = readdir(This->m_dirFolder);
2447 if (!pDirEntry) break; /* No more entries */
2448 if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
2450 /* Temporarily build absolute path in This->m_szFolder. Then construct a pidl
2451 * and see if it passes the filter.
2453 lstrcpyA(pszRelativePath, pDirEntry->d_name);
2454 rgelt[i] = SHAlloc(
2455 UNIXFS_shitemid_len_from_filename(pszRelativePath, NULL, NULL)+sizeof(USHORT));
2456 if (!UNIXFS_build_shitemid(This->m_szFolder, TRUE, NULL, rgelt[i]) ||
2457 !UNIXFS_is_pidl_of_type(rgelt[i], This->m_fFilter))
2459 SHFree(rgelt[i]);
2460 rgelt[i] = NULL;
2461 continue;
2463 memset(((PBYTE)rgelt[i])+rgelt[i]->mkid.cb, 0, sizeof(USHORT));
2464 i++;
2466 *pszRelativePath = '\0'; /* Restore the original path in This->m_szFolder. */
2469 if (pceltFetched)
2470 *pceltFetched = i;
2472 return (i == 0) ? S_FALSE : S_OK;
2475 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
2477 LPITEMIDLIST *apidl;
2478 ULONG cFetched;
2479 HRESULT hr;
2481 TRACE("(iface=%p, celt=%d)\n", iface, celt);
2483 /* Call IEnumIDList::Next and delete the resulting pidls. */
2484 apidl = SHAlloc(celt * sizeof(LPITEMIDLIST));
2485 hr = IEnumIDList_Next(iface, celt, apidl, &cFetched);
2486 if (SUCCEEDED(hr))
2487 while (cFetched--)
2488 SHFree(apidl[cFetched]);
2489 SHFree(apidl);
2491 return hr;
2494 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
2496 UnixSubFolderIterator *This = impl_from_IEnumIDList(iface);
2498 TRACE("(iface=%p)\n", iface);
2500 if (This->m_dirFolder)
2501 rewinddir(This->m_dirFolder);
2503 return S_OK;
2506 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This,
2507 IEnumIDList** ppenum)
2509 FIXME("stub\n");
2510 return E_NOTIMPL;
2513 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
2515 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
2516 UnixSubFolderIterator_IEnumIDList_QueryInterface,
2517 UnixSubFolderIterator_IEnumIDList_AddRef,
2518 UnixSubFolderIterator_IEnumIDList_Release,
2519 UnixSubFolderIterator_IEnumIDList_Next,
2520 UnixSubFolderIterator_IEnumIDList_Skip,
2521 UnixSubFolderIterator_IEnumIDList_Reset,
2522 UnixSubFolderIterator_IEnumIDList_Clone
2525 static IEnumIDList *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter)
2527 UnixSubFolderIterator *iterator;
2529 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
2531 iterator = SHAlloc(sizeof(*iterator));
2532 iterator->IEnumIDList_iface.lpVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
2533 iterator->ref = 1;
2534 iterator->m_fFilter = fFilter;
2535 iterator->m_dirFolder = opendir(pUnixFolder->m_pszPath);
2536 lstrcpyA(iterator->m_szFolder, pUnixFolder->m_pszPath);
2538 return &iterator->IEnumIDList_iface;
2541 #else /* __MINGW32__ || _MSC_VER */
2543 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2545 return E_NOTIMPL;
2548 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2550 return E_NOTIMPL;
2553 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2555 return E_NOTIMPL;
2558 HRESULT WINAPI MyDocuments_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2560 return E_NOTIMPL;
2563 #endif /* __MINGW32__ || _MSC_VER */
2565 /******************************************************************************
2566 * UNIXFS_is_rooted_at_desktop [Internal]
2568 * Checks if the unixfs namespace extension is rooted at desktop level.
2570 * RETURNS
2571 * TRUE, if unixfs is rooted at desktop level
2572 * FALSE, if not.
2574 BOOL UNIXFS_is_rooted_at_desktop(void) {
2575 HKEY hKey;
2576 WCHAR wszRootedAtDesktop[69 + CHARS_IN_GUID] = {
2577 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2578 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2579 'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
2580 'N','a','m','e','S','p','a','c','e','\\',0 };
2582 if (StringFromGUID2(&CLSID_UnixDosFolder, wszRootedAtDesktop + 69, CHARS_IN_GUID) &&
2583 RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszRootedAtDesktop, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
2585 RegCloseKey(hKey);
2586 return TRUE;
2588 return FALSE;