1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/volume.cpp
3 // Purpose: wxFSVolume - encapsulates system volume information
4 // Author: George Policello
8 // Copyright: (c) 2002 George Policello
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
36 #include "wx/hashmap.h"
37 #include "wx/dynlib.h"
38 #include "wx/arrimpl.cpp"
40 #include "wx/volume.h"
44 #include "wx/msw/missing.h"
48 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
49 // Dynamic library function defs.
50 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
52 static wxDynamicLibrary s_mprLib
;
54 typedef DWORD (WINAPI
* WNetOpenEnumPtr
)(DWORD
, DWORD
, DWORD
, LPNETRESOURCE
, LPHANDLE
);
55 typedef DWORD (WINAPI
* WNetEnumResourcePtr
)(HANDLE
, LPDWORD
, LPVOID
, LPDWORD
);
56 typedef DWORD (WINAPI
* WNetCloseEnumPtr
)(HANDLE
);
58 static WNetOpenEnumPtr s_pWNetOpenEnum
;
59 static WNetEnumResourcePtr s_pWNetEnumResource
;
60 static WNetCloseEnumPtr s_pWNetCloseEnum
;
62 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
64 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
65 static long s_cancelSearch
= FALSE
;
69 FileInfo(unsigned flag
=0, wxFSVolumeKind type
=wxFS_VOL_OTHER
) :
70 m_flags(flag
), m_type(type
) {}
72 FileInfo(const FileInfo
& other
) { *this = other
; }
73 FileInfo
& operator=(const FileInfo
& other
)
75 m_flags
= other
.m_flags
;
76 m_type
= other
.m_type
;
81 wxFSVolumeKind m_type
;
83 WX_DECLARE_STRING_HASH_MAP(FileInfo
, FileInfoMap
);
84 // Cygwin bug (?) destructor for global s_fileInfo is called twice...
85 static FileInfoMap
& GetFileInfoMap()
87 static FileInfoMap
s_fileInfo(25);
91 #define s_fileInfo (GetFileInfoMap())
93 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
94 // Local helper functions.
95 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
97 //=============================================================================
98 // Function: GetBasicFlags
99 // Purpose: Set basic flags, primarily wxFS_VOL_REMOTE and wxFS_VOL_REMOVABLE.
100 // Notes: - Local and mapped drives are mounted by definition. We have no
101 // way to determine mounted status of network drives, so assume that
102 // all drives are mounted, and let the caller decide otherwise.
103 // - Other flags are 'best guess' from type of drive. The system will
104 // not report the file attributes with any degree of accuracy.
105 //=============================================================================
106 static unsigned GetBasicFlags(const wxChar
* filename
)
108 unsigned flags
= wxFS_VOL_MOUNTED
;
110 //----------------------------------
111 // 'Best Guess' based on drive type.
112 //----------------------------------
114 switch(GetDriveType(filename
))
117 type
= wxFS_VOL_DISK
;
120 case DRIVE_REMOVABLE
:
121 flags
|= wxFS_VOL_REMOVABLE
;
122 type
= wxFS_VOL_FLOPPY
;
126 flags
|= wxFS_VOL_REMOVABLE
| wxFS_VOL_READONLY
;
127 type
= wxFS_VOL_CDROM
;
131 flags
|= wxFS_VOL_REMOTE
;
132 type
= wxFS_VOL_NETWORK
;
135 case DRIVE_NO_ROOT_DIR
:
136 flags
&= ~wxFS_VOL_MOUNTED
;
137 type
= wxFS_VOL_OTHER
;
141 type
= wxFS_VOL_OTHER
;
145 //-----------------------------------------------------------------------
146 // The following will most likely will not modify anything not set above,
147 // and will not work at all for network shares or empty CD ROM drives.
148 // But it is a good check if the Win API ever gets better about reporting
150 //-----------------------------------------------------------------------
153 rc
= SHGetFileInfo(filename
, 0, &fi
, sizeof(fi
), SHGFI_ATTRIBUTES
);
156 wxLogError(_("Cannot read typename from '%s'!"), filename
);
160 if (fi
.dwAttributes
& SFGAO_READONLY
)
161 flags
|= wxFS_VOL_READONLY
;
162 if (fi
.dwAttributes
& SFGAO_REMOVABLE
)
163 flags
|= wxFS_VOL_REMOVABLE
;
169 s_fileInfo
[filename
] = FileInfo(flags
, type
);
174 //=============================================================================
175 // Function: FilteredAdd
176 // Purpose: Add a file to the list if it meets the filter requirement.
177 // Notes: - See GetBasicFlags for remarks about the Mounted flag.
178 //=============================================================================
179 static bool FilteredAdd(wxArrayString
& list
, const wxChar
* filename
,
180 unsigned flagsSet
, unsigned flagsUnset
)
183 unsigned flags
= GetBasicFlags(filename
);
185 if (flagsSet
& wxFS_VOL_MOUNTED
&& !(flags
& wxFS_VOL_MOUNTED
))
187 else if (flagsUnset
& wxFS_VOL_MOUNTED
&& (flags
& wxFS_VOL_MOUNTED
))
189 else if (flagsSet
& wxFS_VOL_REMOVABLE
&& !(flags
& wxFS_VOL_REMOVABLE
))
191 else if (flagsUnset
& wxFS_VOL_REMOVABLE
&& (flags
& wxFS_VOL_REMOVABLE
))
193 else if (flagsSet
& wxFS_VOL_READONLY
&& !(flags
& wxFS_VOL_READONLY
))
195 else if (flagsUnset
& wxFS_VOL_READONLY
&& (flags
& wxFS_VOL_READONLY
))
197 else if (flagsSet
& wxFS_VOL_REMOTE
&& !(flags
& wxFS_VOL_REMOTE
))
199 else if (flagsUnset
& wxFS_VOL_REMOTE
&& (flags
& wxFS_VOL_REMOTE
))
202 // Add to the list if passed the filter.
209 //=============================================================================
210 // Function: BuildListFromNN
211 // Purpose: Append or remove items from the list
212 // Notes: - There is no way to find all disconnected NN items, or even to find
213 // all items while determining which are connected and not. So this
214 // function will find either all items or connected items.
215 //=============================================================================
216 static void BuildListFromNN(wxArrayString
& list
, NETRESOURCE
* pResSrc
,
217 unsigned flagsSet
, unsigned flagsUnset
)
222 //-----------------------------------------------
223 // Scope may be all drives or all mounted drives.
224 //-----------------------------------------------
225 unsigned scope
= RESOURCE_GLOBALNET
;
226 if (flagsSet
& wxFS_VOL_MOUNTED
)
227 scope
= RESOURCE_CONNECTED
;
229 //----------------------------------------------------------------------
230 // Enumerate all items, adding only non-containers (ie. network shares).
231 // Containers cause a recursive call to this function for their own
233 //----------------------------------------------------------------------
234 if (rc
= s_pWNetOpenEnum(scope
, RESOURCETYPE_DISK
, 0, pResSrc
, &hEnum
), rc
== NO_ERROR
)
238 NETRESOURCE
* pRes
= (NETRESOURCE
*)malloc(size
);
239 memset(pRes
, 0, sizeof(NETRESOURCE
));
240 while (rc
= s_pWNetEnumResource(hEnum
, &count
, pRes
, &size
), rc
== NO_ERROR
|| rc
== ERROR_MORE_DATA
)
245 if (rc
== ERROR_MORE_DATA
)
247 pRes
= (NETRESOURCE
*)realloc(pRes
, size
);
252 // Enumerate the container.
253 if (pRes
->dwUsage
& RESOURCEUSAGE_CONTAINER
)
255 BuildListFromNN(list
, pRes
, flagsSet
, flagsUnset
);
258 // Add the network share.
261 wxString
filename(pRes
->lpRemoteName
);
265 if (filename
.Last() != '\\')
266 filename
.Append('\\');
268 // The filter function will not know mounted from unmounted, and neither do we unless
269 // we are iterating using RESOURCE_CONNECTED, in which case they all are mounted.
270 // Volumes on disconnected servers, however, will correctly show as unmounted.
271 FilteredAdd(list
, filename
, flagsSet
, flagsUnset
&~wxFS_VOL_MOUNTED
);
272 if (scope
== RESOURCE_GLOBALNET
)
273 s_fileInfo
[filename
].m_flags
&= ~wxFS_VOL_MOUNTED
;
281 s_pWNetCloseEnum(hEnum
);
285 //=============================================================================
286 // Function: CompareFcn
287 // Purpose: Used to sort the NN list alphabetically, case insensitive.
288 //=============================================================================
289 static int CompareFcn(const wxString
& first
, const wxString
& second
)
291 return wxStricmp(first
.c_str(), second
.c_str());
294 //=============================================================================
295 // Function: BuildRemoteList
296 // Purpose: Append Network Neighborhood items to the list.
297 // Notes: - Mounted gets transalated into Connected. FilteredAdd is told
298 // to ignore the Mounted flag since we need to handle it in a weird
300 // - The resulting list is sorted alphabetically.
301 //=============================================================================
302 static bool BuildRemoteList(wxArrayString
& list
, NETRESOURCE
* pResSrc
,
303 unsigned flagsSet
, unsigned flagsUnset
)
305 // NN query depends on dynamically loaded library.
306 if (!s_pWNetOpenEnum
|| !s_pWNetEnumResource
|| !s_pWNetCloseEnum
)
308 wxLogError(_("Failed to load mpr.dll."));
312 // Don't waste time doing the work if the flags conflict.
313 if (flagsSet
& wxFS_VOL_MOUNTED
&& flagsUnset
& wxFS_VOL_MOUNTED
)
316 //----------------------------------------------
317 // Generate the list according to the flags set.
318 //----------------------------------------------
319 BuildListFromNN(list
, pResSrc
, flagsSet
, flagsUnset
);
320 list
.Sort(CompareFcn
);
322 //-------------------------------------------------------------------------
323 // If mounted only is requested, then we only need one simple pass.
324 // Otherwise, we need to build a list of all NN volumes and then apply the
325 // list of mounted drives to it.
326 //-------------------------------------------------------------------------
327 if (!(flagsSet
& wxFS_VOL_MOUNTED
))
330 wxArrayString mounted
;
331 BuildListFromNN(mounted
, pResSrc
, flagsSet
| wxFS_VOL_MOUNTED
, flagsUnset
& ~wxFS_VOL_MOUNTED
);
332 mounted
.Sort(CompareFcn
);
334 // apply list from bottom to top to preserve indexes if removing items.
335 ssize_t iList
= list
.GetCount()-1;
336 for (ssize_t iMounted
= mounted
.GetCount()-1; iMounted
>= 0 && iList
>= 0; iMounted
--)
339 wxString
all(list
[iList
]);
340 wxString
mount(mounted
[iMounted
]);
343 wxStricmp(list
[iList
].c_str(), mounted
[iMounted
].c_str()),
344 compare
> 0 && iList
>= 0)
353 // Found the element. Remove it or mark it mounted.
354 if (flagsUnset
& wxFS_VOL_MOUNTED
)
355 list
.RemoveAt(iList
);
357 s_fileInfo
[list
[iList
]].m_flags
|= wxFS_VOL_MOUNTED
;
368 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
370 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
372 //=============================================================================
373 // Function: GetVolumes
374 // Purpose: Generate and return a list of all volumes (drives) available.
376 //=============================================================================
377 wxArrayString
wxFSVolumeBase::GetVolumes(int flagsSet
, int flagsUnset
)
379 ::InterlockedExchange(&s_cancelSearch
, FALSE
); // reset
381 if (!s_mprLib
.IsLoaded() && s_mprLib
.Load(_T("mpr.dll")))
384 s_pWNetOpenEnum
= (WNetOpenEnumPtr
)s_mprLib
.GetSymbol(_T("WNetOpenEnumW"));
385 s_pWNetEnumResource
= (WNetEnumResourcePtr
)s_mprLib
.GetSymbol(_T("WNetEnumResourceW"));
387 s_pWNetOpenEnum
= (WNetOpenEnumPtr
)s_mprLib
.GetSymbol(_T("WNetOpenEnumA"));
388 s_pWNetEnumResource
= (WNetEnumResourcePtr
)s_mprLib
.GetSymbol(_T("WNetEnumResourceA"));
390 s_pWNetCloseEnum
= (WNetCloseEnumPtr
)s_mprLib
.GetSymbol(_T("WNetCloseEnum"));
395 //-------------------------------
396 // Local and mapped drives first.
397 //-------------------------------
398 // Allocate the required space for the API call.
399 const DWORD chars
= GetLogicalDriveStrings(0, NULL
);
400 TCHAR
* buf
= new TCHAR
[chars
+1];
402 // Get the list of drives.
403 GetLogicalDriveStrings(chars
, buf
);
405 // Parse the list into an array, applying appropriate filters.
410 FilteredAdd(list
, pVol
, flagsSet
, flagsUnset
);
411 pVol
= pVol
+ wxStrlen(pVol
) + 1;
417 //---------------------------
418 // Network Neighborhood next.
419 //---------------------------
421 // not exclude remote and not removable
422 if (!(flagsUnset
& wxFS_VOL_REMOTE
) &&
423 !(flagsSet
& wxFS_VOL_REMOVABLE
)
426 // The returned list will be sorted alphabetically. We don't pass
427 // our in since we don't want to change to order of the local drives.
429 if (BuildRemoteList(nn
, 0, flagsSet
, flagsUnset
))
431 for (size_t idx
= 0; idx
< nn
.GetCount(); idx
++)
439 //=============================================================================
440 // Function: CancelSearch
441 // Purpose: Instruct an active search to stop.
442 // Notes: - This will only sensibly be called by a thread other than the one
443 // performing the search. This is the only thread-safe function
444 // provided by the class.
445 //=============================================================================
446 void wxFSVolumeBase::CancelSearch()
448 ::InterlockedExchange(&s_cancelSearch
, TRUE
);
451 //=============================================================================
452 // Function: constructor
453 // Purpose: default constructor
454 //=============================================================================
455 wxFSVolumeBase::wxFSVolumeBase()
460 //=============================================================================
461 // Function: constructor
462 // Purpose: constructor that calls Create
463 //=============================================================================
464 wxFSVolumeBase::wxFSVolumeBase(const wxString
& name
)
469 //=============================================================================
471 // Purpose: Finds, logs in, etc. to the request volume.
472 //=============================================================================
473 bool wxFSVolumeBase::Create(const wxString
& name
)
483 long rc
= SHGetFileInfo(m_volName
, 0, &fi
, sizeof(fi
), SHGFI_DISPLAYNAME
);
486 wxLogError(_("Cannot read typename from '%s'!"), m_volName
.c_str());
489 m_dispName
= fi
.szDisplayName
;
492 return m_isOk
= true;
495 //=============================================================================
497 // Purpose: returns true if the volume is legal.
498 // Notes: For fixed disks, it must exist. For removable disks, it must also
499 // be present. For Network Shares, it must also be logged in, etc.
500 //=============================================================================
501 bool wxFSVolumeBase::IsOk() const
506 //=============================================================================
508 // Purpose: Return the type of the volume.
509 //=============================================================================
510 wxFSVolumeKind
wxFSVolumeBase::GetKind() const
513 return wxFS_VOL_OTHER
;
515 FileInfoMap::iterator itr
= s_fileInfo
.find(m_volName
);
516 if (itr
== s_fileInfo
.end())
517 return wxFS_VOL_OTHER
;
519 return itr
->second
.m_type
;
522 //=============================================================================
523 // Function: GetFlags
524 // Purpose: Return the caches flags for this volume.
525 // Notes: - Returns -1 if no flags were cached.
526 //=============================================================================
527 int wxFSVolumeBase::GetFlags() const
532 FileInfoMap::iterator itr
= s_fileInfo
.find(m_volName
);
533 if (itr
== s_fileInfo
.end())
536 return itr
->second
.m_flags
;
541 // ============================================================================
543 // ============================================================================
547 void wxFSVolume::InitIcons()
549 m_icons
.Alloc(wxFS_VOL_ICO_MAX
);
551 for (int idx
= 0; idx
< wxFS_VOL_ICO_MAX
; idx
++)
555 //=============================================================================
557 // Purpose: return the requested icon.
558 //=============================================================================
560 wxIcon
wxFSVolume::GetIcon(wxFSIconType type
) const
562 wxCHECK_MSG( type
>= 0 && (size_t)type
< m_icons
.GetCount(), wxNullIcon
,
563 _T("wxFSIconType::GetIcon(): invalid icon index") );
566 if (m_icons
[type
].IsNull())
568 UINT flags
= SHGFI_ICON
;
571 case wxFS_VOL_ICO_SMALL
:
572 flags
|= SHGFI_SMALLICON
;
575 case wxFS_VOL_ICO_LARGE
:
576 flags
|= SHGFI_SHELLICONSIZE
;
579 case wxFS_VOL_ICO_SEL_SMALL
:
580 flags
|= SHGFI_SMALLICON
| SHGFI_OPENICON
;
583 case wxFS_VOL_ICO_SEL_LARGE
:
584 flags
|= SHGFI_SHELLICONSIZE
| SHGFI_OPENICON
;
587 case wxFS_VOL_ICO_MAX
:
588 wxFAIL_MSG(_T("wxFS_VOL_ICO_MAX is not valid icon type"));
593 long rc
= SHGetFileInfo(m_volName
, 0, &fi
, sizeof(fi
), flags
);
594 m_icons
[type
].SetHICON((WXHICON
)fi
.hIcon
);
595 if (!rc
|| !fi
.hIcon
)
596 wxLogError(_("Cannot load icon from '%s'."), m_volName
.c_str());
599 return m_icons
[type
];
604 #endif // wxUSE_FSVOLUME