volume.cpp
来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 613 行 · 第 1/2 页
CPP
613 行
///////////////////////////////////////////////////////////////////////////////
// Name: src/msw/volume.cpp
// Purpose: wxFSVolume - encapsulates system volume information
// Author: George Policello
// Modified by:
// Created: 28 Jan 02
// RCS-ID: $Id: volume.cpp,v 1.28.2.1 2006/01/21 16:46:47 JS Exp $
// Copyright: (c) 2002 George Policello
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "fsvolume.h"
#endif
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#if wxUSE_FSVOLUME
#ifndef WX_PRECOMP
#if wxUSE_GUI
#include "wx/icon.h"
#endif
#include "wx/intl.h"
#endif // WX_PRECOMP
#include "wx/dir.h"
#include "wx/hashmap.h"
#include "wx/dynlib.h"
#include "wx/arrimpl.cpp"
#include "wx/volume.h"
#include <shellapi.h>
#include <shlobj.h>
#include "wx/msw/missing.h"
#if wxUSE_BASE
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Dynamic library function defs.
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#if wxUSE_DYNLIB_CLASS
static wxDynamicLibrary s_mprLib;
#endif
typedef DWORD (WINAPI* WNetOpenEnumPtr)(DWORD, DWORD, DWORD, LPNETRESOURCE, LPHANDLE);
typedef DWORD (WINAPI* WNetEnumResourcePtr)(HANDLE, LPDWORD, LPVOID, LPDWORD);
typedef DWORD (WINAPI* WNetCloseEnumPtr)(HANDLE);
static WNetOpenEnumPtr s_pWNetOpenEnum;
static WNetEnumResourcePtr s_pWNetEnumResource;
static WNetCloseEnumPtr s_pWNetCloseEnum;
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Globals/Statics
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
static long s_cancelSearch = FALSE;
struct FileInfo
{
FileInfo(unsigned flag=0, wxFSVolumeKind type=wxFS_VOL_OTHER) :
m_flags(flag), m_type(type) {}
FileInfo(const FileInfo& other) { *this = other; }
FileInfo& operator=(const FileInfo& other)
{
m_flags = other.m_flags;
m_type = other.m_type;
return *this;
}
unsigned m_flags;
wxFSVolumeKind m_type;
};
WX_DECLARE_STRING_HASH_MAP(FileInfo, FileInfoMap);
// Cygwin bug (?) destructor for global s_fileInfo is called twice...
static FileInfoMap& GetFileInfoMap()
{
static FileInfoMap s_fileInfo(25);
return s_fileInfo;
}
#define s_fileInfo (GetFileInfoMap())
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Local helper functions.
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//=============================================================================
// Function: GetBasicFlags
// Purpose: Set basic flags, primarily wxFS_VOL_REMOTE and wxFS_VOL_REMOVABLE.
// Notes: - Local and mapped drives are mounted by definition. We have no
// way to determine mounted status of network drives, so assume that
// all drives are mounted, and let the caller decide otherwise.
// - Other flags are 'best guess' from type of drive. The system will
// not report the file attributes with any degree of accuracy.
//=============================================================================
static unsigned GetBasicFlags(const wxChar* filename)
{
unsigned flags = wxFS_VOL_MOUNTED;
//----------------------------------
// 'Best Guess' based on drive type.
//----------------------------------
wxFSVolumeKind type;
switch(GetDriveType(filename))
{
case DRIVE_FIXED:
type = wxFS_VOL_DISK;
break;
case DRIVE_REMOVABLE:
flags |= wxFS_VOL_REMOVABLE;
type = wxFS_VOL_FLOPPY;
break;
case DRIVE_CDROM:
flags |= wxFS_VOL_REMOVABLE | wxFS_VOL_READONLY;
type = wxFS_VOL_CDROM;
break;
case DRIVE_REMOTE:
flags |= wxFS_VOL_REMOTE;
type = wxFS_VOL_NETWORK;
break;
case DRIVE_NO_ROOT_DIR:
flags &= ~wxFS_VOL_MOUNTED;
type = wxFS_VOL_OTHER;
break;
default:
type = wxFS_VOL_OTHER;
break;
}
//-----------------------------------------------------------------------
// The following will most likely will not modify anything not set above,
// and will not work at all for network shares or empty CD ROM drives.
// But it is a good check if the Win API ever gets better about reporting
// this information.
//-----------------------------------------------------------------------
SHFILEINFO fi;
long rc;
rc = SHGetFileInfo(filename, 0, &fi, sizeof(fi), SHGFI_ATTRIBUTES );
if (!rc)
{
wxLogError(_("Cannot read typename from '%s'!"), filename);
}
else
{
if (fi.dwAttributes & SFGAO_READONLY)
flags |= wxFS_VOL_READONLY;
if (fi.dwAttributes & SFGAO_REMOVABLE)
flags |= wxFS_VOL_REMOVABLE;
}
//------------------
// Flags are cached.
//------------------
s_fileInfo[filename] = FileInfo(flags, type);
return flags;
} // GetBasicFlags
//=============================================================================
// Function: FilteredAdd
// Purpose: Add a file to the list if it meets the filter requirement.
// Notes: - See GetBasicFlags for remarks about the Mounted flag.
//=============================================================================
static bool FilteredAdd(wxArrayString& list, const wxChar* filename,
unsigned flagsSet, unsigned flagsUnset)
{
bool accept = true;
unsigned flags = GetBasicFlags(filename);
if (flagsSet & wxFS_VOL_MOUNTED && !(flags & wxFS_VOL_MOUNTED))
accept = false;
else if (flagsUnset & wxFS_VOL_MOUNTED && (flags & wxFS_VOL_MOUNTED))
accept = false;
else if (flagsSet & wxFS_VOL_REMOVABLE && !(flags & wxFS_VOL_REMOVABLE))
accept = false;
else if (flagsUnset & wxFS_VOL_REMOVABLE && (flags & wxFS_VOL_REMOVABLE))
accept = false;
else if (flagsSet & wxFS_VOL_READONLY && !(flags & wxFS_VOL_READONLY))
accept = false;
else if (flagsUnset & wxFS_VOL_READONLY && (flags & wxFS_VOL_READONLY))
accept = false;
else if (flagsSet & wxFS_VOL_REMOTE && !(flags & wxFS_VOL_REMOTE))
accept = false;
else if (flagsUnset & wxFS_VOL_REMOTE && (flags & wxFS_VOL_REMOTE))
accept = false;
// Add to the list if passed the filter.
if (accept)
list.Add(filename);
return accept;
} // FilteredAdd
//=============================================================================
// Function: BuildListFromNN
// Purpose: Append or remove items from the list
// Notes: - There is no way to find all disconnected NN items, or even to find
// all items while determining which are connected and not. So this
// function will find either all items or connected items.
//=============================================================================
static void BuildListFromNN(wxArrayString& list, NETRESOURCE* pResSrc,
unsigned flagsSet, unsigned flagsUnset)
{
HANDLE hEnum;
int rc;
//-----------------------------------------------
// Scope may be all drives or all mounted drives.
//-----------------------------------------------
unsigned scope = RESOURCE_GLOBALNET;
if (flagsSet & wxFS_VOL_MOUNTED)
scope = RESOURCE_CONNECTED;
//----------------------------------------------------------------------
// Enumerate all items, adding only non-containers (ie. network shares).
// Containers cause a recursive call to this function for their own
// enumeration.
//----------------------------------------------------------------------
if (rc = s_pWNetOpenEnum(scope, RESOURCETYPE_DISK, 0, pResSrc, &hEnum), rc == NO_ERROR)
{
DWORD count = 1;
DWORD size = 256;
NETRESOURCE* pRes = (NETRESOURCE*)malloc(size);
memset(pRes, 0, sizeof(NETRESOURCE));
while (rc = s_pWNetEnumResource(hEnum, &count, pRes, &size), rc == NO_ERROR || rc == ERROR_MORE_DATA)
{
if (s_cancelSearch)
break;
if (rc == ERROR_MORE_DATA)
{
pRes = (NETRESOURCE*)realloc(pRes, size);
count = 1;
}
else if (count == 1)
{
// Enumerate the container.
if (pRes->dwUsage & RESOURCEUSAGE_CONTAINER)
{
BuildListFromNN(list, pRes, flagsSet, flagsUnset);
}
// Add the network share.
else
{
wxString filename(pRes->lpRemoteName);
if (filename.Len())
{
if (filename.Last() != '\\')
filename.Append('\\');
// The filter function will not know mounted from unmounted, and neither do we unless
// we are iterating using RESOURCE_CONNECTED, in which case they all are mounted.
// Volumes on disconnected servers, however, will correctly show as unmounted.
FilteredAdd(list, filename, flagsSet, flagsUnset&~wxFS_VOL_MOUNTED);
if (scope == RESOURCE_GLOBALNET)
s_fileInfo[filename].m_flags &= ~wxFS_VOL_MOUNTED;
}
}
}
else if (count == 0)
break;
}
free(pRes);
s_pWNetCloseEnum(hEnum);
}
} // BuildListFromNN
//=============================================================================
// Function: CompareFcn
// Purpose: Used to sort the NN list alphabetically, case insensitive.
//=============================================================================
static int CompareFcn(const wxString& first, const wxString& second)
{
return wxStricmp(first.c_str(), second.c_str());
} // CompareFcn
//=============================================================================
// Function: BuildRemoteList
// Purpose: Append Network Neighborhood items to the list.
// Notes: - Mounted gets transalated into Connected. FilteredAdd is told
// to ignore the Mounted flag since we need to handle it in a weird
// way manually.
// - The resulting list is sorted alphabetically.
//=============================================================================
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?