test version of startmenu root with big icons
[reactos.git] / reactos / subsys / system / explorer / shell / shellfs.cpp
1 /*
2 * Copyright 2003, 2004 Martin Fuchs
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19
20 //
21 // Explorer clone
22 //
23 // shellfs.cpp
24 //
25 // Martin Fuchs, 23.07.2003
26 //
27
28
29 #include <precomp.h>
30
31 //#include "shellfs.h"
32 //#include "winfs.h"
33
34 #include <shlwapi.h>
35
36
37 bool ShellDirectory::fill_w32fdata_shell(LPCITEMIDLIST pidl, SFGAOF attribs, WIN32_FIND_DATA* pw32fdata, BY_HANDLE_FILE_INFORMATION* pbhfi, bool do_access)
38 {
39 CONTEXT("ShellDirectory::fill_w32fdata_shell()");
40
41 bool bhfi_valid = false;
42
43 if (do_access && !( (attribs&SFGAO_FILESYSTEM) && SUCCEEDED(
44 SHGetDataFromIDList(_folder, pidl, SHGDFIL_FINDDATA, pw32fdata, sizeof(WIN32_FIND_DATA))) )) {
45 WIN32_FILE_ATTRIBUTE_DATA fad;
46 IDataObject* pDataObj;
47
48 STGMEDIUM medium = {0, {0}, 0};
49 FORMATETC fmt = {g_Globals._cfStrFName, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
50
51 HRESULT hr = _folder->GetUIObjectOf(0, 1, &pidl, IID_IDataObject, 0, (LPVOID*)&pDataObj);
52
53 if (SUCCEEDED(hr)) {
54 hr = pDataObj->GetData(&fmt, &medium);
55
56 pDataObj->Release();
57
58 if (SUCCEEDED(hr)) {
59 LPCTSTR path = (LPCTSTR)GlobalLock(medium.UNION_MEMBER(hGlobal));
60
61 if (path) {
62 // fill with drive names "C:", ...
63 assert(_tcslen(path) < GlobalSize(medium.UNION_MEMBER(hGlobal)));
64 _tcscpy(pw32fdata->cFileName, path);
65
66 UINT sem_org = SetErrorMode(SEM_FAILCRITICALERRORS);
67
68 if (GetFileAttributesEx(path, GetFileExInfoStandard, &fad)) {
69 pw32fdata->dwFileAttributes = fad.dwFileAttributes;
70 pw32fdata->ftCreationTime = fad.ftCreationTime;
71 pw32fdata->ftLastAccessTime = fad.ftLastAccessTime;
72 pw32fdata->ftLastWriteTime = fad.ftLastWriteTime;
73
74 if (!(fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
75 pw32fdata->nFileSizeLow = fad.nFileSizeLow;
76 pw32fdata->nFileSizeHigh = fad.nFileSizeHigh;
77 }
78 }
79
80 HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
81 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
82
83 if (hFile != INVALID_HANDLE_VALUE) {
84 if (GetFileInformationByHandle(hFile, pbhfi))
85 bhfi_valid = true;
86
87 CloseHandle(hFile);
88 }
89
90 SetErrorMode(sem_org);
91
92 GlobalUnlock(medium.UNION_MEMBER(hGlobal));
93 GlobalFree(medium.UNION_MEMBER(hGlobal));
94 }
95 }
96 }
97 }
98
99 if (!do_access || !(attribs&SFGAO_FILESYSTEM)) // Archiv files should not be displayed as folders in explorer view.
100 if (attribs & (SFGAO_FOLDER|SFGAO_HASSUBFOLDER))
101 pw32fdata->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
102
103 if (attribs & SFGAO_READONLY)
104 pw32fdata->dwFileAttributes |= FILE_ATTRIBUTE_READONLY;
105
106 if (attribs & SFGAO_COMPRESSED)
107 pw32fdata->dwFileAttributes |= FILE_ATTRIBUTE_COMPRESSED;
108
109 return bhfi_valid;
110 }
111
112
113 ShellPath ShellEntry::create_absolute_pidl() const
114 {
115 CONTEXT("ShellEntry::create_absolute_pidl()");
116
117 if (_up)
118 if (_up->_etype == ET_SHELL) {
119 ShellDirectory* dir = static_cast<ShellDirectory*>(_up);
120
121 if (dir->_pidl->mkid.cb) // Caching of absolute PIDLs could enhance performance.
122 return _pidl.create_absolute_pidl(dir->create_absolute_pidl());
123 } else
124 return _pidl.create_absolute_pidl(_up->create_absolute_pidl());
125
126 return _pidl;
127 }
128
129
130 // get full path of a shell entry
131 bool ShellEntry::get_path(PTSTR path, size_t path_count) const
132 {
133 if (!path || path_count==0)
134 return false;
135 /*
136 path[0] = TEXT('\0');
137
138 if (FAILED(path_from_pidl(get_parent_folder(), &*_pidl, path, MAX_PATH)))
139 return false;
140 */
141 FileSysShellPath fs_path(create_absolute_pidl());
142 LPCTSTR ret = fs_path;
143
144 if (ret) {
145 lstrcpyn(path, ret, path_count);
146 return true;
147 } else
148 return false;
149 }
150
151
152 // get full path of a shell folder
153 bool ShellDirectory::get_path(PTSTR path, size_t path_count) const
154 {
155 CONTEXT("ShellDirectory::get_path()");
156
157 if (!path || path_count==0)
158 return false;
159
160 path[0] = TEXT('\0');
161
162 if (_folder.empty())
163 return false;
164
165 SFGAOF attribs = SFGAO_FILESYSTEM;
166
167 if (FAILED(const_cast<ShellFolder&>(_folder)->GetAttributesOf(1, (LPCITEMIDLIST*)&_pidl, &attribs)))
168 return false;
169
170 if (!(attribs & SFGAO_FILESYSTEM))
171 return false;
172
173 if (FAILED(path_from_pidl(get_parent_folder(), &*_pidl, path, path_count)))
174 return false;
175
176 return true;
177 }
178
179
180 BOOL ShellEntry::launch_entry(HWND hwnd, UINT nCmdShow)
181 {
182 CONTEXT("ShellEntry::launch_entry()");
183
184 SHELLEXECUTEINFO shexinfo;
185
186 shexinfo.cbSize = sizeof(SHELLEXECUTEINFO);
187 shexinfo.fMask = SEE_MASK_INVOKEIDLIST; // SEE_MASK_IDLIST is also possible.
188 shexinfo.hwnd = hwnd;
189 shexinfo.lpVerb = NULL;
190 shexinfo.lpFile = NULL;
191 shexinfo.lpParameters = NULL;
192 shexinfo.lpDirectory = NULL;
193 shexinfo.nShow = nCmdShow;
194
195 ShellPath shell_path = create_absolute_pidl();
196 shexinfo.lpIDList = &*shell_path;
197
198 // add PIDL to the recent file list
199 SHAddToRecentDocs(SHARD_PIDL, shexinfo.lpIDList);
200
201 BOOL ret = TRUE;
202
203 if (!ShellExecuteEx(&shexinfo)) {
204 display_error(hwnd, GetLastError());
205 ret = FALSE;
206 }
207
208 return ret;
209 }
210
211
212 HRESULT ShellEntry::do_context_menu(HWND hwnd, LPPOINT pptScreen, CtxMenuInterfaces& cm_ifs)
213 {
214 ShellDirectory* dir = static_cast<ShellDirectory*>(_up);
215
216 ShellFolder folder = dir? dir->_folder: GetDesktopFolder();
217 LPCITEMIDLIST pidl = _pidl;
218
219 return ShellFolderContextMenu(folder, hwnd, 1, &pidl, pptScreen->x, pptScreen->y, cm_ifs);
220 }
221
222
223 HRESULT ShellEntry::GetUIObjectOf(HWND hWnd, REFIID riid, LPVOID* ppvOut)
224 {
225 LPCITEMIDLIST pidl = _pidl;
226
227 return get_parent_folder()->GetUIObjectOf(hWnd, 1, &pidl, riid, NULL, ppvOut);
228 }
229
230
231 void ShellDirectory::read_directory(int scan_flags)
232 {
233 CONTEXT("ShellDirectory::read_directory()");
234
235 int level = _level + 1;
236
237 Entry* first_entry = NULL;
238 Entry* last = NULL;
239
240 /*if (_folder.empty())
241 return;*/
242
243 TCHAR buffer[MAX_PATH];
244
245 if ((scan_flags&SCAN_FILESYSTEM) && get_path(buffer, COUNTOF(buffer)) && _tcsncmp(buffer,TEXT("::{"),3)) {
246 Entry* entry = NULL; // eliminate useless GCC warning by initializing entry
247
248 LPTSTR p = buffer + _tcslen(buffer);
249
250 // TODO FIXME - this can overflow
251 lstrcpy(p, TEXT("\\*"));
252
253 WIN32_FIND_DATA w32fd;
254 HANDLE hFind = FindFirstFile(buffer, &w32fd);
255
256 if (hFind != INVALID_HANDLE_VALUE) {
257 do {
258 // ignore hidden files (usefull in the start menu)
259 if (w32fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
260 continue;
261
262 // ignore directory entries "." and ".."
263 if ((w32fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) &&
264 w32fd.cFileName[0]==TEXT('.') &&
265 (w32fd.cFileName[1]==TEXT('\0') ||
266 (w32fd.cFileName[1]==TEXT('.') && w32fd.cFileName[2]==TEXT('\0'))))
267 continue;
268
269 lstrcpy(p+1, w32fd.cFileName);
270
271 if (w32fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
272 entry = new WinDirectory(this, buffer);
273 else
274 entry = new WinEntry(this);
275
276 if (!first_entry)
277 first_entry = entry;
278
279 if (last)
280 last->_next = entry;
281
282 memcpy(&entry->_data, &w32fd, sizeof(WIN32_FIND_DATA));
283
284 entry->_level = level;
285
286 if (scan_flags & SCAN_DO_ACCESS) {
287 HANDLE hFile = CreateFile(buffer, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
288 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
289
290 if (hFile != INVALID_HANDLE_VALUE) {
291 if (GetFileInformationByHandle(hFile, &entry->_bhfi))
292 entry->_bhfi_valid = true;
293
294 if (ScanNTFSStreams(entry, hFile))
295 entry->_scanned = true; // There exist named NTFS sub-streams in this file.
296
297 CloseHandle(hFile);
298 }
299 }
300
301 // set file type name
302 LPCTSTR ext = g_Globals._ftype_mgr.set_type(entry);
303
304 DWORD attribs = SFGAO_FILESYSTEM;
305
306 if (w32fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
307 attribs |= SFGAO_FOLDER;
308
309 if (w32fd.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
310 attribs |= SFGAO_READONLY;
311
312 //if (w32fd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
313 // attribs |= SFGAO_HIDDEN;
314
315 if (w32fd.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)
316 attribs |= SFGAO_COMPRESSED;
317
318 if (ext && !_tcsicmp(ext, _T(".lnk"))) {
319 attribs |= SFGAO_LINK;
320 w32fd.dwFileAttributes |= ATTRIBUTE_SYMBOLIC_LINK;
321 }
322
323 entry->_shell_attribs = attribs;
324
325 if (w32fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
326 entry->_icon_id = ICID_FOLDER;
327 /* else if (scan_flags & SCAN_EXTRACT_ICONS)
328 try {
329 entry->extract_icon(big_icons);
330 } catch(COMException&) {
331 // ignore unexpected exceptions while extracting icons
332 }
333 */
334 last = entry;
335 } while(FindNextFile(hFind, &w32fd));
336
337 FindClose(hFind);
338 }
339
340 } else { // !SCAN_FILESYSTEM
341
342 ShellItemEnumerator enumerator(_folder, SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN|SHCONTF_SHAREABLE|SHCONTF_STORAGE);
343
344 TCHAR name[MAX_PATH];
345 HRESULT hr_next = S_OK;
346
347 do {
348 #define FETCH_ITEM_COUNT 32
349 LPITEMIDLIST pidls[FETCH_ITEM_COUNT];
350 ULONG cnt = 0;
351
352 memset(pidls, 0, sizeof(pidls));
353
354 hr_next = enumerator->Next(FETCH_ITEM_COUNT, pidls, &cnt);
355
356 /* don't break yet now: Registry Explorer Plugin returns E_FAIL!
357 if (!SUCCEEDED(hr_next))
358 break; */
359
360 if (hr_next == S_FALSE)
361 break;
362
363 for(ULONG n=0; n<cnt; ++n) {
364 WIN32_FIND_DATA w32fd;
365 BY_HANDLE_FILE_INFORMATION bhfi;
366 bool bhfi_valid = false;
367
368 memset(&w32fd, 0, sizeof(WIN32_FIND_DATA));
369
370 SFGAOF attribs_before = ~SFGAO_READONLY & ~SFGAO_VALIDATE;
371 SFGAOF attribs = attribs_before;
372 HRESULT hr = _folder->GetAttributesOf(1, (LPCITEMIDLIST*)&pidls[n], &attribs);
373 bool removeable = false;
374
375 if (SUCCEEDED(hr) && attribs!=attribs_before) {
376 // avoid accessing floppy drives when browsing "My Computer"
377 if (attribs & SFGAO_REMOVABLE) {
378 attribs |= SFGAO_HASSUBFOLDER;
379 removeable = true;
380 } else if (scan_flags & SCAN_DO_ACCESS) {
381 DWORD attribs2 = SFGAO_READONLY;
382
383 HRESULT hr = _folder->GetAttributesOf(1, (LPCITEMIDLIST*)&pidls[n], &attribs2);
384
385 if (SUCCEEDED(hr))
386 attribs |= attribs2;
387 }
388 } else
389 attribs = 0;
390
391 bhfi_valid = fill_w32fdata_shell(pidls[n], attribs, &w32fd, &bhfi,
392 (scan_flags&SCAN_DO_ACCESS) && !removeable);
393
394 try {
395 Entry* entry = NULL; // eliminate useless GCC warning by initializing entry
396
397 if (w32fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
398 entry = new ShellDirectory(this, pidls[n], _hwnd);
399 else
400 entry = new ShellEntry(this, pidls[n]);
401
402 if (!first_entry)
403 first_entry = entry;
404
405 if (last)
406 last->_next = entry;
407
408 memcpy(&entry->_data, &w32fd, sizeof(WIN32_FIND_DATA));
409
410 if (bhfi_valid)
411 memcpy(&entry->_bhfi, &bhfi, sizeof(BY_HANDLE_FILE_INFORMATION));
412
413 if (SUCCEEDED(name_from_pidl(_folder, pidls[n], name, MAX_PATH, SHGDN_INFOLDER|0x2000/*0x2000=SHGDN_INCLUDE_NONFILESYS*/))) {
414 if (!entry->_data.cFileName[0])
415 _tcscpy(entry->_data.cFileName, name);
416 else if (_tcscmp(entry->_display_name, name))
417 entry->_display_name = _tcsdup(name); // store display name separate from file name; sort display by file name
418 }
419
420 if (attribs & SFGAO_LINK)
421 w32fd.dwFileAttributes |= ATTRIBUTE_SYMBOLIC_LINK;
422
423 entry->_level = level;
424 entry->_shell_attribs = attribs;
425 entry->_bhfi_valid = bhfi_valid;
426
427 // set file type name
428 g_Globals._ftype_mgr.set_type(entry);
429
430 // get icons for files and virtual objects
431 if (!(entry->_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
432 !(attribs & SFGAO_FILESYSTEM)) {
433 /* if (scan_flags & SCAN_EXTRACT_ICONS)
434 try {
435 entry->extract_icon(big_icons);
436 } catch(COMException&) {
437 // ignore unexpected exceptions while extracting icons
438 }
439 */ } else if (entry->_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
440 entry->_icon_id = ICID_FOLDER;
441 else
442 entry->_icon_id = ICID_NONE; // don't try again later
443
444 last = entry;
445 } catch(COMException& e) {
446 HandleException(e, _hwnd);
447 }
448 }
449 } while(SUCCEEDED(hr_next));
450 }
451
452 if (last)
453 last->_next = NULL;
454
455 _down = first_entry;
456 _scanned = true;
457 }
458
459 const void* ShellDirectory::get_next_path_component(const void* p) const
460 {
461 LPITEMIDLIST pidl = (LPITEMIDLIST)p;
462
463 if (!pidl || !pidl->mkid.cb)
464 return NULL;
465
466 // go to next element
467 pidl = (LPITEMIDLIST)((LPBYTE)pidl+pidl->mkid.cb);
468
469 return pidl;
470 }
471
472 Entry* ShellDirectory::find_entry(const void* p)
473 {
474 LPITEMIDLIST pidl = (LPITEMIDLIST) p;
475
476 // handle special case of empty trailing id list entry
477 if (!pidl->mkid.cb)
478 return this;
479
480 for(Entry*entry=_down; entry; entry=entry->_next)
481 if (entry->_etype == ET_SHELL) {
482 ShellEntry* se = static_cast<ShellEntry*>(entry);
483
484 if (se->_pidl && se->_pidl->mkid.cb==pidl->mkid.cb && !memcmp(se->_pidl, pidl, se->_pidl->mkid.cb))
485 return entry;
486 }
487
488 return NULL;
489 }
490
491 int ShellDirectory::extract_icons(bool big_icons)
492 {
493 int cnt = 0;
494
495 for(Entry*entry=_down; entry; entry=entry->_next)
496 if (entry->_icon_id == ICID_UNKNOWN) {
497 entry->extract_icon(big_icons);
498
499 if (entry->_icon_id != ICID_NONE)
500 ++cnt;
501 }
502
503 return cnt;
504 }