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"
44 #ifndef SHGFI_ATTRIBUTES
45 #define SHGFI_ATTRIBUTES 2048
48 #ifndef SFGAO_READONLY
49 #define SFGAO_READONLY 0x00040000L
52 #ifndef SFGAO_REMOVABLE
53 #define SFGAO_REMOVABLE 0x02000000L
56 #ifndef SHGFI_DISPLAYNAME
57 #define SHGFI_DISPLAYNAME 512
61 #define SHGFI_ICON 256
64 #ifndef SHGFI_SMALLICON
65 #define SHGFI_SMALLICON 1
68 #ifndef SHGFI_SHELLICONSIZE
69 #define SHGFI_SHELLICONSIZE 4
72 #ifndef SHGFI_OPENICON
73 #define SHGFI_OPENICON 2
76 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
77 // Dynamic library function defs.
78 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
80 static wxDynamicLibrary s_mprLib
;
82 typedef DWORD (WINAPI
* WNetOpenEnumPtr
)(DWORD
, DWORD
, DWORD
, LPNETRESOURCE
, LPHANDLE
);
83 typedef DWORD (WINAPI
* WNetEnumResourcePtr
)(HANDLE
, LPDWORD
, LPVOID
, LPDWORD
);
84 typedef DWORD (WINAPI
* WNetCloseEnumPtr
)(HANDLE
);
86 static WNetOpenEnumPtr s_pWNetOpenEnum
;
87 static WNetEnumResourcePtr s_pWNetEnumResource
;
88 static WNetCloseEnumPtr s_pWNetCloseEnum
;
90 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
92 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
93 static long s_cancelSearch
= FALSE
;
95 struct FileInfo
: public wxObject
97 FileInfo(unsigned flag
=0, wxFSVolumeKind type
=wxFS_VOL_OTHER
) :
98 m_flags(flag
), m_type(type
) {}
100 FileInfo(const FileInfo
& other
) { *this = other
; }
101 FileInfo
& operator=(const FileInfo
& other
)
103 m_flags
= other
.m_flags
;
104 m_type
= other
.m_type
;
109 wxFSVolumeKind m_type
;
111 WX_DECLARE_STRING_HASH_MAP(FileInfo
, FileInfoMap
);
112 static FileInfoMap
s_fileInfo(25);
114 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
115 // Other initialization.
116 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
118 // already in wx/iconbndl.h
119 // WX_DEFINE_OBJARRAY(wxIconArray);
122 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
123 // Local helper functions.
124 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
126 //=============================================================================
127 // Function: GetBasicFlags
128 // Purpose: Set basic flags, primarily wxFS_VOL_REMOTE and wxFS_VOL_REMOVABLE.
129 // Notes: - Local and mapped drives are mounted by definition. We have no
130 // way to determine mounted status of network drives, so assume that
131 // all drives are mounted, and let the caller decide otherwise.
132 // - Other flags are 'best guess' from type of drive. The system will
133 // not report the file attributes with any degree of accuracy.
134 //=============================================================================
135 static unsigned GetBasicFlags(const wxChar
* filename
)
137 unsigned flags
= wxFS_VOL_MOUNTED
;
139 //----------------------------------
140 // 'Best Guess' based on drive type.
141 //----------------------------------
143 switch(GetDriveType(filename
))
146 type
= wxFS_VOL_DISK
;
149 case DRIVE_REMOVABLE
:
150 flags
|= wxFS_VOL_REMOVABLE
;
151 type
= wxFS_VOL_FLOPPY
;
155 flags
|= wxFS_VOL_REMOVABLE
| wxFS_VOL_READONLY
;
156 type
= wxFS_VOL_CDROM
;
160 flags
|= wxFS_VOL_REMOTE
;
161 type
= wxFS_VOL_NETWORK
;
164 case DRIVE_NO_ROOT_DIR
:
165 flags
&= ~wxFS_VOL_MOUNTED
;
166 type
= wxFS_VOL_OTHER
;
170 type
= wxFS_VOL_OTHER
;
174 //-----------------------------------------------------------------------
175 // The following will most likely will not modify anything not set above,
176 // and will not work at all for network shares or empty CD ROM drives.
177 // But it is a good check if the Win API ever gets better about reporting
179 //-----------------------------------------------------------------------
182 rc
= SHGetFileInfo(filename
, 0, &fi
, sizeof(fi
), SHGFI_ATTRIBUTES
);
185 wxLogError(_("Cannot read typename from '%s'!"), filename
);
189 if (fi
.dwAttributes
& SFGAO_READONLY
)
190 flags
|= wxFS_VOL_READONLY
;
191 if (fi
.dwAttributes
& SFGAO_REMOVABLE
)
192 flags
|= wxFS_VOL_REMOVABLE
;
198 s_fileInfo
[filename
] = FileInfo(flags
, type
);
203 //=============================================================================
204 // Function: FilteredAdd
205 // Purpose: Add a file to the list if it meets the filter requirement.
206 // Notes: - See GetBasicFlags for remarks about the Mounted flag.
207 //=============================================================================
208 static bool FilteredAdd(wxArrayString
& list
, const wxChar
* filename
,
209 unsigned flagsSet
, unsigned flagsUnset
)
212 unsigned flags
= GetBasicFlags(filename
);
214 if (flagsSet
& wxFS_VOL_MOUNTED
&& !(flags
& wxFS_VOL_MOUNTED
))
216 else if (flagsUnset
& wxFS_VOL_MOUNTED
&& (flags
& wxFS_VOL_MOUNTED
))
218 else if (flagsSet
& wxFS_VOL_REMOVABLE
&& !(flags
& wxFS_VOL_REMOVABLE
))
220 else if (flagsUnset
& wxFS_VOL_REMOVABLE
&& (flags
& wxFS_VOL_REMOVABLE
))
222 else if (flagsSet
& wxFS_VOL_READONLY
&& !(flags
& wxFS_VOL_READONLY
))
224 else if (flagsUnset
& wxFS_VOL_READONLY
&& (flags
& wxFS_VOL_READONLY
))
226 else if (flagsSet
& wxFS_VOL_REMOTE
&& !(flags
& wxFS_VOL_REMOTE
))
228 else if (flagsUnset
& wxFS_VOL_REMOTE
&& (flags
& wxFS_VOL_REMOTE
))
231 // Add to the list if passed the filter.
238 //=============================================================================
239 // Function: BuildListFromNN
240 // Purpose: Append or remove items from the list
241 // Notes: - There is no way to find all disconnected NN items, or even to find
242 // all items while determining which are connected and not. So this
243 // function will find either all items or connected items.
244 //=============================================================================
245 static void BuildListFromNN(wxArrayString
& list
, NETRESOURCE
* pResSrc
,
246 unsigned flagsSet
, unsigned flagsUnset
)
251 //-----------------------------------------------
252 // Scope may be all drives or all mounted drives.
253 //-----------------------------------------------
254 unsigned scope
= RESOURCE_GLOBALNET
;
255 if (flagsSet
& wxFS_VOL_MOUNTED
)
256 scope
= RESOURCE_CONNECTED
;
258 //----------------------------------------------------------------------
259 // Enumerate all items, adding only non-containers (ie. network shares).
260 // Containers cause a recursive call to this function for their own
262 //----------------------------------------------------------------------
263 if (rc
= s_pWNetOpenEnum(scope
, RESOURCETYPE_DISK
, 0, pResSrc
, &hEnum
), rc
== NO_ERROR
)
267 NETRESOURCE
* pRes
= (NETRESOURCE
*)malloc(size
);
268 memset(pRes
, 0, sizeof(NETRESOURCE
));
269 while (rc
= s_pWNetEnumResource(hEnum
, &count
, pRes
, &size
), rc
== NO_ERROR
|| rc
== ERROR_MORE_DATA
)
274 if (rc
== ERROR_MORE_DATA
)
276 pRes
= (NETRESOURCE
*)realloc(pRes
, size
);
281 // Enumerate the container.
282 if (pRes
->dwUsage
& RESOURCEUSAGE_CONTAINER
)
284 BuildListFromNN(list
, pRes
, flagsSet
, flagsUnset
);
287 // Add the network share.
290 wxString
filename(pRes
->lpRemoteName
);
294 if (filename
.Last() != '\\')
295 filename
.Append('\\');
297 // The filter function will not know mounted from unmounted, and neither do we unless
298 // we are iterating using RESOURCE_CONNECTED, in which case they all are mounted.
299 // Volumes on disconnected servers, however, will correctly show as unmounted.
300 FilteredAdd(list
, filename
, flagsSet
, flagsUnset
&~wxFS_VOL_MOUNTED
);
301 if (scope
== RESOURCE_GLOBALNET
)
302 s_fileInfo
[filename
].m_flags
&= ~wxFS_VOL_MOUNTED
;
310 s_pWNetCloseEnum(hEnum
);
314 //=============================================================================
315 // Function: CompareFcn
316 // Purpose: Used to sort the NN list alphabetically, case insensitive.
317 //=============================================================================
318 static int CompareFcn(const wxString
& first
, const wxString
& second
)
320 return wxStricmp(first
.c_str(), second
.c_str());
323 //=============================================================================
324 // Function: BuildRemoteList
325 // Purpose: Append Network Neighborhood items to the list.
326 // Notes: - Mounted gets transalated into Connected. FilteredAdd is told
327 // to ignore the Mounted flag since we need to handle it in a weird
329 // - The resulting list is sorted alphabetically.
330 //=============================================================================
331 static bool BuildRemoteList(wxArrayString
& list
, NETRESOURCE
* pResSrc
,
332 unsigned flagsSet
, unsigned flagsUnset
)
334 // NN query depends on dynamically loaded library.
335 if (!s_pWNetOpenEnum
|| !s_pWNetEnumResource
|| !s_pWNetCloseEnum
)
337 wxLogError(_("Failed to load mpr.dll."));
341 // Don't waste time doing the work if the flags conflict.
342 if (flagsSet
& wxFS_VOL_MOUNTED
&& flagsUnset
& wxFS_VOL_MOUNTED
)
345 //----------------------------------------------
346 // Generate the list according to the flags set.
347 //----------------------------------------------
348 BuildListFromNN(list
, pResSrc
, flagsSet
, flagsUnset
);
349 list
.Sort(CompareFcn
);
351 //-------------------------------------------------------------------------
352 // If mounted only is requested, then we only need one simple pass.
353 // Otherwise, we need to build a list of all NN volumes and then apply the
354 // list of mounted drives to it.
355 //-------------------------------------------------------------------------
356 if (!(flagsSet
& wxFS_VOL_MOUNTED
))
359 wxArrayString mounted
;
360 BuildListFromNN(mounted
, pResSrc
, flagsSet
| wxFS_VOL_MOUNTED
, flagsUnset
& ~wxFS_VOL_MOUNTED
);
361 mounted
.Sort(CompareFcn
);
363 // apply list from bottom to top to preserve indexes if removing items.
364 int iList
= list
.GetCount()-1;
366 for (iMounted
= mounted
.GetCount()-1; iMounted
>= 0 && iList
>= 0; iMounted
--)
369 wxString
all(list
[iList
]);
370 wxString
mount(mounted
[iMounted
]);
373 wxStricmp(list
[iList
].c_str(), mounted
[iMounted
].c_str()),
374 compare
> 0 && iList
>= 0)
383 // Found the element. Remove it or mark it mounted.
384 if (flagsUnset
& wxFS_VOL_MOUNTED
)
387 s_fileInfo
[list
[iList
]].m_flags
|= wxFS_VOL_MOUNTED
;
398 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
400 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
402 //=============================================================================
403 // Function: GetVolumes
404 // Purpose: Generate and return a list of all volumes (drives) available.
406 //=============================================================================
407 wxArrayString
wxFSVolume::GetVolumes(int flagsSet
, int flagsUnset
)
409 InterlockedExchange(&s_cancelSearch
, FALSE
); // reset
411 if (!s_mprLib
.IsLoaded() && s_mprLib
.Load(_T("mpr.dll")))
414 s_pWNetOpenEnum
= (WNetOpenEnumPtr
)s_mprLib
.GetSymbol(_T("WNetOpenEnumW"));
415 s_pWNetEnumResource
= (WNetEnumResourcePtr
)s_mprLib
.GetSymbol("WNetEnumResourceW");
417 s_pWNetOpenEnum
= (WNetOpenEnumPtr
)s_mprLib
.GetSymbol(_T("WNetOpenEnumA"));
418 s_pWNetEnumResource
= (WNetEnumResourcePtr
)s_mprLib
.GetSymbol(_T("WNetEnumResourceA"));
420 s_pWNetCloseEnum
= (WNetCloseEnumPtr
)s_mprLib
.GetSymbol(_T("WNetCloseEnum"));
425 //-------------------------------
426 // Local and mapped drives first.
427 //-------------------------------
428 // Allocate the required space for the API call.
429 size_t chars
= GetLogicalDriveStrings(0, 0);
430 TCHAR
* buf
= new TCHAR
[chars
+1];
432 // Get the list of drives.
433 chars
= GetLogicalDriveStrings(chars
, buf
);
435 // Parse the list into an array, applying appropriate filters.
440 FilteredAdd(list
, pVol
, flagsSet
, flagsUnset
);
441 pVol
= pVol
+ wxStrlen(pVol
) + 1;
447 //---------------------------
448 // Network Neighborhood next.
449 //---------------------------
451 // not exclude remote and not removable
452 if (!(flagsUnset
& wxFS_VOL_REMOTE
) &&
453 !(flagsSet
& wxFS_VOL_REMOVABLE
)
456 // The returned list will be sorted alphabetically. We don't pass
457 // our in since we don't want to change to order of the local drives.
459 if (BuildRemoteList(nn
, 0, flagsSet
, flagsUnset
))
461 for (size_t idx
= 0; idx
< nn
.GetCount(); idx
++)
469 //=============================================================================
470 // Function: CancelSearch
471 // Purpose: Instruct an active search to stop.
472 // Notes: - This will only sensibly be called by a thread other than the one
473 // performing the search. This is the only thread-safe function
474 // provided by the class.
475 //=============================================================================
476 void wxFSVolume::CancelSearch()
478 InterlockedExchange(&s_cancelSearch
, TRUE
);
481 //=============================================================================
482 // Function: constructor
483 // Purpose: default constructor
484 //=============================================================================
485 wxFSVolume::wxFSVolume()
490 //=============================================================================
491 // Function: constructor
492 // Purpose: constructor that calls Create
493 //=============================================================================
494 wxFSVolume::wxFSVolume(const wxString
& name
)
499 //=============================================================================
501 // Purpose: Finds, logs in, etc. to the request volume.
502 //=============================================================================
503 bool wxFSVolume::Create(const wxString
& name
)
513 long rc
= SHGetFileInfo(m_volName
, 0, &fi
, sizeof(fi
), SHGFI_DISPLAYNAME
);
516 wxLogError(_("Cannot read typename from '%s'!"), m_volName
.c_str());
519 m_dispName
= fi
.szDisplayName
;
523 m_icons
.Alloc(wxFS_VOL_ICO_MAX
);
526 for (idx
= 0; idx
< wxFS_VOL_ICO_MAX
; idx
++)
532 return m_isOk
= TRUE
;
535 //=============================================================================
537 // Purpose: returns TRUE if the volume is legal.
538 // Notes: For fixed disks, it must exist. For removable disks, it must also
539 // be present. For Network Shares, it must also be logged in, etc.
540 //=============================================================================
541 bool wxFSVolume::IsOk() const
546 //=============================================================================
548 // Purpose: Return the type of the volume.
549 //=============================================================================
550 wxFSVolumeKind
wxFSVolume::GetKind() const
553 return wxFS_VOL_OTHER
;
555 FileInfoMap::iterator itr
= s_fileInfo
.find(m_volName
);
556 if (itr
== s_fileInfo
.end())
557 return wxFS_VOL_OTHER
;
559 return itr
->second
.m_type
;
562 //=============================================================================
563 // Function: GetFlags
564 // Purpose: Return the caches flags for this volume.
565 // Notes: - Returns -1 if no flags were cached.
566 //=============================================================================
567 int wxFSVolume::GetFlags() const
572 FileInfoMap::iterator itr
= s_fileInfo
.find(m_volName
);
573 if (itr
== s_fileInfo
.end())
576 return itr
->second
.m_flags
;
581 //=============================================================================
583 // Purpose: return the requested icon.
584 //=============================================================================
585 wxIcon
wxFSVolume::GetIcon(wxFSIconType type
) const
587 wxCHECK_MSG(type
< (int)m_icons
.GetCount(), wxNullIcon
,
588 _T("Invalid request for icon type!"));
589 wxCHECK_MSG( type
>= 0 && (size_t)type
< m_icons
.GetCount(),
591 _T("invalid icon index") );
594 if (m_icons
[type
].IsNull())
599 case wxFS_VOL_ICO_SMALL
:
600 flags
= SHGFI_ICON
| SHGFI_SMALLICON
;
603 case wxFS_VOL_ICO_LARGE
:
604 flags
= SHGFI_ICON
| SHGFI_SHELLICONSIZE
;
607 case wxFS_VOL_ICO_SEL_SMALL
:
608 flags
= SHGFI_ICON
| SHGFI_SMALLICON
| SHGFI_OPENICON
;
611 case wxFS_VOL_ICO_SEL_LARGE
:
612 flags
= SHGFI_ICON
| SHGFI_SHELLICONSIZE
| SHGFI_OPENICON
;
615 case wxFS_VOL_ICO_MAX
:
616 wxFAIL_MSG(_T("wxFS_VOL_ICO_MAX is not valid icon type"));
621 long rc
= SHGetFileInfo(m_volName
, 0, &fi
, sizeof(fi
), flags
);
622 m_icons
[type
].SetHICON((WXHICON
)fi
.hIcon
);
623 if (!rc
|| !fi
.hIcon
)
624 wxLogError(_("Cannot load icon from '%s'."), m_volName
.c_str());
627 return m_icons
[type
];
632 #endif // wxUSE_FSVOLUME