2 * Copyright 2003, 2004 Martin Fuchs
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.
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.
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
21 // Explorer clone, lean version
25 // Martin Fuchs, 23.07.2003
29 #include "../utility/utility.h"
30 #include "../utility/shellclasses.h"
31 #include "../globals.h" // for _prescan_nodes
36 // allocate and initialise a directory entry
37 Entry::Entry(ENTRY_TYPE etype
)
47 _icon_id
= ICID_UNKNOWN
;
48 _display_name
= _data
.cFileName
;
53 Entry::Entry(Entry
* parent
, ENTRY_TYPE etype
)
63 _icon_id
= ICID_UNKNOWN
;
64 _display_name
= _data
.cFileName
;
69 Entry::Entry(const Entry
& other
)
79 _expanded
= other
._expanded
;
80 _scanned
= other
._scanned
;
81 _level
= other
._level
;
85 _shell_attribs
= other
._shell_attribs
;
86 _display_name
= other
._display_name
==other
._data
.cFileName
? _data
.cFileName
: _tcsdup(other
._display_name
);
87 _type_name
= other
._type_name
? _tcsdup(other
._type_name
): NULL
;
88 _content
= other
._content
? _tcsdup(other
._content
): NULL
;
90 _etype
= other
._etype
;
91 _icon_id
= other
._icon_id
;
94 _bhfi_valid
= other
._bhfi_valid
;
97 // free a directory entry
100 if (_icon_id
> ICID_NONE
)
101 g_Globals
._icon_cache
.free_icon(_icon_id
);
103 if (_display_name
!= _data
.cFileName
)
114 // read directory tree and expand to the given location
115 Entry
* Entry::read_tree(const void* path
, SORT_ORDER sortOrder
)
117 CONTEXT("Entry::read_tree()");
119 HCURSOR old_cursor
= SetCursor(LoadCursor(0, IDC_WAIT
));
122 Entry
* next_entry
= entry
;
124 for(const void*p
=path
; p
&&next_entry
; p
=entry
->get_next_path_component(p
)) {
127 entry
->read_directory(sortOrder
);
130 entry
->_expanded
= true;
132 next_entry
= entry
->find_entry(p
);
135 SetCursor(old_cursor
);
141 void Entry::read_directory(SORT_ORDER sortOrder
, int scan_flags
)
143 CONTEXT("Entry::read_directory(SORT_ORDER)");
145 // call into subclass
146 read_directory(scan_flags
);
148 if (g_Globals
._prescan_nodes
) { //@todo _prescan_nodes should not be used for reading the start menu.
149 for(Entry
*entry
=_down
; entry
; entry
=entry
->_next
)
150 if (entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
151 entry
->read_directory(scan_flags
);
152 entry
->sort_directory(sortOrder
);
156 sort_directory(sortOrder
);
162 memset(this, 0, sizeof(Root
));
168 _entry
->free_subentries();
172 // directories first...
173 static int compareType(const WIN32_FIND_DATA
* fd1
, const WIN32_FIND_DATA
* fd2
)
175 int dir1
= fd1
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
;
176 int dir2
= fd2
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
;
178 return dir2
==dir1
? 0: dir2
<dir1
? -1: 1;
182 static int compareNothing(const void* arg1
, const void* arg2
)
187 static int compareName(const void* arg1
, const void* arg2
)
189 const WIN32_FIND_DATA
* fd1
= &(*(Entry
**)arg1
)->_data
;
190 const WIN32_FIND_DATA
* fd2
= &(*(Entry
**)arg2
)->_data
;
192 int cmp
= compareType(fd1
, fd2
);
196 return lstrcmpi(fd1
->cFileName
, fd2
->cFileName
);
199 static int compareExt(const void* arg1
, const void* arg2
)
201 const WIN32_FIND_DATA
* fd1
= &(*(Entry
**)arg1
)->_data
;
202 const WIN32_FIND_DATA
* fd2
= &(*(Entry
**)arg2
)->_data
;
203 const TCHAR
*name1
, *name2
, *ext1
, *ext2
;
205 int cmp
= compareType(fd1
, fd2
);
209 name1
= fd1
->cFileName
;
210 name2
= fd2
->cFileName
;
212 ext1
= _tcsrchr(name1
, TEXT('.'));
213 ext2
= _tcsrchr(name2
, TEXT('.'));
225 cmp
= lstrcmpi(ext1
, ext2
);
229 return lstrcmpi(name1
, name2
);
232 static int compareSize(const void* arg1
, const void* arg2
)
234 WIN32_FIND_DATA
* fd1
= &(*(Entry
**)arg1
)->_data
;
235 WIN32_FIND_DATA
* fd2
= &(*(Entry
**)arg2
)->_data
;
237 int cmp
= compareType(fd1
, fd2
);
241 cmp
= fd2
->nFileSizeHigh
- fd1
->nFileSizeHigh
;
248 cmp
= fd2
->nFileSizeLow
- fd1
->nFileSizeLow
;
250 return cmp
<0? -1: cmp
>0? 1: 0;
253 static int compareDate(const void* arg1
, const void* arg2
)
255 WIN32_FIND_DATA
* fd1
= &(*(Entry
**)arg1
)->_data
;
256 WIN32_FIND_DATA
* fd2
= &(*(Entry
**)arg2
)->_data
;
258 int cmp
= compareType(fd1
, fd2
);
262 return CompareFileTime(&fd2
->ftLastWriteTime
, &fd1
->ftLastWriteTime
);
266 static int (*sortFunctions
[])(const void* arg1
, const void* arg2
) = {
267 compareNothing
, // SORT_NONE
268 compareName
, // SORT_NAME
269 compareExt
, // SORT_EXT
270 compareSize
, // SORT_SIZE
271 compareDate
// SORT_DATE
275 void Entry::sort_directory(SORT_ORDER sortOrder
)
277 if (sortOrder
!= SORT_NONE
) {
278 Entry
* entry
= _down
;
283 for(entry
=_down
; entry
; entry
=entry
->_next
)
287 array
= (Entry
**) alloca(len
*sizeof(Entry
*));
290 for(entry
=_down
; entry
; entry
=entry
->_next
)
293 // call qsort with the appropriate compare function
294 qsort(array
, len
, sizeof(array
[0]), sortFunctions
[sortOrder
]);
298 for(p
=array
; --len
; p
++)
307 void Entry::smart_scan(int scan_flags
)
309 CONTEXT("Entry::smart_scan()");
313 read_directory(SORT_NAME
, scan_flags
); // we could use IShellFolder2::GetDefaultColumn to determine sort order
318 void Entry::extract_icon()
320 TCHAR path
[MAX_PATH
];
322 ICON_ID icon_id
= ICID_NONE
;
325 icon_id
= g_Globals
._icon_cache
.extract(path
);
327 if (icon_id
== ICID_NONE
) {
328 IExtractIcon
* pExtract
;
329 if (SUCCEEDED(GetUIObjectOf(0, IID_IExtractIcon
, (LPVOID
*)&pExtract
))) {
333 if (SUCCEEDED(pExtract
->GetIconLocation(GIL_FORSHELL
, path
, MAX_PATH
, &idx
, &flags
))) {
334 if (flags
& GIL_NOTFILENAME
)
335 icon_id
= g_Globals
._icon_cache
.extract(pExtract
, path
, idx
);
338 idx
= 0; // special case for some control panel applications ("System")
340 icon_id
= g_Globals
._icon_cache
.extract(path
, idx
);
343 /* using create_absolute_pidl() [see below] results in more correct icons for some control panel applets ("NVidia").
344 if (icon_id == ICID_NONE) {
347 if (SHGetFileInfo(path, 0, &sfi, sizeof(sfi), SHGFI_ICON|SHGFI_SMALLICON))
348 icon_id = g_Globals._icon_cache.add(sfi.hIcon)._id;
351 if (icon_id == ICID_NONE) {
352 LPBYTE b = (LPBYTE) alloca(0x10000);
355 FILE* file = fopen(path, "rb");
357 int l = fread(b, 1, 0x10000, file);
361 icon_id = g_Globals._icon_cache.add(CreateIconFromResourceEx(b, l, TRUE, 0x00030000, 16, 16, LR_DEFAULTCOLOR));
367 if (icon_id
== ICID_NONE
) {
370 const ShellPath
& pidl_abs
= create_absolute_pidl();
371 LPCITEMIDLIST pidl
= pidl_abs
;
373 HIMAGELIST himlSys
= (HIMAGELIST
) SHGetFileInfo((LPCTSTR
)pidl
, 0, &sfi
, sizeof(sfi
), SHGFI_SYSICONINDEX
|SHGFI_PIDL
|SHGFI_SMALLICON
);
375 icon_id
= g_Globals
._icon_cache
.add(sfi
.iIcon
);
377 if (SHGetFileInfo((LPCTSTR)pidl, 0, &sfi, sizeof(sfi), SHGFI_PIDL|SHGFI_ICON|SHGFI_SMALLICON))
378 icon_id = g_Globals._icon_cache.add(sfi.hIcon)._id;
387 BOOL
Entry::launch_entry(HWND hwnd
, UINT nCmdShow
)
394 // add path to the recent file list
395 SHAddToRecentDocs(SHARD_PATH
, cmd
);
397 // start program, open document...
398 return launch_file(hwnd
, cmd
, nCmdShow
);
402 HRESULT
Entry::GetUIObjectOf(HWND hWnd
, REFIID riid
, LPVOID
* ppvOut
)
404 TCHAR path
[MAX_PATH
];
409 ShellPath shell_path(path);
411 IShellFolder* pFolder;
412 LPCITEMIDLIST pidl_last = NULL;
414 static DynamicFct<HRESULT(WINAPI*)(LPCITEMIDLIST, REFIID, LPVOID*, LPCITEMIDLIST*)> SHBindToParent(TEXT("SHELL32"), "SHBindToParent");
419 HRESULT hr = (*SHBindToParent)(shell_path, IID_IShellFolder, (LPVOID*)&pFolder, &pidl_last);
423 ShellFolder shell_folder(pFolder);
425 shell_folder->Release();
427 return shell_folder->GetUIObjectOf(hWnd, 1, &pidl_last, riid, NULL, ppvOut);
432 if (!_up
->get_path(path
))
435 ShellPath
shell_path(path
);
436 ShellFolder
shell_folder(shell_path
);
439 LPWSTR wname
= _data
.cFileName
;
441 WCHAR wname
[MAX_PATH
];
442 MultiByteToWideChar(CP_ACP
, 0, _data
.cFileName
, -1, wname
, MAX_PATH
);
445 LPITEMIDLIST pidl_last
= NULL
;
446 HRESULT hr
= shell_folder
->ParseDisplayName(hWnd
, NULL
, wname
, NULL
, &pidl_last
, NULL
);
451 hr
= shell_folder
->GetUIObjectOf(hWnd
, 1, (LPCITEMIDLIST
*)&pidl_last
, riid
, NULL
, ppvOut
);
453 ShellMalloc()->Free((void*)pidl_last
);
459 // recursively free all child entries
460 void Entry::free_subentries()
462 Entry
*entry
, *next
=_down
;
471 entry
->free_subentries();
478 const void* Directory::get_next_path_component(const void* p
)
480 LPCTSTR s
= (LPCTSTR
) p
;
482 while(*s
&& *s
!=TEXT('\\') && *s
!=TEXT('/'))
485 while(*s
==TEXT('\\') || *s
==TEXT('/'))