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
))
414 for (idx
= 0; idx
< nn
.GetCount(); idx
++)
422 //=============================================================================
423 // Function: CancelSearch
424 // Purpose: Instruct an active search to stop.
425 // Notes: - This will only sensibly be called by a thread other than the one
426 // performing the search. This is the only thread-safe function
427 // provided by the class.
428 //=============================================================================
429 void wxFSVolume::CancelSearch()
431 InterlockedExchange(&s_cancelSearch
, TRUE
);
434 //=============================================================================
435 // Function: constructor
436 // Purpose: default constructor
437 //=============================================================================
438 wxFSVolume::wxFSVolume()
443 //=============================================================================
444 // Function: constructor
445 // Purpose: constructor that calls Create
446 //=============================================================================
447 wxFSVolume::wxFSVolume(const wxString
& name
)
452 //=============================================================================
454 // Purpose: Finds, logs in, etc. to the request volume.
455 //=============================================================================
456 bool wxFSVolume::Create(const wxString
& name
)
466 long rc
= SHGetFileInfo(m_volName
, 0, &fi
, sizeof(fi
), SHGFI_DISPLAYNAME
);
469 wxLogError(_("Cannot read typename from '%s'!"), m_volName
);
472 m_dispName
= fi
.szDisplayName
;
476 m_icons
.Alloc(wxFS_VOL_ICO_MAX
);
479 for (idx
= 0; idx
< wxFS_VOL_ICO_MAX
; idx
++)
485 return m_isOk
= TRUE
;
488 //=============================================================================
490 // Purpose: returns TRUE if the volume is legal.
491 // Notes: For fixed disks, it must exist. For removable disks, it must also
492 // be present. For Network Shares, it must also be logged in, etc.
493 //=============================================================================
494 bool wxFSVolume::IsOk() const
499 //=============================================================================
501 // Purpose: Return the type of the volume.
502 //=============================================================================
503 wxFSVolumeKind
wxFSVolume::GetKind() const
506 return wxFS_VOL_OTHER
;
508 FileInfoMap::iterator itr
= s_fileInfo
.find(m_volName
);
509 if (itr
== s_fileInfo
.end())
510 return wxFS_VOL_OTHER
;
512 return itr
->second
.m_type
;
515 //=============================================================================
516 // Function: GetFlags
517 // Purpose: Return the caches flags for this volume.
518 // Notes: - Returns -1 if no flags were cached.
519 //=============================================================================
520 int wxFSVolume::GetFlags() const
525 FileInfoMap::iterator itr
= s_fileInfo
.find(m_volName
);
526 if (itr
== s_fileInfo
.end())
529 return itr
->second
.m_flags
;
534 //=============================================================================
536 // Purpose: return the requested icon.
537 //=============================================================================
538 wxIcon
wxFSVolume::GetIcon(wxFSIconType type
) const
540 wxASSERT(type
< m_icons
.GetCount());
542 if (type
>= m_icons
.GetCount())
544 wxLogError(_("Invalid request for icon type!"));
550 if (m_icons
[type
].IsNull())
555 case wxFS_VOL_ICO_SMALL
:
556 flags
= SHGFI_ICON
| SHGFI_SMALLICON
;
559 case wxFS_VOL_ICO_LARGE
:
560 flags
= SHGFI_ICON
| SHGFI_SHELLICONSIZE
;
563 case wxFS_VOL_ICO_SEL_SMALL
:
564 flags
= SHGFI_ICON
| SHGFI_SMALLICON
| SHGFI_OPENICON
;
567 case wxFS_VOL_ICO_SEL_LARGE
:
568 flags
= SHGFI_ICON
| SHGFI_SHELLICONSIZE
| SHGFI_OPENICON
;
573 long rc
= SHGetFileInfo(m_volName
, 0, &fi
, sizeof(fi
), flags
);
574 m_icons
[type
].SetHICON((WXHICON
)fi
.hIcon
);
575 if (!rc
|| !fi
.hIcon
)
576 wxLogError(_("Cannot load icon from '%s'."), m_volName
);
579 return m_icons
[type
];