PrusaSlicer-NonPlainar/src/slic3r/GUI/RemovableDriveManagerMM.mm

164 lines
5.1 KiB
Plaintext
Raw Normal View History

#import "RemovableDriveManager.hpp"
2019-12-10 10:17:12 +00:00
#import "RemovableDriveManagerMM.h"
#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
static void eject_callback(DADiskRef disk, DADissenterRef dissenter, void *context)
{
NSLog(@"eject successfull");
}
static void unmount_callback(DADiskRef disk, DADissenterRef dissenter, void *context)
{
//if (dissenter) {
//?
//} else {
DADiskEject(disk, kDADiskEjectOptionDefault, eject_callback, context);
//}
}
2019-12-10 10:17:12 +00:00
@implementation RemovableDriveManagerMM
2019-12-10 09:08:57 +00:00
-(instancetype) init
{
2019-12-10 09:08:57 +00:00
self = [super init];
//if(self){}
2019-12-10 09:08:57 +00:00
return self;
}
-(void) on_device_unmount: (NSNotification*) notification
{
//NSLog(@"on device change");
Slic3r::GUI::wxGetApp().removable_drive_manager()->update();
}
2019-12-10 09:08:57 +00:00
-(void) add_unmount_observer
{
//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
}
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;
session = DASessionCreate(nullptr);
if (session == nullptr)
2019-12-17 12:08:17 +00:00
err = EINVAL;
if (err == 0) {
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);
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);
2019-12-17 12:08:17 +00:00
CFTypeRef deviceModelKey = CFDictionaryGetValue(descDict, kDADiskDescriptionDeviceModelKey);
//debug logging
/*
if (deviceProtocolName)
NSLog(@"%@",(CFStringRef)deviceProtocolName);
if (deviceModelKey)
NSLog(@"-%@",(CFStringRef)deviceModelKey);
*/
if (mediaEjectableKey != nullptr) {
BOOL op = ejectable && (CFEqual(deviceProtocolName, CFSTR("USB")) || CFEqual(deviceModelKey, CFSTR("SD Card Reader")) || CFEqual(deviceProtocolName, CFSTR("Secure Digital")));
2019-12-17 12:08:17 +00:00
//!CFEqual(deviceModelKey, CFSTR("Disk Image"));
if (op)
2019-12-17 12:08:17 +00:00
[result addObject:volURL.path];
}
}
if (descDict != nullptr)
2019-12-17 12:08:17 +00:00
CFRelease(descDict);
}
return result;
}
//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;
session = DASessionCreate(nullptr);
if (session == nullptr)
2019-12-17 12:08:17 +00:00
err = EINVAL;
if (err == 0)
disk = DADiskCreateFromVolumePath(nullptr,session,(CFURLRef)url);
2019-12-17 12:08:17 +00:00
if( err == 0)
//DADiskUnmount(disk, kDADiskUnmountOptionDefault, nullptr, nullptr);
DADiskUnmount(disk, kDADiskUnmountOptionWhole | kDADiskUnmountOptionForce, unmount_callback, nullptr);
if (disk != nullptr)
2019-12-17 12:08:17 +00:00
CFRelease(disk);
if (session != nullptr)
2019-12-17 12:08:17 +00:00
CFRelease(session);
2019-12-10 10:35:39 +00:00
}
@end
2019-12-10 10:17:12 +00:00
namespace Slic3r {
namespace GUI {
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
}
void RemovableDriveManager::unregister_window_osx()
2019-12-10 09:08:57 +00:00
{
if (m_impl_osx) {
[m_impl_osx release];
m_impl_osx = nullptr;
}
2019-12-10 09:08:57 +00:00
}
namespace search_for_drives_internal
2019-12-10 10:35:39 +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
}
void RemovableDriveManager::list_devices(std::vector<DriveData> &out) const
2019-12-10 13:10:47 +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)
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
}
// not used as of now
void RemovableDriveManager::eject_device(const std::string &path)
2019-12-17 12:08:17 +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
}}//namespace Slicer::GUI