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"
48 #define SHGetFileInfo SHGetFileInfoW
50 #define SHGetFileInfo SHGetFileInfoA
54 #ifndef SHGFI_ATTRIBUTES
55 #define SHGFI_ATTRIBUTES 2048
58 #ifndef SFGAO_READONLY
59 #define SFGAO_READONLY 0x00040000L
62 #ifndef SFGAO_REMOVABLE
63 #define SFGAO_REMOVABLE 0x02000000L
66 #ifndef SHGFI_DISPLAYNAME
67 #define SHGFI_DISPLAYNAME 512
71 #define SHGFI_ICON 256
74 #ifndef SHGFI_SMALLICON
75 #define SHGFI_SMALLICON 1
78 #ifndef SHGFI_SHELLICONSIZE
79 #define SHGFI_SHELLICONSIZE 4
82 #ifndef SHGFI_OPENICON
83 #define SHGFI_OPENICON 2
86 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
87 // Dynamic library function defs.
88 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
90 static wxDynamicLibrary s_mprLib
;
92 typedef DWORD (WINAPI
* WNetOpenEnumPtr
)(DWORD
, DWORD
, DWORD
, LPNETRESOURCE
, LPHANDLE
);
93 typedef DWORD (WINAPI
* WNetEnumResourcePtr
)(HANDLE
, LPDWORD
, LPVOID
, LPDWORD
);
94 typedef DWORD (WINAPI
* WNetCloseEnumPtr
)(HANDLE
);
96 static WNetOpenEnumPtr s_pWNetOpenEnum
;
97 static WNetEnumResourcePtr s_pWNetEnumResource
;
98 static WNetCloseEnumPtr s_pWNetCloseEnum
;
100 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
102 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
103 static long s_cancelSearch
= FALSE
;
105 struct FileInfo
: public wxObject
107 FileInfo(unsigned flag
=0, wxFSVolumeKind type
=wxFS_VOL_OTHER
) :
108 m_flags(flag
), m_type(type
) {}
110 FileInfo(const FileInfo
& other
) { *this = other
; }
111 FileInfo
& operator=(const FileInfo
& other
)
113 m_flags
= other
.m_flags
;
114 m_type
= other
.m_type
;
119 wxFSVolumeKind m_type
;
121 WX_DECLARE_STRING_HASH_MAP(FileInfo
, FileInfoMap
);
122 static FileInfoMap
s_fileInfo(25);
124 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
125 // Other initialization.
126 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
128 // already in wx/iconbndl.h
129 // WX_DEFINE_OBJARRAY(wxIconArray);
132 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
133 // Local helper functions.
134 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
136 //=============================================================================
137 // Function: GetBasicFlags
138 // Purpose: Set basic flags, primarily wxFS_VOL_REMOTE and wxFS_VOL_REMOVABLE.
139 // Notes: - Local and mapped drives are mounted by definition. We have no
140 // way to determine mounted status of network drives, so assume that
141 // all drives are mounted, and let the caller decide otherwise.
142 // - Other flags are 'best guess' from type of drive. The system will
143 // not report the file attributes with any degree of accuracy.
144 //=============================================================================
145 static unsigned GetBasicFlags(const wxChar
* filename
)
147 unsigned flags
= wxFS_VOL_MOUNTED
;
149 //----------------------------------
150 // 'Best Guess' based on drive type.
151 //----------------------------------
153 switch(GetDriveType(filename
))
156 type
= wxFS_VOL_DISK
;
159 case DRIVE_REMOVABLE
:
160 flags
|= wxFS_VOL_REMOVABLE
;
161 type
= wxFS_VOL_FLOPPY
;
165 flags
|= wxFS_VOL_REMOVABLE
| wxFS_VOL_READONLY
;
166 type
= wxFS_VOL_CDROM
;
170 flags
|= wxFS_VOL_REMOTE
;
171 type
= wxFS_VOL_NETWORK
;
174 case DRIVE_NO_ROOT_DIR
:
175 flags
&= ~wxFS_VOL_MOUNTED
;
176 type
= wxFS_VOL_OTHER
;
180 type
= wxFS_VOL_OTHER
;
184 //-----------------------------------------------------------------------
185 // The following will most likely will not modify anything not set above,
186 // and will not work at all for network shares or empty CD ROM drives.
187 // But it is a good check if the Win API ever gets better about reporting
189 //-----------------------------------------------------------------------
192 rc
= SHGetFileInfo(filename
, 0, &fi
, sizeof(fi
), SHGFI_ATTRIBUTES
);
195 wxLogError(_("Cannot read typename from '%s'!"), filename
);
199 if (fi
.dwAttributes
& SFGAO_READONLY
)
200 flags
|= wxFS_VOL_READONLY
;
201 if (fi
.dwAttributes
& SFGAO_REMOVABLE
)
202 flags
|= wxFS_VOL_REMOVABLE
;
208 s_fileInfo
[filename
] = FileInfo(flags
, type
);
213 //=============================================================================
214 // Function: FilteredAdd
215 // Purpose: Add a file to the list if it meets the filter requirement.
216 // Notes: - See GetBasicFlags for remarks about the Mounted flag.
217 //=============================================================================
218 static bool FilteredAdd(wxArrayString
& list
, const wxChar
* filename
,
219 unsigned flagsSet
, unsigned flagsUnset
)
222 unsigned flags
= GetBasicFlags(filename
);
224 if (flagsSet
& wxFS_VOL_MOUNTED
&& !(flags
& wxFS_VOL_MOUNTED
))
226 else if (flagsUnset
& wxFS_VOL_MOUNTED
&& (flags
& wxFS_VOL_MOUNTED
))
228 else if (flagsSet
& wxFS_VOL_REMOVABLE
&& !(flags
& wxFS_VOL_REMOVABLE
))
230 else if (flagsUnset
& wxFS_VOL_REMOVABLE
&& (flags
& wxFS_VOL_REMOVABLE
))
232 else if (flagsSet
& wxFS_VOL_READONLY
&& !(flags
& wxFS_VOL_READONLY
))
234 else if (flagsUnset
& wxFS_VOL_READONLY
&& (flags
& wxFS_VOL_READONLY
))
236 else if (flagsSet
& wxFS_VOL_REMOTE
&& !(flags
& wxFS_VOL_REMOTE
))
238 else if (flagsUnset
& wxFS_VOL_REMOTE
&& (flags
& wxFS_VOL_REMOTE
))
241 // Add to the list if passed the filter.
248 //=============================================================================
249 // Function: BuildListFromNN
250 // Purpose: Append or remove items from the list
251 // Notes: - There is no way to find all disconnected NN items, or even to find
252 // all items while determining which are connected and not. So this
253 // function will find either all items or connected items.
254 //=============================================================================
255 static void BuildListFromNN(wxArrayString
& list
, NETRESOURCE
* pResSrc
,
256 unsigned flagsSet
, unsigned flagsUnset
)
261 //-----------------------------------------------
262 // Scope may be all drives or all mounted drives.
263 //-----------------------------------------------
264 unsigned scope
= RESOURCE_GLOBALNET
;
265 if (flagsSet
& wxFS_VOL_MOUNTED
)
266 scope
= RESOURCE_CONNECTED
;
268 //----------------------------------------------------------------------
269 // Enumerate all items, adding only non-containers (ie. network shares).
270 // Containers cause a recursive call to this function for their own
272 //----------------------------------------------------------------------
273 if (rc
= s_pWNetOpenEnum(scope
, RESOURCETYPE_DISK
, 0, pResSrc
, &hEnum
), rc
== NO_ERROR
)
277 NETRESOURCE
* pRes
= (NETRESOURCE
*)malloc(size
);
278 memset(pRes
, 0, sizeof(NETRESOURCE
));
279 while (rc
= s_pWNetEnumResource(hEnum
, &count
, pRes
, &size
), rc
== NO_ERROR
|| rc
== ERROR_MORE_DATA
)
284 if (rc
== ERROR_MORE_DATA
)
286 pRes
= (NETRESOURCE
*)realloc(pRes
, size
);
291 // Enumerate the container.
292 if (pRes
->dwUsage
& RESOURCEUSAGE_CONTAINER
)
294 BuildListFromNN(list
, pRes
, flagsSet
, flagsUnset
);
297 // Add the network share.
300 wxString
filename(pRes
->lpRemoteName
);
304 if (filename
.Last() != '\\')
305 filename
.Append('\\');
307 // The filter function will not know mounted from unmounted, and neither do we unless
308 // we are iterating using RESOURCE_CONNECTED, in which case they all are mounted.
309 // Volumes on disconnected servers, however, will correctly show as unmounted.
310 FilteredAdd(list
, filename
, flagsSet
, flagsUnset
&~wxFS_VOL_MOUNTED
);
311 if (scope
== RESOURCE_GLOBALNET
)
312 s_fileInfo
[filename
].m_flags
&= ~wxFS_VOL_MOUNTED
;
320 s_pWNetCloseEnum(hEnum
);
324 //=============================================================================
325 // Function: CompareFcn
326 // Purpose: Used to sort the NN list alphabetically, case insensitive.
327 //=============================================================================
328 static int CompareFcn(const wxString
& first
, const wxString
& second
)
330 return wxStricmp(first
.c_str(), second
.c_str());
333 //=============================================================================
334 // Function: BuildRemoteList
335 // Purpose: Append Network Neighborhood items to the list.
336 // Notes: - Mounted gets transalated into Connected. FilteredAdd is told
337 // to ignore the Mounted flag since we need to handle it in a weird
339 // - The resulting list is sorted alphabetically.
340 //=============================================================================
341 static bool BuildRemoteList(wxArrayString
& list
, NETRESOURCE
* pResSrc
,
342 unsigned flagsSet
, unsigned flagsUnset
)
344 // NN query depends on dynamically loaded library.
345 if (!s_pWNetOpenEnum
|| !s_pWNetEnumResource
|| !s_pWNetCloseEnum
)
347 wxLogError(_("Failed to load mpr.dll."));
351 // Don't waste time doing the work if the flags conflict.
352 if (flagsSet
& wxFS_VOL_MOUNTED
&& flagsUnset
& wxFS_VOL_MOUNTED
)
355 //----------------------------------------------
356 // Generate the list according to the flags set.
357 //----------------------------------------------
358 BuildListFromNN(list
, pResSrc
, flagsSet
, flagsUnset
);
359 list
.Sort(CompareFcn
);
361 //-------------------------------------------------------------------------
362 // If mounted only is requested, then we only need one simple pass.
363 // Otherwise, we need to build a list of all NN volumes and then apply the
364 // list of mounted drives to it.
365 //-------------------------------------------------------------------------
366 if (!(flagsSet
& wxFS_VOL_MOUNTED
))
369 wxArrayString mounted
;
370 BuildListFromNN(mounted
, pResSrc
, flagsSet
| wxFS_VOL_MOUNTED
, flagsUnset
& ~wxFS_VOL_MOUNTED
);
371 mounted
.Sort(CompareFcn
);
373 // apply list from bottom to top to preserve indexes if removing items.
374 int iList
= list
.GetCount()-1;
376 for (iMounted
= mounted
.GetCount()-1; iMounted
>= 0 && iList
>= 0; iMounted
--)
379 wxString
all(list
[iList
]);
380 wxString
mount(mounted
[iMounted
]);
383 wxStricmp(list
[iList
].c_str(), mounted
[iMounted
].c_str()),
384 compare
> 0 && iList
>= 0)
393 // Found the element. Remove it or mark it mounted.
394 if (flagsUnset
& wxFS_VOL_MOUNTED
)
397 s_fileInfo
[list
[iList
]].m_flags
|= wxFS_VOL_MOUNTED
;
408 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
410 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
412 //=============================================================================
413 // Function: GetVolumes
414 // Purpose: Generate and return a list of all volumes (drives) available.
416 //=============================================================================
417 wxArrayString
wxFSVolume::GetVolumes(int flagsSet
, int flagsUnset
)
419 InterlockedExchange(&s_cancelSearch
, FALSE
); // reset
421 if (!s_mprLib
.IsLoaded() && s_mprLib
.Load(_T("mpr.dll")))
424 s_pWNetOpenEnum
= (WNetOpenEnumPtr
)s_mprLib
.GetSymbol(_T("WNetOpenEnumW"));
425 s_pWNetEnumResource
= (WNetEnumResourcePtr
)s_mprLib
.GetSymbol(_T("WNetEnumResourceW"));
427 s_pWNetOpenEnum
= (WNetOpenEnumPtr
)s_mprLib
.GetSymbol(_T("WNetOpenEnumA"));
428 s_pWNetEnumResource
= (WNetEnumResourcePtr
)s_mprLib
.GetSymbol(_T("WNetEnumResourceA"));
430 s_pWNetCloseEnum
= (WNetCloseEnumPtr
)s_mprLib
.GetSymbol(_T("WNetCloseEnum"));
435 //-------------------------------
436 // Local and mapped drives first.
437 //-------------------------------
438 // Allocate the required space for the API call.
439 size_t chars
= GetLogicalDriveStrings(0, 0);
440 TCHAR
* buf
= new TCHAR
[chars
+1];
442 // Get the list of drives.
443 chars
= GetLogicalDriveStrings(chars
, buf
);
445 // Parse the list into an array, applying appropriate filters.
450 FilteredAdd(list
, pVol
, flagsSet
, flagsUnset
);
451 pVol
= pVol
+ wxStrlen(pVol
) + 1;
457 //---------------------------
458 // Network Neighborhood next.
459 //---------------------------
461 // not exclude remote and not removable
462 if (!(flagsUnset
& wxFS_VOL_REMOTE
) &&
463 !(flagsSet
& wxFS_VOL_REMOVABLE
)
466 // The returned list will be sorted alphabetically. We don't pass
467 // our in since we don't want to change to order of the local drives.
469 if (BuildRemoteList(nn
, 0, flagsSet
, flagsUnset
))
471 for (size_t idx
= 0; idx
< nn
.GetCount(); idx
++)
479 //=============================================================================
480 // Function: CancelSearch
481 // Purpose: Instruct an active search to stop.
482 // Notes: - This will only sensibly be called by a thread other than the one
483 // performing the search. This is the only thread-safe function
484 // provided by the class.
485 //=============================================================================
486 void wxFSVolume::CancelSearch()
488 InterlockedExchange(&s_cancelSearch
, TRUE
);
491 //=============================================================================
492 // Function: constructor
493 // Purpose: default constructor
494 //=============================================================================
495 wxFSVolume::wxFSVolume()
500 //=============================================================================
501 // Function: constructor
502 // Purpose: constructor that calls Create
503 //=============================================================================
504 wxFSVolume::wxFSVolume(const wxString
& name
)
509 //=============================================================================
511 // Purpose: Finds, logs in, etc. to the request volume.
512 //=============================================================================
513 bool wxFSVolume::Create(const wxString
& name
)
523 long rc
= SHGetFileInfo(m_volName
, 0, &fi
, sizeof(fi
), SHGFI_DISPLAYNAME
);
526 wxLogError(_("Cannot read typename from '%s'!"), m_volName
.c_str());
529 m_dispName
= fi
.szDisplayName
;
533 m_icons
.Alloc(wxFS_VOL_ICO_MAX
);
536 for (idx
= 0; idx
< wxFS_VOL_ICO_MAX
; idx
++)
542 return m_isOk
= TRUE
;
545 //=============================================================================
547 // Purpose: returns TRUE if the volume is legal.
548 // Notes: For fixed disks, it must exist. For removable disks, it must also
549 // be present. For Network Shares, it must also be logged in, etc.
550 //=============================================================================
551 bool wxFSVolume::IsOk() const
556 //=============================================================================
558 // Purpose: Return the type of the volume.
559 //=============================================================================
560 wxFSVolumeKind
wxFSVolume::GetKind() const
563 return wxFS_VOL_OTHER
;
565 FileInfoMap::iterator itr
= s_fileInfo
.find(m_volName
);
566 if (itr
== s_fileInfo
.end())
567 return wxFS_VOL_OTHER
;
569 return itr
->second
.m_type
;
572 //=============================================================================
573 // Function: GetFlags
574 // Purpose: Return the caches flags for this volume.
575 // Notes: - Returns -1 if no flags were cached.
576 //=============================================================================
577 int wxFSVolume::GetFlags() const
582 FileInfoMap::iterator itr
= s_fileInfo
.find(m_volName
);
583 if (itr
== s_fileInfo
.end())
586 return itr
->second
.m_flags
;
591 //=============================================================================
593 // Purpose: return the requested icon.
594 //=============================================================================
595 wxIcon
wxFSVolume::GetIcon(wxFSIconType type
) const
597 wxCHECK_MSG(type
< (int)m_icons
.GetCount(), wxNullIcon
,
598 _T("Invalid request for icon type!"));
599 wxCHECK_MSG( type
>= 0 && (size_t)type
< m_icons
.GetCount(),
601 _T("invalid icon index") );
604 if (m_icons
[type
].IsNull())
609 case wxFS_VOL_ICO_SMALL
:
610 flags
= SHGFI_ICON
| SHGFI_SMALLICON
;
613 case wxFS_VOL_ICO_LARGE
:
614 flags
= SHGFI_ICON
| SHGFI_SHELLICONSIZE
;
617 case wxFS_VOL_ICO_SEL_SMALL
:
618 flags
= SHGFI_ICON
| SHGFI_SMALLICON
| SHGFI_OPENICON
;
621 case wxFS_VOL_ICO_SEL_LARGE
:
622 flags
= SHGFI_ICON
| SHGFI_SHELLICONSIZE
| SHGFI_OPENICON
;
625 case wxFS_VOL_ICO_MAX
:
626 wxFAIL_MSG(_T("wxFS_VOL_ICO_MAX is not valid icon type"));
631 long rc
= SHGetFileInfo(m_volName
, 0, &fi
, sizeof(fi
), flags
);
632 m_icons
[type
].SetHICON((WXHICON
)fi
.hIcon
);
633 if (!rc
|| !fi
.hIcon
)
634 wxLogError(_("Cannot load icon from '%s'."), m_volName
.c_str());
637 return m_icons
[type
];
642 #endif // wxUSE_FSVOLUME