webservices: Add a stub implementation of WS_TYPE_ATTRIBUTE_FIELD_MAPPING in the...
[wine.git] / dlls / shell32 / shfldr_unixfs.c
blob9df761fcbc71d37e69fde3f6e3421e15e8eebbab
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 WINE_DEFAULT_DEBUG_CHANNEL(shell);
170 #if !defined(__MINGW32__) && !defined(_MSC_VER)
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 HeapFree(GetProcessHeap(), 0, 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 HeapFree(GetProcessHeap(), 0, 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 HeapFree( GetProcessHeap(), 0, 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 = HeapAlloc(GetProcessHeap(), 0, (iSrcLen + 2) * sizeof(WCHAR));
835 pwszDst = HeapAlloc(GetProcessHeap(), 0, (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 HeapFree(GetProcessHeap(), 0, pwszSrc);
860 HeapFree(GetProcessHeap(), 0, 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;
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 HeapFree( GetProcessHeap(), 0, 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;
1164 return hr;
1167 static HRESULT WINAPI ShellFolder2_GetUIObjectOf(IShellFolder2* iface, HWND hwndOwner,
1168 UINT cidl, LPCITEMIDLIST* apidl, REFIID riid, UINT* prgfInOut, void** ppvOut)
1170 UnixFolder *This = impl_from_IShellFolder2(iface);
1171 HRESULT hr;
1172 UINT i;
1174 TRACE("(%p)->(%p %d %p riid=%s %p %p)\n",
1175 This, hwndOwner, cidl, apidl, debugstr_guid(riid), prgfInOut, ppvOut);
1177 if (!cidl || !apidl || !riid || !ppvOut)
1178 return E_INVALIDARG;
1180 for (i=0; i<cidl; i++)
1181 if (!apidl[i])
1182 return E_INVALIDARG;
1184 if(cidl == 1) {
1185 hr = SHELL32_CreateExtensionUIObject(iface, *apidl, riid, ppvOut);
1186 if(hr != S_FALSE)
1187 return hr;
1190 if (IsEqualIID(&IID_IContextMenu, riid)) {
1191 return ItemMenu_Constructor((IShellFolder*)iface, This->m_pidlLocation, apidl, cidl, riid, ppvOut);
1192 } else if (IsEqualIID(&IID_IDataObject, riid)) {
1193 *ppvOut = IDataObject_Constructor(hwndOwner, This->m_pidlLocation, apidl, cidl);
1194 return S_OK;
1195 } else if (IsEqualIID(&IID_IExtractIconA, riid)) {
1196 LPITEMIDLIST pidl;
1197 if (cidl != 1) return E_INVALIDARG;
1198 pidl = ILCombine(This->m_pidlLocation, apidl[0]);
1199 *ppvOut = IExtractIconA_Constructor(pidl);
1200 SHFree(pidl);
1201 return S_OK;
1202 } else if (IsEqualIID(&IID_IExtractIconW, riid)) {
1203 LPITEMIDLIST pidl;
1204 if (cidl != 1) return E_INVALIDARG;
1205 pidl = ILCombine(This->m_pidlLocation, apidl[0]);
1206 *ppvOut = IExtractIconW_Constructor(pidl);
1207 SHFree(pidl);
1208 return S_OK;
1209 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
1210 if (cidl != 1) return E_INVALIDARG;
1211 return IShellFolder2_BindToObject(iface, apidl[0], NULL, &IID_IDropTarget, ppvOut);
1212 } else if (IsEqualIID(&IID_IShellLinkW, riid)) {
1213 FIXME("IShellLinkW\n");
1214 return E_FAIL;
1215 } else if (IsEqualIID(&IID_IShellLinkA, riid)) {
1216 FIXME("IShellLinkA\n");
1217 return E_FAIL;
1218 } else {
1219 FIXME("Unknown interface %s in GetUIObjectOf\n", debugstr_guid(riid));
1220 return E_NOINTERFACE;
1224 static HRESULT WINAPI ShellFolder2_GetDisplayNameOf(IShellFolder2* iface,
1225 LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET* lpName)
1227 UnixFolder *This = impl_from_IShellFolder2(iface);
1228 SHITEMID emptyIDL = { 0, { 0 } };
1229 HRESULT hr = S_OK;
1231 TRACE("(%p)->(%p 0x%x %p)\n", This, pidl, uFlags, lpName);
1233 if ((GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) &&
1234 (GET_SHGDN_RELATION(uFlags) != SHGDN_INFOLDER))
1236 if (_ILIsEmpty(pidl)) {
1237 lpName->uType = STRRET_WSTR;
1238 if (This->m_dwPathMode == PATHMODE_UNIX) {
1239 UINT len = MultiByteToWideChar(CP_UNIXCP, 0, This->m_pszPath, -1, NULL, 0);
1240 lpName->u.pOleStr = SHAlloc(len * sizeof(WCHAR));
1241 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1242 MultiByteToWideChar(CP_UNIXCP, 0, This->m_pszPath, -1, lpName->u.pOleStr, len);
1243 } else {
1244 LPWSTR pwszDosFileName = wine_get_dos_file_name(This->m_pszPath);
1245 if (!pwszDosFileName) return HRESULT_FROM_WIN32(GetLastError());
1246 lpName->u.pOleStr = SHAlloc((lstrlenW(pwszDosFileName) + 1) * sizeof(WCHAR));
1247 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1248 lstrcpyW(lpName->u.pOleStr, pwszDosFileName);
1249 PathRemoveBackslashW(lpName->u.pOleStr);
1250 HeapFree(GetProcessHeap(), 0, pwszDosFileName);
1252 } else if (_ILIsValue(pidl)) {
1253 STRRET str;
1254 PWSTR path, file;
1256 /* We are looking for the complete path to a file */
1258 /* Get the complete path for the current folder object */
1259 hr = IShellFolder2_GetDisplayNameOf(iface, (LPITEMIDLIST)&emptyIDL, uFlags, &str);
1260 if (SUCCEEDED(hr)) {
1261 hr = StrRetToStrW(&str, NULL, &path);
1262 if (SUCCEEDED(hr)) {
1264 /* Get the child filename */
1265 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_FORPARSING | SHGDN_INFOLDER, &str);
1266 if (SUCCEEDED(hr)) {
1267 hr = StrRetToStrW(&str, NULL, &file);
1268 if (SUCCEEDED(hr)) {
1269 static const WCHAR slashW = '/';
1270 UINT len_path = strlenW(path), len_file = strlenW(file);
1272 /* Now, combine them */
1273 lpName->uType = STRRET_WSTR;
1274 lpName->u.pOleStr = SHAlloc( (len_path + len_file + 2)*sizeof(WCHAR) );
1275 lstrcpyW(lpName->u.pOleStr, path);
1276 if (This->m_dwPathMode == PATHMODE_UNIX &&
1277 lpName->u.pOleStr[len_path-1] != slashW) {
1278 lpName->u.pOleStr[len_path] = slashW;
1279 lpName->u.pOleStr[len_path+1] = '\0';
1280 } else
1281 PathAddBackslashW(lpName->u.pOleStr);
1282 lstrcatW(lpName->u.pOleStr, file);
1284 CoTaskMemFree(file);
1285 } else
1286 WARN("Failed to convert strret (file)\n");
1288 CoTaskMemFree(path);
1289 } else
1290 WARN("Failed to convert strret (path)\n");
1292 } else {
1293 IShellFolder *pSubFolder;
1295 hr = IShellFolder2_BindToObject(iface, pidl, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1296 if (SUCCEEDED(hr)) {
1297 hr = IShellFolder_GetDisplayNameOf(pSubFolder, (LPITEMIDLIST)&emptyIDL, uFlags, lpName);
1298 IShellFolder_Release(pSubFolder);
1299 } else if (FAILED(hr) && !_ILIsPidlSimple(pidl)) {
1300 LPITEMIDLIST pidl_parent = ILClone(pidl);
1301 LPITEMIDLIST pidl_child = ILFindLastID(pidl);
1303 /* Might be a file, try binding to its parent */
1304 ILRemoveLastID(pidl_parent);
1305 hr = IShellFolder2_BindToObject(iface, pidl_parent, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1306 if (SUCCEEDED(hr)) {
1307 hr = IShellFolder_GetDisplayNameOf(pSubFolder, pidl_child, uFlags, lpName);
1308 IShellFolder_Release(pSubFolder);
1310 ILFree(pidl_parent);
1313 } else {
1314 WCHAR wszFileName[MAX_PATH];
1315 if (!_ILSimpleGetTextW(pidl, wszFileName, MAX_PATH)) return E_INVALIDARG;
1316 lpName->uType = STRRET_WSTR;
1317 lpName->u.pOleStr = SHAlloc((lstrlenW(wszFileName)+1)*sizeof(WCHAR));
1318 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1319 lstrcpyW(lpName->u.pOleStr, wszFileName);
1320 if (!(GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) && This->m_dwPathMode == PATHMODE_DOS &&
1321 !_ILIsFolder(pidl) && wszFileName[0] != '.' && SHELL_FS_HideExtension(wszFileName))
1323 PathRemoveExtensionW(lpName->u.pOleStr);
1327 TRACE("--> %s\n", debugstr_w(lpName->u.pOleStr));
1329 return hr;
1332 static HRESULT WINAPI ShellFolder2_SetNameOf(IShellFolder2* iface, HWND hwnd,
1333 LPCITEMIDLIST pidl, LPCOLESTR lpcwszName, SHGDNF uFlags, LPITEMIDLIST* ppidlOut)
1335 UnixFolder *This = impl_from_IShellFolder2(iface);
1337 static const WCHAR awcInvalidChars[] = { '\\', '/', ':', '*', '?', '"', '<', '>', '|' };
1338 char szSrc[FILENAME_MAX], szDest[FILENAME_MAX];
1339 WCHAR wszSrcRelative[MAX_PATH], *pwszExt = NULL;
1340 unsigned int i;
1341 int cBasePathLen = lstrlenA(This->m_pszPath), cNameLen;
1342 struct stat statDest;
1343 LPITEMIDLIST pidlSrc, pidlDest, pidlRelativeDest;
1344 LPOLESTR lpwszName;
1345 HRESULT hr;
1347 TRACE("(%p)->(%p %p %s 0x%08x %p)\n", This, hwnd, pidl, debugstr_w(lpcwszName), uFlags, ppidlOut);
1349 /* prepare to fail */
1350 if (ppidlOut)
1351 *ppidlOut = NULL;
1353 /* pidl has to contain a single non-empty SHITEMID */
1354 if (_ILIsDesktop(pidl) || !_ILIsPidlSimple(pidl) || !_ILGetTextPointer(pidl))
1355 return E_INVALIDARG;
1357 /* check for invalid characters in lpcwszName. */
1358 for (i=0; i < sizeof(awcInvalidChars)/sizeof(*awcInvalidChars); i++)
1359 if (StrChrW(lpcwszName, awcInvalidChars[i]))
1360 return HRESULT_FROM_WIN32(ERROR_CANCELLED);
1362 /* build source path */
1363 memcpy(szSrc, This->m_pszPath, cBasePathLen);
1364 UNIXFS_filename_from_shitemid(pidl, szSrc + cBasePathLen);
1366 /* build destination path */
1367 memcpy(szDest, This->m_pszPath, cBasePathLen);
1368 WideCharToMultiByte(CP_UNIXCP, 0, lpcwszName, -1, szDest+cBasePathLen,
1369 FILENAME_MAX-cBasePathLen, NULL, NULL);
1371 /* If the filename's extension is hidden to the user, we have to append it. */
1372 if (!(uFlags & SHGDN_FORPARSING) &&
1373 _ILSimpleGetTextW(pidl, wszSrcRelative, MAX_PATH) &&
1374 SHELL_FS_HideExtension(wszSrcRelative))
1376 int cLenDest = strlen(szDest);
1377 pwszExt = PathFindExtensionW(wszSrcRelative);
1378 WideCharToMultiByte(CP_UNIXCP, 0, pwszExt, -1, szDest + cLenDest,
1379 FILENAME_MAX - cLenDest, NULL, NULL);
1382 TRACE("src=%s dest=%s\n", szSrc, szDest);
1384 /* Fail, if destination does already exist */
1385 if (!stat(szDest, &statDest))
1386 return E_FAIL;
1388 /* Rename the file */
1389 if (rename(szSrc, szDest))
1390 return E_FAIL;
1392 /* Build a pidl for the path of the renamed file */
1393 cNameLen = lstrlenW(lpcwszName) + 1;
1394 if(pwszExt)
1395 cNameLen += lstrlenW(pwszExt);
1396 lpwszName = SHAlloc(cNameLen*sizeof(WCHAR)); /* due to const correctness. */
1397 lstrcpyW(lpwszName, lpcwszName);
1398 if(pwszExt)
1399 lstrcatW(lpwszName, pwszExt);
1401 hr = IShellFolder2_ParseDisplayName(iface, NULL, NULL, lpwszName, NULL, &pidlRelativeDest, NULL);
1402 SHFree(lpwszName);
1403 if (FAILED(hr)) {
1404 rename(szDest, szSrc); /* Undo the renaming */
1405 return E_FAIL;
1407 pidlDest = ILCombine(This->m_pidlLocation, pidlRelativeDest);
1408 ILFree(pidlRelativeDest);
1409 pidlSrc = ILCombine(This->m_pidlLocation, pidl);
1411 /* Inform the shell */
1412 if (_ILIsFolder(ILFindLastID(pidlDest)))
1413 SHChangeNotify(SHCNE_RENAMEFOLDER, SHCNF_IDLIST, pidlSrc, pidlDest);
1414 else
1415 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_IDLIST, pidlSrc, pidlDest);
1417 if (ppidlOut)
1418 *ppidlOut = ILClone(ILFindLastID(pidlDest));
1420 ILFree(pidlSrc);
1421 ILFree(pidlDest);
1423 return S_OK;
1426 static HRESULT WINAPI ShellFolder2_EnumSearches(IShellFolder2* iface, IEnumExtraSearch **ppEnum)
1428 UnixFolder *This = impl_from_IShellFolder2(iface);
1429 FIXME("(%p)->(%p): stub\n", This, ppEnum);
1430 return E_NOTIMPL;
1433 static HRESULT WINAPI ShellFolder2_GetDefaultColumn(IShellFolder2* iface,
1434 DWORD dwReserved, ULONG *pSort, ULONG *pDisplay)
1436 UnixFolder *This = impl_from_IShellFolder2(iface);
1438 TRACE("(%p)->(0x%x %p %p)\n", This, dwReserved, pSort, pDisplay);
1440 if (pSort)
1441 *pSort = 0;
1442 if (pDisplay)
1443 *pDisplay = 0;
1445 return S_OK;
1448 static HRESULT WINAPI ShellFolder2_GetDefaultColumnState(IShellFolder2* iface,
1449 UINT column, SHCOLSTATEF *flags)
1451 UnixFolder *This = impl_from_IShellFolder2(iface);
1452 FIXME("(%p)->(%u %p): stub\n", This, column, flags);
1453 return E_NOTIMPL;
1456 static HRESULT WINAPI ShellFolder2_GetDefaultSearchGUID(IShellFolder2* iface,
1457 GUID *guid)
1459 UnixFolder *This = impl_from_IShellFolder2(iface);
1460 FIXME("(%p)->(%p): stub\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
1474 static HRESULT WINAPI ShellFolder2_GetDetailsOf(IShellFolder2* iface,
1475 LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
1477 UnixFolder *This = impl_from_IShellFolder2(iface);
1478 HRESULT hr = E_FAIL;
1479 struct passwd *pPasswd;
1480 struct group *pGroup;
1481 struct stat statItem;
1483 static const shvheader unixfs_header[SHELLVIEWCOLUMNS] = {
1484 {IDS_SHV_COLUMN1, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
1485 {IDS_SHV_COLUMN2, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1486 {IDS_SHV_COLUMN3, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1487 {IDS_SHV_COLUMN4, SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12},
1488 {IDS_SHV_COLUMN5, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 9},
1489 {IDS_SHV_COLUMN10, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
1490 {IDS_SHV_COLUMN11, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7}
1493 TRACE("(%p)->(%p %d %p)\n", This, pidl, iColumn, psd);
1495 if (!psd || iColumn >= SHELLVIEWCOLUMNS)
1496 return E_INVALIDARG;
1498 if (!pidl)
1499 return SHELL32_GetColumnDetails(unixfs_header, iColumn, psd);
1501 if (iColumn == 4 || iColumn == 5 || iColumn == 6) {
1502 char szPath[FILENAME_MAX];
1503 strcpy(szPath, This->m_pszPath);
1504 if (!UNIXFS_filename_from_shitemid(pidl, szPath + strlen(szPath)))
1505 return E_INVALIDARG;
1506 if (stat(szPath, &statItem))
1507 return E_INVALIDARG;
1510 psd->str.u.cStr[0] = '\0';
1511 psd->str.uType = STRRET_CSTR;
1513 switch (iColumn) {
1514 case 0:
1515 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL|SHGDN_INFOLDER, &psd->str);
1516 break;
1517 case 1:
1518 _ILGetFileSize(pidl, psd->str.u.cStr, MAX_PATH);
1519 break;
1520 case 2:
1521 _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
1522 break;
1523 case 3:
1524 _ILGetFileDate(pidl, psd->str.u.cStr, MAX_PATH);
1525 break;
1526 case 4:
1527 psd->str.u.cStr[0] = S_ISDIR(statItem.st_mode) ? 'd' : '-';
1528 psd->str.u.cStr[1] = (statItem.st_mode & S_IRUSR) ? 'r' : '-';
1529 psd->str.u.cStr[2] = (statItem.st_mode & S_IWUSR) ? 'w' : '-';
1530 psd->str.u.cStr[3] = (statItem.st_mode & S_IXUSR) ? 'x' : '-';
1531 psd->str.u.cStr[4] = (statItem.st_mode & S_IRGRP) ? 'r' : '-';
1532 psd->str.u.cStr[5] = (statItem.st_mode & S_IWGRP) ? 'w' : '-';
1533 psd->str.u.cStr[6] = (statItem.st_mode & S_IXGRP) ? 'x' : '-';
1534 psd->str.u.cStr[7] = (statItem.st_mode & S_IROTH) ? 'r' : '-';
1535 psd->str.u.cStr[8] = (statItem.st_mode & S_IWOTH) ? 'w' : '-';
1536 psd->str.u.cStr[9] = (statItem.st_mode & S_IXOTH) ? 'x' : '-';
1537 psd->str.u.cStr[10] = '\0';
1538 break;
1539 case 5:
1540 pPasswd = getpwuid(statItem.st_uid);
1541 if (pPasswd) strcpy(psd->str.u.cStr, pPasswd->pw_name);
1542 break;
1543 case 6:
1544 pGroup = getgrgid(statItem.st_gid);
1545 if (pGroup) strcpy(psd->str.u.cStr, pGroup->gr_name);
1546 break;
1549 return hr;
1552 static HRESULT WINAPI ShellFolder2_MapColumnToSCID(IShellFolder2* iface, UINT column,
1553 SHCOLUMNID *pscid)
1555 UnixFolder *This = impl_from_IShellFolder2(iface);
1556 FIXME("(%p)->(%u %p): stub\n", This, column, pscid);
1557 return E_NOTIMPL;
1560 static const IShellFolder2Vtbl ShellFolder2Vtbl = {
1561 ShellFolder2_QueryInterface,
1562 ShellFolder2_AddRef,
1563 ShellFolder2_Release,
1564 ShellFolder2_ParseDisplayName,
1565 ShellFolder2_EnumObjects,
1566 ShellFolder2_BindToObject,
1567 ShellFolder2_BindToStorage,
1568 ShellFolder2_CompareIDs,
1569 ShellFolder2_CreateViewObject,
1570 ShellFolder2_GetAttributesOf,
1571 ShellFolder2_GetUIObjectOf,
1572 ShellFolder2_GetDisplayNameOf,
1573 ShellFolder2_SetNameOf,
1574 ShellFolder2_GetDefaultSearchGUID,
1575 ShellFolder2_EnumSearches,
1576 ShellFolder2_GetDefaultColumn,
1577 ShellFolder2_GetDefaultColumnState,
1578 ShellFolder2_GetDetailsEx,
1579 ShellFolder2_GetDetailsOf,
1580 ShellFolder2_MapColumnToSCID
1583 static HRESULT WINAPI PersistFolder3_QueryInterface(IPersistFolder3* iface, REFIID riid,
1584 void** ppvObject)
1586 UnixFolder *This = impl_from_IPersistFolder3(iface);
1587 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppvObject);
1590 static ULONG WINAPI PersistFolder3_AddRef(IPersistFolder3* iface)
1592 UnixFolder *This = impl_from_IPersistFolder3(iface);
1593 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
1596 static ULONG WINAPI PersistFolder3_Release(IPersistFolder3* iface)
1598 UnixFolder *This = impl_from_IPersistFolder3(iface);
1599 return IShellFolder2_Release(&This->IShellFolder2_iface);
1602 static HRESULT WINAPI PersistFolder3_GetClassID(IPersistFolder3* iface, CLSID* pClassID)
1604 UnixFolder *This = impl_from_IPersistFolder3(iface);
1606 TRACE("(%p)->(%p)\n", This, pClassID);
1608 if (!pClassID)
1609 return E_INVALIDARG;
1611 *pClassID = *This->m_pCLSID;
1612 return S_OK;
1615 static HRESULT WINAPI PersistFolder3_Initialize(IPersistFolder3* iface, LPCITEMIDLIST pidl)
1617 UnixFolder *This = impl_from_IPersistFolder3(iface);
1618 LPCITEMIDLIST current = pidl;
1619 char szBasePath[FILENAME_MAX] = "/";
1621 TRACE("(%p)->(%p)\n", This, pidl);
1623 /* Find the UnixFolderClass root */
1624 while (current->mkid.cb) {
1625 if ((_ILIsDrive(current) && IsEqualCLSID(This->m_pCLSID, &CLSID_ShellFSFolder)) ||
1626 (_ILIsSpecialFolder(current) && IsEqualCLSID(This->m_pCLSID, _ILGetGUIDPointer(current))))
1628 break;
1630 current = ILGetNext(current);
1633 if (current->mkid.cb) {
1634 if (_ILIsDrive(current)) {
1635 WCHAR wszDrive[] = { '?', ':', '\\', 0 };
1636 wszDrive[0] = (WCHAR)*_ILGetTextPointer(current);
1637 if (!UNIXFS_get_unix_path(wszDrive, szBasePath))
1638 return E_FAIL;
1639 } else if (IsEqualIID(&CLSID_MyDocuments, _ILGetGUIDPointer(current))) {
1640 WCHAR wszMyDocumentsPath[MAX_PATH];
1641 if (!SHGetSpecialFolderPathW(0, wszMyDocumentsPath, CSIDL_PERSONAL, FALSE))
1642 return E_FAIL;
1643 PathAddBackslashW(wszMyDocumentsPath);
1644 if (!UNIXFS_get_unix_path(wszMyDocumentsPath, szBasePath))
1645 return E_FAIL;
1647 current = ILGetNext(current);
1648 } else if (_ILIsDesktop(pidl) || _ILIsValue(pidl) || _ILIsFolder(pidl)) {
1649 /* Path rooted at Desktop */
1650 WCHAR wszDesktopPath[MAX_PATH];
1651 if (!SHGetSpecialFolderPathW(0, wszDesktopPath, CSIDL_DESKTOPDIRECTORY, FALSE))
1652 return E_FAIL;
1653 PathAddBackslashW(wszDesktopPath);
1654 if (!UNIXFS_get_unix_path(wszDesktopPath, szBasePath))
1655 return E_FAIL;
1656 current = pidl;
1657 } else if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
1658 /* FolderShortcuts' Initialize method only sets the ITEMIDLIST, which
1659 * specifies the location in the shell namespace, but leaves the
1660 * target folder (m_pszPath) alone. See unit tests in tests/shlfolder.c */
1661 This->m_pidlLocation = ILClone(pidl);
1662 return S_OK;
1663 } else {
1664 ERR("Unknown pidl type!\n");
1665 pdump(pidl);
1666 return E_INVALIDARG;
1669 This->m_pidlLocation = ILClone(pidl);
1670 return UNIXFS_initialize_target_folder(This, szBasePath, current, 0);
1673 static HRESULT WINAPI PersistFolder3_GetCurFolder(IPersistFolder3* iface, LPITEMIDLIST* ppidl)
1675 UnixFolder *This = impl_from_IPersistFolder3(iface);
1677 TRACE ("(iface=%p, ppidl=%p)\n", iface, ppidl);
1679 if (!ppidl)
1680 return E_POINTER;
1681 *ppidl = ILClone (This->m_pidlLocation);
1682 return S_OK;
1685 static HRESULT WINAPI PersistFolder3_InitializeEx(IPersistFolder3 *iface, IBindCtx *pbc,
1686 LPCITEMIDLIST pidlRoot, const PERSIST_FOLDER_TARGET_INFO *ppfti)
1688 UnixFolder *This = impl_from_IPersistFolder3(iface);
1689 WCHAR wszTargetDosPath[MAX_PATH];
1690 char szTargetPath[FILENAME_MAX] = "";
1692 TRACE("(%p)->(%p %p %p)\n", This, pbc, pidlRoot, ppfti);
1694 /* If no PERSIST_FOLDER_TARGET_INFO is given InitializeEx is equivalent to Initialize. */
1695 if (!ppfti)
1696 return IPersistFolder3_Initialize(iface, pidlRoot);
1698 if (ppfti->csidl != -1) {
1699 if (FAILED(SHGetFolderPathW(0, ppfti->csidl, NULL, 0, wszTargetDosPath)) ||
1700 !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1702 return E_FAIL;
1704 } else if (*ppfti->szTargetParsingName) {
1705 lstrcpyW(wszTargetDosPath, ppfti->szTargetParsingName);
1706 PathAddBackslashW(wszTargetDosPath);
1707 if (!UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath)) {
1708 return E_FAIL;
1710 } else if (ppfti->pidlTargetFolder) {
1711 if (!SHGetPathFromIDListW(ppfti->pidlTargetFolder, wszTargetDosPath) ||
1712 !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1714 return E_FAIL;
1716 } else {
1717 return E_FAIL;
1720 This->m_pszPath = SHAlloc(lstrlenA(szTargetPath)+1);
1721 if (!This->m_pszPath)
1722 return E_FAIL;
1723 lstrcpyA(This->m_pszPath, szTargetPath);
1724 This->m_pidlLocation = ILClone(pidlRoot);
1725 This->m_dwAttributes = (ppfti->dwAttributes != -1) ? ppfti->dwAttributes :
1726 (SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME|SFGAO_FILESYSTEM);
1728 return S_OK;
1731 static HRESULT WINAPI PersistFolder3_GetFolderTargetInfo(IPersistFolder3 *iface,
1732 PERSIST_FOLDER_TARGET_INFO *ppfti)
1734 UnixFolder *This = impl_from_IPersistFolder3(iface);
1735 FIXME("(%p)->(%p): stub\n", This, ppfti);
1736 return E_NOTIMPL;
1739 static const IPersistFolder3Vtbl PersistFolder3Vtbl = {
1740 PersistFolder3_QueryInterface,
1741 PersistFolder3_AddRef,
1742 PersistFolder3_Release,
1743 PersistFolder3_GetClassID,
1744 PersistFolder3_Initialize,
1745 PersistFolder3_GetCurFolder,
1746 PersistFolder3_InitializeEx,
1747 PersistFolder3_GetFolderTargetInfo
1750 static HRESULT WINAPI PersistPropertyBag_QueryInterface(IPersistPropertyBag* iface,
1751 REFIID riid, void** ppv)
1753 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1754 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppv);
1757 static ULONG WINAPI PersistPropertyBag_AddRef(IPersistPropertyBag* iface)
1759 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1760 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
1763 static ULONG WINAPI PersistPropertyBag_Release(IPersistPropertyBag* iface)
1765 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1766 return IShellFolder2_Release(&This->IShellFolder2_iface);
1769 static HRESULT WINAPI PersistPropertyBag_GetClassID(IPersistPropertyBag* iface, CLSID* pClassID)
1771 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1772 return IPersistFolder3_GetClassID(&This->IPersistFolder3_iface, pClassID);
1775 static HRESULT WINAPI PersistPropertyBag_InitNew(IPersistPropertyBag* iface)
1777 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1778 FIXME("(%p): stub\n", This);
1779 return E_NOTIMPL;
1782 static HRESULT WINAPI PersistPropertyBag_Load(IPersistPropertyBag *iface,
1783 IPropertyBag *pPropertyBag, IErrorLog *pErrorLog)
1785 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1787 static const WCHAR wszTarget[] = { 'T','a','r','g','e','t', 0 }, wszNull[] = { 0 };
1788 PERSIST_FOLDER_TARGET_INFO pftiTarget;
1789 VARIANT var;
1790 HRESULT hr;
1792 TRACE("(%p)->(%p %p)\n", This, pPropertyBag, pErrorLog);
1794 if (!pPropertyBag)
1795 return E_POINTER;
1797 /* Get 'Target' property from the property bag. */
1798 V_VT(&var) = VT_BSTR;
1799 hr = IPropertyBag_Read(pPropertyBag, wszTarget, &var, NULL);
1800 if (FAILED(hr))
1801 return E_FAIL;
1802 lstrcpyW(pftiTarget.szTargetParsingName, V_BSTR(&var));
1803 SysFreeString(V_BSTR(&var));
1805 pftiTarget.pidlTargetFolder = NULL;
1806 lstrcpyW(pftiTarget.szNetworkProvider, wszNull);
1807 pftiTarget.dwAttributes = -1;
1808 pftiTarget.csidl = -1;
1810 return IPersistFolder3_InitializeEx(&This->IPersistFolder3_iface, NULL, NULL, &pftiTarget);
1813 static HRESULT WINAPI PersistPropertyBag_Save(IPersistPropertyBag *iface,
1814 IPropertyBag *pPropertyBag, BOOL fClearDirty, BOOL fSaveAllProperties)
1816 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1817 FIXME("(%p): stub\n", This);
1818 return E_NOTIMPL;
1821 static const IPersistPropertyBagVtbl PersistPropertyBagVtbl = {
1822 PersistPropertyBag_QueryInterface,
1823 PersistPropertyBag_AddRef,
1824 PersistPropertyBag_Release,
1825 PersistPropertyBag_GetClassID,
1826 PersistPropertyBag_InitNew,
1827 PersistPropertyBag_Load,
1828 PersistPropertyBag_Save
1831 static HRESULT WINAPI SFHelper_QueryInterface(ISFHelper* iface, REFIID riid, void** ppvObject)
1833 UnixFolder *This = impl_from_ISFHelper(iface);
1834 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppvObject);
1837 static ULONG WINAPI SFHelper_AddRef(ISFHelper* iface)
1839 UnixFolder *This = impl_from_ISFHelper(iface);
1840 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
1843 static ULONG WINAPI SFHelper_Release(ISFHelper* iface)
1845 UnixFolder *This = impl_from_ISFHelper(iface);
1846 return IShellFolder2_Release(&This->IShellFolder2_iface);
1849 static HRESULT WINAPI SFHelper_GetUniqueName(ISFHelper* iface, LPWSTR pwszName, UINT uLen)
1851 UnixFolder *This = impl_from_ISFHelper(iface);
1852 IEnumIDList *pEnum;
1853 HRESULT hr;
1854 LPITEMIDLIST pidlElem;
1855 DWORD dwFetched;
1856 int i;
1857 WCHAR wszNewFolder[25];
1858 static const WCHAR wszFormat[] = { '%','s',' ','%','d',0 };
1860 TRACE("(%p)->(%p %u)\n", This, pwszName, uLen);
1862 LoadStringW(shell32_hInstance, IDS_NEWFOLDER, wszNewFolder, sizeof(wszNewFolder)/sizeof(WCHAR));
1864 if (uLen < sizeof(wszNewFolder)/sizeof(WCHAR)+3)
1865 return E_INVALIDARG;
1867 hr = IShellFolder2_EnumObjects(&This->IShellFolder2_iface, 0,
1868 SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN, &pEnum);
1869 if (SUCCEEDED(hr)) {
1870 lstrcpynW(pwszName, wszNewFolder, uLen);
1871 IEnumIDList_Reset(pEnum);
1872 i = 2;
1873 while ((IEnumIDList_Next(pEnum, 1, &pidlElem, &dwFetched) == S_OK) && (dwFetched == 1)) {
1874 WCHAR wszTemp[MAX_PATH];
1875 _ILSimpleGetTextW(pidlElem, wszTemp, MAX_PATH);
1876 if (!lstrcmpiW(wszTemp, pwszName)) {
1877 IEnumIDList_Reset(pEnum);
1878 snprintfW(pwszName, uLen, wszFormat, wszNewFolder, i++);
1879 if (i > 99) {
1880 hr = E_FAIL;
1881 break;
1885 IEnumIDList_Release(pEnum);
1887 return hr;
1890 static HRESULT WINAPI SFHelper_AddFolder(ISFHelper* iface, HWND hwnd, LPCWSTR pwszName,
1891 LPITEMIDLIST* ppidlOut)
1893 UnixFolder *This = impl_from_ISFHelper(iface);
1894 char szNewDir[FILENAME_MAX];
1895 int cBaseLen;
1897 TRACE("(%p)->(%p %s %p)\n", This, hwnd, debugstr_w(pwszName), ppidlOut);
1899 if (ppidlOut)
1900 *ppidlOut = NULL;
1902 if (!This->m_pszPath || !(This->m_dwAttributes & SFGAO_FILESYSTEM))
1903 return E_FAIL;
1905 lstrcpynA(szNewDir, This->m_pszPath, FILENAME_MAX);
1906 cBaseLen = lstrlenA(szNewDir);
1907 WideCharToMultiByte(CP_UNIXCP, 0, pwszName, -1, szNewDir+cBaseLen, FILENAME_MAX-cBaseLen, 0, 0);
1909 if (mkdir(szNewDir, 0777)) {
1910 char szMessage[256 + FILENAME_MAX];
1911 char szCaption[256];
1913 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_DENIED, szCaption, sizeof(szCaption));
1914 sprintf(szMessage, szCaption, szNewDir);
1915 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_CAPTION, szCaption, sizeof(szCaption));
1916 MessageBoxA(hwnd, szMessage, szCaption, MB_OK | MB_ICONEXCLAMATION);
1918 return E_FAIL;
1919 } else {
1920 LPITEMIDLIST pidlRelative;
1922 /* Inform the shell */
1923 if (SUCCEEDED(UNIXFS_path_to_pidl(This, NULL, pwszName, &pidlRelative))) {
1924 LPITEMIDLIST pidlAbsolute = ILCombine(This->m_pidlLocation, pidlRelative);
1925 if (ppidlOut)
1926 *ppidlOut = pidlRelative;
1927 else
1928 ILFree(pidlRelative);
1929 SHChangeNotify(SHCNE_MKDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1930 ILFree(pidlAbsolute);
1931 } else return E_FAIL;
1932 return S_OK;
1937 * Delete specified files by converting the path to DOS paths and calling
1938 * SHFileOperationW. If an error occurs it returns an error code. If the paths can't
1939 * be converted, S_FALSE is returned. In such situation DeleteItems will try to delete
1940 * the files using syscalls
1942 static HRESULT UNIXFS_delete_with_shfileop(UnixFolder *This, UINT cidl, const LPCITEMIDLIST *apidl)
1944 char szAbsolute[FILENAME_MAX], *pszRelative;
1945 LPWSTR wszPathsList, wszListPos;
1946 SHFILEOPSTRUCTW op;
1947 HRESULT ret;
1948 UINT i;
1950 lstrcpyA(szAbsolute, This->m_pszPath);
1951 pszRelative = szAbsolute + lstrlenA(szAbsolute);
1953 wszListPos = wszPathsList = HeapAlloc(GetProcessHeap(), 0, cidl*MAX_PATH*sizeof(WCHAR)+1);
1954 if (wszPathsList == NULL)
1955 return E_OUTOFMEMORY;
1956 for (i=0; i<cidl; i++) {
1957 LPWSTR wszDosPath;
1959 if (!_ILIsFolder(apidl[i]) && !_ILIsValue(apidl[i]))
1960 continue;
1961 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
1963 HeapFree(GetProcessHeap(), 0, wszPathsList);
1964 return E_INVALIDARG;
1966 wszDosPath = wine_get_dos_file_name(szAbsolute);
1967 if (wszDosPath == NULL || lstrlenW(wszDosPath) >= MAX_PATH)
1969 HeapFree(GetProcessHeap(), 0, wszPathsList);
1970 HeapFree(GetProcessHeap(), 0, wszDosPath);
1971 return S_FALSE;
1973 lstrcpyW(wszListPos, wszDosPath);
1974 wszListPos += lstrlenW(wszListPos)+1;
1975 HeapFree(GetProcessHeap(), 0, wszDosPath);
1977 *wszListPos = 0;
1979 ZeroMemory(&op, sizeof(op));
1980 op.hwnd = GetActiveWindow();
1981 op.wFunc = FO_DELETE;
1982 op.pFrom = wszPathsList;
1983 op.fFlags = FOF_ALLOWUNDO;
1984 if (SHFileOperationW(&op))
1986 WARN("SHFileOperationW failed\n");
1987 ret = E_FAIL;
1989 else
1990 ret = S_OK;
1992 HeapFree(GetProcessHeap(), 0, wszPathsList);
1993 return ret;
1996 static HRESULT UNIXFS_delete_with_syscalls(UnixFolder *This, UINT cidl, const LPCITEMIDLIST *apidl)
1998 char szAbsolute[FILENAME_MAX], *pszRelative;
1999 static const WCHAR empty[] = {0};
2000 UINT i;
2002 if (!SHELL_ConfirmYesNoW(GetActiveWindow(), ASK_DELETE_SELECTED, empty))
2003 return S_OK;
2005 lstrcpyA(szAbsolute, This->m_pszPath);
2006 pszRelative = szAbsolute + lstrlenA(szAbsolute);
2008 for (i=0; i<cidl; i++) {
2009 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
2010 return E_INVALIDARG;
2011 if (_ILIsFolder(apidl[i])) {
2012 if (rmdir(szAbsolute))
2013 return E_FAIL;
2014 } else if (_ILIsValue(apidl[i])) {
2015 if (unlink(szAbsolute))
2016 return E_FAIL;
2019 return S_OK;
2022 static HRESULT WINAPI SFHelper_DeleteItems(ISFHelper* iface, UINT cidl, LPCITEMIDLIST* apidl)
2024 UnixFolder *This = impl_from_ISFHelper(iface);
2025 char szAbsolute[FILENAME_MAX], *pszRelative;
2026 LPITEMIDLIST pidlAbsolute;
2027 HRESULT hr = S_OK;
2028 UINT i;
2029 struct stat st;
2031 TRACE("(%p)->(%d %p)\n", This, cidl, apidl);
2033 hr = UNIXFS_delete_with_shfileop(This, cidl, apidl);
2034 if (hr == S_FALSE)
2035 hr = UNIXFS_delete_with_syscalls(This, cidl, apidl);
2037 lstrcpyA(szAbsolute, This->m_pszPath);
2038 pszRelative = szAbsolute + lstrlenA(szAbsolute);
2040 /* we need to manually send the notifies if the files doesn't exist */
2041 for (i=0; i<cidl; i++) {
2042 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
2043 continue;
2044 pidlAbsolute = ILCombine(This->m_pidlLocation, apidl[i]);
2045 if (stat(szAbsolute, &st))
2047 if (_ILIsFolder(apidl[i])) {
2048 SHChangeNotify(SHCNE_RMDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
2049 } else if (_ILIsValue(apidl[i])) {
2050 SHChangeNotify(SHCNE_DELETE, SHCNF_IDLIST, pidlAbsolute, NULL);
2053 ILFree(pidlAbsolute);
2056 return hr;
2059 static HRESULT WINAPI SFHelper_CopyItems(ISFHelper* iface, IShellFolder *psfFrom,
2060 UINT cidl, LPCITEMIDLIST *apidl)
2062 UnixFolder *This = impl_from_ISFHelper(iface);
2063 DWORD dwAttributes;
2064 UINT i;
2065 HRESULT hr;
2066 char szAbsoluteDst[FILENAME_MAX], *pszRelativeDst;
2068 TRACE("(%p)->(%p %d %p)\n", This, psfFrom, cidl, apidl);
2070 if (!psfFrom || !cidl || !apidl)
2071 return E_INVALIDARG;
2073 /* All source items have to be filesystem items. */
2074 dwAttributes = SFGAO_FILESYSTEM;
2075 hr = IShellFolder_GetAttributesOf(psfFrom, cidl, apidl, &dwAttributes);
2076 if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM))
2077 return E_INVALIDARG;
2079 lstrcpyA(szAbsoluteDst, This->m_pszPath);
2080 pszRelativeDst = szAbsoluteDst + strlen(szAbsoluteDst);
2082 for (i=0; i<cidl; i++) {
2083 WCHAR wszSrc[MAX_PATH];
2084 char szSrc[FILENAME_MAX];
2085 STRRET strret;
2086 HRESULT res;
2087 WCHAR *pwszDosSrc, *pwszDosDst;
2089 /* Build the unix path of the current source item. */
2090 if (FAILED(IShellFolder_GetDisplayNameOf(psfFrom, apidl[i], SHGDN_FORPARSING, &strret)))
2091 return E_FAIL;
2092 if (FAILED(StrRetToBufW(&strret, apidl[i], wszSrc, MAX_PATH)))
2093 return E_FAIL;
2094 if (!UNIXFS_get_unix_path(wszSrc, szSrc))
2095 return E_FAIL;
2097 /* Build the unix path of the current destination item */
2098 UNIXFS_filename_from_shitemid(apidl[i], pszRelativeDst);
2100 pwszDosSrc = wine_get_dos_file_name(szSrc);
2101 pwszDosDst = wine_get_dos_file_name(szAbsoluteDst);
2103 if (pwszDosSrc && pwszDosDst)
2104 res = UNIXFS_copy(pwszDosSrc, pwszDosDst);
2105 else
2106 res = E_OUTOFMEMORY;
2108 HeapFree(GetProcessHeap(), 0, pwszDosSrc);
2109 HeapFree(GetProcessHeap(), 0, pwszDosDst);
2111 if (res != S_OK)
2112 return res;
2114 return S_OK;
2117 static const ISFHelperVtbl SFHelperVtbl = {
2118 SFHelper_QueryInterface,
2119 SFHelper_AddRef,
2120 SFHelper_Release,
2121 SFHelper_GetUniqueName,
2122 SFHelper_AddFolder,
2123 SFHelper_DeleteItems,
2124 SFHelper_CopyItems
2127 static HRESULT WINAPI DropTarget_QueryInterface(IDropTarget* iface, REFIID riid, void** ppvObject)
2129 UnixFolder *This = impl_from_IDropTarget(iface);
2130 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppvObject);
2133 static ULONG WINAPI DropTarget_AddRef(IDropTarget* iface)
2135 UnixFolder *This = impl_from_IDropTarget(iface);
2136 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
2139 static ULONG WINAPI DropTarget_Release(IDropTarget* iface)
2141 UnixFolder *This = impl_from_IDropTarget(iface);
2142 return IShellFolder2_Release(&This->IShellFolder2_iface);
2145 #define HIDA_GetPIDLFolder(pida) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[0])
2146 #define HIDA_GetPIDLItem(pida, i) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[i+1])
2148 static HRESULT WINAPI DropTarget_DragEnter(IDropTarget *iface, IDataObject *pDataObject,
2149 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2151 UnixFolder *This = impl_from_IDropTarget(iface);
2152 FORMATETC format;
2153 STGMEDIUM medium;
2155 TRACE("(%p)->(%p 0x%08x {.x=%d, .y=%d} %p)\n", This, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
2157 if (!pdwEffect || !pDataObject)
2158 return E_INVALIDARG;
2160 /* Compute a mask of supported drop-effects for this shellfolder object and the given data
2161 * object. Dropping is only supported on folders, which represent filesystem locations. One
2162 * can't drop on file objects. And the 'move' drop effect is only supported, if the source
2163 * folder is not identical to the target folder. */
2164 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2165 InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
2166 if ((This->m_dwAttributes & SFGAO_FILESYSTEM) && /* Only drop to filesystem folders */
2167 _ILIsFolder(ILFindLastID(This->m_pidlLocation)) && /* Only drop to folders, not to files */
2168 SUCCEEDED(IDataObject_GetData(pDataObject, &format, &medium))) /* Only ShellIDList format */
2170 LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
2171 This->m_dwDropEffectsMask |= DROPEFFECT_COPY|DROPEFFECT_LINK;
2173 if (pidaShellIDList) { /* Files can only be moved between two different folders */
2174 if (!ILIsEqual(HIDA_GetPIDLFolder(pidaShellIDList), This->m_pidlLocation))
2175 This->m_dwDropEffectsMask |= DROPEFFECT_MOVE;
2176 GlobalUnlock(medium.u.hGlobal);
2180 *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
2182 return S_OK;
2185 static HRESULT WINAPI DropTarget_DragOver(IDropTarget *iface, DWORD dwKeyState,
2186 POINTL pt, DWORD *pdwEffect)
2188 UnixFolder *This = impl_from_IDropTarget(iface);
2190 TRACE("(%p)->(0x%08x {.x=%d, .y=%d} %p)\n", This, dwKeyState, pt.x, pt.y, pdwEffect);
2192 if (!pdwEffect)
2193 return E_INVALIDARG;
2195 *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
2197 return S_OK;
2200 static HRESULT WINAPI DropTarget_DragLeave(IDropTarget *iface)
2202 UnixFolder *This = impl_from_IDropTarget(iface);
2204 TRACE("(%p)\n", This);
2206 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2207 return S_OK;
2210 static HRESULT WINAPI DropTarget_Drop(IDropTarget *iface, IDataObject *pDataObject,
2211 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2213 UnixFolder *This = impl_from_IDropTarget(iface);
2214 FORMATETC format;
2215 STGMEDIUM medium;
2216 HRESULT hr;
2218 TRACE("(%p)->(%p %d {.x=%d, .y=%d} %p) semi-stub\n",
2219 This, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
2221 InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
2222 hr = IDataObject_GetData(pDataObject, &format, &medium);
2223 if (FAILED(hr))
2224 return hr;
2226 if (medium.tymed == TYMED_HGLOBAL) {
2227 IShellFolder *psfSourceFolder, *psfDesktopFolder;
2228 LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
2229 STRRET strret;
2230 UINT i;
2232 if (!pidaShellIDList)
2233 return HRESULT_FROM_WIN32(GetLastError());
2235 hr = SHGetDesktopFolder(&psfDesktopFolder);
2236 if (FAILED(hr)) {
2237 GlobalUnlock(medium.u.hGlobal);
2238 return hr;
2241 hr = IShellFolder_BindToObject(psfDesktopFolder, HIDA_GetPIDLFolder(pidaShellIDList), NULL,
2242 &IID_IShellFolder, (LPVOID*)&psfSourceFolder);
2243 IShellFolder_Release(psfDesktopFolder);
2244 if (FAILED(hr)) {
2245 GlobalUnlock(medium.u.hGlobal);
2246 return hr;
2249 for (i = 0; i < pidaShellIDList->cidl; i++) {
2250 WCHAR wszSourcePath[MAX_PATH];
2252 hr = IShellFolder_GetDisplayNameOf(psfSourceFolder, HIDA_GetPIDLItem(pidaShellIDList, i),
2253 SHGDN_FORPARSING, &strret);
2254 if (FAILED(hr))
2255 break;
2257 hr = StrRetToBufW(&strret, NULL, wszSourcePath, MAX_PATH);
2258 if (FAILED(hr))
2259 break;
2261 switch (*pdwEffect) {
2262 case DROPEFFECT_MOVE:
2263 FIXME("Move %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2264 break;
2265 case DROPEFFECT_COPY:
2266 FIXME("Copy %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2267 break;
2268 case DROPEFFECT_LINK:
2269 FIXME("Link %s from %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2270 break;
2274 IShellFolder_Release(psfSourceFolder);
2275 GlobalUnlock(medium.u.hGlobal);
2276 return hr;
2279 return E_NOTIMPL;
2282 static const IDropTargetVtbl DropTargetVtbl = {
2283 DropTarget_QueryInterface,
2284 DropTarget_AddRef,
2285 DropTarget_Release,
2286 DropTarget_DragEnter,
2287 DropTarget_DragOver,
2288 DropTarget_DragLeave,
2289 DropTarget_Drop
2292 /******************************************************************************
2293 * Unix[Dos]Folder_Constructor [Internal]
2295 * PARAMS
2296 * pUnkOuter [I] Outer class for aggregation. Currently ignored.
2297 * riid [I] Interface asked for by the client.
2298 * ppv [O] Pointer to an riid interface to the UnixFolder object.
2300 * NOTES
2301 * Those are the only functions exported from shfldr_unixfs.c. They are called from
2302 * shellole.c's default class factory and thus have to exhibit a LPFNCREATEINSTANCE
2303 * compatible signature.
2305 * The UnixDosFolder_Constructor sets the dwPathMode member to PATHMODE_DOS. This
2306 * means that paths are converted from dos to unix and back at the interfaces.
2308 static HRESULT CreateUnixFolder(IUnknown *outer, REFIID riid, void **ppv, const CLSID *clsid)
2310 UnixFolder *This;
2311 HRESULT hr;
2313 if (outer) {
2314 FIXME("Aggregation not yet implemented!\n");
2315 return CLASS_E_NOAGGREGATION;
2318 This = SHAlloc((ULONG)sizeof(UnixFolder));
2319 if (!This) return E_OUTOFMEMORY;
2321 This->IShellFolder2_iface.lpVtbl = &ShellFolder2Vtbl;
2322 This->IPersistFolder3_iface.lpVtbl = &PersistFolder3Vtbl;
2323 This->IPersistPropertyBag_iface.lpVtbl = &PersistPropertyBagVtbl;
2324 This->ISFHelper_iface.lpVtbl = &SFHelperVtbl;
2325 This->IDropTarget_iface.lpVtbl = &DropTargetVtbl;
2326 This->ref = 1;
2327 This->m_pszPath = NULL;
2328 This->m_pidlLocation = NULL;
2329 This->m_dwPathMode = IsEqualCLSID(&CLSID_UnixFolder, clsid) ? PATHMODE_UNIX : PATHMODE_DOS;
2330 This->m_dwAttributes = 0;
2331 This->m_pCLSID = clsid;
2332 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2334 hr = IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppv);
2335 IShellFolder2_Release(&This->IShellFolder2_iface);
2337 return hr;
2340 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2341 TRACE("(pUnkOuter=%p, riid=%s, ppv=%p)\n", pUnkOuter, debugstr_guid(riid), ppv);
2342 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixFolder);
2345 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2346 TRACE("(pUnkOuter=%p, riid=%s, ppv=%p)\n", pUnkOuter, debugstr_guid(riid), ppv);
2347 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixDosFolder);
2350 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2351 TRACE("(pUnkOuter=%p, riid=%s, ppv=%p)\n", pUnkOuter, debugstr_guid(riid), ppv);
2352 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_FolderShortcut);
2355 HRESULT WINAPI MyDocuments_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2356 TRACE("(pUnkOuter=%p, riid=%s, ppv=%p)\n", pUnkOuter, debugstr_guid(riid), ppv);
2357 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_MyDocuments);
2360 /******************************************************************************
2361 * UnixSubFolderIterator
2363 * Class whose heap based objects represent iterators over the sub-directories
2364 * of a given UnixFolder object.
2367 /* UnixSubFolderIterator object layout and typedef.
2369 typedef struct _UnixSubFolderIterator {
2370 IEnumIDList IEnumIDList_iface;
2371 LONG ref;
2372 SHCONTF m_fFilter;
2373 DIR *m_dirFolder;
2374 char m_szFolder[FILENAME_MAX];
2375 } UnixSubFolderIterator;
2377 static inline UnixSubFolderIterator *impl_from_IEnumIDList(IEnumIDList *iface)
2379 return CONTAINING_RECORD(iface, UnixSubFolderIterator, IEnumIDList_iface);
2382 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
2383 TRACE("(iterator=%p)\n", iterator);
2385 if (iterator->m_dirFolder)
2386 closedir(iterator->m_dirFolder);
2387 SHFree(iterator);
2390 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface,
2391 REFIID riid, void** ppv)
2393 TRACE("(iface=%p, riid=%s, ppv=%p)\n", iface, debugstr_guid(riid), ppv);
2395 if (!ppv) return E_INVALIDARG;
2397 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
2398 *ppv = iface;
2399 } else {
2400 *ppv = NULL;
2401 return E_NOINTERFACE;
2404 IEnumIDList_AddRef(iface);
2405 return S_OK;
2408 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
2410 UnixSubFolderIterator *This = impl_from_IEnumIDList(iface);
2411 ULONG ref = InterlockedIncrement(&This->ref);
2413 TRACE("(%p) ref=%d\n", This, ref);
2415 return ref;
2418 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
2420 UnixSubFolderIterator *This = impl_from_IEnumIDList(iface);
2421 ULONG ref = InterlockedDecrement(&This->ref);
2423 TRACE("(%p) ref=%d\n", This, ref);
2425 if (!ref)
2426 UnixSubFolderIterator_Destroy(This);
2428 return ref;
2431 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt,
2432 LPITEMIDLIST* rgelt, ULONG* pceltFetched)
2434 UnixSubFolderIterator *This = impl_from_IEnumIDList(iface);
2435 ULONG i = 0;
2437 /* This->m_dirFolder will be NULL if the user doesn't have access rights for the dir. */
2438 if (This->m_dirFolder) {
2439 char *pszRelativePath = This->m_szFolder + lstrlenA(This->m_szFolder);
2440 struct dirent *pDirEntry;
2442 while (i < celt) {
2443 pDirEntry = readdir(This->m_dirFolder);
2444 if (!pDirEntry) break; /* No more entries */
2445 if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
2447 /* Temporarily build absolute path in This->m_szFolder. Then construct a pidl
2448 * and see if it passes the filter.
2450 lstrcpyA(pszRelativePath, pDirEntry->d_name);
2451 rgelt[i] = SHAlloc(
2452 UNIXFS_shitemid_len_from_filename(pszRelativePath, NULL, NULL)+sizeof(USHORT));
2453 if (!UNIXFS_build_shitemid(This->m_szFolder, TRUE, NULL, rgelt[i]) ||
2454 !UNIXFS_is_pidl_of_type(rgelt[i], This->m_fFilter))
2456 SHFree(rgelt[i]);
2457 rgelt[i] = NULL;
2458 continue;
2460 memset(((PBYTE)rgelt[i])+rgelt[i]->mkid.cb, 0, sizeof(USHORT));
2461 i++;
2463 *pszRelativePath = '\0'; /* Restore the original path in This->m_szFolder. */
2466 if (pceltFetched)
2467 *pceltFetched = i;
2469 return (i == 0) ? S_FALSE : S_OK;
2472 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
2474 LPITEMIDLIST *apidl;
2475 ULONG cFetched;
2476 HRESULT hr;
2478 TRACE("(iface=%p, celt=%d)\n", iface, celt);
2480 /* Call IEnumIDList::Next and delete the resulting pidls. */
2481 apidl = SHAlloc(celt * sizeof(LPITEMIDLIST));
2482 hr = IEnumIDList_Next(iface, celt, apidl, &cFetched);
2483 if (SUCCEEDED(hr))
2484 while (cFetched--)
2485 SHFree(apidl[cFetched]);
2486 SHFree(apidl);
2488 return hr;
2491 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
2493 UnixSubFolderIterator *This = impl_from_IEnumIDList(iface);
2495 TRACE("(iface=%p)\n", iface);
2497 if (This->m_dirFolder)
2498 rewinddir(This->m_dirFolder);
2500 return S_OK;
2503 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This,
2504 IEnumIDList** ppenum)
2506 FIXME("stub\n");
2507 return E_NOTIMPL;
2510 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
2512 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
2513 UnixSubFolderIterator_IEnumIDList_QueryInterface,
2514 UnixSubFolderIterator_IEnumIDList_AddRef,
2515 UnixSubFolderIterator_IEnumIDList_Release,
2516 UnixSubFolderIterator_IEnumIDList_Next,
2517 UnixSubFolderIterator_IEnumIDList_Skip,
2518 UnixSubFolderIterator_IEnumIDList_Reset,
2519 UnixSubFolderIterator_IEnumIDList_Clone
2522 static IEnumIDList *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter)
2524 UnixSubFolderIterator *iterator;
2526 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
2528 iterator = SHAlloc(sizeof(*iterator));
2529 iterator->IEnumIDList_iface.lpVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
2530 iterator->ref = 1;
2531 iterator->m_fFilter = fFilter;
2532 iterator->m_dirFolder = opendir(pUnixFolder->m_pszPath);
2533 lstrcpyA(iterator->m_szFolder, pUnixFolder->m_pszPath);
2535 return &iterator->IEnumIDList_iface;
2538 #else /* __MINGW32__ || _MSC_VER */
2540 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2542 return E_NOTIMPL;
2545 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2547 return E_NOTIMPL;
2550 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2552 return E_NOTIMPL;
2555 HRESULT WINAPI MyDocuments_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2557 return E_NOTIMPL;
2560 #endif /* __MINGW32__ || _MSC_VER */
2562 /******************************************************************************
2563 * UNIXFS_is_rooted_at_desktop [Internal]
2565 * Checks if the unixfs namespace extension is rooted at desktop level.
2567 * RETURNS
2568 * TRUE, if unixfs is rooted at desktop level
2569 * FALSE, if not.
2571 BOOL UNIXFS_is_rooted_at_desktop(void) {
2572 HKEY hKey;
2573 WCHAR wszRootedAtDesktop[69 + CHARS_IN_GUID] = {
2574 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2575 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2576 'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
2577 'N','a','m','e','S','p','a','c','e','\\',0 };
2579 if (StringFromGUID2(&CLSID_UnixDosFolder, wszRootedAtDesktop + 69, CHARS_IN_GUID) &&
2580 RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszRootedAtDesktop, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
2582 RegCloseKey(hKey);
2583 return TRUE;
2585 return FALSE;