macos better wrapper

This commit is contained in:
David Kocik 2019-12-10 14:10:47 +01:00
parent 3b6daf64c7
commit d2a440c794
4 changed files with 87 additions and 66 deletions

View file

@ -220,9 +220,11 @@ void RemovableDriveManager::search_for_drives()
{ {
#if __APPLE__ #if __APPLE__
list_devices(); if(m_rdmmm)
#endif {
m_rdmmm->list_devices();
}
#else
m_current_drives.clear(); m_current_drives.clear();
m_current_drives.reserve(26); m_current_drives.reserve(26);
@ -273,6 +275,7 @@ void RemovableDriveManager::search_for_drives()
} }
//std::cout << "found drives:" <<m_current_drives.size() << "\n"; //std::cout << "found drives:" <<m_current_drives.size() << "\n";
#endif
} }
void RemovableDriveManager::search_path(const std::string &path,const std::string &parent_path) void RemovableDriveManager::search_path(const std::string &path,const std::string &parent_path)
{ {
@ -283,24 +286,7 @@ void RemovableDriveManager::search_path(const std::string &path,const std::strin
{ {
for(size_t i = 0; i < globbuf.gl_pathc; i++) for(size_t i = 0; i < globbuf.gl_pathc; i++)
{ {
//if not same file system - could be removable drive
if(!compare_filesystem_id(globbuf.gl_pathv[i], parent_path))
{
//user id
struct stat buf;
stat(globbuf.gl_pathv[i],&buf);
uid_t uid = buf.st_uid;
std::string username(std::getenv("USER"));
struct passwd *pw = getpwuid(uid);
if(pw != 0)
{
if(pw->pw_name == username)
{
std::string name = basename(globbuf.gl_pathv[i]);
m_current_drives.push_back(DriveData(name,globbuf.gl_pathv[i]));
}
}
}
} }
}else }else
{ {
@ -310,7 +296,27 @@ void RemovableDriveManager::search_path(const std::string &path,const std::strin
globfree(&globbuf); globfree(&globbuf);
} }
void RemovableDriveManager::inspect_file(const std::string &path, const std::string &parent_path)
{
//if not same file system - could be removable drive
if(!compare_filesystem_id(globbuf.gl_pathv[i], parent_path))
{
//user id
struct stat buf;
stat(globbuf.gl_pathv[i],&buf);
uid_t uid = buf.st_uid;
std::string username(std::getenv("USER"));
struct passwd *pw = getpwuid(uid);
if(pw != 0)
{
if(pw->pw_name == username)
{
std::string name = basename(globbuf.gl_pathv[i]);
m_current_drives.push_back(DriveData(name,globbuf.gl_pathv[i]));
}
}
}
}
bool RemovableDriveManager::compare_filesystem_id(const std::string &path_a, const std::string &path_b) bool RemovableDriveManager::compare_filesystem_id(const std::string &path_a, const std::string &path_b)
{ {
struct stat buf; struct stat buf;

View file

@ -39,7 +39,11 @@ public:
void reset_last_save_path(); void reset_last_save_path();
void print(); void print();
private: private:
RemovableDriveManager():m_drives_count(0),m_last_update(0),m_last_save_path(""),m_rdmmm(nullptr){} #if __APPLE__
RemovableDriveManager():m_drives_count(0),m_last_update(0),m_last_save_path(""),m_rdmmm(new RemovableDriveManagerMM()){}
#else
RemovableDriveManager() : m_drives_count(0), m_last_update(0), m_last_save_path(""){}
#endif
void search_for_drives(); void search_for_drives();
void check_and_notify(); void check_and_notify();
std::string get_drive_from_path(const std::string& path);//returns drive path (same as path in DriveData) if exists otherwise empty string "" std::string get_drive_from_path(const std::string& path);//returns drive path (same as path in DriveData) if exists otherwise empty string ""
@ -51,16 +55,27 @@ private:
#if _WIN32 #if _WIN32
void register_window(); void register_window();
//INT_PTR WINAPI WinProcCallback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); //INT_PTR WINAPI WinProcCallback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
#elif __APPLE__
void *m_rdmmm;
void register_window();
void list_devices();
void search_path(const std::string &path, const std::string &parent_path);
bool compare_filesystem_id(const std::string &path_a, const std::string &path_b);
#else #else
void search_path(const std::string &path, const std::string &parent_path); #if __APPLE__
bool compare_filesystem_id(const std::string &path_a, const std::string &path_b); RemovableDriveManagerMM * m_rdmmm;
#endif
void search_path(const std::string &path, const std::string &parent_path);
void inspect_file(const std::string &path, const std::string &parent_path);
bool compare_filesystem_id(const std::string &path_a, const std::string &path_b);
#endif #endif
}; };
#if __APPLE__
class RemovableDriveManagerMM
{
public:
RemovableDriveManagerMM();
~RemovableDriveManagerMM();
register_window();
list_devices();
private:
RemovableDriveManagerMMImpl *m_imp;
friend void RemovableDriveManager::inspect_file(const std::string &path, const std::string &parent_path);
};
#endif
}} }}
#endif #endif

View file

@ -5,5 +5,5 @@
-(instancetype) init; -(instancetype) init;
-(void) add_unmount_observer; -(void) add_unmount_observer;
-(void) on_device_unmount: (NSNotification*) notification; -(void) on_device_unmount: (NSNotification*) notification;
-(void) list_dev; -(NSArray*) list_dev;
@end @end

View file

@ -10,8 +10,7 @@
{ {
self = [super init]; self = [super init];
if(self) if(self)
{ {
[self add_unmount_observer];
} }
return self; return self;
} }
@ -25,48 +24,49 @@
NSLog(@"add unmount observer"); NSLog(@"add unmount observer");
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector: @selector(on_device_unmount:) name:NSWorkspaceDidUnmountNotification object:nil]; [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector: @selector(on_device_unmount:) name:NSWorkspaceDidUnmountNotification object:nil];
} }
-(void) list_dev -(NSArray*) list_dev
{ {
NSLog(@"---");
NSArray* devices = [[NSWorkspace sharedWorkspace] mountedRemovableMedia]; NSArray* devices = [[NSWorkspace sharedWorkspace] mountedRemovableMedia];
for (NSString* volumePath in devices) for (NSString* volumePath in devices)
{ {
NSLog(@"@", volumePath); NSLog(@"%@", volumePath);
} }
NSLog(@"--"); return devices;
//removable here means CD not USB :/
NSArray* listOfMedia = [[NSWorkspace sharedWorkspace] mountedLocalVolumePaths];
NSLog(@"%@", listOfMedia);
for (NSString* volumePath in listOfMedia)
{
BOOL isRemovable = NO;
BOOL isWritable = NO;
BOOL isUnmountable = NO;
NSString* description = [NSString string];
NSString* type = [NSString string];
BOOL result = [[NSWorkspace sharedWorkspace] getFileSystemInfoForPath:volumePath
isRemovable:&isRemovable
isWritable:&isWritable
isUnmountable:&isUnmountable
description:&description
type:&type];
NSLog(@"Result:%i Volume: %@, Removable:%i, W:%i, Unmountable:%i, Desc:%@, type:%@", result, volumePath, isRemovable, isWritable, isUnmountable, description, type);
}
} }
namespace Slic3r { namespace Slic3r {
namespace GUI { namespace GUI {
void RemovableDriveManager::register_window() struct RemovableDriveManagerMMImpl{
{ RemovableDriveManagerMM * wrap;
m_rdmmm = nullptr;
m_rdmmm = [[RemovableDriveManagerMM alloc] init];
} }
void RemovableDriveManager::list_devices() RemovableDriveManagerMM():impl(new RemovableDriveManagerMMImpl){
impl->wrap = [[RemovableDriveManagerMM alloc] init];
}
RemovableDriveManagerMM::~RemovableDriveManagerMM()
{ {
if(m_rdmmm == nullptr) if(impl)
return; {
[m_rdmmm list_dev]; [impl->wrap release];
}
}
void RDMMMWrapper::register_window()
{
if(impl->wrap)
{
[impl->wrap add_unmount_observer];
}
}
void RDMMMWrapper::list_devices()
{
if(impl->wrap)
{
NSArray* devices = [impl->wrap list_dev];
for (NSString* volumePath in devices)
{
NSLog(@"%@", volumePath);
Slic3r::GUI::RemovableDriveManager::get_instance().inspect_file(std::string([volumePath UTF8String]), "/Volumes");
}
}
} }
}}//namespace Slicer::GUI }}//namespace Slicer::GUI