2019-12-09 14:33:10 +00:00
|
|
|
#import "RemovableDriveManager.hpp"
|
2019-12-10 10:17:12 +00:00
|
|
|
#import "RemovableDriveManagerMM.h"
|
2020-03-06 14:10:58 +00:00
|
|
|
#import "GUI_App.hpp"
|
2019-12-10 09:08:57 +00:00
|
|
|
#import <AppKit/AppKit.h>
|
2019-12-17 12:08:17 +00:00
|
|
|
#import <DiskArbitration/DiskArbitration.h>
|
2019-12-10 09:08:57 +00:00
|
|
|
|
|
|
|
@implementation RemovableDriveManagerMM
|
2019-12-09 14:33:10 +00:00
|
|
|
|
2019-12-10 10:17:12 +00:00
|
|
|
|
2019-12-09 14:33:10 +00:00
|
|
|
|
2019-12-10 09:08:57 +00:00
|
|
|
-(instancetype) init
|
2019-12-09 14:33:10 +00:00
|
|
|
{
|
2019-12-10 09:08:57 +00:00
|
|
|
self = [super init];
|
2020-03-06 14:10:58 +00:00
|
|
|
//if(self){}
|
2019-12-10 09:08:57 +00:00
|
|
|
return self;
|
2019-12-09 14:33:10 +00:00
|
|
|
}
|
2020-03-06 14:10:58 +00:00
|
|
|
|
2019-12-09 14:33:10 +00:00
|
|
|
-(void) on_device_unmount: (NSNotification*) notification
|
|
|
|
{
|
2020-03-06 14:10:58 +00:00
|
|
|
//NSLog(@"on device change");
|
|
|
|
Slic3r::GUI::wxGetApp().removable_drive_manager()->update();
|
2019-12-09 14:33:10 +00:00
|
|
|
}
|
2020-03-06 14:10:58 +00:00
|
|
|
|
2019-12-10 09:08:57 +00:00
|
|
|
-(void) add_unmount_observer
|
|
|
|
{
|
2020-03-06 14:10:58 +00:00
|
|
|
//NSLog(@"add unmount observer");
|
2019-12-10 09:08:57 +00:00
|
|
|
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector: @selector(on_device_unmount:) name:NSWorkspaceDidUnmountNotification object:nil];
|
2019-12-16 14:46:25 +00:00
|
|
|
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector: @selector(on_device_unmount:) name:NSWorkspaceDidMountNotification object:nil];
|
2019-12-10 09:08:57 +00:00
|
|
|
}
|
2020-03-06 14:10:58 +00:00
|
|
|
|
2019-12-10 13:10:47 +00:00
|
|
|
-(NSArray*) list_dev
|
2019-12-10 10:35:39 +00:00
|
|
|
{
|
2019-12-17 12:08:17 +00:00
|
|
|
// DEPRICATED:
|
|
|
|
//NSArray* devices = [[NSWorkspace sharedWorkspace] mountedRemovableMedia];
|
|
|
|
//return devices;
|
2019-12-10 10:35:39 +00:00
|
|
|
|
2019-12-17 12:08:17 +00:00
|
|
|
NSArray *mountedRemovableMedia = [[NSFileManager defaultManager] mountedVolumeURLsIncludingResourceValuesForKeys:nil options:NSVolumeEnumerationSkipHiddenVolumes];
|
|
|
|
NSMutableArray *result = [NSMutableArray array];
|
|
|
|
for(NSURL *volURL in mountedRemovableMedia)
|
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
DADiskRef disk;
|
|
|
|
DASessionRef session;
|
|
|
|
CFDictionaryRef descDict;
|
2020-03-06 14:10:58 +00:00
|
|
|
session = DASessionCreate(nullptr);
|
|
|
|
if (session == nullptr)
|
2019-12-17 12:08:17 +00:00
|
|
|
err = EINVAL;
|
|
|
|
if (err == 0) {
|
2020-03-06 14:10:58 +00:00
|
|
|
disk = DADiskCreateFromVolumePath(nullptr,session,(CFURLRef)volURL);
|
|
|
|
if (session == nullptr)
|
2019-12-17 12:08:17 +00:00
|
|
|
err = EINVAL;
|
|
|
|
}
|
|
|
|
if (err == 0) {
|
|
|
|
descDict = DADiskCopyDescription(disk);
|
2020-03-06 14:10:58 +00:00
|
|
|
if (descDict == nullptr)
|
2019-12-17 12:08:17 +00:00
|
|
|
err = EINVAL;
|
|
|
|
}
|
|
|
|
if (err == 0) {
|
|
|
|
CFTypeRef mediaEjectableKey = CFDictionaryGetValue(descDict,kDADiskDescriptionMediaEjectableKey);
|
|
|
|
BOOL ejectable = [mediaEjectableKey boolValue];
|
|
|
|
CFTypeRef deviceProtocolName = CFDictionaryGetValue(descDict,kDADiskDescriptionDeviceProtocolKey);
|
|
|
|
CFTypeRef deviceModelKey = CFDictionaryGetValue(descDict, kDADiskDescriptionDeviceModelKey);
|
2020-03-06 14:10:58 +00:00
|
|
|
if (mediaEjectableKey != nullptr) {
|
2019-12-17 12:08:17 +00:00
|
|
|
BOOL op = ejectable && (CFEqual(deviceProtocolName, CFSTR("USB")) || CFEqual(deviceModelKey, CFSTR("SD Card Reader")));
|
|
|
|
//!CFEqual(deviceModelKey, CFSTR("Disk Image"));
|
2020-03-06 14:10:58 +00:00
|
|
|
if (op)
|
2019-12-17 12:08:17 +00:00
|
|
|
[result addObject:volURL.path];
|
|
|
|
}
|
|
|
|
}
|
2020-03-06 14:10:58 +00:00
|
|
|
if (descDict != nullptr)
|
2019-12-17 12:08:17 +00:00
|
|
|
CFRelease(descDict);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2020-03-06 14:10:58 +00:00
|
|
|
|
|
|
|
//this eject drive is not used now
|
2019-12-17 12:08:17 +00:00
|
|
|
-(void)eject_drive:(NSString *)path
|
|
|
|
{
|
|
|
|
DADiskRef disk;
|
|
|
|
DASessionRef session;
|
|
|
|
NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
|
|
|
|
int err = 0;
|
2020-03-06 14:10:58 +00:00
|
|
|
session = DASessionCreate(nullptr);
|
|
|
|
if (session == nullptr)
|
2019-12-17 12:08:17 +00:00
|
|
|
err = EINVAL;
|
2020-03-06 14:10:58 +00:00
|
|
|
if (err == 0)
|
|
|
|
disk = DADiskCreateFromVolumePath(nullptr,session,(CFURLRef)url);
|
2019-12-17 12:08:17 +00:00
|
|
|
if( err == 0)
|
2020-03-06 14:10:58 +00:00
|
|
|
DADiskUnmount(disk, kDADiskUnmountOptionDefault, nullptr, nullptr);
|
|
|
|
if (disk != nullptr)
|
2019-12-17 12:08:17 +00:00
|
|
|
CFRelease(disk);
|
2020-03-06 14:10:58 +00:00
|
|
|
if (session != nullptr)
|
2019-12-17 12:08:17 +00:00
|
|
|
CFRelease(session);
|
2019-12-10 10:35:39 +00:00
|
|
|
}
|
2020-03-06 14:10:58 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2019-12-10 10:17:12 +00:00
|
|
|
namespace Slic3r {
|
|
|
|
namespace GUI {
|
2020-03-06 14:10:58 +00:00
|
|
|
|
|
|
|
void RemovableDriveManager::register_window_osx()
|
|
|
|
{
|
|
|
|
assert(m_impl_osx == nullptr);
|
|
|
|
m_impl_osx = [[RemovableDriveManagerMM alloc] init];
|
|
|
|
if (m_impl_osx)
|
|
|
|
[m_impl_osx add_unmount_observer];
|
2019-12-10 13:10:47 +00:00
|
|
|
}
|
2020-03-06 14:10:58 +00:00
|
|
|
|
|
|
|
void RemovableDriveManager::unregister_window_osx()
|
2019-12-10 09:08:57 +00:00
|
|
|
{
|
2020-03-06 14:10:58 +00:00
|
|
|
if (m_impl_osx)
|
|
|
|
[m_impl_osx release];
|
2019-12-10 09:08:57 +00:00
|
|
|
}
|
2020-03-06 14:10:58 +00:00
|
|
|
|
|
|
|
namespace search_for_drives_internal
|
2019-12-10 10:35:39 +00:00
|
|
|
{
|
2020-03-06 14:10:58 +00:00
|
|
|
void inspect_file(const std::string &path, const std::string &parent_path, std::vector<DriveData> &out);
|
2019-12-10 13:10:47 +00:00
|
|
|
}
|
2020-03-06 14:10:58 +00:00
|
|
|
|
|
|
|
void RemovableDriveManager::list_devices(std::vector<DriveData> &out) const
|
2019-12-10 13:10:47 +00:00
|
|
|
{
|
2020-03-06 14:10:58 +00:00
|
|
|
assert(m_impl_osx != nullptr);
|
|
|
|
if (m_impl_osx) {
|
|
|
|
NSArray* devices = [m_impl_osx list_dev];
|
2019-12-10 13:10:47 +00:00
|
|
|
for (NSString* volumePath in devices)
|
2020-03-06 14:10:58 +00:00
|
|
|
search_for_drives_internal::inspect_file(std::string([volumePath UTF8String]), "/Volumes", out);
|
2019-12-10 13:10:47 +00:00
|
|
|
}
|
2019-12-10 10:35:39 +00:00
|
|
|
}
|
2020-03-06 14:10:58 +00:00
|
|
|
|
|
|
|
// not used as of now
|
|
|
|
void RemovableDriveManager::eject_device(const std::string &path)
|
2019-12-17 12:08:17 +00:00
|
|
|
{
|
2020-03-06 14:10:58 +00:00
|
|
|
assert(m_impl_osx != nullptr);
|
|
|
|
if (m_impl_osx) {
|
|
|
|
NSString * pth = [NSString stringWithCString:path.c_str() encoding:[NSString defaultCStringEncoding]];
|
|
|
|
[m_impl_osx eject_drive:pth];
|
2019-12-17 12:08:17 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-10 09:08:57 +00:00
|
|
|
|
2020-03-06 14:10:58 +00:00
|
|
|
}}//namespace Slicer::GUI
|