-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathController.m
565 lines (490 loc) · 17.3 KB
/
Controller.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
//
// Controller.m
// DelayedLauncher
//
// Created by Greg Slepak on 12/5/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import <Carbon/Carbon.h>
#import "Controller.h"
#import "TEApplication.h"
#import "TEIconFromFilepath.h"
#import "ImageAndProgressCell.h"
#import "CustomTableView.h"
#import "LoginItemsAE.h"
#import "Common.h"
#define MyDragType @"MyDragType"
#define OpenedBeforeKey @"OpenedBeforeKey"
#define LoginItemsCheckKey @"LoginItemsCheckKey"
#define ItemsKey @"ItemsKey"
#define DelayKey @"DelayKey"
#define PreferencesVersionKey @"PreferencesVersionKey"
#define FilePathKey @"FilePathKey"
#define IconKey @"IconKey"
#define HideKey @"HideKey"
#define QUIT_TIME 5
#define DEFAULT_DELAY 20.0
// TODO: add reveal in finder contextual menu to each item
OSStatus loginListContainsApp(NSString *appPath, NSMutableArray **copyCats, BOOL *answer)
{
NSArray *loginItems = nil;
OSStatus err;
int i;
FAIL_IF(!copyCats, err = kNSLNullListPtr; log_err("null list given"));
if ( answer ) *answer = NO;
if ( *copyCats == nil ) *copyCats = [NSMutableArray array];
DO_FAILABLE(err, LIAECopyLoginItems, (CFArrayRef *)&loginItems);
NSString *appName = [appPath lastPathComponent];
for (i=0; i < [loginItems count]; ++i)
{
NSDictionary *item = [loginItems objectAtIndex:i];
NSURL *itemURL = [item objectForKey:(NSString*)kLIAEURL];
NSString *itemPath = [itemURL path];
if ( [itemPath rangeOfString:appName].location != NSNotFound )
{
if ( [itemPath isEqualToString:appPath] == NO ) {
log_warn("Login Item %@ found but located in wrong place!", appPath);
[*copyCats addObject:itemPath];
}
else if ( answer ) {
*answer = YES; // it's in the list as it should be
}
}
}
fail_label:
[loginItems release];
return err;
}
OSStatus removeLoginItem(NSString *itemPath)
{
NSArray *loginItems = nil;
OSStatus err, i, deleted = nbpNotFound;
DO_FAILABLE(err, LIAECopyLoginItems, (CFArrayRef *)&loginItems);
for (i=0; i < [loginItems count]; ++i) {
if ( [itemPath isEqualToString:[[[loginItems objectAtIndex:i] objectForKey:(NSString*)kLIAEURL] path]] ) {
LIAERemove(i);
deleted = 0;
break;
}
}
fail_label:
[loginItems release];
return err || deleted;
}
@implementation Controller
- (id)init
{
if ( (self = [super init]) != nil ) {
// set user defaults
NSMutableDictionary *defaults = [NSMutableDictionary dictionary];
[defaults setObject:[NSNumber numberWithDouble:DEFAULT_DELAY] forKey:DelayKey];
[defaults setObject:[NSMutableArray array] forKey:ItemsKey];
NSUserDefaults *stdDefs = [NSUserDefaults standardUserDefaults];
[stdDefs registerDefaults:defaults];
itemList = [[NSMutableArray alloc] initWithArray:[stdDefs objectForKey:ItemsKey]];
if ( [stdDefs integerForKey:PreferencesVersionKey] == 1 )
{
log_debug("converting from version 1...");
//ENUMERATE(NSMutableDictionary *, item, [itemList objectEnumerator])
for ( unsigned i=0; i < [itemList count]; ++i )
{
// we do it this way because 10.4 doesn't allow treating NSDictionaries as NSMutableDictionaries
NSMutableDictionary *item = [NSMutableDictionary dictionaryWithDictionary:[itemList objectAtIndex:i]];
[item removeObjectForKey:IconKey];
if ( i == 0 )
[item setObject:[stdDefs objectForKey:DelayKey] forKey:DelayKey];
else
[item setObject:[NSNumber numberWithDouble:0] forKey:DelayKey];
[itemList replaceObjectAtIndex:i withObject:item];
}
log_debug("converted to version 2!");
}
[[NSUserDefaults standardUserDefaults] setInteger:2 forKey:PreferencesVersionKey];
// these are runtime values but are saved to the preferences... so we need to get rid of them
// we also need to observe the HideKey
for ( unsigned i=0; i < [itemList count]; ++i ) {
NSMutableDictionary *item = [NSMutableDictionary dictionaryWithDictionary:[itemList objectAtIndex:i]];
[item removeObjectForKey:@"launched"];
[item addObserver:self forKeyPath:HideKey options:0 context:NULL];
[itemList replaceObjectAtIndex:i withObject:item];
}
[self addObserver:self forKeyPath:@"countingDown" options:NSKeyValueObservingOptionNew context:NULL];
// on 10.4 this seems necessary. For some reason this works fine without it on 10.5 and 10.6.
[NSValueTransformer setValueTransformer:[[TEIconFromFilepath new] autorelease] forName:@"TEIconFromFilepath"];
}
return self;
}
extern char **environ;
- (void)awakeFromNib
{
[slider setContinuous:YES];
[delayTextField setHidden:YES];
[warningField setHidden:YES];
[playPauseButton setHidden:YES];
[contextualMenu setAutoenablesItems:NO];
[self updateDelayTextField];
[tableView registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, MyDragType, nil]];
//for (int i=0; environ[i]; ++i ) log_debug("%s", environ[i]);
}
- (void)applicationDidFinishLaunching:(NSNotification *)n
{
BOOL firstRun = ![[NSUserDefaults standardUserDefaults] boolForKey:OpenedBeforeKey];
if ( firstRun )
{
log_debug("first launch detected");
NSAlert *alert = [[NSAlert alloc] init];
alert.messageText = @"Welcome to DelayedLauncher!";
alert.informativeText = @"Drag items you want launched onto the table, they will be opened after the specified delays the next time this program runs.\n\nFor best results, add DelayedLauncher to your list of login items.";
[alert addButtonWithTitle:@"OK"];
[alert runModal];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:OpenedBeforeKey];
[window makeKeyAndOrderFront:self];
return;
}
// if ( ([TEApplication launchModifiers] & cmdKey) > 0 ) {
// [warningField setStringValue:@"launch stopped"];
// [window makeKeyAndOrderFront:self];
// return;
// }
if ( [itemList count] > 0 )
{
BOOL loginItemsChecked = [[NSUserDefaults standardUserDefaults] boolForKey:LoginItemsCheckKey];
if ( !loginItemsChecked )
{
NSString *ourPath = [[NSBundle mainBundle] bundlePath];
NSMutableArray *copyCats = nil;
BOOL inLoginItems;
OSStatus err = loginListContainsApp(ourPath, ©Cats, &inLoginItems);
if ( err == noErr )
{
ENUMERATE(NSString *, copyCat, [copyCats objectEnumerator]) {
log_info("removing copycat: %@", copyCat);
removeLoginItem(copyCat);
}
if ( !inLoginItems )
{
NSAlert *alert = [[NSAlert alloc] init];
alert.messageText = @"Do you want to add DelayedLauncher to Login Items?";
alert.informativeText = @"For best results, DelayedLauncher can add itself to the list of login items so that it automatically runs whenever you login to your account.\n\nAdd DelayedLauncher to the login items? You can always remove it later.";
[alert addButtonWithTitle:@"Yes"];
[alert addButtonWithTitle:@"No"];
if ( [alert runModal] == NSAlertFirstButtonReturn )
{
log_info("added self to login items");
LIAEAddURLAtEnd((CFURLRef)[NSURL fileURLWithPath:ourPath], NO);
}
}
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:LoginItemsCheckKey];
}
}
currentTimeLeft = [[self currentDelay] intValue];
[warningField setStringValue:@""];
[self setValue:NSYES forKey:@"countingDown"];
[[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timer:) userInfo:nil repeats:YES] fire];
}
}
// On Mac OS 10.5 and above, NSTableView and NSOutlineView have better contextual menu support. We now see a highlighted item for what was clicked on, and can access that item to do particular things (such as dynamically change the menu, as we do here!). Each of the contextual menus in the nib file have the delegate set to be the AppController instance. In menuNeedsUpdate, we dynamically update the menus based on the currently clicked upon row/column pair.
// The above comment is from the DragNDropOutlineView developer example
- (void)menuNeedsUpdate:(NSMenu *)menu
{
clickedRow = [tableView clickedRow];
// setting things enabled like this requires that setAutoenablesItems is set to NO (see -awakeFromNib)
if ( clickedRow == -1 )
[[menu itemAtIndex:0] setEnabled:NO];
else
[[menu itemAtIndex:0] setEnabled:YES];
}
- (IBAction)revealInFinder:sender;
{
if ( clickedRow != -1 ) {
NSDictionary *item = [itemList objectAtIndex:clickedRow];
[[NSWorkspace sharedWorkspace] selectFile:[item objectForKey:FilePathKey] inFileViewerRootedAtPath:@""];
}
}
- (void)timer:(NSTimer *)timer
{
if ( __likely(countingDown) )
{
if ( --currentTimeLeft <= 0 )
{
NSMutableDictionary *item;
if ( launched )
{
[NSApp terminate:self];
}
else if ( (item = [self currentItem]) != nil )
{
[item setObject:NSYES forKey:@"launched"];
NSString *path = [item objectForKey:FilePathKey];
BOOL hide = [[item objectForKey:HideKey] boolValue];
if ( [[NSFileManager defaultManager] fileExistsAtPath:path] )
{
NSBundle *bundle = [NSBundle bundleWithPath:path];
if ( bundle && [bundle bundleIdentifier] )
[self launchAppBundle:bundle hide:hide];
else
{
log_debug("opening file: %@", path);
[[NSWorkspace sharedWorkspace] openFile:path];
}
}
[self showProgressForCurrentRow];
currentTimeLeft = [[self currentDelay] intValue];
}
else
{
launched = YES;
currentTimeLeft = QUIT_TIME;
}
}
[warningField setStringValue:NSSTR_FMT("%s in: %d", (launched ? "quitting" : "launching"), currentTimeLeft)];
}
}
- (int)nextLaunchIndex
{
int idx = [self currentLaunchIndex];
return ( idx != -1 && ++idx < [itemList count] ) ? idx : -1;
}
- (int)currentLaunchIndex
{
int idx = -1;
unsigned count = [itemList count];
for (unsigned i=0; i<count; i++) {
NSDictionary *item = [itemList objectAtIndex:i];
if ( ![[item objectForKey:@"launched"] boolValue] ) {
idx = (int)i;
break;
}
}
return idx;
}
- (void)showProgressForCurrentRow
{
[tableView setShouldHighlight:countingDown rowAtIndex:[self currentLaunchIndex]];
[tableView setNeedsDisplay:YES];
}
- (void)launchAppBundle:(NSBundle*)bundle hide:(BOOL)hide
{
log_debug("opening bundle with path: %@", [bundle bundlePath]);
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
NSWorkspaceLaunchOptions options = NSWorkspaceLaunchDefault;
if ( hide ) options |= NSWorkspaceLaunchAndHide;
BOOL success = [workspace launchAppWithBundleIdentifier:[bundle bundleIdentifier]
options:options
additionalEventParamDescriptor:NULL
launchIdentifier:nil];
if ( !success ) {
log_warn("couldn't launch app w/workspace, trying alternate for: %@", [bundle bundlePath]);
if ( [workspace openFile:[bundle bundlePath]] == NO ) {
log_err("couldn't launch: %@", [bundle bundlePath]);
}
}
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if ( [keyPath isEqualToString:HideKey] ) {
[self saveToDisk];
}
else if ( [keyPath isEqualToString:@"countingDown"] )
{
if ( countingDown )
{
[playPauseButton setImage:[NSImage imageNamed:@"Pause"]];
[warningField setHidden:NO];
[playPauseButton setHidden:NO];
}
else
{
[playPauseButton setImage:[NSImage imageNamed:@"Play"]];
}
[self showProgressForCurrentRow];
}
}
- (IBAction)openPreferences:(id)sender
{
if ( ![window isVisible] ) [window center];
[window makeKeyAndOrderFront:self];
}
- (IBAction)removeSelectedItems:(id)sender
{
ENUMERID(obj, [[arrayController selectedObjects] objectEnumerator]) {
[obj removeObserver:self forKeyPath:HideKey];
}
[arrayController remove:sender];
[self saveToDisk];
}
- (IBAction)fire:(id)sender
{
[self updateDelayTextField];
if ( [[NSApp currentEvent] type] == NSEventTypeLeftMouseDown ) {
[delayTextField setHidden:NO];
if ( countingDown )
[self setValue:NSNO forKey:@"countingDown"];
} else if ( [[NSApp currentEvent] type] == NSEventTypeLeftMouseUp ) {
[delayTextField setHidden:YES];
[self saveToDisk];
}
}
- (IBAction)toggleCountdown:(id)sender
{
[self setValue:[NSNumber numberWithBool:!countingDown] forKey:@"countingDown"];
}
- (IBAction)launchNow:(id)sender
{
}
- (BOOL)validateUserInterfaceItem:(id <NSValidatedUserInterfaceItem>)theItem
{
SEL action = [theItem action];
if ( action == @selector(removeSelectedItems:) )
return [tableView numberOfSelectedRows] > 0;
return YES;
}
double tenToThe(int power) {
int x = 10, i;
if ( power == 0 ) return 1;
if ( power < 0 ) return pow(10, power);
for (i=1; i<power; ++i) x *= 10;
return x;
}
int decimalPlace(double x, int place) {
double big = floor(x * tenToThe(place));
double small = floor(x * tenToThe(place-1)) * 10;
return big - small;
}
- (void)updateDelayTextField
{
double delaySec = [slider doubleValue];
double delayMin = delaySec/60.0;
if ( delaySec < 0.5 )
[delayTextField setStringValue:@"Immediately"];
else if ( round(delaySec) == 1.0 )
[delayTextField setStringValue:@"1 second"];
else if ( delaySec < 59.5 )
[delayTextField setStringValue:NSSTR_FMT("%.0f seconds", delaySec)];
else if ( delaySec >= 59.5 ) {
int tenths = decimalPlace(delayMin, 1);
int hundredths = decimalPlace(delayMin, 2);
if ( (tenths == 0 && hundredths < 5) || (tenths == 9 && hundredths >= 5 ) )
[delayTextField setStringValue:NSSTR_FMT("%.0f minute%s", delayMin, delayMin < 1.05 ? "" : "s")];
else
[delayTextField setStringValue:NSSTR_FMT("%.1f minutes", delayMin)];
}
}
- (NSMutableDictionary*)currentItem
{
int idx = [self currentLaunchIndex];
return (idx < [itemList count] && idx != -1) ? [itemList objectAtIndex:idx] : nil;
}
- (NSNumber*)currentDelay
{
return [[self currentItem] objectForKey:DelayKey];
}
- (void)setItemList:(NSMutableArray*)aList
{
if ( aList == itemList ) return;
ASSIGN(itemList, aList);
}
- (NSMutableArray*)itemList
{
return itemList;
}
- (void)saveToDisk
{
[[NSUserDefaults standardUserDefaults] setObject:itemList forKey:ItemsKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (BOOL)addItemWithPath:(NSString*)path atIndex:(unsigned)idx
{
ENUMERATE(NSMutableDictionary *, dict, [itemList objectEnumerator]) {
if ( [[dict objectForKey:FilePathKey] isEqualToString:path] ) {
NSAlert *alert = [[NSAlert alloc] init];
alert.messageText = @"Duplicate";
alert.informativeText = [NSString stringWithFormat:@"'%@' is already in the list.", [path lastPathComponent]];
[alert addButtonWithTitle:@"OK"];
[alert runModal];
return NO;
}
};
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[NSNumber numberWithDouble:(idx == 0 ? DEFAULT_DELAY : 0)] forKey:DelayKey];
[dict setObject:path forKey:FilePathKey];
[dict setObject:[NSNumber numberWithBool:NO] forKey:HideKey];
[arrayController insertObject:dict atArrangedObjectIndex:idx];
[dict addObserver:self forKeyPath:HideKey options:0 context:NULL];
return YES;
}
ACC_COMBOP_M(BOOL, countingDown, CountingDown)
#pragma mark -
#pragma mark Table View Shit
- (BOOL)tableView:(NSTableView *)aTableView shouldSelectRow:(int)rowIndex
{
return !countingDown || ![[[itemList objectAtIndex:rowIndex] objectForKey:@"launched"] boolValue];
}
// begin dragging
- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard
{
// Copy the row numbers to the pasteboard.
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
[pboard declareTypes:[NSArray arrayWithObject:MyDragType] owner:self];
[pboard setData:data forType:MyDragType];
return YES;
}
// validates it
- (NSDragOperation)tableView:(NSTableView*)tv
validateDrop:(id <NSDraggingInfo>)info
proposedRow:(int)row
proposedDropOperation:(NSTableViewDropOperation)op
{
if ( op != NSTableViewDropAbove ) return NSDragOperationNone;
NSArray *types = [[info draggingPasteboard] types];
if ( [types containsObject:NSFilenamesPboardType] || [types containsObject:MyDragType] )
return NSDragOperationEvery;
return NSDragOperationNone;
}
// end dragging
- (BOOL)tableView:(NSTableView *)aTableView
acceptDrop:(id <NSDraggingInfo>)info
row:(int)row
dropOperation:(NSTableViewDropOperation)operation
{
NSPasteboard *pboard = [info draggingPasteboard];
if ( [[pboard types] containsObject:MyDragType] ) {
// dragging within table
NSData *rowData = [pboard dataForType:MyDragType];
NSIndexSet *rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];
if ( ![rowIndexes containsIndex:row] )
{
NSArray *objects = [itemList objectsAtIndexes:rowIndexes];
unsigned dstIdx = [rowIndexes firstIndex] < row ? row - [rowIndexes count] : row;
[itemList removeObjectsAtIndexes:rowIndexes];
//log_debug("Dragging ended on row: %d. IndexSet: %@. [itemList count] = %d. dstIdex = %d", row, rowIndexes, [itemList count], dstIdx);
[itemList insertObjects:objects atIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(dstIdx, [rowIndexes count])]];
[arrayController rearrangeObjects]; // update the controller
}
}
else
{
// otherwise we're being dragged onto from another location (probably Finder)
NSArray *paths = [pboard propertyListForType:NSFilenamesPboardType];
ENUMERATE(NSString *, path, [paths reverseObjectEnumerator]) {
[self addItemWithPath:path atIndex:row];
}
}
[self showProgressForCurrentRow];
[self saveToDisk];
return YES;
}
// NOTE: these two methods are required to keep 10.4 from bitching about
// NSTableView's datasource not supporting them
- (int)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [itemList count];
}
- (id)tableView:(NSTableView *)tv objectValueForTableColumn:(NSTableColumn *)c row:(int)r
{
return nil;
}
@end