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 FileInfo(const FileInfo
& other
) { *this = other
; }
104 FileInfo
& operator=(const FileInfo
& other
)
106 m_flags
= other
.m_flags
;
107 m_type
= other
.m_type
;
112 wxFSVolumeKind m_type
;
114 WX_DECLARE_STRING_HASH_MAP(FileInfo
, FileInfoMap
);
115 static FileInfoMap
s_fileInfo(25);
117 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
118 // Other initialization.
119 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
121 // already in wx/iconbndl.h
122 // WX_DEFINE_OBJARRAY(wxIconArray);
125 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
126 // Local helper functions.
127 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
129 //=============================================================================
130 // Function: GetBasicFlags
131 // Purpose: Set basic flags, primarily wxFS_VOL_REMOTE and wxFS_VOL_REMOVABLE.
132 // Notes: - Local and mapped drives are mounted by definition. We have no
133 // way to determine mounted status of network drives, so assume that
134 // all drives are mounted, and let the caller decide otherwise.
135 // - Other flags are 'best guess' from type of drive. The system will
136 // not report the file attributes with any degree of accuracy.
137 //=============================================================================
138 static unsigned GetBasicFlags(const wxChar
* filename
)
140 unsigned flags
= wxFS_VOL_MOUNTED
;
142 //----------------------------------
143 // 'Best Guess' based on drive type.
144 //----------------------------------
146 switch(GetDriveType(filename
))
149 type
= wxFS_VOL_DISK
;
152 case DRIVE_REMOVABLE
:
153 flags
|= wxFS_VOL_REMOVABLE
;
154 type
= wxFS_VOL_FLOPPY
;
158 flags
|= wxFS_VOL_REMOVABLE
| wxFS_VOL_READONLY
;
159 type
= wxFS_VOL_CDROM
;
163 flags
|= wxFS_VOL_REMOTE
;
164 type
= wxFS_VOL_NETWORK
;
167 case DRIVE_NO_ROOT_DIR
:
168 flags
&= ~wxFS_VOL_MOUNTED
;
169 type
= wxFS_VOL_OTHER
;
173 type
= wxFS_VOL_OTHER
;
177 //-----------------------------------------------------------------------
178 // The following will most likely will not modify anything not set above,
179 // and will not work at all for network shares or empty CD ROM drives.
180 // But it is a good check if the Win API ever gets better about reporting
182 //-----------------------------------------------------------------------
185 rc
= SHGetFileInfo(filename
, 0, &fi
, sizeof(fi
), SHGFI_ATTRIBUTES
);
188 wxLogError(_("Cannot read typename from '%s'!"), filename
);
192 if (fi
.dwAttributes
& SFGAO_READONLY
)
193 flags
|= wxFS_VOL_READONLY
;
194 if (fi
.dwAttributes
& SFGAO_REMOVABLE
)
195 flags
|= wxFS_VOL_REMOVABLE
;
201 s_fileInfo
[filename
] = FileInfo(flags
, type
);
206 //=============================================================================
207 // Function: FilteredAdd
208 // Purpose: Add a file to the list if it meets the filter requirement.
209 // Notes: - See GetBasicFlags for remarks about the Mounted flag.
210 //=============================================================================
211 static bool FilteredAdd(wxArrayString
& list
, const wxChar
* filename
,
212 unsigned flagsSet
, unsigned flagsUnset
)
215 unsigned flags
= GetBasicFlags(filename
);
217 if (flagsSet
& wxFS_VOL_MOUNTED
&& !(flags
& wxFS_VOL_MOUNTED
))
219 else if (flagsUnset
& wxFS_VOL_MOUNTED
&& (flags
& wxFS_VOL_MOUNTED
))
221 else if (flagsSet
& wxFS_VOL_REMOVABLE
&& !(flags
& wxFS_VOL_REMOVABLE
))
223 else if (flagsUnset
& wxFS_VOL_REMOVABLE
&& (flags
& wxFS_VOL_REMOVABLE
))
225 else if (flagsSet
& wxFS_VOL_READONLY
&& !(flags
& wxFS_VOL_READONLY
))
227 else if (flagsUnset
& wxFS_VOL_READONLY
&& (flags
& wxFS_VOL_READONLY
))
229 else if (flagsSet
& wxFS_VOL_REMOTE
&& !(flags
& wxFS_VOL_REMOTE
))
231 else if (flagsUnset
& wxFS_VOL_REMOTE
&& (flags
& wxFS_VOL_REMOTE
))
234 // Add to the list if passed the filter.
241 //=============================================================================
242 // Function: BuildListFromNN
243 // Purpose: Append or remove items from the list
244 // Notes: - There is no way to find all disconnected NN items, or even to find
245 // all items while determining which are connected and not. So this
246 // function will find either all items or connected items.
247 //=============================================================================
248 static void BuildListFromNN(wxArrayString
& list
, NETRESOURCE
* pResSrc
,
249 unsigned flagsSet
, unsigned flagsUnset
)
254 //-----------------------------------------------
255 // Scope may be all drives or all mounted drives.
256 //-----------------------------------------------
257 unsigned scope
= RESOURCE_GLOBALNET
;
258 if (flagsSet
& wxFS_VOL_MOUNTED
)
259 scope
= RESOURCE_CONNECTED
;
261 //----------------------------------------------------------------------
262 // Enumerate all items, adding only non-containers (ie. network shares).
263 // Containers cause a recursive call to this function for their own
265 //----------------------------------------------------------------------
266 if (rc
= s_pWNetOpenEnum(scope
, RESOURCETYPE_DISK
, 0, pResSrc
, &hEnum
), rc
== NO_ERROR
)
270 NETRESOURCE
* pRes
= (NETRESOURCE
*)malloc(size
);
271 memset(pRes
, 0, sizeof(NETRESOURCE
));
272 while (rc
= s_pWNetEnumResource(hEnum
, &count
, pRes
, &size
), rc
== NO_ERROR
|| rc
== ERROR_MORE_DATA
)
277 if (rc
== ERROR_MORE_DATA
)
279 pRes
= (NETRESOURCE
*)realloc(pRes
, size
);
284 // Enumerate the container.
285 if (pRes
->dwUsage
& RESOURCEUSAGE_CONTAINER
)
287 BuildListFromNN(list
, pRes
, flagsSet
, flagsUnset
);
290 // Add the network share.
293 wxString
filename(pRes
->lpRemoteName
);
297 if (filename
.Last() != '\\')
298 filename
.Append('\\');
300 // The filter function will not know mounted from unmounted, and neither do we unless
301 // we are iterating using RESOURCE_CONNECTED, in which case they all are mounted.
302 // Volumes on disconnected servers, however, will correctly show as unmounted.
303 FilteredAdd(list
, filename
, flagsSet
, flagsUnset
&~wxFS_VOL_MOUNTED
);
304 if (scope
== RESOURCE_GLOBALNET
)
305 s_fileInfo
[filename
].m_flags
&= ~wxFS_VOL_MOUNTED
;
313 s_pWNetCloseEnum(hEnum
);
317 //=============================================================================
318 // Function: CompareFcn
319 // Purpose: Used to sort the NN list alphabetically, case insensitive.
320 //=============================================================================
321 static int CompareFcn(const wxString
& first
, const wxString
& second
)
323 return wxStricmp(first
.c_str(), second
.c_str());
326 //=============================================================================
327 // Function: BuildRemoteList
328 // Purpose: Append Network Neighborhood items to the list.
329 // Notes: - Mounted gets transalated into Connected. FilteredAdd is told
330 // to ignore the Mounted flag since we need to handle it in a weird
332 // - The resulting list is sorted alphabetically.
333 //=============================================================================
334 static bool BuildRemoteList(wxArrayString
& list
, NETRESOURCE
* pResSrc
,
335 unsigned flagsSet
, unsigned flagsUnset
)
337 // NN query depends on dynamically loaded library.
338 if (!s_pWNetOpenEnum
|| !s_pWNetEnumResource
|| !s_pWNetCloseEnum
)
340 wxLogError(_("Failed to load mpr.dll."));
344 // Don't waste time doing the work if the flags conflict.
345 if (flagsSet
& wxFS_VOL_MOUNTED
&& flagsUnset
& wxFS_VOL_MOUNTED
)
348 //----------------------------------------------
349 // Generate the list according to the flags set.
350 //----------------------------------------------
351 BuildListFromNN(list
, pResSrc
, flagsSet
, flagsUnset
);
352 list
.Sort(CompareFcn
);
354 //-------------------------------------------------------------------------
355 // If mounted only is requested, then we only need one simple pass.
356 // Otherwise, we need to build a list of all NN volumes and then apply the
357 // list of mounted drives to it.
358 //-------------------------------------------------------------------------
359 if (!(flagsSet
& wxFS_VOL_MOUNTED
))
362 wxArrayString mounted
;
363 BuildListFromNN(mounted
, pResSrc
, flagsSet
| wxFS_VOL_MOUNTED
, flagsUnset
& ~wxFS_VOL_MOUNTED
);
364 mounted
.Sort(CompareFcn
);
366 // apply list from bottom to top to preserve indexes if removing items.
367 int iList
= list
.GetCount()-1;
369 for (iMounted
= mounted
.GetCount()-1; iMounted
>= 0 && iList
>= 0; iMounted
--)
372 wxString
all(list
[iList
]);
373 wxString
mount(mounted
[iMounted
]);
376 wxStricmp(list
[iList
].c_str(), mounted
[iMounted
].c_str()),
377 compare
> 0 && iList
>= 0)
386 // Found the element. Remove it or mark it mounted.
387 if (flagsUnset
& wxFS_VOL_MOUNTED
)
390 s_fileInfo
[list
[iList
]].m_flags
|= wxFS_VOL_MOUNTED
;
401 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
403 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
405 //=============================================================================
406 // Function: GetVolumes
407 // Purpose: Generate and return a list of all volumes (drives) available.
409 //=============================================================================
410 wxArrayString
wxFSVolume::GetVolumes(int flagsSet
, int flagsUnset
)
412 InterlockedExchange(&s_cancelSearch
, FALSE
); // reset
414 if (!s_mprLib
.IsLoaded() && s_mprLib
.Load(_T("mpr.dll")))
417 s_pWNetOpenEnum
= (WNetOpenEnumPtr
)s_mprLib
.GetSymbol(_T("WNetOpenEnumW"));
418 s_pWNetEnumResource
= (WNetEnumResourcePtr
)s_mprLib
.GetSymbol("WNetEnumResourceW");
420 s_pWNetOpenEnum
= (WNetOpenEnumPtr
)s_mprLib
.GetSymbol(_T("WNetOpenEnumA"));
421 s_pWNetEnumResource
= (WNetEnumResourcePtr
)s_mprLib
.GetSymbol(_T("WNetEnumResourceA"));
423 s_pWNetCloseEnum
= (WNetCloseEnumPtr
)s_mprLib
.GetSymbol(_T("WNetCloseEnum"));
428 //-------------------------------
429 // Local and mapped drives first.
430 //-------------------------------
431 // Allocate the required space for the API call.
432 size_t chars
= GetLogicalDriveStrings(0, 0);
433 TCHAR
* buf
= new TCHAR
[chars
+1];
435 // Get the list of drives.
436 chars
= GetLogicalDriveStrings(chars
, buf
);
438 // Parse the list into an array, applying appropriate filters.
443 FilteredAdd(list
, pVol
, flagsSet
, flagsUnset
);
444 pVol
= pVol
+ wxStrlen(pVol
) + 1;
450 //---------------------------
451 // Network Neighborhood next.
452 //---------------------------
454 // not exclude remote and not removable
455 if (!(flagsUnset
& wxFS_VOL_REMOTE
) &&
456 !(flagsSet
& wxFS_VOL_REMOVABLE
)
459 // The returned list will be sorted alphabetically. We don't pass
460 // our in since we don't want to change to order of the local drives.
462 if (BuildRemoteList(nn
, 0, flagsSet
, flagsUnset
))
464 for (size_t idx
= 0; idx
< nn
.GetCount(); idx
++)
472 //=============================================================================
473 // Function: CancelSearch
474 // Purpose: Instruct an active search to stop.
475 // Notes: - This will only sensibly be called by a thread other than the one
476 // performing the search. This is the only thread-safe function
477 // provided by the class.
478 //=============================================================================
479 void wxFSVolume::CancelSearch()
481 InterlockedExchange(&s_cancelSearch
, TRUE
);
484 //=============================================================================
485 // Function: constructor
486 // Purpose: default constructor
487 //=============================================================================
488 wxFSVolume::wxFSVolume()
493 //=============================================================================
494 // Function: constructor
495 // Purpose: constructor that calls Create
496 //=============================================================================
497 wxFSVolume::wxFSVolume(const wxString
& name
)
502 //=============================================================================
504 // Purpose: Finds, logs in, etc. to the request volume.
505 //=============================================================================
506 bool wxFSVolume::Create(const wxString
& name
)
516 long rc
= SHGetFileInfo(m_volName
, 0, &fi
, sizeof(fi
), SHGFI_DISPLAYNAME
);
519 wxLogError(_("Cannot read typename from '%s'!"), m_volName
.c_str());
522 m_dispName
= fi
.szDisplayName
;
526 m_icons
.Alloc(wxFS_VOL_ICO_MAX
);
529 for (idx
= 0; idx
< wxFS_VOL_ICO_MAX
; idx
++)
535 return m_isOk
= TRUE
;
538 //=============================================================================
540 // Purpose: returns TRUE if the volume is legal.
541 // Notes: For fixed disks, it must exist. For removable disks, it must also
542 // be present. For Network Shares, it must also be logged in, etc.
543 //=============================================================================
544 bool wxFSVolume::IsOk() const
549 //=============================================================================
551 // Purpose: Return the type of the volume.
552 //=============================================================================
553 wxFSVolumeKind
wxFSVolume::GetKind() const
556 return wxFS_VOL_OTHER
;
558 FileInfoMap::iterator itr
= s_fileInfo
.find(m_volName
);
559 if (itr
== s_fileInfo
.end())
560 return wxFS_VOL_OTHER
;
562 return itr
->second
.m_type
;
565 //=============================================================================
566 // Function: GetFlags
567 // Purpose: Return the caches flags for this volume.
568 // Notes: - Returns -1 if no flags were cached.
569 //=============================================================================
570 int wxFSVolume::GetFlags() const
575 FileInfoMap::iterator itr
= s_fileInfo
.find(m_volName
);
576 if (itr
== s_fileInfo
.end())
579 return itr
->second
.m_flags
;
584 //=============================================================================
586 // Purpose: return the requested icon.
587 //=============================================================================
588 wxIcon
wxFSVolume::GetIcon(wxFSIconType type
) const
590 wxCHECK_MSG(type
< (int)m_icons
.GetCount(), wxNullIcon
,
591 _T("Invalid request for icon type!"));
592 wxCHECK_MSG( type
>= 0 && (size_t)type
< m_icons
.GetCount(),
594 _T("invalid icon index") );
597 if (m_icons
[type
].IsNull())
602 case wxFS_VOL_ICO_SMALL
:
603 flags
= SHGFI_ICON
| SHGFI_SMALLICON
;
606 case wxFS_VOL_ICO_LARGE
:
607 flags
= SHGFI_ICON
| SHGFI_SHELLICONSIZE
;
610 case wxFS_VOL_ICO_SEL_SMALL
:
611 flags
= SHGFI_ICON
| SHGFI_SMALLICON
| SHGFI_OPENICON
;
614 case wxFS_VOL_ICO_SEL_LARGE
:
615 flags
= SHGFI_ICON
| SHGFI_SHELLICONSIZE
| SHGFI_OPENICON
;
618 case wxFS_VOL_ICO_MAX
:
619 wxFAIL_MSG(_T("wxFS_VOL_ICO_MAX is not valid icon type"));
624 long rc
= SHGetFileInfo(m_volName
, 0, &fi
, sizeof(fi
), flags
);
625 m_icons
[type
].SetHICON((WXHICON
)fi
.hIcon
);
626 if (!rc
|| !fi
.hIcon
)
627 wxLogError(_("Cannot load icon from '%s'."), m_volName
.c_str());
630 return m_icons
[type
];
635 #endif // wxUSE_FSVOLUME