mirror of
https://git.mirrors.martin98.com/https://github.com/prusa3d/PrusaSlicer.git
synced 2025-07-12 22:31:52 +08:00
refactoring
This commit is contained in:
parent
4bf8a0ef24
commit
72f1adbb44
@ -363,7 +363,7 @@ std::string AppConfig::get_last_output_dir(const std::string &alt) const
|
|||||||
=======
|
=======
|
||||||
if (GUI::RemovableDriveManager::getInstance().update())
|
if (GUI::RemovableDriveManager::getInstance().update())
|
||||||
{
|
{
|
||||||
return GUI::RemovableDriveManager::getInstance().getLastDrivePath();
|
return GUI::RemovableDriveManager::getInstance().get_last_drive_path();
|
||||||
}
|
}
|
||||||
>>>>>>> removable drive manager - Windows part
|
>>>>>>> removable drive manager - Windows part
|
||||||
const auto it = m_storage.find("");
|
const auto it = m_storage.find("");
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#include "RemovableDriveManager.hpp"
|
#include "RemovableDriveManager.hpp"
|
||||||
<<<<<<< HEAD
|
<<<<<<< HEAD
|
||||||
|
<<<<<<< HEAD
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "boost/nowide/convert.hpp"
|
#include "boost/nowide/convert.hpp"
|
||||||
|
|
||||||
@ -71,13 +72,16 @@ void RemovableDriveManager::search_for_drives()
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
=======
|
||||||
|
>>>>>>> refactoring
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include "boost/nowide/convert.hpp"
|
||||||
|
|
||||||
#if _WIN32
|
#if _WIN32
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include <tchar.h>
|
#include <tchar.h>
|
||||||
|
#include <winioctl.h>
|
||||||
DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE,
|
DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE,
|
||||||
0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);
|
0xA5DCBF10L, 0x6530, 0x11D2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED);
|
||||||
#else
|
#else
|
||||||
@ -92,16 +96,13 @@ DEFINE_GUID(GUID_DEVINTERFACE_USB_DEVICE,
|
|||||||
namespace Slic3r {
|
namespace Slic3r {
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
|
||||||
std::vector<DriveData> RemovableDriveManager::currentDrives;
|
std::vector<DriveData> RemovableDriveManager::m_current_drives;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if _WIN32
|
#if _WIN32
|
||||||
void RemovableDriveManager::searchForDrives(std::vector<DriveData>& newDrives)
|
void RemovableDriveManager::search_for_drives()
|
||||||
{
|
{
|
||||||
newDrives.clear();
|
m_current_drives.clear();
|
||||||
newDrives.reserve(26);
|
m_current_drives.reserve(26);
|
||||||
DWORD drivesMask = GetLogicalDrives();
|
DWORD drivesMask = GetLogicalDrives();
|
||||||
for (size_t i = 0; i < 26; i++)
|
for (size_t i = 0; i < 26; i++)
|
||||||
{
|
{
|
||||||
@ -134,8 +135,12 @@ void RemovableDriveManager::searchForDrives(std::vector<DriveData>& newDrives)
|
|||||||
//std::cout << std::string(volumeName.begin(), volumeName.end()) << " " << std::string(fileSystemName.begin(), fileSystemName.end()) << " " << freeSpace.QuadPart << "\n";
|
//std::cout << std::string(volumeName.begin(), volumeName.end()) << " " << std::string(fileSystemName.begin(), fileSystemName.end()) << " " << freeSpace.QuadPart << "\n";
|
||||||
if (freeSpace.QuadPart > 0)
|
if (freeSpace.QuadPart > 0)
|
||||||
{
|
{
|
||||||
|
<<<<<<< HEAD
|
||||||
newDrives.push_back(DriveData(volumeName, path));
|
newDrives.push_back(DriveData(volumeName, path));
|
||||||
>>>>>>> removable drive manager - Windows part
|
>>>>>>> removable drive manager - Windows part
|
||||||
|
=======
|
||||||
|
m_current_drives.push_back(DriveData(boost::nowide::narrow(volumeName), path));
|
||||||
|
>>>>>>> refactoring
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -597,49 +602,51 @@ void RemovableDriveManager::reset_last_save_path()
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
void RemovableDriveManager::ejectDrive(std::string path)
|
void RemovableDriveManager::eject_drive(const std::string &path)
|
||||||
{
|
{
|
||||||
if (!update() || !isDriveMounted(path))
|
|
||||||
return;
|
|
||||||
|
|
||||||
path = "\\\\.\\"+path;
|
//if (!update() || !is_drive_mounted(path))
|
||||||
HANDLE handle = CreateFileA(path.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
|
if(m_current_drives.empty())
|
||||||
if(handle == INVALID_HANDLE_VALUE)
|
|
||||||
{
|
|
||||||
std::cerr << "Ejecting " << path << " failed " << GetLastError() << " \n";
|
|
||||||
return;
|
return;
|
||||||
}
|
for (auto it = m_current_drives.begin(); it != m_current_drives.end(); ++it)
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
if ((*it).path == path)
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
void RemovableDriveManager::searchForDrives(std::vector<DriveData>& newDrives)
|
void RemovableDriveManager::search_for_drives()
|
||||||
{
|
{
|
||||||
struct stat buf;
|
struct stat buf;
|
||||||
std::string path(std::getenv("USER"));
|
std::string path(std::getenv("USER"));
|
||||||
std::string pp(path);
|
std::string pp(path);
|
||||||
|
|
||||||
newDrives.clear();
|
m_current_drives.clear();
|
||||||
newDrives.reserve(26);
|
m_current_Drives.reserve(26);
|
||||||
|
|
||||||
//search /media/* folder
|
//search /media/* folder
|
||||||
stat("/media/",&buf);
|
stat("/media/",&buf);
|
||||||
std::cout << "/media ID: " <<buf.st_dev << "\n";
|
std::cout << "/media ID: " <<buf.st_dev << "\n";
|
||||||
searchPath(newDrives, "/media/*", buf.st_dev);
|
search_path("/media/*", buf.st_dev);
|
||||||
|
|
||||||
//search /media/USERNAME/* folder
|
//search /media/USERNAME/* folder
|
||||||
pp = "/media/"+pp;
|
pp = "/media/"+pp;
|
||||||
@ -647,18 +654,18 @@ void RemovableDriveManager::searchForDrives(std::vector<DriveData>& newDrives)
|
|||||||
|
|
||||||
stat(pp.c_str() ,&buf);
|
stat(pp.c_str() ,&buf);
|
||||||
std::cout << pp <<" ID: " <<buf.st_dev << "\n";
|
std::cout << pp <<" ID: " <<buf.st_dev << "\n";
|
||||||
searchPath(newDrives, path, buf.st_dev);
|
searchPath(path, buf.st_dev);
|
||||||
|
|
||||||
//search /run/media/USERNAME/* folder
|
//search /run/media/USERNAME/* folder
|
||||||
path = "/run" + path;
|
path = "/run" + path;
|
||||||
pp = "/run"+pp;
|
pp = "/run"+pp;
|
||||||
stat(pp.c_str() ,&buf);
|
stat(pp.c_str() ,&buf);
|
||||||
std::cout << pp <<" ID: " <<buf.st_dev << "\n";
|
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";
|
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;
|
glob_t globbuf;
|
||||||
globbuf.gl_offs = 2;
|
globbuf.gl_offs = 2;
|
||||||
@ -679,17 +686,17 @@ void RemovableDriveManager::searchPath(std::vector<DriveData>& newDrives,const s
|
|||||||
std::cout << buf.st_dev << "\n";
|
std::cout << buf.st_dev << "\n";
|
||||||
if(buf.st_dev != parentDevID)// not same file system
|
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);
|
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;
|
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)
|
if((*it).path == path)
|
||||||
{
|
{
|
||||||
@ -700,7 +707,7 @@ void RemovableDriveManager::ejectDrive(std::string path)
|
|||||||
int errsv = errno;
|
int errsv = errno;
|
||||||
std::cerr<<"Ejecting failed Error "<< errsv<<"\n";
|
std::cerr<<"Ejecting failed Error "<< errsv<<"\n";
|
||||||
}
|
}
|
||||||
currentDrives.erase(it);
|
m_current_drives.erase(it);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -710,22 +717,14 @@ void RemovableDriveManager::ejectDrive(std::string path)
|
|||||||
#endif
|
#endif
|
||||||
bool RemovableDriveManager::update()
|
bool RemovableDriveManager::update()
|
||||||
{
|
{
|
||||||
searchForDrives(currentDrives);
|
search_for_drives();
|
||||||
return !currentDrives.empty();
|
return !m_current_drives.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
void RemovableDriveManager::updateCurrentDrives(const std::vector<DriveData>& newDrives)
|
|
||||||
|
bool RemovableDriveManager::is_drive_mounted(const std::string &path)
|
||||||
{
|
{
|
||||||
currentDrives.clear();
|
for (auto it = m_current_drives.begin(); it != m_current_drives.end(); ++it)
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
if ((*it).path == path)
|
if ((*it).path == path)
|
||||||
{
|
{
|
||||||
@ -735,22 +734,17 @@ bool RemovableDriveManager::isDriveMounted(std::string path)
|
|||||||
return false;
|
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 "";
|
return "";
|
||||||
}
|
}
|
||||||
void RemovableDriveManager::getAllDrives(std::vector<DriveData>& drives)
|
std::vector<DriveData> RemovableDriveManager::get_all_drives()
|
||||||
{
|
{
|
||||||
drives.clear();
|
return m_current_drives;
|
||||||
drives.reserve(26);
|
|
||||||
for (auto it = currentDrives.begin(); it != currentDrives.end(); ++it)
|
|
||||||
{
|
|
||||||
drives.push_back(*it);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
>>>>>>> removable drive manager - Windows part
|
>>>>>>> removable drive manager - Windows part
|
||||||
|
@ -112,18 +112,17 @@ protected:
|
|||||||
|
|
||||||
//update() searches for removable devices, returns false if empty.
|
//update() searches for removable devices, returns false if empty.
|
||||||
static bool update();
|
static bool update();
|
||||||
static bool isDriveMounted(std::string path);
|
static bool is_drive_mounted(const std::string &path);
|
||||||
static void ejectDrive(std::string path);
|
static void eject_drive(const std::string &path);
|
||||||
static std::string getLastDrivePath();
|
static std::string get_last_drive_path();
|
||||||
static void getAllDrives(std::vector<DriveData>& drives);
|
static std::vector<DriveData> get_all_drives();
|
||||||
private:
|
private:
|
||||||
RemovableDriveManager(){}
|
RemovableDriveManager(){}
|
||||||
static void searchForDrives(std::vector<DriveData>& newDrives);
|
static void search_for_drives();
|
||||||
static void updateCurrentDrives(const std::vector<DriveData>& newDrives);
|
static std::vector<DriveData> m_current_drives;
|
||||||
static std::vector<DriveData> currentDrives;
|
|
||||||
#if _WIN32
|
#if _WIN32
|
||||||
#else
|
#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
|
#endif
|
||||||
};
|
};
|
||||||
}}
|
}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user