wined3d: Take sRGB_decode into account in texture_srgb_mode.
[wine.git] / dlls / shell32 / shfldr_unixfs.c
blob362d4e6e6567c3d49a7f288c7f60575a4c2f484d
1 /*
2 * UNIXFS - Shell namespace extension for the unix filesystem
4 * Copyright (C) 2005 Michael Jung
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * As you know, windows and unix do have a different philosophy with regard to
23 * the question of how a filesystem should be laid out. While we unix geeks
24 * learned to love the 'one-tree-rooted-at-/' approach, windows has in fact
25 * a whole forest of filesystem trees, each of which is typically identified by
26 * a drive letter.
28 * We would like wine to integrate as smoothly as possible (that is without
29 * sacrificing win32 compatibility) into the unix environment. For the
30 * filesystem question, this means we really would like those windows
31 * applications to work with unix path- and file-names. Unfortunately, this
32 * seems to be impossible in general. Therefore we have those symbolic links
33 * in wine's 'dosdevices' directory, which are used to simulate drives
34 * to keep windows applications happy. And as a consequence, we have those
35 * drive letters show up now and then in GUI applications running under wine,
36 * which gets the unix hardcore fans all angry, shouting at us @#!&$%* wine
37 * hackers that we are seducing the big companies not to port their applications
38 * to unix.
40 * DOS paths do appear at various places in GUI applications. Sometimes, they
41 * show up in the title bar of an application's window. They tend to accumulate
42 * in the most-recently-used section of the file-menu. And I've even seen some
43 * in a configuration dialog's edit control. In those examples, wine can't do a
44 * lot about this, since path-names can't be told apart from ordinary strings
45 * here. That's different in the file dialogs, though.
47 * With the introduction of the 'shell' in win32, Microsoft established an
48 * abstraction layer on top of the filesystem, called the shell namespace (I was
49 * told that GNOME's virtual filesystem is conceptually similar). In the shell
50 * namespace, one doesn't use ascii- or unicode-strings to uniquely identify
51 * objects. Instead Microsoft introduced item-identifier-lists (The c type is
52 * called ITEMIDLIST) as an abstraction of path-names. As you probably would
53 * have guessed, an item-identifier-list is a list of item-identifiers (whose
54 * c type's funny name is SHITEMID), which are opaque binary objects. This means
55 * that no application (apart from Microsoft Office) should make any assumptions
56 * on the internal structure of these SHITEMIDs.
58 * Since the user prefers to be presented the good-old DOS file-names instead of
59 * binary ITEMIDLISTs, a translation method between string-based file-names and
60 * ITEMIDLISTs was established. At the core of this are the COM-Interface
61 * IShellFolder and especially it's methods ParseDisplayName and
62 * GetDisplayNameOf. Basically, you give a DOS-path (let's say C:\windows) to
63 * ParseDisplayName and get a SHITEMID similar to <Desktop|My Computer|C:|windows|>.
64 * Since it's opaque, you can't see the 'C', the 'windows' and the other stuff.
65 * You can only figure out that the ITEMIDLIST is composed of four SHITEMIDS.
66 * The file dialog applies IShellFolder's BindToObject method to bind to each of
67 * those four objects (Desktop, My Computer, C: and windows. All of them have to
68 * implement the IShellFolder interface.) and asks them how they would like to be
69 * displayed (basically their icon and the string displayed). If the file dialog
70 * asks <Desktop|My Computer|C:|windows> which sub-objects it contains (via
71 * EnumObjects) it gets a list of opaque SHITEMIDs, which can be concatenated to
72 * <Desktop|...|windows> to build a new ITEMIDLIST and browse, for instance,
73 * into <system32>. This means the file dialog browses the shell namespace by
74 * identifying objects via ITEMIDLISTs. Once the user has selected a location to
75 * save his valuable file, the file dialog calls IShellFolder's GetDisplayNameOf
76 * method to translate the ITEMIDLIST back to a DOS filename.
78 * It seems that one intention of the shell namespace concept was to make it
79 * possible to have objects in the namespace, which don't have any counterpart
80 * in the filesystem. The 'My Computer' shell folder object is one instance
81 * which comes to mind (Go try to save a file into 'My Computer' on windows.)
82 * So, to make matters a little more complex, before the file dialog asks a
83 * shell namespace object for it's DOS path, it asks if it actually has one.
84 * This is done via the IShellFolder::GetAttributesOf method, which sets the
85 * SFGAO_FILESYSTEM if - and only if - it has.
87 * The two things, described in the previous two paragraphs, are what unixfs is
88 * based on. So basically, if UnixDosFolder's ParseDisplayName method is called
89 * with a 'c:\windows' path-name, it doesn't return an
90 * <Desktop|My Computer|C:|windows|> ITEMIDLIST. Instead, it uses
91 * shell32's wine_get_unix_path_name and the _posix_ (which means not the win32)
92 * fileio api's to figure out that c: is mapped to - let's say -
93 * /home/mjung/.wine/drive_c and then constructs a
94 * <Desktop|/|home|mjung|.wine|drive_c> ITEMIDLIST. Which is what the file
95 * dialog uses to display the folder and file objects, which is why you see a
96 * unix path. When the user has found a nice place for his file and hits the
97 * save button, the ITEMIDLIST of the selected folder object is passed to
98 * GetDisplayNameOf, which returns a _DOS_ path name
99 * (like H:\home_of_my_new_file out of <|Desktop|/|home|mjung|home_of_my_new_file|>).
100 * Unixfs basically mounts your dos devices together in order to construct
101 * a copy of your unix filesystem structure.
103 * But what if none of the symbolic links in 'dosdevices' points to '/', you
104 * might ask ("And I don't want wine have access to my complete hard drive, you
105 * *%&1#!"). No problem, as I stated above, unixfs uses the _posix_ apis to
106 * construct the ITEMIDLISTs. Folders, which aren't accessible via a drive letter,
107 * don't have the SFGAO_FILESYSTEM flag set. So the file dialogs shouldn't allow
108 * the user to select such a folder for file storage (And if it does anyhow, it
109 * will not be able to return a valid path, since there is none). Think of those
110 * folders as a hierarchy of 'My Computer'-like folders, which happen to be a
111 * shadow of your unix filesystem tree. And since all of this stuff doesn't
112 * change anything at all in wine's fileio api's, windows applications will have
113 * no more access rights as they had before.
115 * To sum it all up, you can still safely run wine with you root account (Just
116 * kidding, don't do it.)
118 * If you are now standing in front of your computer, shouting hotly
119 * "I am not convinced, Mr. Rumsfeld^H^H^H^H^H^H^H^H^H^H^H^H", fire up regedit
120 * and delete HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\
121 * Explorer\Desktop\Namespace\{9D20AAE8-0625-44B0-9CA7-71889C2254D9} and you
122 * will be back in the pre-unixfs days.
125 #include "config.h"
126 #include "wine/port.h"
128 #include <stdio.h>
129 #include <stdarg.h>
130 #include <limits.h>
131 #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 ADJUST_THIS(c,m,p) ((c*)(((long)p)-(long)&(((c*)0)->lp##m##Vtbl)))
174 #define STATIC_CAST(i,p) ((i*)&p->lp##i##Vtbl)
176 #define LEN_SHITEMID_FIXED_PART ((USHORT) \
177 ( sizeof(USHORT) /* SHITEMID's cb field. */ \
178 + sizeof(PIDLTYPE) /* PIDLDATA's type field. */ \
179 + sizeof(FileStruct) /* Well, the FileStruct. */ \
180 - sizeof(char) /* One char too much in FileStruct. */ \
181 + sizeof(FileStructW) /* You name it. */ \
182 - sizeof(WCHAR) /* One WCHAR too much in FileStructW. */ \
183 + sizeof(WORD) )) /* Offset of FileStructW field in PIDL. */
185 #define PATHMODE_UNIX 0
186 #define PATHMODE_DOS 1
188 static const WCHAR wFileSystemBindData[] = {
189 'F','i','l','e',' ','S','y','s','t','e','m',' ','B','i','n','d',' ','D','a','t','a',0};
191 typedef struct {
192 IShellFolder2 IShellFolder2_iface;
193 IPersistFolder3 IPersistFolder3_iface;
194 IPersistPropertyBag IPersistPropertyBag_iface;
195 IDropTarget IDropTarget_iface;
196 ISFHelper ISFHelper_iface;
198 LONG ref;
199 CHAR *m_pszPath; /* Target path of the shell folder (CP_UNIXCP) */
200 LPITEMIDLIST m_pidlLocation; /* Location in the shell namespace */
201 DWORD m_dwPathMode;
202 DWORD m_dwAttributes;
203 const CLSID *m_pCLSID;
204 DWORD m_dwDropEffectsMask;
205 } UnixFolder;
207 static inline UnixFolder *impl_from_IShellFolder2(IShellFolder2 *iface)
209 return CONTAINING_RECORD(iface, UnixFolder, IShellFolder2_iface);
212 static inline UnixFolder *impl_from_IPersistFolder3(IPersistFolder3 *iface)
214 return CONTAINING_RECORD(iface, UnixFolder, IPersistFolder3_iface);
217 static inline UnixFolder *impl_from_IPersistPropertyBag(IPersistPropertyBag *iface)
219 return CONTAINING_RECORD(iface, UnixFolder, IPersistPropertyBag_iface);
222 static inline UnixFolder *impl_from_ISFHelper(ISFHelper *iface)
224 return CONTAINING_RECORD(iface, UnixFolder, ISFHelper_iface);
227 static inline UnixFolder *impl_from_IDropTarget(IDropTarget *iface)
229 return CONTAINING_RECORD(iface, UnixFolder, IDropTarget_iface);
232 /* Will hold the registered clipboard format identifier for ITEMIDLISTS. */
233 static UINT cfShellIDList = 0;
235 /******************************************************************************
236 * UNIXFS_filename_from_shitemid [Internal]
238 * Get CP_UNIXCP encoded filename corresponding to the first item of a pidl
240 * PARAMS
241 * pidl [I] A simple SHITEMID
242 * pszPathElement [O] Filename in CP_UNIXCP encoding will be stored here
244 * RETURNS
245 * Success: Number of bytes necessary to store the CP_UNIXCP encoded filename
246 * _without_ the terminating NUL.
247 * Failure: 0
249 * NOTES
250 * Size of the buffer at pszPathElement has to be FILENAME_MAX. pszPathElement
251 * may be NULL, if you are only interested in the return value.
253 static int UNIXFS_filename_from_shitemid(LPCITEMIDLIST pidl, char* pszPathElement) {
254 FileStructW *pFileStructW = _ILGetFileStructW(pidl);
255 int cLen = 0;
257 if (pFileStructW) {
258 cLen = WideCharToMultiByte(CP_UNIXCP, 0, pFileStructW->wszName, -1, pszPathElement,
259 pszPathElement ? FILENAME_MAX : 0, 0, 0);
260 } else {
261 /* There might be pidls slipping in from shfldr_fs.c, which don't contain the
262 * FileStructW field. In this case, we have to convert from CP_ACP to CP_UNIXCP. */
263 char *pszText = _ILGetTextPointer(pidl);
264 WCHAR *pwszPathElement = NULL;
265 int cWideChars;
267 cWideChars = MultiByteToWideChar(CP_ACP, 0, pszText, -1, NULL, 0);
268 if (!cWideChars) goto cleanup;
270 pwszPathElement = SHAlloc(cWideChars * sizeof(WCHAR));
271 if (!pwszPathElement) goto cleanup;
273 cWideChars = MultiByteToWideChar(CP_ACP, 0, pszText, -1, pwszPathElement, cWideChars);
274 if (!cWideChars) goto cleanup;
276 cLen = WideCharToMultiByte(CP_UNIXCP, 0, pwszPathElement, -1, pszPathElement,
277 pszPathElement ? FILENAME_MAX : 0, 0, 0);
279 cleanup:
280 SHFree(pwszPathElement);
283 if (cLen) cLen--; /* Don't count terminating NUL! */
284 return cLen;
287 /******************************************************************************
288 * UNIXFS_shitemid_len_from_filename [Internal]
290 * Computes the necessary length of a pidl to hold a path element
292 * PARAMS
293 * szPathElement [I] The path element string in CP_UNIXCP encoding.
294 * ppszPathElement [O] Path element string in CP_ACP encoding.
295 * ppwszPathElement [O] Path element string as WCHAR string.
297 * RETURNS
298 * Success: Length in bytes of a SHITEMID representing szPathElement
299 * Failure: 0
301 * NOTES
302 * Provide NULL values if not interested in pp(w)szPathElement. Otherwise
303 * caller is responsible to free ppszPathElement and ppwszPathElement with
304 * SHFree.
306 static USHORT UNIXFS_shitemid_len_from_filename(
307 const char *szPathElement, char **ppszPathElement, WCHAR **ppwszPathElement)
309 USHORT cbPidlLen = 0;
310 WCHAR *pwszPathElement = NULL;
311 char *pszPathElement = NULL;
312 int cWideChars, cChars;
314 /* There and Back Again: A Hobbit's Holiday. CP_UNIXCP might be some ANSI
315 * codepage or it might be a real multi-byte encoding like utf-8. There is no
316 * other way to figure out the length of the corresponding WCHAR and CP_ACP
317 * strings without actually doing the full CP_UNIXCP -> WCHAR -> CP_ACP cycle. */
319 cWideChars = MultiByteToWideChar(CP_UNIXCP, 0, szPathElement, -1, NULL, 0);
320 if (!cWideChars) goto cleanup;
322 pwszPathElement = SHAlloc(cWideChars * sizeof(WCHAR));
323 if (!pwszPathElement) goto cleanup;
325 cWideChars = MultiByteToWideChar(CP_UNIXCP, 0, szPathElement, -1, pwszPathElement, cWideChars);
326 if (!cWideChars) goto cleanup;
328 cChars = WideCharToMultiByte(CP_ACP, 0, pwszPathElement, -1, NULL, 0, 0, 0);
329 if (!cChars) goto cleanup;
331 pszPathElement = SHAlloc(cChars);
332 if (!pszPathElement) goto cleanup;
334 cChars = WideCharToMultiByte(CP_ACP, 0, pwszPathElement, -1, pszPathElement, cChars, 0, 0);
335 if (!cChars) goto cleanup;
337 /* (cChars & 0x1) is for the potential alignment byte */
338 cbPidlLen = LEN_SHITEMID_FIXED_PART + cChars + (cChars & 0x1) + cWideChars * sizeof(WCHAR);
340 cleanup:
341 if (cbPidlLen && ppszPathElement)
342 *ppszPathElement = pszPathElement;
343 else
344 SHFree(pszPathElement);
346 if (cbPidlLen && ppwszPathElement)
347 *ppwszPathElement = pwszPathElement;
348 else
349 SHFree(pwszPathElement);
351 return cbPidlLen;
354 /******************************************************************************
355 * UNIXFS_is_pidl_of_type [Internal]
357 * Checks for the first SHITEMID of an ITEMIDLIST if it passes a filter.
359 * PARAMS
360 * pIDL [I] The ITEMIDLIST to be checked.
361 * fFilter [I] Shell condition flags, which specify the filter.
363 * RETURNS
364 * TRUE, if pIDL is accepted by fFilter
365 * FALSE, otherwise
367 static inline BOOL UNIXFS_is_pidl_of_type(LPCITEMIDLIST pIDL, SHCONTF fFilter) {
368 const PIDLDATA *pIDLData = _ILGetDataPointer(pIDL);
369 if (!(fFilter & SHCONTF_INCLUDEHIDDEN) && pIDLData &&
370 (pIDLData->u.file.uFileAttribs & FILE_ATTRIBUTE_HIDDEN))
372 return FALSE;
374 if (_ILIsFolder(pIDL) && (fFilter & SHCONTF_FOLDERS)) return TRUE;
375 if (_ILIsValue(pIDL) && (fFilter & SHCONTF_NONFOLDERS)) return TRUE;
376 return FALSE;
379 /******************************************************************************
380 * UNIXFS_get_unix_path [Internal]
382 * Convert an absolute dos path to an absolute unix path.
383 * Evaluate "/.", "/.." and the symbolic links in $WINEPREFIX/dosdevices.
385 * PARAMS
386 * pszDosPath [I] An absolute dos path
387 * pszCanonicalPath [O] Buffer of length FILENAME_MAX. Will receive the canonical path.
389 * RETURNS
390 * Success, TRUE
391 * Failure, FALSE - Path not existent, too long, insufficient rights, to many symlinks
393 static BOOL UNIXFS_get_unix_path(LPCWSTR pszDosPath, char *pszCanonicalPath)
395 char *pPathTail, *pElement, *pCanonicalTail, szPath[FILENAME_MAX], *pszUnixPath, mb_path[FILENAME_MAX];
396 BOOL has_failed = FALSE;
397 WCHAR wszDrive[] = { '?', ':', '\\', 0 }, dospath[MAX_PATH], *dospath_end;
398 int cDriveSymlinkLen;
399 void *redir;
401 TRACE("(pszDosPath=%s, pszCanonicalPath=%p)\n", debugstr_w(pszDosPath), pszCanonicalPath);
403 if (!pszDosPath || pszDosPath[1] != ':')
404 return FALSE;
406 /* Get the canonicalized unix path corresponding to the drive letter. */
407 wszDrive[0] = pszDosPath[0];
408 pszUnixPath = wine_get_unix_file_name(wszDrive);
409 if (!pszUnixPath) return FALSE;
410 cDriveSymlinkLen = strlen(pszUnixPath);
411 pElement = realpath(pszUnixPath, szPath);
412 HeapFree(GetProcessHeap(), 0, pszUnixPath);
413 if (!pElement) return FALSE;
414 if (szPath[strlen(szPath)-1] != '/') strcat(szPath, "/");
416 /* Append the part relative to the drive symbolic link target. */
417 lstrcpyW(dospath, pszDosPath);
418 dospath_end = dospath + lstrlenW(dospath);
419 /* search for the most valid UNIX path possible, then append missing
420 * path parts */
421 Wow64DisableWow64FsRedirection(&redir);
422 while(!(pszUnixPath = wine_get_unix_file_name(dospath))){
423 if(has_failed){
424 *dospath_end = '/';
425 --dospath_end;
426 }else
427 has_failed = TRUE;
428 while(*dospath_end != '\\' && *dospath_end != '/'){
429 --dospath_end;
430 if(dospath_end < dospath)
431 break;
433 *dospath_end = '\0';
435 Wow64RevertWow64FsRedirection(redir);
436 if(dospath_end < dospath)
437 return FALSE;
438 strcat(szPath, pszUnixPath + cDriveSymlinkLen);
439 HeapFree(GetProcessHeap(), 0, pszUnixPath);
441 if(has_failed && WideCharToMultiByte(CP_UNIXCP, 0, dospath_end + 1, -1,
442 mb_path, FILENAME_MAX, NULL, NULL) > 0){
443 strcat(szPath, "/");
444 strcat(szPath, mb_path);
447 /* pCanonicalTail always points to the end of the canonical path constructed
448 * thus far. pPathTail points to the still to be processed part of the input
449 * path. pElement points to the path element currently investigated.
451 *pszCanonicalPath = '\0';
452 pCanonicalTail = pszCanonicalPath;
453 pPathTail = szPath;
455 do {
456 char cTemp;
458 pElement = pPathTail;
459 pPathTail = strchr(pPathTail+1, '/');
460 if (!pPathTail) /* Last path element may not be terminated by '/'. */
461 pPathTail = pElement + strlen(pElement);
462 /* Temporarily terminate the current path element. Will be restored later. */
463 cTemp = *pPathTail;
464 *pPathTail = '\0';
466 /* Skip "/." path elements */
467 if (!strcmp("/.", pElement)) {
468 *pPathTail = cTemp;
469 } else if (!strcmp("/..", pElement)) {
470 /* Remove last element in canonical path for "/.." elements, then skip. */
471 char *pTemp = strrchr(pszCanonicalPath, '/');
472 if (pTemp)
473 pCanonicalTail = pTemp;
474 *pCanonicalTail = '\0';
475 *pPathTail = cTemp;
476 } else {
477 /* Directory or file. Copy to canonical path */
478 if (pCanonicalTail - pszCanonicalPath + pPathTail - pElement + 1 > FILENAME_MAX)
479 return FALSE;
481 memcpy(pCanonicalTail, pElement, pPathTail - pElement + 1);
482 pCanonicalTail += pPathTail - pElement;
483 *pPathTail = cTemp;
485 } while (pPathTail[0] == '/');
487 TRACE("--> %s\n", debugstr_a(pszCanonicalPath));
489 return TRUE;
492 /******************************************************************************
493 * UNIXFS_build_shitemid [Internal]
495 * Constructs a new SHITEMID for the last component of path 'pszUnixPath' into
496 * buffer 'pIDL'.
498 * PARAMS
499 * pszUnixPath [I] An absolute path. The SHITEMID will be built for the last component.
500 * pbc [I] Bind context for this action, used to determine if the file must exist
501 * pIDL [O] SHITEMID will be constructed here.
503 * RETURNS
504 * Success: A pointer to the terminating '\0' character of path.
505 * Failure: NULL
507 * NOTES
508 * Minimum size of pIDL is SHITEMID_LEN_FROM_NAME_LEN(strlen(last_component_of_path)).
509 * If what you need is a PIDLLIST with a single SHITEMID, don't forget to append
510 * a 0 USHORT value.
512 static char* UNIXFS_build_shitemid(char *pszUnixPath, BOOL bMustExist, WIN32_FIND_DATAW *pFindData, void *pIDL) {
513 LPPIDLDATA pIDLData;
514 struct stat fileStat;
515 WIN32_FIND_DATAW findData;
516 char *pszComponentU, *pszComponentA;
517 WCHAR *pwszComponentW;
518 int cComponentULen, cComponentALen;
519 USHORT cbLen;
520 FileStructW *pFileStructW;
521 WORD uOffsetW, *pOffsetW;
523 TRACE("(pszUnixPath=%s, bMustExist=%s, pFindData=%p, pIDL=%p)\n",
524 debugstr_a(pszUnixPath), bMustExist ? "T" : "F", pFindData, pIDL);
526 if (pFindData)
527 memcpy(&findData, pFindData, sizeof(WIN32_FIND_DATAW));
528 else {
529 memset(&findData, 0, sizeof(WIN32_FIND_DATAW));
530 findData.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
533 /* We are only interested in regular files and directories. */
534 if (stat(pszUnixPath, &fileStat)){
535 if (bMustExist || errno != ENOENT)
536 return NULL;
537 } else {
538 LARGE_INTEGER time;
540 if (S_ISDIR(fileStat.st_mode))
541 findData.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
542 else if (S_ISREG(fileStat.st_mode))
543 findData.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
544 else
545 return NULL;
547 findData.nFileSizeLow = (DWORD)fileStat.st_size;
548 findData.nFileSizeHigh = fileStat.st_size >> 32;
550 RtlSecondsSince1970ToTime(fileStat.st_mtime, &time);
551 findData.ftLastWriteTime.dwLowDateTime = time.u.LowPart;
552 findData.ftLastWriteTime.dwHighDateTime = time.u.HighPart;
553 RtlSecondsSince1970ToTime(fileStat.st_atime, &time);
554 findData.ftLastAccessTime.dwLowDateTime = time.u.LowPart;
555 findData.ftLastAccessTime.dwHighDateTime = time.u.HighPart;
558 /* Compute the SHITEMID's length and wipe it. */
559 pszComponentU = strrchr(pszUnixPath, '/') + 1;
560 cComponentULen = strlen(pszComponentU);
561 cbLen = UNIXFS_shitemid_len_from_filename(pszComponentU, &pszComponentA, &pwszComponentW);
562 if (!cbLen) return NULL;
563 memset(pIDL, 0, cbLen);
564 ((LPSHITEMID)pIDL)->cb = cbLen;
566 /* Set shell32's standard SHITEMID data fields. */
567 pIDLData = _ILGetDataPointer(pIDL);
568 pIDLData->type = (findData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) ? PT_FOLDER : PT_VALUE;
569 pIDLData->u.file.dwFileSize = findData.nFileSizeLow;
570 FileTimeToDosDateTime(&findData.ftLastWriteTime, &pIDLData->u.file.uFileDate,
571 &pIDLData->u.file.uFileTime);
572 pIDLData->u.file.uFileAttribs = 0;
573 pIDLData->u.file.uFileAttribs |= findData.dwFileAttributes;
574 if (pszComponentU[0] == '.') pIDLData->u.file.uFileAttribs |= FILE_ATTRIBUTE_HIDDEN;
575 cComponentALen = lstrlenA(pszComponentA) + 1;
576 memcpy(pIDLData->u.file.szNames, pszComponentA, cComponentALen);
578 pFileStructW = (FileStructW*)(pIDLData->u.file.szNames + cComponentALen + (cComponentALen & 0x1));
579 uOffsetW = (WORD)(((LPBYTE)pFileStructW) - ((LPBYTE)pIDL));
580 pFileStructW->cbLen = cbLen - uOffsetW;
581 FileTimeToDosDateTime(&findData.ftLastWriteTime, &pFileStructW->uCreationDate,
582 &pFileStructW->uCreationTime);
583 FileTimeToDosDateTime(&findData.ftLastAccessTime, &pFileStructW->uLastAccessDate,
584 &pFileStructW->uLastAccessTime);
585 lstrcpyW(pFileStructW->wszName, pwszComponentW);
587 pOffsetW = (WORD*)(((LPBYTE)pIDL) + cbLen - sizeof(WORD));
588 *pOffsetW = uOffsetW;
590 SHFree(pszComponentA);
591 SHFree(pwszComponentW);
593 return pszComponentU + cComponentULen;
596 /******************************************************************************
597 * UNIXFS_path_to_pidl [Internal]
599 * PARAMS
600 * pUnixFolder [I] If path is relative, pUnixFolder represents the base path
601 * path [I] An absolute unix or dos path or a path relative to pUnixFolder
602 * ppidl [O] The corresponding ITEMIDLIST. Release with SHFree/ILFree
604 * RETURNS
605 * Success: S_OK
606 * Failure: Error code, invalid params or out of memory
608 * NOTES
609 * pUnixFolder also carries the information if the path is expected to be unix or dos.
611 static HRESULT UNIXFS_path_to_pidl(UnixFolder *pUnixFolder, LPBC pbc, const WCHAR *path,
612 LPITEMIDLIST *ppidl) {
613 LPITEMIDLIST pidl;
614 int cPidlLen, cPathLen;
615 char *pSlash, *pNextSlash, szCompletePath[FILENAME_MAX], *pNextPathElement, *pszAPath;
616 WCHAR *pwszPath;
617 WIN32_FIND_DATAW find_data;
618 BOOL must_exist = TRUE;
620 TRACE("pUnixFolder=%p, pbc=%p, path=%s, ppidl=%p\n", pUnixFolder, pbc, debugstr_w(path), ppidl);
622 if (!ppidl || !path)
623 return E_INVALIDARG;
625 /* Build an absolute path and let pNextPathElement point to the interesting
626 * relative sub-path. We need the absolute path to call 'stat', but the pidl
627 * will only contain the relative part.
629 if ((pUnixFolder->m_dwPathMode == PATHMODE_DOS) && (path[1] == ':'))
631 /* Absolute dos path. Convert to unix */
632 if (!UNIXFS_get_unix_path(path, szCompletePath))
633 return E_FAIL;
634 pNextPathElement = szCompletePath;
636 else if ((pUnixFolder->m_dwPathMode == PATHMODE_UNIX) && (path[0] == '/'))
638 /* Absolute unix path. Just convert to ANSI. */
639 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath, FILENAME_MAX, NULL, NULL);
640 pNextPathElement = szCompletePath;
642 else
644 /* Relative dos or unix path. Concat with this folder's path */
645 int cBasePathLen = strlen(pUnixFolder->m_pszPath);
646 memcpy(szCompletePath, pUnixFolder->m_pszPath, cBasePathLen);
647 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath + cBasePathLen,
648 FILENAME_MAX - cBasePathLen, NULL, NULL);
649 pNextPathElement = szCompletePath + cBasePathLen - 1;
651 /* If in dos mode, replace '\' with '/' */
652 if (pUnixFolder->m_dwPathMode == PATHMODE_DOS) {
653 char *pBackslash = strchr(pNextPathElement, '\\');
654 while (pBackslash) {
655 *pBackslash = '/';
656 pBackslash = strchr(pBackslash, '\\');
661 /* Special case for the root folder. */
662 if (!strcmp(szCompletePath, "/")) {
663 *ppidl = pidl = SHAlloc(sizeof(USHORT));
664 if (!pidl) return E_FAIL;
665 pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
666 return S_OK;
669 /* Remove trailing slash, if present */
670 cPathLen = strlen(szCompletePath);
671 if (szCompletePath[cPathLen-1] == '/')
672 szCompletePath[cPathLen-1] = '\0';
674 if ((szCompletePath[0] != '/') || (pNextPathElement[0] != '/')) {
675 ERR("szCompletePath: %s, pNextPathElment: %s\n", szCompletePath, pNextPathElement);
676 return E_FAIL;
679 /* At this point, we have an absolute unix path in szCompletePath
680 * and the relative portion of it in pNextPathElement. Both starting with '/'
681 * and _not_ terminated by a '/'. */
682 TRACE("complete path: %s, relative path: %s\n", szCompletePath, pNextPathElement);
684 /* Convert to CP_ACP and WCHAR */
685 if (!UNIXFS_shitemid_len_from_filename(pNextPathElement, &pszAPath, &pwszPath))
686 return E_FAIL;
688 /* Compute the length of the complete ITEMIDLIST */
689 cPidlLen = 0;
690 pSlash = pszAPath;
691 while (pSlash) {
692 pNextSlash = strchr(pSlash+1, '/');
693 cPidlLen += LEN_SHITEMID_FIXED_PART + /* Fixed part length plus potential alignment byte. */
694 (pNextSlash ? (pNextSlash - pSlash) & 0x1 : lstrlenA(pSlash) & 0x1);
695 pSlash = pNextSlash;
698 /* The USHORT is for the ITEMIDLIST terminator. The NUL terminators for the sub-path-strings
699 * are accounted for by the '/' separators, which are not stored in the SHITEMIDs. Above we
700 * have ensured that the number of '/'s exactly matches the number of sub-path-strings. */
701 cPidlLen += lstrlenA(pszAPath) + lstrlenW(pwszPath) * sizeof(WCHAR) + sizeof(USHORT);
703 SHFree(pszAPath);
704 SHFree(pwszPath);
706 *ppidl = pidl = SHAlloc(cPidlLen);
707 if (!pidl) return E_FAIL;
709 if (pbc) {
710 IUnknown *unk;
711 IFileSystemBindData *fsb;
712 HRESULT hr;
714 hr = IBindCtx_GetObjectParam(pbc, (LPOLESTR)wFileSystemBindData, &unk);
715 if (SUCCEEDED(hr)) {
716 hr = IUnknown_QueryInterface(unk, &IID_IFileSystemBindData, (LPVOID*)&fsb);
717 if (SUCCEEDED(hr)) {
718 hr = IFileSystemBindData_GetFindData(fsb, &find_data);
719 if (FAILED(hr))
720 memset(&find_data, 0, sizeof(WIN32_FIND_DATAW));
722 must_exist = FALSE;
723 IFileSystemBindData_Release(fsb);
725 IUnknown_Release(unk);
729 /* Concatenate the SHITEMIDs of the sub-directories. */
730 while (*pNextPathElement) {
731 pSlash = strchr(pNextPathElement+1, '/');
732 if (pSlash) *pSlash = '\0';
733 pNextPathElement = UNIXFS_build_shitemid(szCompletePath, must_exist,
734 must_exist&&!pSlash ? &find_data : NULL, pidl);
735 if (pSlash) *pSlash = '/';
737 if (!pNextPathElement) {
738 SHFree(*ppidl);
739 *ppidl = NULL;
740 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
742 pidl = ILGetNext(pidl);
744 pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
746 if ((char *)pidl-(char *)*ppidl+sizeof(USHORT) != cPidlLen) /* We've corrupted the heap :( */
747 ERR("Computed length of pidl incorrect. Please report.\n");
749 return S_OK;
752 /******************************************************************************
753 * UNIXFS_initialize_target_folder [Internal]
755 * Initialize the m_pszPath member of an UnixFolder, given an absolute unix
756 * base path and a relative ITEMIDLIST. Leave the m_pidlLocation member, which
757 * specifies the location in the shell namespace alone.
759 * PARAMS
760 * This [IO] The UnixFolder, whose target path is to be initialized
761 * szBasePath [I] The absolute base path
762 * pidlSubFolder [I] Relative part of the path, given as an ITEMIDLIST
763 * dwAttributes [I] Attributes to add to the Folders m_dwAttributes member
764 * (Used to pass the SFGAO_FILESYSTEM flag down the path)
765 * RETURNS
766 * Success: S_OK,
767 * Failure: E_FAIL
769 static HRESULT UNIXFS_initialize_target_folder(UnixFolder *This, const char *szBasePath,
770 LPCITEMIDLIST pidlSubFolder, DWORD dwAttributes)
772 LPCITEMIDLIST current = pidlSubFolder;
773 DWORD dwPathLen = strlen(szBasePath)+1;
774 char *pNextDir;
775 WCHAR *dos_name;
777 /* Determine the path's length bytes */
778 while (!_ILIsEmpty(current)) {
779 dwPathLen += UNIXFS_filename_from_shitemid(current, NULL) + 1; /* For the '/' */
780 current = ILGetNext(current);
783 /* Build the path and compute the attributes*/
784 This->m_dwAttributes =
785 dwAttributes|SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME;
786 This->m_pszPath = pNextDir = SHAlloc(dwPathLen);
787 if (!This->m_pszPath) {
788 WARN("SHAlloc failed!\n");
789 return E_FAIL;
791 current = pidlSubFolder;
792 strcpy(pNextDir, szBasePath);
793 pNextDir += strlen(szBasePath);
794 if (This->m_dwPathMode == PATHMODE_UNIX || IsEqualCLSID(&CLSID_MyDocuments, This->m_pCLSID))
795 This->m_dwAttributes |= SFGAO_FILESYSTEM;
796 while (!_ILIsEmpty(current)) {
797 pNextDir += UNIXFS_filename_from_shitemid(current, pNextDir);
798 *pNextDir++ = '/';
799 current = ILGetNext(current);
801 *pNextDir='\0';
803 if (!(This->m_dwAttributes & SFGAO_FILESYSTEM) &&
804 ((dos_name = wine_get_dos_file_name(This->m_pszPath))))
806 This->m_dwAttributes |= SFGAO_FILESYSTEM;
807 HeapFree( GetProcessHeap(), 0, dos_name );
810 return S_OK;
813 /******************************************************************************
814 * UNIXFS_copy [Internal]
816 * Copy pwszDosSrc to pwszDosDst.
818 * PARAMS
819 * pwszDosSrc [I] absolute path of the source
820 * pwszDosDst [I] absolute path of the destination
822 * RETURNS
823 * Success: S_OK,
824 * Failure: E_FAIL
826 static HRESULT UNIXFS_copy(LPCWSTR pwszDosSrc, LPCWSTR pwszDosDst)
828 SHFILEOPSTRUCTW op;
829 LPWSTR pwszSrc, pwszDst;
830 HRESULT res = E_OUTOFMEMORY;
831 UINT iSrcLen, iDstLen;
833 if (!pwszDosSrc || !pwszDosDst)
834 return E_FAIL;
836 iSrcLen = lstrlenW(pwszDosSrc);
837 iDstLen = lstrlenW(pwszDosDst);
838 pwszSrc = HeapAlloc(GetProcessHeap(), 0, (iSrcLen + 2) * sizeof(WCHAR));
839 pwszDst = HeapAlloc(GetProcessHeap(), 0, (iDstLen + 2) * sizeof(WCHAR));
841 if (pwszSrc && pwszDst) {
842 lstrcpyW(pwszSrc, pwszDosSrc);
843 lstrcpyW(pwszDst, pwszDosDst);
844 /* double null termination */
845 pwszSrc[iSrcLen + 1] = 0;
846 pwszDst[iDstLen + 1] = 0;
848 ZeroMemory(&op, sizeof(op));
849 op.hwnd = GetActiveWindow();
850 op.wFunc = FO_COPY;
851 op.pFrom = pwszSrc;
852 op.pTo = pwszDst;
853 op.fFlags = FOF_ALLOWUNDO;
854 if (!SHFileOperationW(&op))
856 WARN("SHFileOperationW failed\n");
857 res = E_FAIL;
859 else
860 res = S_OK;
863 HeapFree(GetProcessHeap(), 0, pwszSrc);
864 HeapFree(GetProcessHeap(), 0, pwszDst);
865 return res;
868 /******************************************************************************
869 * UnixFolder
871 * Class whose heap based instances represent unix filesystem directories.
874 static void UnixFolder_Destroy(UnixFolder *pUnixFolder) {
875 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
877 SHFree(pUnixFolder->m_pszPath);
878 ILFree(pUnixFolder->m_pidlLocation);
879 SHFree(pUnixFolder);
882 static HRESULT WINAPI ShellFolder2_QueryInterface(IShellFolder2 *iface, REFIID riid,
883 void **ppv)
885 UnixFolder *This = impl_from_IShellFolder2(iface);
887 TRACE("(%p)->(%s %p)\n", This, shdebugstr_guid(riid), ppv);
889 if (!ppv) return E_INVALIDARG;
891 if (IsEqualIID(&IID_IUnknown, riid) ||
892 IsEqualIID(&IID_IShellFolder, riid) ||
893 IsEqualIID(&IID_IShellFolder2, riid))
895 *ppv = &This->IShellFolder2_iface;
896 } else if (IsEqualIID(&IID_IPersistFolder3, riid) ||
897 IsEqualIID(&IID_IPersistFolder2, riid) ||
898 IsEqualIID(&IID_IPersistFolder, riid) ||
899 IsEqualIID(&IID_IPersist, riid))
901 *ppv = &This->IPersistFolder3_iface;
902 } else if (IsEqualIID(&IID_IPersistPropertyBag, riid)) {
903 *ppv = &This->IPersistPropertyBag_iface;
904 } else if (IsEqualIID(&IID_ISFHelper, riid)) {
905 *ppv = &This->ISFHelper_iface;
906 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
907 *ppv = &This->IDropTarget_iface;
908 if (!cfShellIDList)
909 cfShellIDList = RegisterClipboardFormatW(CFSTR_SHELLIDLISTW);
910 } else {
911 *ppv = NULL;
912 TRACE("Unimplemented interface %s\n", shdebugstr_guid(riid));
913 return E_NOINTERFACE;
916 IUnknown_AddRef((IUnknown*)*ppv);
917 return S_OK;
920 static ULONG WINAPI ShellFolder2_AddRef(IShellFolder2 *iface)
922 UnixFolder *This = impl_from_IShellFolder2(iface);
923 ULONG ref = InterlockedIncrement(&This->ref);
924 TRACE("(%p)->(%u)\n", This, ref);
925 return ref;
928 static ULONG WINAPI ShellFolder2_Release(IShellFolder2 *iface)
930 UnixFolder *This = impl_from_IShellFolder2(iface);
931 ULONG ref = InterlockedDecrement(&This->ref);
933 TRACE("(%p)->(%u)\n", This, ref);
935 if (!ref)
936 UnixFolder_Destroy(This);
938 return ref;
941 static HRESULT WINAPI ShellFolder2_ParseDisplayName(IShellFolder2* iface, HWND hwndOwner,
942 LPBC pbc, LPOLESTR display_name, ULONG* pchEaten, LPITEMIDLIST* ppidl,
943 ULONG* attrs)
945 UnixFolder *This = impl_from_IShellFolder2(iface);
946 HRESULT result;
948 TRACE("(%p)->(%p %p %s %p %p %p)\n", This, hwndOwner, pbc, debugstr_w(display_name),
949 pchEaten, ppidl, attrs);
951 result = UNIXFS_path_to_pidl(This, pbc, display_name, ppidl);
952 if (SUCCEEDED(result) && attrs && *attrs)
954 IShellFolder *parent;
955 LPCITEMIDLIST pidlLast;
956 LPITEMIDLIST pidlComplete = ILCombine(This->m_pidlLocation, *ppidl);
957 HRESULT hr;
959 hr = SHBindToParent(pidlComplete, &IID_IShellFolder, (void**)&parent, &pidlLast);
960 if (FAILED(hr)) {
961 FIXME("SHBindToParent failed! hr = 0x%08x\n", hr);
962 ILFree(pidlComplete);
963 return E_FAIL;
965 IShellFolder_GetAttributesOf(parent, 1, &pidlLast, attrs);
966 IShellFolder_Release(parent);
967 ILFree(pidlComplete);
970 if (FAILED(result)) TRACE("FAILED!\n");
971 return result;
974 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter);
976 static HRESULT WINAPI ShellFolder2_EnumObjects(IShellFolder2* iface, HWND hwndOwner,
977 SHCONTF grfFlags, IEnumIDList** ppEnumIDList)
979 UnixFolder *This = impl_from_IShellFolder2(iface);
980 IUnknown *newIterator;
981 HRESULT hr;
983 TRACE("(%p)->(%p 0x%08x %p)\n", This, hwndOwner, grfFlags, ppEnumIDList);
985 if (!This->m_pszPath) {
986 WARN("EnumObjects called on uninitialized UnixFolder-object!\n");
987 return E_UNEXPECTED;
990 newIterator = UnixSubFolderIterator_Constructor(This, grfFlags);
991 hr = IUnknown_QueryInterface(newIterator, &IID_IEnumIDList, (void**)ppEnumIDList);
992 IUnknown_Release(newIterator);
994 return hr;
997 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, const CLSID *pCLSID);
999 static HRESULT WINAPI ShellFolder2_BindToObject(IShellFolder2* iface, LPCITEMIDLIST pidl,
1000 LPBC pbcReserved, REFIID riid, void** ppvOut)
1002 UnixFolder *This = impl_from_IShellFolder2(iface);
1003 IPersistFolder3 *persistFolder;
1004 const CLSID *clsidChild;
1005 HRESULT hr;
1007 TRACE("(%p)->(%p %p %s %p)\n", This, pidl, pbcReserved, debugstr_guid(riid), ppvOut);
1009 if (_ILIsEmpty(pidl))
1010 return E_INVALIDARG;
1012 /* Don't bind to files */
1013 if (_ILIsValue(ILFindLastID(pidl)))
1014 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
1016 if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
1017 /* Children of FolderShortcuts are ShellFSFolders on Windows.
1018 * Unixfs' counterpart is UnixDosFolder. */
1019 clsidChild = &CLSID_UnixDosFolder;
1020 } else {
1021 clsidChild = This->m_pCLSID;
1024 hr = CreateUnixFolder(NULL, &IID_IPersistFolder3, (void**)&persistFolder, clsidChild);
1025 if (FAILED(hr)) return hr;
1026 hr = IPersistFolder3_QueryInterface(persistFolder, riid, ppvOut);
1028 if (SUCCEEDED(hr)) {
1029 UnixFolder *subfolder = impl_from_IPersistFolder3(persistFolder);
1030 subfolder->m_pidlLocation = ILCombine(This->m_pidlLocation, pidl);
1031 hr = UNIXFS_initialize_target_folder(subfolder, This->m_pszPath, pidl,
1032 This->m_dwAttributes & SFGAO_FILESYSTEM);
1035 IPersistFolder3_Release(persistFolder);
1037 return hr;
1040 static HRESULT WINAPI ShellFolder2_BindToStorage(IShellFolder2* iface, LPCITEMIDLIST pidl,
1041 LPBC pbcReserved, REFIID riid, void** ppvObj)
1043 UnixFolder *This = impl_from_IShellFolder2(iface);
1044 FIXME("(%p)->(%p %p %s %p): stub\n", This, pidl, pbcReserved, debugstr_guid(riid), ppvObj);
1045 return E_NOTIMPL;
1048 static HRESULT WINAPI ShellFolder2_CompareIDs(IShellFolder2* iface, LPARAM lParam,
1049 LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
1051 UnixFolder *This = impl_from_IShellFolder2(iface);
1052 BOOL isEmpty1, isEmpty2;
1053 HRESULT hr = E_FAIL;
1054 LPCITEMIDLIST firstpidl;
1055 IShellFolder2 *psf;
1056 int compare;
1058 TRACE("(%p)->(%ld %p %p)\n", This, lParam, pidl1, pidl2);
1060 isEmpty1 = _ILIsEmpty(pidl1);
1061 isEmpty2 = _ILIsEmpty(pidl2);
1063 if (isEmpty1 && isEmpty2)
1064 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
1065 else if (isEmpty1)
1066 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1067 else if (isEmpty2)
1068 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1070 compare = CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
1071 _ILGetTextPointer(pidl1), -1,
1072 _ILGetTextPointer(pidl2), -1);
1074 if ((compare != CSTR_EQUAL) && _ILIsFolder(pidl1) && !_ILIsFolder(pidl2))
1075 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1076 if ((compare != CSTR_EQUAL) && !_ILIsFolder(pidl1) && _ILIsFolder(pidl2))
1077 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1079 if ((compare == CSTR_LESS_THAN) || (compare == CSTR_GREATER_THAN))
1080 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)((compare == CSTR_LESS_THAN)?-1:1));
1082 if (pidl1->mkid.cb < pidl2->mkid.cb)
1083 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1084 else if (pidl1->mkid.cb > pidl2->mkid.cb)
1085 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1087 firstpidl = pidl1;
1088 pidl1 = ILGetNext(pidl1);
1089 pidl2 = ILGetNext(pidl2);
1091 isEmpty1 = _ILIsEmpty(pidl1);
1092 isEmpty2 = _ILIsEmpty(pidl2);
1094 if (isEmpty1 && isEmpty2)
1095 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
1096 else if (isEmpty1)
1097 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1098 else if (isEmpty2)
1099 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1100 else if (SUCCEEDED(IShellFolder2_BindToObject(iface, firstpidl, NULL, &IID_IShellFolder, (void**)&psf))) {
1101 hr = IShellFolder2_CompareIDs(psf, lParam, pidl1, pidl2);
1102 IShellFolder2_Release(psf);
1105 return hr;
1108 static HRESULT WINAPI ShellFolder2_CreateViewObject(IShellFolder2* iface, HWND hwndOwner,
1109 REFIID riid, void** ppv)
1111 UnixFolder *This = impl_from_IShellFolder2(iface);
1112 HRESULT hr = E_INVALIDARG;
1114 TRACE("(%p)->(%p %s %p)\n", This, hwndOwner, debugstr_guid(riid), ppv);
1116 if (!ppv) return E_INVALIDARG;
1117 *ppv = NULL;
1119 if (IsEqualIID(&IID_IShellView, riid)) {
1120 IShellView *view;
1122 view = IShellView_Constructor((IShellFolder*)iface);
1123 if (view) {
1124 hr = IShellView_QueryInterface(view, riid, ppv);
1125 IShellView_Release(view);
1127 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
1128 hr = IShellFolder2_QueryInterface(iface, &IID_IDropTarget, ppv);
1131 return hr;
1134 static HRESULT WINAPI ShellFolder2_GetAttributesOf(IShellFolder2* iface, UINT cidl,
1135 LPCITEMIDLIST* apidl, SFGAOF* rgfInOut)
1137 UnixFolder *This = impl_from_IShellFolder2(iface);
1138 HRESULT hr = S_OK;
1140 TRACE("(%p)->(%u %p %p)\n", This, cidl, apidl, rgfInOut);
1142 if (!rgfInOut || (cidl && !apidl))
1143 return E_INVALIDARG;
1145 if (cidl == 0) {
1146 *rgfInOut &= This->m_dwAttributes;
1147 } else {
1148 char szAbsolutePath[FILENAME_MAX], *pszRelativePath;
1149 UINT i;
1151 *rgfInOut = SFGAO_CANCOPY|SFGAO_CANMOVE|SFGAO_CANLINK|SFGAO_CANRENAME|SFGAO_CANDELETE|
1152 SFGAO_HASPROPSHEET|SFGAO_DROPTARGET|SFGAO_FILESYSTEM;
1153 lstrcpyA(szAbsolutePath, This->m_pszPath);
1154 pszRelativePath = szAbsolutePath + lstrlenA(szAbsolutePath);
1155 for (i=0; i<cidl; i++) {
1156 if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
1157 WCHAR *dos_name;
1158 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelativePath))
1159 return E_INVALIDARG;
1160 if (!(dos_name = wine_get_dos_file_name( szAbsolutePath )))
1161 *rgfInOut &= ~SFGAO_FILESYSTEM;
1162 else
1163 HeapFree( GetProcessHeap(), 0, dos_name );
1165 if (_ILIsFolder(apidl[i]))
1166 *rgfInOut |= SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR;
1170 return hr;
1173 static HRESULT WINAPI ShellFolder2_GetUIObjectOf(IShellFolder2* iface, HWND hwndOwner,
1174 UINT cidl, LPCITEMIDLIST* apidl, REFIID riid, UINT* prgfInOut, void** ppvOut)
1176 UnixFolder *This = impl_from_IShellFolder2(iface);
1177 HRESULT hr;
1178 UINT i;
1180 TRACE("(%p)->(%p %d %p riid=%s %p %p)\n",
1181 This, hwndOwner, cidl, apidl, debugstr_guid(riid), prgfInOut, ppvOut);
1183 if (!cidl || !apidl || !riid || !ppvOut)
1184 return E_INVALIDARG;
1186 for (i=0; i<cidl; i++)
1187 if (!apidl[i])
1188 return E_INVALIDARG;
1190 if(cidl == 1) {
1191 hr = SHELL32_CreateExtensionUIObject(iface, *apidl, riid, ppvOut);
1192 if(hr != S_FALSE)
1193 return hr;
1196 if (IsEqualIID(&IID_IContextMenu, riid)) {
1197 return ItemMenu_Constructor((IShellFolder*)iface, This->m_pidlLocation, apidl, cidl, riid, ppvOut);
1198 } else if (IsEqualIID(&IID_IDataObject, riid)) {
1199 *ppvOut = IDataObject_Constructor(hwndOwner, This->m_pidlLocation, apidl, cidl);
1200 return S_OK;
1201 } else if (IsEqualIID(&IID_IExtractIconA, riid)) {
1202 LPITEMIDLIST pidl;
1203 if (cidl != 1) return E_INVALIDARG;
1204 pidl = ILCombine(This->m_pidlLocation, apidl[0]);
1205 *ppvOut = IExtractIconA_Constructor(pidl);
1206 SHFree(pidl);
1207 return S_OK;
1208 } else if (IsEqualIID(&IID_IExtractIconW, riid)) {
1209 LPITEMIDLIST pidl;
1210 if (cidl != 1) return E_INVALIDARG;
1211 pidl = ILCombine(This->m_pidlLocation, apidl[0]);
1212 *ppvOut = IExtractIconW_Constructor(pidl);
1213 SHFree(pidl);
1214 return S_OK;
1215 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
1216 if (cidl != 1) return E_INVALIDARG;
1217 return IShellFolder2_BindToObject(iface, apidl[0], NULL, &IID_IDropTarget, ppvOut);
1218 } else if (IsEqualIID(&IID_IShellLinkW, riid)) {
1219 FIXME("IShellLinkW\n");
1220 return E_FAIL;
1221 } else if (IsEqualIID(&IID_IShellLinkA, riid)) {
1222 FIXME("IShellLinkA\n");
1223 return E_FAIL;
1224 } else {
1225 FIXME("Unknown interface %s in GetUIObjectOf\n", debugstr_guid(riid));
1226 return E_NOINTERFACE;
1230 static HRESULT WINAPI ShellFolder2_GetDisplayNameOf(IShellFolder2* iface,
1231 LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET* lpName)
1233 UnixFolder *This = impl_from_IShellFolder2(iface);
1234 SHITEMID emptyIDL = { 0, { 0 } };
1235 HRESULT hr = S_OK;
1237 TRACE("(%p)->(%p 0x%x %p)\n", This, pidl, uFlags, lpName);
1239 if ((GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) &&
1240 (GET_SHGDN_RELATION(uFlags) != SHGDN_INFOLDER))
1242 if (_ILIsEmpty(pidl)) {
1243 lpName->uType = STRRET_WSTR;
1244 if (This->m_dwPathMode == PATHMODE_UNIX) {
1245 UINT len = MultiByteToWideChar(CP_UNIXCP, 0, This->m_pszPath, -1, NULL, 0);
1246 lpName->u.pOleStr = SHAlloc(len * sizeof(WCHAR));
1247 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1248 MultiByteToWideChar(CP_UNIXCP, 0, This->m_pszPath, -1, lpName->u.pOleStr, len);
1249 } else {
1250 LPWSTR pwszDosFileName = wine_get_dos_file_name(This->m_pszPath);
1251 if (!pwszDosFileName) return HRESULT_FROM_WIN32(GetLastError());
1252 lpName->u.pOleStr = SHAlloc((lstrlenW(pwszDosFileName) + 1) * sizeof(WCHAR));
1253 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1254 lstrcpyW(lpName->u.pOleStr, pwszDosFileName);
1255 PathRemoveBackslashW(lpName->u.pOleStr);
1256 HeapFree(GetProcessHeap(), 0, pwszDosFileName);
1258 } else if (_ILIsValue(pidl)) {
1259 STRRET str;
1260 PWSTR path, file;
1262 /* We are looking for the complete path to a file */
1264 /* Get the complete path for the current folder object */
1265 hr = IShellFolder2_GetDisplayNameOf(iface, (LPITEMIDLIST)&emptyIDL, uFlags, &str);
1266 if (SUCCEEDED(hr)) {
1267 hr = StrRetToStrW(&str, NULL, &path);
1268 if (SUCCEEDED(hr)) {
1270 /* Get the child filename */
1271 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_FORPARSING | SHGDN_INFOLDER, &str);
1272 if (SUCCEEDED(hr)) {
1273 hr = StrRetToStrW(&str, NULL, &file);
1274 if (SUCCEEDED(hr)) {
1275 static const WCHAR slashW = '/';
1276 UINT len_path = strlenW(path), len_file = strlenW(file);
1278 /* Now, combine them */
1279 lpName->uType = STRRET_WSTR;
1280 lpName->u.pOleStr = SHAlloc( (len_path + len_file + 2)*sizeof(WCHAR) );
1281 lstrcpyW(lpName->u.pOleStr, path);
1282 if (This->m_dwPathMode == PATHMODE_UNIX &&
1283 lpName->u.pOleStr[len_path-1] != slashW) {
1284 lpName->u.pOleStr[len_path] = slashW;
1285 lpName->u.pOleStr[len_path+1] = '\0';
1286 } else
1287 PathAddBackslashW(lpName->u.pOleStr);
1288 lstrcatW(lpName->u.pOleStr, file);
1290 CoTaskMemFree(file);
1291 } else
1292 WARN("Failed to convert strret (file)\n");
1294 CoTaskMemFree(path);
1295 } else
1296 WARN("Failed to convert strret (path)\n");
1298 } else {
1299 IShellFolder *pSubFolder;
1301 hr = IShellFolder2_BindToObject(iface, pidl, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1302 if (SUCCEEDED(hr)) {
1303 hr = IShellFolder_GetDisplayNameOf(pSubFolder, (LPITEMIDLIST)&emptyIDL, uFlags, lpName);
1304 IShellFolder_Release(pSubFolder);
1305 } else if (FAILED(hr) && !_ILIsPidlSimple(pidl)) {
1306 LPITEMIDLIST pidl_parent = ILClone(pidl);
1307 LPITEMIDLIST pidl_child = ILFindLastID(pidl);
1309 /* Might be a file, try binding to its parent */
1310 ILRemoveLastID(pidl_parent);
1311 hr = IShellFolder2_BindToObject(iface, pidl_parent, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1312 if (SUCCEEDED(hr)) {
1313 hr = IShellFolder_GetDisplayNameOf(pSubFolder, pidl_child, uFlags, lpName);
1314 IShellFolder_Release(pSubFolder);
1316 ILFree(pidl_parent);
1319 } else {
1320 WCHAR wszFileName[MAX_PATH];
1321 if (!_ILSimpleGetTextW(pidl, wszFileName, MAX_PATH)) return E_INVALIDARG;
1322 lpName->uType = STRRET_WSTR;
1323 lpName->u.pOleStr = SHAlloc((lstrlenW(wszFileName)+1)*sizeof(WCHAR));
1324 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1325 lstrcpyW(lpName->u.pOleStr, wszFileName);
1326 if (!(GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) && This->m_dwPathMode == PATHMODE_DOS &&
1327 !_ILIsFolder(pidl) && wszFileName[0] != '.' && SHELL_FS_HideExtension(wszFileName))
1329 PathRemoveExtensionW(lpName->u.pOleStr);
1333 TRACE("--> %s\n", debugstr_w(lpName->u.pOleStr));
1335 return hr;
1338 static HRESULT WINAPI ShellFolder2_SetNameOf(IShellFolder2* iface, HWND hwnd,
1339 LPCITEMIDLIST pidl, LPCOLESTR lpcwszName, SHGDNF uFlags, LPITEMIDLIST* ppidlOut)
1341 UnixFolder *This = impl_from_IShellFolder2(iface);
1343 static const WCHAR awcInvalidChars[] = { '\\', '/', ':', '*', '?', '"', '<', '>', '|' };
1344 char szSrc[FILENAME_MAX], szDest[FILENAME_MAX];
1345 WCHAR wszSrcRelative[MAX_PATH], *pwszExt = NULL;
1346 unsigned int i;
1347 int cBasePathLen = lstrlenA(This->m_pszPath), cNameLen;
1348 struct stat statDest;
1349 LPITEMIDLIST pidlSrc, pidlDest, pidlRelativeDest;
1350 LPOLESTR lpwszName;
1351 HRESULT hr;
1353 TRACE("(%p)->(%p %p %s 0x%08x %p)\n", This, hwnd, pidl, debugstr_w(lpcwszName), uFlags, ppidlOut);
1355 /* prepare to fail */
1356 if (ppidlOut)
1357 *ppidlOut = NULL;
1359 /* pidl has to contain a single non-empty SHITEMID */
1360 if (_ILIsDesktop(pidl) || !_ILIsPidlSimple(pidl) || !_ILGetTextPointer(pidl))
1361 return E_INVALIDARG;
1363 /* check for invalid characters in lpcwszName. */
1364 for (i=0; i < sizeof(awcInvalidChars)/sizeof(*awcInvalidChars); i++)
1365 if (StrChrW(lpcwszName, awcInvalidChars[i]))
1366 return HRESULT_FROM_WIN32(ERROR_CANCELLED);
1368 /* build source path */
1369 memcpy(szSrc, This->m_pszPath, cBasePathLen);
1370 UNIXFS_filename_from_shitemid(pidl, szSrc + cBasePathLen);
1372 /* build destination path */
1373 memcpy(szDest, This->m_pszPath, cBasePathLen);
1374 WideCharToMultiByte(CP_UNIXCP, 0, lpcwszName, -1, szDest+cBasePathLen,
1375 FILENAME_MAX-cBasePathLen, NULL, NULL);
1377 /* If the filename's extension is hidden to the user, we have to append it. */
1378 if (!(uFlags & SHGDN_FORPARSING) &&
1379 _ILSimpleGetTextW(pidl, wszSrcRelative, MAX_PATH) &&
1380 SHELL_FS_HideExtension(wszSrcRelative))
1382 int cLenDest = strlen(szDest);
1383 pwszExt = PathFindExtensionW(wszSrcRelative);
1384 WideCharToMultiByte(CP_UNIXCP, 0, pwszExt, -1, szDest + cLenDest,
1385 FILENAME_MAX - cLenDest, NULL, NULL);
1388 TRACE("src=%s dest=%s\n", szSrc, szDest);
1390 /* Fail, if destination does already exist */
1391 if (!stat(szDest, &statDest))
1392 return E_FAIL;
1394 /* Rename the file */
1395 if (rename(szSrc, szDest))
1396 return E_FAIL;
1398 /* Build a pidl for the path of the renamed file */
1399 cNameLen = lstrlenW(lpcwszName) + 1;
1400 if(pwszExt)
1401 cNameLen += lstrlenW(pwszExt);
1402 lpwszName = SHAlloc(cNameLen*sizeof(WCHAR)); /* due to const correctness. */
1403 lstrcpyW(lpwszName, lpcwszName);
1404 if(pwszExt)
1405 lstrcatW(lpwszName, pwszExt);
1407 hr = IShellFolder2_ParseDisplayName(iface, NULL, NULL, lpwszName, NULL, &pidlRelativeDest, NULL);
1408 SHFree(lpwszName);
1409 if (FAILED(hr)) {
1410 rename(szDest, szSrc); /* Undo the renaming */
1411 return E_FAIL;
1413 pidlDest = ILCombine(This->m_pidlLocation, pidlRelativeDest);
1414 ILFree(pidlRelativeDest);
1415 pidlSrc = ILCombine(This->m_pidlLocation, pidl);
1417 /* Inform the shell */
1418 if (_ILIsFolder(ILFindLastID(pidlDest)))
1419 SHChangeNotify(SHCNE_RENAMEFOLDER, SHCNF_IDLIST, pidlSrc, pidlDest);
1420 else
1421 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_IDLIST, pidlSrc, pidlDest);
1423 if (ppidlOut)
1424 *ppidlOut = ILClone(ILFindLastID(pidlDest));
1426 ILFree(pidlSrc);
1427 ILFree(pidlDest);
1429 return S_OK;
1432 static HRESULT WINAPI ShellFolder2_EnumSearches(IShellFolder2* iface, IEnumExtraSearch **ppEnum)
1434 UnixFolder *This = impl_from_IShellFolder2(iface);
1435 FIXME("(%p)->(%p): stub\n", This, ppEnum);
1436 return E_NOTIMPL;
1439 static HRESULT WINAPI ShellFolder2_GetDefaultColumn(IShellFolder2* iface,
1440 DWORD dwReserved, ULONG *pSort, ULONG *pDisplay)
1442 UnixFolder *This = impl_from_IShellFolder2(iface);
1444 TRACE("(%p)->(0x%x %p %p)\n", This, dwReserved, pSort, pDisplay);
1446 if (pSort)
1447 *pSort = 0;
1448 if (pDisplay)
1449 *pDisplay = 0;
1451 return S_OK;
1454 static HRESULT WINAPI ShellFolder2_GetDefaultColumnState(IShellFolder2* iface,
1455 UINT column, SHCOLSTATEF *flags)
1457 UnixFolder *This = impl_from_IShellFolder2(iface);
1458 FIXME("(%p)->(%u %p): stub\n", This, column, flags);
1459 return E_NOTIMPL;
1462 static HRESULT WINAPI ShellFolder2_GetDefaultSearchGUID(IShellFolder2* iface,
1463 GUID *guid)
1465 UnixFolder *This = impl_from_IShellFolder2(iface);
1466 FIXME("(%p)->(%p): stub\n", This, guid);
1467 return E_NOTIMPL;
1470 static HRESULT WINAPI ShellFolder2_GetDetailsEx(IShellFolder2* iface,
1471 LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
1473 UnixFolder *This = impl_from_IShellFolder2(iface);
1474 FIXME("(%p)->(%p %p %p): stub\n", This, pidl, pscid, pv);
1475 return E_NOTIMPL;
1478 #define SHELLVIEWCOLUMNS 7
1480 static HRESULT WINAPI ShellFolder2_GetDetailsOf(IShellFolder2* iface,
1481 LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
1483 UnixFolder *This = impl_from_IShellFolder2(iface);
1484 HRESULT hr = E_FAIL;
1485 struct passwd *pPasswd;
1486 struct group *pGroup;
1487 struct stat statItem;
1489 static const shvheader unixfs_header[SHELLVIEWCOLUMNS] = {
1490 {IDS_SHV_COLUMN1, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
1491 {IDS_SHV_COLUMN2, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1492 {IDS_SHV_COLUMN3, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1493 {IDS_SHV_COLUMN4, SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12},
1494 {IDS_SHV_COLUMN5, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 9},
1495 {IDS_SHV_COLUMN10, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
1496 {IDS_SHV_COLUMN11, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7}
1499 TRACE("(%p)->(%p %d %p)\n", This, pidl, iColumn, psd);
1501 if (!psd || iColumn >= SHELLVIEWCOLUMNS)
1502 return E_INVALIDARG;
1504 if (!pidl)
1505 return SHELL32_GetColumnDetails(unixfs_header, iColumn, psd);
1507 if (iColumn == 4 || iColumn == 5 || iColumn == 6) {
1508 char szPath[FILENAME_MAX];
1509 strcpy(szPath, This->m_pszPath);
1510 if (!UNIXFS_filename_from_shitemid(pidl, szPath + strlen(szPath)))
1511 return E_INVALIDARG;
1512 if (stat(szPath, &statItem))
1513 return E_INVALIDARG;
1516 psd->str.u.cStr[0] = '\0';
1517 psd->str.uType = STRRET_CSTR;
1519 switch (iColumn) {
1520 case 0:
1521 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL|SHGDN_INFOLDER, &psd->str);
1522 break;
1523 case 1:
1524 _ILGetFileSize(pidl, psd->str.u.cStr, MAX_PATH);
1525 break;
1526 case 2:
1527 _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
1528 break;
1529 case 3:
1530 _ILGetFileDate(pidl, psd->str.u.cStr, MAX_PATH);
1531 break;
1532 case 4:
1533 psd->str.u.cStr[0] = S_ISDIR(statItem.st_mode) ? 'd' : '-';
1534 psd->str.u.cStr[1] = (statItem.st_mode & S_IRUSR) ? 'r' : '-';
1535 psd->str.u.cStr[2] = (statItem.st_mode & S_IWUSR) ? 'w' : '-';
1536 psd->str.u.cStr[3] = (statItem.st_mode & S_IXUSR) ? 'x' : '-';
1537 psd->str.u.cStr[4] = (statItem.st_mode & S_IRGRP) ? 'r' : '-';
1538 psd->str.u.cStr[5] = (statItem.st_mode & S_IWGRP) ? 'w' : '-';
1539 psd->str.u.cStr[6] = (statItem.st_mode & S_IXGRP) ? 'x' : '-';
1540 psd->str.u.cStr[7] = (statItem.st_mode & S_IROTH) ? 'r' : '-';
1541 psd->str.u.cStr[8] = (statItem.st_mode & S_IWOTH) ? 'w' : '-';
1542 psd->str.u.cStr[9] = (statItem.st_mode & S_IXOTH) ? 'x' : '-';
1543 psd->str.u.cStr[10] = '\0';
1544 break;
1545 case 5:
1546 pPasswd = getpwuid(statItem.st_uid);
1547 if (pPasswd) strcpy(psd->str.u.cStr, pPasswd->pw_name);
1548 break;
1549 case 6:
1550 pGroup = getgrgid(statItem.st_gid);
1551 if (pGroup) strcpy(psd->str.u.cStr, pGroup->gr_name);
1552 break;
1555 return hr;
1558 static HRESULT WINAPI ShellFolder2_MapColumnToSCID(IShellFolder2* iface, UINT column,
1559 SHCOLUMNID *pscid)
1561 UnixFolder *This = impl_from_IShellFolder2(iface);
1562 FIXME("(%p)->(%u %p): stub\n", This, column, pscid);
1563 return E_NOTIMPL;
1566 static const IShellFolder2Vtbl ShellFolder2Vtbl = {
1567 ShellFolder2_QueryInterface,
1568 ShellFolder2_AddRef,
1569 ShellFolder2_Release,
1570 ShellFolder2_ParseDisplayName,
1571 ShellFolder2_EnumObjects,
1572 ShellFolder2_BindToObject,
1573 ShellFolder2_BindToStorage,
1574 ShellFolder2_CompareIDs,
1575 ShellFolder2_CreateViewObject,
1576 ShellFolder2_GetAttributesOf,
1577 ShellFolder2_GetUIObjectOf,
1578 ShellFolder2_GetDisplayNameOf,
1579 ShellFolder2_SetNameOf,
1580 ShellFolder2_GetDefaultSearchGUID,
1581 ShellFolder2_EnumSearches,
1582 ShellFolder2_GetDefaultColumn,
1583 ShellFolder2_GetDefaultColumnState,
1584 ShellFolder2_GetDetailsEx,
1585 ShellFolder2_GetDetailsOf,
1586 ShellFolder2_MapColumnToSCID
1589 static HRESULT WINAPI PersistFolder3_QueryInterface(IPersistFolder3* iface, REFIID riid,
1590 void** ppvObject)
1592 UnixFolder *This = impl_from_IPersistFolder3(iface);
1593 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppvObject);
1596 static ULONG WINAPI PersistFolder3_AddRef(IPersistFolder3* iface)
1598 UnixFolder *This = impl_from_IPersistFolder3(iface);
1599 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
1602 static ULONG WINAPI PersistFolder3_Release(IPersistFolder3* iface)
1604 UnixFolder *This = impl_from_IPersistFolder3(iface);
1605 return IShellFolder2_Release(&This->IShellFolder2_iface);
1608 static HRESULT WINAPI PersistFolder3_GetClassID(IPersistFolder3* iface, CLSID* pClassID)
1610 UnixFolder *This = impl_from_IPersistFolder3(iface);
1612 TRACE("(%p)->(%p)\n", This, pClassID);
1614 if (!pClassID)
1615 return E_INVALIDARG;
1617 *pClassID = *This->m_pCLSID;
1618 return S_OK;
1621 static HRESULT WINAPI PersistFolder3_Initialize(IPersistFolder3* iface, LPCITEMIDLIST pidl)
1623 UnixFolder *This = impl_from_IPersistFolder3(iface);
1624 LPCITEMIDLIST current = pidl;
1625 char szBasePath[FILENAME_MAX] = "/";
1627 TRACE("(%p)->(%p)\n", This, pidl);
1629 /* Find the UnixFolderClass root */
1630 while (current->mkid.cb) {
1631 if ((_ILIsDrive(current) && IsEqualCLSID(This->m_pCLSID, &CLSID_ShellFSFolder)) ||
1632 (_ILIsSpecialFolder(current) && IsEqualCLSID(This->m_pCLSID, _ILGetGUIDPointer(current))))
1634 break;
1636 current = ILGetNext(current);
1639 if (current->mkid.cb) {
1640 if (_ILIsDrive(current)) {
1641 WCHAR wszDrive[] = { '?', ':', '\\', 0 };
1642 wszDrive[0] = (WCHAR)*_ILGetTextPointer(current);
1643 if (!UNIXFS_get_unix_path(wszDrive, szBasePath))
1644 return E_FAIL;
1645 } else if (IsEqualIID(&CLSID_MyDocuments, _ILGetGUIDPointer(current))) {
1646 WCHAR wszMyDocumentsPath[MAX_PATH];
1647 if (!SHGetSpecialFolderPathW(0, wszMyDocumentsPath, CSIDL_PERSONAL, FALSE))
1648 return E_FAIL;
1649 PathAddBackslashW(wszMyDocumentsPath);
1650 if (!UNIXFS_get_unix_path(wszMyDocumentsPath, szBasePath))
1651 return E_FAIL;
1653 current = ILGetNext(current);
1654 } else if (_ILIsDesktop(pidl) || _ILIsValue(pidl) || _ILIsFolder(pidl)) {
1655 /* Path rooted at Desktop */
1656 WCHAR wszDesktopPath[MAX_PATH];
1657 if (!SHGetSpecialFolderPathW(0, wszDesktopPath, CSIDL_DESKTOPDIRECTORY, FALSE))
1658 return E_FAIL;
1659 PathAddBackslashW(wszDesktopPath);
1660 if (!UNIXFS_get_unix_path(wszDesktopPath, szBasePath))
1661 return E_FAIL;
1662 current = pidl;
1663 } else if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
1664 /* FolderShortcuts' Initialize method only sets the ITEMIDLIST, which
1665 * specifies the location in the shell namespace, but leaves the
1666 * target folder (m_pszPath) alone. See unit tests in tests/shlfolder.c */
1667 This->m_pidlLocation = ILClone(pidl);
1668 return S_OK;
1669 } else {
1670 ERR("Unknown pidl type!\n");
1671 pdump(pidl);
1672 return E_INVALIDARG;
1675 This->m_pidlLocation = ILClone(pidl);
1676 return UNIXFS_initialize_target_folder(This, szBasePath, current, 0);
1679 static HRESULT WINAPI PersistFolder3_GetCurFolder(IPersistFolder3* iface, LPITEMIDLIST* ppidl)
1681 UnixFolder *This = impl_from_IPersistFolder3(iface);
1683 TRACE ("(iface=%p, ppidl=%p)\n", iface, ppidl);
1685 if (!ppidl)
1686 return E_POINTER;
1687 *ppidl = ILClone (This->m_pidlLocation);
1688 return S_OK;
1691 static HRESULT WINAPI PersistFolder3_InitializeEx(IPersistFolder3 *iface, IBindCtx *pbc,
1692 LPCITEMIDLIST pidlRoot, const PERSIST_FOLDER_TARGET_INFO *ppfti)
1694 UnixFolder *This = impl_from_IPersistFolder3(iface);
1695 WCHAR wszTargetDosPath[MAX_PATH];
1696 char szTargetPath[FILENAME_MAX] = "";
1698 TRACE("(%p)->(%p %p %p)\n", This, pbc, pidlRoot, ppfti);
1700 /* If no PERSIST_FOLDER_TARGET_INFO is given InitializeEx is equivalent to Initialize. */
1701 if (!ppfti)
1702 return IPersistFolder3_Initialize(iface, pidlRoot);
1704 if (ppfti->csidl != -1) {
1705 if (FAILED(SHGetFolderPathW(0, ppfti->csidl, NULL, 0, wszTargetDosPath)) ||
1706 !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1708 return E_FAIL;
1710 } else if (*ppfti->szTargetParsingName) {
1711 lstrcpyW(wszTargetDosPath, ppfti->szTargetParsingName);
1712 PathAddBackslashW(wszTargetDosPath);
1713 if (!UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath)) {
1714 return E_FAIL;
1716 } else if (ppfti->pidlTargetFolder) {
1717 if (!SHGetPathFromIDListW(ppfti->pidlTargetFolder, wszTargetDosPath) ||
1718 !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1720 return E_FAIL;
1722 } else {
1723 return E_FAIL;
1726 This->m_pszPath = SHAlloc(lstrlenA(szTargetPath)+1);
1727 if (!This->m_pszPath)
1728 return E_FAIL;
1729 lstrcpyA(This->m_pszPath, szTargetPath);
1730 This->m_pidlLocation = ILClone(pidlRoot);
1731 This->m_dwAttributes = (ppfti->dwAttributes != -1) ? ppfti->dwAttributes :
1732 (SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME|SFGAO_FILESYSTEM);
1734 return S_OK;
1737 static HRESULT WINAPI PersistFolder3_GetFolderTargetInfo(IPersistFolder3 *iface,
1738 PERSIST_FOLDER_TARGET_INFO *ppfti)
1740 UnixFolder *This = impl_from_IPersistFolder3(iface);
1741 FIXME("(%p)->(%p): stub\n", This, ppfti);
1742 return E_NOTIMPL;
1745 static const IPersistFolder3Vtbl PersistFolder3Vtbl = {
1746 PersistFolder3_QueryInterface,
1747 PersistFolder3_AddRef,
1748 PersistFolder3_Release,
1749 PersistFolder3_GetClassID,
1750 PersistFolder3_Initialize,
1751 PersistFolder3_GetCurFolder,
1752 PersistFolder3_InitializeEx,
1753 PersistFolder3_GetFolderTargetInfo
1756 static HRESULT WINAPI PersistPropertyBag_QueryInterface(IPersistPropertyBag* iface,
1757 REFIID riid, void** ppv)
1759 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1760 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppv);
1763 static ULONG WINAPI PersistPropertyBag_AddRef(IPersistPropertyBag* iface)
1765 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1766 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
1769 static ULONG WINAPI PersistPropertyBag_Release(IPersistPropertyBag* iface)
1771 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1772 return IShellFolder2_Release(&This->IShellFolder2_iface);
1775 static HRESULT WINAPI PersistPropertyBag_GetClassID(IPersistPropertyBag* iface, CLSID* pClassID)
1777 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1778 return IPersistFolder3_GetClassID(&This->IPersistFolder3_iface, pClassID);
1781 static HRESULT WINAPI PersistPropertyBag_InitNew(IPersistPropertyBag* iface)
1783 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1784 FIXME("(%p): stub\n", This);
1785 return E_NOTIMPL;
1788 static HRESULT WINAPI PersistPropertyBag_Load(IPersistPropertyBag *iface,
1789 IPropertyBag *pPropertyBag, IErrorLog *pErrorLog)
1791 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1793 static const WCHAR wszTarget[] = { 'T','a','r','g','e','t', 0 }, wszNull[] = { 0 };
1794 PERSIST_FOLDER_TARGET_INFO pftiTarget;
1795 VARIANT var;
1796 HRESULT hr;
1798 TRACE("(%p)->(%p %p)\n", This, pPropertyBag, pErrorLog);
1800 if (!pPropertyBag)
1801 return E_POINTER;
1803 /* Get 'Target' property from the property bag. */
1804 V_VT(&var) = VT_BSTR;
1805 hr = IPropertyBag_Read(pPropertyBag, wszTarget, &var, NULL);
1806 if (FAILED(hr))
1807 return E_FAIL;
1808 lstrcpyW(pftiTarget.szTargetParsingName, V_BSTR(&var));
1809 SysFreeString(V_BSTR(&var));
1811 pftiTarget.pidlTargetFolder = NULL;
1812 lstrcpyW(pftiTarget.szNetworkProvider, wszNull);
1813 pftiTarget.dwAttributes = -1;
1814 pftiTarget.csidl = -1;
1816 return IPersistFolder3_InitializeEx(&This->IPersistFolder3_iface, NULL, NULL, &pftiTarget);
1819 static HRESULT WINAPI PersistPropertyBag_Save(IPersistPropertyBag *iface,
1820 IPropertyBag *pPropertyBag, BOOL fClearDirty, BOOL fSaveAllProperties)
1822 UnixFolder *This = impl_from_IPersistPropertyBag(iface);
1823 FIXME("(%p): stub\n", This);
1824 return E_NOTIMPL;
1827 static const IPersistPropertyBagVtbl PersistPropertyBagVtbl = {
1828 PersistPropertyBag_QueryInterface,
1829 PersistPropertyBag_AddRef,
1830 PersistPropertyBag_Release,
1831 PersistPropertyBag_GetClassID,
1832 PersistPropertyBag_InitNew,
1833 PersistPropertyBag_Load,
1834 PersistPropertyBag_Save
1837 static HRESULT WINAPI SFHelper_QueryInterface(ISFHelper* iface, REFIID riid, void** ppvObject)
1839 UnixFolder *This = impl_from_ISFHelper(iface);
1840 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppvObject);
1843 static ULONG WINAPI SFHelper_AddRef(ISFHelper* iface)
1845 UnixFolder *This = impl_from_ISFHelper(iface);
1846 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
1849 static ULONG WINAPI SFHelper_Release(ISFHelper* iface)
1851 UnixFolder *This = impl_from_ISFHelper(iface);
1852 return IShellFolder2_Release(&This->IShellFolder2_iface);
1855 static HRESULT WINAPI SFHelper_GetUniqueName(ISFHelper* iface, LPWSTR pwszName, UINT uLen)
1857 UnixFolder *This = impl_from_ISFHelper(iface);
1858 IEnumIDList *pEnum;
1859 HRESULT hr;
1860 LPITEMIDLIST pidlElem;
1861 DWORD dwFetched;
1862 int i;
1863 WCHAR wszNewFolder[25];
1864 static const WCHAR wszFormat[] = { '%','s',' ','%','d',0 };
1866 TRACE("(%p)->(%p %u)\n", This, pwszName, uLen);
1868 LoadStringW(shell32_hInstance, IDS_NEWFOLDER, wszNewFolder, sizeof(wszNewFolder)/sizeof(WCHAR));
1870 if (uLen < sizeof(wszNewFolder)/sizeof(WCHAR)+3)
1871 return E_INVALIDARG;
1873 hr = IShellFolder2_EnumObjects(&This->IShellFolder2_iface, 0,
1874 SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN, &pEnum);
1875 if (SUCCEEDED(hr)) {
1876 lstrcpynW(pwszName, wszNewFolder, uLen);
1877 IEnumIDList_Reset(pEnum);
1878 i = 2;
1879 while ((IEnumIDList_Next(pEnum, 1, &pidlElem, &dwFetched) == S_OK) && (dwFetched == 1)) {
1880 WCHAR wszTemp[MAX_PATH];
1881 _ILSimpleGetTextW(pidlElem, wszTemp, MAX_PATH);
1882 if (!lstrcmpiW(wszTemp, pwszName)) {
1883 IEnumIDList_Reset(pEnum);
1884 snprintfW(pwszName, uLen, wszFormat, wszNewFolder, i++);
1885 if (i > 99) {
1886 hr = E_FAIL;
1887 break;
1891 IEnumIDList_Release(pEnum);
1893 return hr;
1896 static HRESULT WINAPI SFHelper_AddFolder(ISFHelper* iface, HWND hwnd, LPCWSTR pwszName,
1897 LPITEMIDLIST* ppidlOut)
1899 UnixFolder *This = impl_from_ISFHelper(iface);
1900 char szNewDir[FILENAME_MAX];
1901 int cBaseLen;
1903 TRACE("(%p)->(%p %s %p)\n", This, hwnd, debugstr_w(pwszName), ppidlOut);
1905 if (ppidlOut)
1906 *ppidlOut = NULL;
1908 if (!This->m_pszPath || !(This->m_dwAttributes & SFGAO_FILESYSTEM))
1909 return E_FAIL;
1911 lstrcpynA(szNewDir, This->m_pszPath, FILENAME_MAX);
1912 cBaseLen = lstrlenA(szNewDir);
1913 WideCharToMultiByte(CP_UNIXCP, 0, pwszName, -1, szNewDir+cBaseLen, FILENAME_MAX-cBaseLen, 0, 0);
1915 if (mkdir(szNewDir, 0777)) {
1916 char szMessage[256 + FILENAME_MAX];
1917 char szCaption[256];
1919 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_DENIED, szCaption, sizeof(szCaption));
1920 sprintf(szMessage, szCaption, szNewDir);
1921 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_CAPTION, szCaption, sizeof(szCaption));
1922 MessageBoxA(hwnd, szMessage, szCaption, MB_OK | MB_ICONEXCLAMATION);
1924 return E_FAIL;
1925 } else {
1926 LPITEMIDLIST pidlRelative;
1928 /* Inform the shell */
1929 if (SUCCEEDED(UNIXFS_path_to_pidl(This, NULL, pwszName, &pidlRelative))) {
1930 LPITEMIDLIST pidlAbsolute = ILCombine(This->m_pidlLocation, pidlRelative);
1931 if (ppidlOut)
1932 *ppidlOut = pidlRelative;
1933 else
1934 ILFree(pidlRelative);
1935 SHChangeNotify(SHCNE_MKDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1936 ILFree(pidlAbsolute);
1937 } else return E_FAIL;
1938 return S_OK;
1943 * Delete specified files by converting the path to DOS paths and calling
1944 * SHFileOperationW. If an error occurs it returns an error code. If the paths can't
1945 * be converted, S_FALSE is returned. In such situation DeleteItems will try to delete
1946 * the files using syscalls
1948 static HRESULT UNIXFS_delete_with_shfileop(UnixFolder *This, UINT cidl, const LPCITEMIDLIST *apidl)
1950 char szAbsolute[FILENAME_MAX], *pszRelative;
1951 LPWSTR wszPathsList, wszListPos;
1952 SHFILEOPSTRUCTW op;
1953 HRESULT ret;
1954 UINT i;
1956 lstrcpyA(szAbsolute, This->m_pszPath);
1957 pszRelative = szAbsolute + lstrlenA(szAbsolute);
1959 wszListPos = wszPathsList = HeapAlloc(GetProcessHeap(), 0, cidl*MAX_PATH*sizeof(WCHAR)+1);
1960 if (wszPathsList == NULL)
1961 return E_OUTOFMEMORY;
1962 for (i=0; i<cidl; i++) {
1963 LPWSTR wszDosPath;
1965 if (!_ILIsFolder(apidl[i]) && !_ILIsValue(apidl[i]))
1966 continue;
1967 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
1969 HeapFree(GetProcessHeap(), 0, wszPathsList);
1970 return E_INVALIDARG;
1972 wszDosPath = wine_get_dos_file_name(szAbsolute);
1973 if (wszDosPath == NULL || lstrlenW(wszDosPath) >= MAX_PATH)
1975 HeapFree(GetProcessHeap(), 0, wszPathsList);
1976 HeapFree(GetProcessHeap(), 0, wszDosPath);
1977 return S_FALSE;
1979 lstrcpyW(wszListPos, wszDosPath);
1980 wszListPos += lstrlenW(wszListPos)+1;
1981 HeapFree(GetProcessHeap(), 0, wszDosPath);
1983 *wszListPos = 0;
1985 ZeroMemory(&op, sizeof(op));
1986 op.hwnd = GetActiveWindow();
1987 op.wFunc = FO_DELETE;
1988 op.pFrom = wszPathsList;
1989 op.fFlags = FOF_ALLOWUNDO;
1990 if (!SHFileOperationW(&op))
1992 WARN("SHFileOperationW failed\n");
1993 ret = E_FAIL;
1995 else
1996 ret = S_OK;
1998 HeapFree(GetProcessHeap(), 0, wszPathsList);
1999 return ret;
2002 static HRESULT UNIXFS_delete_with_syscalls(UnixFolder *This, UINT cidl, const LPCITEMIDLIST *apidl)
2004 char szAbsolute[FILENAME_MAX], *pszRelative;
2005 static const WCHAR empty[] = {0};
2006 UINT i;
2008 if (!SHELL_ConfirmYesNoW(GetActiveWindow(), ASK_DELETE_SELECTED, empty))
2009 return S_OK;
2011 lstrcpyA(szAbsolute, This->m_pszPath);
2012 pszRelative = szAbsolute + lstrlenA(szAbsolute);
2014 for (i=0; i<cidl; i++) {
2015 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
2016 return E_INVALIDARG;
2017 if (_ILIsFolder(apidl[i])) {
2018 if (rmdir(szAbsolute))
2019 return E_FAIL;
2020 } else if (_ILIsValue(apidl[i])) {
2021 if (unlink(szAbsolute))
2022 return E_FAIL;
2025 return S_OK;
2028 static HRESULT WINAPI SFHelper_DeleteItems(ISFHelper* iface, UINT cidl, LPCITEMIDLIST* apidl)
2030 UnixFolder *This = impl_from_ISFHelper(iface);
2031 char szAbsolute[FILENAME_MAX], *pszRelative;
2032 LPITEMIDLIST pidlAbsolute;
2033 HRESULT hr = S_OK;
2034 UINT i;
2035 struct stat st;
2037 TRACE("(%p)->(%d %p)\n", This, cidl, apidl);
2039 hr = UNIXFS_delete_with_shfileop(This, cidl, apidl);
2040 if (hr == S_FALSE)
2041 hr = UNIXFS_delete_with_syscalls(This, cidl, apidl);
2043 lstrcpyA(szAbsolute, This->m_pszPath);
2044 pszRelative = szAbsolute + lstrlenA(szAbsolute);
2046 /* we need to manually send the notifies if the files doesn't exist */
2047 for (i=0; i<cidl; i++) {
2048 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
2049 continue;
2050 pidlAbsolute = ILCombine(This->m_pidlLocation, apidl[i]);
2051 if (stat(szAbsolute, &st))
2053 if (_ILIsFolder(apidl[i])) {
2054 SHChangeNotify(SHCNE_RMDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
2055 } else if (_ILIsValue(apidl[i])) {
2056 SHChangeNotify(SHCNE_DELETE, SHCNF_IDLIST, pidlAbsolute, NULL);
2059 ILFree(pidlAbsolute);
2062 return hr;
2065 static HRESULT WINAPI SFHelper_CopyItems(ISFHelper* iface, IShellFolder *psfFrom,
2066 UINT cidl, LPCITEMIDLIST *apidl)
2068 UnixFolder *This = impl_from_ISFHelper(iface);
2069 DWORD dwAttributes;
2070 UINT i;
2071 HRESULT hr;
2072 char szAbsoluteDst[FILENAME_MAX], *pszRelativeDst;
2074 TRACE("(%p)->(%p %d %p)\n", This, psfFrom, cidl, apidl);
2076 if (!psfFrom || !cidl || !apidl)
2077 return E_INVALIDARG;
2079 /* All source items have to be filesystem items. */
2080 dwAttributes = SFGAO_FILESYSTEM;
2081 hr = IShellFolder_GetAttributesOf(psfFrom, cidl, apidl, &dwAttributes);
2082 if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM))
2083 return E_INVALIDARG;
2085 lstrcpyA(szAbsoluteDst, This->m_pszPath);
2086 pszRelativeDst = szAbsoluteDst + strlen(szAbsoluteDst);
2088 for (i=0; i<cidl; i++) {
2089 WCHAR wszSrc[MAX_PATH];
2090 char szSrc[FILENAME_MAX];
2091 STRRET strret;
2092 HRESULT res;
2093 WCHAR *pwszDosSrc, *pwszDosDst;
2095 /* Build the unix path of the current source item. */
2096 if (FAILED(IShellFolder_GetDisplayNameOf(psfFrom, apidl[i], SHGDN_FORPARSING, &strret)))
2097 return E_FAIL;
2098 if (FAILED(StrRetToBufW(&strret, apidl[i], wszSrc, MAX_PATH)))
2099 return E_FAIL;
2100 if (!UNIXFS_get_unix_path(wszSrc, szSrc))
2101 return E_FAIL;
2103 /* Build the unix path of the current destination item */
2104 UNIXFS_filename_from_shitemid(apidl[i], pszRelativeDst);
2106 pwszDosSrc = wine_get_dos_file_name(szSrc);
2107 pwszDosDst = wine_get_dos_file_name(szAbsoluteDst);
2109 if (pwszDosSrc && pwszDosDst)
2110 res = UNIXFS_copy(pwszDosSrc, pwszDosDst);
2111 else
2112 res = E_OUTOFMEMORY;
2114 HeapFree(GetProcessHeap(), 0, pwszDosSrc);
2115 HeapFree(GetProcessHeap(), 0, pwszDosDst);
2117 if (res != S_OK)
2118 return res;
2120 return S_OK;
2123 static const ISFHelperVtbl SFHelperVtbl = {
2124 SFHelper_QueryInterface,
2125 SFHelper_AddRef,
2126 SFHelper_Release,
2127 SFHelper_GetUniqueName,
2128 SFHelper_AddFolder,
2129 SFHelper_DeleteItems,
2130 SFHelper_CopyItems
2133 static HRESULT WINAPI DropTarget_QueryInterface(IDropTarget* iface, REFIID riid, void** ppvObject)
2135 UnixFolder *This = impl_from_IDropTarget(iface);
2136 return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppvObject);
2139 static ULONG WINAPI DropTarget_AddRef(IDropTarget* iface)
2141 UnixFolder *This = impl_from_IDropTarget(iface);
2142 return IShellFolder2_AddRef(&This->IShellFolder2_iface);
2145 static ULONG WINAPI DropTarget_Release(IDropTarget* iface)
2147 UnixFolder *This = impl_from_IDropTarget(iface);
2148 return IShellFolder2_Release(&This->IShellFolder2_iface);
2151 #define HIDA_GetPIDLFolder(pida) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[0])
2152 #define HIDA_GetPIDLItem(pida, i) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[i+1])
2154 static HRESULT WINAPI DropTarget_DragEnter(IDropTarget *iface, IDataObject *pDataObject,
2155 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2157 UnixFolder *This = impl_from_IDropTarget(iface);
2158 FORMATETC format;
2159 STGMEDIUM medium;
2161 TRACE("(%p)->(%p 0x%08x {.x=%d, .y=%d} %p)\n", This, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
2163 if (!pdwEffect || !pDataObject)
2164 return E_INVALIDARG;
2166 /* Compute a mask of supported drop-effects for this shellfolder object and the given data
2167 * object. Dropping is only supported on folders, which represent filesystem locations. One
2168 * can't drop on file objects. And the 'move' drop effect is only supported, if the source
2169 * folder is not identical to the target folder. */
2170 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2171 InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
2172 if ((This->m_dwAttributes & SFGAO_FILESYSTEM) && /* Only drop to filesystem folders */
2173 _ILIsFolder(ILFindLastID(This->m_pidlLocation)) && /* Only drop to folders, not to files */
2174 SUCCEEDED(IDataObject_GetData(pDataObject, &format, &medium))) /* Only ShellIDList format */
2176 LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
2177 This->m_dwDropEffectsMask |= DROPEFFECT_COPY|DROPEFFECT_LINK;
2179 if (pidaShellIDList) { /* Files can only be moved between two different folders */
2180 if (!ILIsEqual(HIDA_GetPIDLFolder(pidaShellIDList), This->m_pidlLocation))
2181 This->m_dwDropEffectsMask |= DROPEFFECT_MOVE;
2182 GlobalUnlock(medium.u.hGlobal);
2186 *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
2188 return S_OK;
2191 static HRESULT WINAPI DropTarget_DragOver(IDropTarget *iface, DWORD dwKeyState,
2192 POINTL pt, DWORD *pdwEffect)
2194 UnixFolder *This = impl_from_IDropTarget(iface);
2196 TRACE("(%p)->(0x%08x {.x=%d, .y=%d} %p)\n", This, dwKeyState, pt.x, pt.y, pdwEffect);
2198 if (!pdwEffect)
2199 return E_INVALIDARG;
2201 *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
2203 return S_OK;
2206 static HRESULT WINAPI DropTarget_DragLeave(IDropTarget *iface)
2208 UnixFolder *This = impl_from_IDropTarget(iface);
2210 TRACE("(%p)\n", This);
2212 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2213 return S_OK;
2216 static HRESULT WINAPI DropTarget_Drop(IDropTarget *iface, IDataObject *pDataObject,
2217 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2219 UnixFolder *This = impl_from_IDropTarget(iface);
2220 FORMATETC format;
2221 STGMEDIUM medium;
2222 HRESULT hr;
2224 TRACE("(%p)->(%p %d {.x=%d, .y=%d} %p) semi-stub\n",
2225 This, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
2227 InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
2228 hr = IDataObject_GetData(pDataObject, &format, &medium);
2229 if (FAILED(hr))
2230 return hr;
2232 if (medium.tymed == TYMED_HGLOBAL) {
2233 IShellFolder *psfSourceFolder, *psfDesktopFolder;
2234 LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
2235 STRRET strret;
2236 UINT i;
2238 if (!pidaShellIDList)
2239 return HRESULT_FROM_WIN32(GetLastError());
2241 hr = SHGetDesktopFolder(&psfDesktopFolder);
2242 if (FAILED(hr)) {
2243 GlobalUnlock(medium.u.hGlobal);
2244 return hr;
2247 hr = IShellFolder_BindToObject(psfDesktopFolder, HIDA_GetPIDLFolder(pidaShellIDList), NULL,
2248 &IID_IShellFolder, (LPVOID*)&psfSourceFolder);
2249 IShellFolder_Release(psfDesktopFolder);
2250 if (FAILED(hr)) {
2251 GlobalUnlock(medium.u.hGlobal);
2252 return hr;
2255 for (i = 0; i < pidaShellIDList->cidl; i++) {
2256 WCHAR wszSourcePath[MAX_PATH];
2258 hr = IShellFolder_GetDisplayNameOf(psfSourceFolder, HIDA_GetPIDLItem(pidaShellIDList, i),
2259 SHGDN_FORPARSING, &strret);
2260 if (FAILED(hr))
2261 break;
2263 hr = StrRetToBufW(&strret, NULL, wszSourcePath, MAX_PATH);
2264 if (FAILED(hr))
2265 break;
2267 switch (*pdwEffect) {
2268 case DROPEFFECT_MOVE:
2269 FIXME("Move %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2270 break;
2271 case DROPEFFECT_COPY:
2272 FIXME("Copy %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2273 break;
2274 case DROPEFFECT_LINK:
2275 FIXME("Link %s from %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2276 break;
2280 IShellFolder_Release(psfSourceFolder);
2281 GlobalUnlock(medium.u.hGlobal);
2282 return hr;
2285 return E_NOTIMPL;
2288 static const IDropTargetVtbl DropTargetVtbl = {
2289 DropTarget_QueryInterface,
2290 DropTarget_AddRef,
2291 DropTarget_Release,
2292 DropTarget_DragEnter,
2293 DropTarget_DragOver,
2294 DropTarget_DragLeave,
2295 DropTarget_Drop
2298 /******************************************************************************
2299 * Unix[Dos]Folder_Constructor [Internal]
2301 * PARAMS
2302 * pUnkOuter [I] Outer class for aggregation. Currently ignored.
2303 * riid [I] Interface asked for by the client.
2304 * ppv [O] Pointer to an riid interface to the UnixFolder object.
2306 * NOTES
2307 * Those are the only functions exported from shfldr_unixfs.c. They are called from
2308 * shellole.c's default class factory and thus have to exhibit a LPFNCREATEINSTANCE
2309 * compatible signature.
2311 * The UnixDosFolder_Constructor sets the dwPathMode member to PATHMODE_DOS. This
2312 * means that paths are converted from dos to unix and back at the interfaces.
2314 static HRESULT CreateUnixFolder(IUnknown *outer, REFIID riid, void **ppv, const CLSID *clsid)
2316 UnixFolder *This;
2317 HRESULT hr;
2319 if (outer) {
2320 FIXME("Aggregation not yet implemented!\n");
2321 return CLASS_E_NOAGGREGATION;
2324 This = SHAlloc((ULONG)sizeof(UnixFolder));
2325 if (!This) return E_OUTOFMEMORY;
2327 This->IShellFolder2_iface.lpVtbl = &ShellFolder2Vtbl;
2328 This->IPersistFolder3_iface.lpVtbl = &PersistFolder3Vtbl;
2329 This->IPersistPropertyBag_iface.lpVtbl = &PersistPropertyBagVtbl;
2330 This->ISFHelper_iface.lpVtbl = &SFHelperVtbl;
2331 This->IDropTarget_iface.lpVtbl = &DropTargetVtbl;
2332 This->ref = 1;
2333 This->m_pszPath = NULL;
2334 This->m_pidlLocation = NULL;
2335 This->m_dwPathMode = IsEqualCLSID(&CLSID_UnixFolder, clsid) ? PATHMODE_UNIX : PATHMODE_DOS;
2336 This->m_dwAttributes = 0;
2337 This->m_pCLSID = clsid;
2338 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2340 hr = IShellFolder2_QueryInterface(&This->IShellFolder2_iface, riid, ppv);
2341 IShellFolder2_Release(&This->IShellFolder2_iface);
2343 return hr;
2346 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2347 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2348 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixFolder);
2351 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2352 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2353 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixDosFolder);
2356 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2357 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2358 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_FolderShortcut);
2361 HRESULT WINAPI MyDocuments_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2362 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2363 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_MyDocuments);
2366 /******************************************************************************
2367 * UnixSubFolderIterator
2369 * Class whose heap based objects represent iterators over the sub-directories
2370 * of a given UnixFolder object.
2373 /* UnixSubFolderIterator object layout and typedef.
2375 typedef struct _UnixSubFolderIterator {
2376 const IEnumIDListVtbl *lpIEnumIDListVtbl;
2377 LONG m_cRef;
2378 SHCONTF m_fFilter;
2379 DIR *m_dirFolder;
2380 char m_szFolder[FILENAME_MAX];
2381 } UnixSubFolderIterator;
2383 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
2384 TRACE("(iterator=%p)\n", iterator);
2386 if (iterator->m_dirFolder)
2387 closedir(iterator->m_dirFolder);
2388 SHFree(iterator);
2391 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface,
2392 REFIID riid, void** ppv)
2394 TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
2396 if (!ppv) return E_INVALIDARG;
2398 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
2399 *ppv = iface;
2400 } else {
2401 *ppv = NULL;
2402 return E_NOINTERFACE;
2405 IEnumIDList_AddRef(iface);
2406 return S_OK;
2409 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
2411 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2413 TRACE("(iface=%p)\n", iface);
2415 return InterlockedIncrement(&This->m_cRef);
2418 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
2420 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2421 ULONG cRef;
2423 TRACE("(iface=%p)\n", iface);
2425 cRef = InterlockedDecrement(&This->m_cRef);
2427 if (!cRef)
2428 UnixSubFolderIterator_Destroy(This);
2430 return cRef;
2433 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt,
2434 LPITEMIDLIST* rgelt, ULONG* pceltFetched)
2436 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2437 ULONG i = 0;
2439 /* This->m_dirFolder will be NULL if the user doesn't have access rights for the dir. */
2440 if (This->m_dirFolder) {
2441 char *pszRelativePath = This->m_szFolder + lstrlenA(This->m_szFolder);
2442 struct dirent *pDirEntry;
2444 while (i < celt) {
2445 pDirEntry = readdir(This->m_dirFolder);
2446 if (!pDirEntry) break; /* No more entries */
2447 if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
2449 /* Temporarily build absolute path in This->m_szFolder. Then construct a pidl
2450 * and see if it passes the filter.
2452 lstrcpyA(pszRelativePath, pDirEntry->d_name);
2453 rgelt[i] = SHAlloc(
2454 UNIXFS_shitemid_len_from_filename(pszRelativePath, NULL, NULL)+sizeof(USHORT));
2455 if (!UNIXFS_build_shitemid(This->m_szFolder, TRUE, NULL, rgelt[i]) ||
2456 !UNIXFS_is_pidl_of_type(rgelt[i], This->m_fFilter))
2458 SHFree(rgelt[i]);
2459 rgelt[i] = NULL;
2460 continue;
2462 memset(((PBYTE)rgelt[i])+rgelt[i]->mkid.cb, 0, sizeof(USHORT));
2463 i++;
2465 *pszRelativePath = '\0'; /* Restore the original path in This->m_szFolder. */
2468 if (pceltFetched)
2469 *pceltFetched = i;
2471 return (i == 0) ? S_FALSE : S_OK;
2474 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
2476 LPITEMIDLIST *apidl;
2477 ULONG cFetched;
2478 HRESULT hr;
2480 TRACE("(iface=%p, celt=%d)\n", iface, celt);
2482 /* Call IEnumIDList::Next and delete the resulting pidls. */
2483 apidl = SHAlloc(celt * sizeof(LPITEMIDLIST));
2484 hr = IEnumIDList_Next(iface, celt, apidl, &cFetched);
2485 if (SUCCEEDED(hr))
2486 while (cFetched--)
2487 SHFree(apidl[cFetched]);
2488 SHFree(apidl);
2490 return hr;
2493 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
2495 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2497 TRACE("(iface=%p)\n", iface);
2499 if (This->m_dirFolder)
2500 rewinddir(This->m_dirFolder);
2502 return S_OK;
2505 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This,
2506 IEnumIDList** ppenum)
2508 FIXME("stub\n");
2509 return E_NOTIMPL;
2512 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
2514 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
2515 UnixSubFolderIterator_IEnumIDList_QueryInterface,
2516 UnixSubFolderIterator_IEnumIDList_AddRef,
2517 UnixSubFolderIterator_IEnumIDList_Release,
2518 UnixSubFolderIterator_IEnumIDList_Next,
2519 UnixSubFolderIterator_IEnumIDList_Skip,
2520 UnixSubFolderIterator_IEnumIDList_Reset,
2521 UnixSubFolderIterator_IEnumIDList_Clone
2524 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter) {
2525 UnixSubFolderIterator *iterator;
2527 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
2529 iterator = SHAlloc((ULONG)sizeof(UnixSubFolderIterator));
2530 iterator->lpIEnumIDListVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
2531 iterator->m_cRef = 0;
2532 iterator->m_fFilter = fFilter;
2533 iterator->m_dirFolder = opendir(pUnixFolder->m_pszPath);
2534 lstrcpyA(iterator->m_szFolder, pUnixFolder->m_pszPath);
2536 UnixSubFolderIterator_IEnumIDList_AddRef((IEnumIDList*)iterator);
2538 return (IUnknown*)iterator;
2541 #else /* __MINGW32__ || _MSC_VER */
2543 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2545 return E_NOTIMPL;
2548 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2550 return E_NOTIMPL;
2553 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2555 return E_NOTIMPL;
2558 HRESULT WINAPI MyDocuments_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2560 return E_NOTIMPL;
2563 #endif /* __MINGW32__ || _MSC_VER */
2565 /******************************************************************************
2566 * UNIXFS_is_rooted_at_desktop [Internal]
2568 * Checks if the unixfs namespace extension is rooted at desktop level.
2570 * RETURNS
2571 * TRUE, if unixfs is rooted at desktop level
2572 * FALSE, if not.
2574 BOOL UNIXFS_is_rooted_at_desktop(void) {
2575 HKEY hKey;
2576 WCHAR wszRootedAtDesktop[69 + CHARS_IN_GUID] = {
2577 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2578 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2579 'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
2580 'N','a','m','e','S','p','a','c','e','\\',0 };
2582 if (StringFromGUID2(&CLSID_UnixDosFolder, wszRootedAtDesktop + 69, CHARS_IN_GUID) &&
2583 RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszRootedAtDesktop, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
2585 RegCloseKey(hKey);
2586 return TRUE;
2588 return FALSE;