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"
28 #include "wx/volume.h"
36 #include "wx/hashmap.h"
37 #include "wx/filefn.h"
41 #include "wx/dynlib.h"
42 #include "wx/arrimpl.cpp"
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"
49 #include "wx/msw/missing.h"
53 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
54 // Dynamic library function defs.
55 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57 #if wxUSE_DYNLIB_CLASS
58 static wxDynamicLibrary s_mprLib
;
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
);
65 static WNetOpenEnumPtr s_pWNetOpenEnum
;
66 static WNetEnumResourcePtr s_pWNetEnumResource
;
67 static WNetCloseEnumPtr s_pWNetCloseEnum
;
69 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
71 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
72 static long s_cancelSearch
= FALSE
;
76 FileInfo(unsigned flag
=0, wxFSVolumeKind type
=wxFS_VOL_OTHER
) :
77 m_flags(flag
), m_type(type
) {}
79 FileInfo(const FileInfo
& other
) { *this = other
; }
80 FileInfo
& operator=(const FileInfo
& other
)
82 m_flags
= other
.m_flags
;
83 m_type
= other
.m_type
;
88 wxFSVolumeKind m_type
;
90 WX_DECLARE_STRING_HASH_MAP(FileInfo
, FileInfoMap
);
91 // Cygwin bug (?) destructor for global s_fileInfo is called twice...
92 static FileInfoMap
& GetFileInfoMap()
94 static FileInfoMap
s_fileInfo(25);
98 #define s_fileInfo (GetFileInfoMap())
100 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
101 // Local helper functions.
102 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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
)
115 unsigned flags
= wxFS_VOL_MOUNTED
;
117 //----------------------------------
118 // 'Best Guess' based on drive type.
119 //----------------------------------
121 switch(GetDriveType(filename
))
124 type
= wxFS_VOL_DISK
;
127 case DRIVE_REMOVABLE
:
128 flags
|= wxFS_VOL_REMOVABLE
;
129 type
= wxFS_VOL_FLOPPY
;
133 flags
|= wxFS_VOL_REMOVABLE
| wxFS_VOL_READONLY
;
134 type
= wxFS_VOL_CDROM
;
138 flags
|= wxFS_VOL_REMOTE
;
139 type
= wxFS_VOL_NETWORK
;
142 case DRIVE_NO_ROOT_DIR
:
143 flags
&= ~wxFS_VOL_MOUNTED
;
144 type
= wxFS_VOL_OTHER
;
148 type
= wxFS_VOL_OTHER
;
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
157 //-----------------------------------------------------------------------
159 long rc
= SHGetFileInfo(filename
, 0, &fi
, sizeof(fi
), SHGFI_ATTRIBUTES
);
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"));
169 if (fi
.dwAttributes
& SFGAO_READONLY
)
170 flags
|= wxFS_VOL_READONLY
;
171 if (fi
.dwAttributes
& SFGAO_REMOVABLE
)
172 flags
|= wxFS_VOL_REMOVABLE
;
178 s_fileInfo
[filename
] = FileInfo(flags
, type
);
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
)
192 unsigned flags
= GetBasicFlags(filename
);
194 if (flagsSet
& wxFS_VOL_MOUNTED
&& !(flags
& wxFS_VOL_MOUNTED
))
196 else if (flagsUnset
& wxFS_VOL_MOUNTED
&& (flags
& wxFS_VOL_MOUNTED
))
198 else if (flagsSet
& wxFS_VOL_REMOVABLE
&& !(flags
& wxFS_VOL_REMOVABLE
))
200 else if (flagsUnset
& wxFS_VOL_REMOVABLE
&& (flags
& wxFS_VOL_REMOVABLE
))
202 else if (flagsSet
& wxFS_VOL_READONLY
&& !(flags
& wxFS_VOL_READONLY
))
204 else if (flagsUnset
& wxFS_VOL_READONLY
&& (flags
& wxFS_VOL_READONLY
))
206 else if (flagsSet
& wxFS_VOL_REMOTE
&& !(flags
& wxFS_VOL_REMOTE
))
208 else if (flagsUnset
& wxFS_VOL_REMOTE
&& (flags
& wxFS_VOL_REMOTE
))
211 // Add to the list if passed the filter.
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
)
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
;
238 //----------------------------------------------------------------------
239 // Enumerate all items, adding only non-containers (ie. network shares).
240 // Containers cause a recursive call to this function for their own
242 //----------------------------------------------------------------------
243 if (rc
= s_pWNetOpenEnum(scope
, RESOURCETYPE_DISK
, 0, pResSrc
, &hEnum
), rc
== NO_ERROR
)
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
)
254 if (rc
== ERROR_MORE_DATA
)
256 pRes
= (NETRESOURCE
*)realloc(pRes
, size
);
261 // Enumerate the container.
262 if (pRes
->dwUsage
& RESOURCEUSAGE_CONTAINER
)
264 BuildListFromNN(list
, pRes
, flagsSet
, flagsUnset
);
267 // Add the network share.
270 wxString
filename(pRes
->lpRemoteName
);
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
) )
283 if (!filename
.empty())
285 if (filename
.Last() != '\\')
286 filename
.Append('\\');
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
.t_str(), flagsSet
, flagsUnset
&~wxFS_VOL_MOUNTED
);
292 if (scope
== RESOURCE_GLOBALNET
)
293 s_fileInfo
[filename
].m_flags
&= ~wxFS_VOL_MOUNTED
;
301 s_pWNetCloseEnum(hEnum
);
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
)
311 return wxStricmp(first
.c_str(), second
.c_str());
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
320 // - The resulting list is sorted alphabetically.
321 //=============================================================================
322 static bool BuildRemoteList(wxArrayString
& list
, NETRESOURCE
* pResSrc
,
323 unsigned flagsSet
, unsigned flagsUnset
)
325 // NN query depends on dynamically loaded library.
326 if (!s_pWNetOpenEnum
|| !s_pWNetEnumResource
|| !s_pWNetCloseEnum
)
328 wxLogError(_("Failed to load mpr.dll."));
332 // Don't waste time doing the work if the flags conflict.
333 if (flagsSet
& wxFS_VOL_MOUNTED
&& flagsUnset
& wxFS_VOL_MOUNTED
)
336 //----------------------------------------------
337 // Generate the list according to the flags set.
338 //----------------------------------------------
339 BuildListFromNN(list
, pResSrc
, flagsSet
, flagsUnset
);
340 list
.Sort(CompareFcn
);
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
))
350 wxArrayString mounted
;
351 BuildListFromNN(mounted
, pResSrc
, flagsSet
| wxFS_VOL_MOUNTED
, flagsUnset
& ~wxFS_VOL_MOUNTED
);
352 mounted
.Sort(CompareFcn
);
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
--)
359 wxString
all(list
[iList
]);
360 wxString
mount(mounted
[iMounted
]);
363 wxStricmp(list
[iList
].c_str(), mounted
[iMounted
].c_str()),
364 compare
> 0 && iList
>= 0)
373 // Found the element. Remove it or mark it mounted.
374 if (flagsUnset
& wxFS_VOL_MOUNTED
)
375 list
.RemoveAt(iList
);
377 s_fileInfo
[list
[iList
]].m_flags
|= wxFS_VOL_MOUNTED
;
388 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
390 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
392 //=============================================================================
393 // Function: GetVolumes
394 // Purpose: Generate and return a list of all volumes (drives) available.
396 //=============================================================================
397 wxArrayString
wxFSVolumeBase::GetVolumes(int flagsSet
, int flagsUnset
)
399 ::InterlockedExchange(&s_cancelSearch
, FALSE
); // reset
401 #if wxUSE_DYNLIB_CLASS
402 if (!s_mprLib
.IsLoaded() && s_mprLib
.Load(wxT("mpr.dll")))
405 s_pWNetOpenEnum
= (WNetOpenEnumPtr
)s_mprLib
.GetSymbol(wxT("WNetOpenEnumW"));
406 s_pWNetEnumResource
= (WNetEnumResourcePtr
)s_mprLib
.GetSymbol(wxT("WNetEnumResourceW"));
408 s_pWNetOpenEnum
= (WNetOpenEnumPtr
)s_mprLib
.GetSymbol(wxT("WNetOpenEnumA"));
409 s_pWNetEnumResource
= (WNetEnumResourcePtr
)s_mprLib
.GetSymbol(wxT("WNetEnumResourceA"));
411 s_pWNetCloseEnum
= (WNetCloseEnumPtr
)s_mprLib
.GetSymbol(wxT("WNetCloseEnum"));
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];
424 // Get the list of drives.
425 GetLogicalDriveStrings(chars
, buf
);
427 // Parse the list into an array, applying appropriate filters.
432 FilteredAdd(list
, pVol
, flagsSet
, flagsUnset
);
433 pVol
= pVol
+ wxStrlen(pVol
) + 1;
439 //---------------------------
440 // Network Neighborhood next.
441 //---------------------------
443 // not exclude remote and not removable
444 if (!(flagsUnset
& wxFS_VOL_REMOTE
) &&
445 !(flagsSet
& wxFS_VOL_REMOVABLE
)
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.
451 if (BuildRemoteList(nn
, 0, flagsSet
, flagsUnset
))
453 for (size_t idx
= 0; idx
< nn
.GetCount(); idx
++)
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()
470 ::InterlockedExchange(&s_cancelSearch
, TRUE
);
473 //=============================================================================
474 // Function: constructor
475 // Purpose: default constructor
476 //=============================================================================
477 wxFSVolumeBase::wxFSVolumeBase()
482 //=============================================================================
483 // Function: constructor
484 // Purpose: constructor that calls Create
485 //=============================================================================
486 wxFSVolumeBase::wxFSVolumeBase(const wxString
& name
)
491 //=============================================================================
493 // Purpose: Finds, logs in, etc. to the request volume.
494 //=============================================================================
495 bool wxFSVolumeBase::Create(const wxString
& name
)
505 long rc
= SHGetFileInfo(m_volName
.t_str(), 0, &fi
, sizeof(fi
), SHGFI_DISPLAYNAME
);
508 wxLogError(_("Cannot read typename from '%s'!"), m_volName
.c_str());
511 m_dispName
= fi
.szDisplayName
;
514 return m_isOk
= true;
517 //=============================================================================
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
528 //=============================================================================
530 // Purpose: Return the type of the volume.
531 //=============================================================================
532 wxFSVolumeKind
wxFSVolumeBase::GetKind() const
535 return wxFS_VOL_OTHER
;
537 FileInfoMap::iterator itr
= s_fileInfo
.find(m_volName
);
538 if (itr
== s_fileInfo
.end())
539 return wxFS_VOL_OTHER
;
541 return itr
->second
.m_type
;
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
554 FileInfoMap::iterator itr
= s_fileInfo
.find(m_volName
);
555 if (itr
== s_fileInfo
.end())
558 return itr
->second
.m_flags
;
563 // ============================================================================
565 // ============================================================================
569 void wxFSVolume::InitIcons()
571 m_icons
.Alloc(wxFS_VOL_ICO_MAX
);
573 for (int idx
= 0; idx
< wxFS_VOL_ICO_MAX
; idx
++)
577 //=============================================================================
579 // Purpose: return the requested icon.
580 //=============================================================================
582 wxIcon
wxFSVolume::GetIcon(wxFSIconType type
) const
584 wxCHECK_MSG( type
>= 0 && (size_t)type
< m_icons
.GetCount(), wxNullIcon
,
585 wxT("wxFSIconType::GetIcon(): invalid icon index") );
589 if (m_icons
[type
].IsNull())
591 UINT flags
= SHGFI_ICON
;
594 case wxFS_VOL_ICO_SMALL
:
595 flags
|= SHGFI_SMALLICON
;
598 case wxFS_VOL_ICO_LARGE
:
599 flags
|= SHGFI_SHELLICONSIZE
;
602 case wxFS_VOL_ICO_SEL_SMALL
:
603 flags
|= SHGFI_SMALLICON
| SHGFI_OPENICON
;
606 case wxFS_VOL_ICO_SEL_LARGE
:
607 flags
|= SHGFI_SHELLICONSIZE
| SHGFI_OPENICON
;
610 case wxFS_VOL_ICO_MAX
:
611 wxFAIL_MSG(wxT("wxFS_VOL_ICO_MAX is not valid icon type"));
616 long rc
= SHGetFileInfo(m_volName
.t_str(), 0, &fi
, sizeof(fi
), flags
);
617 m_icons
[type
].SetHICON((WXHICON
)fi
.hIcon
);
618 if (!rc
|| !fi
.hIcon
)
620 wxLogError(_("Cannot load icon from '%s'."), m_volName
.c_str());
624 return m_icons
[type
];
626 wxFAIL_MSG(wxS("Can't convert HICON to wxIcon in this port."));
633 #endif // wxUSE_FSVOLUME