Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RemovableDriveManagerMM.mm « GUI « slic3r « src - github.com/supermerill/SuperSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3e4b32f29520893a8067eae91ac22a81285d79ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#import "RemovableDriveManager.hpp"
#import "RemovableDriveManagerMM.h"
#import "GUI_App.hpp"
#import <AppKit/AppKit.h> 
#import <DiskArbitration/DiskArbitration.h>

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);
    //}
}

@implementation RemovableDriveManagerMM

-(instancetype) init
{
	self = [super init];
	//if(self){}
	return self;
}

-(void) on_device_unmount: (NSNotification*) notification
{
    //NSLog(@"on device change");
    Slic3r::GUI::wxGetApp().removable_drive_manager()->update();
}

-(void) 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:NSWorkspaceDidMountNotification object:nil];
}

-(NSArray*) list_dev
{
    // DEPRICATED:
    //NSArray* devices = [[NSWorkspace sharedWorkspace] mountedRemovableMedia];
	//return devices;
    
    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)
            err = EINVAL;
        if (err == 0) {
            disk = DADiskCreateFromVolumePath(nullptr,session,(CFURLRef)volURL);
            if (session == nullptr)
                err = EINVAL;
        }
        if (err == 0) {
            descDict = DADiskCopyDescription(disk);
            if (descDict == nullptr)
                err = EINVAL;
        }
        if (err == 0) {
            CFTypeRef mediaEjectableKey = CFDictionaryGetValue(descDict,kDADiskDescriptionMediaEjectableKey);
            BOOL ejectable = [mediaEjectableKey boolValue];
            CFTypeRef deviceProtocolName = CFDictionaryGetValue(descDict,kDADiskDescriptionDeviceProtocolKey);
            
            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")));
                //!CFEqual(deviceModelKey, CFSTR("Disk Image"));
                if (op)
                    [result addObject:volURL.path];
            }
        }
        if (descDict != nullptr)
            CFRelease(descDict);
    }
    return result;
}

//this eject drive is not used now
-(void)eject_drive:(NSString *)path
{
    DADiskRef disk;
    DASessionRef session;
    NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
    int err = 0;
    session = DASessionCreate(nullptr);
    if (session == nullptr)
        err = EINVAL;
    if (err == 0)
        disk = DADiskCreateFromVolumePath(nullptr,session,(CFURLRef)url);
    if( err == 0)
        //DADiskUnmount(disk, kDADiskUnmountOptionDefault, nullptr, nullptr);
        DADiskUnmount(disk, kDADiskUnmountOptionWhole | kDADiskUnmountOptionForce, unmount_callback, nullptr);
    if (disk != nullptr)
        CFRelease(disk);
    if (session != nullptr)
        CFRelease(session);
}

@end

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];
}

void RemovableDriveManager::unregister_window_osx()
{
    if (m_impl_osx)
        [m_impl_osx release];
}

namespace search_for_drives_internal 
{
    void inspect_file(const std::string &path, const std::string &parent_path, std::vector<DriveData> &out);
}

void RemovableDriveManager::list_devices(std::vector<DriveData> &out) const
{
    assert(m_impl_osx != nullptr);
    if (m_impl_osx) {
    	NSArray* devices = [m_impl_osx list_dev];
    	for (NSString* volumePath in devices)
        	search_for_drives_internal::inspect_file(std::string([volumePath UTF8String]), "/Volumes", out);
    }
}

// not used as of now
void RemovableDriveManager::eject_device(const std::string &path)
{
    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];
    }
}

}}//namespace Slicer::GUI