2 * Copyright 2003 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
29 #include "../utility/utility.h"
30 #include "../utility/shellclasses.h"
31 #include "../globals.h"
36 // allocate and initialise a directory entry
37 Entry::Entry(ENTRY_TYPE etype
)
49 Entry::Entry(Entry
* parent
)
50 : _etype(parent
->_etype
),
61 // free a directory entry
64 if (_hicon
&& _hicon
!=(HICON
)-1)
69 // read directory tree and expand to the given location
70 Entry
* Entry::read_tree(const void* path
, SORT_ORDER sortOrder
)
72 HCURSOR old_cursor
= SetCursor(LoadCursor(0, IDC_WAIT
));
75 Entry
* next_entry
= entry
;
77 for(const void*p
=path
; p
&&next_entry
; p
=entry
->get_next_path_component(p
)) {
80 entry
->read_directory(sortOrder
);
83 entry
->_expanded
= true;
85 next_entry
= entry
->find_entry(p
);
88 SetCursor(old_cursor
);
94 void Entry::read_directory(SORT_ORDER sortOrder
)
99 if (g_Globals
._prescan_nodes
) {
100 for(Entry
*entry
=_down
; entry
; entry
=entry
->_next
)
101 if (entry
->_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
) {
102 entry
->read_directory();
103 entry
->sort_directory(sortOrder
);
107 sort_directory(sortOrder
);
113 memset(this, 0, sizeof(Root
));
119 _entry
->free_subentries();
123 // directories first...
124 static int compareType(const WIN32_FIND_DATA
* fd1
, const WIN32_FIND_DATA
* fd2
)
126 int dir1
= fd1
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
;
127 int dir2
= fd2
->dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
;
129 return dir2
==dir1
? 0: dir2
<dir1
? -1: 1;
133 static int compareName(const void* arg1
, const void* arg2
)
135 const WIN32_FIND_DATA
* fd1
= &(*(Entry
**)arg1
)->_data
;
136 const WIN32_FIND_DATA
* fd2
= &(*(Entry
**)arg2
)->_data
;
138 int cmp
= compareType(fd1
, fd2
);
142 return lstrcmpi(fd1
->cFileName
, fd2
->cFileName
);
145 static int compareExt(const void* arg1
, const void* arg2
)
147 const WIN32_FIND_DATA
* fd1
= &(*(Entry
**)arg1
)->_data
;
148 const WIN32_FIND_DATA
* fd2
= &(*(Entry
**)arg2
)->_data
;
149 const TCHAR
*name1
, *name2
, *ext1
, *ext2
;
151 int cmp
= compareType(fd1
, fd2
);
155 name1
= fd1
->cFileName
;
156 name2
= fd2
->cFileName
;
158 ext1
= _tcsrchr(name1
, TEXT('.'));
159 ext2
= _tcsrchr(name2
, TEXT('.'));
171 cmp
= lstrcmpi(ext1
, ext2
);
175 return lstrcmpi(name1
, name2
);
178 static int compareSize(const void* arg1
, const void* arg2
)
180 WIN32_FIND_DATA
* fd1
= &(*(Entry
**)arg1
)->_data
;
181 WIN32_FIND_DATA
* fd2
= &(*(Entry
**)arg2
)->_data
;
183 int cmp
= compareType(fd1
, fd2
);
187 cmp
= fd2
->nFileSizeHigh
- fd1
->nFileSizeHigh
;
194 cmp
= fd2
->nFileSizeLow
- fd1
->nFileSizeLow
;
196 return cmp
<0? -1: cmp
>0? 1: 0;
199 static int compareDate(const void* arg1
, const void* arg2
)
201 WIN32_FIND_DATA
* fd1
= &(*(Entry
**)arg1
)->_data
;
202 WIN32_FIND_DATA
* fd2
= &(*(Entry
**)arg2
)->_data
;
204 int cmp
= compareType(fd1
, fd2
);
208 return CompareFileTime(&fd2
->ftLastWriteTime
, &fd1
->ftLastWriteTime
);
212 static int (*sortFunctions
[])(const void* arg1
, const void* arg2
) = {
213 compareName
, // SORT_NAME
214 compareExt
, // SORT_EXT
215 compareSize
, // SORT_SIZE
216 compareDate
// SORT_DATE
220 void Entry::sort_directory(SORT_ORDER sortOrder
)
222 Entry
* entry
= _down
;
227 for(entry
=_down
; entry
; entry
=entry
->_next
)
231 array
= (Entry
**) alloca(len
*sizeof(Entry
*));
234 for(entry
=_down
; entry
; entry
=entry
->_next
)
237 // call qsort with the appropriate compare function
238 qsort(array
, len
, sizeof(array
[0]), sortFunctions
[sortOrder
]);
242 for(p
=array
; --len
; p
++)
250 BOOL
Entry::launch_entry(HWND hwnd
, UINT nCmdShow
)
256 // start program, open document...
257 return launch_file(hwnd
, cmd
, nCmdShow
);
261 // recursively free all child entries
262 void Entry::free_subentries()
264 Entry
*entry
, *next
=_down
;
273 entry
->free_subentries();