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