-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExpanDriveAction.m
316 lines (283 loc) · 9.21 KB
/
ExpanDriveAction.m
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//
// ExpanDriveAction.m
// ExpanDriveNLPlugin
//
// Created by Christopher Campbell Jensen on 10/26/10.
//
#import "ExpanDriveAction.h"
#import "ExpanDriveOptionSheetController.h"
#import "Constants.h"
@implementation ExpanDriveAction
- (void)dealloc
{
[expanDrive release];
[super dealloc];
}
/* The action ID only has to be unique within the scope of the bundle.
By default, NLAction will assume the action ID to be the name of the nib
containing the option sheet.
*/
+ (NSString *)actionID
{
return @"ExpanDriveAction";
}
/* the name that will appear in the "add action" sheet
*/
+ (NSString *)title
{
return @"ExpanDrive…";
}
+ (NSImage *)icon
{
static NSImage * icon = nil;
if (!icon) {
NSString * path = [[NSWorkspace sharedWorkspace] fullPathForApplication: @"ExpanDrive"];
path = [path stringByAppendingPathComponent: @"Contents/Resources/ExpanDrive.icns"];
icon = [[NSImage alloc] initWithContentsOfFile: path];
}
return icon;
}
/* hides plugin if application is not installed
*/
+ (BOOL)invisible
{
NSString *path = [[NSWorkspace sharedWorkspace] fullPathForApplication: @"ExpanDrive"];
if (!path)
NSLog(@"ExpanDrivePlugin: %@", [NSString stringWithString:@"Application ExpanDrive not found. Disabling plugin"]);
return !path;
}
/* this method is invoked to get a description of the action to show in the
location's list of actions
*/
- (NSString *)title
{
NSString *title;
NSString *action = [self selectedActionTitle];
if ([[self class] invisible]) {
[self logString:[NSString stringWithString:@"ERROR: Orphaned action"]];
return [NSString stringWithString:@"Application ExpanDrive not found"];
}
int performAction = [self selectedSingleOrMultiple];
switch (performAction) {
case PERFORMSINGLEACTION: {
ScriptBridgeDrive *drive = [self selectedDrive];
NSString *drivename = [drive drivename] ? [drive drivename] : @"";
title = [NSString stringWithFormat:@"%@ %@", action, drivename];
break;
}
case PERFORMMULTIPLEACTION: {
NSString *driveContains = [self selectedDriveContains] ? [self selectedDriveContains] : @"";
title = [NSString stringWithFormat:@"%@ %@", action, driveContains];
break;
}
case PERFORMALLACTION: {
title = [NSString stringWithFormat:@"%@ all drives", action];
break;
}
default:
title = [NSString stringWithFormat:@"%@ <unknown>", action];
break;
}
return title;
}
/* actually do the action
*/
- (void)performAction
{
[self logString:[NSString stringWithFormat:@"ExpanDrivePlugin: %@", [self title]]];
int performAction = [self selectedSingleOrMultiple];
switch (performAction) {
case PERFORMSINGLEACTION:
[self performSingleAction];
break;
case PERFORMMULTIPLEACTION:
[self performMultipleAction];
break;
case PERFORMALLACTION:
[self performAllAction];
break;
default:
[self logString:[NSString stringWithFormat:@"ERROR: Perform action with id <%d> not recognised", performAction]];
break;
}
}
- (void)performSingleAction
{
ScriptBridgeDrive *drive = [self selectedDrive];
if (!drive) {
//no valid drive object, skip action
[self logString:[NSString stringWithFormat:@"Drive not found or not set."]];
return;
}
int action = [self selectedAction];
switch (action) {
case ACTIONCONNECT:
if (![drive isConnected]) {
[drive connect];
[self logString:[NSString stringWithFormat:@"Connected to %@", [[self selectedDrive] drivename]]];
}
break;
case ACTIONEJECT:
if ([drive isConnected]) {
[drive eject];
[self logString:[NSString stringWithFormat:@"Ejected %@", [[self selectedDrive] drivename]]];
}
break;
default:
[self logString:[NSString stringWithFormat:@"ERROR: Action with id <%d> not recognised", action]];
break;
}
}
- (void)performMultipleAction
{
NSString *driveProperty = [self selectedDriveproperty];
NSString *driveContains = [self selectedDriveContains];
int action = [self selectedAction];
switch (action) {
case ACTIONCONNECT: {
NSArray *matchingDrives = [[[self expanDrive] drives] filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:@"(isConnected == NO) AND (%K contains %@)", driveProperty, driveContains]];
if ( [matchingDrives count] >= 1 ) {
[self logString:[NSString stringWithFormat:@"Connecting %d drives...", [matchingDrives count]]];
[matchingDrives makeObjectsPerformSelector:@selector(connect)];
}
[self logString:[NSString stringWithFormat:@"Connected to all ejected drives whose %@ contains %@", driveProperty, driveContains]];
} break;
case ACTIONEJECT: {
NSArray *matchingDrives = [[[self expanDrive] drives] filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:@"(isConnected == YES) AND (%K contains %@)", driveProperty, driveContains]];
if ( [matchingDrives count] >= 1 ) {
[self logString:[NSString stringWithFormat:@"Ejecting %d drives...", [matchingDrives count]]];
[matchingDrives makeObjectsPerformSelector:@selector(eject)];
}
[self logString:[NSString stringWithFormat:@"Ejected all connected drives whose %@ contains %@", driveProperty, driveContains]];
} break;
default:
[self logString:[NSString stringWithFormat:@"ERROR: Action with id <%d> not recognised"]];
break;
}
}
- (void)performAllAction
{
int action = [self selectedAction];
switch (action) {
case ACTIONCONNECT: {
NSArray *matchingDrives = [[[self expanDrive] drives] filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:@"(isConnected == NO)"]];
if ( [matchingDrives count] >= 1 ) {
[self logString:[NSString stringWithFormat:@"Connecting %d drives...", [matchingDrives count]]];
[matchingDrives makeObjectsPerformSelector:@selector(connect)];
}
[self logString:[NSString stringWithFormat:@"Connected all ejected drives"]];
} break;
case ACTIONEJECT: {
NSArray *matchingDrives = [[[self expanDrive] drives] filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:@"(isConnected == YES)"]];
if ( [matchingDrives count] >= 1 ) {
[self logString:[NSString stringWithFormat:@"Ejecting %d drives...", [matchingDrives count]]];
[matchingDrives makeObjectsPerformSelector:@selector(eject)];
}
[self logString:[NSString stringWithFormat:@"Ejected all connected drives"]];
} break;
default:
[self logString:[NSString stringWithFormat:@"ERROR: Action with id <%d> not recognised"]];
break;
}
}
/* perform cleanup when leaving the location or quitting the application
this method must be defined, even if it does not do anything.
*/
- (void)cleanupAction
{
}
/**
* In special cases, when bindings prove inadequate, you will need
* to provide controller code for your option interface. To do this, you should
* subclass OptionSheetController and override this method to return your subclass's
* class. The default implementation of this method returns the class
* OptionSheetController. The class returned by this method is instantiated and used
* as the nib's owner.
*/
- (Class)optionSheetControllerClass
{
return [ExpanDriveOptionSheetController class];
}
#pragma mark -
#pragma mark Added Methods
- (void)logString:(NSString *)log
{
NSLog(@"ExpanDrivePlugin: %@", log);
}
- (ScriptBridgeExpanDrive *)expanDrive
{
if (!expanDrive) {
expanDrive = [SBApplication applicationWithBundleIdentifier:APPLICATIONBUNDLEIDENTIFIER];
}
return expanDrive;
}
- (int)selectedAction
{
NSNumber *result = [[self options] objectForKey:ACTIONKEY];
return [result intValue];
}
- (int)selectedSingleOrMultiple
{
NSNumber *result = [[self options] objectForKey:PERFORMACTIONKEY];
return [result intValue];
}
- (NSString *)selectedDriveproperty
{
return [[self options] objectForKey:DRIVEPROPERTYKEY];
}
- (NSString *)selectedDriveContains
{
return [[self options] objectForKey:DRIVECONTAINSKEY];
}
- (NSString *)selectedActionTitle
{
NSMutableString *result = [NSMutableString string];
int action = [self selectedAction];
switch (action) {
case ACTIONCONNECT:
[result appendString:ACTIONCONNECTSTRING];
break;
case ACTIONEJECT:
[result appendString:ACTIONEJECTSTRING];
break;
default:
[result appendString:@"<Perform unknown action>"];
break;
}
int performAction = [self selectedSingleOrMultiple];
switch (performAction) {
case PERFORMSINGLEACTION:
//do nothing
break;
case PERFORMMULTIPLEACTION:
[result appendFormat:@" all drives whose %@ contains", [self selectedDriveproperty]];
break;
default:
break;
}
return result;
}
- (ScriptBridgeDrive *)selectedDrive
{
ScriptBridgeDrive *drive;
NSString *drivename = [[self options] objectForKey:DRIVENAMEKEY];
NSArray *matchingDrives = [[[self expanDrive] drives] filteredArrayUsingPredicate:
[NSPredicate predicateWithFormat:@"(drivename == %@)", drivename]];
int numberOfDrives = [matchingDrives count];
if (numberOfDrives == 1) {
[self logString:[NSString stringWithFormat:@"Drive with drivename [%@] found", drivename]];
drive = [matchingDrives objectAtIndex:0];
} else if (numberOfDrives < 1) {
[self logString:[NSString stringWithFormat:@"ERROR: No drives with drivename [%@] found", drivename]];
drive = nil;
} else {
[self logString:[NSString stringWithFormat:@"ERROR: Multiple drives with drivename [%@] found. Names need to be unique", drivename]];
drive = nil;
}
return drive;
}
@end