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