Context Trace Outputs for Exceptions
[reactos.git] / reactos / subsys / system / explorer / shell / entries.cpp
1 /*
2 * Copyright 2003 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 // entries.cpp
24 //
25 // Martin Fuchs, 23.07.2003
26 //
27
28
29 #include "../utility/utility.h"
30 #include "../utility/shellclasses.h"
31 #include "../globals.h" // for _prescan_nodes
32
33 #include "entries.h"
34
35
36 // allocate and initialise a directory entry
37 Entry::Entry(ENTRY_TYPE etype)
38 : _etype(etype)
39 {
40 _up = NULL;
41 _next = NULL;
42 _down = NULL;
43 _expanded = false;
44 _scanned = false;
45 _bhfi_valid = false;
46 _level = 0;
47 _hIcon = 0;
48 _display_name = _data.cFileName;
49 }
50
51 Entry::Entry(Entry* parent)
52 : _up(parent),
53 _etype(parent->_etype)
54 {
55 _next = NULL;
56 _down = NULL;
57 _expanded = false;
58 _scanned = false;
59 _bhfi_valid = false;
60 _level = 0;
61 _hIcon = 0;
62 _display_name = _data.cFileName;
63 }
64
65 Entry::Entry(const Entry& other)
66 {
67 _next = NULL;
68 _down = NULL;
69 _up = NULL;
70
71 assert(!other._next);
72 assert(!other._down);
73 assert(!other._up);
74
75 _expanded = other._expanded;
76 _scanned = other._scanned;
77 _level = other._level;
78
79 _data = other._data;
80
81 _shell_attribs = other._shell_attribs;
82 _display_name = other._display_name==other._data.cFileName? _data.cFileName: _tcsdup(other._display_name);
83
84 _etype = other._etype;
85 _hIcon = other._hIcon;
86
87 _bhfi = other._bhfi;
88 _bhfi_valid = other._bhfi_valid;
89 }
90
91 // free a directory entry
92 Entry::~Entry()
93 {
94 if (_hIcon && _hIcon!=(HICON)-1)
95 DestroyIcon(_hIcon);
96
97 if (_display_name != _data.cFileName)
98 free(_display_name);
99 }
100
101
102 // read directory tree and expand to the given location
103 Entry* Entry::read_tree(const void* path, SORT_ORDER sortOrder)
104 {
105 CONTEXT("Entry::read_tree()");
106
107 HCURSOR old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
108
109 Entry* entry = this;
110 Entry* next_entry = entry;
111
112 for(const void*p=path; p&&next_entry; p=entry->get_next_path_component(p)) {
113 entry = next_entry;
114
115 entry->read_directory(sortOrder);
116
117 if (entry->_down)
118 entry->_expanded = true;
119
120 next_entry = entry->find_entry(p);
121 }
122
123 SetCursor(old_cursor);
124
125 return entry;
126 }
127
128
129 void Entry::read_directory(SORT_ORDER sortOrder)
130 {
131 CONTEXT("Entry::read_directory(SORT_ORDER)");
132
133 // call into subclass
134 read_directory();
135
136 if (g_Globals._prescan_nodes) {
137 for(Entry*entry=_down; entry; entry=entry->_next)
138 if (entry->_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
139 entry->read_directory();
140 entry->sort_directory(sortOrder);
141 }
142 }
143
144 sort_directory(sortOrder);
145 }
146
147
148 Root::Root()
149 {
150 memset(this, 0, sizeof(Root));
151 }
152
153 Root::~Root()
154 {
155 if (_entry)
156 _entry->free_subentries();
157 }
158
159
160 // directories first...
161 static int compareType(const WIN32_FIND_DATA* fd1, const WIN32_FIND_DATA* fd2)
162 {
163 int dir1 = fd1->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
164 int dir2 = fd2->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
165
166 return dir2==dir1? 0: dir2<dir1? -1: 1;
167 }
168
169
170 static int compareName(const void* arg1, const void* arg2)
171 {
172 const WIN32_FIND_DATA* fd1 = &(*(Entry**)arg1)->_data;
173 const WIN32_FIND_DATA* fd2 = &(*(Entry**)arg2)->_data;
174
175 int cmp = compareType(fd1, fd2);
176 if (cmp)
177 return cmp;
178
179 return lstrcmpi(fd1->cFileName, fd2->cFileName);
180 }
181
182 static int compareExt(const void* arg1, const void* arg2)
183 {
184 const WIN32_FIND_DATA* fd1 = &(*(Entry**)arg1)->_data;
185 const WIN32_FIND_DATA* fd2 = &(*(Entry**)arg2)->_data;
186 const TCHAR *name1, *name2, *ext1, *ext2;
187
188 int cmp = compareType(fd1, fd2);
189 if (cmp)
190 return cmp;
191
192 name1 = fd1->cFileName;
193 name2 = fd2->cFileName;
194
195 ext1 = _tcsrchr(name1, TEXT('.'));
196 ext2 = _tcsrchr(name2, TEXT('.'));
197
198 if (ext1)
199 ++ext1;
200 else
201 ext1 = TEXT("");
202
203 if (ext2)
204 ++ext2;
205 else
206 ext2 = TEXT("");
207
208 cmp = lstrcmpi(ext1, ext2);
209 if (cmp)
210 return cmp;
211
212 return lstrcmpi(name1, name2);
213 }
214
215 static int compareSize(const void* arg1, const void* arg2)
216 {
217 WIN32_FIND_DATA* fd1 = &(*(Entry**)arg1)->_data;
218 WIN32_FIND_DATA* fd2 = &(*(Entry**)arg2)->_data;
219
220 int cmp = compareType(fd1, fd2);
221 if (cmp)
222 return cmp;
223
224 cmp = fd2->nFileSizeHigh - fd1->nFileSizeHigh;
225
226 if (cmp < 0)
227 return -1;
228 else if (cmp > 0)
229 return 1;
230
231 cmp = fd2->nFileSizeLow - fd1->nFileSizeLow;
232
233 return cmp<0? -1: cmp>0? 1: 0;
234 }
235
236 static int compareDate(const void* arg1, const void* arg2)
237 {
238 WIN32_FIND_DATA* fd1 = &(*(Entry**)arg1)->_data;
239 WIN32_FIND_DATA* fd2 = &(*(Entry**)arg2)->_data;
240
241 int cmp = compareType(fd1, fd2);
242 if (cmp)
243 return cmp;
244
245 return CompareFileTime(&fd2->ftLastWriteTime, &fd1->ftLastWriteTime);
246 }
247
248
249 static int (*sortFunctions[])(const void* arg1, const void* arg2) = {
250 compareName, // SORT_NAME
251 compareExt, // SORT_EXT
252 compareSize, // SORT_SIZE
253 compareDate // SORT_DATE
254 };
255
256
257 void Entry::sort_directory(SORT_ORDER sortOrder)
258 {
259 Entry* entry = _down;
260 Entry** array, **p;
261 int len;
262
263 len = 0;
264 for(entry=_down; entry; entry=entry->_next)
265 ++len;
266
267 if (len) {
268 array = (Entry**) alloca(len*sizeof(Entry*));
269
270 p = array;
271 for(entry=_down; entry; entry=entry->_next)
272 *p++ = entry;
273
274 // call qsort with the appropriate compare function
275 qsort(array, len, sizeof(array[0]), sortFunctions[sortOrder]);
276
277 _down = array[0];
278
279 for(p=array; --len; p++)
280 p[0]->_next = p[1];
281
282 (*p)->_next = 0;
283 }
284 }
285
286
287 void Entry::smart_scan()
288 {
289 CONTEXT("Entry::smart_scan()");
290
291 if (!_scanned) {
292 free_subentries();
293 read_directory(SORT_NAME); // we could use IShellFolder2::GetDefaultColumn to determine sort order
294 }
295 }
296
297
298 BOOL Entry::launch_entry(HWND hwnd, UINT nCmdShow)
299 {
300 TCHAR cmd[MAX_PATH];
301
302 get_path(cmd);
303
304 // start program, open document...
305 return launch_file(hwnd, cmd, nCmdShow);
306 }
307
308
309 // recursively free all child entries
310 void Entry::free_subentries()
311 {
312 Entry *entry, *next=_down;
313
314 if (next) {
315 _down = 0;
316
317 do {
318 entry = next;
319 next = entry->_next;
320
321 entry->free_subentries();
322 delete entry;
323 } while(next);
324 }
325 }