1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/volume.cpp
3 // Purpose: wxFSVolume - encapsulates system volume information
4 // Author: George Policello
7 // Copyright: (c) 2002 George Policello
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
27 #include "wx/volume.h"
35 #include "wx/hashmap.h"
36 #include "wx/filefn.h"
40 #include "wx/dynlib.h"
41 #include "wx/arrimpl.cpp"
43 // some compilers require including <windows.h> before <shellapi.h> so do it
44 // even if this is not necessary with most of them
45 #include "wx/msw/wrapwin.h"
48 #include "wx/msw/missing.h"
52 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
53 // Dynamic library function defs.
54 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56 #if wxUSE_DYNLIB_CLASS
57 static wxDynamicLibrary s_mprLib
;
60 typedef DWORD (WINAPI
* WNetOpenEnumPtr
)(DWORD
, DWORD
, DWORD
, LPNETRESOURCE
, LPHANDLE
);
61 typedef DWORD (WINAPI
* WNetEnumResourcePtr
)(HANDLE
, LPDWORD
, LPVOID
, LPDWORD
);
62 typedef DWORD (WINAPI
* WNetCloseEnumPtr
)(HANDLE
);
64 static WNetOpenEnumPtr s_pWNetOpenEnum
;
65 static WNetEnumResourcePtr s_pWNetEnumResource
;
66 static WNetCloseEnumPtr s_pWNetCloseEnum
;
68 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
70 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
71 static long s_cancelSearch
= FALSE
;
75 FileInfo(unsigned flag
=0, wxFSVolumeKind type
=wxFS_VOL_OTHER
) :
76 m_flags(flag
), m_type(type
) {}
78 FileInfo(const FileInfo
& other
) { *this = other
; }
79 FileInfo
& operator=(const FileInfo
& other
)
81 m_flags
= other
.m_flags
;
82 m_type
= other
.m_type
;
87 wxFSVolumeKind m_type
;
89 WX_DECLARE_STRING_HASH_MAP(FileInfo
, FileInfoMap
);
90 // Cygwin bug (?) destructor for global s_fileInfo is called twice...
91 static FileInfoMap
& GetFileInfoMap()
93 static FileInfoMap
s_fileInfo(25);
97 #define s_fileInfo (GetFileInfoMap())
99 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
100 // Local helper functions.
101 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
103 //=============================================================================
104 // Function: GetBasicFlags
105 // Purpose: Set basic flags, primarily wxFS_VOL_REMOTE and wxFS_VOL_REMOVABLE.
106 // Notes: - Local and mapped drives are mounted by definition. We have no
107 // way to determine mounted status of network drives, so assume that
108 // all drives are mounted, and let the caller decide otherwise.
109 // - Other flags are 'best guess' from type of drive. The system will
110 // not report the file attributes with any degree of accuracy.
111 //=============================================================================
112 static unsigned GetBasicFlags(const wxChar
* filename
)
114 unsigned flags
= wxFS_VOL_MOUNTED
;
116 //----------------------------------
117 // 'Best Guess' based on drive type.
118 //----------------------------------
120 switch(GetDriveType(filename
))
123 type
= wxFS_VOL_DISK
;
126 case DRIVE_REMOVABLE
:
127 flags
|= wxFS_VOL_REMOVABLE
;
128 type
= wxFS_VOL_FLOPPY
;
132 flags
|= wxFS_VOL_REMOVABLE
| wxFS_VOL_READONLY
;
133 type
= wxFS_VOL_CDROM
;
137 flags
|= wxFS_VOL_REMOTE
;
138 type
= wxFS_VOL_NETWORK
;
141 case DRIVE_NO_ROOT_DIR
:
142 flags
&= ~wxFS_VOL_MOUNTED
;
143 type
= wxFS_VOL_OTHER
;
147 type
= wxFS_VOL_OTHER
;
151 //-----------------------------------------------------------------------
152 // The following most likely will not modify anything not set above,
153 // and will not work at all for network shares or empty CD ROM drives.
154 // But it is a good check if the Win API ever gets better about reporting
156 //-----------------------------------------------------------------------
158 long rc
= SHGetFileInfo(filename
, 0, &fi
, sizeof(fi
), SHGFI_ATTRIBUTES
);
161 // this error is not fatal, so don't show a message to the user about
162 // it, otherwise it would appear every time a generic directory picker
163 // dialog is used and there is a connected network drive
164 wxLogLastError(wxT("SHGetFileInfo"));
168 if (fi
.dwAttributes
& SFGAO_READONLY
)
169 flags
|= wxFS_VOL_READONLY
;
170 if (fi
.dwAttributes
& SFGAO_REMOVABLE
)
171 flags
|= wxFS_VOL_REMOVABLE
;
177 s_fileInfo
[filename
] = FileInfo(flags
, type
);
182 //=============================================================================
183 // Function: FilteredAdd
184 // Purpose: Add a file to the list if it meets the filter requirement.
185 // Notes: - See GetBasicFlags for remarks about the Mounted flag.
186 //=============================================================================
187 static bool FilteredAdd(wxArrayString
& list
, const wxChar
* filename
,
188 unsigned flagsSet
, unsigned flagsUnset
)
191 unsigned flags
= GetBasicFlags(filename
);
193 if (flagsSet
& wxFS_VOL_MOUNTED
&& !(flags
& wxFS_VOL_MOUNTED
))
195 else if (flagsUnset
& wxFS_VOL_MOUNTED
&& (flags
& wxFS_VOL_MOUNTED
))
197 else if (flagsSet
& wxFS_VOL_REMOVABLE
&& !(flags
& wxFS_VOL_REMOVABLE
))
199 else if (flagsUnset
& wxFS_VOL_REMOVABLE
&& (flags
& wxFS_VOL_REMOVABLE
))
201 else if (flagsSet
& wxFS_VOL_READONLY
&& !(flags
& wxFS_VOL_READONLY
))
203 else if (flagsUnset
& wxFS_VOL_READONLY
&& (flags
& wxFS_VOL_READONLY
))
205 else if (flagsSet
& wxFS_VOL_REMOTE
&& !(flags
& wxFS_VOL_REMOTE
))
207 else if (flagsUnset
& wxFS_VOL_REMOTE
&& (flags
& wxFS_VOL_REMOTE
))
210 // Add to the list if passed the filter.
217 //=============================================================================
218 // Function: BuildListFromNN
219 // Purpose: Append or remove items from the list
220 // Notes: - There is no way to find all disconnected NN items, or even to find
221 // all items while determining which are connected and not. So this
222 // function will find either all items or connected items.
223 //=============================================================================
224 static void BuildListFromNN(wxArrayString
& list
, NETRESOURCE
* pResSrc
,
225 unsigned flagsSet
, unsigned flagsUnset
)
230 //-----------------------------------------------
231 // Scope may be all drives or all mounted drives.
232 //-----------------------------------------------
233 unsigned scope
= RESOURCE_GLOBALNET
;
234 if (flagsSet
& wxFS_VOL_MOUNTED
)
235 scope
= RESOURCE_CONNECTED
;
237 //----------------------------------------------------------------------
238 // Enumerate all items, adding only non-containers (ie. network shares).
239 // Containers cause a recursive call to this function for their own
241 //----------------------------------------------------------------------
242 if (rc
= s_pWNetOpenEnum(scope
, RESOURCETYPE_DISK
, 0, pResSrc
, &hEnum
), rc
== NO_ERROR
)
246 NETRESOURCE
* pRes
= (NETRESOURCE
*)malloc(size
);
247 memset(pRes
, 0, sizeof(NETRESOURCE
));
248 while (rc
= s_pWNetEnumResource(hEnum
, &count
, pRes
, &size
), rc
== NO_ERROR
|| rc
== ERROR_MORE_DATA
)
253 if (rc
== ERROR_MORE_DATA
)
255 pRes
= (NETRESOURCE
*)realloc(pRes
, size
);
260 // Enumerate the container.
261 if (pRes
->dwUsage
& RESOURCEUSAGE_CONTAINER
)
263 BuildListFromNN(list
, pRes
, flagsSet
, flagsUnset
);
266 // Add the network share.
269 wxString
filename(pRes
->lpRemoteName
);
271 // if the drive is unavailable, FilteredAdd() can hang for
272 // a long time and, moreover, its failure appears to be not
273 // cached so this will happen every time we use it, so try
274 // a much quicker wxDirExists() test (which still hangs but
275 // for much shorter time) for locally mapped drives first
276 // to try to avoid this
277 if ( pRes
->lpLocalName
&&
278 *pRes
->lpLocalName
&&
279 !wxDirExists(pRes
->lpLocalName
) )
282 if (!filename
.empty())
284 if (filename
.Last() != '\\')
285 filename
.Append('\\');
287 // The filter function will not know mounted from unmounted, and neither do we unless
288 // we are iterating using RESOURCE_CONNECTED, in which case they all are mounted.
289 // Volumes on disconnected servers, however, will correctly show as unmounted.
290 FilteredAdd(list
, filename
.t_str(), flagsSet
, flagsUnset
&~wxFS_VOL_MOUNTED
);
291 if (scope
== RESOURCE_GLOBALNET
)
292 s_fileInfo
[filename
].m_flags
&= ~wxFS_VOL_MOUNTED
;
300 s_pWNetCloseEnum(hEnum
);
304 //=============================================================================
305 // Function: CompareFcn
306 // Purpose: Used to sort the NN list alphabetically, case insensitive.
307 //=============================================================================
308 static int CompareFcn(const wxString
& first
, const wxString
& second
)
310 return wxStricmp(first
.c_str(), second
.c_str());
313 //=============================================================================
314 // Function: BuildRemoteList
315 // Purpose: Append Network Neighborhood items to the list.
316 // Notes: - Mounted gets transalated into Connected. FilteredAdd is told
317 // to ignore the Mounted flag since we need to handle it in a weird
319 // - The resulting list is sorted alphabetically.
320 //=============================================================================
321 static bool BuildRemoteList(wxArrayString
& list
, NETRESOURCE
* pResSrc
,
322 unsigned flagsSet
, unsigned flagsUnset
)
324 // NN query depends on dynamically loaded library.
325 if (!s_pWNetOpenEnum
|| !s_pWNetEnumResource
|| !s_pWNetCloseEnum
)
327 wxLogError(_("Failed to load mpr.dll."));
331 // Don't waste time doing the work if the flags conflict.
332 if (flagsSet
& wxFS_VOL_MOUNTED
&& flagsUnset
& wxFS_VOL_MOUNTED
)
335 //----------------------------------------------
336 // Generate the list according to the flags set.
337 //----------------------------------------------
338 BuildListFromNN(list
, pResSrc
, flagsSet
, flagsUnset
);
339 list
.Sort(CompareFcn
);
341 //-------------------------------------------------------------------------
342 // If mounted only is requested, then we only need one simple pass.
343 // Otherwise, we need to build a list of all NN volumes and then apply the
344 // list of mounted drives to it.
345 //-------------------------------------------------------------------------
346 if (!(flagsSet
& wxFS_VOL_MOUNTED
))
349 wxArrayString mounted
;
350 BuildListFromNN(mounted
, pResSrc
, flagsSet
| wxFS_VOL_MOUNTED
, flagsUnset
& ~wxFS_VOL_MOUNTED
);
351 mounted
.Sort(CompareFcn
);
353 // apply list from bottom to top to preserve indexes if removing items.
354 ssize_t iList
= list
.GetCount()-1;
355 for (ssize_t iMounted
= mounted
.GetCount()-1; iMounted
>= 0 && iList
>= 0; iMounted
--)
358 wxString
all(list
[iList
]);
359 wxString
mount(mounted
[iMounted
]);
362 wxStricmp(list
[iList
].c_str(), mounted
[iMounted
].c_str()),
363 compare
> 0 && iList
>= 0)
372 // Found the element. Remove it or mark it mounted.
373 if (flagsUnset
& wxFS_VOL_MOUNTED
)
374 list
.RemoveAt(iList
);
376 s_fileInfo
[list
[iList
]].m_flags
|= wxFS_VOL_MOUNTED
;
387 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
389 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
391 //=============================================================================
392 // Function: GetVolumes
393 // Purpose: Generate and return a list of all volumes (drives) available.
395 //=============================================================================
396 wxArrayString
wxFSVolumeBase::GetVolumes(int flagsSet
, int flagsUnset
)
398 ::InterlockedExchange(&s_cancelSearch
, FALSE
); // reset
400 #if wxUSE_DYNLIB_CLASS
401 if (!s_mprLib
.IsLoaded() && s_mprLib
.Load(wxT("mpr.dll")))
404 s_pWNetOpenEnum
= (WNetOpenEnumPtr
)s_mprLib
.GetSymbol(wxT("WNetOpenEnumW"));
405 s_pWNetEnumResource
= (WNetEnumResourcePtr
)s_mprLib
.GetSymbol(wxT("WNetEnumResourceW"));
407 s_pWNetOpenEnum
= (WNetOpenEnumPtr
)s_mprLib
.GetSymbol(wxT("WNetOpenEnumA"));
408 s_pWNetEnumResource
= (WNetEnumResourcePtr
)s_mprLib
.GetSymbol(wxT("WNetEnumResourceA"));
410 s_pWNetCloseEnum
= (WNetCloseEnumPtr
)s_mprLib
.GetSymbol(wxT("WNetCloseEnum"));
416 //-------------------------------
417 // Local and mapped drives first.
418 //-------------------------------
419 // Allocate the required space for the API call.
420 const DWORD chars
= GetLogicalDriveStrings(0, NULL
);
421 TCHAR
* buf
= new TCHAR
[chars
+1];
423 // Get the list of drives.
424 GetLogicalDriveStrings(chars
, buf
);
426 // Parse the list into an array, applying appropriate filters.
431 FilteredAdd(list
, pVol
, flagsSet
, flagsUnset
);
432 pVol
= pVol
+ wxStrlen(pVol
) + 1;
438 //---------------------------
439 // Network Neighborhood next.
440 //---------------------------
442 // not exclude remote and not removable
443 if (!(flagsUnset
& wxFS_VOL_REMOTE
) &&
444 !(flagsSet
& wxFS_VOL_REMOVABLE
)
447 // The returned list will be sorted alphabetically. We don't pass
448 // our in since we don't want to change to order of the local drives.
450 if (BuildRemoteList(nn
, 0, flagsSet
, flagsUnset
))
452 for (size_t idx
= 0; idx
< nn
.GetCount(); idx
++)
460 //=============================================================================
461 // Function: CancelSearch
462 // Purpose: Instruct an active search to stop.
463 // Notes: - This will only sensibly be called by a thread other than the one
464 // performing the search. This is the only thread-safe function
465 // provided by the class.
466 //=============================================================================
467 void wxFSVolumeBase::CancelSearch()
469 ::InterlockedExchange(&s_cancelSearch
, TRUE
);
472 //=============================================================================
473 // Function: constructor
474 // Purpose: default constructor
475 //=============================================================================
476 wxFSVolumeBase::wxFSVolumeBase()
481 //=============================================================================
482 // Function: constructor
483 // Purpose: constructor that calls Create
484 //=============================================================================
485 wxFSVolumeBase::wxFSVolumeBase(const wxString
& name
)
490 //=============================================================================
492 // Purpose: Finds, logs in, etc. to the request volume.
493 //=============================================================================
494 bool wxFSVolumeBase::Create(const wxString
& name
)
504 long rc
= SHGetFileInfo(m_volName
.t_str(), 0, &fi
, sizeof(fi
), SHGFI_DISPLAYNAME
);
507 wxLogError(_("Cannot read typename from '%s'!"), m_volName
.c_str());
510 m_dispName
= fi
.szDisplayName
;
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