]>
Commit | Line | Data |
---|---|---|
7183fd72 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/msw/volume.cpp | |
3 | // Purpose: wxFSVolume - encapsulates system volume information | |
4 | // Author: George Policello | |
5 | // Modified by: | |
6 | // Created: 28 Jan 02 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2002 George Policello | |
65571936 | 9 | // Licence: wxWindows licence |
7183fd72 VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
7183fd72 VZ |
20 | #include "wx/wxprec.h" |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
05815ab3 VZ |
26 | #if wxUSE_FSVOLUME |
27 | ||
7212d155 PC |
28 | #include "wx/volume.h" |
29 | ||
7183fd72 | 30 | #ifndef WX_PRECOMP |
df5168c4 MB |
31 | #if wxUSE_GUI |
32 | #include "wx/icon.h" | |
33 | #endif | |
05815ab3 | 34 | #include "wx/intl.h" |
df69528b | 35 | #include "wx/hashmap.h" |
7183fd72 VZ |
36 | #endif // WX_PRECOMP |
37 | ||
38 | #include "wx/dir.h" | |
7183fd72 VZ |
39 | #include "wx/dynlib.h" |
40 | #include "wx/arrimpl.cpp" | |
41 | ||
eaeeb91e | 42 | #include <shellapi.h> |
a6c7a0f8 | 43 | #include <shlobj.h> |
7212d155 | 44 | #include "wx/msw/missing.h" |
eaeeb91e | 45 | |
ec67cff1 | 46 | #if wxUSE_BASE |
7cfdeaad | 47 | |
7183fd72 VZ |
48 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
49 | // Dynamic library function defs. | |
50 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
51 | ||
64c288fa | 52 | #if wxUSE_DYNLIB_CLASS |
7183fd72 | 53 | static wxDynamicLibrary s_mprLib; |
64c288fa | 54 | #endif |
7183fd72 VZ |
55 | |
56 | typedef DWORD (WINAPI* WNetOpenEnumPtr)(DWORD, DWORD, DWORD, LPNETRESOURCE, LPHANDLE); | |
57 | typedef DWORD (WINAPI* WNetEnumResourcePtr)(HANDLE, LPDWORD, LPVOID, LPDWORD); | |
58 | typedef DWORD (WINAPI* WNetCloseEnumPtr)(HANDLE); | |
59 | ||
60 | static WNetOpenEnumPtr s_pWNetOpenEnum; | |
61 | static WNetEnumResourcePtr s_pWNetEnumResource; | |
62 | static WNetCloseEnumPtr s_pWNetCloseEnum; | |
63 | ||
64 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
65 | // Globals/Statics | |
66 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
67 | static long s_cancelSearch = FALSE; | |
68 | ||
17ebae65 | 69 | struct FileInfo |
7183fd72 VZ |
70 | { |
71 | FileInfo(unsigned flag=0, wxFSVolumeKind type=wxFS_VOL_OTHER) : | |
72 | m_flags(flag), m_type(type) {} | |
51fdd3da VZ |
73 | |
74 | FileInfo(const FileInfo& other) { *this = other; } | |
75 | FileInfo& operator=(const FileInfo& other) | |
76 | { | |
77 | m_flags = other.m_flags; | |
78 | m_type = other.m_type; | |
79 | return *this; | |
80 | } | |
81 | ||
7183fd72 VZ |
82 | unsigned m_flags; |
83 | wxFSVolumeKind m_type; | |
84 | }; | |
85 | WX_DECLARE_STRING_HASH_MAP(FileInfo, FileInfoMap); | |
17ebae65 MB |
86 | // Cygwin bug (?) destructor for global s_fileInfo is called twice... |
87 | static FileInfoMap& GetFileInfoMap() | |
88 | { | |
89 | static FileInfoMap s_fileInfo(25); | |
90 | ||
91 | return s_fileInfo; | |
92 | } | |
93 | #define s_fileInfo (GetFileInfoMap()) | |
7183fd72 | 94 | |
7183fd72 VZ |
95 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
96 | // Local helper functions. | |
97 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
98 | ||
99 | //============================================================================= | |
100 | // Function: GetBasicFlags | |
101 | // Purpose: Set basic flags, primarily wxFS_VOL_REMOTE and wxFS_VOL_REMOVABLE. | |
102 | // Notes: - Local and mapped drives are mounted by definition. We have no | |
103 | // way to determine mounted status of network drives, so assume that | |
104 | // all drives are mounted, and let the caller decide otherwise. | |
105 | // - Other flags are 'best guess' from type of drive. The system will | |
106 | // not report the file attributes with any degree of accuracy. | |
107 | //============================================================================= | |
4d90b8de | 108 | static unsigned GetBasicFlags(const wxChar* filename) |
7183fd72 VZ |
109 | { |
110 | unsigned flags = wxFS_VOL_MOUNTED; | |
111 | ||
112 | //---------------------------------- | |
113 | // 'Best Guess' based on drive type. | |
114 | //---------------------------------- | |
115 | wxFSVolumeKind type; | |
116 | switch(GetDriveType(filename)) | |
117 | { | |
118 | case DRIVE_FIXED: | |
119 | type = wxFS_VOL_DISK; | |
120 | break; | |
121 | ||
122 | case DRIVE_REMOVABLE: | |
123 | flags |= wxFS_VOL_REMOVABLE; | |
124 | type = wxFS_VOL_FLOPPY; | |
125 | break; | |
126 | ||
127 | case DRIVE_CDROM: | |
128 | flags |= wxFS_VOL_REMOVABLE | wxFS_VOL_READONLY; | |
129 | type = wxFS_VOL_CDROM; | |
130 | break; | |
131 | ||
132 | case DRIVE_REMOTE: | |
133 | flags |= wxFS_VOL_REMOTE; | |
134 | type = wxFS_VOL_NETWORK; | |
135 | break; | |
136 | ||
137 | case DRIVE_NO_ROOT_DIR: | |
138 | flags &= ~wxFS_VOL_MOUNTED; | |
139 | type = wxFS_VOL_OTHER; | |
140 | break; | |
141 | ||
142 | default: | |
143 | type = wxFS_VOL_OTHER; | |
144 | break; | |
145 | } | |
146 | ||
147 | //----------------------------------------------------------------------- | |
90e572f1 | 148 | // The following most likely will not modify anything not set above, |
7183fd72 VZ |
149 | // and will not work at all for network shares or empty CD ROM drives. |
150 | // But it is a good check if the Win API ever gets better about reporting | |
151 | // this information. | |
152 | //----------------------------------------------------------------------- | |
153 | SHFILEINFO fi; | |
154 | long rc; | |
155 | rc = SHGetFileInfo(filename, 0, &fi, sizeof(fi), SHGFI_ATTRIBUTES ); | |
156 | if (!rc) | |
157 | { | |
158 | wxLogError(_("Cannot read typename from '%s'!"), filename); | |
159 | } | |
160 | else | |
161 | { | |
162 | if (fi.dwAttributes & SFGAO_READONLY) | |
163 | flags |= wxFS_VOL_READONLY; | |
164 | if (fi.dwAttributes & SFGAO_REMOVABLE) | |
165 | flags |= wxFS_VOL_REMOVABLE; | |
166 | } | |
167 | ||
168 | //------------------ | |
169 | // Flags are cached. | |
170 | //------------------ | |
171 | s_fileInfo[filename] = FileInfo(flags, type); | |
172 | ||
173 | return flags; | |
174 | } // GetBasicFlags | |
175 | ||
176 | //============================================================================= | |
177 | // Function: FilteredAdd | |
178 | // Purpose: Add a file to the list if it meets the filter requirement. | |
179 | // Notes: - See GetBasicFlags for remarks about the Mounted flag. | |
180 | //============================================================================= | |
27d2dbbc | 181 | static bool FilteredAdd(wxArrayString& list, const wxChar* filename, |
4d90b8de | 182 | unsigned flagsSet, unsigned flagsUnset) |
7183fd72 | 183 | { |
27d2dbbc | 184 | bool accept = true; |
7183fd72 VZ |
185 | unsigned flags = GetBasicFlags(filename); |
186 | ||
187 | if (flagsSet & wxFS_VOL_MOUNTED && !(flags & wxFS_VOL_MOUNTED)) | |
27d2dbbc | 188 | accept = false; |
7183fd72 | 189 | else if (flagsUnset & wxFS_VOL_MOUNTED && (flags & wxFS_VOL_MOUNTED)) |
27d2dbbc | 190 | accept = false; |
7183fd72 | 191 | else if (flagsSet & wxFS_VOL_REMOVABLE && !(flags & wxFS_VOL_REMOVABLE)) |
27d2dbbc | 192 | accept = false; |
7183fd72 | 193 | else if (flagsUnset & wxFS_VOL_REMOVABLE && (flags & wxFS_VOL_REMOVABLE)) |
27d2dbbc | 194 | accept = false; |
7183fd72 | 195 | else if (flagsSet & wxFS_VOL_READONLY && !(flags & wxFS_VOL_READONLY)) |
27d2dbbc | 196 | accept = false; |
7183fd72 | 197 | else if (flagsUnset & wxFS_VOL_READONLY && (flags & wxFS_VOL_READONLY)) |
27d2dbbc | 198 | accept = false; |
7183fd72 | 199 | else if (flagsSet & wxFS_VOL_REMOTE && !(flags & wxFS_VOL_REMOTE)) |
27d2dbbc | 200 | accept = false; |
7183fd72 | 201 | else if (flagsUnset & wxFS_VOL_REMOTE && (flags & wxFS_VOL_REMOTE)) |
27d2dbbc | 202 | accept = false; |
7183fd72 VZ |
203 | |
204 | // Add to the list if passed the filter. | |
205 | if (accept) | |
206 | list.Add(filename); | |
207 | ||
208 | return accept; | |
209 | } // FilteredAdd | |
210 | ||
211 | //============================================================================= | |
212 | // Function: BuildListFromNN | |
213 | // Purpose: Append or remove items from the list | |
214 | // Notes: - There is no way to find all disconnected NN items, or even to find | |
215 | // all items while determining which are connected and not. So this | |
216 | // function will find either all items or connected items. | |
217 | //============================================================================= | |
27d2dbbc | 218 | static void BuildListFromNN(wxArrayString& list, NETRESOURCE* pResSrc, |
4d90b8de | 219 | unsigned flagsSet, unsigned flagsUnset) |
7183fd72 VZ |
220 | { |
221 | HANDLE hEnum; | |
222 | int rc; | |
223 | ||
224 | //----------------------------------------------- | |
225 | // Scope may be all drives or all mounted drives. | |
226 | //----------------------------------------------- | |
227 | unsigned scope = RESOURCE_GLOBALNET; | |
228 | if (flagsSet & wxFS_VOL_MOUNTED) | |
229 | scope = RESOURCE_CONNECTED; | |
230 | ||
231 | //---------------------------------------------------------------------- | |
232 | // Enumerate all items, adding only non-containers (ie. network shares). | |
233 | // Containers cause a recursive call to this function for their own | |
234 | // enumeration. | |
235 | //---------------------------------------------------------------------- | |
236 | if (rc = s_pWNetOpenEnum(scope, RESOURCETYPE_DISK, 0, pResSrc, &hEnum), rc == NO_ERROR) | |
237 | { | |
4aebbc59 MB |
238 | DWORD count = 1; |
239 | DWORD size = 256; | |
7183fd72 VZ |
240 | NETRESOURCE* pRes = (NETRESOURCE*)malloc(size); |
241 | memset(pRes, 0, sizeof(NETRESOURCE)); | |
242 | while (rc = s_pWNetEnumResource(hEnum, &count, pRes, &size), rc == NO_ERROR || rc == ERROR_MORE_DATA) | |
243 | { | |
244 | if (s_cancelSearch) | |
245 | break; | |
246 | ||
247 | if (rc == ERROR_MORE_DATA) | |
248 | { | |
249 | pRes = (NETRESOURCE*)realloc(pRes, size); | |
250 | count = 1; | |
251 | } | |
252 | else if (count == 1) | |
253 | { | |
254 | // Enumerate the container. | |
255 | if (pRes->dwUsage & RESOURCEUSAGE_CONTAINER) | |
256 | { | |
257 | BuildListFromNN(list, pRes, flagsSet, flagsUnset); | |
258 | } | |
259 | ||
260 | // Add the network share. | |
261 | else | |
262 | { | |
263 | wxString filename(pRes->lpRemoteName); | |
264 | ||
df69528b | 265 | if (!filename.empty()) |
7183fd72 VZ |
266 | { |
267 | if (filename.Last() != '\\') | |
268 | filename.Append('\\'); | |
269 | ||
270 | // The filter function will not know mounted from unmounted, and neither do we unless | |
271 | // we are iterating using RESOURCE_CONNECTED, in which case they all are mounted. | |
272 | // Volumes on disconnected servers, however, will correctly show as unmounted. | |
273 | FilteredAdd(list, filename, flagsSet, flagsUnset&~wxFS_VOL_MOUNTED); | |
274 | if (scope == RESOURCE_GLOBALNET) | |
275 | s_fileInfo[filename].m_flags &= ~wxFS_VOL_MOUNTED; | |
276 | } | |
277 | } | |
278 | } | |
279 | else if (count == 0) | |
280 | break; | |
281 | } | |
282 | free(pRes); | |
283 | s_pWNetCloseEnum(hEnum); | |
284 | } | |
285 | } // BuildListFromNN | |
286 | ||
287 | //============================================================================= | |
288 | // Function: CompareFcn | |
289 | // Purpose: Used to sort the NN list alphabetically, case insensitive. | |
290 | //============================================================================= | |
99f161bd | 291 | static int CompareFcn(const wxString& first, const wxString& second) |
7183fd72 | 292 | { |
99f161bd | 293 | return wxStricmp(first.c_str(), second.c_str()); |
7183fd72 VZ |
294 | } // CompareFcn |
295 | ||
296 | //============================================================================= | |
297 | // Function: BuildRemoteList | |
298 | // Purpose: Append Network Neighborhood items to the list. | |
299 | // Notes: - Mounted gets transalated into Connected. FilteredAdd is told | |
300 | // to ignore the Mounted flag since we need to handle it in a weird | |
301 | // way manually. | |
302 | // - The resulting list is sorted alphabetically. | |
303 | //============================================================================= | |
27d2dbbc | 304 | static bool BuildRemoteList(wxArrayString& list, NETRESOURCE* pResSrc, |
4d90b8de | 305 | unsigned flagsSet, unsigned flagsUnset) |
7183fd72 VZ |
306 | { |
307 | // NN query depends on dynamically loaded library. | |
308 | if (!s_pWNetOpenEnum || !s_pWNetEnumResource || !s_pWNetCloseEnum) | |
309 | { | |
310 | wxLogError(_("Failed to load mpr.dll.")); | |
27d2dbbc | 311 | return false; |
7183fd72 VZ |
312 | } |
313 | ||
314 | // Don't waste time doing the work if the flags conflict. | |
315 | if (flagsSet & wxFS_VOL_MOUNTED && flagsUnset & wxFS_VOL_MOUNTED) | |
27d2dbbc | 316 | return false; |
7183fd72 VZ |
317 | |
318 | //---------------------------------------------- | |
319 | // Generate the list according to the flags set. | |
320 | //---------------------------------------------- | |
321 | BuildListFromNN(list, pResSrc, flagsSet, flagsUnset); | |
322 | list.Sort(CompareFcn); | |
323 | ||
324 | //------------------------------------------------------------------------- | |
325 | // If mounted only is requested, then we only need one simple pass. | |
326 | // Otherwise, we need to build a list of all NN volumes and then apply the | |
327 | // list of mounted drives to it. | |
328 | //------------------------------------------------------------------------- | |
329 | if (!(flagsSet & wxFS_VOL_MOUNTED)) | |
330 | { | |
331 | // generate. | |
332 | wxArrayString mounted; | |
333 | BuildListFromNN(mounted, pResSrc, flagsSet | wxFS_VOL_MOUNTED, flagsUnset & ~wxFS_VOL_MOUNTED); | |
334 | mounted.Sort(CompareFcn); | |
335 | ||
336 | // apply list from bottom to top to preserve indexes if removing items. | |
4a559093 WS |
337 | ssize_t iList = list.GetCount()-1; |
338 | for (ssize_t iMounted = mounted.GetCount()-1; iMounted >= 0 && iList >= 0; iMounted--) | |
7183fd72 VZ |
339 | { |
340 | int compare; | |
341 | wxString all(list[iList]); | |
342 | wxString mount(mounted[iMounted]); | |
343 | ||
27d2dbbc | 344 | while (compare = |
4d90b8de VS |
345 | wxStricmp(list[iList].c_str(), mounted[iMounted].c_str()), |
346 | compare > 0 && iList >= 0) | |
7183fd72 VZ |
347 | { |
348 | iList--; | |
349 | all = list[iList]; | |
350 | } | |
351 | ||
352 | ||
353 | if (compare == 0) | |
354 | { | |
355 | // Found the element. Remove it or mark it mounted. | |
356 | if (flagsUnset & wxFS_VOL_MOUNTED) | |
ba8c1601 | 357 | list.RemoveAt(iList); |
7183fd72 VZ |
358 | else |
359 | s_fileInfo[list[iList]].m_flags |= wxFS_VOL_MOUNTED; | |
360 | ||
361 | } | |
362 | ||
363 | iList--; | |
364 | } | |
365 | } | |
366 | ||
27d2dbbc | 367 | return true; |
7183fd72 VZ |
368 | } // BuildRemoteList |
369 | ||
370 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
371 | // wxFSVolume | |
372 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
373 | ||
374 | //============================================================================= | |
375 | // Function: GetVolumes | |
376 | // Purpose: Generate and return a list of all volumes (drives) available. | |
377 | // Notes: | |
378 | //============================================================================= | |
e2478fde | 379 | wxArrayString wxFSVolumeBase::GetVolumes(int flagsSet, int flagsUnset) |
7183fd72 | 380 | { |
27d2dbbc | 381 | ::InterlockedExchange(&s_cancelSearch, FALSE); // reset |
7183fd72 | 382 | |
64c288fa | 383 | #if wxUSE_DYNLIB_CLASS |
7183fd72 VZ |
384 | if (!s_mprLib.IsLoaded() && s_mprLib.Load(_T("mpr.dll"))) |
385 | { | |
386 | #ifdef UNICODE | |
387 | s_pWNetOpenEnum = (WNetOpenEnumPtr)s_mprLib.GetSymbol(_T("WNetOpenEnumW")); | |
2b5f62a0 | 388 | s_pWNetEnumResource = (WNetEnumResourcePtr)s_mprLib.GetSymbol(_T("WNetEnumResourceW")); |
7183fd72 VZ |
389 | #else |
390 | s_pWNetOpenEnum = (WNetOpenEnumPtr)s_mprLib.GetSymbol(_T("WNetOpenEnumA")); | |
391 | s_pWNetEnumResource = (WNetEnumResourcePtr)s_mprLib.GetSymbol(_T("WNetEnumResourceA")); | |
392 | #endif | |
393 | s_pWNetCloseEnum = (WNetCloseEnumPtr)s_mprLib.GetSymbol(_T("WNetCloseEnum")); | |
394 | } | |
64c288fa | 395 | #endif |
7183fd72 VZ |
396 | |
397 | wxArrayString list; | |
398 | ||
399 | //------------------------------- | |
400 | // Local and mapped drives first. | |
401 | //------------------------------- | |
402 | // Allocate the required space for the API call. | |
95dc6a2b | 403 | const DWORD chars = GetLogicalDriveStrings(0, NULL); |
7183fd72 VZ |
404 | TCHAR* buf = new TCHAR[chars+1]; |
405 | ||
406 | // Get the list of drives. | |
999836aa | 407 | GetLogicalDriveStrings(chars, buf); |
7183fd72 VZ |
408 | |
409 | // Parse the list into an array, applying appropriate filters. | |
410 | TCHAR *pVol; | |
411 | pVol = buf; | |
412 | while (*pVol) | |
413 | { | |
414 | FilteredAdd(list, pVol, flagsSet, flagsUnset); | |
67fade33 | 415 | pVol = pVol + wxStrlen(pVol) + 1; |
7183fd72 VZ |
416 | } |
417 | ||
418 | // Cleanup. | |
419 | delete[] buf; | |
420 | ||
421 | //--------------------------- | |
422 | // Network Neighborhood next. | |
423 | //--------------------------- | |
424 | ||
425 | // not exclude remote and not removable | |
426 | if (!(flagsUnset & wxFS_VOL_REMOTE) && | |
427 | !(flagsSet & wxFS_VOL_REMOVABLE) | |
428 | ) | |
429 | { | |
430 | // The returned list will be sorted alphabetically. We don't pass | |
431 | // our in since we don't want to change to order of the local drives. | |
432 | wxArrayString nn; | |
433 | if (BuildRemoteList(nn, 0, flagsSet, flagsUnset)) | |
434 | { | |
89c98653 | 435 | for (size_t idx = 0; idx < nn.GetCount(); idx++) |
7183fd72 VZ |
436 | list.Add(nn[idx]); |
437 | } | |
438 | } | |
439 | ||
440 | return list; | |
441 | } // GetVolumes | |
442 | ||
443 | //============================================================================= | |
444 | // Function: CancelSearch | |
445 | // Purpose: Instruct an active search to stop. | |
446 | // Notes: - This will only sensibly be called by a thread other than the one | |
447 | // performing the search. This is the only thread-safe function | |
448 | // provided by the class. | |
449 | //============================================================================= | |
e2478fde | 450 | void wxFSVolumeBase::CancelSearch() |
7183fd72 | 451 | { |
27d2dbbc | 452 | ::InterlockedExchange(&s_cancelSearch, TRUE); |
7183fd72 VZ |
453 | } // CancelSearch |
454 | ||
455 | //============================================================================= | |
456 | // Function: constructor | |
457 | // Purpose: default constructor | |
458 | //============================================================================= | |
e2478fde | 459 | wxFSVolumeBase::wxFSVolumeBase() |
7183fd72 | 460 | { |
27d2dbbc | 461 | m_isOk = false; |
7183fd72 VZ |
462 | } // wxVolume |
463 | ||
464 | //============================================================================= | |
465 | // Function: constructor | |
466 | // Purpose: constructor that calls Create | |
467 | //============================================================================= | |
e2478fde | 468 | wxFSVolumeBase::wxFSVolumeBase(const wxString& name) |
7183fd72 VZ |
469 | { |
470 | Create(name); | |
471 | } // wxVolume | |
472 | ||
473 | //============================================================================= | |
474 | // Function: Create | |
475 | // Purpose: Finds, logs in, etc. to the request volume. | |
476 | //============================================================================= | |
e2478fde | 477 | bool wxFSVolumeBase::Create(const wxString& name) |
7183fd72 VZ |
478 | { |
479 | // assume fail. | |
27d2dbbc | 480 | m_isOk = false; |
7183fd72 VZ |
481 | |
482 | // supplied. | |
483 | m_volName = name; | |
484 | ||
485 | // Display name. | |
486 | SHFILEINFO fi; | |
487 | long rc = SHGetFileInfo(m_volName, 0, &fi, sizeof(fi), SHGFI_DISPLAYNAME); | |
488 | if (!rc) | |
489 | { | |
4d90b8de | 490 | wxLogError(_("Cannot read typename from '%s'!"), m_volName.c_str()); |
7183fd72 VZ |
491 | return m_isOk; |
492 | } | |
493 | m_dispName = fi.szDisplayName; | |
494 | ||
7183fd72 | 495 | // all tests passed. |
27d2dbbc | 496 | return m_isOk = true; |
7183fd72 VZ |
497 | } // Create |
498 | ||
499 | //============================================================================= | |
500 | // Function: IsOk | |
27d2dbbc | 501 | // Purpose: returns true if the volume is legal. |
7183fd72 VZ |
502 | // Notes: For fixed disks, it must exist. For removable disks, it must also |
503 | // be present. For Network Shares, it must also be logged in, etc. | |
504 | //============================================================================= | |
e2478fde | 505 | bool wxFSVolumeBase::IsOk() const |
7183fd72 VZ |
506 | { |
507 | return m_isOk; | |
508 | } // IsOk | |
509 | ||
510 | //============================================================================= | |
511 | // Function: GetKind | |
512 | // Purpose: Return the type of the volume. | |
513 | //============================================================================= | |
e2478fde | 514 | wxFSVolumeKind wxFSVolumeBase::GetKind() const |
7183fd72 VZ |
515 | { |
516 | if (!m_isOk) | |
517 | return wxFS_VOL_OTHER; | |
518 | ||
519 | FileInfoMap::iterator itr = s_fileInfo.find(m_volName); | |
520 | if (itr == s_fileInfo.end()) | |
521 | return wxFS_VOL_OTHER; | |
522 | ||
523 | return itr->second.m_type; | |
524 | } | |
525 | ||
526 | //============================================================================= | |
527 | // Function: GetFlags | |
528 | // Purpose: Return the caches flags for this volume. | |
529 | // Notes: - Returns -1 if no flags were cached. | |
530 | //============================================================================= | |
e2478fde | 531 | int wxFSVolumeBase::GetFlags() const |
7183fd72 VZ |
532 | { |
533 | if (!m_isOk) | |
534 | return -1; | |
535 | ||
536 | FileInfoMap::iterator itr = s_fileInfo.find(m_volName); | |
537 | if (itr == s_fileInfo.end()) | |
538 | return -1; | |
539 | ||
540 | return itr->second.m_flags; | |
541 | } // GetFlags | |
542 | ||
ec67cff1 | 543 | #endif // wxUSE_BASE |
e2478fde VZ |
544 | |
545 | // ============================================================================ | |
546 | // wxFSVolume | |
547 | // ============================================================================ | |
548 | ||
0e7fa3a6 | 549 | #if wxUSE_GUI |
7183fd72 | 550 | |
e2478fde VZ |
551 | void wxFSVolume::InitIcons() |
552 | { | |
553 | m_icons.Alloc(wxFS_VOL_ICO_MAX); | |
554 | wxIcon null; | |
555 | for (int idx = 0; idx < wxFS_VOL_ICO_MAX; idx++) | |
556 | m_icons.Add(null); | |
557 | } | |
558 | ||
7183fd72 VZ |
559 | //============================================================================= |
560 | // Function: GetIcon | |
561 | // Purpose: return the requested icon. | |
562 | //============================================================================= | |
e2478fde | 563 | |
7183fd72 VZ |
564 | wxIcon wxFSVolume::GetIcon(wxFSIconType type) const |
565 | { | |
e2478fde VZ |
566 | wxCHECK_MSG( type >= 0 && (size_t)type < m_icons.GetCount(), wxNullIcon, |
567 | _T("wxFSIconType::GetIcon(): invalid icon index") ); | |
7183fd72 VZ |
568 | |
569 | // Load on demand. | |
570 | if (m_icons[type].IsNull()) | |
571 | { | |
e2478fde | 572 | UINT flags = SHGFI_ICON; |
7183fd72 VZ |
573 | switch (type) |
574 | { | |
575 | case wxFS_VOL_ICO_SMALL: | |
e2478fde | 576 | flags |= SHGFI_SMALLICON; |
7183fd72 VZ |
577 | break; |
578 | ||
579 | case wxFS_VOL_ICO_LARGE: | |
e2478fde | 580 | flags |= SHGFI_SHELLICONSIZE; |
7183fd72 VZ |
581 | break; |
582 | ||
583 | case wxFS_VOL_ICO_SEL_SMALL: | |
e2478fde | 584 | flags |= SHGFI_SMALLICON | SHGFI_OPENICON; |
7183fd72 VZ |
585 | break; |
586 | ||
587 | case wxFS_VOL_ICO_SEL_LARGE: | |
e2478fde | 588 | flags |= SHGFI_SHELLICONSIZE | SHGFI_OPENICON; |
7183fd72 | 589 | break; |
27d2dbbc | 590 | |
4d90b8de VS |
591 | case wxFS_VOL_ICO_MAX: |
592 | wxFAIL_MSG(_T("wxFS_VOL_ICO_MAX is not valid icon type")); | |
593 | break; | |
7183fd72 VZ |
594 | } |
595 | ||
596 | SHFILEINFO fi; | |
597 | long rc = SHGetFileInfo(m_volName, 0, &fi, sizeof(fi), flags); | |
598 | m_icons[type].SetHICON((WXHICON)fi.hIcon); | |
599 | if (!rc || !fi.hIcon) | |
4d90b8de | 600 | wxLogError(_("Cannot load icon from '%s'."), m_volName.c_str()); |
7183fd72 VZ |
601 | } |
602 | ||
603 | return m_icons[type]; | |
604 | } // GetIcon | |
605 | ||
606 | #endif // wxUSE_GUI | |
607 | ||
05815ab3 | 608 | #endif // wxUSE_FSVOLUME |