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 license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "fsvolume.h"
24 #include "wx/wxprec.h"
34 #include "wx/hashmap.h"
35 #include "wx/dynlib.h"
36 #include "wx/arrimpl.cpp"
38 #include "wx/volume.h"
43 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
44 // Dynamic library function defs.
45 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
47 static wxDynamicLibrary s_mprLib
;
49 typedef DWORD (WINAPI
* WNetOpenEnumPtr
)(DWORD
, DWORD
, DWORD
, LPNETRESOURCE
, LPHANDLE
);
50 typedef DWORD (WINAPI
* WNetEnumResourcePtr
)(HANDLE
, LPDWORD
, LPVOID
, LPDWORD
);
51 typedef DWORD (WINAPI
* WNetCloseEnumPtr
)(HANDLE
);
53 static WNetOpenEnumPtr s_pWNetOpenEnum
;
54 static WNetEnumResourcePtr s_pWNetEnumResource
;
55 static WNetCloseEnumPtr s_pWNetCloseEnum
;
57 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
59 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
60 static long s_cancelSearch
= FALSE
;
62 struct FileInfo
: public wxObject
64 FileInfo(unsigned flag
=0, wxFSVolumeKind type
=wxFS_VOL_OTHER
) :
65 m_flags(flag
), m_type(type
) {}
67 wxFSVolumeKind m_type
;
69 WX_DECLARE_STRING_HASH_MAP(FileInfo
, FileInfoMap
);
70 static FileInfoMap
s_fileInfo(25);
72 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
73 // Other initialization.
74 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
75 WX_DEFINE_OBJARRAY(wxIconArray
);
77 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
78 // Local helper functions.
79 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
81 //=============================================================================
82 // Function: GetBasicFlags
83 // Purpose: Set basic flags, primarily wxFS_VOL_REMOTE and wxFS_VOL_REMOVABLE.
84 // Notes: - Local and mapped drives are mounted by definition. We have no
85 // way to determine mounted status of network drives, so assume that
86 // all drives are mounted, and let the caller decide otherwise.
87 // - Other flags are 'best guess' from type of drive. The system will
88 // not report the file attributes with any degree of accuracy.
89 //=============================================================================
90 unsigned GetBasicFlags(const char* filename
)
92 unsigned flags
= wxFS_VOL_MOUNTED
;
94 //----------------------------------
95 // 'Best Guess' based on drive type.
96 //----------------------------------
98 switch(GetDriveType(filename
))
101 type
= wxFS_VOL_DISK
;
104 case DRIVE_REMOVABLE
:
105 flags
|= wxFS_VOL_REMOVABLE
;
106 type
= wxFS_VOL_FLOPPY
;
110 flags
|= wxFS_VOL_REMOVABLE
| wxFS_VOL_READONLY
;
111 type
= wxFS_VOL_CDROM
;
115 flags
|= wxFS_VOL_REMOTE
;
116 type
= wxFS_VOL_NETWORK
;
119 case DRIVE_NO_ROOT_DIR
:
120 flags
&= ~wxFS_VOL_MOUNTED
;
121 type
= wxFS_VOL_OTHER
;
125 type
= wxFS_VOL_OTHER
;
129 //-----------------------------------------------------------------------
130 // The following will most likely will not modify anything not set above,
131 // and will not work at all for network shares or empty CD ROM drives.
132 // But it is a good check if the Win API ever gets better about reporting
134 //-----------------------------------------------------------------------
137 rc
= SHGetFileInfo(filename
, 0, &fi
, sizeof(fi
), SHGFI_ATTRIBUTES
);
140 wxLogError(_("Cannot read typename from '%s'!"), filename
);
144 if (fi
.dwAttributes
& SFGAO_READONLY
)
145 flags
|= wxFS_VOL_READONLY
;
146 if (fi
.dwAttributes
& SFGAO_REMOVABLE
)
147 flags
|= wxFS_VOL_REMOVABLE
;
153 s_fileInfo
[filename
] = FileInfo(flags
, type
);
158 //=============================================================================
159 // Function: FilteredAdd
160 // Purpose: Add a file to the list if it meets the filter requirement.
161 // Notes: - See GetBasicFlags for remarks about the Mounted flag.
162 //=============================================================================
163 bool FilteredAdd(wxArrayString
& list
, const char* filename
, unsigned flagsSet
, unsigned flagsUnset
)
166 unsigned flags
= GetBasicFlags(filename
);
168 if (flagsSet
& wxFS_VOL_MOUNTED
&& !(flags
& wxFS_VOL_MOUNTED
))
170 else if (flagsUnset
& wxFS_VOL_MOUNTED
&& (flags
& wxFS_VOL_MOUNTED
))
172 else if (flagsSet
& wxFS_VOL_REMOVABLE
&& !(flags
& wxFS_VOL_REMOVABLE
))
174 else if (flagsUnset
& wxFS_VOL_REMOVABLE
&& (flags
& wxFS_VOL_REMOVABLE
))
176 else if (flagsSet
& wxFS_VOL_READONLY
&& !(flags
& wxFS_VOL_READONLY
))
178 else if (flagsUnset
& wxFS_VOL_READONLY
&& (flags
& wxFS_VOL_READONLY
))
180 else if (flagsSet
& wxFS_VOL_REMOTE
&& !(flags
& wxFS_VOL_REMOTE
))
182 else if (flagsUnset
& wxFS_VOL_REMOTE
&& (flags
& wxFS_VOL_REMOTE
))
185 // Add to the list if passed the filter.
192 //=============================================================================
193 // Function: BuildListFromNN
194 // Purpose: Append or remove items from the list
195 // Notes: - There is no way to find all disconnected NN items, or even to find
196 // all items while determining which are connected and not. So this
197 // function will find either all items or connected items.
198 //=============================================================================
199 void BuildListFromNN(wxArrayString
& list
, NETRESOURCE
* pResSrc
, unsigned flagsSet
, unsigned flagsUnset
)
204 //-----------------------------------------------
205 // Scope may be all drives or all mounted drives.
206 //-----------------------------------------------
207 unsigned scope
= RESOURCE_GLOBALNET
;
208 if (flagsSet
& wxFS_VOL_MOUNTED
)
209 scope
= RESOURCE_CONNECTED
;
211 //----------------------------------------------------------------------
212 // Enumerate all items, adding only non-containers (ie. network shares).
213 // Containers cause a recursive call to this function for their own
215 //----------------------------------------------------------------------
216 if (rc
= s_pWNetOpenEnum(scope
, RESOURCETYPE_DISK
, 0, pResSrc
, &hEnum
), rc
== NO_ERROR
)
218 unsigned long count
= 1;
219 unsigned long size
= 256;
220 NETRESOURCE
* pRes
= (NETRESOURCE
*)malloc(size
);
221 memset(pRes
, 0, sizeof(NETRESOURCE
));
222 while (rc
= s_pWNetEnumResource(hEnum
, &count
, pRes
, &size
), rc
== NO_ERROR
|| rc
== ERROR_MORE_DATA
)
227 if (rc
== ERROR_MORE_DATA
)
229 pRes
= (NETRESOURCE
*)realloc(pRes
, size
);
234 // Enumerate the container.
235 if (pRes
->dwUsage
& RESOURCEUSAGE_CONTAINER
)
237 BuildListFromNN(list
, pRes
, flagsSet
, flagsUnset
);
240 // Add the network share.
243 wxString
filename(pRes
->lpRemoteName
);
247 if (filename
.Last() != '\\')
248 filename
.Append('\\');
250 // The filter function will not know mounted from unmounted, and neither do we unless
251 // we are iterating using RESOURCE_CONNECTED, in which case they all are mounted.
252 // Volumes on disconnected servers, however, will correctly show as unmounted.
253 FilteredAdd(list
, filename
, flagsSet
, flagsUnset
&~wxFS_VOL_MOUNTED
);
254 if (scope
== RESOURCE_GLOBALNET
)
255 s_fileInfo
[filename
].m_flags
&= ~wxFS_VOL_MOUNTED
;
263 s_pWNetCloseEnum(hEnum
);
267 //=============================================================================
268 // Function: CompareFcn
269 // Purpose: Used to sort the NN list alphabetically, case insensitive.
270 //=============================================================================
271 static int CompareFcn(const wxString
& first
, const wxString
& second
)
273 return stricmp(first
.c_str(), second
.c_str());
276 //=============================================================================
277 // Function: BuildRemoteList
278 // Purpose: Append Network Neighborhood items to the list.
279 // Notes: - Mounted gets transalated into Connected. FilteredAdd is told
280 // to ignore the Mounted flag since we need to handle it in a weird
282 // - The resulting list is sorted alphabetically.
283 //=============================================================================
284 bool BuildRemoteList(wxArrayString
& list
, NETRESOURCE
* pResSrc
, unsigned flagsSet
, unsigned flagsUnset
)
286 // NN query depends on dynamically loaded library.
287 if (!s_pWNetOpenEnum
|| !s_pWNetEnumResource
|| !s_pWNetCloseEnum
)
289 wxLogError(_("Failed to load mpr.dll."));
293 // Don't waste time doing the work if the flags conflict.
294 if (flagsSet
& wxFS_VOL_MOUNTED
&& flagsUnset
& wxFS_VOL_MOUNTED
)
297 //----------------------------------------------
298 // Generate the list according to the flags set.
299 //----------------------------------------------
300 BuildListFromNN(list
, pResSrc
, flagsSet
, flagsUnset
);
301 list
.Sort(CompareFcn
);
303 //-------------------------------------------------------------------------
304 // If mounted only is requested, then we only need one simple pass.
305 // Otherwise, we need to build a list of all NN volumes and then apply the
306 // list of mounted drives to it.
307 //-------------------------------------------------------------------------
308 if (!(flagsSet
& wxFS_VOL_MOUNTED
))
311 wxArrayString mounted
;
312 BuildListFromNN(mounted
, pResSrc
, flagsSet
| wxFS_VOL_MOUNTED
, flagsUnset
& ~wxFS_VOL_MOUNTED
);
313 mounted
.Sort(CompareFcn
);
315 // apply list from bottom to top to preserve indexes if removing items.
316 int iList
= list
.GetCount()-1;
318 for (iMounted
= mounted
.GetCount()-1; iMounted
>= 0 && iList
>= 0; iMounted
--)
321 wxString
all(list
[iList
]);
322 wxString
mount(mounted
[iMounted
]);
324 while (compare
= stricmp(list
[iList
], mounted
[iMounted
]), compare
> 0 && iList
>= 0)
333 // Found the element. Remove it or mark it mounted.
334 if (flagsUnset
& wxFS_VOL_MOUNTED
)
337 s_fileInfo
[list
[iList
]].m_flags
|= wxFS_VOL_MOUNTED
;
348 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
350 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
352 //=============================================================================
353 // Function: GetVolumes
354 // Purpose: Generate and return a list of all volumes (drives) available.
356 //=============================================================================
357 wxArrayString
wxFSVolume::GetVolumes(int flagsSet
, int flagsUnset
)
359 InterlockedExchange(&s_cancelSearch
, FALSE
); // reset
361 if (!s_mprLib
.IsLoaded() && s_mprLib
.Load(_T("mpr.dll")))
364 s_pWNetOpenEnum
= (WNetOpenEnumPtr
)s_mprLib
.GetSymbol(_T("WNetOpenEnumW"));
365 s_pWNetEnumResource
= (WNetEnumResourcePtr
)s_mprLib
.GetSymbol("WNetEnumResourceW");
367 s_pWNetOpenEnum
= (WNetOpenEnumPtr
)s_mprLib
.GetSymbol(_T("WNetOpenEnumA"));
368 s_pWNetEnumResource
= (WNetEnumResourcePtr
)s_mprLib
.GetSymbol(_T("WNetEnumResourceA"));
370 s_pWNetCloseEnum
= (WNetCloseEnumPtr
)s_mprLib
.GetSymbol(_T("WNetCloseEnum"));
375 //-------------------------------
376 // Local and mapped drives first.
377 //-------------------------------
378 // Allocate the required space for the API call.
379 size_t chars
= GetLogicalDriveStrings(0, 0);
380 TCHAR
* buf
= new TCHAR
[chars
+1];
382 // Get the list of drives.
383 chars
= GetLogicalDriveStrings(chars
, buf
);
385 // Parse the list into an array, applying appropriate filters.
390 FilteredAdd(list
, pVol
, flagsSet
, flagsUnset
);
391 pVol
= pVol
+ _tcslen(pVol
) + 1;
397 //---------------------------
398 // Network Neighborhood next.
399 //---------------------------
401 // not exclude remote and not removable
402 if (!(flagsUnset
& wxFS_VOL_REMOTE
) &&
403 !(flagsSet
& wxFS_VOL_REMOVABLE
)
406 // The returned list will be sorted alphabetically. We don't pass
407 // our in since we don't want to change to order of the local drives.
409 if (BuildRemoteList(nn
, 0, flagsSet
, flagsUnset
))
412 for (idx
= 0; idx
< nn
.GetCount(); idx
++)
420 //=============================================================================
421 // Function: CancelSearch
422 // Purpose: Instruct an active search to stop.
423 // Notes: - This will only sensibly be called by a thread other than the one
424 // performing the search. This is the only thread-safe function
425 // provided by the class.
426 //=============================================================================
427 void wxFSVolume::CancelSearch()
429 InterlockedExchange(&s_cancelSearch
, TRUE
);
432 //=============================================================================
433 // Function: constructor
434 // Purpose: default constructor
435 //=============================================================================
436 wxFSVolume::wxFSVolume()
441 //=============================================================================
442 // Function: constructor
443 // Purpose: constructor that calls Create
444 //=============================================================================
445 wxFSVolume::wxFSVolume(const wxString
& name
)
450 //=============================================================================
452 // Purpose: Finds, logs in, etc. to the request volume.
453 //=============================================================================
454 bool wxFSVolume::Create(const wxString
& name
)
464 long rc
= SHGetFileInfo(m_volName
, 0, &fi
, sizeof(fi
), SHGFI_DISPLAYNAME
);
467 wxLogError(_("Cannot read typename from '%s'!"), m_volName
);
470 m_dispName
= fi
.szDisplayName
;
474 m_icons
.Alloc(wxFS_VOL_ICO_MAX
);
477 for (idx
= 0; idx
< wxFS_VOL_ICO_MAX
; idx
++)
483 return m_isOk
= TRUE
;
486 //=============================================================================
488 // Purpose: returns TRUE if the volume is legal.
489 // Notes: For fixed disks, it must exist. For removable disks, it must also
490 // be present. For Network Shares, it must also be logged in, etc.
491 //=============================================================================
492 bool wxFSVolume::IsOk() const
497 //=============================================================================
499 // Purpose: Return the type of the volume.
500 //=============================================================================
501 wxFSVolumeKind
wxFSVolume::GetKind() const
504 return wxFS_VOL_OTHER
;
506 FileInfoMap::iterator itr
= s_fileInfo
.find(m_volName
);
507 if (itr
== s_fileInfo
.end())
508 return wxFS_VOL_OTHER
;
510 return itr
->second
.m_type
;
513 //=============================================================================
514 // Function: GetFlags
515 // Purpose: Return the caches flags for this volume.
516 // Notes: - Returns -1 if no flags were cached.
517 //=============================================================================
518 int wxFSVolume::GetFlags() const
523 FileInfoMap::iterator itr
= s_fileInfo
.find(m_volName
);
524 if (itr
== s_fileInfo
.end())
527 return itr
->second
.m_flags
;
532 //=============================================================================
534 // Purpose: return the requested icon.
535 //=============================================================================
536 wxIcon
wxFSVolume::GetIcon(wxFSIconType type
) const
538 wxASSERT(type
< m_icons
.GetCount());
540 if (type
>= m_icons
.GetCount())
542 wxLogError(_("Invalid request for icon type!"));
548 if (m_icons
[type
].IsNull())
553 case wxFS_VOL_ICO_SMALL
:
554 flags
= SHGFI_ICON
| SHGFI_SMALLICON
;
557 case wxFS_VOL_ICO_LARGE
:
558 flags
= SHGFI_ICON
| SHGFI_SHELLICONSIZE
;
561 case wxFS_VOL_ICO_SEL_SMALL
:
562 flags
= SHGFI_ICON
| SHGFI_SMALLICON
| SHGFI_OPENICON
;
565 case wxFS_VOL_ICO_SEL_LARGE
:
566 flags
= SHGFI_ICON
| SHGFI_SHELLICONSIZE
| SHGFI_OPENICON
;
571 long rc
= SHGetFileInfo(m_volName
, 0, &fi
, sizeof(fi
), flags
);
572 m_icons
[type
].SetHICON((WXHICON
)fi
.hIcon
);
573 if (!rc
|| !fi
.hIcon
)
574 wxLogError(_("Cannot load icon from '%s'."), m_volName
);
577 return m_icons
[type
];