ole32: Get rid of the ICOM_THIS_MULTI macro.
[wine/multimedia.git] / dlls / shell32 / shfldr_unixfs.c
blob11f3686f0fe835033a29fae9aa15d8b37adb1279
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 /* UnixFolder object layout and typedef.
193 typedef struct _UnixFolder {
194 const IShellFolder2Vtbl *lpIShellFolder2Vtbl;
195 const IPersistFolder3Vtbl *lpIPersistFolder3Vtbl;
196 const IPersistPropertyBagVtbl *lpIPersistPropertyBagVtbl;
197 const IDropTargetVtbl *lpIDropTargetVtbl;
198 const ISFHelperVtbl *lpISFHelperVtbl;
199 LONG m_cRef;
200 CHAR *m_pszPath; /* Target path of the shell folder (CP_UNIXCP) */
201 LPITEMIDLIST m_pidlLocation; /* Location in the shell namespace */
202 DWORD m_dwPathMode;
203 DWORD m_dwAttributes;
204 const CLSID *m_pCLSID;
205 DWORD m_dwDropEffectsMask;
206 } UnixFolder;
208 /* Will hold the registered clipboard format identifier for ITEMIDLISTS. */
209 static UINT cfShellIDList = 0;
211 /******************************************************************************
212 * UNIXFS_filename_from_shitemid [Internal]
214 * Get CP_UNIXCP encoded filename corresponding to the first item of a pidl
216 * PARAMS
217 * pidl [I] A simple SHITEMID
218 * pszPathElement [O] Filename in CP_UNIXCP encoding will be stored here
220 * RETURNS
221 * Success: Number of bytes necessary to store the CP_UNIXCP encoded filename
222 * _without_ the terminating NUL.
223 * Failure: 0
225 * NOTES
226 * Size of the buffer at pszPathElement has to be FILENAME_MAX. pszPathElement
227 * may be NULL, if you are only interested in the return value.
229 static int UNIXFS_filename_from_shitemid(LPCITEMIDLIST pidl, char* pszPathElement) {
230 FileStructW *pFileStructW = _ILGetFileStructW(pidl);
231 int cLen = 0;
233 if (pFileStructW) {
234 cLen = WideCharToMultiByte(CP_UNIXCP, 0, pFileStructW->wszName, -1, pszPathElement,
235 pszPathElement ? FILENAME_MAX : 0, 0, 0);
236 } else {
237 /* There might be pidls slipping in from shfldr_fs.c, which don't contain the
238 * FileStructW field. In this case, we have to convert from CP_ACP to CP_UNIXCP. */
239 char *pszText = _ILGetTextPointer(pidl);
240 WCHAR *pwszPathElement = NULL;
241 int cWideChars;
243 cWideChars = MultiByteToWideChar(CP_ACP, 0, pszText, -1, NULL, 0);
244 if (!cWideChars) goto cleanup;
246 pwszPathElement = SHAlloc(cWideChars * sizeof(WCHAR));
247 if (!pwszPathElement) goto cleanup;
249 cWideChars = MultiByteToWideChar(CP_ACP, 0, pszText, -1, pwszPathElement, cWideChars);
250 if (!cWideChars) goto cleanup;
252 cLen = WideCharToMultiByte(CP_UNIXCP, 0, pwszPathElement, -1, pszPathElement,
253 pszPathElement ? FILENAME_MAX : 0, 0, 0);
255 cleanup:
256 SHFree(pwszPathElement);
259 if (cLen) cLen--; /* Don't count terminating NUL! */
260 return cLen;
263 /******************************************************************************
264 * UNIXFS_shitemid_len_from_filename [Internal]
266 * Computes the necessary length of a pidl to hold a path element
268 * PARAMS
269 * szPathElement [I] The path element string in CP_UNIXCP encoding.
270 * ppszPathElement [O] Path element string in CP_ACP encoding.
271 * ppwszPathElement [O] Path element string as WCHAR string.
273 * RETURNS
274 * Success: Length in bytes of a SHITEMID representing szPathElement
275 * Failure: 0
277 * NOTES
278 * Provide NULL values if not interested in pp(w)szPathElement. Otherwise
279 * caller is responsible to free ppszPathElement and ppwszPathElement with
280 * SHFree.
282 static USHORT UNIXFS_shitemid_len_from_filename(
283 const char *szPathElement, char **ppszPathElement, WCHAR **ppwszPathElement)
285 USHORT cbPidlLen = 0;
286 WCHAR *pwszPathElement = NULL;
287 char *pszPathElement = NULL;
288 int cWideChars, cChars;
290 /* There and Back Again: A Hobbit's Holiday. CP_UNIXCP might be some ANSI
291 * codepage or it might be a real multi-byte encoding like utf-8. There is no
292 * other way to figure out the length of the corresponding WCHAR and CP_ACP
293 * strings without actually doing the full CP_UNIXCP -> WCHAR -> CP_ACP cycle. */
295 cWideChars = MultiByteToWideChar(CP_UNIXCP, 0, szPathElement, -1, NULL, 0);
296 if (!cWideChars) goto cleanup;
298 pwszPathElement = SHAlloc(cWideChars * sizeof(WCHAR));
299 if (!pwszPathElement) goto cleanup;
301 cWideChars = MultiByteToWideChar(CP_UNIXCP, 0, szPathElement, -1, pwszPathElement, cWideChars);
302 if (!cWideChars) goto cleanup;
304 cChars = WideCharToMultiByte(CP_ACP, 0, pwszPathElement, -1, NULL, 0, 0, 0);
305 if (!cChars) goto cleanup;
307 pszPathElement = SHAlloc(cChars);
308 if (!pszPathElement) goto cleanup;
310 cChars = WideCharToMultiByte(CP_ACP, 0, pwszPathElement, -1, pszPathElement, cChars, 0, 0);
311 if (!cChars) goto cleanup;
313 /* (cChars & 0x1) is for the potential alignment byte */
314 cbPidlLen = LEN_SHITEMID_FIXED_PART + cChars + (cChars & 0x1) + cWideChars * sizeof(WCHAR);
316 cleanup:
317 if (cbPidlLen && ppszPathElement)
318 *ppszPathElement = pszPathElement;
319 else
320 SHFree(pszPathElement);
322 if (cbPidlLen && ppwszPathElement)
323 *ppwszPathElement = pwszPathElement;
324 else
325 SHFree(pwszPathElement);
327 return cbPidlLen;
330 /******************************************************************************
331 * UNIXFS_is_pidl_of_type [Internal]
333 * Checks for the first SHITEMID of an ITEMIDLIST if it passes a filter.
335 * PARAMS
336 * pIDL [I] The ITEMIDLIST to be checked.
337 * fFilter [I] Shell condition flags, which specify the filter.
339 * RETURNS
340 * TRUE, if pIDL is accepted by fFilter
341 * FALSE, otherwise
343 static inline BOOL UNIXFS_is_pidl_of_type(LPCITEMIDLIST pIDL, SHCONTF fFilter) {
344 const PIDLDATA *pIDLData = _ILGetDataPointer(pIDL);
345 if (!(fFilter & SHCONTF_INCLUDEHIDDEN) && pIDLData &&
346 (pIDLData->u.file.uFileAttribs & FILE_ATTRIBUTE_HIDDEN))
348 return FALSE;
350 if (_ILIsFolder(pIDL) && (fFilter & SHCONTF_FOLDERS)) return TRUE;
351 if (_ILIsValue(pIDL) && (fFilter & SHCONTF_NONFOLDERS)) return TRUE;
352 return FALSE;
355 /******************************************************************************
356 * UNIXFS_get_unix_path [Internal]
358 * Convert an absolute dos path to an absolute unix path.
359 * Evaluate "/.", "/.." and the symbolic links in $WINEPREFIX/dosdevices.
361 * PARAMS
362 * pszDosPath [I] An absolute dos path
363 * pszCanonicalPath [O] Buffer of length FILENAME_MAX. Will receive the canonical path.
365 * RETURNS
366 * Success, TRUE
367 * Failure, FALSE - Path not existent, too long, insufficient rights, to many symlinks
369 static BOOL UNIXFS_get_unix_path(LPCWSTR pszDosPath, char *pszCanonicalPath)
371 char *pPathTail, *pElement, *pCanonicalTail, szPath[FILENAME_MAX], *pszUnixPath;
372 WCHAR wszDrive[] = { '?', ':', '\\', 0 };
373 int cDriveSymlinkLen;
375 TRACE("(pszDosPath=%s, pszCanonicalPath=%p)\n", debugstr_w(pszDosPath), pszCanonicalPath);
377 if (!pszDosPath || pszDosPath[1] != ':')
378 return FALSE;
380 /* Get the canonicalized unix path corresponding to the drive letter. */
381 wszDrive[0] = pszDosPath[0];
382 pszUnixPath = wine_get_unix_file_name(wszDrive);
383 if (!pszUnixPath) return FALSE;
384 cDriveSymlinkLen = strlen(pszUnixPath);
385 pElement = realpath(pszUnixPath, szPath);
386 HeapFree(GetProcessHeap(), 0, pszUnixPath);
387 if (!pElement) return FALSE;
388 if (szPath[strlen(szPath)-1] != '/') strcat(szPath, "/");
390 /* Append the part relative to the drive symbolic link target. */
391 pszUnixPath = wine_get_unix_file_name(pszDosPath);
392 if (!pszUnixPath) return FALSE;
393 strcat(szPath, pszUnixPath + cDriveSymlinkLen);
394 HeapFree(GetProcessHeap(), 0, pszUnixPath);
396 /* pCanonicalTail always points to the end of the canonical path constructed
397 * thus far. pPathTail points to the still to be processed part of the input
398 * path. pElement points to the path element currently investigated.
400 *pszCanonicalPath = '\0';
401 pCanonicalTail = pszCanonicalPath;
402 pPathTail = szPath;
404 do {
405 char cTemp;
407 pElement = pPathTail;
408 pPathTail = strchr(pPathTail+1, '/');
409 if (!pPathTail) /* Last path element may not be terminated by '/'. */
410 pPathTail = pElement + strlen(pElement);
411 /* Temporarily terminate the current path element. Will be restored later. */
412 cTemp = *pPathTail;
413 *pPathTail = '\0';
415 /* Skip "/." path elements */
416 if (!strcmp("/.", pElement)) {
417 *pPathTail = cTemp;
418 } else if (!strcmp("/..", pElement)) {
419 /* Remove last element in canonical path for "/.." elements, then skip. */
420 char *pTemp = strrchr(pszCanonicalPath, '/');
421 if (pTemp)
422 pCanonicalTail = pTemp;
423 *pCanonicalTail = '\0';
424 *pPathTail = cTemp;
425 } else {
426 /* Directory or file. Copy to canonical path */
427 if (pCanonicalTail - pszCanonicalPath + pPathTail - pElement + 1 > FILENAME_MAX)
428 return FALSE;
430 memcpy(pCanonicalTail, pElement, pPathTail - pElement + 1);
431 pCanonicalTail += pPathTail - pElement;
432 *pPathTail = cTemp;
434 } while (pPathTail[0] == '/');
436 TRACE("--> %s\n", debugstr_a(pszCanonicalPath));
438 return TRUE;
441 /******************************************************************************
442 * UNIXFS_seconds_since_1970_to_dos_date_time [Internal]
444 * Convert unix time to FAT time
446 * PARAMS
447 * ss1970 [I] Unix time (seconds since 1970)
448 * pDate [O] Corresponding FAT date
449 * pTime [O] Corresponding FAT time
451 static inline void UNIXFS_seconds_since_1970_to_dos_date_time(
452 time_t ss1970, LPWORD pDate, LPWORD pTime)
454 LARGE_INTEGER time;
455 FILETIME fileTime;
457 RtlSecondsSince1970ToTime( ss1970, &time );
458 fileTime.dwLowDateTime = time.u.LowPart;
459 fileTime.dwHighDateTime = time.u.HighPart;
460 FileTimeToDosDateTime(&fileTime, pDate, pTime);
463 /******************************************************************************
464 * UNIXFS_build_shitemid [Internal]
466 * Constructs a new SHITEMID for the last component of path 'pszUnixPath' into
467 * buffer 'pIDL'.
469 * PARAMS
470 * pszUnixPath [I] An absolute path. The SHITEMID will be built for the last component.
471 * pbc [I] Bind context for this action, used to determine if the file must exist
472 * pIDL [O] SHITEMID will be constructed here.
474 * RETURNS
475 * Success: A pointer to the terminating '\0' character of path.
476 * Failure: NULL
478 * NOTES
479 * Minimum size of pIDL is SHITEMID_LEN_FROM_NAME_LEN(strlen(last_component_of_path)).
480 * If what you need is a PIDLLIST with a single SHITEMID, don't forget to append
481 * a 0 USHORT value.
483 static char* UNIXFS_build_shitemid(char *pszUnixPath, LPBC pbc, void *pIDL) {
484 LPPIDLDATA pIDLData;
485 struct stat fileStat;
486 char *pszComponentU, *pszComponentA;
487 WCHAR *pwszComponentW;
488 int cComponentULen, cComponentALen;
489 USHORT cbLen;
490 FileStructW *pFileStructW;
491 WORD uOffsetW, *pOffsetW;
492 BOOL must_exist = TRUE;
494 TRACE("(pszUnixPath=%s, pbc=%p, pIDL=%p)\n", debugstr_a(pszUnixPath), pbc, pIDL);
496 if (pbc){
497 IUnknown *unk;
498 IFileSystemBindData *fsb;
499 HRESULT hr;
501 hr = IBindCtx_GetObjectParam(pbc, (LPOLESTR)wFileSystemBindData, &unk);
502 if (SUCCEEDED(hr)) {
503 hr = IUnknown_QueryInterface(unk, &IID_IFileSystemBindData, (LPVOID*)&fsb);
504 if (SUCCEEDED(hr)) {
505 /* Windows tries to get WIN32_FIND_DATAW structure from
506 * fsb here for no known reason */
507 must_exist = FALSE;
508 IFileSystemBindData_Release(fsb);
510 IUnknown_Release(unk);
514 /* We are only interested in regular files and directories. */
515 if (stat(pszUnixPath, &fileStat)){
516 if (must_exist || errno != ENOENT)
517 return NULL;
518 }else
519 if (!S_ISDIR(fileStat.st_mode) && !S_ISREG(fileStat.st_mode))
520 return NULL;
522 /* Compute the SHITEMID's length and wipe it. */
523 pszComponentU = strrchr(pszUnixPath, '/') + 1;
524 cComponentULen = strlen(pszComponentU);
525 cbLen = UNIXFS_shitemid_len_from_filename(pszComponentU, &pszComponentA, &pwszComponentW);
526 if (!cbLen) return NULL;
527 memset(pIDL, 0, cbLen);
528 ((LPSHITEMID)pIDL)->cb = cbLen;
530 /* Set shell32's standard SHITEMID data fields. */
531 pIDLData = _ILGetDataPointer(pIDL);
532 pIDLData->type = S_ISDIR(fileStat.st_mode) ? PT_FOLDER : PT_VALUE;
533 pIDLData->u.file.dwFileSize = (DWORD)fileStat.st_size;
534 UNIXFS_seconds_since_1970_to_dos_date_time(fileStat.st_mtime, &pIDLData->u.file.uFileDate,
535 &pIDLData->u.file.uFileTime);
536 pIDLData->u.file.uFileAttribs = 0;
537 if (S_ISDIR(fileStat.st_mode)) pIDLData->u.file.uFileAttribs |= FILE_ATTRIBUTE_DIRECTORY;
538 if (pszComponentU[0] == '.') pIDLData->u.file.uFileAttribs |= FILE_ATTRIBUTE_HIDDEN;
539 cComponentALen = lstrlenA(pszComponentA) + 1;
540 memcpy(pIDLData->u.file.szNames, pszComponentA, cComponentALen);
542 pFileStructW = (FileStructW*)(pIDLData->u.file.szNames + cComponentALen + (cComponentALen & 0x1));
543 uOffsetW = (WORD)(((LPBYTE)pFileStructW) - ((LPBYTE)pIDL));
544 pFileStructW->cbLen = cbLen - uOffsetW;
545 UNIXFS_seconds_since_1970_to_dos_date_time(fileStat.st_mtime, &pFileStructW->uCreationDate,
546 &pFileStructW->uCreationTime);
547 UNIXFS_seconds_since_1970_to_dos_date_time(fileStat.st_atime, &pFileStructW->uLastAccessDate,
548 &pFileStructW->uLastAccessTime);
549 lstrcpyW(pFileStructW->wszName, pwszComponentW);
551 pOffsetW = (WORD*)(((LPBYTE)pIDL) + cbLen - sizeof(WORD));
552 *pOffsetW = uOffsetW;
554 SHFree(pszComponentA);
555 SHFree(pwszComponentW);
557 return pszComponentU + cComponentULen;
560 /******************************************************************************
561 * UNIXFS_path_to_pidl [Internal]
563 * PARAMS
564 * pUnixFolder [I] If path is relative, pUnixFolder represents the base path
565 * path [I] An absolute unix or dos path or a path relative to pUnixFolder
566 * ppidl [O] The corresponding ITEMIDLIST. Release with SHFree/ILFree
568 * RETURNS
569 * Success: S_OK
570 * Failure: Error code, invalid params or out of memory
572 * NOTES
573 * pUnixFolder also carries the information if the path is expected to be unix or dos.
575 static HRESULT UNIXFS_path_to_pidl(UnixFolder *pUnixFolder, LPBC pbc, const WCHAR *path,
576 LPITEMIDLIST *ppidl) {
577 LPITEMIDLIST pidl;
578 int cPidlLen, cPathLen;
579 char *pSlash, *pNextSlash, szCompletePath[FILENAME_MAX], *pNextPathElement, *pszAPath;
580 WCHAR *pwszPath;
582 TRACE("pUnixFolder=%p, pbc=%p, path=%s, ppidl=%p\n", pUnixFolder, pbc, debugstr_w(path), ppidl);
584 if (!ppidl || !path)
585 return E_INVALIDARG;
587 /* Build an absolute path and let pNextPathElement point to the interesting
588 * relative sub-path. We need the absolute path to call 'stat', but the pidl
589 * will only contain the relative part.
591 if ((pUnixFolder->m_dwPathMode == PATHMODE_DOS) && (path[1] == ':'))
593 /* Absolute dos path. Convert to unix */
594 if (!UNIXFS_get_unix_path(path, szCompletePath))
595 return E_FAIL;
596 pNextPathElement = szCompletePath;
598 else if ((pUnixFolder->m_dwPathMode == PATHMODE_UNIX) && (path[0] == '/'))
600 /* Absolute unix path. Just convert to ANSI. */
601 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath, FILENAME_MAX, NULL, NULL);
602 pNextPathElement = szCompletePath;
604 else
606 /* Relative dos or unix path. Concat with this folder's path */
607 int cBasePathLen = strlen(pUnixFolder->m_pszPath);
608 memcpy(szCompletePath, pUnixFolder->m_pszPath, cBasePathLen);
609 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath + cBasePathLen,
610 FILENAME_MAX - cBasePathLen, NULL, NULL);
611 pNextPathElement = szCompletePath + cBasePathLen - 1;
613 /* If in dos mode, replace '\' with '/' */
614 if (pUnixFolder->m_dwPathMode == PATHMODE_DOS) {
615 char *pBackslash = strchr(pNextPathElement, '\\');
616 while (pBackslash) {
617 *pBackslash = '/';
618 pBackslash = strchr(pBackslash, '\\');
623 /* Special case for the root folder. */
624 if (!strcmp(szCompletePath, "/")) {
625 *ppidl = pidl = SHAlloc(sizeof(USHORT));
626 if (!pidl) return E_FAIL;
627 pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
628 return S_OK;
631 /* Remove trailing slash, if present */
632 cPathLen = strlen(szCompletePath);
633 if (szCompletePath[cPathLen-1] == '/')
634 szCompletePath[cPathLen-1] = '\0';
636 if ((szCompletePath[0] != '/') || (pNextPathElement[0] != '/')) {
637 ERR("szCompletePath: %s, pNextPathElment: %s\n", szCompletePath, pNextPathElement);
638 return E_FAIL;
641 /* At this point, we have an absolute unix path in szCompletePath
642 * and the relative portion of it in pNextPathElement. Both starting with '/'
643 * and _not_ terminated by a '/'. */
644 TRACE("complete path: %s, relative path: %s\n", szCompletePath, pNextPathElement);
646 /* Convert to CP_ACP and WCHAR */
647 if (!UNIXFS_shitemid_len_from_filename(pNextPathElement, &pszAPath, &pwszPath))
648 return E_FAIL;
650 /* Compute the length of the complete ITEMIDLIST */
651 cPidlLen = 0;
652 pSlash = pszAPath;
653 while (pSlash) {
654 pNextSlash = strchr(pSlash+1, '/');
655 cPidlLen += LEN_SHITEMID_FIXED_PART + /* Fixed part length plus potential alignment byte. */
656 (pNextSlash ? (pNextSlash - pSlash) & 0x1 : lstrlenA(pSlash) & 0x1);
657 pSlash = pNextSlash;
660 /* The USHORT is for the ITEMIDLIST terminator. The NUL terminators for the sub-path-strings
661 * are accounted for by the '/' separators, which are not stored in the SHITEMIDs. Above we
662 * have ensured that the number of '/'s exactly matches the number of sub-path-strings. */
663 cPidlLen += lstrlenA(pszAPath) + lstrlenW(pwszPath) * sizeof(WCHAR) + sizeof(USHORT);
665 SHFree(pszAPath);
666 SHFree(pwszPath);
668 *ppidl = pidl = SHAlloc(cPidlLen);
669 if (!pidl) return E_FAIL;
671 /* Concatenate the SHITEMIDs of the sub-directories. */
672 while (*pNextPathElement) {
673 pSlash = strchr(pNextPathElement+1, '/');
674 if (pSlash) *pSlash = '\0';
675 pNextPathElement = UNIXFS_build_shitemid(szCompletePath, pbc, pidl);
676 if (pSlash) *pSlash = '/';
678 if (!pNextPathElement) {
679 SHFree(*ppidl);
680 *ppidl = NULL;
681 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
683 pidl = ILGetNext(pidl);
685 pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
687 if ((char *)pidl-(char *)*ppidl+sizeof(USHORT) != cPidlLen) /* We've corrupted the heap :( */
688 ERR("Computed length of pidl incorrect. Please report.\n");
690 return S_OK;
693 /******************************************************************************
694 * UNIXFS_initialize_target_folder [Internal]
696 * Initialize the m_pszPath member of an UnixFolder, given an absolute unix
697 * base path and a relative ITEMIDLIST. Leave the m_pidlLocation member, which
698 * specifies the location in the shell namespace alone.
700 * PARAMS
701 * This [IO] The UnixFolder, whose target path is to be initialized
702 * szBasePath [I] The absolute base path
703 * pidlSubFolder [I] Relative part of the path, given as an ITEMIDLIST
704 * dwAttributes [I] Attributes to add to the Folders m_dwAttributes member
705 * (Used to pass the SFGAO_FILESYSTEM flag down the path)
706 * RETURNS
707 * Success: S_OK,
708 * Failure: E_FAIL
710 static HRESULT UNIXFS_initialize_target_folder(UnixFolder *This, const char *szBasePath,
711 LPCITEMIDLIST pidlSubFolder, DWORD dwAttributes)
713 LPCITEMIDLIST current = pidlSubFolder;
714 DWORD dwPathLen = strlen(szBasePath)+1;
715 char *pNextDir;
716 WCHAR *dos_name;
718 /* Determine the path's length bytes */
719 while (!_ILIsEmpty(current)) {
720 dwPathLen += UNIXFS_filename_from_shitemid(current, NULL) + 1; /* For the '/' */
721 current = ILGetNext(current);
724 /* Build the path and compute the attributes*/
725 This->m_dwAttributes =
726 dwAttributes|SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME;
727 This->m_pszPath = pNextDir = SHAlloc(dwPathLen);
728 if (!This->m_pszPath) {
729 WARN("SHAlloc failed!\n");
730 return E_FAIL;
732 current = pidlSubFolder;
733 strcpy(pNextDir, szBasePath);
734 pNextDir += strlen(szBasePath);
735 if (This->m_dwPathMode == PATHMODE_UNIX || IsEqualCLSID(&CLSID_MyDocuments, This->m_pCLSID))
736 This->m_dwAttributes |= SFGAO_FILESYSTEM;
737 while (!_ILIsEmpty(current)) {
738 pNextDir += UNIXFS_filename_from_shitemid(current, pNextDir);
739 *pNextDir++ = '/';
740 current = ILGetNext(current);
742 *pNextDir='\0';
744 if (!(This->m_dwAttributes & SFGAO_FILESYSTEM) &&
745 ((dos_name = wine_get_dos_file_name(This->m_pszPath))))
747 This->m_dwAttributes |= SFGAO_FILESYSTEM;
748 HeapFree( GetProcessHeap(), 0, dos_name );
751 return S_OK;
754 /******************************************************************************
755 * UNIXFS_copy [Internal]
757 * Copy pwszDosSrc to pwszDosDst.
759 * PARAMS
760 * pwszDosSrc [I] absolute path of the source
761 * pwszDosDst [I] absolute path of the destination
763 * RETURNS
764 * Success: S_OK,
765 * Failure: E_FAIL
767 static HRESULT UNIXFS_copy(LPCWSTR pwszDosSrc, LPCWSTR pwszDosDst)
769 SHFILEOPSTRUCTW op;
770 LPWSTR pwszSrc, pwszDst;
771 HRESULT res = E_OUTOFMEMORY;
772 UINT iSrcLen, iDstLen;
774 if (!pwszDosSrc || !pwszDosDst)
775 return E_FAIL;
777 iSrcLen = lstrlenW(pwszDosSrc);
778 iDstLen = lstrlenW(pwszDosDst);
779 pwszSrc = HeapAlloc(GetProcessHeap(), 0, (iSrcLen + 2) * sizeof(WCHAR));
780 pwszDst = HeapAlloc(GetProcessHeap(), 0, (iDstLen + 2) * sizeof(WCHAR));
782 if (pwszSrc && pwszDst) {
783 lstrcpyW(pwszSrc, pwszDosSrc);
784 lstrcpyW(pwszDst, pwszDosDst);
785 /* double null termination */
786 pwszSrc[iSrcLen + 1] = 0;
787 pwszDst[iDstLen + 1] = 0;
789 ZeroMemory(&op, sizeof(op));
790 op.hwnd = GetActiveWindow();
791 op.wFunc = FO_COPY;
792 op.pFrom = pwszSrc;
793 op.pTo = pwszDst;
794 op.fFlags = FOF_ALLOWUNDO;
795 if (!SHFileOperationW(&op))
797 WARN("SHFileOperationW failed\n");
798 res = E_FAIL;
800 else
801 res = S_OK;
804 HeapFree(GetProcessHeap(), 0, pwszSrc);
805 HeapFree(GetProcessHeap(), 0, pwszDst);
806 return res;
809 /******************************************************************************
810 * UnixFolder
812 * Class whose heap based instances represent unix filesystem directories.
815 static void UnixFolder_Destroy(UnixFolder *pUnixFolder) {
816 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
818 SHFree(pUnixFolder->m_pszPath);
819 ILFree(pUnixFolder->m_pidlLocation);
820 SHFree(pUnixFolder);
823 static HRESULT WINAPI UnixFolder_IShellFolder2_QueryInterface(IShellFolder2 *iface, REFIID riid,
824 void **ppv)
826 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
828 TRACE("(iface=%p, riid=%s, ppv=%p)\n", iface, shdebugstr_guid(riid), ppv);
830 if (!ppv) return E_INVALIDARG;
832 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IShellFolder, riid) ||
833 IsEqualIID(&IID_IShellFolder2, riid))
835 *ppv = STATIC_CAST(IShellFolder2, This);
836 } else if (IsEqualIID(&IID_IPersistFolder3, riid) || IsEqualIID(&IID_IPersistFolder2, riid) ||
837 IsEqualIID(&IID_IPersistFolder, riid) || IsEqualIID(&IID_IPersist, riid))
839 *ppv = STATIC_CAST(IPersistFolder3, This);
840 } else if (IsEqualIID(&IID_IPersistPropertyBag, riid)) {
841 *ppv = STATIC_CAST(IPersistPropertyBag, This);
842 } else if (IsEqualIID(&IID_ISFHelper, riid)) {
843 *ppv = STATIC_CAST(ISFHelper, This);
844 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
845 *ppv = STATIC_CAST(IDropTarget, This);
846 if (!cfShellIDList)
847 cfShellIDList = RegisterClipboardFormatW(CFSTR_SHELLIDLISTW);
848 } else {
849 *ppv = NULL;
850 TRACE("Unimplemented interface %s\n", shdebugstr_guid(riid));
851 return E_NOINTERFACE;
854 IUnknown_AddRef((IUnknown*)*ppv);
855 return S_OK;
858 static ULONG WINAPI UnixFolder_IShellFolder2_AddRef(IShellFolder2 *iface) {
859 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
861 TRACE("(iface=%p)\n", iface);
863 return InterlockedIncrement(&This->m_cRef);
866 static ULONG WINAPI UnixFolder_IShellFolder2_Release(IShellFolder2 *iface) {
867 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
868 ULONG cRef;
870 TRACE("(iface=%p)\n", iface);
872 cRef = InterlockedDecrement(&This->m_cRef);
874 if (!cRef)
875 UnixFolder_Destroy(This);
877 return cRef;
880 static HRESULT WINAPI UnixFolder_IShellFolder2_ParseDisplayName(IShellFolder2* iface, HWND hwndOwner,
881 LPBC pbc, LPOLESTR lpszDisplayName, ULONG* pchEaten, LPITEMIDLIST* ppidl,
882 ULONG* pdwAttributes)
884 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
885 HRESULT result;
887 TRACE("(iface=%p, hwndOwner=%p, pbc=%p, lpszDisplayName=%s, pchEaten=%p, ppidl=%p, "
888 "pdwAttributes=%p) stub\n", iface, hwndOwner, pbc, debugstr_w(lpszDisplayName),
889 pchEaten, ppidl, pdwAttributes);
891 result = UNIXFS_path_to_pidl(This, pbc, lpszDisplayName, ppidl);
892 if (SUCCEEDED(result) && pdwAttributes && *pdwAttributes)
894 IShellFolder *pParentSF;
895 LPCITEMIDLIST pidlLast;
896 LPITEMIDLIST pidlComplete = ILCombine(This->m_pidlLocation, *ppidl);
897 HRESULT hr;
899 hr = SHBindToParent(pidlComplete, &IID_IShellFolder, (LPVOID*)&pParentSF, &pidlLast);
900 if (FAILED(hr)) {
901 FIXME("SHBindToParent failed! hr = %08x\n", hr);
902 ILFree(pidlComplete);
903 return E_FAIL;
905 IShellFolder_GetAttributesOf(pParentSF, 1, &pidlLast, pdwAttributes);
906 IShellFolder_Release(pParentSF);
907 ILFree(pidlComplete);
910 if (FAILED(result)) TRACE("FAILED!\n");
911 return result;
914 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter);
916 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumObjects(IShellFolder2* iface, HWND hwndOwner,
917 SHCONTF grfFlags, IEnumIDList** ppEnumIDList)
919 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
920 IUnknown *newIterator;
921 HRESULT hr;
923 TRACE("(iface=%p, hwndOwner=%p, grfFlags=%08x, ppEnumIDList=%p)\n",
924 iface, hwndOwner, grfFlags, ppEnumIDList);
926 if (!This->m_pszPath) {
927 WARN("EnumObjects called on uninitialized UnixFolder-object!\n");
928 return E_UNEXPECTED;
931 newIterator = UnixSubFolderIterator_Constructor(This, grfFlags);
932 hr = IUnknown_QueryInterface(newIterator, &IID_IEnumIDList, (void**)ppEnumIDList);
933 IUnknown_Release(newIterator);
935 return hr;
938 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, const CLSID *pCLSID);
940 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToObject(IShellFolder2* iface, LPCITEMIDLIST pidl,
941 LPBC pbcReserved, REFIID riid, void** ppvOut)
943 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
944 IPersistFolder3 *persistFolder;
945 HRESULT hr;
946 const CLSID *clsidChild;
948 TRACE("(iface=%p, pidl=%p, pbcReserver=%p, riid=%p, ppvOut=%p)\n",
949 iface, pidl, pbcReserved, riid, ppvOut);
951 if (_ILIsEmpty(pidl))
952 return E_INVALIDARG;
954 /* Don't bind to files */
955 if (_ILIsValue(ILFindLastID(pidl)))
956 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
958 if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
959 /* Children of FolderShortcuts are ShellFSFolders on Windows.
960 * Unixfs' counterpart is UnixDosFolder. */
961 clsidChild = &CLSID_UnixDosFolder;
962 } else {
963 clsidChild = This->m_pCLSID;
966 hr = CreateUnixFolder(NULL, &IID_IPersistFolder3, (void**)&persistFolder, clsidChild);
967 if (FAILED(hr)) return hr;
968 hr = IPersistFolder_QueryInterface(persistFolder, riid, ppvOut);
970 if (SUCCEEDED(hr)) {
971 UnixFolder *subfolder = ADJUST_THIS(UnixFolder, IPersistFolder3, persistFolder);
972 subfolder->m_pidlLocation = ILCombine(This->m_pidlLocation, pidl);
973 hr = UNIXFS_initialize_target_folder(subfolder, This->m_pszPath, pidl,
974 This->m_dwAttributes & SFGAO_FILESYSTEM);
977 IPersistFolder3_Release(persistFolder);
979 return hr;
982 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToStorage(IShellFolder2* This, LPCITEMIDLIST pidl,
983 LPBC pbcReserved, REFIID riid, void** ppvObj)
985 FIXME("stub\n");
986 return E_NOTIMPL;
989 static HRESULT WINAPI UnixFolder_IShellFolder2_CompareIDs(IShellFolder2* iface, LPARAM lParam,
990 LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
992 BOOL isEmpty1, isEmpty2;
993 HRESULT hr = E_FAIL;
994 LPITEMIDLIST firstpidl;
995 IShellFolder2 *psf;
996 int compare;
998 TRACE("(iface=%p, lParam=%ld, pidl1=%p, pidl2=%p)\n", iface, lParam, pidl1, pidl2);
1000 isEmpty1 = _ILIsEmpty(pidl1);
1001 isEmpty2 = _ILIsEmpty(pidl2);
1003 if (isEmpty1 && isEmpty2)
1004 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
1005 else if (isEmpty1)
1006 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1007 else if (isEmpty2)
1008 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1010 if (_ILIsFolder(pidl1) && !_ILIsFolder(pidl2))
1011 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1012 if (!_ILIsFolder(pidl1) && _ILIsFolder(pidl2))
1013 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1015 compare = CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
1016 _ILGetTextPointer(pidl1), -1,
1017 _ILGetTextPointer(pidl2), -1);
1019 if ((compare == CSTR_LESS_THAN) || (compare == CSTR_GREATER_THAN))
1020 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)((compare == CSTR_LESS_THAN)?-1:1));
1022 if (pidl1->mkid.cb < pidl2->mkid.cb)
1023 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1024 else if (pidl1->mkid.cb > pidl2->mkid.cb)
1025 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1027 firstpidl = ILCloneFirst(pidl1);
1028 pidl1 = ILGetNext(pidl1);
1029 pidl2 = ILGetNext(pidl2);
1031 isEmpty1 = _ILIsEmpty(pidl1);
1032 isEmpty2 = _ILIsEmpty(pidl2);
1034 if (isEmpty1 && isEmpty2)
1035 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
1036 else if (isEmpty1)
1037 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1038 else if (isEmpty2)
1039 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1040 else if (SUCCEEDED(IShellFolder2_BindToObject(iface, firstpidl, NULL, &IID_IShellFolder, (void**)&psf))) {
1041 hr = IShellFolder_CompareIDs(psf, lParam, pidl1, pidl2);
1042 IShellFolder2_Release(psf);
1045 ILFree(firstpidl);
1046 return hr;
1049 static HRESULT WINAPI UnixFolder_IShellFolder2_CreateViewObject(IShellFolder2* iface, HWND hwndOwner,
1050 REFIID riid, void** ppv)
1052 HRESULT hr = E_INVALIDARG;
1054 TRACE("(iface=%p, hwndOwner=%p, riid=%p, ppv=%p) stub\n", iface, hwndOwner, riid, ppv);
1056 if (!ppv) return E_INVALIDARG;
1057 *ppv = NULL;
1059 if (IsEqualIID(&IID_IShellView, riid)) {
1060 LPSHELLVIEW pShellView;
1062 pShellView = IShellView_Constructor((IShellFolder*)iface);
1063 if (pShellView) {
1064 hr = IShellView_QueryInterface(pShellView, riid, ppv);
1065 IShellView_Release(pShellView);
1067 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
1068 hr = IShellFolder2_QueryInterface(iface, &IID_IDropTarget, ppv);
1071 return hr;
1074 static HRESULT WINAPI UnixFolder_IShellFolder2_GetAttributesOf(IShellFolder2* iface, UINT cidl,
1075 LPCITEMIDLIST* apidl, SFGAOF* rgfInOut)
1077 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1078 HRESULT hr = S_OK;
1080 TRACE("(iface=%p, cidl=%u, apidl=%p, rgfInOut=%p)\n", iface, cidl, apidl, rgfInOut);
1082 if (!rgfInOut || (cidl && !apidl))
1083 return E_INVALIDARG;
1085 if (cidl == 0) {
1086 *rgfInOut &= This->m_dwAttributes;
1087 } else {
1088 char szAbsolutePath[FILENAME_MAX], *pszRelativePath;
1089 UINT i;
1091 *rgfInOut = SFGAO_CANCOPY|SFGAO_CANMOVE|SFGAO_CANLINK|SFGAO_CANRENAME|SFGAO_CANDELETE|
1092 SFGAO_HASPROPSHEET|SFGAO_DROPTARGET|SFGAO_FILESYSTEM;
1093 lstrcpyA(szAbsolutePath, This->m_pszPath);
1094 pszRelativePath = szAbsolutePath + lstrlenA(szAbsolutePath);
1095 for (i=0; i<cidl; i++) {
1096 if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
1097 WCHAR *dos_name;
1098 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelativePath))
1099 return E_INVALIDARG;
1100 if (!(dos_name = wine_get_dos_file_name( szAbsolutePath )))
1101 *rgfInOut &= ~SFGAO_FILESYSTEM;
1102 else
1103 HeapFree( GetProcessHeap(), 0, dos_name );
1105 if (_ILIsFolder(apidl[i]))
1106 *rgfInOut |= SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR;
1110 return hr;
1113 static HRESULT WINAPI UnixFolder_IShellFolder2_GetUIObjectOf(IShellFolder2* iface, HWND hwndOwner,
1114 UINT cidl, LPCITEMIDLIST* apidl, REFIID riid, UINT* prgfInOut, void** ppvOut)
1116 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1117 UINT i;
1118 HRESULT hr;
1120 TRACE("(iface=%p, hwndOwner=%p, cidl=%d, apidl=%p, riid=%s, prgfInOut=%p, ppv=%p)\n",
1121 iface, hwndOwner, cidl, apidl, debugstr_guid(riid), prgfInOut, ppvOut);
1123 if (!cidl || !apidl || !riid || !ppvOut)
1124 return E_INVALIDARG;
1126 for (i=0; i<cidl; i++)
1127 if (!apidl[i])
1128 return E_INVALIDARG;
1130 if(cidl == 1) {
1131 hr = SHELL32_CreateExtensionUIObject(iface, *apidl, riid, ppvOut);
1132 if(hr != S_FALSE)
1133 return hr;
1136 if (IsEqualIID(&IID_IContextMenu, riid)) {
1137 *ppvOut = ISvItemCm_Constructor((IShellFolder*)iface, This->m_pidlLocation, apidl, cidl);
1138 return S_OK;
1139 } else if (IsEqualIID(&IID_IDataObject, riid)) {
1140 *ppvOut = IDataObject_Constructor(hwndOwner, This->m_pidlLocation, apidl, cidl);
1141 return S_OK;
1142 } else if (IsEqualIID(&IID_IExtractIconA, riid)) {
1143 LPITEMIDLIST pidl;
1144 if (cidl != 1) return E_INVALIDARG;
1145 pidl = ILCombine(This->m_pidlLocation, apidl[0]);
1146 *ppvOut = IExtractIconA_Constructor(pidl);
1147 SHFree(pidl);
1148 return S_OK;
1149 } else if (IsEqualIID(&IID_IExtractIconW, riid)) {
1150 LPITEMIDLIST pidl;
1151 if (cidl != 1) return E_INVALIDARG;
1152 pidl = ILCombine(This->m_pidlLocation, apidl[0]);
1153 *ppvOut = IExtractIconW_Constructor(pidl);
1154 SHFree(pidl);
1155 return S_OK;
1156 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
1157 if (cidl != 1) return E_INVALIDARG;
1158 return IShellFolder2_BindToObject(iface, apidl[0], NULL, &IID_IDropTarget, ppvOut);
1159 } else if (IsEqualIID(&IID_IShellLinkW, riid)) {
1160 FIXME("IShellLinkW\n");
1161 return E_FAIL;
1162 } else if (IsEqualIID(&IID_IShellLinkA, riid)) {
1163 FIXME("IShellLinkA\n");
1164 return E_FAIL;
1165 } else {
1166 FIXME("Unknown interface %s in GetUIObjectOf\n", debugstr_guid(riid));
1167 return E_NOINTERFACE;
1171 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDisplayNameOf(IShellFolder2* iface,
1172 LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET* lpName)
1174 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1175 SHITEMID emptyIDL = { 0, { 0 } };
1176 HRESULT hr = S_OK;
1178 TRACE("(iface=%p, pidl=%p, uFlags=%x, lpName=%p)\n", iface, pidl, uFlags, lpName);
1180 if ((GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) &&
1181 (GET_SHGDN_RELATION(uFlags) != SHGDN_INFOLDER))
1183 if (_ILIsEmpty(pidl)) {
1184 lpName->uType = STRRET_WSTR;
1185 if (This->m_dwPathMode == PATHMODE_UNIX) {
1186 UINT len = MultiByteToWideChar(CP_UNIXCP, 0, This->m_pszPath, -1, NULL, 0);
1187 lpName->u.pOleStr = SHAlloc(len * sizeof(WCHAR));
1188 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1189 MultiByteToWideChar(CP_UNIXCP, 0, This->m_pszPath, -1, lpName->u.pOleStr, len);
1190 } else {
1191 LPWSTR pwszDosFileName = wine_get_dos_file_name(This->m_pszPath);
1192 if (!pwszDosFileName) return HRESULT_FROM_WIN32(GetLastError());
1193 lpName->u.pOleStr = SHAlloc((lstrlenW(pwszDosFileName) + 1) * sizeof(WCHAR));
1194 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1195 lstrcpyW(lpName->u.pOleStr, pwszDosFileName);
1196 PathRemoveBackslashW(lpName->u.pOleStr);
1197 HeapFree(GetProcessHeap(), 0, pwszDosFileName);
1199 } else if (_ILIsValue(pidl)) {
1200 STRRET str;
1201 PWSTR path, file;
1203 /* We are looking for the complete path to a file */
1205 /* Get the complete path for the current folder object */
1206 hr = IShellFolder_GetDisplayNameOf(iface, (LPITEMIDLIST)&emptyIDL, uFlags, &str);
1207 if (SUCCEEDED(hr)) {
1208 hr = StrRetToStrW(&str, NULL, &path);
1209 if (SUCCEEDED(hr)) {
1211 /* Get the child filename */
1212 hr = IShellFolder_GetDisplayNameOf(iface, pidl, SHGDN_FORPARSING | SHGDN_INFOLDER, &str);
1213 if (SUCCEEDED(hr)) {
1214 hr = StrRetToStrW(&str, NULL, &file);
1215 if (SUCCEEDED(hr)) {
1216 static const WCHAR slashW = '/';
1217 UINT len_path = strlenW(path), len_file = strlenW(file);
1219 /* Now, combine them */
1220 lpName->uType = STRRET_WSTR;
1221 lpName->u.pOleStr = SHAlloc( (len_path + len_file + 2)*sizeof(WCHAR) );
1222 lstrcpyW(lpName->u.pOleStr, path);
1223 if (This->m_dwPathMode == PATHMODE_UNIX &&
1224 lpName->u.pOleStr[len_path-1] != slashW) {
1225 lpName->u.pOleStr[len_path] = slashW;
1226 lpName->u.pOleStr[len_path+1] = '\0';
1227 } else
1228 PathAddBackslashW(lpName->u.pOleStr);
1229 lstrcatW(lpName->u.pOleStr, file);
1231 CoTaskMemFree(file);
1232 } else
1233 WARN("Failed to convert strret (file)\n");
1235 CoTaskMemFree(path);
1236 } else
1237 WARN("Failed to convert strret (path)\n");
1239 } else {
1240 IShellFolder *pSubFolder;
1242 hr = IShellFolder_BindToObject(iface, pidl, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1243 if (SUCCEEDED(hr)) {
1244 hr = IShellFolder_GetDisplayNameOf(pSubFolder, (LPITEMIDLIST)&emptyIDL, uFlags, lpName);
1245 IShellFolder_Release(pSubFolder);
1246 } else if (FAILED(hr) && !_ILIsPidlSimple(pidl)) {
1247 LPITEMIDLIST pidl_parent = ILClone(pidl);
1248 LPITEMIDLIST pidl_child = ILFindLastID(pidl);
1250 /* Might be a file, try binding to its parent */
1251 ILRemoveLastID(pidl_parent);
1252 hr = IShellFolder_BindToObject(iface, pidl_parent, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1253 if (SUCCEEDED(hr)) {
1254 hr = IShellFolder_GetDisplayNameOf(pSubFolder, pidl_child, uFlags, lpName);
1255 IShellFolder_Release(pSubFolder);
1257 ILFree(pidl_parent);
1260 } else {
1261 WCHAR wszFileName[MAX_PATH];
1262 if (!_ILSimpleGetTextW(pidl, wszFileName, MAX_PATH)) return E_INVALIDARG;
1263 lpName->uType = STRRET_WSTR;
1264 lpName->u.pOleStr = SHAlloc((lstrlenW(wszFileName)+1)*sizeof(WCHAR));
1265 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1266 lstrcpyW(lpName->u.pOleStr, wszFileName);
1267 if (!(GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) && This->m_dwPathMode == PATHMODE_DOS &&
1268 !_ILIsFolder(pidl) && wszFileName[0] != '.' && SHELL_FS_HideExtension(wszFileName))
1270 PathRemoveExtensionW(lpName->u.pOleStr);
1274 TRACE("--> %s\n", debugstr_w(lpName->u.pOleStr));
1276 return hr;
1279 static HRESULT WINAPI UnixFolder_IShellFolder2_SetNameOf(IShellFolder2* iface, HWND hwnd,
1280 LPCITEMIDLIST pidl, LPCOLESTR lpcwszName, SHGDNF uFlags, LPITEMIDLIST* ppidlOut)
1282 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1284 static const WCHAR awcInvalidChars[] = { '\\', '/', ':', '*', '?', '"', '<', '>', '|' };
1285 char szSrc[FILENAME_MAX], szDest[FILENAME_MAX];
1286 WCHAR wszSrcRelative[MAX_PATH], *pwszExt = NULL;
1287 unsigned int i;
1288 int cBasePathLen = lstrlenA(This->m_pszPath), cNameLen;
1289 struct stat statDest;
1290 LPITEMIDLIST pidlSrc, pidlDest, pidlRelativeDest;
1291 LPOLESTR lpwszName;
1292 HRESULT hr;
1294 TRACE("(iface=%p, hwnd=%p, pidl=%p, lpcwszName=%s, uFlags=0x%08x, ppidlOut=%p)\n",
1295 iface, hwnd, pidl, debugstr_w(lpcwszName), uFlags, ppidlOut);
1297 /* prepare to fail */
1298 if (ppidlOut)
1299 *ppidlOut = NULL;
1301 /* pidl has to contain a single non-empty SHITEMID */
1302 if (_ILIsDesktop(pidl) || !_ILIsPidlSimple(pidl) || !_ILGetTextPointer(pidl))
1303 return E_INVALIDARG;
1305 /* check for invalid characters in lpcwszName. */
1306 for (i=0; i < sizeof(awcInvalidChars)/sizeof(*awcInvalidChars); i++)
1307 if (StrChrW(lpcwszName, awcInvalidChars[i]))
1308 return HRESULT_FROM_WIN32(ERROR_CANCELLED);
1310 /* build source path */
1311 memcpy(szSrc, This->m_pszPath, cBasePathLen);
1312 UNIXFS_filename_from_shitemid(pidl, szSrc + cBasePathLen);
1314 /* build destination path */
1315 memcpy(szDest, This->m_pszPath, cBasePathLen);
1316 WideCharToMultiByte(CP_UNIXCP, 0, lpcwszName, -1, szDest+cBasePathLen,
1317 FILENAME_MAX-cBasePathLen, NULL, NULL);
1319 /* If the filename's extension is hidden to the user, we have to append it. */
1320 if (!(uFlags & SHGDN_FORPARSING) &&
1321 _ILSimpleGetTextW(pidl, wszSrcRelative, MAX_PATH) &&
1322 SHELL_FS_HideExtension(wszSrcRelative))
1324 int cLenDest = strlen(szDest);
1325 pwszExt = PathFindExtensionW(wszSrcRelative);
1326 WideCharToMultiByte(CP_UNIXCP, 0, pwszExt, -1, szDest + cLenDest,
1327 FILENAME_MAX - cLenDest, NULL, NULL);
1330 TRACE("src=%s dest=%s\n", szSrc, szDest);
1332 /* Fail, if destination does already exist */
1333 if (!stat(szDest, &statDest))
1334 return E_FAIL;
1336 /* Rename the file */
1337 if (rename(szSrc, szDest))
1338 return E_FAIL;
1340 /* Build a pidl for the path of the renamed file */
1341 cNameLen = lstrlenW(lpcwszName) + 1;
1342 if(pwszExt)
1343 cNameLen += lstrlenW(pwszExt);
1344 lpwszName = SHAlloc(cNameLen*sizeof(WCHAR)); /* due to const correctness. */
1345 lstrcpyW(lpwszName, lpcwszName);
1346 if(pwszExt)
1347 lstrcatW(lpwszName, pwszExt);
1349 hr = IShellFolder2_ParseDisplayName(iface, NULL, NULL, lpwszName, NULL, &pidlRelativeDest, NULL);
1350 SHFree(lpwszName);
1351 if (FAILED(hr)) {
1352 rename(szDest, szSrc); /* Undo the renaming */
1353 return E_FAIL;
1355 pidlDest = ILCombine(This->m_pidlLocation, pidlRelativeDest);
1356 ILFree(pidlRelativeDest);
1357 pidlSrc = ILCombine(This->m_pidlLocation, pidl);
1359 /* Inform the shell */
1360 if (_ILIsFolder(ILFindLastID(pidlDest)))
1361 SHChangeNotify(SHCNE_RENAMEFOLDER, SHCNF_IDLIST, pidlSrc, pidlDest);
1362 else
1363 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_IDLIST, pidlSrc, pidlDest);
1365 if (ppidlOut)
1366 *ppidlOut = ILClone(ILFindLastID(pidlDest));
1368 ILFree(pidlSrc);
1369 ILFree(pidlDest);
1371 return S_OK;
1374 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumSearches(IShellFolder2* iface,
1375 IEnumExtraSearch **ppEnum)
1377 FIXME("stub\n");
1378 return E_NOTIMPL;
1381 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumn(IShellFolder2* iface,
1382 DWORD dwReserved, ULONG *pSort, ULONG *pDisplay)
1384 TRACE("(iface=%p,dwReserved=%x,pSort=%p,pDisplay=%p)\n", iface, dwReserved, pSort, pDisplay);
1386 if (pSort)
1387 *pSort = 0;
1388 if (pDisplay)
1389 *pDisplay = 0;
1391 return S_OK;
1394 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumnState(IShellFolder2* iface,
1395 UINT iColumn, SHCOLSTATEF *pcsFlags)
1397 FIXME("stub\n");
1398 return E_NOTIMPL;
1401 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultSearchGUID(IShellFolder2* iface,
1402 GUID *pguid)
1404 FIXME("stub\n");
1405 return E_NOTIMPL;
1408 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsEx(IShellFolder2* iface,
1409 LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
1411 FIXME("stub\n");
1412 return E_NOTIMPL;
1415 #define SHELLVIEWCOLUMNS 7
1417 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsOf(IShellFolder2* iface,
1418 LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
1420 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1421 HRESULT hr = E_FAIL;
1422 struct passwd *pPasswd;
1423 struct group *pGroup;
1424 struct stat statItem;
1426 static const shvheader unixfs_header[SHELLVIEWCOLUMNS] = {
1427 {IDS_SHV_COLUMN1, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
1428 {IDS_SHV_COLUMN2, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1429 {IDS_SHV_COLUMN3, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1430 {IDS_SHV_COLUMN4, SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12},
1431 {IDS_SHV_COLUMN5, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 9},
1432 {IDS_SHV_COLUMN10, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
1433 {IDS_SHV_COLUMN11, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7}
1436 TRACE("(iface=%p, pidl=%p, iColumn=%d, psd=%p) stub\n", iface, pidl, iColumn, psd);
1438 if (!psd || iColumn >= SHELLVIEWCOLUMNS)
1439 return E_INVALIDARG;
1441 if (!pidl)
1442 return SHELL32_GetColumnDetails(unixfs_header, iColumn, psd);
1444 if (iColumn == 4 || iColumn == 5 || iColumn == 6) {
1445 char szPath[FILENAME_MAX];
1446 strcpy(szPath, This->m_pszPath);
1447 if (!UNIXFS_filename_from_shitemid(pidl, szPath + strlen(szPath)))
1448 return E_INVALIDARG;
1449 if (stat(szPath, &statItem))
1450 return E_INVALIDARG;
1453 psd->str.u.cStr[0] = '\0';
1454 psd->str.uType = STRRET_CSTR;
1456 switch (iColumn) {
1457 case 0:
1458 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL|SHGDN_INFOLDER, &psd->str);
1459 break;
1460 case 1:
1461 _ILGetFileSize(pidl, psd->str.u.cStr, MAX_PATH);
1462 break;
1463 case 2:
1464 _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
1465 break;
1466 case 3:
1467 _ILGetFileDate(pidl, psd->str.u.cStr, MAX_PATH);
1468 break;
1469 case 4:
1470 psd->str.u.cStr[0] = S_ISDIR(statItem.st_mode) ? 'd' : '-';
1471 psd->str.u.cStr[1] = (statItem.st_mode & S_IRUSR) ? 'r' : '-';
1472 psd->str.u.cStr[2] = (statItem.st_mode & S_IWUSR) ? 'w' : '-';
1473 psd->str.u.cStr[3] = (statItem.st_mode & S_IXUSR) ? 'x' : '-';
1474 psd->str.u.cStr[4] = (statItem.st_mode & S_IRGRP) ? 'r' : '-';
1475 psd->str.u.cStr[5] = (statItem.st_mode & S_IWGRP) ? 'w' : '-';
1476 psd->str.u.cStr[6] = (statItem.st_mode & S_IXGRP) ? 'x' : '-';
1477 psd->str.u.cStr[7] = (statItem.st_mode & S_IROTH) ? 'r' : '-';
1478 psd->str.u.cStr[8] = (statItem.st_mode & S_IWOTH) ? 'w' : '-';
1479 psd->str.u.cStr[9] = (statItem.st_mode & S_IXOTH) ? 'x' : '-';
1480 psd->str.u.cStr[10] = '\0';
1481 break;
1482 case 5:
1483 pPasswd = getpwuid(statItem.st_uid);
1484 if (pPasswd) strcpy(psd->str.u.cStr, pPasswd->pw_name);
1485 break;
1486 case 6:
1487 pGroup = getgrgid(statItem.st_gid);
1488 if (pGroup) strcpy(psd->str.u.cStr, pGroup->gr_name);
1489 break;
1492 return hr;
1495 static HRESULT WINAPI UnixFolder_IShellFolder2_MapColumnToSCID(IShellFolder2* iface, UINT iColumn,
1496 SHCOLUMNID *pscid)
1498 FIXME("stub\n");
1499 return E_NOTIMPL;
1502 /* VTable for UnixFolder's IShellFolder2 interface.
1504 static const IShellFolder2Vtbl UnixFolder_IShellFolder2_Vtbl = {
1505 UnixFolder_IShellFolder2_QueryInterface,
1506 UnixFolder_IShellFolder2_AddRef,
1507 UnixFolder_IShellFolder2_Release,
1508 UnixFolder_IShellFolder2_ParseDisplayName,
1509 UnixFolder_IShellFolder2_EnumObjects,
1510 UnixFolder_IShellFolder2_BindToObject,
1511 UnixFolder_IShellFolder2_BindToStorage,
1512 UnixFolder_IShellFolder2_CompareIDs,
1513 UnixFolder_IShellFolder2_CreateViewObject,
1514 UnixFolder_IShellFolder2_GetAttributesOf,
1515 UnixFolder_IShellFolder2_GetUIObjectOf,
1516 UnixFolder_IShellFolder2_GetDisplayNameOf,
1517 UnixFolder_IShellFolder2_SetNameOf,
1518 UnixFolder_IShellFolder2_GetDefaultSearchGUID,
1519 UnixFolder_IShellFolder2_EnumSearches,
1520 UnixFolder_IShellFolder2_GetDefaultColumn,
1521 UnixFolder_IShellFolder2_GetDefaultColumnState,
1522 UnixFolder_IShellFolder2_GetDetailsEx,
1523 UnixFolder_IShellFolder2_GetDetailsOf,
1524 UnixFolder_IShellFolder2_MapColumnToSCID
1527 static HRESULT WINAPI UnixFolder_IPersistFolder3_QueryInterface(IPersistFolder3* iface, REFIID riid,
1528 void** ppvObject)
1530 return UnixFolder_IShellFolder2_QueryInterface(
1531 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistFolder3, iface)), riid, ppvObject);
1534 static ULONG WINAPI UnixFolder_IPersistFolder3_AddRef(IPersistFolder3* iface)
1536 return UnixFolder_IShellFolder2_AddRef(
1537 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistFolder3, iface)));
1540 static ULONG WINAPI UnixFolder_IPersistFolder3_Release(IPersistFolder3* iface)
1542 return UnixFolder_IShellFolder2_Release(
1543 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistFolder3, iface)));
1546 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetClassID(IPersistFolder3* iface, CLSID* pClassID)
1548 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1550 TRACE("(iface=%p, pClassId=%p)\n", iface, pClassID);
1552 if (!pClassID)
1553 return E_INVALIDARG;
1555 *pClassID = *This->m_pCLSID;
1556 return S_OK;
1559 static HRESULT WINAPI UnixFolder_IPersistFolder3_Initialize(IPersistFolder3* iface, LPCITEMIDLIST pidl)
1561 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1562 LPCITEMIDLIST current = pidl;
1563 char szBasePath[FILENAME_MAX] = "/";
1565 TRACE("(iface=%p, pidl=%p)\n", iface, pidl);
1567 /* Find the UnixFolderClass root */
1568 while (current->mkid.cb) {
1569 if ((_ILIsDrive(current) && IsEqualCLSID(This->m_pCLSID, &CLSID_ShellFSFolder)) ||
1570 (_ILIsSpecialFolder(current) && IsEqualCLSID(This->m_pCLSID, _ILGetGUIDPointer(current))))
1572 break;
1574 current = ILGetNext(current);
1577 if (current->mkid.cb) {
1578 if (_ILIsDrive(current)) {
1579 WCHAR wszDrive[4] = { '?', ':', '\\', 0 };
1580 wszDrive[0] = (WCHAR)*_ILGetTextPointer(current);
1581 if (!UNIXFS_get_unix_path(wszDrive, szBasePath))
1582 return E_FAIL;
1583 } else if (IsEqualIID(&CLSID_MyDocuments, _ILGetGUIDPointer(current))) {
1584 WCHAR wszMyDocumentsPath[MAX_PATH];
1585 if (!SHGetSpecialFolderPathW(0, wszMyDocumentsPath, CSIDL_PERSONAL, FALSE))
1586 return E_FAIL;
1587 PathAddBackslashW(wszMyDocumentsPath);
1588 if (!UNIXFS_get_unix_path(wszMyDocumentsPath, szBasePath))
1589 return E_FAIL;
1591 current = ILGetNext(current);
1592 } else if (_ILIsDesktop(pidl) || _ILIsValue(pidl) || _ILIsFolder(pidl)) {
1593 /* Path rooted at Desktop */
1594 WCHAR wszDesktopPath[MAX_PATH];
1595 if (!SHGetSpecialFolderPathW(0, wszDesktopPath, CSIDL_DESKTOPDIRECTORY, FALSE))
1596 return E_FAIL;
1597 PathAddBackslashW(wszDesktopPath);
1598 if (!UNIXFS_get_unix_path(wszDesktopPath, szBasePath))
1599 return E_FAIL;
1600 current = pidl;
1601 } else if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
1602 /* FolderShortcuts' Initialize method only sets the ITEMIDLIST, which
1603 * specifies the location in the shell namespace, but leaves the
1604 * target folder (m_pszPath) alone. See unit tests in tests/shlfolder.c */
1605 This->m_pidlLocation = ILClone(pidl);
1606 return S_OK;
1607 } else {
1608 ERR("Unknown pidl type!\n");
1609 pdump(pidl);
1610 return E_INVALIDARG;
1613 This->m_pidlLocation = ILClone(pidl);
1614 return UNIXFS_initialize_target_folder(This, szBasePath, current, 0);
1617 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetCurFolder(IPersistFolder3* iface, LPITEMIDLIST* ppidl)
1619 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1621 TRACE ("(iface=%p, ppidl=%p)\n", iface, ppidl);
1623 if (!ppidl)
1624 return E_POINTER;
1625 *ppidl = ILClone (This->m_pidlLocation);
1626 return S_OK;
1629 static HRESULT WINAPI UnixFolder_IPersistFolder3_InitializeEx(IPersistFolder3 *iface, IBindCtx *pbc,
1630 LPCITEMIDLIST pidlRoot, const PERSIST_FOLDER_TARGET_INFO *ppfti)
1632 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1633 WCHAR wszTargetDosPath[MAX_PATH];
1634 char szTargetPath[FILENAME_MAX] = "";
1636 TRACE("(iface=%p, pbc=%p, pidlRoot=%p, ppfti=%p)\n", iface, pbc, pidlRoot, ppfti);
1638 /* If no PERSIST_FOLDER_TARGET_INFO is given InitializeEx is equivalent to Initialize. */
1639 if (!ppfti)
1640 return IPersistFolder3_Initialize(iface, pidlRoot);
1642 if (ppfti->csidl != -1) {
1643 if (FAILED(SHGetFolderPathW(0, ppfti->csidl, NULL, 0, wszTargetDosPath)) ||
1644 !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1646 return E_FAIL;
1648 } else if (*ppfti->szTargetParsingName) {
1649 lstrcpyW(wszTargetDosPath, ppfti->szTargetParsingName);
1650 PathAddBackslashW(wszTargetDosPath);
1651 if (!UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath)) {
1652 return E_FAIL;
1654 } else if (ppfti->pidlTargetFolder) {
1655 if (!SHGetPathFromIDListW(ppfti->pidlTargetFolder, wszTargetDosPath) ||
1656 !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1658 return E_FAIL;
1660 } else {
1661 return E_FAIL;
1664 This->m_pszPath = SHAlloc(lstrlenA(szTargetPath)+1);
1665 if (!This->m_pszPath)
1666 return E_FAIL;
1667 lstrcpyA(This->m_pszPath, szTargetPath);
1668 This->m_pidlLocation = ILClone(pidlRoot);
1669 This->m_dwAttributes = (ppfti->dwAttributes != -1) ? ppfti->dwAttributes :
1670 (SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME|SFGAO_FILESYSTEM);
1672 return S_OK;
1675 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetFolderTargetInfo(IPersistFolder3 *iface,
1676 PERSIST_FOLDER_TARGET_INFO *ppfti)
1678 FIXME("(iface=%p, ppfti=%p) stub\n", iface, ppfti);
1679 return E_NOTIMPL;
1682 /* VTable for UnixFolder's IPersistFolder interface.
1684 static const IPersistFolder3Vtbl UnixFolder_IPersistFolder3_Vtbl = {
1685 UnixFolder_IPersistFolder3_QueryInterface,
1686 UnixFolder_IPersistFolder3_AddRef,
1687 UnixFolder_IPersistFolder3_Release,
1688 UnixFolder_IPersistFolder3_GetClassID,
1689 UnixFolder_IPersistFolder3_Initialize,
1690 UnixFolder_IPersistFolder3_GetCurFolder,
1691 UnixFolder_IPersistFolder3_InitializeEx,
1692 UnixFolder_IPersistFolder3_GetFolderTargetInfo
1695 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_QueryInterface(IPersistPropertyBag* iface,
1696 REFIID riid, void** ppv)
1698 return UnixFolder_IShellFolder2_QueryInterface(
1699 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)), riid, ppv);
1702 static ULONG WINAPI UnixFolder_IPersistPropertyBag_AddRef(IPersistPropertyBag* iface)
1704 return UnixFolder_IShellFolder2_AddRef(
1705 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)));
1708 static ULONG WINAPI UnixFolder_IPersistPropertyBag_Release(IPersistPropertyBag* iface)
1710 return UnixFolder_IShellFolder2_Release(
1711 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)));
1714 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_GetClassID(IPersistPropertyBag* iface,
1715 CLSID* pClassID)
1717 return UnixFolder_IPersistFolder3_GetClassID(
1718 STATIC_CAST(IPersistFolder3, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)), pClassID);
1721 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_InitNew(IPersistPropertyBag* iface)
1723 FIXME("() stub\n");
1724 return E_NOTIMPL;
1727 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_Load(IPersistPropertyBag *iface,
1728 IPropertyBag *pPropertyBag, IErrorLog *pErrorLog)
1730 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface);
1731 static const WCHAR wszTarget[] = { 'T','a','r','g','e','t', 0 }, wszNull[] = { 0 };
1732 PERSIST_FOLDER_TARGET_INFO pftiTarget;
1733 VARIANT var;
1734 HRESULT hr;
1736 TRACE("(iface=%p, pPropertyBag=%p, pErrorLog=%p)\n", iface, pPropertyBag, pErrorLog);
1738 if (!pPropertyBag)
1739 return E_POINTER;
1741 /* Get 'Target' property from the property bag. */
1742 V_VT(&var) = VT_BSTR;
1743 hr = IPropertyBag_Read(pPropertyBag, wszTarget, &var, NULL);
1744 if (FAILED(hr))
1745 return E_FAIL;
1746 lstrcpyW(pftiTarget.szTargetParsingName, V_BSTR(&var));
1747 SysFreeString(V_BSTR(&var));
1749 pftiTarget.pidlTargetFolder = NULL;
1750 lstrcpyW(pftiTarget.szNetworkProvider, wszNull);
1751 pftiTarget.dwAttributes = -1;
1752 pftiTarget.csidl = -1;
1754 return UnixFolder_IPersistFolder3_InitializeEx(
1755 STATIC_CAST(IPersistFolder3, This), NULL, NULL, &pftiTarget);
1758 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_Save(IPersistPropertyBag *iface,
1759 IPropertyBag *pPropertyBag, BOOL fClearDirty, BOOL fSaveAllProperties)
1761 FIXME("() stub\n");
1762 return E_NOTIMPL;
1765 /* VTable for UnixFolder's IPersistPropertyBag interface.
1767 static const IPersistPropertyBagVtbl UnixFolder_IPersistPropertyBag_Vtbl = {
1768 UnixFolder_IPersistPropertyBag_QueryInterface,
1769 UnixFolder_IPersistPropertyBag_AddRef,
1770 UnixFolder_IPersistPropertyBag_Release,
1771 UnixFolder_IPersistPropertyBag_GetClassID,
1772 UnixFolder_IPersistPropertyBag_InitNew,
1773 UnixFolder_IPersistPropertyBag_Load,
1774 UnixFolder_IPersistPropertyBag_Save
1777 static HRESULT WINAPI UnixFolder_ISFHelper_QueryInterface(ISFHelper* iface, REFIID riid,
1778 void** ppvObject)
1780 return UnixFolder_IShellFolder2_QueryInterface(
1781 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, ISFHelper, iface)), riid, ppvObject);
1784 static ULONG WINAPI UnixFolder_ISFHelper_AddRef(ISFHelper* iface)
1786 return UnixFolder_IShellFolder2_AddRef(
1787 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, ISFHelper, iface)));
1790 static ULONG WINAPI UnixFolder_ISFHelper_Release(ISFHelper* iface)
1792 return UnixFolder_IShellFolder2_Release(
1793 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, ISFHelper, iface)));
1796 static HRESULT WINAPI UnixFolder_ISFHelper_GetUniqueName(ISFHelper* iface, LPWSTR pwszName, UINT uLen)
1798 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1799 IEnumIDList *pEnum;
1800 HRESULT hr;
1801 LPITEMIDLIST pidlElem;
1802 DWORD dwFetched;
1803 int i;
1804 WCHAR wszNewFolder[25];
1805 static const WCHAR wszFormat[] = { '%','s',' ','%','d',0 };
1807 TRACE("(iface=%p, pwszName=%p, uLen=%u)\n", iface, pwszName, uLen);
1809 LoadStringW(shell32_hInstance, IDS_NEWFOLDER, wszNewFolder, sizeof(wszNewFolder)/sizeof(WCHAR));
1811 if (uLen < sizeof(wszNewFolder)/sizeof(WCHAR)+3)
1812 return E_INVALIDARG;
1814 hr = IShellFolder2_EnumObjects(STATIC_CAST(IShellFolder2, This), 0,
1815 SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN, &pEnum);
1816 if (SUCCEEDED(hr)) {
1817 lstrcpynW(pwszName, wszNewFolder, uLen);
1818 IEnumIDList_Reset(pEnum);
1819 i = 2;
1820 while ((IEnumIDList_Next(pEnum, 1, &pidlElem, &dwFetched) == S_OK) && (dwFetched == 1)) {
1821 WCHAR wszTemp[MAX_PATH];
1822 _ILSimpleGetTextW(pidlElem, wszTemp, MAX_PATH);
1823 if (!lstrcmpiW(wszTemp, pwszName)) {
1824 IEnumIDList_Reset(pEnum);
1825 snprintfW(pwszName, uLen, wszFormat, wszNewFolder, i++);
1826 if (i > 99) {
1827 hr = E_FAIL;
1828 break;
1832 IEnumIDList_Release(pEnum);
1834 return hr;
1837 static HRESULT WINAPI UnixFolder_ISFHelper_AddFolder(ISFHelper* iface, HWND hwnd, LPCWSTR pwszName,
1838 LPITEMIDLIST* ppidlOut)
1840 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1841 char szNewDir[FILENAME_MAX];
1842 int cBaseLen;
1844 TRACE("(iface=%p, hwnd=%p, pwszName=%s, ppidlOut=%p)\n",
1845 iface, hwnd, debugstr_w(pwszName), ppidlOut);
1847 if (ppidlOut)
1848 *ppidlOut = NULL;
1850 if (!This->m_pszPath || !(This->m_dwAttributes & SFGAO_FILESYSTEM))
1851 return E_FAIL;
1853 lstrcpynA(szNewDir, This->m_pszPath, FILENAME_MAX);
1854 cBaseLen = lstrlenA(szNewDir);
1855 WideCharToMultiByte(CP_UNIXCP, 0, pwszName, -1, szNewDir+cBaseLen, FILENAME_MAX-cBaseLen, 0, 0);
1857 if (mkdir(szNewDir, 0777)) {
1858 char szMessage[256 + FILENAME_MAX];
1859 char szCaption[256];
1861 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_DENIED, szCaption, sizeof(szCaption));
1862 sprintf(szMessage, szCaption, szNewDir);
1863 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_CAPTION, szCaption, sizeof(szCaption));
1864 MessageBoxA(hwnd, szMessage, szCaption, MB_OK | MB_ICONEXCLAMATION);
1866 return E_FAIL;
1867 } else {
1868 LPITEMIDLIST pidlRelative;
1870 /* Inform the shell */
1871 if (SUCCEEDED(UNIXFS_path_to_pidl(This, NULL, pwszName, &pidlRelative))) {
1872 LPITEMIDLIST pidlAbsolute = ILCombine(This->m_pidlLocation, pidlRelative);
1873 if (ppidlOut)
1874 *ppidlOut = pidlRelative;
1875 else
1876 ILFree(pidlRelative);
1877 SHChangeNotify(SHCNE_MKDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1878 ILFree(pidlAbsolute);
1879 } else return E_FAIL;
1880 return S_OK;
1885 * Delete specified files by converting the path to DOS paths and calling
1886 * SHFileOperationW. If an error occurs it returns an error code. If the paths can't
1887 * be converted, S_FALSE is returned. In such situation DeleteItems will try to delete
1888 * the files using syscalls
1890 static HRESULT UNIXFS_delete_with_shfileop(UnixFolder *This, UINT cidl, const LPCITEMIDLIST *apidl)
1892 char szAbsolute[FILENAME_MAX], *pszRelative;
1893 LPWSTR wszPathsList, wszListPos;
1894 SHFILEOPSTRUCTW op;
1895 HRESULT ret;
1896 UINT i;
1898 lstrcpyA(szAbsolute, This->m_pszPath);
1899 pszRelative = szAbsolute + lstrlenA(szAbsolute);
1901 wszListPos = wszPathsList = HeapAlloc(GetProcessHeap(), 0, cidl*MAX_PATH*sizeof(WCHAR)+1);
1902 if (wszPathsList == NULL)
1903 return E_OUTOFMEMORY;
1904 for (i=0; i<cidl; i++) {
1905 LPWSTR wszDosPath;
1907 if (!_ILIsFolder(apidl[i]) && !_ILIsValue(apidl[i]))
1908 continue;
1909 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
1911 HeapFree(GetProcessHeap(), 0, wszPathsList);
1912 return E_INVALIDARG;
1914 wszDosPath = wine_get_dos_file_name(szAbsolute);
1915 if (wszDosPath == NULL || lstrlenW(wszDosPath) >= MAX_PATH)
1917 HeapFree(GetProcessHeap(), 0, wszPathsList);
1918 HeapFree(GetProcessHeap(), 0, wszDosPath);
1919 return S_FALSE;
1921 lstrcpyW(wszListPos, wszDosPath);
1922 wszListPos += lstrlenW(wszListPos)+1;
1923 HeapFree(GetProcessHeap(), 0, wszDosPath);
1925 *wszListPos = 0;
1927 ZeroMemory(&op, sizeof(op));
1928 op.hwnd = GetActiveWindow();
1929 op.wFunc = FO_DELETE;
1930 op.pFrom = wszPathsList;
1931 op.fFlags = FOF_ALLOWUNDO;
1932 if (!SHFileOperationW(&op))
1934 WARN("SHFileOperationW failed\n");
1935 ret = E_FAIL;
1937 else
1938 ret = S_OK;
1940 HeapFree(GetProcessHeap(), 0, wszPathsList);
1941 return ret;
1944 static HRESULT UNIXFS_delete_with_syscalls(UnixFolder *This, UINT cidl, const LPCITEMIDLIST *apidl)
1946 char szAbsolute[FILENAME_MAX], *pszRelative;
1947 static const WCHAR empty[] = {0};
1948 UINT i;
1950 if (!SHELL_ConfirmYesNoW(GetActiveWindow(), ASK_DELETE_SELECTED, empty))
1951 return S_OK;
1953 lstrcpyA(szAbsolute, This->m_pszPath);
1954 pszRelative = szAbsolute + lstrlenA(szAbsolute);
1956 for (i=0; i<cidl; i++) {
1957 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
1958 return E_INVALIDARG;
1959 if (_ILIsFolder(apidl[i])) {
1960 if (rmdir(szAbsolute))
1961 return E_FAIL;
1962 } else if (_ILIsValue(apidl[i])) {
1963 if (unlink(szAbsolute))
1964 return E_FAIL;
1967 return S_OK;
1970 static HRESULT WINAPI UnixFolder_ISFHelper_DeleteItems(ISFHelper* iface, UINT cidl,
1971 LPCITEMIDLIST* apidl)
1973 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1974 char szAbsolute[FILENAME_MAX], *pszRelative;
1975 LPITEMIDLIST pidlAbsolute;
1976 HRESULT hr = S_OK;
1977 UINT i;
1978 struct stat st;
1980 TRACE("(iface=%p, cidl=%d, apidl=%p)\n", iface, cidl, apidl);
1982 hr = UNIXFS_delete_with_shfileop(This, cidl, apidl);
1983 if (hr == S_FALSE)
1984 hr = UNIXFS_delete_with_syscalls(This, cidl, apidl);
1986 lstrcpyA(szAbsolute, This->m_pszPath);
1987 pszRelative = szAbsolute + lstrlenA(szAbsolute);
1989 /* we need to manually send the notifies if the files doesn't exist */
1990 for (i=0; i<cidl; i++) {
1991 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
1992 continue;
1993 pidlAbsolute = ILCombine(This->m_pidlLocation, apidl[i]);
1994 if (stat(szAbsolute, &st))
1996 if (_ILIsFolder(apidl[i])) {
1997 SHChangeNotify(SHCNE_RMDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1998 } else if (_ILIsValue(apidl[i])) {
1999 SHChangeNotify(SHCNE_DELETE, SHCNF_IDLIST, pidlAbsolute, NULL);
2002 ILFree(pidlAbsolute);
2005 return hr;
2008 static HRESULT WINAPI UnixFolder_ISFHelper_CopyItems(ISFHelper* iface, IShellFolder *psfFrom,
2009 UINT cidl, LPCITEMIDLIST *apidl)
2011 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
2012 DWORD dwAttributes;
2013 UINT i;
2014 HRESULT hr;
2015 char szAbsoluteDst[FILENAME_MAX], *pszRelativeDst;
2017 TRACE("(iface=%p, psfFrom=%p, cidl=%d, apidl=%p)\n", iface, psfFrom, cidl, apidl);
2019 if (!psfFrom || !cidl || !apidl)
2020 return E_INVALIDARG;
2022 /* All source items have to be filesystem items. */
2023 dwAttributes = SFGAO_FILESYSTEM;
2024 hr = IShellFolder_GetAttributesOf(psfFrom, cidl, apidl, &dwAttributes);
2025 if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM))
2026 return E_INVALIDARG;
2028 lstrcpyA(szAbsoluteDst, This->m_pszPath);
2029 pszRelativeDst = szAbsoluteDst + strlen(szAbsoluteDst);
2031 for (i=0; i<cidl; i++) {
2032 WCHAR wszSrc[MAX_PATH];
2033 char szSrc[FILENAME_MAX];
2034 STRRET strret;
2035 HRESULT res;
2036 WCHAR *pwszDosSrc, *pwszDosDst;
2038 /* Build the unix path of the current source item. */
2039 if (FAILED(IShellFolder_GetDisplayNameOf(psfFrom, apidl[i], SHGDN_FORPARSING, &strret)))
2040 return E_FAIL;
2041 if (FAILED(StrRetToBufW(&strret, apidl[i], wszSrc, MAX_PATH)))
2042 return E_FAIL;
2043 if (!UNIXFS_get_unix_path(wszSrc, szSrc))
2044 return E_FAIL;
2046 /* Build the unix path of the current destination item */
2047 UNIXFS_filename_from_shitemid(apidl[i], pszRelativeDst);
2049 pwszDosSrc = wine_get_dos_file_name(szSrc);
2050 pwszDosDst = wine_get_dos_file_name(szAbsoluteDst);
2052 if (pwszDosSrc && pwszDosDst)
2053 res = UNIXFS_copy(pwszDosSrc, pwszDosDst);
2054 else
2055 res = E_OUTOFMEMORY;
2057 HeapFree(GetProcessHeap(), 0, pwszDosSrc);
2058 HeapFree(GetProcessHeap(), 0, pwszDosDst);
2060 if (res != S_OK)
2061 return res;
2063 return S_OK;
2066 /* VTable for UnixFolder's ISFHelper interface
2068 static const ISFHelperVtbl UnixFolder_ISFHelper_Vtbl = {
2069 UnixFolder_ISFHelper_QueryInterface,
2070 UnixFolder_ISFHelper_AddRef,
2071 UnixFolder_ISFHelper_Release,
2072 UnixFolder_ISFHelper_GetUniqueName,
2073 UnixFolder_ISFHelper_AddFolder,
2074 UnixFolder_ISFHelper_DeleteItems,
2075 UnixFolder_ISFHelper_CopyItems
2078 static HRESULT WINAPI UnixFolder_IDropTarget_QueryInterface(IDropTarget* iface, REFIID riid,
2079 void** ppvObject)
2081 return UnixFolder_IShellFolder2_QueryInterface(
2082 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IDropTarget, iface)), riid, ppvObject);
2085 static ULONG WINAPI UnixFolder_IDropTarget_AddRef(IDropTarget* iface)
2087 return UnixFolder_IShellFolder2_AddRef(
2088 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IDropTarget, iface)));
2091 static ULONG WINAPI UnixFolder_IDropTarget_Release(IDropTarget* iface)
2093 return UnixFolder_IShellFolder2_Release(
2094 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IDropTarget, iface)));
2097 #define HIDA_GetPIDLFolder(pida) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[0])
2098 #define HIDA_GetPIDLItem(pida, i) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[i+1])
2100 static HRESULT WINAPI UnixFolder_IDropTarget_DragEnter(IDropTarget *iface, IDataObject *pDataObject,
2101 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2103 UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
2104 FORMATETC format;
2105 STGMEDIUM medium;
2107 TRACE("(iface=%p, pDataObject=%p, dwKeyState=%08x, pt={.x=%d, .y=%d}, pdwEffect=%p)\n",
2108 iface, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
2110 if (!pdwEffect || !pDataObject)
2111 return E_INVALIDARG;
2113 /* Compute a mask of supported drop-effects for this shellfolder object and the given data
2114 * object. Dropping is only supported on folders, which represent filesystem locations. One
2115 * can't drop on file objects. And the 'move' drop effect is only supported, if the source
2116 * folder is not identical to the target folder. */
2117 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2118 InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
2119 if ((This->m_dwAttributes & SFGAO_FILESYSTEM) && /* Only drop to filesystem folders */
2120 _ILIsFolder(ILFindLastID(This->m_pidlLocation)) && /* Only drop to folders, not to files */
2121 SUCCEEDED(IDataObject_GetData(pDataObject, &format, &medium))) /* Only ShellIDList format */
2123 LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
2124 This->m_dwDropEffectsMask |= DROPEFFECT_COPY|DROPEFFECT_LINK;
2126 if (pidaShellIDList) { /* Files can only be moved between two different folders */
2127 if (!ILIsEqual(HIDA_GetPIDLFolder(pidaShellIDList), This->m_pidlLocation))
2128 This->m_dwDropEffectsMask |= DROPEFFECT_MOVE;
2129 GlobalUnlock(medium.u.hGlobal);
2133 *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
2135 return S_OK;
2138 static HRESULT WINAPI UnixFolder_IDropTarget_DragOver(IDropTarget *iface, DWORD dwKeyState,
2139 POINTL pt, DWORD *pdwEffect)
2141 UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
2143 TRACE("(iface=%p, dwKeyState=%08x, pt={.x=%d, .y=%d}, pdwEffect=%p)\n", iface, dwKeyState,
2144 pt.x, pt.y, pdwEffect);
2146 if (!pdwEffect)
2147 return E_INVALIDARG;
2149 *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
2151 return S_OK;
2154 static HRESULT WINAPI UnixFolder_IDropTarget_DragLeave(IDropTarget *iface) {
2155 UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
2157 TRACE("(iface=%p)\n", iface);
2159 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2161 return S_OK;
2164 static HRESULT WINAPI UnixFolder_IDropTarget_Drop(IDropTarget *iface, IDataObject *pDataObject,
2165 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2167 UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
2168 FORMATETC format;
2169 STGMEDIUM medium;
2170 HRESULT hr;
2172 TRACE("(iface=%p, pDataObject=%p, dwKeyState=%d, pt={.x=%d, .y=%d}, pdwEffect=%p) semi-stub\n",
2173 iface, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
2175 InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
2176 hr = IDataObject_GetData(pDataObject, &format, &medium);
2177 if (FAILED(hr))
2178 return hr;
2180 if (medium.tymed == TYMED_HGLOBAL) {
2181 IShellFolder *psfSourceFolder, *psfDesktopFolder;
2182 LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
2183 STRRET strret;
2184 UINT i;
2186 if (!pidaShellIDList)
2187 return HRESULT_FROM_WIN32(GetLastError());
2189 hr = SHGetDesktopFolder(&psfDesktopFolder);
2190 if (FAILED(hr)) {
2191 GlobalUnlock(medium.u.hGlobal);
2192 return hr;
2195 hr = IShellFolder_BindToObject(psfDesktopFolder, HIDA_GetPIDLFolder(pidaShellIDList), NULL,
2196 &IID_IShellFolder, (LPVOID*)&psfSourceFolder);
2197 IShellFolder_Release(psfDesktopFolder);
2198 if (FAILED(hr)) {
2199 GlobalUnlock(medium.u.hGlobal);
2200 return hr;
2203 for (i = 0; i < pidaShellIDList->cidl; i++) {
2204 WCHAR wszSourcePath[MAX_PATH];
2206 hr = IShellFolder_GetDisplayNameOf(psfSourceFolder, HIDA_GetPIDLItem(pidaShellIDList, i),
2207 SHGDN_FORPARSING, &strret);
2208 if (FAILED(hr))
2209 break;
2211 hr = StrRetToBufW(&strret, NULL, wszSourcePath, MAX_PATH);
2212 if (FAILED(hr))
2213 break;
2215 switch (*pdwEffect) {
2216 case DROPEFFECT_MOVE:
2217 FIXME("Move %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2218 break;
2219 case DROPEFFECT_COPY:
2220 FIXME("Copy %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2221 break;
2222 case DROPEFFECT_LINK:
2223 FIXME("Link %s from %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2224 break;
2228 IShellFolder_Release(psfSourceFolder);
2229 GlobalUnlock(medium.u.hGlobal);
2230 return hr;
2233 return E_NOTIMPL;
2236 /* VTable for UnixFolder's IDropTarget interface
2238 static const IDropTargetVtbl UnixFolder_IDropTarget_Vtbl = {
2239 UnixFolder_IDropTarget_QueryInterface,
2240 UnixFolder_IDropTarget_AddRef,
2241 UnixFolder_IDropTarget_Release,
2242 UnixFolder_IDropTarget_DragEnter,
2243 UnixFolder_IDropTarget_DragOver,
2244 UnixFolder_IDropTarget_DragLeave,
2245 UnixFolder_IDropTarget_Drop
2248 /******************************************************************************
2249 * Unix[Dos]Folder_Constructor [Internal]
2251 * PARAMS
2252 * pUnkOuter [I] Outer class for aggregation. Currently ignored.
2253 * riid [I] Interface asked for by the client.
2254 * ppv [O] Pointer to an riid interface to the UnixFolder object.
2256 * NOTES
2257 * Those are the only functions exported from shfldr_unixfs.c. They are called from
2258 * shellole.c's default class factory and thus have to exhibit a LPFNCREATEINSTANCE
2259 * compatible signature.
2261 * The UnixDosFolder_Constructor sets the dwPathMode member to PATHMODE_DOS. This
2262 * means that paths are converted from dos to unix and back at the interfaces.
2264 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, const CLSID *pCLSID)
2266 HRESULT hr = E_FAIL;
2267 UnixFolder *pUnixFolder;
2269 if (pUnkOuter) {
2270 FIXME("Aggregation not yet implemented!\n");
2271 return CLASS_E_NOAGGREGATION;
2274 pUnixFolder = SHAlloc((ULONG)sizeof(UnixFolder));
2276 if(pUnixFolder) {
2277 pUnixFolder->lpIShellFolder2Vtbl = &UnixFolder_IShellFolder2_Vtbl;
2278 pUnixFolder->lpIPersistFolder3Vtbl = &UnixFolder_IPersistFolder3_Vtbl;
2279 pUnixFolder->lpIPersistPropertyBagVtbl = &UnixFolder_IPersistPropertyBag_Vtbl;
2280 pUnixFolder->lpISFHelperVtbl = &UnixFolder_ISFHelper_Vtbl;
2281 pUnixFolder->lpIDropTargetVtbl = &UnixFolder_IDropTarget_Vtbl;
2282 pUnixFolder->m_cRef = 0;
2283 pUnixFolder->m_pszPath = NULL;
2284 pUnixFolder->m_pidlLocation = NULL;
2285 pUnixFolder->m_dwPathMode = IsEqualCLSID(&CLSID_UnixFolder, pCLSID) ? PATHMODE_UNIX : PATHMODE_DOS;
2286 pUnixFolder->m_dwAttributes = 0;
2287 pUnixFolder->m_pCLSID = pCLSID;
2288 pUnixFolder->m_dwDropEffectsMask = DROPEFFECT_NONE;
2290 UnixFolder_IShellFolder2_AddRef(STATIC_CAST(IShellFolder2, pUnixFolder));
2291 hr = UnixFolder_IShellFolder2_QueryInterface(STATIC_CAST(IShellFolder2, pUnixFolder), riid, ppv);
2292 UnixFolder_IShellFolder2_Release(STATIC_CAST(IShellFolder2, pUnixFolder));
2294 return hr;
2297 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2298 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2299 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixFolder);
2302 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2303 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2304 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixDosFolder);
2307 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2308 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2309 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_FolderShortcut);
2312 HRESULT WINAPI MyDocuments_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2313 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2314 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_MyDocuments);
2317 /******************************************************************************
2318 * UnixSubFolderIterator
2320 * Class whose heap based objects represent iterators over the sub-directories
2321 * of a given UnixFolder object.
2324 /* UnixSubFolderIterator object layout and typedef.
2326 typedef struct _UnixSubFolderIterator {
2327 const IEnumIDListVtbl *lpIEnumIDListVtbl;
2328 LONG m_cRef;
2329 SHCONTF m_fFilter;
2330 DIR *m_dirFolder;
2331 char m_szFolder[FILENAME_MAX];
2332 } UnixSubFolderIterator;
2334 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
2335 TRACE("(iterator=%p)\n", iterator);
2337 if (iterator->m_dirFolder)
2338 closedir(iterator->m_dirFolder);
2339 SHFree(iterator);
2342 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface,
2343 REFIID riid, void** ppv)
2345 TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
2347 if (!ppv) return E_INVALIDARG;
2349 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
2350 *ppv = iface;
2351 } else {
2352 *ppv = NULL;
2353 return E_NOINTERFACE;
2356 IEnumIDList_AddRef(iface);
2357 return S_OK;
2360 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
2362 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2364 TRACE("(iface=%p)\n", iface);
2366 return InterlockedIncrement(&This->m_cRef);
2369 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
2371 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2372 ULONG cRef;
2374 TRACE("(iface=%p)\n", iface);
2376 cRef = InterlockedDecrement(&This->m_cRef);
2378 if (!cRef)
2379 UnixSubFolderIterator_Destroy(This);
2381 return cRef;
2384 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt,
2385 LPITEMIDLIST* rgelt, ULONG* pceltFetched)
2387 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2388 ULONG i = 0;
2390 /* This->m_dirFolder will be NULL if the user doesn't have access rights for the dir. */
2391 if (This->m_dirFolder) {
2392 char *pszRelativePath = This->m_szFolder + lstrlenA(This->m_szFolder);
2393 struct dirent *pDirEntry;
2395 while (i < celt) {
2396 pDirEntry = readdir(This->m_dirFolder);
2397 if (!pDirEntry) break; /* No more entries */
2398 if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
2400 /* Temporarily build absolute path in This->m_szFolder. Then construct a pidl
2401 * and see if it passes the filter.
2403 lstrcpyA(pszRelativePath, pDirEntry->d_name);
2404 rgelt[i] = SHAlloc(
2405 UNIXFS_shitemid_len_from_filename(pszRelativePath, NULL, NULL)+sizeof(USHORT));
2406 if (!UNIXFS_build_shitemid(This->m_szFolder, NULL, rgelt[i]) ||
2407 !UNIXFS_is_pidl_of_type(rgelt[i], This->m_fFilter))
2409 SHFree(rgelt[i]);
2410 continue;
2412 memset(((PBYTE)rgelt[i])+rgelt[i]->mkid.cb, 0, sizeof(USHORT));
2413 i++;
2415 *pszRelativePath = '\0'; /* Restore the original path in This->m_szFolder. */
2418 if (pceltFetched)
2419 *pceltFetched = i;
2421 return (i == 0) ? S_FALSE : S_OK;
2424 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
2426 LPITEMIDLIST *apidl;
2427 ULONG cFetched;
2428 HRESULT hr;
2430 TRACE("(iface=%p, celt=%d)\n", iface, celt);
2432 /* Call IEnumIDList::Next and delete the resulting pidls. */
2433 apidl = SHAlloc(celt * sizeof(LPITEMIDLIST));
2434 hr = IEnumIDList_Next(iface, celt, apidl, &cFetched);
2435 if (SUCCEEDED(hr))
2436 while (cFetched--)
2437 SHFree(apidl[cFetched]);
2438 SHFree(apidl);
2440 return hr;
2443 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
2445 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2447 TRACE("(iface=%p)\n", iface);
2449 if (This->m_dirFolder)
2450 rewinddir(This->m_dirFolder);
2452 return S_OK;
2455 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This,
2456 IEnumIDList** ppenum)
2458 FIXME("stub\n");
2459 return E_NOTIMPL;
2462 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
2464 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
2465 UnixSubFolderIterator_IEnumIDList_QueryInterface,
2466 UnixSubFolderIterator_IEnumIDList_AddRef,
2467 UnixSubFolderIterator_IEnumIDList_Release,
2468 UnixSubFolderIterator_IEnumIDList_Next,
2469 UnixSubFolderIterator_IEnumIDList_Skip,
2470 UnixSubFolderIterator_IEnumIDList_Reset,
2471 UnixSubFolderIterator_IEnumIDList_Clone
2474 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter) {
2475 UnixSubFolderIterator *iterator;
2477 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
2479 iterator = SHAlloc((ULONG)sizeof(UnixSubFolderIterator));
2480 iterator->lpIEnumIDListVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
2481 iterator->m_cRef = 0;
2482 iterator->m_fFilter = fFilter;
2483 iterator->m_dirFolder = opendir(pUnixFolder->m_pszPath);
2484 lstrcpyA(iterator->m_szFolder, pUnixFolder->m_pszPath);
2486 UnixSubFolderIterator_IEnumIDList_AddRef((IEnumIDList*)iterator);
2488 return (IUnknown*)iterator;
2491 #else /* __MINGW32__ || _MSC_VER */
2493 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2495 return E_NOTIMPL;
2498 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2500 return E_NOTIMPL;
2503 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2505 return E_NOTIMPL;
2508 HRESULT WINAPI MyDocuments_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2510 return E_NOTIMPL;
2513 #endif /* __MINGW32__ || _MSC_VER */
2515 /******************************************************************************
2516 * UNIXFS_is_rooted_at_desktop [Internal]
2518 * Checks if the unixfs namespace extension is rooted at desktop level.
2520 * RETURNS
2521 * TRUE, if unixfs is rooted at desktop level
2522 * FALSE, if not.
2524 BOOL UNIXFS_is_rooted_at_desktop(void) {
2525 HKEY hKey;
2526 WCHAR wszRootedAtDesktop[69 + CHARS_IN_GUID] = {
2527 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2528 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2529 'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
2530 'N','a','m','e','S','p','a','c','e','\\',0 };
2532 if (StringFromGUID2(&CLSID_UnixDosFolder, wszRootedAtDesktop + 69, CHARS_IN_GUID) &&
2533 RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszRootedAtDesktop, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
2535 RegCloseKey(hKey);
2536 return TRUE;
2538 return FALSE;