winejoystick: Fix a crash on accessing a CFArray past its end due to an off-by-one...
[wine/multimedia.git] / dlls / shell32 / shfldr_unixfs.c
blob8f93e7ff3103f54c4a23ecdb29fe409ca89e5ffb
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
151 #define NONAMELESSSTRUCT
153 #include "windef.h"
154 #include "winbase.h"
155 #include "winuser.h"
156 #include "objbase.h"
157 #include "winreg.h"
158 #include "shlwapi.h"
159 #include "winternl.h"
160 #include "wine/debug.h"
162 #include "shell32_main.h"
163 #include "shellfolder.h"
164 #include "shfldr.h"
165 #include "shresdef.h"
166 #include "pidl.h"
167 #include "debughlp.h"
169 WINE_DEFAULT_DEBUG_CHANNEL(shell);
171 #if !defined(__MINGW32__) && !defined(_MSC_VER)
173 #define LEN_SHITEMID_FIXED_PART ((USHORT) \
174 ( sizeof(USHORT) /* SHITEMID's cb field. */ \
175 + sizeof(PIDLTYPE) /* PIDLDATA's type field. */ \
176 + sizeof(FileStruct) /* Well, the FileStruct. */ \
177 - sizeof(char) /* One char too much in FileStruct. */ \
178 + sizeof(FileStructW) /* You name it. */ \
179 - sizeof(WCHAR) /* One WCHAR too much in FileStructW. */ \
180 + sizeof(WORD) )) /* Offset of FileStructW field in PIDL. */
182 #define PATHMODE_UNIX 0
183 #define PATHMODE_DOS 1
185 static const WCHAR wFileSystemBindData[] = {
186 'F','i','l','e',' ','S','y','s','t','e','m',' ','B','i','n','d',' ','D','a','t','a',0};
188 typedef struct {
189 IShellFolder2 IShellFolder2_iface;
190 IPersistFolder3 IPersistFolder3_iface;
191 IPersistPropertyBag IPersistPropertyBag_iface;
192 IDropTarget IDropTarget_iface;
193 ISFHelper ISFHelper_iface;
195 LONG ref;
196 CHAR *m_pszPath; /* Target path of the shell folder (CP_UNIXCP) */
197 LPITEMIDLIST m_pidlLocation; /* Location in the shell namespace */
198 DWORD m_dwPathMode;
199 DWORD m_dwAttributes;
200 const CLSID *m_pCLSID;
201 DWORD m_dwDropEffectsMask;
202 } UnixFolder;
204 static inline UnixFolder *impl_from_IShellFolder2(IShellFolder2 *iface)
206 return CONTAINING_RECORD(iface, UnixFolder, IShellFolder2_iface);
209 static inline UnixFolder *impl_from_IPersistFolder3(IPersistFolder3 *iface)
211 return CONTAINING_RECORD(iface, UnixFolder, IPersistFolder3_iface);
214 static inline UnixFolder *impl_from_IPersistPropertyBag(IPersistPropertyBag *iface)
216 return CONTAINING_RECORD(iface, UnixFolder, IPersistPropertyBag_iface);
219 static inline UnixFolder *impl_from_ISFHelper(ISFHelper *iface)
221 return CONTAINING_RECORD(iface, UnixFolder, ISFHelper_iface);
224 static inline UnixFolder *impl_from_IDropTarget(IDropTarget *iface)
226 return CONTAINING_RECORD(iface, UnixFolder, IDropTarget_iface);
229 /* Will hold the registered clipboard format identifier for ITEMIDLISTS. */
230 static UINT cfShellIDList = 0;
232 /******************************************************************************
233 * UNIXFS_filename_from_shitemid [Internal]
235 * Get CP_UNIXCP encoded filename corresponding to the first item of a pidl
237 * PARAMS
238 * pidl [I] A simple SHITEMID
239 * pszPathElement [O] Filename in CP_UNIXCP encoding will be stored here
241 * RETURNS
242 * Success: Number of bytes necessary to store the CP_UNIXCP encoded filename
243 * _without_ the terminating NUL.
244 * Failure: 0
246 * NOTES
247 * Size of the buffer at pszPathElement has to be FILENAME_MAX. pszPathElement
248 * may be NULL, if you are only interested in the return value.
250 static int UNIXFS_filename_from_shitemid(LPCITEMIDLIST pidl, char* pszPathElement) {
251 FileStructW *pFileStructW = _ILGetFileStructW(pidl);
252 int cLen = 0;
254 if (pFileStructW) {
255 cLen = WideCharToMultiByte(CP_UNIXCP, 0, pFileStructW->wszName, -1, pszPathElement,
256 pszPathElement ? FILENAME_MAX : 0, 0, 0);
257 } else {
258 /* There might be pidls slipping in from shfldr_fs.c, which don't contain the
259 * FileStructW field. In this case, we have to convert from CP_ACP to CP_UNIXCP. */
260 char *pszText = _ILGetTextPointer(pidl);
261 WCHAR *pwszPathElement = NULL;
262 int cWideChars;
264 cWideChars = MultiByteToWideChar(CP_ACP, 0, pszText, -1, NULL, 0);
265 if (!cWideChars) goto cleanup;
267 pwszPathElement = SHAlloc(cWideChars * sizeof(WCHAR));
268 if (!pwszPathElement) goto cleanup;
270 cWideChars = MultiByteToWideChar(CP_ACP, 0, pszText, -1, pwszPathElement, cWideChars);
271 if (!cWideChars) goto cleanup;
273 cLen = WideCharToMultiByte(CP_UNIXCP, 0, pwszPathElement, -1, pszPathElement,
274 pszPathElement ? FILENAME_MAX : 0, 0, 0);
276 cleanup:
277 SHFree(pwszPathElement);
280 if (cLen) cLen--; /* Don't count terminating NUL! */
281 return cLen;
284 /******************************************************************************
285 * UNIXFS_shitemid_len_from_filename [Internal]
287 * Computes the necessary length of a pidl to hold a path element
289 * PARAMS
290 * szPathElement [I] The path element string in CP_UNIXCP encoding.
291 * ppszPathElement [O] Path element string in CP_ACP encoding.
292 * ppwszPathElement [O] Path element string as WCHAR string.
294 * RETURNS
295 * Success: Length in bytes of a SHITEMID representing szPathElement
296 * Failure: 0
298 * NOTES
299 * Provide NULL values if not interested in pp(w)szPathElement. Otherwise
300 * caller is responsible to free ppszPathElement and ppwszPathElement with
301 * SHFree.
303 static USHORT UNIXFS_shitemid_len_from_filename(
304 const char *szPathElement, char **ppszPathElement, WCHAR **ppwszPathElement)
306 USHORT cbPidlLen = 0;
307 WCHAR *pwszPathElement = NULL;
308 char *pszPathElement = NULL;
309 int cWideChars, cChars;
311 /* There and Back Again: A Hobbit's Holiday. CP_UNIXCP might be some ANSI
312 * codepage or it might be a real multi-byte encoding like utf-8. There is no
313 * other way to figure out the length of the corresponding WCHAR and CP_ACP
314 * strings without actually doing the full CP_UNIXCP -> WCHAR -> CP_ACP cycle. */
316 cWideChars = MultiByteToWideChar(CP_UNIXCP, 0, szPathElement, -1, NULL, 0);
317 if (!cWideChars) goto cleanup;
319 pwszPathElement = SHAlloc(cWideChars * sizeof(WCHAR));
320 if (!pwszPathElement) goto cleanup;
322 cWideChars = MultiByteToWideChar(CP_UNIXCP, 0, szPathElement, -1, pwszPathElement, cWideChars);
323 if (!cWideChars) goto cleanup;
325 cChars = WideCharToMultiByte(CP_ACP, 0, pwszPathElement, -1, NULL, 0, 0, 0);
326 if (!cChars) goto cleanup;
328 pszPathElement = SHAlloc(cChars);
329 if (!pszPathElement) goto cleanup;
331 cChars = WideCharToMultiByte(CP_ACP, 0, pwszPathElement, -1, pszPathElement, cChars, 0, 0);
332 if (!cChars) goto cleanup;
334 /* (cChars & 0x1) is for the potential alignment byte */
335 cbPidlLen = LEN_SHITEMID_FIXED_PART + cChars + (cChars & 0x1) + cWideChars * sizeof(WCHAR);
337 cleanup:
338 if (cbPidlLen && ppszPathElement)
339 *ppszPathElement = pszPathElement;
340 else
341 SHFree(pszPathElement);
343 if (cbPidlLen && ppwszPathElement)
344 *ppwszPathElement = pwszPathElement;
345 else
346 SHFree(pwszPathElement);
348 return cbPidlLen;
351 /******************************************************************************
352 * UNIXFS_is_pidl_of_type [Internal]
354 * Checks for the first SHITEMID of an ITEMIDLIST if it passes a filter.
356 * PARAMS
357 * pIDL [I] The ITEMIDLIST to be checked.
358 * fFilter [I] Shell condition flags, which specify the filter.
360 * RETURNS
361 * TRUE, if pIDL is accepted by fFilter
362 * FALSE, otherwise
364 static inline BOOL UNIXFS_is_pidl_of_type(LPCITEMIDLIST pIDL, SHCONTF fFilter) {
365 const PIDLDATA *pIDLData = _ILGetDataPointer(pIDL);
366 if (!(fFilter & SHCONTF_INCLUDEHIDDEN) && pIDLData &&
367 (pIDLData->u.file.uFileAttribs & FILE_ATTRIBUTE_HIDDEN))
369 return FALSE;
371 if (_ILIsFolder(pIDL) && (fFilter & SHCONTF_FOLDERS)) return TRUE;
372 if (_ILIsValue(pIDL) && (fFilter & SHCONTF_NONFOLDERS)) return TRUE;
373 return FALSE;
376 /******************************************************************************
377 * UNIXFS_get_unix_path [Internal]
379 * Convert an absolute dos path to an absolute unix path.
380 * Evaluate "/.", "/.." and the symbolic links in $WINEPREFIX/dosdevices.
382 * PARAMS
383 * pszDosPath [I] An absolute dos path
384 * pszCanonicalPath [O] Buffer of length FILENAME_MAX. Will receive the canonical path.
386 * RETURNS
387 * Success, TRUE
388 * Failure, FALSE - Path not existent, too long, insufficient rights, too many symlinks
390 static BOOL UNIXFS_get_unix_path(LPCWSTR pszDosPath, char *pszCanonicalPath)
392 char *pPathTail, *pElement, *pCanonicalTail, szPath[FILENAME_MAX], *pszUnixPath, mb_path[FILENAME_MAX];
393 BOOL has_failed = FALSE;
394 WCHAR wszDrive[] = { '?', ':', '\\', 0 }, dospath[MAX_PATH], *dospath_end;
395 int cDriveSymlinkLen;
396 void *redir;
398 TRACE("(pszDosPath=%s, pszCanonicalPath=%p)\n", debugstr_w(pszDosPath), pszCanonicalPath);
400 if (!pszDosPath || pszDosPath[1] != ':')
401 return FALSE;
403 /* Get the canonicalized unix path corresponding to the drive letter. */
404 wszDrive[0] = pszDosPath[0];
405 pszUnixPath = wine_get_unix_file_name(wszDrive);
406 if (!pszUnixPath) return FALSE;
407 cDriveSymlinkLen = strlen(pszUnixPath);
408 pElement = realpath(pszUnixPath, szPath);
409 HeapFree(GetProcessHeap(), 0, pszUnixPath);
410 if (!pElement) return FALSE;
411 if (szPath[strlen(szPath)-1] != '/') strcat(szPath, "/");
413 /* Append the part relative to the drive symbolic link target. */
414 lstrcpyW(dospath, pszDosPath);
415 dospath_end = dospath + lstrlenW(dospath);
416 /* search for the most valid UNIX path possible, then append missing
417 * path parts */
418 Wow64DisableWow64FsRedirection(&redir);
419 while(!(pszUnixPath = wine_get_unix_file_name(dospath))){
420 if(has_failed){
421 *dospath_end = '/';
422 --dospath_end;
423 }else
424 has_failed = TRUE;
425 while(*dospath_end != '\\' && *dospath_end != '/'){
426 --dospath_end;
427 if(dospath_end < dospath)
428 break;
430 *dospath_end = '\0';
432 Wow64RevertWow64FsRedirection(redir);
433 if(dospath_end < dospath)
434 return FALSE;
435 strcat(szPath, pszUnixPath + cDriveSymlinkLen);
436 HeapFree(GetProcessHeap(), 0, pszUnixPath);
438 if(has_failed && WideCharToMultiByte(CP_UNIXCP, 0, dospath_end + 1, -1,
439 mb_path, FILENAME_MAX, NULL, NULL) > 0){
440 strcat(szPath, "/");
441 strcat(szPath, mb_path);
444 /* pCanonicalTail always points to the end of the canonical path constructed
445 * thus far. pPathTail points to the still to be processed part of the input
446 * path. pElement points to the path element currently investigated.
448 *pszCanonicalPath = '\0';
449 pCanonicalTail = pszCanonicalPath;
450 pPathTail = szPath;
452 do {
453 char cTemp;
455 pElement = pPathTail;
456 pPathTail = strchr(pPathTail+1, '/');
457 if (!pPathTail) /* Last path element may not be terminated by '/'. */
458 pPathTail = pElement + strlen(pElement);
459 /* Temporarily terminate the current path element. Will be restored later. */
460 cTemp = *pPathTail;
461 *pPathTail = '\0';
463 /* Skip "/." path elements */
464 if (!strcmp("/.", pElement)) {
465 *pPathTail = cTemp;
466 } else if (!strcmp("/..", pElement)) {
467 /* Remove last element in canonical path for "/.." elements, then skip. */
468 char *pTemp = strrchr(pszCanonicalPath, '/');
469 if (pTemp)
470 pCanonicalTail = pTemp;
471 *pCanonicalTail = '\0';
472 *pPathTail = cTemp;
473 } else {
474 /* Directory or file. Copy to canonical path */
475 if (pCanonicalTail - pszCanonicalPath + pPathTail - pElement + 1 > FILENAME_MAX)
476 return FALSE;
478 memcpy(pCanonicalTail, pElement, pPathTail - pElement + 1);
479 pCanonicalTail += pPathTail - pElement;
480 *pPathTail = cTemp;
482 } while (pPathTail[0] == '/');
484 TRACE("--> %s\n", debugstr_a(pszCanonicalPath));
486 return TRUE;
489 /******************************************************************************
490 * UNIXFS_build_shitemid [Internal]
492 * Constructs a new SHITEMID for the last component of path 'pszUnixPath' into
493 * buffer 'pIDL'.
495 * PARAMS
496 * pszUnixPath [I] An absolute path. The SHITEMID will be built for the last component.
497 * pbc [I] Bind context for this action, used to determine if the file must exist
498 * pIDL [O] SHITEMID will be constructed here.
500 * RETURNS
501 * Success: A pointer to the terminating '\0' character of path.
502 * Failure: NULL
504 * NOTES
505 * Minimum size of pIDL is SHITEMID_LEN_FROM_NAME_LEN(strlen(last_component_of_path)).
506 * If what you need is a PIDLLIST with a single SHITEMID, don't forget to append
507 * a 0 USHORT value.
509 static char* UNIXFS_build_shitemid(char *pszUnixPath, BOOL bMustExist, WIN32_FIND_DATAW *pFindData, void *pIDL) {
510 LPPIDLDATA pIDLData;
511 struct stat fileStat;
512 WIN32_FIND_DATAW findData;
513 char *pszComponentU, *pszComponentA;
514 WCHAR *pwszComponentW;
515 int cComponentULen, cComponentALen;
516 USHORT cbLen;
517 FileStructW *pFileStructW;
518 WORD uOffsetW, *pOffsetW;
520 TRACE("(pszUnixPath=%s, bMustExist=%s, pFindData=%p, pIDL=%p)\n",
521 debugstr_a(pszUnixPath), bMustExist ? "T" : "F", pFindData, pIDL);
523 if (pFindData)
524 memcpy(&findData, pFindData, sizeof(WIN32_FIND_DATAW));
525 else {
526 memset(&findData, 0, sizeof(WIN32_FIND_DATAW));
527 findData.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
530 /* We are only interested in regular files and directories. */
531 if (stat(pszUnixPath, &fileStat)){
532 if (bMustExist || errno != ENOENT)
533 return NULL;
534 } else {
535 LARGE_INTEGER time;
537 if (S_ISDIR(fileStat.st_mode))
538 findData.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
539 else if (S_ISREG(fileStat.st_mode))
540 findData.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
541 else
542 return NULL;
544 findData.nFileSizeLow = (DWORD)fileStat.st_size;
545 findData.nFileSizeHigh = fileStat.st_size >> 32;
547 RtlSecondsSince1970ToTime(fileStat.st_mtime, &time);
548 findData.ftLastWriteTime.dwLowDateTime = time.u.LowPart;
549 findData.ftLastWriteTime.dwHighDateTime = time.u.HighPart;
550 RtlSecondsSince1970ToTime(fileStat.st_atime, &time);
551 findData.ftLastAccessTime.dwLowDateTime = time.u.LowPart;
552 findData.ftLastAccessTime.dwHighDateTime = time.u.HighPart;
555 /* Compute the SHITEMID's length and wipe it. */
556 pszComponentU = strrchr(pszUnixPath, '/') + 1;
557 cComponentULen = strlen(pszComponentU);
558 cbLen = UNIXFS_shitemid_len_from_filename(pszComponentU, &pszComponentA, &pwszComponentW);
559 if (!cbLen) return NULL;
560 memset(pIDL, 0, cbLen);
561 ((LPSHITEMID)pIDL)->cb = cbLen;
563 /* Set shell32's standard SHITEMID data fields. */
564 pIDLData = _ILGetDataPointer(pIDL);
565 pIDLData->type = (findData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) ? PT_FOLDER : PT_VALUE;
566 pIDLData->u.file.dwFileSize = findData.nFileSizeLow;
567 FileTimeToDosDateTime(&findData.ftLastWriteTime, &pIDLData->u.file.uFileDate,
568 &pIDLData->u.file.uFileTime);
569 pIDLData->u.file.uFileAttribs = 0;
570 pIDLData->u.file.uFileAttribs |= findData.dwFileAttributes;
571 if (pszComponentU[0] == '.') pIDLData->u.file.uFileAttribs |= FILE_ATTRIBUTE_HIDDEN;
572 cComponentALen = lstrlenA(pszComponentA) + 1;
573 memcpy(pIDLData->u.file.szNames, pszComponentA, cComponentALen);
575 pFileStructW = (FileStructW*)(pIDLData->u.file.szNames + cComponentALen + (cComponentALen & 0x1));
576 uOffsetW = (WORD)(((LPBYTE)pFileStructW) - ((LPBYTE)pIDL));
577 pFileStructW->cbLen = cbLen - uOffsetW;
578 FileTimeToDosDateTime(&findData.ftLastWriteTime, &pFileStructW->uCreationDate,
579 &pFileStructW->uCreationTime);
580 FileTimeToDosDateTime(&findData.ftLastAccessTime, &pFileStructW->uLastAccessDate,
581 &pFileStructW->uLastAccessTime);
582 lstrcpyW(pFileStructW->wszName, pwszComponentW);
584 pOffsetW = (WORD*)(((LPBYTE)pIDL) + cbLen - sizeof(WORD));
585 *pOffsetW = uOffsetW;
587 SHFree(pszComponentA);
588 SHFree(pwszComponentW);
590 return pszComponentU + cComponentULen;
593 /******************************************************************************
594 * UNIXFS_path_to_pidl [Internal]
596 * PARAMS
597 * pUnixFolder [I] If path is relative, pUnixFolder represents the base path
598 * path [I] An absolute unix or dos path or a path relative to pUnixFolder
599 * ppidl [O] The corresponding ITEMIDLIST. Release with SHFree/ILFree
601 * RETURNS
602 * Success: S_OK
603 * Failure: Error code, invalid params or out of memory
605 * NOTES
606 * pUnixFolder also carries the information if the path is expected to be unix or dos.
608 static HRESULT UNIXFS_path_to_pidl(UnixFolder *pUnixFolder, LPBC pbc, const WCHAR *path,
609 LPITEMIDLIST *ppidl) {
610 LPITEMIDLIST pidl;
611 int cPidlLen, cPathLen;
612 char *pSlash, *pNextSlash, szCompletePath[FILENAME_MAX], *pNextPathElement, *pszAPath;
613 WCHAR *pwszPath;
614 WIN32_FIND_DATAW find_data;
615 BOOL must_exist = TRUE;
617 TRACE("pUnixFolder=%p, pbc=%p, path=%s, ppidl=%p\n", pUnixFolder, pbc, debugstr_w(path), ppidl);
619 if (!ppidl || !path)
620 return E_INVALIDARG;
622 /* Build an absolute path and let pNextPathElement point to the interesting
623 * relative sub-path. We need the absolute path to call 'stat', but the pidl
624 * will only contain the relative part.
626 if ((pUnixFolder->m_dwPathMode == PATHMODE_DOS) && (path[1] == ':'))
628 /* Absolute dos path. Convert to unix */
629 if (!UNIXFS_get_unix_path(path, szCompletePath))
630 return E_FAIL;
631 pNextPathElement = szCompletePath;
633 else if ((pUnixFolder->m_dwPathMode == PATHMODE_UNIX) && (path[0] == '/'))
635 /* Absolute unix path. Just convert to ANSI. */
636 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath, FILENAME_MAX, NULL, NULL);
637 pNextPathElement = szCompletePath;
639 else
641 /* Relative dos or unix path. Concat with this folder's path */
642 int cBasePathLen = strlen(pUnixFolder->m_pszPath);
643 memcpy(szCompletePath, pUnixFolder->m_pszPath, cBasePathLen);
644 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath + cBasePathLen,
645 FILENAME_MAX - cBasePathLen, NULL, NULL);
646 pNextPathElement = szCompletePath + cBasePathLen - 1;
648 /* If in dos mode, replace '\' with '/' */
649 if (pUnixFolder->m_dwPathMode == PATHMODE_DOS) {
650 char *pBackslash = strchr(pNextPathElement, '\\');
651 while (pBackslash) {
652 *pBackslash = '/';
653 pBackslash = strchr(pBackslash, '\\');
658 /* Special case for the root folder. */
659 if (!strcmp(szCompletePath, "/")) {
660 *ppidl = pidl = SHAlloc(sizeof(USHORT));
661 if (!pidl) return E_FAIL;
662 pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
663 return S_OK;
666 /* Remove trailing slash, if present */
667 cPathLen = strlen(szCompletePath);
668 if (szCompletePath[cPathLen-1] == '/')
669 szCompletePath[cPathLen-1] = '\0';
671 if ((szCompletePath[0] != '/') || (pNextPathElement[0] != '/')) {
672 ERR("szCompletePath: %s, pNextPathElment: %s\n", szCompletePath, pNextPathElement);
673 return E_FAIL;
676 /* At this point, we have an absolute unix path in szCompletePath
677 * and the relative portion of it in pNextPathElement. Both starting with '/'
678 * and _not_ terminated by a '/'. */
679 TRACE("complete path: %s, relative path: %s\n", szCompletePath, pNextPathElement);
681 /* Convert to CP_ACP and WCHAR */
682 if (!UNIXFS_shitemid_len_from_filename(pNextPathElement, &pszAPath, &pwszPath))
683 return E_FAIL;
685 /* Compute the length of the complete ITEMIDLIST */
686 cPidlLen = 0;
687 pSlash = pszAPath;
688 while (pSlash) {
689 pNextSlash = strchr(pSlash+1, '/');
690 cPidlLen += LEN_SHITEMID_FIXED_PART + /* Fixed part length plus potential alignment byte. */
691 (pNextSlash ? (pNextSlash - pSlash) & 0x1 : lstrlenA(pSlash) & 0x1);
692 pSlash = pNextSlash;
695 /* The USHORT is for the ITEMIDLIST terminator. The NUL terminators for the sub-path-strings
696 * are accounted for by the '/' separators, which are not stored in the SHITEMIDs. Above we
697 * have ensured that the number of '/'s exactly matches the number of sub-path-strings. */
698 cPidlLen += lstrlenA(pszAPath) + lstrlenW(pwszPath) * sizeof(WCHAR) + sizeof(USHORT);
700 SHFree(pszAPath);
701 SHFree(pwszPath);
703 *ppidl = pidl = SHAlloc(cPidlLen);
704 if (!pidl) return E_FAIL;
706 if (pbc) {
707 IUnknown *unk;
708 IFileSystemBindData *fsb;
709 HRESULT hr;
711 hr = IBindCtx_GetObjectParam(pbc, (LPOLESTR)wFileSystemBindData, &unk);
712 if (SUCCEEDED(hr)) {
713 hr = IUnknown_QueryInterface(unk, &IID_IFileSystemBindData, (LPVOID*)&fsb);
714 if (SUCCEEDED(hr)) {
715 hr = IFileSystemBindData_GetFindData(fsb, &find_data);
716 if (FAILED(hr))
717 memset(&find_data, 0, sizeof(WIN32_FIND_DATAW));
719 must_exist = FALSE;
720 IFileSystemBindData_Release(fsb);
722 IUnknown_Release(unk);
726 /* Concatenate the SHITEMIDs of the sub-directories. */
727 while (*pNextPathElement) {
728 pSlash = strchr(pNextPathElement+1, '/');
729 if (pSlash) *pSlash = '\0';
730 pNextPathElement = UNIXFS_build_shitemid(szCompletePath, must_exist,
731 must_exist&&!pSlash ? &find_data : NULL, pidl);
732 if (pSlash) *pSlash = '/';
734 if (!pNextPathElement) {
735 SHFree(*ppidl);
736 *ppidl = NULL;
737 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
739 pidl = ILGetNext(pidl);
741 pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
743 if ((char *)pidl-(char *)*ppidl+sizeof(USHORT) != cPidlLen) /* We've corrupted the heap :( */
744 ERR("Computed length of pidl incorrect. Please report.\n");
746 return S_OK;
749 /******************************************************************************
750 * UNIXFS_initialize_target_folder [Internal]
752 * Initialize the m_pszPath member of an UnixFolder, given an absolute unix
753 * base path and a relative ITEMIDLIST. Leave the m_pidlLocation member, which
754 * specifies the location in the shell namespace alone.
756 * PARAMS
757 * This [IO] The UnixFolder, whose target path is to be initialized
758 * szBasePath [I] The absolute base path
759 * pidlSubFolder [I] Relative part of the path, given as an ITEMIDLIST
760 * dwAttributes [I] Attributes to add to the Folders m_dwAttributes member
761 * (Used to pass the SFGAO_FILESYSTEM flag down the path)
762 * RETURNS
763 * Success: S_OK,
764 * Failure: E_FAIL
766 static HRESULT UNIXFS_initialize_target_folder(UnixFolder *This, const char *szBasePath,
767 LPCITEMIDLIST pidlSubFolder, DWORD dwAttributes)
769 LPCITEMIDLIST current = pidlSubFolder;
770 DWORD dwPathLen = strlen(szBasePath)+1;
771 char *pNextDir;
772 WCHAR *dos_name;
774 /* Determine the path's length bytes */
775 while (!_ILIsEmpty(current)) {
776 dwPathLen += UNIXFS_filename_from_shitemid(current, NULL) + 1; /* For the '/' */
777 current = ILGetNext(current);
780 /* Build the path and compute the attributes*/
781 This->m_dwAttributes =
782 dwAttributes|SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME;
783 This->m_pszPath = pNextDir = SHAlloc(dwPathLen);
784 if (!This->m_pszPath) {
785 WARN("SHAlloc failed!\n");
786 return E_FAIL;
788 current = pidlSubFolder;
789 strcpy(pNextDir, szBasePath);
790 pNextDir += strlen(szBasePath);
791 if (This->m_dwPathMode == PATHMODE_UNIX || IsEqualCLSID(&CLSID_MyDocuments, This->m_pCLSID))
792 This->m_dwAttributes |= SFGAO_FILESYSTEM;
793 while (!_ILIsEmpty(current)) {
794 pNextDir += UNIXFS_filename_from_shitemid(current, pNextDir);
795 *pNextDir++ = '/';
796 current = ILGetNext(current);
798 *pNextDir='\0';
800 if (!(This->m_dwAttributes & SFGAO_FILESYSTEM) &&
801 ((dos_name = wine_get_dos_file_name(This->m_pszPath))))
803 This->m_dwAttributes |= SFGAO_FILESYSTEM;
804 HeapFree( GetProcessHeap(), 0, dos_name );
807 return S_OK;
810 /******************************************************************************
811 * UNIXFS_copy [Internal]
813 * Copy pwszDosSrc to pwszDosDst.
815 * PARAMS
816 * pwszDosSrc [I] absolute path of the source
817 * pwszDosDst [I] absolute path of the destination
819 * RETURNS
820 * Success: S_OK,
821 * Failure: E_FAIL
823 static HRESULT UNIXFS_copy(LPCWSTR pwszDosSrc, LPCWSTR pwszDosDst)
825 SHFILEOPSTRUCTW op;
826 LPWSTR pwszSrc, pwszDst;
827 HRESULT res = E_OUTOFMEMORY;
828 UINT iSrcLen, iDstLen;
830 if (!pwszDosSrc || !pwszDosDst)
831 return E_FAIL;
833 iSrcLen = lstrlenW(pwszDosSrc);
834 iDstLen = lstrlenW(pwszDosDst);
835 pwszSrc = HeapAlloc(GetProcessHeap(), 0, (iSrcLen + 2) * sizeof(WCHAR));
836 pwszDst = HeapAlloc(GetProcessHeap(), 0, (iDstLen + 2) * sizeof(WCHAR));
838 if (pwszSrc && pwszDst) {
839 lstrcpyW(pwszSrc, pwszDosSrc);
840 lstrcpyW(pwszDst, pwszDosDst);
841 /* double null termination */
842 pwszSrc[iSrcLen + 1] = 0;
843 pwszDst[iDstLen + 1] = 0;
845 ZeroMemory(&op, sizeof(op));
846 op.hwnd = GetActiveWindow();
847 op.wFunc = FO_COPY;
848 op.pFrom = pwszSrc;
849 op.pTo = pwszDst;
850 op.fFlags = FOF_ALLOWUNDO;
851 if (!SHFileOperationW(&op))
853 WARN("SHFileOperationW failed\n");
854 res = E_FAIL;
856 else
857 res = S_OK;
860 HeapFree(GetProcessHeap(), 0, pwszSrc);
861 HeapFree(GetProcessHeap(), 0, pwszDst);
862 return res;
865 /******************************************************************************
866 * UnixFolder
868 * Class whose heap based instances represent unix filesystem directories.
871 static void UnixFolder_Destroy(UnixFolder *pUnixFolder) {
872 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
874 SHFree(pUnixFolder->m_pszPath);
875 ILFree(pUnixFolder->m_pidlLocation);
876 SHFree(pUnixFolder);
879 static HRESULT WINAPI ShellFolder2_QueryInterface(IShellFolder2 *iface, REFIID riid,
880 void **ppv)
882 UnixFolder *This = impl_from_IShellFolder2(iface);
884 TRACE("(%p)->(%s %p)\n", This, shdebugstr_guid(riid), ppv);
886 if (!ppv) return E_INVALIDARG;
888 if (IsEqualIID(&IID_IUnknown, riid) ||
889 IsEqualIID(&IID_IShellFolder, riid) ||
890 IsEqualIID(&IID_IShellFolder2, riid))
892 *ppv = &This->IShellFolder2_iface;
893 } else if (IsEqualIID(&IID_IPersistFolder3, riid) ||
894 IsEqualIID(&IID_IPersistFolder2, riid) ||
895 IsEqualIID(&IID_IPersistFolder, riid) ||
896 IsEqualIID(&IID_IPersist, riid))
898 *ppv = &This->IPersistFolder3_iface;
899 } else if (IsEqualIID(&IID_IPersistPropertyBag, riid)) {
900 *ppv = &This->IPersistPropertyBag_iface;
901 } else if (IsEqualIID(&IID_ISFHelper, riid)) {
902 *ppv = &This->ISFHelper_iface;
903 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
904 *ppv = &This->IDropTarget_iface;
905 if (!cfShellIDList)
906 cfShellIDList = RegisterClipboardFormatW(CFSTR_SHELLIDLISTW);
907 } else {
908 *ppv = NULL;
909 TRACE("Unimplemented interface %s\n", shdebugstr_guid(riid));
910 return E_NOINTERFACE;
913 IUnknown_AddRef((IUnknown*)*ppv);
914 return S_OK;
917 static ULONG WINAPI ShellFolder2_AddRef(IShellFolder2 *iface)
919 UnixFolder *This = impl_from_IShellFolder2(iface);
920 ULONG ref = InterlockedIncrement(&This->ref);
921 TRACE("(%p)->(%u)\n", This, ref);
922 return ref;
925 static ULONG WINAPI ShellFolder2_Release(IShellFolder2 *iface)
927 UnixFolder *This = impl_from_IShellFolder2(iface);
928 ULONG ref = InterlockedDecrement(&This->ref);
930 TRACE("(%p)->(%u)\n", This, ref);
932 if (!ref)
933 UnixFolder_Destroy(This);
935 return ref;
938 static HRESULT WINAPI ShellFolder2_ParseDisplayName(IShellFolder2* iface, HWND hwndOwner,
939 LPBC pbc, LPOLESTR display_name, ULONG* pchEaten, LPITEMIDLIST* ppidl,
940 ULONG* attrs)
942 UnixFolder *This = impl_from_IShellFolder2(iface);
943 HRESULT result;
945 TRACE("(%p)->(%p %p %s %p %p %p)\n", This, hwndOwner, pbc, debugstr_w(display_name),
946 pchEaten, ppidl, attrs);
948 result = UNIXFS_path_to_pidl(This, pbc, display_name, ppidl);
949 if (SUCCEEDED(result) && attrs && *attrs)
951 IShellFolder *parent;
952 LPCITEMIDLIST pidlLast;
953 LPITEMIDLIST pidlComplete = ILCombine(This->m_pidlLocation, *ppidl);
954 HRESULT hr;
956 hr = SHBindToParent(pidlComplete, &IID_IShellFolder, (void**)&parent, &pidlLast);
957 if (FAILED(hr)) {
958 FIXME("SHBindToParent failed! hr = 0x%08x\n", hr);
959 ILFree(pidlComplete);
960 return E_FAIL;
962 IShellFolder_GetAttributesOf(parent, 1, &pidlLast, attrs);
963 IShellFolder_Release(parent);
964 ILFree(pidlComplete);
967 if (FAILED(result)) TRACE("FAILED!\n");
968 return result;
971 static IEnumIDList *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter);
973 static HRESULT WINAPI ShellFolder2_EnumObjects(IShellFolder2* iface, HWND hwndOwner,
974 SHCONTF grfFlags, IEnumIDList** ppEnumIDList)
976 UnixFolder *This = impl_from_IShellFolder2(iface);
978 TRACE("(%p)->(%p 0x%08x %p)\n", This, hwndOwner, grfFlags, ppEnumIDList);
980 if (!This->m_pszPath) {
981 WARN("EnumObjects called on uninitialized UnixFolder-object!\n");
982 return E_UNEXPECTED;
985 *ppEnumIDList = UnixSubFolderIterator_Constructor(This, grfFlags);
986 return S_OK;
989 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, const CLSID *pCLSID);
991 static HRESULT WINAPI ShellFolder2_BindToObject(IShellFolder2* iface, LPCITEMIDLIST pidl,
992 LPBC pbcReserved, REFIID riid, void** ppvOut)
994 UnixFolder *This = impl_from_IShellFolder2(iface);
995 IPersistFolder3 *persistFolder;
996 const CLSID *clsidChild;
997 HRESULT hr;
999 TRACE("(%p)->(%p %p %s %p)\n", This, pidl, pbcReserved, debugstr_guid(riid), ppvOut);
1001 if (_ILIsEmpty(pidl))
1002 return E_INVALIDARG;
1004 /* Don't bind to files */
1005 if (_ILIsValue(ILFindLastID(pidl)))
1006 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1008 if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
1009 /* Children of FolderShortcuts are ShellFSFolders on Windows.
1010 * Unixfs' counterpart is UnixDosFolder. */
1011 clsidChild = &CLSID_UnixDosFolder;
1012 } else {
1013 clsidChild = This->m_pCLSID;
1016 hr = CreateUnixFolder(NULL, &IID_IPersistFolder3, (void**)&persistFolder, clsidChild);
1017 if (FAILED(hr)) return hr;
1018 hr = IPersistFolder3_QueryInterface(persistFolder, riid, ppvOut);
1020 if (SUCCEEDED(hr)) {
1021 UnixFolder *subfolder = impl_from_IPersistFolder3(persistFolder);
1022 subfolder->m_pidlLocation = ILCombine(This->m_pidlLocation, pidl);
1023 hr = UNIXFS_initialize_target_folder(subfolder, This->m_pszPath, pidl,
1024 This->m_dwAttributes & SFGAO_FILESYSTEM);
1027 IPersistFolder3_Release(persistFolder);
1029 return hr;
1032 static HRESULT WINAPI ShellFolder2_BindToStorage(IShellFolder2* iface, LPCITEMIDLIST pidl,
1033 LPBC pbcReserved, REFIID riid, void** ppvObj)
1035 UnixFolder *This = impl_from_IShellFolder2(iface);
1036 FIXME("(%p)->(%p %p %s %p): stub\n", This, pidl, pbcReserved, debugstr_guid(riid), ppvObj);
1037 return E_NOTIMPL;
1040 static HRESULT WINAPI ShellFolder2_CompareIDs(IShellFolder2* iface, LPARAM lParam,
1041 LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
1043 UnixFolder *This = impl_from_IShellFolder2(iface);
1044 BOOL isEmpty1, isEmpty2;
1045 HRESULT hr = E_FAIL;
1046 LPCITEMIDLIST firstpidl;
1047 IShellFolder2 *psf;
1048 int compare;
1050 TRACE("(%p)->(%ld %p %p)\n", This, lParam, pidl1, pidl2);
1052 isEmpty1 = _ILIsEmpty(pidl1);
1053 isEmpty2 = _ILIsEmpty(pidl2);
1055 if (isEmpty1 && isEmpty2)
1056 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
1057 else if (isEmpty1)
1058 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1059 else if (isEmpty2)
1060 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1062 compare = CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
1063 _ILGetTextPointer(pidl1), -1,
1064 _ILGetTextPointer(pidl2), -1);
1066 if ((compare != CSTR_EQUAL) && _ILIsFolder(pidl1) && !_ILIsFolder(pidl2))
1067 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1068 if ((compare != CSTR_EQUAL) && !_ILIsFolder(pidl1) && _ILIsFolder(pidl2))
1069 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1071 if ((compare == CSTR_LESS_THAN) || (compare == CSTR_GREATER_THAN))
1072 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)((compare == CSTR_LESS_THAN)?-1:1));
1074 if (pidl1->mkid.cb < pidl2->mkid.cb)
1075 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1076 else if (pidl1->mkid.cb > pidl2->mkid.cb)
1077 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1079 firstpidl = pidl1;
1080 pidl1 = ILGetNext(pidl1);
1081 pidl2 = ILGetNext(pidl2);
1083 isEmpty1 = _ILIsEmpty(pidl1);
1084 isEmpty2 = _ILIsEmpty(pidl2);
1086 if (isEmpty1 && isEmpty2)
1087 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
1088 else if (isEmpty1)
1089 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1090 else if (isEmpty2)
1091 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1092 else if (SUCCEEDED(IShellFolder2_BindToObject(iface, firstpidl, NULL, &IID_IShellFolder, (void**)&psf))) {
1093 hr = IShellFolder2_CompareIDs(psf, lParam, pidl1, pidl2);
1094 IShellFolder2_Release(psf);
1097 return hr;
1100 static HRESULT WINAPI ShellFolder2_CreateViewObject(IShellFolder2* iface, HWND hwndOwner,
1101 REFIID riid, void** ppv)
1103 UnixFolder *This = impl_from_IShellFolder2(iface);
1104 HRESULT hr = E_INVALIDARG;
1106 TRACE("(%p)->(%p %s %p)\n", This, hwndOwner, debugstr_guid(riid), ppv);
1108 if (!ppv) return E_INVALIDARG;
1109 *ppv = NULL;
1111 if (IsEqualIID(&IID_IShellView, riid)) {
1112 IShellView *view;
1114 view = IShellView_Constructor((IShellFolder*)iface);
1115 if (view) {
1116 hr = IShellView_QueryInterface(view, riid, ppv);
1117 IShellView_Release(view);
1119 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
1120 hr = IShellFolder2_QueryInterface(iface, &IID_IDropTarget, ppv);
1123 return hr;
1126 static HRESULT WINAPI ShellFolder2_GetAttributesOf(IShellFolder2* iface, UINT cidl,
1127 LPCITEMIDLIST* apidl, SFGAOF* rgfInOut)
1129 UnixFolder *This = impl_from_IShellFolder2(iface);
1130 HRESULT hr = S_OK;
1132 TRACE("(%p)->(%u %p %p)\n", This, cidl, apidl, rgfInOut);
1134 if (!rgfInOut || (cidl && !apidl))
1135 return E_INVALIDARG;
1137 if (cidl == 0) {
1138 *rgfInOut &= This->m_dwAttributes;
1139 } else {
1140 char szAbsolutePath[FILENAME_MAX], *pszRelativePath;
1141 UINT i;
1143 *rgfInOut = SFGAO_CANCOPY|SFGAO_CANMOVE|SFGAO_CANLINK|SFGAO_CANRENAME|SFGAO_CANDELETE|
1144 SFGAO_HASPROPSHEET|SFGAO_DROPTARGET|SFGAO_FILESYSTEM;
1145 lstrcpyA(szAbsolutePath, This->m_pszPath);
1146 pszRelativePath = szAbsolutePath + lstrlenA(szAbsolutePath);
1147 for (i=0; i<cidl; i++) {
1148 if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
1149 WCHAR *dos_name;
1150 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelativePath))
1151 return E_INVALIDARG;
1152 if (!(dos_name = wine_get_dos_file_name( szAbsolutePath )))
1153 *rgfInOut &= ~SFGAO_FILESYSTEM;
1154 else
1155 HeapFree( GetProcessHeap(), 0, dos_name );
1157 if (_ILIsFolder(apidl[i]))
1158 *rgfInOut |= SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR;
1162 return hr;
1165 static HRESULT WINAPI ShellFolder2_GetUIObjectOf(IShellFolder2* iface, HWND hwndOwner,
1166 UINT cidl, LPCITEMIDLIST* apidl, REFIID riid, UINT* prgfInOut, void** ppvOut)
1168 UnixFolder *This = impl_from_IShellFolder2(iface);
1169 HRESULT hr;
1170 UINT i;
1172 TRACE("(%p)->(%p %d %p riid=%s %p %p)\n",
1173 This, hwndOwner, cidl, apidl, debugstr_guid(riid), prgfInOut, ppvOut);
1175 if (!cidl || !apidl || !riid || !ppvOut)
1176 return E_INVALIDARG;
1178 for (i=0; i<cidl; i++)
1179 if (!apidl[i])
1180 return E_INVALIDARG;
1182 if(cidl == 1) {
1183 hr = SHELL32_CreateExtensionUIObject(iface, *apidl, riid, ppvOut);
1184 if(hr != S_FALSE)
1185 return hr;
1188 if (IsEqualIID(&IID_IContextMenu, riid)) {
1189 return ItemMenu_Constructor((IShellFolder*)iface, This->m_pidlLocation, apidl, cidl, riid, ppvOut);
1190 } else if (IsEqualIID(&IID_IDataObject, riid)) {
1191 *ppvOut = IDataObject_Constructor(hwndOwner, This->m_pidlLocation, apidl, cidl);
1192 return S_OK;
1193 } else if (IsEqualIID(&IID_IExtractIconA, riid)) {
1194 LPITEMIDLIST pidl;
1195 if (cidl != 1) return E_INVALIDARG;
1196 pidl = ILCombine(This->m_pidlLocation, apidl[0]);
1197 *ppvOut = IExtractIconA_Constructor(pidl);
1198 SHFree(pidl);
1199 return S_OK;
1200 } else if (IsEqualIID(&IID_IExtractIconW, riid)) {
1201 LPITEMIDLIST pidl;
1202 if (cidl != 1) return E_INVALIDARG;
1203 pidl = ILCombine(This->m_pidlLocation, apidl[0]);
1204 *ppvOut = IExtractIconW_Constructor(pidl);
1205 SHFree(pidl);
1206 return S_OK;
1207 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
1208 if (cidl != 1) return E_INVALIDARG;
1209 return IShellFolder2_BindToObject(iface, apidl[0], NULL, &IID_IDropTarget, ppvOut);
1210 } else if (IsEqualIID(&IID_IShellLinkW, riid)) {
1211 FIXME("IShellLinkW\n");
1212 return E_FAIL;
1213 } else if (IsEqualIID(&IID_IShellLinkA, riid)) {
1214 FIXME("IShellLinkA\n");
1215 return E_FAIL;
1216 } else {
1217 FIXME("Unknown interface %s in GetUIObjectOf\n", debugstr_guid(riid));
1218 return E_NOINTERFACE;
1222 static HRESULT WINAPI ShellFolder2_GetDisplayNameOf(IShellFolder2* iface,
1223 LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET* lpName)
1225 UnixFolder *This = impl_from_IShellFolder2(iface);
1226 SHITEMID emptyIDL = { 0, { 0 } };
1227 HRESULT hr = S_OK;
1229 TRACE("(%p)->(%p 0x%x %p)\n", This, pidl, uFlags, lpName);
1231 if ((GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) &&
1232 (GET_SHGDN_RELATION(uFlags) != SHGDN_INFOLDER))
1234 if (_ILIsEmpty(pidl)) {
1235 lpName->uType = STRRET_WSTR;
1236 if (This->m_dwPathMode == PATHMODE_UNIX) {
1237 UINT len = MultiByteToWideChar(CP_UNIXCP, 0, This->m_pszPath, -1, NULL, 0);
1238 lpName->u.pOleStr = SHAlloc(len * sizeof(WCHAR));
1239 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1240 MultiByteToWideChar(CP_UNIXCP, 0, This->m_pszPath, -1, lpName->u.pOleStr, len);
1241 } else {
1242 LPWSTR pwszDosFileName = wine_get_dos_file_name(This->m_pszPath);
1243 if (!pwszDosFileName) return HRESULT_FROM_WIN32(GetLastError());
1244 lpName->u.pOleStr = SHAlloc((lstrlenW(pwszDosFileName) + 1) * sizeof(WCHAR));
1245 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1246 lstrcpyW(lpName->u.pOleStr, pwszDosFileName);
1247 PathRemoveBackslashW(lpName->u.pOleStr);
1248 HeapFree(GetProcessHeap(), 0, pwszDosFileName);
1250 } else if (_ILIsValue(pidl)) {
1251 STRRET str;
1252 PWSTR path, file;
1254 /* We are looking for the complete path to a file */
1256 /* Get the complete path for the current folder object */
1257 hr = IShellFolder2_GetDisplayNameOf(iface, (LPITEMIDLIST)&emptyIDL, uFlags, &str);
1258 if (SUCCEEDED(hr)) {
1259 hr = StrRetToStrW(&str, NULL, &path);
1260 if (SUCCEEDED(hr)) {
1262 /* Get the child filename */
1263 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_FORPARSING | SHGDN_INFOLDER, &str);
1264 if (SUCCEEDED(hr)) {
1265 hr = StrRetToStrW(&str, NULL, &file);
1266 if (SUCCEEDED(hr)) {
1267 static const WCHAR slashW = '/';
1268 UINT len_path = strlenW(path), len_file = strlenW(file);
1270 /* Now, combine them */
1271 lpName->uType = STRRET_WSTR;
1272 lpName->u.pOleStr = SHAlloc( (len_path + len_file + 2)*sizeof(WCHAR) );
1273 lstrcpyW(lpName->u.pOleStr, path);
1274 if (This->m_dwPathMode == PATHMODE_UNIX &&
1275 lpName->u.pOleStr[len_path-1] != slashW) {
1276 lpName->u.pOleStr[len_path] = slashW;
1277 lpName->u.pOleStr[len_path+1] = '\0';
1278 } else
1279 PathAddBackslashW(lpName->u.pOleStr);
1280 lstrcatW(lpName->u.pOleStr, file);
1282 CoTaskMemFree(file);
1283 } else
1284 WARN("Failed to convert strret (file)\n");
1286 CoTaskMemFree(path);
1287 } else
1288 WARN("Failed to convert strret (path)\n");
1290 } else {
1291 IShellFolder *pSubFolder;
1293 hr = IShellFolder2_BindToObject(iface, pidl, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1294 if (SUCCEEDED(hr)) {
1295 hr = IShellFolder_GetDisplayNameOf(pSubFolder, (LPITEMIDLIST)&emptyIDL, uFlags, lpName);
1296 IShellFolder_Release(pSubFolder);
1297 } else if (FAILED(hr) && !_ILIsPidlSimple(pidl)) {
1298 LPITEMIDLIST pidl_parent = ILClone(pidl);
1299 LPITEMIDLIST pidl_child = ILFindLastID(pidl);
1301 /* Might be a file, try binding to its parent */
1302 ILRemoveLastID(pidl_parent);
1303 hr = IShellFolder2_BindToObject(iface, pidl_parent, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1304 if (SUCCEEDED(hr)) {
1305 hr = IShellFolder_GetDisplayNameOf(pSubFolder, pidl_child, uFlags, lpName);
1306 IShellFolder_Release(pSubFolder);
1308 ILFree(pidl_parent);
1311 } else {
1312 WCHAR wszFileName[MAX_PATH];
1313 if (!_ILSimpleGetTextW(pidl, wszFileName, MAX_PATH)) return E_INVALIDARG;
1314 lpName->uType = STRRET_WSTR;
1315 lpName->u.pOleStr = SHAlloc((lstrlenW(wszFileName)+1)*sizeof(WCHAR));
1316 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1317 lstrcpyW(lpName->u.pOleStr, wszFileName);
1318 if (!(GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) && This->m_dwPathMode == PATHMODE_DOS &&
1319 !_ILIsFolder(pidl) && wszFileName[0] != '.' && SHELL_FS_HideExtension(wszFileName))
1321 PathRemoveExtensionW(lpName->u.pOleStr);
1325 TRACE("--> %s\n", debugstr_w(lpName->u.pOleStr));
1327 return hr;
1330 static HRESULT WINAPI ShellFolder2_SetNameOf(IShellFolder2* iface, HWND hwnd,
1331 LPCITEMIDLIST pidl, LPCOLESTR lpcwszName, SHGDNF uFlags, LPITEMIDLIST* ppidlOut)
1333 UnixFolder *This = impl_from_IShellFolder2(iface);
1335 static const WCHAR awcInvalidChars[] = { '\\', '/', ':', '*', '?', '"', '<', '>', '|' };
1336 char szSrc[FILENAME_MAX], szDest[FILENAME_MAX];
1337 WCHAR wszSrcRelative[MAX_PATH], *pwszExt = NULL;
1338 unsigned int i;
1339 int cBasePathLen = lstrlenA(This->m_pszPath), cNameLen;
1340 struct stat statDest;
1341 LPITEMIDLIST pidlSrc, pidlDest, pidlRelativeDest;
1342 LPOLESTR lpwszName;
1343 HRESULT hr;
1345 TRACE("(%p)->(%p %p %s 0x%08x %p)\n", This, hwnd, pidl, debugstr_w(lpcwszName), uFlags, ppidlOut);
1347 /* prepare to fail */
1348 if (ppidlOut)
1349 *ppidlOut = NULL;
1351 /* pidl has to contain a single non-empty SHITEMID */
1352 if (_ILIsDesktop(pidl) || !_ILIsPidlSimple(pidl) || !_ILGetTextPointer(pidl))
1353 return E_INVALIDARG;
1355 /* check for invalid characters in lpcwszName. */
1356 for (i=0; i < sizeof(awcInvalidChars)/sizeof(*awcInvalidChars); i++)
1357 if (StrChrW(lpcwszName, awcInvalidChars[i]))
1358 return HRESULT_FROM_WIN32(ERROR_CANCELLED);
1360 /* build source path */
1361 memcpy(szSrc, This->m_pszPath, cBasePathLen);
1362 UNIXFS_filename_from_shitemid(pidl, szSrc + cBasePathLen);
1364 /* build destination path */
1365 memcpy(szDest, This->m_pszPath, cBasePathLen);
1366 WideCharToMultiByte(CP_UNIXCP, 0, lpcwszName, -1, szDest+cBasePathLen,
1367 FILENAME_MAX-cBasePathLen, NULL, NULL);
1369 /* If the filename's extension is hidden to the user, we have to append it. */
1370 if (!(uFlags & SHGDN_FORPARSING) &&
1371 _ILSimpleGetTextW(pidl, wszSrcRelative, MAX_PATH) &&
1372 SHELL_FS_HideExtension(wszSrcRelative))
1374 int cLenDest = strlen(szDest);
1375 pwszExt = PathFindExtensionW(wszSrcRelative);
1376 WideCharToMultiByte(CP_UNIXCP, 0, pwszExt, -1, szDest + cLenDest,
1377 FILENAME_MAX - cLenDest, NULL, NULL);
1380 TRACE("src=%s dest=%s\n", szSrc, szDest);
1382 /* Fail, if destination does already exist */
1383 if (!stat(szDest, &statDest))
1384 return E_FAIL;
1386 /* Rename the file */
1387 if (rename(szSrc, szDest))
1388 return E_FAIL;
1390 /* Build a pidl for the path of the renamed file */
1391 cNameLen = lstrlenW(lpcwszName) + 1;
1392 if(pwszExt)
1393 cNameLen += lstrlenW(pwszExt);
1394 lpwszName = SHAlloc(cNameLen*sizeof(WCHAR)); /* due to const correctness. */
1395 lstrcpyW(lpwszName, lpcwszName);
1396 if(pwszExt)
1397 lstrcatW(lpwszName, pwszExt);
1399 hr = IShellFolder2_ParseDisplayName(iface, NULL, NULL, lpwszName, NULL, &pidlRelativeDest, NULL);
1400 SHFree(lpwszName);
1401 if (FAILED(hr)) {
1402 rename(szDest, szSrc); /* Undo the renaming */
1403 return E_FAIL;
1405 pidlDest = ILCombine(This->m_pidlLocation, pidlRelativeDest);
1406 ILFree(pidlRelativeDest);
1407 pidlSrc = ILCombine(This->m_pidlLocation, pidl);
1409 /* Inform the shell */
1410 if (_ILIsFolder(ILFindLastID(pidlDest)))
1411 SHChangeNotify(SHCNE_RENAMEFOLDER, SHCNF_IDLIST, pidlSrc, pidlDest);
1412 else
1413 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_IDLIST, pidlSrc, pidlDest);
1415 if (ppidlOut)
1416 *ppidlOut = ILClone(ILFindLastID(pidlDest));
1418 ILFree(pidlSrc);
1419 ILFree(pidlDest);
1421 return S_OK;
1424 static HRESULT WINAPI ShellFolder2_EnumSearches(IShellFolder2* iface, IEnumExtraSearch **ppEnum)
1426 UnixFolder *This = impl_from_IShellFolder2(iface);
1427 FIXME("(%p)->(%p): stub\n", This, ppEnum);
1428 return E_NOTIMPL;
1431 static HRESULT WINAPI ShellFolder2_GetDefaultColumn(IShellFolder2* iface,
1432 DWORD dwReserved, ULONG *pSort, ULONG *pDisplay)
1434 UnixFolder *This = impl_from_IShellFolder2(iface);
1436 TRACE("(%p)->(0x%x %p %p)\n", This, dwReserved, pSort, pDisplay);
1438 if (pSort)
1439 *pSort = 0;
1440 if (pDisplay)
1441 *pDisplay = 0;
1443 return S_OK;
1446 static HRESULT WINAPI ShellFolder2_GetDefaultColumnState(IShellFolder2* iface,
1447 UINT column, SHCOLSTATEF *flags)
1449 UnixFolder *This = impl_from_IShellFolder2(iface);
1450 FIXME("(%p)->(%u %p): stub\n", This, column, flags);
1451 return E_NOTIMPL;
1454 static HRESULT WINAPI ShellFolder2_GetDefaultSearchGUID(IShellFolder2* iface,
1455 GUID *guid)
1457 UnixFolder *This = impl_from_IShellFolder2(iface);
1458 FIXME("(%p)->(%p): stub\n", This, guid);
1459 return E_NOTIMPL;
1462 static HRESULT WINAPI ShellFolder2_GetDetailsEx(IShellFolder2* iface,
1463 LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
1465 UnixFolder *This = impl_from_IShellFolder2(iface);
1466 FIXME("(%p)->(%p %p %p): stub\n", This, pidl, pscid, pv);
1467 return E_NOTIMPL;
1470 #define SHELLVIEWCOLUMNS 7
1472 static HRESULT WINAPI ShellFolder2_GetDetailsOf(IShellFolder2* iface,
1473 LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
1475 UnixFolder *This = impl_from_IShellFolder2(iface);
1476 HRESULT hr = E_FAIL;
1477 struct passwd *pPasswd;
1478 struct group *pGroup;
1479 struct stat statItem;
1481 static const shvheader unixfs_header[SHELLVIEWCOLUMNS] = {
1482 {IDS_SHV_COLUMN1, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
1483 {IDS_SHV_COLUMN2, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1484 {IDS_SHV_COLUMN3, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1485 {IDS_SHV_COLUMN4, SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12},
1486 {IDS_SHV_COLUMN5, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 9},
1487 {IDS_SHV_COLUMN10, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
1488 {IDS_SHV_COLUMN11, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7}
1491 TRACE("(%p)->(%p %d %p)\n", This, pidl, iColumn, psd);
1493 if (!psd || iColumn >= SHELLVIEWCOLUMNS)
1494 return E_INVALIDARG;
1496 if (!pidl)
1497 return SHELL32_GetColumnDetails(unixfs_header, iColumn, psd);
1499 if (iColumn == 4 || iColumn == 5 || iColumn == 6) {
1500 char szPath[FILENAME_MAX];
1501 strcpy(szPath, This->m_pszPath);
1502 if (!UNIXFS_filename_from_shitemid(pidl, szPath + strlen(szPath)))
1503 return E_INVALIDARG;
1504 if (stat(szPath, &statItem))
1505 return E_INVALIDARG;
1508 psd->str.u.cStr[0] = '\0';
1509 psd->str.uType = STRRET_CSTR;
1511 switch (iColumn) {
1512 case 0:
1513 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL|SHGDN_INFOLDER, &psd->str);
1514 break;
1515 case 1:
1516 _ILGetFileSize(pidl, psd->str.u.cStr, MAX_PATH);
1517 break;
1518 case 2:
1519 _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
1520 break;
1521 case 3:
1522 _ILGetFileDate(pidl, psd->str.u.cStr, MAX_PATH);
1523 break;
1524 case 4:
1525 psd->str.u.cStr[0] = S_ISDIR(statItem.st_mode) ? 'd' : '-';
1526 psd->str.u.cStr[1] = (statItem.st_mode & S_IRUSR) ? 'r' : '-';
1527 psd->str.u.cStr[2] = (statItem.st_mode & S_IWUSR) ? 'w' : '-';
1528 psd->str.u.cStr[3] = (statItem.st_mode & S_IXUSR) ? 'x' : '-';
1529 psd->str.u.cStr[4] = (statItem.st_mode & S_IRGRP) ? 'r' : '-';
1530 psd->str.u.cStr[5] = (statItem.st_mode & S_IWGRP) ? 'w' : '-';
1531 psd->str.u.cStr[6] = (statItem.st_mode & S_IXGRP) ? 'x' : '-';
1532 psd->str.u.cStr[7] = (statItem.st_mode & S_IROTH) ? 'r' : '-';
1533 psd->str.u.cStr[8] = (statItem.st_mode & S_IWOTH) ? 'w' : '-';
1534 psd->str.u.cStr[9] = (statItem.st_mode & S_IXOTH) ? 'x' : '-';
1535 psd->str.u.cStr[10] = '\0';
1536 break;
1537 case 5:
1538 pPasswd = getpwuid(statItem.st_uid);
1539 if (pPasswd) strcpy(psd->str.u.cStr, pPasswd->pw_name);
1540 break;
1541 case 6:
1542 pGroup = getgrgid(statItem.st_gid);
1543 if (pGroup) strcpy(psd->str.u.cStr, pGroup->gr_name);
1544 break;
1547 return hr;
1550 static HRESULT WINAPI ShellFolder2_MapColumnToSCID(IShellFolder2* iface, UINT column,
1551 SHCOLUMNID *pscid)
1553 UnixFolder *This = impl_from_IShellFolder2(iface);
1554 FIXME("(%p)->(%u %p): stub\n", This, column, pscid);
1555 return E_NOTIMPL;
1558 static const IShellFolder2Vtbl ShellFolder2Vtbl = {
1559 ShellFolder2_QueryInterface,
1560 ShellFolder2_AddRef,
1561 ShellFolder2_Release,
1562 ShellFolder2_ParseDisplayName,
1563 ShellFolder2_EnumObjects,
1564 ShellFolder2_BindToObject,
1565 ShellFolder2_BindToStorage,
1566 ShellFolder2_CompareIDs,
1567 ShellFolder2_CreateViewObject,
1568 ShellFolder2_GetAttributesOf,
1569 ShellFolder2_GetUIObjectOf,
1570 ShellFolder2_GetDisplayNameOf,
1571 ShellFolder2_SetNameOf,
1572 ShellFolder2_GetDefaultSearchGUID,
1573 ShellFolder2_EnumSearches,
1574 ShellFolder2_GetDefaultColumn,
1575 ShellFolder2_GetDefaultColumnState,
1576 ShellFolder2_GetDetailsEx,
1577 ShellFolder2_GetDetailsOf,
1578 ShellFolder2_MapColumnToSCID
1581 static HRESULT WINAPI PersistFolder3_QueryInterface(IPersistFolder3* iface, REFIID riid,
1582 void** ppvObject)
1584 UnixFolder *This = impl_from_IPersistFolder3(iface);
1585 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppvObject);
1588 static ULONG WINAPI PersistFolder3_AddRef(IPersistFolder3* iface)
1590 UnixFolder *This = impl_from_IPersistFolder3(iface);
1591 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
1594 static ULONG WINAPI PersistFolder3_Release(IPersistFolder3* iface)
1596 UnixFolder *This = impl_from_IPersistFolder3(iface);
1597 return IShellFolder2_Release(&This->IShellFolder2_iface);
1600 static HRESULT WINAPI PersistFolder3_GetClassID(IPersistFolder3* iface, CLSID* pClassID)
1602 UnixFolder *This = impl_from_IPersistFolder3(iface);
1604 TRACE("(%p)->(%p)\n", This, pClassID);
1606 if (!pClassID)
1607 return E_INVALIDARG;
1609 *pClassID = *This->m_pCLSID;
1610 return S_OK;
1613 static HRESULT WINAPI PersistFolder3_Initialize(IPersistFolder3* iface, LPCITEMIDLIST pidl)
1615 UnixFolder *This = impl_from_IPersistFolder3(iface);
1616 LPCITEMIDLIST current = pidl;
1617 char szBasePath[FILENAME_MAX] = "/";
1619 TRACE("(%p)->(%p)\n", This, pidl);
1621 /* Find the UnixFolderClass root */
1622 while (current->mkid.cb) {
1623 if ((_ILIsDrive(current) && IsEqualCLSID(This->m_pCLSID, &CLSID_ShellFSFolder)) ||
1624 (_ILIsSpecialFolder(current) && IsEqualCLSID(This->m_pCLSID, _ILGetGUIDPointer(current))))
1626 break;
1628 current = ILGetNext(current);
1631 if (current->mkid.cb) {
1632 if (_ILIsDrive(current)) {
1633 WCHAR wszDrive[] = { '?', ':', '\\', 0 };
1634 wszDrive[0] = (WCHAR)*_ILGetTextPointer(current);
1635 if (!UNIXFS_get_unix_path(wszDrive, szBasePath))
1636 return E_FAIL;
1637 } else if (IsEqualIID(&CLSID_MyDocuments, _ILGetGUIDPointer(current))) {
1638 WCHAR wszMyDocumentsPath[MAX_PATH];
1639 if (!SHGetSpecialFolderPathW(0, wszMyDocumentsPath, CSIDL_PERSONAL, FALSE))
1640 return E_FAIL;
1641 PathAddBackslashW(wszMyDocumentsPath);
1642 if (!UNIXFS_get_unix_path(wszMyDocumentsPath, szBasePath))
1643 return E_FAIL;
1645 current = ILGetNext(current);
1646 } else if (_ILIsDesktop(pidl) || _ILIsValue(pidl) || _ILIsFolder(pidl)) {
1647 /* Path rooted at Desktop */
1648 WCHAR wszDesktopPath[MAX_PATH];
1649 if (!SHGetSpecialFolderPathW(0, wszDesktopPath, CSIDL_DESKTOPDIRECTORY, FALSE))
1650 return E_FAIL;
1651 PathAddBackslashW(wszDesktopPath);
1652 if (!UNIXFS_get_unix_path(wszDesktopPath, szBasePath))
1653 return E_FAIL;
1654 current = pidl;
1655 } else if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
1656 /* FolderShortcuts' Initialize method only sets the ITEMIDLIST, which
1657 * specifies the location in the shell namespace, but leaves the
1658 * target folder (m_pszPath) alone. See unit tests in tests/shlfolder.c */
1659 This->m_pidlLocation = ILClone(pidl);
1660 return S_OK;
1661 } else {
1662 ERR("Unknown pidl type!\n");
1663 pdump(pidl);
1664 return E_INVALIDARG;
1667 This->m_pidlLocation = ILClone(pidl);
1668 return UNIXFS_initialize_target_folder(This, szBasePath, current, 0);
1671 static HRESULT WINAPI PersistFolder3_GetCurFolder(IPersistFolder3* iface, LPITEMIDLIST* ppidl)
1673 UnixFolder *This = impl_from_IPersistFolder3(iface);
1675 TRACE ("(iface=%p, ppidl=%p)\n", iface, ppidl);
1677 if (!ppidl)
1678 return E_POINTER;
1679 *ppidl = ILClone (This->m_pidlLocation);
1680 return S_OK;
1683 static HRESULT WINAPI PersistFolder3_InitializeEx(IPersistFolder3 *iface, IBindCtx *pbc,
1684 LPCITEMIDLIST pidlRoot, const PERSIST_FOLDER_TARGET_INFO *ppfti)
1686 UnixFolder *This = impl_from_IPersistFolder3(iface);
1687 WCHAR wszTargetDosPath[MAX_PATH];
1688 char szTargetPath[FILENAME_MAX] = "";
1690 TRACE("(%p)->(%p %p %p)\n", This, pbc, pidlRoot, ppfti);
1692 /* If no PERSIST_FOLDER_TARGET_INFO is given InitializeEx is equivalent to Initialize. */
1693 if (!ppfti)
1694 return IPersistFolder3_Initialize(iface, pidlRoot);
1696 if (ppfti->csidl != -1) {
1697 if (FAILED(SHGetFolderPathW(0, ppfti->csidl, NULL, 0, wszTargetDosPath)) ||
1698 !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1700 return E_FAIL;
1702 } else if (*ppfti->szTargetParsingName) {
1703 lstrcpyW(wszTargetDosPath, ppfti->szTargetParsingName);
1704 PathAddBackslashW(wszTargetDosPath);
1705 if (!UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath)) {
1706 return E_FAIL;
1708 } else if (ppfti->pidlTargetFolder) {
1709 if (!SHGetPathFromIDListW(ppfti->pidlTargetFolder, wszTargetDosPath) ||
1710 !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1712 return E_FAIL;
1714 } else {
1715 return E_FAIL;
1718 This->m_pszPath = SHAlloc(lstrlenA(szTargetPath)+1);
1719 if (!This->m_pszPath)
1720 return E_FAIL;
1721 lstrcpyA(This->m_pszPath, szTargetPath);
1722 This->m_pidlLocation = ILClone(pidlRoot);
1723 This->m_dwAttributes = (ppfti->dwAttributes != -1) ? ppfti->dwAttributes :
1724 (SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME|SFGAO_FILESYSTEM);
1726 return S_OK;
1729 static HRESULT WINAPI PersistFolder3_GetFolderTargetInfo(IPersistFolder3 *iface,
1730 PERSIST_FOLDER_TARGET_INFO *ppfti)
1732 UnixFolder *This = impl_from_IPersistFolder3(iface);
1733 FIXME("(%p)->(%p): stub\n", This, ppfti);
1734 return E_NOTIMPL;
1737 static const IPersistFolder3Vtbl PersistFolder3Vtbl = {
1738 PersistFolder3_QueryInterface,
1739 PersistFolder3_AddRef,
1740 PersistFolder3_Release,
1741 PersistFolder3_GetClassID,
1742 PersistFolder3_Initialize,
1743 PersistFolder3_GetCurFolder,
1744 PersistFolder3_InitializeEx,
1745 PersistFolder3_GetFolderTargetInfo
1748 static HRESULT WINAPI PersistPropertyBag_QueryInterface(IPersistPropertyBag* iface,
1749 REFIID riid, void** ppv)
1751 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1752 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppv);
1755 static ULONG WINAPI PersistPropertyBag_AddRef(IPersistPropertyBag* iface)
1757 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1758 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
1761 static ULONG WINAPI PersistPropertyBag_Release(IPersistPropertyBag* iface)
1763 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1764 return IShellFolder2_Release(&This->IShellFolder2_iface);
1767 static HRESULT WINAPI PersistPropertyBag_GetClassID(IPersistPropertyBag* iface, CLSID* pClassID)
1769 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1770 return IPersistFolder3_GetClassID(&This->IPersistFolder3_iface, pClassID);
1773 static HRESULT WINAPI PersistPropertyBag_InitNew(IPersistPropertyBag* iface)
1775 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1776 FIXME("(%p): stub\n", This);
1777 return E_NOTIMPL;
1780 static HRESULT WINAPI PersistPropertyBag_Load(IPersistPropertyBag *iface,
1781 IPropertyBag *pPropertyBag, IErrorLog *pErrorLog)
1783 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1785 static const WCHAR wszTarget[] = { 'T','a','r','g','e','t', 0 }, wszNull[] = { 0 };
1786 PERSIST_FOLDER_TARGET_INFO pftiTarget;
1787 VARIANT var;
1788 HRESULT hr;
1790 TRACE("(%p)->(%p %p)\n", This, pPropertyBag, pErrorLog);
1792 if (!pPropertyBag)
1793 return E_POINTER;
1795 /* Get 'Target' property from the property bag. */
1796 V_VT(&var) = VT_BSTR;
1797 hr = IPropertyBag_Read(pPropertyBag, wszTarget, &var, NULL);
1798 if (FAILED(hr))
1799 return E_FAIL;
1800 lstrcpyW(pftiTarget.szTargetParsingName, V_BSTR(&var));
1801 SysFreeString(V_BSTR(&var));
1803 pftiTarget.pidlTargetFolder = NULL;
1804 lstrcpyW(pftiTarget.szNetworkProvider, wszNull);
1805 pftiTarget.dwAttributes = -1;
1806 pftiTarget.csidl = -1;
1808 return IPersistFolder3_InitializeEx(&This->IPersistFolder3_iface, NULL, NULL, &pftiTarget);
1811 static HRESULT WINAPI PersistPropertyBag_Save(IPersistPropertyBag *iface,
1812 IPropertyBag *pPropertyBag, BOOL fClearDirty, BOOL fSaveAllProperties)
1814 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1815 FIXME("(%p): stub\n", This);
1816 return E_NOTIMPL;
1819 static const IPersistPropertyBagVtbl PersistPropertyBagVtbl = {
1820 PersistPropertyBag_QueryInterface,
1821 PersistPropertyBag_AddRef,
1822 PersistPropertyBag_Release,
1823 PersistPropertyBag_GetClassID,
1824 PersistPropertyBag_InitNew,
1825 PersistPropertyBag_Load,
1826 PersistPropertyBag_Save
1829 static HRESULT WINAPI SFHelper_QueryInterface(ISFHelper* iface, REFIID riid, void** ppvObject)
1831 UnixFolder *This = impl_from_ISFHelper(iface);
1832 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppvObject);
1835 static ULONG WINAPI SFHelper_AddRef(ISFHelper* iface)
1837 UnixFolder *This = impl_from_ISFHelper(iface);
1838 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
1841 static ULONG WINAPI SFHelper_Release(ISFHelper* iface)
1843 UnixFolder *This = impl_from_ISFHelper(iface);
1844 return IShellFolder2_Release(&This->IShellFolder2_iface);
1847 static HRESULT WINAPI SFHelper_GetUniqueName(ISFHelper* iface, LPWSTR pwszName, UINT uLen)
1849 UnixFolder *This = impl_from_ISFHelper(iface);
1850 IEnumIDList *pEnum;
1851 HRESULT hr;
1852 LPITEMIDLIST pidlElem;
1853 DWORD dwFetched;
1854 int i;
1855 WCHAR wszNewFolder[25];
1856 static const WCHAR wszFormat[] = { '%','s',' ','%','d',0 };
1858 TRACE("(%p)->(%p %u)\n", This, pwszName, uLen);
1860 LoadStringW(shell32_hInstance, IDS_NEWFOLDER, wszNewFolder, sizeof(wszNewFolder)/sizeof(WCHAR));
1862 if (uLen < sizeof(wszNewFolder)/sizeof(WCHAR)+3)
1863 return E_INVALIDARG;
1865 hr = IShellFolder2_EnumObjects(&This->IShellFolder2_iface, 0,
1866 SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN, &pEnum);
1867 if (SUCCEEDED(hr)) {
1868 lstrcpynW(pwszName, wszNewFolder, uLen);
1869 IEnumIDList_Reset(pEnum);
1870 i = 2;
1871 while ((IEnumIDList_Next(pEnum, 1, &pidlElem, &dwFetched) == S_OK) && (dwFetched == 1)) {
1872 WCHAR wszTemp[MAX_PATH];
1873 _ILSimpleGetTextW(pidlElem, wszTemp, MAX_PATH);
1874 if (!lstrcmpiW(wszTemp, pwszName)) {
1875 IEnumIDList_Reset(pEnum);
1876 snprintfW(pwszName, uLen, wszFormat, wszNewFolder, i++);
1877 if (i > 99) {
1878 hr = E_FAIL;
1879 break;
1883 IEnumIDList_Release(pEnum);
1885 return hr;
1888 static HRESULT WINAPI SFHelper_AddFolder(ISFHelper* iface, HWND hwnd, LPCWSTR pwszName,
1889 LPITEMIDLIST* ppidlOut)
1891 UnixFolder *This = impl_from_ISFHelper(iface);
1892 char szNewDir[FILENAME_MAX];
1893 int cBaseLen;
1895 TRACE("(%p)->(%p %s %p)\n", This, hwnd, debugstr_w(pwszName), ppidlOut);
1897 if (ppidlOut)
1898 *ppidlOut = NULL;
1900 if (!This->m_pszPath || !(This->m_dwAttributes & SFGAO_FILESYSTEM))
1901 return E_FAIL;
1903 lstrcpynA(szNewDir, This->m_pszPath, FILENAME_MAX);
1904 cBaseLen = lstrlenA(szNewDir);
1905 WideCharToMultiByte(CP_UNIXCP, 0, pwszName, -1, szNewDir+cBaseLen, FILENAME_MAX-cBaseLen, 0, 0);
1907 if (mkdir(szNewDir, 0777)) {
1908 char szMessage[256 + FILENAME_MAX];
1909 char szCaption[256];
1911 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_DENIED, szCaption, sizeof(szCaption));
1912 sprintf(szMessage, szCaption, szNewDir);
1913 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_CAPTION, szCaption, sizeof(szCaption));
1914 MessageBoxA(hwnd, szMessage, szCaption, MB_OK | MB_ICONEXCLAMATION);
1916 return E_FAIL;
1917 } else {
1918 LPITEMIDLIST pidlRelative;
1920 /* Inform the shell */
1921 if (SUCCEEDED(UNIXFS_path_to_pidl(This, NULL, pwszName, &pidlRelative))) {
1922 LPITEMIDLIST pidlAbsolute = ILCombine(This->m_pidlLocation, pidlRelative);
1923 if (ppidlOut)
1924 *ppidlOut = pidlRelative;
1925 else
1926 ILFree(pidlRelative);
1927 SHChangeNotify(SHCNE_MKDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1928 ILFree(pidlAbsolute);
1929 } else return E_FAIL;
1930 return S_OK;
1935 * Delete specified files by converting the path to DOS paths and calling
1936 * SHFileOperationW. If an error occurs it returns an error code. If the paths can't
1937 * be converted, S_FALSE is returned. In such situation DeleteItems will try to delete
1938 * the files using syscalls
1940 static HRESULT UNIXFS_delete_with_shfileop(UnixFolder *This, UINT cidl, const LPCITEMIDLIST *apidl)
1942 char szAbsolute[FILENAME_MAX], *pszRelative;
1943 LPWSTR wszPathsList, wszListPos;
1944 SHFILEOPSTRUCTW op;
1945 HRESULT ret;
1946 UINT i;
1948 lstrcpyA(szAbsolute, This->m_pszPath);
1949 pszRelative = szAbsolute + lstrlenA(szAbsolute);
1951 wszListPos = wszPathsList = HeapAlloc(GetProcessHeap(), 0, cidl*MAX_PATH*sizeof(WCHAR)+1);
1952 if (wszPathsList == NULL)
1953 return E_OUTOFMEMORY;
1954 for (i=0; i<cidl; i++) {
1955 LPWSTR wszDosPath;
1957 if (!_ILIsFolder(apidl[i]) && !_ILIsValue(apidl[i]))
1958 continue;
1959 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
1961 HeapFree(GetProcessHeap(), 0, wszPathsList);
1962 return E_INVALIDARG;
1964 wszDosPath = wine_get_dos_file_name(szAbsolute);
1965 if (wszDosPath == NULL || lstrlenW(wszDosPath) >= MAX_PATH)
1967 HeapFree(GetProcessHeap(), 0, wszPathsList);
1968 HeapFree(GetProcessHeap(), 0, wszDosPath);
1969 return S_FALSE;
1971 lstrcpyW(wszListPos, wszDosPath);
1972 wszListPos += lstrlenW(wszListPos)+1;
1973 HeapFree(GetProcessHeap(), 0, wszDosPath);
1975 *wszListPos = 0;
1977 ZeroMemory(&op, sizeof(op));
1978 op.hwnd = GetActiveWindow();
1979 op.wFunc = FO_DELETE;
1980 op.pFrom = wszPathsList;
1981 op.fFlags = FOF_ALLOWUNDO;
1982 if (!SHFileOperationW(&op))
1984 WARN("SHFileOperationW failed\n");
1985 ret = E_FAIL;
1987 else
1988 ret = S_OK;
1990 HeapFree(GetProcessHeap(), 0, wszPathsList);
1991 return ret;
1994 static HRESULT UNIXFS_delete_with_syscalls(UnixFolder *This, UINT cidl, const LPCITEMIDLIST *apidl)
1996 char szAbsolute[FILENAME_MAX], *pszRelative;
1997 static const WCHAR empty[] = {0};
1998 UINT i;
2000 if (!SHELL_ConfirmYesNoW(GetActiveWindow(), ASK_DELETE_SELECTED, empty))
2001 return S_OK;
2003 lstrcpyA(szAbsolute, This->m_pszPath);
2004 pszRelative = szAbsolute + lstrlenA(szAbsolute);
2006 for (i=0; i<cidl; i++) {
2007 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
2008 return E_INVALIDARG;
2009 if (_ILIsFolder(apidl[i])) {
2010 if (rmdir(szAbsolute))
2011 return E_FAIL;
2012 } else if (_ILIsValue(apidl[i])) {
2013 if (unlink(szAbsolute))
2014 return E_FAIL;
2017 return S_OK;
2020 static HRESULT WINAPI SFHelper_DeleteItems(ISFHelper* iface, UINT cidl, LPCITEMIDLIST* apidl)
2022 UnixFolder *This = impl_from_ISFHelper(iface);
2023 char szAbsolute[FILENAME_MAX], *pszRelative;
2024 LPITEMIDLIST pidlAbsolute;
2025 HRESULT hr = S_OK;
2026 UINT i;
2027 struct stat st;
2029 TRACE("(%p)->(%d %p)\n", This, cidl, apidl);
2031 hr = UNIXFS_delete_with_shfileop(This, cidl, apidl);
2032 if (hr == S_FALSE)
2033 hr = UNIXFS_delete_with_syscalls(This, cidl, apidl);
2035 lstrcpyA(szAbsolute, This->m_pszPath);
2036 pszRelative = szAbsolute + lstrlenA(szAbsolute);
2038 /* we need to manually send the notifies if the files doesn't exist */
2039 for (i=0; i<cidl; i++) {
2040 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
2041 continue;
2042 pidlAbsolute = ILCombine(This->m_pidlLocation, apidl[i]);
2043 if (stat(szAbsolute, &st))
2045 if (_ILIsFolder(apidl[i])) {
2046 SHChangeNotify(SHCNE_RMDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
2047 } else if (_ILIsValue(apidl[i])) {
2048 SHChangeNotify(SHCNE_DELETE, SHCNF_IDLIST, pidlAbsolute, NULL);
2051 ILFree(pidlAbsolute);
2054 return hr;
2057 static HRESULT WINAPI SFHelper_CopyItems(ISFHelper* iface, IShellFolder *psfFrom,
2058 UINT cidl, LPCITEMIDLIST *apidl)
2060 UnixFolder *This = impl_from_ISFHelper(iface);
2061 DWORD dwAttributes;
2062 UINT i;
2063 HRESULT hr;
2064 char szAbsoluteDst[FILENAME_MAX], *pszRelativeDst;
2066 TRACE("(%p)->(%p %d %p)\n", This, psfFrom, cidl, apidl);
2068 if (!psfFrom || !cidl || !apidl)
2069 return E_INVALIDARG;
2071 /* All source items have to be filesystem items. */
2072 dwAttributes = SFGAO_FILESYSTEM;
2073 hr = IShellFolder_GetAttributesOf(psfFrom, cidl, apidl, &dwAttributes);
2074 if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM))
2075 return E_INVALIDARG;
2077 lstrcpyA(szAbsoluteDst, This->m_pszPath);
2078 pszRelativeDst = szAbsoluteDst + strlen(szAbsoluteDst);
2080 for (i=0; i<cidl; i++) {
2081 WCHAR wszSrc[MAX_PATH];
2082 char szSrc[FILENAME_MAX];
2083 STRRET strret;
2084 HRESULT res;
2085 WCHAR *pwszDosSrc, *pwszDosDst;
2087 /* Build the unix path of the current source item. */
2088 if (FAILED(IShellFolder_GetDisplayNameOf(psfFrom, apidl[i], SHGDN_FORPARSING, &strret)))
2089 return E_FAIL;
2090 if (FAILED(StrRetToBufW(&strret, apidl[i], wszSrc, MAX_PATH)))
2091 return E_FAIL;
2092 if (!UNIXFS_get_unix_path(wszSrc, szSrc))
2093 return E_FAIL;
2095 /* Build the unix path of the current destination item */
2096 UNIXFS_filename_from_shitemid(apidl[i], pszRelativeDst);
2098 pwszDosSrc = wine_get_dos_file_name(szSrc);
2099 pwszDosDst = wine_get_dos_file_name(szAbsoluteDst);
2101 if (pwszDosSrc && pwszDosDst)
2102 res = UNIXFS_copy(pwszDosSrc, pwszDosDst);
2103 else
2104 res = E_OUTOFMEMORY;
2106 HeapFree(GetProcessHeap(), 0, pwszDosSrc);
2107 HeapFree(GetProcessHeap(), 0, pwszDosDst);
2109 if (res != S_OK)
2110 return res;
2112 return S_OK;
2115 static const ISFHelperVtbl SFHelperVtbl = {
2116 SFHelper_QueryInterface,
2117 SFHelper_AddRef,
2118 SFHelper_Release,
2119 SFHelper_GetUniqueName,
2120 SFHelper_AddFolder,
2121 SFHelper_DeleteItems,
2122 SFHelper_CopyItems
2125 static HRESULT WINAPI DropTarget_QueryInterface(IDropTarget* iface, REFIID riid, void** ppvObject)
2127 UnixFolder *This = impl_from_IDropTarget(iface);
2128 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppvObject);
2131 static ULONG WINAPI DropTarget_AddRef(IDropTarget* iface)
2133 UnixFolder *This = impl_from_IDropTarget(iface);
2134 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
2137 static ULONG WINAPI DropTarget_Release(IDropTarget* iface)
2139 UnixFolder *This = impl_from_IDropTarget(iface);
2140 return IShellFolder2_Release(&This->IShellFolder2_iface);
2143 #define HIDA_GetPIDLFolder(pida) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[0])
2144 #define HIDA_GetPIDLItem(pida, i) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[i+1])
2146 static HRESULT WINAPI DropTarget_DragEnter(IDropTarget *iface, IDataObject *pDataObject,
2147 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2149 UnixFolder *This = impl_from_IDropTarget(iface);
2150 FORMATETC format;
2151 STGMEDIUM medium;
2153 TRACE("(%p)->(%p 0x%08x {.x=%d, .y=%d} %p)\n", This, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
2155 if (!pdwEffect || !pDataObject)
2156 return E_INVALIDARG;
2158 /* Compute a mask of supported drop-effects for this shellfolder object and the given data
2159 * object. Dropping is only supported on folders, which represent filesystem locations. One
2160 * can't drop on file objects. And the 'move' drop effect is only supported, if the source
2161 * folder is not identical to the target folder. */
2162 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2163 InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
2164 if ((This->m_dwAttributes & SFGAO_FILESYSTEM) && /* Only drop to filesystem folders */
2165 _ILIsFolder(ILFindLastID(This->m_pidlLocation)) && /* Only drop to folders, not to files */
2166 SUCCEEDED(IDataObject_GetData(pDataObject, &format, &medium))) /* Only ShellIDList format */
2168 LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
2169 This->m_dwDropEffectsMask |= DROPEFFECT_COPY|DROPEFFECT_LINK;
2171 if (pidaShellIDList) { /* Files can only be moved between two different folders */
2172 if (!ILIsEqual(HIDA_GetPIDLFolder(pidaShellIDList), This->m_pidlLocation))
2173 This->m_dwDropEffectsMask |= DROPEFFECT_MOVE;
2174 GlobalUnlock(medium.u.hGlobal);
2178 *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
2180 return S_OK;
2183 static HRESULT WINAPI DropTarget_DragOver(IDropTarget *iface, DWORD dwKeyState,
2184 POINTL pt, DWORD *pdwEffect)
2186 UnixFolder *This = impl_from_IDropTarget(iface);
2188 TRACE("(%p)->(0x%08x {.x=%d, .y=%d} %p)\n", This, dwKeyState, pt.x, pt.y, pdwEffect);
2190 if (!pdwEffect)
2191 return E_INVALIDARG;
2193 *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
2195 return S_OK;
2198 static HRESULT WINAPI DropTarget_DragLeave(IDropTarget *iface)
2200 UnixFolder *This = impl_from_IDropTarget(iface);
2202 TRACE("(%p)\n", This);
2204 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2205 return S_OK;
2208 static HRESULT WINAPI DropTarget_Drop(IDropTarget *iface, IDataObject *pDataObject,
2209 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2211 UnixFolder *This = impl_from_IDropTarget(iface);
2212 FORMATETC format;
2213 STGMEDIUM medium;
2214 HRESULT hr;
2216 TRACE("(%p)->(%p %d {.x=%d, .y=%d} %p) semi-stub\n",
2217 This, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
2219 InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
2220 hr = IDataObject_GetData(pDataObject, &format, &medium);
2221 if (FAILED(hr))
2222 return hr;
2224 if (medium.tymed == TYMED_HGLOBAL) {
2225 IShellFolder *psfSourceFolder, *psfDesktopFolder;
2226 LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
2227 STRRET strret;
2228 UINT i;
2230 if (!pidaShellIDList)
2231 return HRESULT_FROM_WIN32(GetLastError());
2233 hr = SHGetDesktopFolder(&psfDesktopFolder);
2234 if (FAILED(hr)) {
2235 GlobalUnlock(medium.u.hGlobal);
2236 return hr;
2239 hr = IShellFolder_BindToObject(psfDesktopFolder, HIDA_GetPIDLFolder(pidaShellIDList), NULL,
2240 &IID_IShellFolder, (LPVOID*)&psfSourceFolder);
2241 IShellFolder_Release(psfDesktopFolder);
2242 if (FAILED(hr)) {
2243 GlobalUnlock(medium.u.hGlobal);
2244 return hr;
2247 for (i = 0; i < pidaShellIDList->cidl; i++) {
2248 WCHAR wszSourcePath[MAX_PATH];
2250 hr = IShellFolder_GetDisplayNameOf(psfSourceFolder, HIDA_GetPIDLItem(pidaShellIDList, i),
2251 SHGDN_FORPARSING, &strret);
2252 if (FAILED(hr))
2253 break;
2255 hr = StrRetToBufW(&strret, NULL, wszSourcePath, MAX_PATH);
2256 if (FAILED(hr))
2257 break;
2259 switch (*pdwEffect) {
2260 case DROPEFFECT_MOVE:
2261 FIXME("Move %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2262 break;
2263 case DROPEFFECT_COPY:
2264 FIXME("Copy %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2265 break;
2266 case DROPEFFECT_LINK:
2267 FIXME("Link %s from %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2268 break;
2272 IShellFolder_Release(psfSourceFolder);
2273 GlobalUnlock(medium.u.hGlobal);
2274 return hr;
2277 return E_NOTIMPL;
2280 static const IDropTargetVtbl DropTargetVtbl = {
2281 DropTarget_QueryInterface,
2282 DropTarget_AddRef,
2283 DropTarget_Release,
2284 DropTarget_DragEnter,
2285 DropTarget_DragOver,
2286 DropTarget_DragLeave,
2287 DropTarget_Drop
2290 /******************************************************************************
2291 * Unix[Dos]Folder_Constructor [Internal]
2293 * PARAMS
2294 * pUnkOuter [I] Outer class for aggregation. Currently ignored.
2295 * riid [I] Interface asked for by the client.
2296 * ppv [O] Pointer to an riid interface to the UnixFolder object.
2298 * NOTES
2299 * Those are the only functions exported from shfldr_unixfs.c. They are called from
2300 * shellole.c's default class factory and thus have to exhibit a LPFNCREATEINSTANCE
2301 * compatible signature.
2303 * The UnixDosFolder_Constructor sets the dwPathMode member to PATHMODE_DOS. This
2304 * means that paths are converted from dos to unix and back at the interfaces.
2306 static HRESULT CreateUnixFolder(IUnknown *outer, REFIID riid, void **ppv, const CLSID *clsid)
2308 UnixFolder *This;
2309 HRESULT hr;
2311 if (outer) {
2312 FIXME("Aggregation not yet implemented!\n");
2313 return CLASS_E_NOAGGREGATION;
2316 This = SHAlloc((ULONG)sizeof(UnixFolder));
2317 if (!This) return E_OUTOFMEMORY;
2319 This->IShellFolder2_iface.lpVtbl = &ShellFolder2Vtbl;
2320 This->IPersistFolder3_iface.lpVtbl = &PersistFolder3Vtbl;
2321 This->IPersistPropertyBag_iface.lpVtbl = &PersistPropertyBagVtbl;
2322 This->ISFHelper_iface.lpVtbl = &SFHelperVtbl;
2323 This->IDropTarget_iface.lpVtbl = &DropTargetVtbl;
2324 This->ref = 1;
2325 This->m_pszPath = NULL;
2326 This->m_pidlLocation = NULL;
2327 This->m_dwPathMode = IsEqualCLSID(&CLSID_UnixFolder, clsid) ? PATHMODE_UNIX : PATHMODE_DOS;
2328 This->m_dwAttributes = 0;
2329 This->m_pCLSID = clsid;
2330 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2332 hr = IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppv);
2333 IShellFolder2_Release(&This->IShellFolder2_iface);
2335 return hr;
2338 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2339 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2340 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixFolder);
2343 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2344 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2345 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixDosFolder);
2348 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2349 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2350 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_FolderShortcut);
2353 HRESULT WINAPI MyDocuments_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2354 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2355 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_MyDocuments);
2358 /******************************************************************************
2359 * UnixSubFolderIterator
2361 * Class whose heap based objects represent iterators over the sub-directories
2362 * of a given UnixFolder object.
2365 /* UnixSubFolderIterator object layout and typedef.
2367 typedef struct _UnixSubFolderIterator {
2368 IEnumIDList IEnumIDList_iface;
2369 LONG ref;
2370 SHCONTF m_fFilter;
2371 DIR *m_dirFolder;
2372 char m_szFolder[FILENAME_MAX];
2373 } UnixSubFolderIterator;
2375 static inline UnixSubFolderIterator *impl_from_IEnumIDList(IEnumIDList *iface)
2377 return CONTAINING_RECORD(iface, UnixSubFolderIterator, IEnumIDList_iface);
2380 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
2381 TRACE("(iterator=%p)\n", iterator);
2383 if (iterator->m_dirFolder)
2384 closedir(iterator->m_dirFolder);
2385 SHFree(iterator);
2388 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface,
2389 REFIID riid, void** ppv)
2391 TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
2393 if (!ppv) return E_INVALIDARG;
2395 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
2396 *ppv = iface;
2397 } else {
2398 *ppv = NULL;
2399 return E_NOINTERFACE;
2402 IEnumIDList_AddRef(iface);
2403 return S_OK;
2406 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
2408 UnixSubFolderIterator *This = impl_from_IEnumIDList(iface);
2409 ULONG ref = InterlockedIncrement(&This->ref);
2411 TRACE("(%p) ref=%d\n", This, ref);
2413 return ref;
2416 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
2418 UnixSubFolderIterator *This = impl_from_IEnumIDList(iface);
2419 ULONG ref = InterlockedDecrement(&This->ref);
2421 TRACE("(%p) ref=%d\n", This, ref);
2423 if (!ref)
2424 UnixSubFolderIterator_Destroy(This);
2426 return ref;
2429 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt,
2430 LPITEMIDLIST* rgelt, ULONG* pceltFetched)
2432 UnixSubFolderIterator *This = impl_from_IEnumIDList(iface);
2433 ULONG i = 0;
2435 /* This->m_dirFolder will be NULL if the user doesn't have access rights for the dir. */
2436 if (This->m_dirFolder) {
2437 char *pszRelativePath = This->m_szFolder + lstrlenA(This->m_szFolder);
2438 struct dirent *pDirEntry;
2440 while (i < celt) {
2441 pDirEntry = readdir(This->m_dirFolder);
2442 if (!pDirEntry) break; /* No more entries */
2443 if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
2445 /* Temporarily build absolute path in This->m_szFolder. Then construct a pidl
2446 * and see if it passes the filter.
2448 lstrcpyA(pszRelativePath, pDirEntry->d_name);
2449 rgelt[i] = SHAlloc(
2450 UNIXFS_shitemid_len_from_filename(pszRelativePath, NULL, NULL)+sizeof(USHORT));
2451 if (!UNIXFS_build_shitemid(This->m_szFolder, TRUE, NULL, rgelt[i]) ||
2452 !UNIXFS_is_pidl_of_type(rgelt[i], This->m_fFilter))
2454 SHFree(rgelt[i]);
2455 rgelt[i] = NULL;
2456 continue;
2458 memset(((PBYTE)rgelt[i])+rgelt[i]->mkid.cb, 0, sizeof(USHORT));
2459 i++;
2461 *pszRelativePath = '\0'; /* Restore the original path in This->m_szFolder. */
2464 if (pceltFetched)
2465 *pceltFetched = i;
2467 return (i == 0) ? S_FALSE : S_OK;
2470 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
2472 LPITEMIDLIST *apidl;
2473 ULONG cFetched;
2474 HRESULT hr;
2476 TRACE("(iface=%p, celt=%d)\n", iface, celt);
2478 /* Call IEnumIDList::Next and delete the resulting pidls. */
2479 apidl = SHAlloc(celt * sizeof(LPITEMIDLIST));
2480 hr = IEnumIDList_Next(iface, celt, apidl, &cFetched);
2481 if (SUCCEEDED(hr))
2482 while (cFetched--)
2483 SHFree(apidl[cFetched]);
2484 SHFree(apidl);
2486 return hr;
2489 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
2491 UnixSubFolderIterator *This = impl_from_IEnumIDList(iface);
2493 TRACE("(iface=%p)\n", iface);
2495 if (This->m_dirFolder)
2496 rewinddir(This->m_dirFolder);
2498 return S_OK;
2501 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This,
2502 IEnumIDList** ppenum)
2504 FIXME("stub\n");
2505 return E_NOTIMPL;
2508 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
2510 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
2511 UnixSubFolderIterator_IEnumIDList_QueryInterface,
2512 UnixSubFolderIterator_IEnumIDList_AddRef,
2513 UnixSubFolderIterator_IEnumIDList_Release,
2514 UnixSubFolderIterator_IEnumIDList_Next,
2515 UnixSubFolderIterator_IEnumIDList_Skip,
2516 UnixSubFolderIterator_IEnumIDList_Reset,
2517 UnixSubFolderIterator_IEnumIDList_Clone
2520 static IEnumIDList *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter)
2522 UnixSubFolderIterator *iterator;
2524 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
2526 iterator = SHAlloc(sizeof(*iterator));
2527 iterator->IEnumIDList_iface.lpVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
2528 iterator->ref = 1;
2529 iterator->m_fFilter = fFilter;
2530 iterator->m_dirFolder = opendir(pUnixFolder->m_pszPath);
2531 lstrcpyA(iterator->m_szFolder, pUnixFolder->m_pszPath);
2533 return &iterator->IEnumIDList_iface;
2536 #else /* __MINGW32__ || _MSC_VER */
2538 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2540 return E_NOTIMPL;
2543 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2545 return E_NOTIMPL;
2548 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2550 return E_NOTIMPL;
2553 HRESULT WINAPI MyDocuments_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2555 return E_NOTIMPL;
2558 #endif /* __MINGW32__ || _MSC_VER */
2560 /******************************************************************************
2561 * UNIXFS_is_rooted_at_desktop [Internal]
2563 * Checks if the unixfs namespace extension is rooted at desktop level.
2565 * RETURNS
2566 * TRUE, if unixfs is rooted at desktop level
2567 * FALSE, if not.
2569 BOOL UNIXFS_is_rooted_at_desktop(void) {
2570 HKEY hKey;
2571 WCHAR wszRootedAtDesktop[69 + CHARS_IN_GUID] = {
2572 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2573 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2574 'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
2575 'N','a','m','e','S','p','a','c','e','\\',0 };
2577 if (StringFromGUID2(&CLSID_UnixDosFolder, wszRootedAtDesktop + 69, CHARS_IN_GUID) &&
2578 RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszRootedAtDesktop, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
2580 RegCloseKey(hKey);
2581 return TRUE;
2583 return FALSE;