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 // ----------------------------------------------------------------------------
21 #pragma implementation "fsvolume.h"
24 #include "wx/wxprec.h"
38 #include "wx/hashmap.h"
39 #include "wx/dynlib.h"
40 #include "wx/arrimpl.cpp"
42 #include "wx/volume.h"
45 #include "wx/msw/missing.h"
49 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
50 // Dynamic library function defs.
51 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
53 static wxDynamicLibrary s_mprLib
;
55 typedef DWORD (WINAPI
* WNetOpenEnumPtr
)(DWORD
, DWORD
, DWORD
, LPNETRESOURCE
, LPHANDLE
);
56 typedef DWORD (WINAPI
* WNetEnumResourcePtr
)(HANDLE
, LPDWORD
, LPVOID
, LPDWORD
);
57 typedef DWORD (WINAPI
* WNetCloseEnumPtr
)(HANDLE
);
59 static WNetOpenEnumPtr s_pWNetOpenEnum
;
60 static WNetEnumResourcePtr s_pWNetEnumResource
;
61 static WNetCloseEnumPtr s_pWNetCloseEnum
;
63 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
65 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
66 static long s_cancelSearch
= FALSE
;
70 FileInfo(unsigned flag
=0, wxFSVolumeKind type
=wxFS_VOL_OTHER
) :
71 m_flags(flag
), m_type(type
) {}
73 FileInfo(const FileInfo
& other
) { *this = other
; }
74 FileInfo
& operator=(const FileInfo
& other
)
76 m_flags
= other
.m_flags
;
77 m_type
= other
.m_type
;
82 wxFSVolumeKind m_type
;
84 WX_DECLARE_STRING_HASH_MAP(FileInfo
, FileInfoMap
);
85 // Cygwin bug (?) destructor for global s_fileInfo is called twice...
86 static FileInfoMap
& GetFileInfoMap()
88 static FileInfoMap
s_fileInfo(25);
92 #define s_fileInfo (GetFileInfoMap())
94 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
95 // Local helper functions.
96 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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 //=============================================================================
107 static unsigned GetBasicFlags(const wxChar
* filename
)
109 unsigned flags
= wxFS_VOL_MOUNTED
;
111 //----------------------------------
112 // 'Best Guess' based on drive type.
113 //----------------------------------
115 switch(GetDriveType(filename
))
118 type
= wxFS_VOL_DISK
;
121 case DRIVE_REMOVABLE
:
122 flags
|= wxFS_VOL_REMOVABLE
;
123 type
= wxFS_VOL_FLOPPY
;
127 flags
|= wxFS_VOL_REMOVABLE
| wxFS_VOL_READONLY
;
128 type
= wxFS_VOL_CDROM
;
132 flags
|= wxFS_VOL_REMOTE
;
133 type
= wxFS_VOL_NETWORK
;
136 case DRIVE_NO_ROOT_DIR
:
137 flags
&= ~wxFS_VOL_MOUNTED
;
138 type
= wxFS_VOL_OTHER
;
142 type
= wxFS_VOL_OTHER
;
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
151 //-----------------------------------------------------------------------
154 rc
= SHGetFileInfo(filename
, 0, &fi
, sizeof(fi
), SHGFI_ATTRIBUTES
);
157 wxLogError(_("Cannot read typename from '%s'!"), filename
);
161 if (fi
.dwAttributes
& SFGAO_READONLY
)
162 flags
|= wxFS_VOL_READONLY
;
163 if (fi
.dwAttributes
& SFGAO_REMOVABLE
)
164 flags
|= wxFS_VOL_REMOVABLE
;
170 s_fileInfo
[filename
] = FileInfo(flags
, type
);
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 //=============================================================================
180 static bool FilteredAdd(wxArrayString
& list
, const wxChar
* filename
,
181 unsigned flagsSet
, unsigned flagsUnset
)
184 unsigned flags
= GetBasicFlags(filename
);
186 if (flagsSet
& wxFS_VOL_MOUNTED
&& !(flags
& wxFS_VOL_MOUNTED
))
188 else if (flagsUnset
& wxFS_VOL_MOUNTED
&& (flags
& wxFS_VOL_MOUNTED
))
190 else if (flagsSet
& wxFS_VOL_REMOVABLE
&& !(flags
& wxFS_VOL_REMOVABLE
))
192 else if (flagsUnset
& wxFS_VOL_REMOVABLE
&& (flags
& wxFS_VOL_REMOVABLE
))
194 else if (flagsSet
& wxFS_VOL_READONLY
&& !(flags
& wxFS_VOL_READONLY
))
196 else if (flagsUnset
& wxFS_VOL_READONLY
&& (flags
& wxFS_VOL_READONLY
))
198 else if (flagsSet
& wxFS_VOL_REMOTE
&& !(flags
& wxFS_VOL_REMOTE
))
200 else if (flagsUnset
& wxFS_VOL_REMOTE
&& (flags
& wxFS_VOL_REMOTE
))
203 // Add to the list if passed the filter.
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 //=============================================================================
217 static void BuildListFromNN(wxArrayString
& list
, NETRESOURCE
* pResSrc
,
218 unsigned flagsSet
, unsigned flagsUnset
)
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
;
230 //----------------------------------------------------------------------
231 // Enumerate all items, adding only non-containers (ie. network shares).
232 // Containers cause a recursive call to this function for their own
234 //----------------------------------------------------------------------
235 if (rc
= s_pWNetOpenEnum(scope
, RESOURCETYPE_DISK
, 0, pResSrc
, &hEnum
), rc
== NO_ERROR
)
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
)
246 if (rc
== ERROR_MORE_DATA
)
248 pRes
= (NETRESOURCE
*)realloc(pRes
, size
);
253 // Enumerate the container.
254 if (pRes
->dwUsage
& RESOURCEUSAGE_CONTAINER
)
256 BuildListFromNN(list
, pRes
, flagsSet
, flagsUnset
);
259 // Add the network share.
262 wxString
filename(pRes
->lpRemoteName
);
266 if (filename
.Last() != '\\')
267 filename
.Append('\\');
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
;
282 s_pWNetCloseEnum(hEnum
);
286 //=============================================================================
287 // Function: CompareFcn
288 // Purpose: Used to sort the NN list alphabetically, case insensitive.
289 //=============================================================================
290 static int CompareFcn(const wxString
& first
, const wxString
& second
)
292 return wxStricmp(first
.c_str(), second
.c_str());
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
301 // - The resulting list is sorted alphabetically.
302 //=============================================================================
303 static bool BuildRemoteList(wxArrayString
& list
, NETRESOURCE
* pResSrc
,
304 unsigned flagsSet
, unsigned flagsUnset
)
306 // NN query depends on dynamically loaded library.
307 if (!s_pWNetOpenEnum
|| !s_pWNetEnumResource
|| !s_pWNetCloseEnum
)
309 wxLogError(_("Failed to load mpr.dll."));
313 // Don't waste time doing the work if the flags conflict.
314 if (flagsSet
& wxFS_VOL_MOUNTED
&& flagsUnset
& wxFS_VOL_MOUNTED
)
317 //----------------------------------------------
318 // Generate the list according to the flags set.
319 //----------------------------------------------
320 BuildListFromNN(list
, pResSrc
, flagsSet
, flagsUnset
);
321 list
.Sort(CompareFcn
);
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
))
331 wxArrayString mounted
;
332 BuildListFromNN(mounted
, pResSrc
, flagsSet
| wxFS_VOL_MOUNTED
, flagsUnset
& ~wxFS_VOL_MOUNTED
);
333 mounted
.Sort(CompareFcn
);
335 // apply list from bottom to top to preserve indexes if removing items.
336 int iList
= list
.GetCount()-1;
338 for (iMounted
= mounted
.GetCount()-1; iMounted
>= 0 && iList
>= 0; iMounted
--)
341 wxString
all(list
[iList
]);
342 wxString
mount(mounted
[iMounted
]);
345 wxStricmp(list
[iList
].c_str(), mounted
[iMounted
].c_str()),
346 compare
> 0 && iList
>= 0)
355 // Found the element. Remove it or mark it mounted.
356 if (flagsUnset
& wxFS_VOL_MOUNTED
)
357 list
.RemoveAt(iList
);
359 s_fileInfo
[list
[iList
]].m_flags
|= wxFS_VOL_MOUNTED
;
370 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
372 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
374 //=============================================================================
375 // Function: GetVolumes
376 // Purpose: Generate and return a list of all volumes (drives) available.
378 //=============================================================================
379 wxArrayString
wxFSVolumeBase::GetVolumes(int flagsSet
, int flagsUnset
)
381 InterlockedExchange(&s_cancelSearch
, FALSE
); // reset
383 if (!s_mprLib
.IsLoaded() && s_mprLib
.Load(_T("mpr.dll")))
386 s_pWNetOpenEnum
= (WNetOpenEnumPtr
)s_mprLib
.GetSymbol(_T("WNetOpenEnumW"));
387 s_pWNetEnumResource
= (WNetEnumResourcePtr
)s_mprLib
.GetSymbol(_T("WNetEnumResourceW"));
389 s_pWNetOpenEnum
= (WNetOpenEnumPtr
)s_mprLib
.GetSymbol(_T("WNetOpenEnumA"));
390 s_pWNetEnumResource
= (WNetEnumResourcePtr
)s_mprLib
.GetSymbol(_T("WNetEnumResourceA"));
392 s_pWNetCloseEnum
= (WNetCloseEnumPtr
)s_mprLib
.GetSymbol(_T("WNetCloseEnum"));
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];
404 // Get the list of drives.
405 chars
= GetLogicalDriveStrings(chars
, buf
);
407 // Parse the list into an array, applying appropriate filters.
412 FilteredAdd(list
, pVol
, flagsSet
, flagsUnset
);
413 pVol
= pVol
+ wxStrlen(pVol
) + 1;
419 //---------------------------
420 // Network Neighborhood next.
421 //---------------------------
423 // not exclude remote and not removable
424 if (!(flagsUnset
& wxFS_VOL_REMOTE
) &&
425 !(flagsSet
& wxFS_VOL_REMOVABLE
)
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.
431 if (BuildRemoteList(nn
, 0, flagsSet
, flagsUnset
))
433 for (size_t idx
= 0; idx
< nn
.GetCount(); idx
++)
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 //=============================================================================
448 void wxFSVolumeBase::CancelSearch()
450 InterlockedExchange(&s_cancelSearch
, TRUE
);
453 //=============================================================================
454 // Function: constructor
455 // Purpose: default constructor
456 //=============================================================================
457 wxFSVolumeBase::wxFSVolumeBase()
462 //=============================================================================
463 // Function: constructor
464 // Purpose: constructor that calls Create
465 //=============================================================================
466 wxFSVolumeBase::wxFSVolumeBase(const wxString
& name
)
471 //=============================================================================
473 // Purpose: Finds, logs in, etc. to the request volume.
474 //=============================================================================
475 bool wxFSVolumeBase::Create(const wxString
& name
)
485 long rc
= SHGetFileInfo(m_volName
, 0, &fi
, sizeof(fi
), SHGFI_DISPLAYNAME
);
488 wxLogError(_("Cannot read typename from '%s'!"), m_volName
.c_str());
491 m_dispName
= fi
.szDisplayName
;
494 return m_isOk
= TRUE
;
497 //=============================================================================
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 //=============================================================================
503 bool wxFSVolumeBase::IsOk() const
508 //=============================================================================
510 // Purpose: Return the type of the volume.
511 //=============================================================================
512 wxFSVolumeKind
wxFSVolumeBase::GetKind() const
515 return wxFS_VOL_OTHER
;
517 FileInfoMap::iterator itr
= s_fileInfo
.find(m_volName
);
518 if (itr
== s_fileInfo
.end())
519 return wxFS_VOL_OTHER
;
521 return itr
->second
.m_type
;
524 //=============================================================================
525 // Function: GetFlags
526 // Purpose: Return the caches flags for this volume.
527 // Notes: - Returns -1 if no flags were cached.
528 //=============================================================================
529 int wxFSVolumeBase::GetFlags() const
534 FileInfoMap::iterator itr
= s_fileInfo
.find(m_volName
);
535 if (itr
== s_fileInfo
.end())
538 return itr
->second
.m_flags
;
543 // ============================================================================
545 // ============================================================================
549 void wxFSVolume::InitIcons()
551 m_icons
.Alloc(wxFS_VOL_ICO_MAX
);
553 for (int idx
= 0; idx
< wxFS_VOL_ICO_MAX
; idx
++)
557 //=============================================================================
559 // Purpose: return the requested icon.
560 //=============================================================================
562 wxIcon
wxFSVolume::GetIcon(wxFSIconType type
) const
564 wxCHECK_MSG( type
>= 0 && (size_t)type
< m_icons
.GetCount(), wxNullIcon
,
565 _T("wxFSIconType::GetIcon(): invalid icon index") );
568 if (m_icons
[type
].IsNull())
570 UINT flags
= SHGFI_ICON
;
573 case wxFS_VOL_ICO_SMALL
:
574 flags
|= SHGFI_SMALLICON
;
577 case wxFS_VOL_ICO_LARGE
:
578 flags
|= SHGFI_SHELLICONSIZE
;
581 case wxFS_VOL_ICO_SEL_SMALL
:
582 flags
|= SHGFI_SMALLICON
| SHGFI_OPENICON
;
585 case wxFS_VOL_ICO_SEL_LARGE
:
586 flags
|= SHGFI_SHELLICONSIZE
| SHGFI_OPENICON
;
589 case wxFS_VOL_ICO_MAX
:
590 wxFAIL_MSG(_T("wxFS_VOL_ICO_MAX is not valid icon type"));
595 long rc
= SHGetFileInfo(m_volName
, 0, &fi
, sizeof(fi
), flags
);
596 m_icons
[type
].SetHICON((WXHICON
)fi
.hIcon
);
597 if (!rc
|| !fi
.hIcon
)
598 wxLogError(_("Cannot load icon from '%s'."), m_volName
.c_str());
601 return m_icons
[type
];
606 #endif // wxUSE_FSVOLUME