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"
38 #include "wx/hashmap.h"
39 #include "wx/dynlib.h"
40 #include "wx/arrimpl.cpp"
42 #include "wx/volume.h"
47 #ifndef SHGFI_ATTRIBUTES
48 #define SHGFI_ATTRIBUTES 2048
51 #ifndef SFGAO_READONLY
52 #define SFGAO_READONLY 0x00040000L
55 #ifndef SFGAO_REMOVABLE
56 #define SFGAO_REMOVABLE 0x02000000L
59 #ifndef SHGFI_DISPLAYNAME
60 #define SHGFI_DISPLAYNAME 512
64 #define SHGFI_ICON 256
67 #ifndef SHGFI_SMALLICON
68 #define SHGFI_SMALLICON 1
71 #ifndef SHGFI_SHELLICONSIZE
72 #define SHGFI_SHELLICONSIZE 4
75 #ifndef SHGFI_OPENICON
76 #define SHGFI_OPENICON 2
79 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
80 // Dynamic library function defs.
81 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
83 static wxDynamicLibrary s_mprLib
;
85 typedef DWORD (WINAPI
* WNetOpenEnumPtr
)(DWORD
, DWORD
, DWORD
, LPNETRESOURCE
, LPHANDLE
);
86 typedef DWORD (WINAPI
* WNetEnumResourcePtr
)(HANDLE
, LPDWORD
, LPVOID
, LPDWORD
);
87 typedef DWORD (WINAPI
* WNetCloseEnumPtr
)(HANDLE
);
89 static WNetOpenEnumPtr s_pWNetOpenEnum
;
90 static WNetEnumResourcePtr s_pWNetEnumResource
;
91 static WNetCloseEnumPtr s_pWNetCloseEnum
;
93 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
95 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
96 static long s_cancelSearch
= FALSE
;
98 struct FileInfo
: public wxObject
100 FileInfo(unsigned flag
=0, wxFSVolumeKind type
=wxFS_VOL_OTHER
) :
101 m_flags(flag
), m_type(type
) {}
103 wxFSVolumeKind m_type
;
105 WX_DECLARE_STRING_HASH_MAP(FileInfo
, FileInfoMap
);
106 static FileInfoMap
s_fileInfo(25);
108 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
109 // Other initialization.
110 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
112 WX_DEFINE_OBJARRAY(wxIconArray
);
115 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
116 // Local helper functions.
117 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
119 //=============================================================================
120 // Function: GetBasicFlags
121 // Purpose: Set basic flags, primarily wxFS_VOL_REMOTE and wxFS_VOL_REMOVABLE.
122 // Notes: - Local and mapped drives are mounted by definition. We have no
123 // way to determine mounted status of network drives, so assume that
124 // all drives are mounted, and let the caller decide otherwise.
125 // - Other flags are 'best guess' from type of drive. The system will
126 // not report the file attributes with any degree of accuracy.
127 //=============================================================================
128 static unsigned GetBasicFlags(const wxChar
* filename
)
130 unsigned flags
= wxFS_VOL_MOUNTED
;
132 //----------------------------------
133 // 'Best Guess' based on drive type.
134 //----------------------------------
136 switch(GetDriveType(filename
))
139 type
= wxFS_VOL_DISK
;
142 case DRIVE_REMOVABLE
:
143 flags
|= wxFS_VOL_REMOVABLE
;
144 type
= wxFS_VOL_FLOPPY
;
148 flags
|= wxFS_VOL_REMOVABLE
| wxFS_VOL_READONLY
;
149 type
= wxFS_VOL_CDROM
;
153 flags
|= wxFS_VOL_REMOTE
;
154 type
= wxFS_VOL_NETWORK
;
157 case DRIVE_NO_ROOT_DIR
:
158 flags
&= ~wxFS_VOL_MOUNTED
;
159 type
= wxFS_VOL_OTHER
;
163 type
= wxFS_VOL_OTHER
;
167 //-----------------------------------------------------------------------
168 // The following will most likely will not modify anything not set above,
169 // and will not work at all for network shares or empty CD ROM drives.
170 // But it is a good check if the Win API ever gets better about reporting
172 //-----------------------------------------------------------------------
175 rc
= SHGetFileInfo(filename
, 0, &fi
, sizeof(fi
), SHGFI_ATTRIBUTES
);
178 wxLogError(_("Cannot read typename from '%s'!"), filename
);
182 if (fi
.dwAttributes
& SFGAO_READONLY
)
183 flags
|= wxFS_VOL_READONLY
;
184 if (fi
.dwAttributes
& SFGAO_REMOVABLE
)
185 flags
|= wxFS_VOL_REMOVABLE
;
191 s_fileInfo
[filename
] = FileInfo(flags
, type
);
196 //=============================================================================
197 // Function: FilteredAdd
198 // Purpose: Add a file to the list if it meets the filter requirement.
199 // Notes: - See GetBasicFlags for remarks about the Mounted flag.
200 //=============================================================================
201 static bool FilteredAdd(wxArrayString
& list
, const wxChar
* filename
,
202 unsigned flagsSet
, unsigned flagsUnset
)
205 unsigned flags
= GetBasicFlags(filename
);
207 if (flagsSet
& wxFS_VOL_MOUNTED
&& !(flags
& wxFS_VOL_MOUNTED
))
209 else if (flagsUnset
& wxFS_VOL_MOUNTED
&& (flags
& wxFS_VOL_MOUNTED
))
211 else if (flagsSet
& wxFS_VOL_REMOVABLE
&& !(flags
& wxFS_VOL_REMOVABLE
))
213 else if (flagsUnset
& wxFS_VOL_REMOVABLE
&& (flags
& wxFS_VOL_REMOVABLE
))
215 else if (flagsSet
& wxFS_VOL_READONLY
&& !(flags
& wxFS_VOL_READONLY
))
217 else if (flagsUnset
& wxFS_VOL_READONLY
&& (flags
& wxFS_VOL_READONLY
))
219 else if (flagsSet
& wxFS_VOL_REMOTE
&& !(flags
& wxFS_VOL_REMOTE
))
221 else if (flagsUnset
& wxFS_VOL_REMOTE
&& (flags
& wxFS_VOL_REMOTE
))
224 // Add to the list if passed the filter.
231 //=============================================================================
232 // Function: BuildListFromNN
233 // Purpose: Append or remove items from the list
234 // Notes: - There is no way to find all disconnected NN items, or even to find
235 // all items while determining which are connected and not. So this
236 // function will find either all items or connected items.
237 //=============================================================================
238 static void BuildListFromNN(wxArrayString
& list
, NETRESOURCE
* pResSrc
,
239 unsigned flagsSet
, unsigned flagsUnset
)
244 //-----------------------------------------------
245 // Scope may be all drives or all mounted drives.
246 //-----------------------------------------------
247 unsigned scope
= RESOURCE_GLOBALNET
;
248 if (flagsSet
& wxFS_VOL_MOUNTED
)
249 scope
= RESOURCE_CONNECTED
;
251 //----------------------------------------------------------------------
252 // Enumerate all items, adding only non-containers (ie. network shares).
253 // Containers cause a recursive call to this function for their own
255 //----------------------------------------------------------------------
256 if (rc
= s_pWNetOpenEnum(scope
, RESOURCETYPE_DISK
, 0, pResSrc
, &hEnum
), rc
== NO_ERROR
)
260 NETRESOURCE
* pRes
= (NETRESOURCE
*)malloc(size
);
261 memset(pRes
, 0, sizeof(NETRESOURCE
));
262 while (rc
= s_pWNetEnumResource(hEnum
, &count
, pRes
, &size
), rc
== NO_ERROR
|| rc
== ERROR_MORE_DATA
)
267 if (rc
== ERROR_MORE_DATA
)
269 pRes
= (NETRESOURCE
*)realloc(pRes
, size
);
274 // Enumerate the container.
275 if (pRes
->dwUsage
& RESOURCEUSAGE_CONTAINER
)
277 BuildListFromNN(list
, pRes
, flagsSet
, flagsUnset
);
280 // Add the network share.
283 wxString
filename(pRes
->lpRemoteName
);
287 if (filename
.Last() != '\\')
288 filename
.Append('\\');
290 // The filter function will not know mounted from unmounted, and neither do we unless
291 // we are iterating using RESOURCE_CONNECTED, in which case they all are mounted.
292 // Volumes on disconnected servers, however, will correctly show as unmounted.
293 FilteredAdd(list
, filename
, flagsSet
, flagsUnset
&~wxFS_VOL_MOUNTED
);
294 if (scope
== RESOURCE_GLOBALNET
)
295 s_fileInfo
[filename
].m_flags
&= ~wxFS_VOL_MOUNTED
;
303 s_pWNetCloseEnum(hEnum
);
307 //=============================================================================
308 // Function: CompareFcn
309 // Purpose: Used to sort the NN list alphabetically, case insensitive.
310 //=============================================================================
311 static int CompareFcn(const wxString
& first
, const wxString
& second
)
313 return wxStricmp(first
.c_str(), second
.c_str());
316 //=============================================================================
317 // Function: BuildRemoteList
318 // Purpose: Append Network Neighborhood items to the list.
319 // Notes: - Mounted gets transalated into Connected. FilteredAdd is told
320 // to ignore the Mounted flag since we need to handle it in a weird
322 // - The resulting list is sorted alphabetically.
323 //=============================================================================
324 static bool BuildRemoteList(wxArrayString
& list
, NETRESOURCE
* pResSrc
,
325 unsigned flagsSet
, unsigned flagsUnset
)
327 // NN query depends on dynamically loaded library.
328 if (!s_pWNetOpenEnum
|| !s_pWNetEnumResource
|| !s_pWNetCloseEnum
)
330 wxLogError(_("Failed to load mpr.dll."));
334 // Don't waste time doing the work if the flags conflict.
335 if (flagsSet
& wxFS_VOL_MOUNTED
&& flagsUnset
& wxFS_VOL_MOUNTED
)
338 //----------------------------------------------
339 // Generate the list according to the flags set.
340 //----------------------------------------------
341 BuildListFromNN(list
, pResSrc
, flagsSet
, flagsUnset
);
342 list
.Sort(CompareFcn
);
344 //-------------------------------------------------------------------------
345 // If mounted only is requested, then we only need one simple pass.
346 // Otherwise, we need to build a list of all NN volumes and then apply the
347 // list of mounted drives to it.
348 //-------------------------------------------------------------------------
349 if (!(flagsSet
& wxFS_VOL_MOUNTED
))
352 wxArrayString mounted
;
353 BuildListFromNN(mounted
, pResSrc
, flagsSet
| wxFS_VOL_MOUNTED
, flagsUnset
& ~wxFS_VOL_MOUNTED
);
354 mounted
.Sort(CompareFcn
);
356 // apply list from bottom to top to preserve indexes if removing items.
357 int iList
= list
.GetCount()-1;
359 for (iMounted
= mounted
.GetCount()-1; iMounted
>= 0 && iList
>= 0; iMounted
--)
362 wxString
all(list
[iList
]);
363 wxString
mount(mounted
[iMounted
]);
366 wxStricmp(list
[iList
].c_str(), mounted
[iMounted
].c_str()),
367 compare
> 0 && iList
>= 0)
376 // Found the element. Remove it or mark it mounted.
377 if (flagsUnset
& wxFS_VOL_MOUNTED
)
380 s_fileInfo
[list
[iList
]].m_flags
|= wxFS_VOL_MOUNTED
;
391 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
393 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
395 //=============================================================================
396 // Function: GetVolumes
397 // Purpose: Generate and return a list of all volumes (drives) available.
399 //=============================================================================
400 wxArrayString
wxFSVolume::GetVolumes(int flagsSet
, int flagsUnset
)
402 InterlockedExchange(&s_cancelSearch
, FALSE
); // reset
404 if (!s_mprLib
.IsLoaded() && s_mprLib
.Load(_T("mpr.dll")))
407 s_pWNetOpenEnum
= (WNetOpenEnumPtr
)s_mprLib
.GetSymbol(_T("WNetOpenEnumW"));
408 s_pWNetEnumResource
= (WNetEnumResourcePtr
)s_mprLib
.GetSymbol("WNetEnumResourceW");
410 s_pWNetOpenEnum
= (WNetOpenEnumPtr
)s_mprLib
.GetSymbol(_T("WNetOpenEnumA"));
411 s_pWNetEnumResource
= (WNetEnumResourcePtr
)s_mprLib
.GetSymbol(_T("WNetEnumResourceA"));
413 s_pWNetCloseEnum
= (WNetCloseEnumPtr
)s_mprLib
.GetSymbol(_T("WNetCloseEnum"));
418 //-------------------------------
419 // Local and mapped drives first.
420 //-------------------------------
421 // Allocate the required space for the API call.
422 size_t chars
= GetLogicalDriveStrings(0, 0);
423 TCHAR
* buf
= new TCHAR
[chars
+1];
425 // Get the list of drives.
426 chars
= GetLogicalDriveStrings(chars
, buf
);
428 // Parse the list into an array, applying appropriate filters.
433 FilteredAdd(list
, pVol
, flagsSet
, flagsUnset
);
434 pVol
= pVol
+ wxStrlen(pVol
) + 1;
440 //---------------------------
441 // Network Neighborhood next.
442 //---------------------------
444 // not exclude remote and not removable
445 if (!(flagsUnset
& wxFS_VOL_REMOTE
) &&
446 !(flagsSet
& wxFS_VOL_REMOVABLE
)
449 // The returned list will be sorted alphabetically. We don't pass
450 // our in since we don't want to change to order of the local drives.
452 if (BuildRemoteList(nn
, 0, flagsSet
, flagsUnset
))
454 for (size_t idx
= 0; idx
< nn
.GetCount(); idx
++)
462 //=============================================================================
463 // Function: CancelSearch
464 // Purpose: Instruct an active search to stop.
465 // Notes: - This will only sensibly be called by a thread other than the one
466 // performing the search. This is the only thread-safe function
467 // provided by the class.
468 //=============================================================================
469 void wxFSVolume::CancelSearch()
471 InterlockedExchange(&s_cancelSearch
, TRUE
);
474 //=============================================================================
475 // Function: constructor
476 // Purpose: default constructor
477 //=============================================================================
478 wxFSVolume::wxFSVolume()
483 //=============================================================================
484 // Function: constructor
485 // Purpose: constructor that calls Create
486 //=============================================================================
487 wxFSVolume::wxFSVolume(const wxString
& name
)
492 //=============================================================================
494 // Purpose: Finds, logs in, etc. to the request volume.
495 //=============================================================================
496 bool wxFSVolume::Create(const wxString
& name
)
506 long rc
= SHGetFileInfo(m_volName
, 0, &fi
, sizeof(fi
), SHGFI_DISPLAYNAME
);
509 wxLogError(_("Cannot read typename from '%s'!"), m_volName
.c_str());
512 m_dispName
= fi
.szDisplayName
;
516 m_icons
.Alloc(wxFS_VOL_ICO_MAX
);
519 for (idx
= 0; idx
< wxFS_VOL_ICO_MAX
; idx
++)
525 return m_isOk
= TRUE
;
528 //=============================================================================
530 // Purpose: returns TRUE if the volume is legal.
531 // Notes: For fixed disks, it must exist. For removable disks, it must also
532 // be present. For Network Shares, it must also be logged in, etc.
533 //=============================================================================
534 bool wxFSVolume::IsOk() const
539 //=============================================================================
541 // Purpose: Return the type of the volume.
542 //=============================================================================
543 wxFSVolumeKind
wxFSVolume::GetKind() const
546 return wxFS_VOL_OTHER
;
548 FileInfoMap::iterator itr
= s_fileInfo
.find(m_volName
);
549 if (itr
== s_fileInfo
.end())
550 return wxFS_VOL_OTHER
;
552 return itr
->second
.m_type
;
555 //=============================================================================
556 // Function: GetFlags
557 // Purpose: Return the caches flags for this volume.
558 // Notes: - Returns -1 if no flags were cached.
559 //=============================================================================
560 int wxFSVolume::GetFlags() const
565 FileInfoMap::iterator itr
= s_fileInfo
.find(m_volName
);
566 if (itr
== s_fileInfo
.end())
569 return itr
->second
.m_flags
;
574 //=============================================================================
576 // Purpose: return the requested icon.
577 //=============================================================================
578 wxIcon
wxFSVolume::GetIcon(wxFSIconType type
) const
580 wxCHECK_MSG(type
< (int)m_icons
.GetCount(), wxNullIcon
,
581 _T("Invalid request for icon type!"));
582 wxCHECK_MSG( type
>= 0 && (size_t)type
< m_icons
.GetCount(),
584 _T("invalid icon index") );
587 if (m_icons
[type
].IsNull())
592 case wxFS_VOL_ICO_SMALL
:
593 flags
= SHGFI_ICON
| SHGFI_SMALLICON
;
596 case wxFS_VOL_ICO_LARGE
:
597 flags
= SHGFI_ICON
| SHGFI_SHELLICONSIZE
;
600 case wxFS_VOL_ICO_SEL_SMALL
:
601 flags
= SHGFI_ICON
| SHGFI_SMALLICON
| SHGFI_OPENICON
;
604 case wxFS_VOL_ICO_SEL_LARGE
:
605 flags
= SHGFI_ICON
| SHGFI_SHELLICONSIZE
| SHGFI_OPENICON
;
608 case wxFS_VOL_ICO_MAX
:
609 wxFAIL_MSG(_T("wxFS_VOL_ICO_MAX is not valid icon type"));
614 long rc
= SHGetFileInfo(m_volName
, 0, &fi
, sizeof(fi
), flags
);
615 m_icons
[type
].SetHICON((WXHICON
)fi
.hIcon
);
616 if (!rc
|| !fi
.hIcon
)
617 wxLogError(_("Cannot load icon from '%s'."), m_volName
.c_str());
620 return m_icons
[type
];
625 #endif // wxUSE_FSVOLUME