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