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"
36 #include "wx/hashmap.h"
37 #include "wx/dynlib.h"
38 #include "wx/arrimpl.cpp"
40 #include "wx/volume.h"
45 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
46 // Dynamic library function defs.
47 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
49 static wxDynamicLibrary s_mprLib
;
51 typedef DWORD (WINAPI
* WNetOpenEnumPtr
)(DWORD
, DWORD
, DWORD
, LPNETRESOURCE
, LPHANDLE
);
52 typedef DWORD (WINAPI
* WNetEnumResourcePtr
)(HANDLE
, LPDWORD
, LPVOID
, LPDWORD
);
53 typedef DWORD (WINAPI
* WNetCloseEnumPtr
)(HANDLE
);
55 static WNetOpenEnumPtr s_pWNetOpenEnum
;
56 static WNetEnumResourcePtr s_pWNetEnumResource
;
57 static WNetCloseEnumPtr s_pWNetCloseEnum
;
59 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
61 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
62 static long s_cancelSearch
= FALSE
;
64 struct FileInfo
: public wxObject
66 FileInfo(unsigned flag
=0, wxFSVolumeKind type
=wxFS_VOL_OTHER
) :
67 m_flags(flag
), m_type(type
) {}
69 wxFSVolumeKind m_type
;
71 WX_DECLARE_STRING_HASH_MAP(FileInfo
, FileInfoMap
);
72 static FileInfoMap
s_fileInfo(25);
74 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
75 // Other initialization.
76 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
77 WX_DEFINE_OBJARRAY(wxIconArray
);
79 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
80 // Local helper functions.
81 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
83 //=============================================================================
84 // Function: GetBasicFlags
85 // Purpose: Set basic flags, primarily wxFS_VOL_REMOTE and wxFS_VOL_REMOVABLE.
86 // Notes: - Local and mapped drives are mounted by definition. We have no
87 // way to determine mounted status of network drives, so assume that
88 // all drives are mounted, and let the caller decide otherwise.
89 // - Other flags are 'best guess' from type of drive. The system will
90 // not report the file attributes with any degree of accuracy.
91 //=============================================================================
92 unsigned GetBasicFlags(const char* filename
)
94 unsigned flags
= wxFS_VOL_MOUNTED
;
96 //----------------------------------
97 // 'Best Guess' based on drive type.
98 //----------------------------------
100 switch(GetDriveType(filename
))
103 type
= wxFS_VOL_DISK
;
106 case DRIVE_REMOVABLE
:
107 flags
|= wxFS_VOL_REMOVABLE
;
108 type
= wxFS_VOL_FLOPPY
;
112 flags
|= wxFS_VOL_REMOVABLE
| wxFS_VOL_READONLY
;
113 type
= wxFS_VOL_CDROM
;
117 flags
|= wxFS_VOL_REMOTE
;
118 type
= wxFS_VOL_NETWORK
;
121 case DRIVE_NO_ROOT_DIR
:
122 flags
&= ~wxFS_VOL_MOUNTED
;
123 type
= wxFS_VOL_OTHER
;
127 type
= wxFS_VOL_OTHER
;
131 //-----------------------------------------------------------------------
132 // The following will most likely will not modify anything not set above,
133 // and will not work at all for network shares or empty CD ROM drives.
134 // But it is a good check if the Win API ever gets better about reporting
136 //-----------------------------------------------------------------------
139 rc
= SHGetFileInfo(filename
, 0, &fi
, sizeof(fi
), SHGFI_ATTRIBUTES
);
142 wxLogError(_("Cannot read typename from '%s'!"), filename
);
146 if (fi
.dwAttributes
& SFGAO_READONLY
)
147 flags
|= wxFS_VOL_READONLY
;
148 if (fi
.dwAttributes
& SFGAO_REMOVABLE
)
149 flags
|= wxFS_VOL_REMOVABLE
;
155 s_fileInfo
[filename
] = FileInfo(flags
, type
);
160 //=============================================================================
161 // Function: FilteredAdd
162 // Purpose: Add a file to the list if it meets the filter requirement.
163 // Notes: - See GetBasicFlags for remarks about the Mounted flag.
164 //=============================================================================
165 bool FilteredAdd(wxArrayString
& list
, const char* filename
, unsigned flagsSet
, unsigned flagsUnset
)
168 unsigned flags
= GetBasicFlags(filename
);
170 if (flagsSet
& wxFS_VOL_MOUNTED
&& !(flags
& wxFS_VOL_MOUNTED
))
172 else if (flagsUnset
& wxFS_VOL_MOUNTED
&& (flags
& wxFS_VOL_MOUNTED
))
174 else if (flagsSet
& wxFS_VOL_REMOVABLE
&& !(flags
& wxFS_VOL_REMOVABLE
))
176 else if (flagsUnset
& wxFS_VOL_REMOVABLE
&& (flags
& wxFS_VOL_REMOVABLE
))
178 else if (flagsSet
& wxFS_VOL_READONLY
&& !(flags
& wxFS_VOL_READONLY
))
180 else if (flagsUnset
& wxFS_VOL_READONLY
&& (flags
& wxFS_VOL_READONLY
))
182 else if (flagsSet
& wxFS_VOL_REMOTE
&& !(flags
& wxFS_VOL_REMOTE
))
184 else if (flagsUnset
& wxFS_VOL_REMOTE
&& (flags
& wxFS_VOL_REMOTE
))
187 // Add to the list if passed the filter.
194 //=============================================================================
195 // Function: BuildListFromNN
196 // Purpose: Append or remove items from the list
197 // Notes: - There is no way to find all disconnected NN items, or even to find
198 // all items while determining which are connected and not. So this
199 // function will find either all items or connected items.
200 //=============================================================================
201 void BuildListFromNN(wxArrayString
& list
, NETRESOURCE
* pResSrc
, unsigned flagsSet
, unsigned flagsUnset
)
206 //-----------------------------------------------
207 // Scope may be all drives or all mounted drives.
208 //-----------------------------------------------
209 unsigned scope
= RESOURCE_GLOBALNET
;
210 if (flagsSet
& wxFS_VOL_MOUNTED
)
211 scope
= RESOURCE_CONNECTED
;
213 //----------------------------------------------------------------------
214 // Enumerate all items, adding only non-containers (ie. network shares).
215 // Containers cause a recursive call to this function for their own
217 //----------------------------------------------------------------------
218 if (rc
= s_pWNetOpenEnum(scope
, RESOURCETYPE_DISK
, 0, pResSrc
, &hEnum
), rc
== NO_ERROR
)
220 unsigned long count
= 1;
221 unsigned long size
= 256;
222 NETRESOURCE
* pRes
= (NETRESOURCE
*)malloc(size
);
223 memset(pRes
, 0, sizeof(NETRESOURCE
));
224 while (rc
= s_pWNetEnumResource(hEnum
, &count
, pRes
, &size
), rc
== NO_ERROR
|| rc
== ERROR_MORE_DATA
)
229 if (rc
== ERROR_MORE_DATA
)
231 pRes
= (NETRESOURCE
*)realloc(pRes
, size
);
236 // Enumerate the container.
237 if (pRes
->dwUsage
& RESOURCEUSAGE_CONTAINER
)
239 BuildListFromNN(list
, pRes
, flagsSet
, flagsUnset
);
242 // Add the network share.
245 wxString
filename(pRes
->lpRemoteName
);
249 if (filename
.Last() != '\\')
250 filename
.Append('\\');
252 // The filter function will not know mounted from unmounted, and neither do we unless
253 // we are iterating using RESOURCE_CONNECTED, in which case they all are mounted.
254 // Volumes on disconnected servers, however, will correctly show as unmounted.
255 FilteredAdd(list
, filename
, flagsSet
, flagsUnset
&~wxFS_VOL_MOUNTED
);
256 if (scope
== RESOURCE_GLOBALNET
)
257 s_fileInfo
[filename
].m_flags
&= ~wxFS_VOL_MOUNTED
;
265 s_pWNetCloseEnum(hEnum
);
269 //=============================================================================
270 // Function: CompareFcn
271 // Purpose: Used to sort the NN list alphabetically, case insensitive.
272 //=============================================================================
273 static int CompareFcn(const wxString
& first
, const wxString
& second
)
275 return stricmp(first
.c_str(), second
.c_str());
278 //=============================================================================
279 // Function: BuildRemoteList
280 // Purpose: Append Network Neighborhood items to the list.
281 // Notes: - Mounted gets transalated into Connected. FilteredAdd is told
282 // to ignore the Mounted flag since we need to handle it in a weird
284 // - The resulting list is sorted alphabetically.
285 //=============================================================================
286 bool BuildRemoteList(wxArrayString
& list
, NETRESOURCE
* pResSrc
, unsigned flagsSet
, unsigned flagsUnset
)
288 // NN query depends on dynamically loaded library.
289 if (!s_pWNetOpenEnum
|| !s_pWNetEnumResource
|| !s_pWNetCloseEnum
)
291 wxLogError(_("Failed to load mpr.dll."));
295 // Don't waste time doing the work if the flags conflict.
296 if (flagsSet
& wxFS_VOL_MOUNTED
&& flagsUnset
& wxFS_VOL_MOUNTED
)
299 //----------------------------------------------
300 // Generate the list according to the flags set.
301 //----------------------------------------------
302 BuildListFromNN(list
, pResSrc
, flagsSet
, flagsUnset
);
303 list
.Sort(CompareFcn
);
305 //-------------------------------------------------------------------------
306 // If mounted only is requested, then we only need one simple pass.
307 // Otherwise, we need to build a list of all NN volumes and then apply the
308 // list of mounted drives to it.
309 //-------------------------------------------------------------------------
310 if (!(flagsSet
& wxFS_VOL_MOUNTED
))
313 wxArrayString mounted
;
314 BuildListFromNN(mounted
, pResSrc
, flagsSet
| wxFS_VOL_MOUNTED
, flagsUnset
& ~wxFS_VOL_MOUNTED
);
315 mounted
.Sort(CompareFcn
);
317 // apply list from bottom to top to preserve indexes if removing items.
318 int iList
= list
.GetCount()-1;
320 for (iMounted
= mounted
.GetCount()-1; iMounted
>= 0 && iList
>= 0; iMounted
--)
323 wxString
all(list
[iList
]);
324 wxString
mount(mounted
[iMounted
]);
326 while (compare
= stricmp(list
[iList
], mounted
[iMounted
]), compare
> 0 && iList
>= 0)
335 // Found the element. Remove it or mark it mounted.
336 if (flagsUnset
& wxFS_VOL_MOUNTED
)
339 s_fileInfo
[list
[iList
]].m_flags
|= wxFS_VOL_MOUNTED
;
350 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
352 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
354 //=============================================================================
355 // Function: GetVolumes
356 // Purpose: Generate and return a list of all volumes (drives) available.
358 //=============================================================================
359 wxArrayString
wxFSVolume::GetVolumes(int flagsSet
, int flagsUnset
)
361 InterlockedExchange(&s_cancelSearch
, FALSE
); // reset
363 if (!s_mprLib
.IsLoaded() && s_mprLib
.Load(_T("mpr.dll")))
366 s_pWNetOpenEnum
= (WNetOpenEnumPtr
)s_mprLib
.GetSymbol(_T("WNetOpenEnumW"));
367 s_pWNetEnumResource
= (WNetEnumResourcePtr
)s_mprLib
.GetSymbol("WNetEnumResourceW");
369 s_pWNetOpenEnum
= (WNetOpenEnumPtr
)s_mprLib
.GetSymbol(_T("WNetOpenEnumA"));
370 s_pWNetEnumResource
= (WNetEnumResourcePtr
)s_mprLib
.GetSymbol(_T("WNetEnumResourceA"));
372 s_pWNetCloseEnum
= (WNetCloseEnumPtr
)s_mprLib
.GetSymbol(_T("WNetCloseEnum"));
377 //-------------------------------
378 // Local and mapped drives first.
379 //-------------------------------
380 // Allocate the required space for the API call.
381 size_t chars
= GetLogicalDriveStrings(0, 0);
382 TCHAR
* buf
= new TCHAR
[chars
+1];
384 // Get the list of drives.
385 chars
= GetLogicalDriveStrings(chars
, buf
);
387 // Parse the list into an array, applying appropriate filters.
392 FilteredAdd(list
, pVol
, flagsSet
, flagsUnset
);
393 pVol
= pVol
+ wxStrlen(pVol
) + 1;
399 //---------------------------
400 // Network Neighborhood next.
401 //---------------------------
403 // not exclude remote and not removable
404 if (!(flagsUnset
& wxFS_VOL_REMOTE
) &&
405 !(flagsSet
& wxFS_VOL_REMOVABLE
)
408 // The returned list will be sorted alphabetically. We don't pass
409 // our in since we don't want to change to order of the local drives.
411 if (BuildRemoteList(nn
, 0, flagsSet
, flagsUnset
))
413 for (size_t idx
= 0; idx
< nn
.GetCount(); idx
++)
421 //=============================================================================
422 // Function: CancelSearch
423 // Purpose: Instruct an active search to stop.
424 // Notes: - This will only sensibly be called by a thread other than the one
425 // performing the search. This is the only thread-safe function
426 // provided by the class.
427 //=============================================================================
428 void wxFSVolume::CancelSearch()
430 InterlockedExchange(&s_cancelSearch
, TRUE
);
433 //=============================================================================
434 // Function: constructor
435 // Purpose: default constructor
436 //=============================================================================
437 wxFSVolume::wxFSVolume()
442 //=============================================================================
443 // Function: constructor
444 // Purpose: constructor that calls Create
445 //=============================================================================
446 wxFSVolume::wxFSVolume(const wxString
& name
)
451 //=============================================================================
453 // Purpose: Finds, logs in, etc. to the request volume.
454 //=============================================================================
455 bool wxFSVolume::Create(const wxString
& name
)
465 long rc
= SHGetFileInfo(m_volName
, 0, &fi
, sizeof(fi
), SHGFI_DISPLAYNAME
);
468 wxLogError(_("Cannot read typename from '%s'!"), m_volName
);
471 m_dispName
= fi
.szDisplayName
;
475 m_icons
.Alloc(wxFS_VOL_ICO_MAX
);
478 for (idx
= 0; idx
< wxFS_VOL_ICO_MAX
; idx
++)
484 return m_isOk
= TRUE
;
487 //=============================================================================
489 // Purpose: returns TRUE if the volume is legal.
490 // Notes: For fixed disks, it must exist. For removable disks, it must also
491 // be present. For Network Shares, it must also be logged in, etc.
492 //=============================================================================
493 bool wxFSVolume::IsOk() const
498 //=============================================================================
500 // Purpose: Return the type of the volume.
501 //=============================================================================
502 wxFSVolumeKind
wxFSVolume::GetKind() const
505 return wxFS_VOL_OTHER
;
507 FileInfoMap::iterator itr
= s_fileInfo
.find(m_volName
);
508 if (itr
== s_fileInfo
.end())
509 return wxFS_VOL_OTHER
;
511 return itr
->second
.m_type
;
514 //=============================================================================
515 // Function: GetFlags
516 // Purpose: Return the caches flags for this volume.
517 // Notes: - Returns -1 if no flags were cached.
518 //=============================================================================
519 int wxFSVolume::GetFlags() const
524 FileInfoMap::iterator itr
= s_fileInfo
.find(m_volName
);
525 if (itr
== s_fileInfo
.end())
528 return itr
->second
.m_flags
;
533 //=============================================================================
535 // Purpose: return the requested icon.
536 //=============================================================================
537 wxIcon
wxFSVolume::GetIcon(wxFSIconType type
) const
539 wxCHECK_MSG( type
>= 0 && (size_t)type
< m_icons
.GetCount(),
541 _T("invalid icon index") );
544 if (m_icons
[type
].IsNull())
549 case wxFS_VOL_ICO_SMALL
:
550 flags
= SHGFI_ICON
| SHGFI_SMALLICON
;
553 case wxFS_VOL_ICO_LARGE
:
554 flags
= SHGFI_ICON
| SHGFI_SHELLICONSIZE
;
557 case wxFS_VOL_ICO_SEL_SMALL
:
558 flags
= SHGFI_ICON
| SHGFI_SMALLICON
| SHGFI_OPENICON
;
561 case wxFS_VOL_ICO_SEL_LARGE
:
562 flags
= SHGFI_ICON
| SHGFI_SHELLICONSIZE
| SHGFI_OPENICON
;
567 long rc
= SHGetFileInfo(m_volName
, 0, &fi
, sizeof(fi
), flags
);
568 m_icons
[type
].SetHICON((WXHICON
)fi
.hIcon
);
569 if (!rc
|| !fi
.hIcon
)
570 wxLogError(_("Cannot load icon from '%s'."), m_volName
);
573 return m_icons
[type
];