refactoring
This commit is contained in:
parent
4822b577d2
commit
fdf159af42
@ -360,7 +360,7 @@ std::string AppConfig::get_last_output_dir(const std::string &alt) const
|
||||
{
|
||||
if (GUI::RemovableDriveManager::getInstance().update())
|
||||
{
|
||||
return GUI::RemovableDriveManager::getInstance().getLastDrivePath();
|
||||
return GUI::RemovableDriveManager::getInstance().get_last_drive_path();
|
||||
}
|
||||
const auto it = m_storage.find("");
|
||||
if (it != m_storage.end()) {
|
||||
|
@ -1,14 +1,12 @@
|
||||
#include "RemovableDriveManager.hpp"
|
||||
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "boost/nowide/convert.hpp"
|
||||
|
||||
#if _WIN32
|
||||
#include <windows.h>
|
||||
#include <tchar.h>
|
||||
#include <winioctl.h>
|
||||
DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE,
|
||||
0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);
|
||||
#else
|
||||
@ -23,16 +21,13 @@ DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE,
|
||||
namespace Slic3r {
|
||||
namespace GUI {
|
||||
|
||||
std::vector<DriveData> RemovableDriveManager::currentDrives;
|
||||
|
||||
|
||||
|
||||
std::vector<DriveData> RemovableDriveManager::m_current_drives;
|
||||
|
||||
#if _WIN32
|
||||
void RemovableDriveManager::searchForDrives(std::vector<DriveData>& newDrives)
|
||||
void RemovableDriveManager::search_for_drives()
|
||||
{
|
||||
newDrives.clear();
|
||||
newDrives.reserve(26);
|
||||
m_current_drives.clear();
|
||||
m_current_drives.reserve(26);
|
||||
DWORD drivesMask = GetLogicalDrives();
|
||||
for (size_t i = 0; i < 26; i++)
|
||||
{
|
||||
@ -65,7 +60,7 @@ void RemovableDriveManager::searchForDrives(std::vector<DriveData>& newDrives)
|
||||
//std::cout << std::string(volumeName.begin(), volumeName.end()) << " " << std::string(fileSystemName.begin(), fileSystemName.end()) << " " << freeSpace.QuadPart << "\n";
|
||||
if (freeSpace.QuadPart > 0)
|
||||
{
|
||||
newDrives.push_back(DriveData(volumeName, path));
|
||||
m_current_drives.push_back(DriveData(boost::nowide::narrow(volumeName), path));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -77,49 +72,51 @@ void RemovableDriveManager::searchForDrives(std::vector<DriveData>& newDrives)
|
||||
}
|
||||
|
||||
}
|
||||
void RemovableDriveManager::ejectDrive(std::string path)
|
||||
void RemovableDriveManager::eject_drive(const std::string &path)
|
||||
{
|
||||
if (!update() || !isDriveMounted(path))
|
||||
return;
|
||||
|
||||
path = "\\\\.\\"+path;
|
||||
HANDLE handle = CreateFileA(path.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
|
||||
if(handle == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
std::cerr << "Ejecting " << path << " failed " << GetLastError() << " \n";
|
||||
//if (!update() || !is_drive_mounted(path))
|
||||
if(m_current_drives.empty())
|
||||
return;
|
||||
}
|
||||
DWORD deviceControlRetVal(0);
|
||||
BOOL error = DeviceIoControl(handle, IOCTL_STORAGE_EJECT_MEDIA, nullptr, 0,nullptr , 0, &deviceControlRetVal, nullptr);
|
||||
CloseHandle(handle);
|
||||
if(error != 0)
|
||||
std::cout << "Ejected " << path << "\n";
|
||||
else
|
||||
std::cerr << "Ejecting " << path << " failed "<< deviceControlRetVal << " " << GetLastError() <<" \n";
|
||||
|
||||
for (auto it = currentDrives.begin(); it != currentDrives.end(); ++it)
|
||||
for (auto it = m_current_drives.begin(); it != m_current_drives.end(); ++it)
|
||||
{
|
||||
if ((*it).path == path)
|
||||
{
|
||||
currentDrives.erase(it);
|
||||
std::string mpath = "\\\\.\\" + path;
|
||||
HANDLE handle = CreateFileA(mpath.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
|
||||
if (handle == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
std::cerr << "Ejecting " << mpath << " failed " << GetLastError() << " \n";
|
||||
return;
|
||||
}
|
||||
DWORD deviceControlRetVal(0);
|
||||
BOOL error = DeviceIoControl(handle, IOCTL_STORAGE_EJECT_MEDIA, nullptr, 0, nullptr, 0, &deviceControlRetVal, nullptr);
|
||||
CloseHandle(handle);
|
||||
if (error != 0)
|
||||
std::cout << "Ejected " << mpath << "\n";
|
||||
else
|
||||
std::cerr << "Ejecting " << mpath << " failed " << deviceControlRetVal << " " << GetLastError() << " \n";
|
||||
|
||||
|
||||
m_current_drives.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
void RemovableDriveManager::searchForDrives(std::vector<DriveData>& newDrives)
|
||||
void RemovableDriveManager::search_for_drives()
|
||||
{
|
||||
struct stat buf;
|
||||
std::string path(std::getenv("USER"));
|
||||
std::string pp(path);
|
||||
|
||||
newDrives.clear();
|
||||
newDrives.reserve(26);
|
||||
m_current_drives.clear();
|
||||
m_current_Drives.reserve(26);
|
||||
|
||||
//search /media/* folder
|
||||
stat("/media/",&buf);
|
||||
std::cout << "/media ID: " <<buf.st_dev << "\n";
|
||||
searchPath(newDrives, "/media/*", buf.st_dev);
|
||||
search_path("/media/*", buf.st_dev);
|
||||
|
||||
//search /media/USERNAME/* folder
|
||||
pp = "/media/"+pp;
|
||||
@ -127,18 +124,18 @@ void RemovableDriveManager::searchForDrives(std::vector<DriveData>& newDrives)
|
||||
|
||||
stat(pp.c_str() ,&buf);
|
||||
std::cout << pp <<" ID: " <<buf.st_dev << "\n";
|
||||
searchPath(newDrives, path, buf.st_dev);
|
||||
searchPath(path, buf.st_dev);
|
||||
|
||||
//search /run/media/USERNAME/* folder
|
||||
path = "/run" + path;
|
||||
pp = "/run"+pp;
|
||||
stat(pp.c_str() ,&buf);
|
||||
std::cout << pp <<" ID: " <<buf.st_dev << "\n";
|
||||
searchPath(newDrives, path, buf.st_dev);
|
||||
searchPath(path, buf.st_dev);
|
||||
|
||||
std::cout << "found drives:" <<newDrives.size() << "\n";
|
||||
}
|
||||
void RemovableDriveManager::searchPath(std::vector<DriveData>& newDrives,const std::string path, const dev_t parentDevID)
|
||||
void RemovableDriveManager::search_path(const std::string &path,const dev_t &parentDevID)
|
||||
{
|
||||
glob_t globbuf;
|
||||
globbuf.gl_offs = 2;
|
||||
@ -159,17 +156,17 @@ void RemovableDriveManager::searchPath(std::vector<DriveData>& newDrives,const s
|
||||
std::cout << buf.st_dev << "\n";
|
||||
if(buf.st_dev != parentDevID)// not same file system
|
||||
{
|
||||
newDrives.push_back(DriveData(name,globbuf.gl_pathv[i]));
|
||||
m_current_drives.push_back(DriveData(name,globbuf.gl_pathv[i]));
|
||||
}
|
||||
}
|
||||
globfree(&globbuf);
|
||||
}
|
||||
void RemovableDriveManager::ejectDrive(std::string path)
|
||||
void RemovableDriveManager::eject_drive(const std::string &path)
|
||||
{
|
||||
if (currentDrives.empty())
|
||||
if (m_current_drives.empty())
|
||||
return;
|
||||
|
||||
for (auto it = currentDrives.begin(); it != currentDrives.end(); ++it)
|
||||
for (auto it = m_current_drives.begin(); it != m_current_drives.end(); ++it)
|
||||
{
|
||||
if((*it).path == path)
|
||||
{
|
||||
@ -180,7 +177,7 @@ void RemovableDriveManager::ejectDrive(std::string path)
|
||||
int errsv = errno;
|
||||
std::cerr<<"Ejecting failed Error "<< errsv<<"\n";
|
||||
}
|
||||
currentDrives.erase(it);
|
||||
m_current_drives.erase(it);
|
||||
break;
|
||||
}
|
||||
|
||||
@ -190,22 +187,14 @@ void RemovableDriveManager::ejectDrive(std::string path)
|
||||
#endif
|
||||
bool RemovableDriveManager::update()
|
||||
{
|
||||
searchForDrives(currentDrives);
|
||||
return !currentDrives.empty();
|
||||
search_for_drives();
|
||||
return !m_current_drives.empty();
|
||||
}
|
||||
|
||||
void RemovableDriveManager::updateCurrentDrives(const std::vector<DriveData>& newDrives)
|
||||
|
||||
bool RemovableDriveManager::is_drive_mounted(const std::string &path)
|
||||
{
|
||||
currentDrives.clear();
|
||||
currentDrives.reserve(26);
|
||||
for (auto it = newDrives.begin(); it != newDrives.end(); ++it)
|
||||
{
|
||||
currentDrives.push_back(*it);
|
||||
}
|
||||
}
|
||||
bool RemovableDriveManager::isDriveMounted(std::string path)
|
||||
{
|
||||
for (auto it = currentDrives.begin(); it != currentDrives.end(); ++it)
|
||||
for (auto it = m_current_drives.begin(); it != m_current_drives.end(); ++it)
|
||||
{
|
||||
if ((*it).path == path)
|
||||
{
|
||||
@ -215,21 +204,16 @@ bool RemovableDriveManager::isDriveMounted(std::string path)
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string RemovableDriveManager::getLastDrivePath()
|
||||
std::string RemovableDriveManager::get_last_drive_path()
|
||||
{
|
||||
if (!currentDrives.empty())
|
||||
if (!m_current_drives.empty())
|
||||
{
|
||||
return currentDrives.back().path;
|
||||
return m_current_drives.back().path;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
void RemovableDriveManager::getAllDrives(std::vector<DriveData>& drives)
|
||||
std::vector<DriveData> RemovableDriveManager::get_all_drives()
|
||||
{
|
||||
drives.clear();
|
||||
drives.reserve(26);
|
||||
for (auto it = currentDrives.begin(); it != currentDrives.end(); ++it)
|
||||
{
|
||||
drives.push_back(*it);
|
||||
}
|
||||
return m_current_drives;
|
||||
}
|
||||
}}
|
@ -25,18 +25,17 @@ public:
|
||||
|
||||
//update() searches for removable devices, returns false if empty.
|
||||
static bool update();
|
||||
static bool isDriveMounted(std::string path);
|
||||
static void ejectDrive(std::string path);
|
||||
static std::string getLastDrivePath();
|
||||
static void getAllDrives(std::vector<DriveData>& drives);
|
||||
static bool is_drive_mounted(const std::string &path);
|
||||
static void eject_drive(const std::string &path);
|
||||
static std::string get_last_drive_path();
|
||||
static std::vector<DriveData> get_all_drives();
|
||||
private:
|
||||
RemovableDriveManager(){}
|
||||
static void searchForDrives(std::vector<DriveData>& newDrives);
|
||||
static void updateCurrentDrives(const std::vector<DriveData>& newDrives);
|
||||
static std::vector<DriveData> currentDrives;
|
||||
static void search_for_drives();
|
||||
static std::vector<DriveData> m_current_drives;
|
||||
#if _WIN32
|
||||
#else
|
||||
static void searchPath(std::vector<DriveData>& newDrives,const std::string path, const dev_t parentDevID);
|
||||
static void search_path(const std::string &path, const dev_t &parentDevID);
|
||||
#endif
|
||||
};
|
||||
}}
|
||||
|
Loading…
Reference in New Issue
Block a user