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
25 // Martin Fuchs, 23.07.2003
31 //#include "entries.h"
34 // allocate and initialise a directory entry
35 Entry::Entry(ENTRY_TYPE etype
)
45 _icon_id
= ICID_UNKNOWN
;
46 _display_name
= _data
.cFileName
;
51 Entry::Entry(Entry
* parent
, ENTRY_TYPE etype
)
61 _icon_id
= ICID_UNKNOWN
;
62 _display_name
= _data
.cFileName
;
67 Entry::Entry(const Entry
& other
)
77 _expanded
= other
._expanded
;
78 _scanned
= other
._scanned
;
79 _level
= other
._level
;
83 _shell_attribs
= other
._shell_attribs
;
84 _display_name
= other
._display_name
==other
._data
.cFileName
? _data
.cFileName
: _tcsdup(other
._display_name
);
85 _type_name
= other
._type_name
? _tcsdup(other
._type_name
): NULL
;
86 _content
= other
._content
? _tcsdup(other
._content
): NULL
;
88 _etype
= other
._etype
;
89 _icon_id
= other
._icon_id
;
92 _bhfi_valid
= other
._bhfi_valid
;
95 // free a directory entry
98 if (_icon_id
> ICID_NONE
)
99 g_Globals
._icon_cache
.free_icon(_icon_id
);
101 if (_display_name
!= _data
.cFileName
)
112 // read directory tree and expand to the given location
113 Entry
* Entry::read_tree(const void* path
, SORT_ORDER sortOrder
, int scan_flags
)
115 CONTEXT("Entry::read_tree()");
121 for(const void*p
=path
; p
&& entry
; ) {
122 entry
->smart_scan(sortOrder
, scan_flags
);
125 entry
->_expanded
= true;
127 Entry
* found
= entry
->find_entry(p
);
128 p
= entry
->get_next_path_component(p
);
137 void Entry::read_directory_base(SORT_ORDER sortOrder
, int scan_flags
)
139 CONTEXT("Entry::read_directory_base()");
141 // call into subclass
142 read_directory(scan_flags
);
145 if (g_Globals
._prescan_nodes
) { //@todo _prescan_nodes should not be used for reading the start menu.
146 for(Entry
*entry
=_down
; entry
; entry
=entry
->_next
)
147 if (entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
148 entry
->read_directory(scan_flags
);
149 entry
->sort_directory(sortOrder
);
154 sort_directory(sortOrder
);
160 memset(this, 0, sizeof(Root
));
166 _entry
->free_subentries();
170 // directories first...
171 static int compareType(const WIN32_FIND_DATA
* fd1
, const WIN32_FIND_DATA
* fd2
)
173 int order1
= fd1
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
;
174 int order2
= fd2
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
;
176 /* Handle "." and ".." as special case and move them at the very first beginning. */
177 if (order1
&& order2
) {
178 order1
= fd1
->cFileName
[0]!='.'? 1: fd1
->cFileName
[1]=='.'? 2: fd1
->cFileName
[1]=='\0'? 3: 1;
179 order2
= fd2
->cFileName
[0]!='.'? 1: fd2
->cFileName
[1]=='.'? 2: fd2
->cFileName
[1]=='\0'? 3: 1;
182 return order2
==order1
? 0: order2
<order1
? -1: 1;
186 static int compareNothing(const void* arg1
, const void* arg2
)
191 static int compareName(const void* arg1
, const void* arg2
)
193 const WIN32_FIND_DATA
* fd1
= &(*(Entry
**)arg1
)->_data
;
194 const WIN32_FIND_DATA
* fd2
= &(*(Entry
**)arg2
)->_data
;
196 int cmp
= compareType(fd1
, fd2
);
200 return lstrcmpi(fd1
->cFileName
, fd2
->cFileName
);
203 static int compareExt(const void* arg1
, const void* arg2
)
205 const WIN32_FIND_DATA
* fd1
= &(*(Entry
**)arg1
)->_data
;
206 const WIN32_FIND_DATA
* fd2
= &(*(Entry
**)arg2
)->_data
;
207 const TCHAR
*name1
, *name2
, *ext1
, *ext2
;
209 int cmp
= compareType(fd1
, fd2
);
213 name1
= fd1
->cFileName
;
214 name2
= fd2
->cFileName
;
216 ext1
= _tcsrchr(name1
, TEXT('.'));
217 ext2
= _tcsrchr(name2
, TEXT('.'));
229 cmp
= lstrcmpi(ext1
, ext2
);
233 return lstrcmpi(name1
, name2
);
236 static int compareSize(const void* arg1
, const void* arg2
)
238 WIN32_FIND_DATA
* fd1
= &(*(Entry
**)arg1
)->_data
;
239 WIN32_FIND_DATA
* fd2
= &(*(Entry
**)arg2
)->_data
;
241 int cmp
= compareType(fd1
, fd2
);
245 cmp
= fd2
->nFileSizeHigh
- fd1
->nFileSizeHigh
;
252 cmp
= fd2
->nFileSizeLow
- fd1
->nFileSizeLow
;
254 return cmp
<0? -1: cmp
>0? 1: 0;
257 static int compareDate(const void* arg1
, const void* arg2
)
259 WIN32_FIND_DATA
* fd1
= &(*(Entry
**)arg1
)->_data
;
260 WIN32_FIND_DATA
* fd2
= &(*(Entry
**)arg2
)->_data
;
262 int cmp
= compareType(fd1
, fd2
);
266 return CompareFileTime(&fd2
->ftLastWriteTime
, &fd1
->ftLastWriteTime
);
270 static int (*sortFunctions
[])(const void* arg1
, const void* arg2
) = {
271 compareNothing
, // SORT_NONE
272 compareName
, // SORT_NAME
273 compareExt
, // SORT_EXT
274 compareSize
, // SORT_SIZE
275 compareDate
// SORT_DATE
279 void Entry::sort_directory(SORT_ORDER sortOrder
)
281 if (sortOrder
!= SORT_NONE
) {
282 Entry
* entry
= _down
;
287 for(entry
=_down
; entry
; entry
=entry
->_next
)
291 array
= (Entry
**) alloca(len
*sizeof(Entry
*));
294 for(entry
=_down
; entry
; entry
=entry
->_next
)
297 // call qsort with the appropriate compare function
298 qsort(array
, len
, sizeof(array
[0]), sortFunctions
[sortOrder
]);
302 for(p
=array
; --len
; p
++)
311 void Entry::smart_scan(SORT_ORDER sortOrder
, int scan_flags
)
313 CONTEXT("Entry::smart_scan()");
317 read_directory_base(sortOrder
, scan_flags
); ///@todo We could use IShellFolder2::GetDefaultColumn to determine sort order.
322 void Entry::extract_icon()
324 TCHAR path
[MAX_PATH
];
326 ICON_ID icon_id
= ICID_NONE
;
328 if (get_path(path
) && _tcsncmp(path
,TEXT("::{"),3))
329 icon_id
= g_Globals
._icon_cache
.extract(path
);
331 if (icon_id
== ICID_NONE
) {
332 IExtractIcon
* pExtract
;
333 if (SUCCEEDED(GetUIObjectOf(0, IID_IExtractIcon
, (LPVOID
*)&pExtract
))) {
337 if (SUCCEEDED(pExtract
->GetIconLocation(GIL_FORSHELL
, path
, MAX_PATH
, &idx
, &flags
))) {
338 if (flags
& GIL_NOTFILENAME
)
339 icon_id
= g_Globals
._icon_cache
.extract(pExtract
, path
, idx
);
342 idx
= 0; // special case for some control panel applications ("System")
344 icon_id
= g_Globals
._icon_cache
.extract(path
, idx
);
347 /* using create_absolute_pidl() [see below] results in more correct icons for some control panel applets ("NVidia").
348 if (icon_id == ICID_NONE) {
351 if (SHGetFileInfo(path, 0, &sfi, sizeof(sfi), SHGFI_ICON|SHGFI_SMALLICON))
352 icon_id = g_Globals._icon_cache.add(sfi.hIcon)._id;
355 if (icon_id == ICID_NONE) {
356 LPBYTE b = (LPBYTE) alloca(0x10000);
359 FILE* file = fopen(path, "rb");
361 int l = fread(b, 1, 0x10000, file);
365 icon_id = g_Globals._icon_cache.add(CreateIconFromResourceEx(b, l, TRUE, 0x00030000, 16, 16, LR_DEFAULTCOLOR));
371 if (icon_id
== ICID_NONE
) {
374 const ShellPath
& pidl_abs
= create_absolute_pidl();
375 LPCITEMIDLIST pidl
= pidl_abs
;
377 HIMAGELIST himlSys
= (HIMAGELIST
) SHGetFileInfo((LPCTSTR
)pidl
, 0, &sfi
, sizeof(sfi
), SHGFI_SYSICONINDEX
|SHGFI_PIDL
|SHGFI_SMALLICON
);
379 icon_id
= g_Globals
._icon_cache
.add(sfi
.iIcon
);
381 if (SHGetFileInfo((LPCTSTR)pidl, 0, &sfi, sizeof(sfi), SHGFI_PIDL|SHGFI_ICON|SHGFI_SMALLICON))
382 icon_id = g_Globals._icon_cache.add(sfi.hIcon)._id;
391 BOOL
Entry::launch_entry(HWND hwnd
, UINT nCmdShow
)
398 // add path to the recent file list
399 SHAddToRecentDocs(SHARD_PATH
, cmd
);
401 // start program, open document...
402 return launch_file(hwnd
, cmd
, nCmdShow
);
406 HRESULT
Entry::do_context_menu(HWND hwnd
, const POINT
& pos
, CtxMenuInterfaces
& cm_ifs
)
408 ShellPath shell_path
= create_absolute_pidl();
409 LPCITEMIDLIST pidl_abs
= shell_path
;
412 return S_FALSE
; // no action for registry entries, etc.
414 static DynamicFct
<HRESULT(WINAPI
*)(LPCITEMIDLIST
, REFIID
, LPVOID
*, LPCITEMIDLIST
*)> SHBindToParent(TEXT("SHELL32"), "SHBindToParent");
416 if (SHBindToParent
) {
417 IShellFolder
* parentFolder
;
418 LPCITEMIDLIST pidlLast
;
420 // get and use the parent folder to display correct context menu in all cases -> correct "Properties" dialog for directories, ...
421 HRESULT hr
= (*SHBindToParent
)(pidl_abs
, IID_IShellFolder
, (LPVOID
*)&parentFolder
, &pidlLast
);
424 hr
= ShellFolderContextMenu(parentFolder
, hwnd
, 1, &pidlLast
, pos
.x
, pos
.y
, cm_ifs
);
426 parentFolder
->Release();
431 /**@todo use parent folder instead of desktop folder
434 ShellPath parent_path;
437 parent_path = dir->create_absolute_pidl();
439 parent_path = DesktopFolderPath();
441 ShellPath shell_path = create_relative_pidl(parent_path);
442 LPCITEMIDLIST pidl = shell_path;
444 ShellFolder parent_folder = parent_path;
445 return ShellFolderContextMenu(parent_folder, hwnd, 1, &pidl, pos.x, pos.y);
447 return ShellFolderContextMenu(GetDesktopFolder(), hwnd
, 1, &pidl_abs
, pos
.x
, pos
.y
, cm_ifs
);
452 HRESULT
Entry::GetUIObjectOf(HWND hWnd
, REFIID riid
, LPVOID
* ppvOut
)
454 TCHAR path
[MAX_PATH
];
459 ShellPath shell_path(path);
461 IShellFolder* pFolder;
462 LPCITEMIDLIST pidl_last = NULL;
464 static DynamicFct<HRESULT(WINAPI*)(LPCITEMIDLIST, REFIID, LPVOID*, LPCITEMIDLIST*)> SHBindToParent(TEXT("SHELL32"), "SHBindToParent");
469 HRESULT hr = (*SHBindToParent)(shell_path, IID_IShellFolder, (LPVOID*)&pFolder, &pidl_last);
473 ShellFolder shell_folder(pFolder);
475 shell_folder->Release();
477 return shell_folder->GetUIObjectOf(hWnd, 1, &pidl_last, riid, NULL, ppvOut);
482 if (!_up
->get_path(path
))
485 ShellPath
shell_path(path
);
486 ShellFolder
shell_folder(shell_path
);
489 LPWSTR wname
= _data
.cFileName
;
491 WCHAR wname
[MAX_PATH
];
492 MultiByteToWideChar(CP_ACP
, 0, _data
.cFileName
, -1, wname
, MAX_PATH
);
495 LPITEMIDLIST pidl_last
= NULL
;
496 HRESULT hr
= shell_folder
->ParseDisplayName(hWnd
, NULL
, wname
, NULL
, &pidl_last
, NULL
);
501 hr
= shell_folder
->GetUIObjectOf(hWnd
, 1, (LPCITEMIDLIST
*)&pidl_last
, riid
, NULL
, ppvOut
);
503 ShellMalloc()->Free((void*)pidl_last
);
509 // recursively free all child entries
510 void Entry::free_subentries()
512 Entry
*entry
, *next
=_down
;
521 entry
->free_subentries();
528 Entry
* Root::read_tree(LPCTSTR path
, int scan_flags
)
533 entry
= _entry
->read_tree(path
, _sort_order
);
535 entry
= _entry
->read_tree(NULL
, _sort_order
);
537 _entry
->smart_scan();
540 _entry
->_expanded
= true;
547 Entry
* Root::read_tree(LPCITEMIDLIST pidl
, int scan_flags
)
549 return _entry
->read_tree(pidl
, _sort_order
);