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