-
Notifications
You must be signed in to change notification settings - Fork 283
/
QSActionProvider_EmbeddedProviders.m
800 lines (693 loc) · 29.8 KB
/
QSActionProvider_EmbeddedProviders.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
#import "NSPasteboard_BLTRExtensions.h"
#import "QSActionProvider_EmbeddedProviders.h"
#import "NSString+NDCarbonUtilities.h"
#import "QSObject.h"
#import "QSObject_FileHandling.h"
#import "NDAlias+AliasFile.h"
#import <Carbon/Carbon.h>
#import "QSController.h"
#import "QSRegistry.h"
#import "QSSimpleWebWindowController.h"
#import "QSLoginItemFunctions.h"
#import "QSFSBrowserMediator.h"
#import "QSNullObject.h"
#import "QSObject_PropertyList.h"
#import "QSObject_StringHandling.h"
#import "QSTaskController.h"
#import "QSAlertManager.h"
#import "QSTypes.h"
#import "NSPasteboard_BLTRExtensions.h"
#import "QSFileConflictPanel.h"
#import "QSProcessSource.h"
#import "QSResourceManager.h"
#import "NSObject+ReaperExtensions.h"
#import <Carbon/Carbon.h>
#import "QSTextProxy.h"
#import "QSLibrarian.h"
#include <Security/Authorization.h>
#include <Security/AuthorizationTags.h>
#import "NSURL_BLTRExtensions.h"
# define kURLOpenAction @"URLOpenAction"
# define kURLOpenActionInBackground @"URLOpenActionInBackground"
# define kURLOpenWithAction @"URLOpenWithAction"
# define kURLJSAction @"URLJSAction"
# define kURLEmailAction @"URLEmailAction"
#import "NSPasteboard_BLTRExtensions.h"
#import "QSLSTools.h"
#import "QSInterfaceController.h"
#import <AudioToolbox/AudioServices.h>
@implementation URLActions
- (NSString *)defaultWebClient {
NSURL *appURL = nil;
OSStatus err = LSGetApplicationForURL((CFURLRef) [NSURL URLWithString: @"http:"], kLSRolesAll, NULL, (CFURLRef *)&appURL);
if (err != noErr)
NSLog(@"error %ld", (long)err);
NSString *clientPath = [appURL path];
[appURL release];
return clientPath;
}
- (NSArray *)validActionsForDirectObject:(QSObject *)dObject indirectObject:(QSObject *)iObject {
NSString *urlString = [dObject objectForType:QSURLType];
// NSMutableArray *newActions = [NSMutableArray arrayWithCapacity:1];
if (urlString) {
if ([urlString hasPrefix:@"javascript:"]) return [NSArray arrayWithObject:kURLJSAction];
else if ([urlString hasPrefix:@"mailto:"]) return [NSArray arrayWithObject:kURLEmailAction];
}
/* [newActions addObject:kURLOpenAction];
[newActions addObject:kURLOpenWithAction];
return newActions; */
return [NSArray arrayWithObjects:kURLOpenAction, kURLOpenWithAction, kURLOpenActionInBackground, nil];
}
// Method to only show apps in the 3rd pane for the 'Open with...' action
- (NSArray *)validIndirectObjectsForAction:(NSString *)action directObject:(QSObject *)dObject{
// only for 'Open URL with...' action
if ([action isEqualToString:@"URLOpenWithAction"]) {
NSMutableArray *validIndirects = [NSMutableArray arrayWithCapacity:1];
NSMutableSet *set = [NSMutableSet set];
// Base the list of apps on the URL in dObject (1st object if multiple are selected)
NSURL *url = [NSURL URLWithString:[[dObject arrayForType:QSURLType] objectAtIndex:0]];
// If for some reason no URLs given (current web page proxy)
if(!url) {
url = [NSURL URLWithString:@"http://"];
}
// Get the default app for the url
NSURL *appURL = nil;
LSGetApplicationForURL((CFURLRef)url, kLSRolesAll, NULL, (CFURLRef *)&appURL);
// Set the default app to be 1st in the returned list
id preferred = [QSObject fileObjectWithPath:[appURL path]];
if (!preferred) {
preferred = [NSNull null];
}
[set addObjectsFromArray:[(NSArray *)LSCopyApplicationURLsForURL((CFURLRef)url, kLSRolesAll) autorelease]];
validIndirects = [[QSLibrarian sharedInstance] scoredArrayForString:nil inSet:[QSObject fileObjectsWithURLArray:[set allObjects]]];
return [NSArray arrayWithObjects:preferred, validIndirects, nil];
}
return nil;
}
- (QSObject *)doURLOpenAction:(QSObject *)dObject {
NSMutableArray *urlArray = [NSMutableArray array];
for (NSString *urlString in [dObject arrayForType:QSURLType]) {
// Escape characters (but not # or %)
NSURL *url = [NSURL URLWithString:[urlString URLEncoding]];
// replace QUERY_KEY *** with nothing if we're just opening the URL
if ([urlString rangeOfString:QUERY_KEY].location != NSNotFound) {
NSInteger pathLoc = [urlString rangeOfString:[url path]].location;
if (pathLoc != NSNotFound)
url = [NSURL URLWithString:[[urlString substringWithRange:NSMakeRange(0, pathLoc)] URLEncoding]];
}
url = [url URLByInjectingPasswordFromKeychain];
if (url) {
[urlArray addObject:url];
}
else {
NSLog(@"error with url: %@", urlString);
}
}
// TODO: Bring this back later
// if (![QSAction modifiersAreIgnored] && mOptionKeyIsDown) {
/* if (mOptionKeyIsDown) {
id cont = [[NSClassFromString(@"QSSimpleWebWindowController") alloc] initWithWindow:nil];
[(QSSimpleWebWindowController *)cont openURL:[urlArray lastObject]];
[[cont window] makeKeyAndOrderFront:nil];
} else {*/
[[NSWorkspace sharedWorkspace] openURLs:urlArray withAppBundleIdentifier:[dObject objectForMeta:@"QSPreferredApplication"] options:0 additionalEventParamDescriptor:nil launchIdentifiers:nil];
// }
return nil;
}
- (QSObject *)doURLOpenActionInBackground:(QSObject *)dObject {
NSMutableArray *urlArray = [NSMutableArray array];
for (NSString *urlString in [dObject arrayForType:QSURLType]) {
NSURL *url = [NSURL URLWithString:[urlString URLEncoding]];
if ([urlString rangeOfString:QUERY_KEY].location != NSNotFound) {
NSInteger pathLoc = [urlString rangeOfString:[url path]].location;
if (pathLoc != NSNotFound)
url = [NSURL URLWithString:[[urlString substringWithRange:NSMakeRange(0, pathLoc)] URLEncoding]];
}
url = [url URLByInjectingPasswordFromKeychain];
if (url) {
[urlArray addObject:url];
}
else {
NSLog(@"error with url: %@", urlString);
}
}
// TODO: Bring this back later
// if (![QSAction modifiersAreIgnored] && mOptionKeyIsDown) {
/* if (mOptionKeyIsDown) {
id cont = [[NSClassFromString(@"QSSimpleWebWindowController") alloc] initWithWindow:nil];
[(QSSimpleWebWindowController *)cont openURL:[urlArray lastObject]];
[[cont window] makeKeyAndOrderFront:nil];
} else {*/
[[NSWorkspace sharedWorkspace] openURLs:urlArray withAppBundleIdentifier:[dObject objectForMeta:@"QSPreferredApplication"] options:NSWorkspaceLaunchWithoutActivation additionalEventParamDescriptor:nil launchIdentifiers:nil];
// }
return nil;
}
- (QSObject *)doURLOpenAction:(QSObject *)dObject with:(QSObject *)iObject {
NSArray *splitObjects = [iObject splitObjects];
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
// Enumerate through list of files in dObject and apps in iObject
for(QSObject *individual in splitObjects) {
for (NSString *urlString in [dObject arrayForType:QSURLType]) {
if([individual isApplication]) {
NSURL *url = [[NSURL URLWithString:[urlString URLEncoding]] URLByInjectingPasswordFromKeychain];
NSString *ident = [[NSBundle bundleWithPath:[individual singleFilePath]] bundleIdentifier];
[ws openURLs:[NSArray arrayWithObject:url] withAppBundleIdentifier:ident
options:0
additionalEventParamDescriptor:nil
launchIdentifiers:nil];
}
// iObject isn't an app
else {
NSBeep();
}
}
}
return nil;
}
- (QSObject *)doURLJSAction:(QSObject *)dObject {
// NSURL *url = [NSURL URLWithString:[dObject primaryObject]];
[self performJavaScript:[[dObject objectForType:QSURLType] URLDecoding]];
return nil;
}
- (void)performJavaScript:(NSString *)jScript {
NSString *key = [[NSUserDefaults standardUserDefaults] stringForKey:@"QSWebBrowserMediators"];
if (!key) key = QSApplicationIdentifierForURL(@"javascript:");
if (!key) key = QSApplicationIdentifierForURL(@"http:");
id instance = [QSReg instanceForKey:key inTable:@"QSWebBrowserMediators"];
// NSLog(@"instance: %@ %@", instance, key);
if ([instance respondsToSelector:@selector(performJavaScript:)])
[instance performJavaScript:jScript];
}
@end
# define kDiskEjectAction @"DiskEjectAction"
# define kDiskForceEjectAction @"DiskForceEjectAction"
@implementation FSDiskActions
- (NSArray *)actions {
QSAction *action = [QSAction actionWithIdentifier:kDiskEjectAction];
[action setIcon:[QSResourceManager imageNamed:@"EjectMediaIcon"]];
[action setProvider:self];
return [NSArray arrayWithObject:action];
}
- (NSArray *)validActionsForDirectObject:(QSObject *)dObject indirectObject:(QSObject *)iObject {
NSArray *paths = [dObject arrayForType:QSFilePathType];
BOOL valid = NO;
if (paths)
{
valid = YES;
for (NSString *path in paths) {
if (![[path stringByStandardizingPath] hasPrefix:@"/Volumes/"]) {
valid = NO;
break;
}
}
}
if (valid)
return [NSArray arrayWithObject:kDiskEjectAction];
else {
return nil;
}
}
- (QSObject *)performAction:(QSAction *)action directObject:(QSBasicObject *)dObject indirectObject:(QSBasicObject *)iObject {
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
for (NSString *mountedVolume in [dObject arrayForType:QSFilePathType]) {
if ([[ws mountedLocalVolumePaths] containsObject:[mountedVolume stringByStandardizingPath]]) {
NSError *err = nil;
if (![ws unmountAndEjectDeviceAtURL:[NSURL fileURLWithPath:mountedVolume] error:&err]) {
NSLog(@"Error unmounting: %@\nTrying to use Finder (via Applescript)",err);
NSDictionary *errorDict = nil;
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:[NSString stringWithFormat:@"tell application \"Finder\" to eject disk \"%@\"", [[NSFileManager defaultManager] displayNameAtPath:mountedVolume]]];
[script executeAndReturnError:&errorDict];
[script release];
if (errorDict) {
NSBeep();
NSLog(@"Error: %@",errorDict);
}
}
}
}
return nil;
}
@end
@implementation FSActions
- (NSArray *)universalApps {
if (!universalApps) {
QSTaskController *qstc = [QSTaskController sharedInstance];
[qstc updateTask:@"Updating Application Database" status:@"Updating Applications" progress:-1];
universalApps = (NSArray *)LSCopyApplicationURLsForURL((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"wildcard" ofType:@"*"]], kLSRolesAll);
[qstc removeTask:@"Updating Application Database"];
}
[self performSelector:@selector(setUniversalApps:) withObject:nil afterDelay:10*MINUTES extend:YES];
return universalApps;
}
- (void)setUniversalApps:(NSArray *)anUniversalApps {
if (universalApps != anUniversalApps) {
[universalApps release];
universalApps = [anUniversalApps retain];
}
}
- (void)dealloc {
[universalApps release];
[super dealloc];
}
// This method validates the 3rd pane for the core plugin actions
- (NSArray *)validIndirectObjectsForAction:(NSString *)action directObject:(QSObject *)dObject {
// Only return an array if the dObject is a file
if(![dObject validPaths]) {
return nil;
}
NSMutableArray *validIndirects = [NSMutableArray arrayWithCapacity:1];
if ([action isEqualToString:kFileOpenWithAction]) {
NSURL *fileURL = nil;
// comma trick - get a list of apps based on the 1st selected file
fileURL = [NSURL fileURLWithPath:[[dObject validPaths] objectAtIndex:0]];
NSURL *appURL = nil;
if (fileURL) LSGetApplicationForURL((CFURLRef) fileURL, kLSRolesAll, NULL, (CFURLRef *)&appURL);
NSMutableSet *set = [NSMutableSet set];
[set addObjectsFromArray:[(NSArray *)LSCopyApplicationURLsForURL((CFURLRef)fileURL, kLSRolesAll) autorelease]];
[set addObjectsFromArray:[self universalApps]];
validIndirects = [[QSLibrarian sharedInstance] scoredArrayForString:nil inSet:[QSObject fileObjectsWithURLArray:[set allObjects]]];
[appURL release];
return [NSArray arrayWithObjects:[NSNull null], validIndirects, nil];
} else if ([action isEqualToString:kFileRenameAction]) {
// return a text object (empty text box) to rename a file
NSString *path = [dObject singleFilePath];
if (path)
return [NSArray arrayWithObject:[QSObject textProxyObjectWithDefaultValue:[path lastPathComponent]]];
} else if ([action isEqualToString:@"QSNewFolderAction"]) {
return [NSArray arrayWithObject:[QSObject textProxyObjectWithDefaultValue:@"untitled folder"]];
} else if ([action isEqualToString:kFileMoveToAction] || [action isEqualToString:kFileCopyToAction]) {
// We only want folders for the move to / copy to actions (can't move to anything else)
NSMutableArray *fileObjects = [[[QSLibrarian sharedInstance] arrayForType:QSFilePathType] mutableCopy];
BOOL isDirectory;
NSString *currentFolderPath = [[[[dObject splitObjects] lastObject] singleFilePath] stringByDeletingLastPathComponent];
// if it wasn't in the catalog, create it from scratch
if (currentFolderPath) {
QSObject *currentFolderObject = [QSObject fileObjectWithPath:currentFolderPath];
[fileObjects removeObject:currentFolderObject];
[fileObjects insertObject:currentFolderObject atIndex:0];
}
NSWorkspace *ws = [[NSWorkspace sharedWorkspace] retain];
NSFileManager *fm = [[NSFileManager alloc] init];
for(QSObject *thisObject in fileObjects) {
NSString *path = [thisObject singleFilePath];
if ([fm fileExistsAtPath:path isDirectory:&isDirectory]) {
if (isDirectory && ![ws isFilePackageAtPath:path])
[validIndirects addObject:thisObject];
}
}
[fileObjects release];
[ws release];
[fm release];
return validIndirects;
}
return nil;
}
- (NSArray *)validActionsForDirectObject:(QSObject *)dObject indirectObject:(QSObject *)iObject {
NSMutableArray *newActions = [NSMutableArray arrayWithObject:kFileGetPathAction];
if ([dObject validPaths]) {
[newActions addObject:kFileOpenAction];
[newActions addObject:kFileOpenWithAction];
[newActions addObject:kFileRevealAction];
[newActions addObject:kFileMakeLinkInAction];
[newActions addObject:kFileDeleteAction];
[newActions addObject:kFileToTrashAction];
[newActions addObject:kFileMoveToAction];
[newActions addObject:kFileCopyToAction];
[newActions addObject:kFileGetInfoAction];
// !!! Andre Berg 20091112: shouldn't the following also be added?
[newActions addObject:kFileAlwaysOpenWithAction];
}
if ([dObject validSingleFilePath])
[newActions addObject:kFileRenameAction];
return newActions;
}
- (BOOL)filesExist:(NSArray *)paths {
NSFileManager *manager = [NSFileManager defaultManager];
NSString *thisFile;
for(thisFile in paths)
if (![manager fileExistsAtPath:thisFile])
return NO;
return YES;
}
- (QSObject *)openFile:(QSObject *)dObject {
NSFileManager *manager = [NSFileManager defaultManager];
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
LSItemInfoRecord infoRec;
for(NSString *thisFile in [dObject validPaths]) {
LSCopyItemInfoForURL((CFURLRef) [NSURL fileURLWithPath:thisFile] , kLSRequestBasicFlagsOnly, &infoRec);
if (!(infoRec.flags & kLSItemInfoIsContainer) || (infoRec.flags & kLSItemInfoIsPackage) || ![mQSFSBrowser openFile:thisFile]) {
if (infoRec.flags & kLSItemInfoIsAliasFile) {
NSString *aliasFile = [manager resolveAliasAtPathWithUI:thisFile];
if (aliasFile && [manager fileExistsAtPath:aliasFile])
thisFile = aliasFile;
}
NSString *fileHandler = [dObject objectForMeta:@"QSPreferredApplication"];
if (fileHandler) {
#ifdef DEBUG
if (VERBOSE) NSLog(@"Using %@", fileHandler);
#endif
[ws openFile:thisFile withApplication:[ws absolutePathForAppBundleWithIdentifier:fileHandler]];
} else {
// if (![QSAction modifiersAreIgnored] && (GetCurrentKeyModifiers() & shiftKey)) { // Open in background
// NSLog(@"Launching in Background");
// [ws openFileInBackground:thisFile];
// } else {
[ws openFile:thisFile];
// }
}
}
}
return nil;
}
- (QSObject *)alwaysOpenFile:(QSObject *)dObject with:(QSObject *)iObject {
FSRef ref;
[[dObject singleFilePath] getFSRef:&ref];
CFStringRef type;
LSCopyItemAttribute(&ref, kLSRolesNone, kLSItemContentType, (CFTypeRef *)&type);
LSSetDefaultRoleHandlerForContentType(type, kLSRolesAll, (CFStringRef) [[NSBundle bundleWithPath:[iObject singleFilePath]] bundleIdentifier]);
CFRelease(type);
return nil;
}
// FileOpenWithAction
- (QSObject *)openFile:(QSObject *)dObject with:(QSObject *)iObject {
NSArray *splitObjects = [iObject splitObjects];
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
// Enumerate through list of files in dObject and apps in iObject
for(QSObject *individual in splitObjects) {
for(NSString *thisFile in [dObject validPaths]) {
// If there's only a single value in iObject
if([individual isApplication]) {
[ws openFile:thisFile withApplication:[individual singleFilePath]];
}
else {
NSBeep();
}
}
}
return nil;
}
- (QSObject *)makeFolderIn:(QSObject *)dObject named:(QSObject *)iObject {
NSString *theFolder = [dObject validSingleFilePath];
NSString *newPath = [theFolder stringByAppendingPathComponent:[iObject stringValue]];
[[NSFileManager defaultManager] createDirectoryAtPath:newPath withIntermediateDirectories:NO attributes:nil error:nil];
[[NSWorkspace sharedWorkspace] noteFileSystemChanged:theFolder];
return [QSObject fileObjectWithPath:newPath];
}
- (QSObject *)revealFile:(QSObject *)dObject {
// ***warning * should resolve aliases
for(NSString *thisFile in [dObject validPaths])
[mQSFSBrowser revealFile:thisFile];
return nil;
}
- (QSObject *)getInfo:(QSObject *)dObject {
[[QSReg getMediator:kQSFSBrowserMediators] getInfoForFiles:[dObject validPaths]];
return nil;
}
- (QSBasicObject *)deleteFile:(QSObject *)dObject {
NSArray *selection = [[dObject arrayForType:QSFilePathType] valueForKey:@"lastPathComponent"];
// ***warning * activate before showing
id QSIC = [[NSApp delegate] interfaceController];
[QSIC showMainWindow:nil];
[QSIC setHiding:YES];
NSInteger choice = QSRunCriticalAlertSheet([(NSWindowController *)QSIC window], @"Delete File", [NSString stringWithFormat:@"Are you sure you want to PERMANENTLY delete:\r %@?", [selection componentsJoinedByString:@", "]], @"Delete", @"Cancel", nil);
[QSIC setHiding:NO];
if (choice == 1) {
NSString *lastDeletedFile;
for(NSString *thisFile in [dObject arrayForType:QSFilePathType]) {
if ([[NSFileManager defaultManager] removeItemAtPath:thisFile error:nil]) {
[[NSWorkspace sharedWorkspace] noteFileSystemChanged:[thisFile stringByDeletingLastPathComponent]];
lastDeletedFile = thisFile;
} else {
NSLog(@"Could not delete file");
}
}
// get settings for playing sound
Boolean isSet;
CFIndex val = CFPreferencesGetAppIntegerValue(CFSTR("com.apple.sound.uiaudio.enabled"),
CFSTR("com.apple.systemsound"),
&isSet);
if (val == 1 || !isSet) {
// play trash sound
CFURLRef soundURL = (CFURLRef)[NSURL fileURLWithPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"dragToTrash" ofType:@"aif"]];
SystemSoundID soundId;
AudioServicesCreateSystemSoundID(soundURL, &soundId);
AudioServicesPlaySystemSound(soundId);
}
// return folder that contained the last file that was deleted
return [QSObject fileObjectWithPath:[lastDeletedFile stringByDeletingLastPathComponent]];;
}
// permanent delete was canceled, so leave files in first pane again
return nil;
}
- (QSBasicObject *)trashFile:(QSObject *)dObject {
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
NSString *lastDeletedFile;
for(NSString *thisFile in [dObject arrayForType:QSFilePathType]) {
[ws performFileOperation:NSWorkspaceRecycleOperation source:[thisFile stringByDeletingLastPathComponent] destination:@"" files:[NSArray arrayWithObject:[thisFile lastPathComponent]] tag:nil];
[ws noteFileSystemChanged:[thisFile stringByDeletingLastPathComponent]];
lastDeletedFile = thisFile;
}
// get settings for playing sound
Boolean isSet;
CFIndex val = CFPreferencesGetAppIntegerValue(CFSTR("com.apple.sound.uiaudio.enabled"),
CFSTR("com.apple.systemsound"),
&isSet);
if (val == 1 || !isSet) {
// play trash sound
CFURLRef soundURL = (CFURLRef)[NSURL fileURLWithPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"dragToTrash" ofType:@"aif"]];
SystemSoundID soundId;
AudioServicesCreateSystemSoundID(soundURL, &soundId);
AudioServicesPlaySystemSound(soundId);
}
// return folder that contained the last file that was deleted
return [QSObject fileObjectWithPath:[lastDeletedFile stringByDeletingLastPathComponent]];;
}
- (QSObject *)openItemAtLogin:(QSObject *)dObject {
for(NSString * path in [dObject arrayForType:QSFilePathType]) {
QSSetItemShouldLaunchAtLogin(path, YES, YES);
}
return nil;
}
- (QSObject *)doNotOpenItemAtLogin:(QSObject *)dObject {
for(NSString *path in [dObject arrayForType:QSFilePathType]) {
QSSetItemShouldLaunchAtLogin(path, NO, YES);
}
return nil;
}
- (QSObject *)renameFile:(QSObject *)dObject withName:(QSObject *)iObject {
NSString *path = [dObject singleFilePath];
NSString *container = [path stringByDeletingLastPathComponent];
NSString *newName = [iObject objectForType:QSTextType];
if ([newName rangeOfString:@":"].location != NSNotFound)
newName = nil;
// ***warning * check the filesystem
if (![[newName pathExtension] length] && [[path pathExtension] length])
newName = [newName stringByAppendingPathExtension:[path pathExtension]];
if (!newName) {
NSBeep();
return nil;
}
newName = [newName stringByReplacing:@"/" with:@":"];
NSString *destinationFile = [container stringByAppendingPathComponent:newName];
if ([[NSFileManager defaultManager] moveItemAtPath:path toPath:destinationFile error:nil])
[[NSWorkspace sharedWorkspace] noteFileSystemChanged:container];
else
[[NSAlert alertWithMessageText:@"error" defaultButton:nil alternateButton:nil otherButton:nil informativeTextWithFormat:@"Error renaming File: %@ to %@", path, destinationFile] runModal];
return [QSObject fileObjectWithPath:destinationFile];
}
- (QSObject *)duplicateFile:(QSObject *)dObject {
// FIXME: Implement
return nil;
}
- (QSObject *)moveFiles:(QSObject *)dObject toFolder:(QSObject *)iObject {return [self moveFiles:dObject toFolder:iObject shouldCopy:NO];}
- (QSObject *)copyFiles:(QSObject *)dObject toFolder:(QSObject *)iObject
{
if (dObject == iObject) {
NSLog(@"Can't copy file to same destination as original file!");
}
return [self moveFiles:dObject toFolder:iObject shouldCopy:YES];
}
- (QSObject *)moveFiles:(QSObject *)dObject toFolder:(QSObject *)iObject shouldCopy:(BOOL)copy {
NSString *destination = [iObject singleFilePath];
NSArray *filePaths = [dObject validPaths];
if (!filePaths)
return nil;
NSFileManager *manager = [NSFileManager defaultManager];
NSDictionary *conflicts = [manager conflictsForFiles:filePaths inDestination:destination];
NSArray *resultPaths = nil;
if (conflicts) {
NSMutableArray *otherFiles;
NSLog(@"Conflicts: %@", conflicts);
id panel = [QSFileConflictPanel conflictPanel];
[panel setConflictNames:[conflicts allValues]];
id QSIC = [[NSApp delegate] interfaceController];
[QSIC showMainWindow:nil];
[QSIC setHiding:YES];
QSFileConflictResolutionMethod copyMethod = [panel runModalAsSheetOnWindow:[QSIC window]];
[QSIC setHiding:NO];
switch (copyMethod) {
case QSCancelReplaceResolution:
return nil;
case QSReplaceFilesResolution: {
for (NSString *file in [conflicts allValues])
{
NSLog(@"%@", file);
if ([file hasPrefix:destination]) {
NSLog(@"File %@ already exists in %@", file, destination);
continue;
}
[manager removeItemAtPath:file error:nil];
}
break;
}
case QSDontReplaceFilesResolution:
otherFiles = [[filePaths mutableCopy] autorelease];
[otherFiles removeObjectsInArray:[conflicts allKeys]];
#ifdef DEBUG
NSLog(@"Only moving %@", otherFiles);
#endif
filePaths = otherFiles;
break;
case QSSmartReplaceFilesResolution: {
NSTask *rsync = [NSTask taskWithLaunchPath:@"/usr/bin/rsync" arguments:[[[NSArray arrayWithObject:@"-auzEq"] arrayByAddingObjectsFromArray:filePaths] arrayByAddingObject:destination]];
[rsync launch]; [rsync waitUntilExit];
return nil;
break;
}
}
}
if( [filePaths count] == 0 ) {
NSLog(@"No file left to move");
return nil;
} else {
resultPaths = (copy) ? [mQSFSBrowser copyFiles:filePaths toFolder:destination] : [mQSFSBrowser moveFiles:filePaths toFolder:destination];
if (resultPaths) {
if ([resultPaths count] <[filePaths count])
NSLog(@"Finder-based move may not return all paths");
return [QSObject fileObjectWithArray:resultPaths];
} else {
NSLog(@"Finder move failed");
}
if (!resultPaths) {
// if (DEBUG) NSLog(@"Using NSFileManager");
NSMutableArray *newPaths = [NSMutableArray arrayWithCapacity:[filePaths count]];
for(NSString *thisFile in filePaths) {
NSString *destinationFile = [destination stringByAppendingPathComponent:[thisFile lastPathComponent]];
if (copy && [[NSFileManager defaultManager] copyItemAtPath:thisFile toPath:destinationFile error:nil]) {
[newPaths addObject:destinationFile];
} else if (!copy && [[NSFileManager defaultManager] moveItemAtPath:thisFile toPath:destinationFile error:nil]) {
[[NSWorkspace sharedWorkspace] noteFileSystemChanged:[thisFile stringByDeletingLastPathComponent]];
[newPaths addObject:destinationFile];
} else {
[[NSAlert alertWithMessageText:@"Move Error" defaultButton:nil alternateButton:nil otherButton:nil informativeTextWithFormat:@"Error Moving File: %@ to %@", thisFile, destination] runModal];
}
}
[[NSWorkspace sharedWorkspace] noteFileSystemChanged:destination];
return [QSObject fileObjectWithArray:newPaths];
}
}
return nil;
}
- (QSObject *)makeAliasTo:(QSObject *)dObject inFolder:(QSObject *)iObject {
NSString *destination = [iObject singleFilePath];
NSString *destinationFile;
for(NSString *thisFile in [dObject arrayForType:QSFilePathType]) {
destinationFile = [destination stringByAppendingPathComponent:[thisFile lastPathComponent]];
if ([(NDAlias *)[NDAlias aliasWithPath:thisFile] writeToFile:destinationFile])
[[NSWorkspace sharedWorkspace] noteFileSystemChanged:destination];
}
return nil;
}
- (QSObject *)makeLinkTo:(QSObject *)dObject inFolder:(QSObject *)iObject {
NSString *destination = [iObject singleFilePath];
for(NSString *thisFile in [dObject arrayForType:QSFilePathType]) {
if ([[NSFileManager defaultManager] createSymbolicLinkAtPath:[destination stringByAppendingPathComponent:[thisFile lastPathComponent]] withDestinationPath:thisFile error:nil])
[[NSWorkspace sharedWorkspace] noteFileSystemChanged:destination];
}
return nil;
}
- (QSObject *)makeHardLinkTo:(QSObject *)dObject inFolder:(QSObject *)iObject {
NSString *destination = [iObject singleFilePath];
for(NSString *thisFile in [dObject arrayForType:QSFilePathType]) {
if ([[NSFileManager defaultManager] linkItemAtPath:thisFile toPath:[destination stringByAppendingPathComponent:[thisFile lastPathComponent]] error:nil])
[[NSWorkspace sharedWorkspace] noteFileSystemChanged:destination];
}
return nil;
}
- (QSObject *)getFilePaths:(QSObject *)dObject withTilde:(BOOL)withTilde
{
// get an array of paths from files in the first pane
NSArray *paths = nil;
if (withTilde)
{
paths = [[dObject arrayForType:QSFilePathType] arrayByPerformingSelector:@selector(stringByAbbreviatingWithTildeInPath)];
} else {
paths = [dObject arrayForType:QSFilePathType];
}
// the name/label should be a one-line string
QSObject *pathResult = [QSObject objectWithName:[paths componentsJoinedByString:@", "]];
// use something other than the path to prevent this from clobbering the existing file (if it's in the catalog)
[pathResult setIdentifier:@"GetPathActionResult"];
// store all paths separated by newlines
// allow it to be used as text (Large Type, Paste, etc.)
[pathResult setObject:[paths componentsJoinedByString:@"\n"] forType:QSTextType];
[pathResult setPrimaryType:QSTextType];
return pathResult;
}
- (QSObject *)getFilePaths:(QSObject *)dObject
{
return [self getFilePaths:dObject withTilde:YES];
}
- (QSObject *)getAbsoluteFilePaths:(QSObject *)dObject
{
return [self getFilePaths:dObject withTilde:NO];
}
- (QSObject *)getFileURLs:(QSObject *)dObject {
return [QSObject objectWithString:[[NSURL performSelector:@selector(fileURLWithPath:) onObjectsInArray:[dObject arrayForType:QSFilePathType] returnValues:YES] componentsJoinedByString:@"\n"]];
}
- (QSObject *)getFileLocations:(QSObject *)dObject {
return [QSObject objectWithString:[[[dObject arrayForType:QSFilePathType] arrayByPerformingSelector:@selector(fileSystemPathHFSStyle)] componentsJoinedByString:@"\n"]];
}
@end
# define kAppLaunchAction @"AppLaunchAction"
# define kAppRootLaunchAction @"AppRootLaunchAction"
# define kAppLaunchAgainAction @"AppLaunchAgainAction"
@implementation AppActions
- (NSArray *)validActionsForDirectObject:(QSObject *)dObject indirectObject:(QSObject *)iObject {
/* NSMutableArray *newActions = [NSMutableArray arrayWithCapacity:1];
if (![dObject objectForType:QSProcessType])
[newActions addObject:kAppLaunchAction];
return newActions;*/
return [dObject objectForType:QSProcessType] ? nil : [NSArray arrayWithObject:kAppLaunchAction];
}
@end
# define kPasteboardPasteAction @"PasteboardPasteAction"
@implementation ClipboardActions
- (QSObject *)copyObject:(QSObject *)dObject {
[dObject putOnPasteboard:[NSPasteboard generalPasteboard]];
return nil;
}
- (QSObject *)pasteObject:(QSObject *)dObject {
[self pasteObject:dObject asPlainText:NO];
return nil;
}
- (QSObject *)pasteObjectAsPlainText:(QSObject *)dObject {
[self pasteObject:dObject asPlainText:YES];
return nil;
}
- (QSObject *)pasteObject:(QSObject *)dObject asPlainText:(BOOL)plainText {
BOOL success;
if(plainText) {
success = [dObject putOnPasteboardAsPlainTextOnly:[NSPasteboard generalPasteboard]];
}
else {
success = [dObject putOnPasteboard:[NSPasteboard generalPasteboard]];
}
if(success) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"WindowsShouldHide" object:self];
[[NSApp keyWindow] orderOut:self];
QSForcePaste();
} else NSBeep();
return nil;
}
@end