-
Notifications
You must be signed in to change notification settings - Fork 29
/
PrefsController.m
692 lines (519 loc) · 22.4 KB
/
PrefsController.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
//
// PrefsController.m
// Jiggler
//
// Created by Ben Haller on Wed Jul 23 2003.
// Copyright (c) 2003 Stick Software. All rights reserved.
//
#import "PrefsController.h"
#import "CocoaExtra.h"
#import "AppDelegate.h"
// User defaults keys
static NSString *JiggleTimeDefaultsKey = @"JiggleTime"; // version 1.3 and before: -1 = 20 secs, 0 = 40 secs, >=1 is number of minutes
static NSString *JiggleSecondsDefaultsKey = @"JiggleSeconds"; // version 1.4 and later: the actual number of seconds between jiggles (minimum 5)
static NSString *ShowIconWhenJigglingDefaultsKey = @"ShowIconWhenJiggling";
static NSString *JiggleOnlyWhenIdleDefaultsKey = @"JiggleOnlyWhenIdle";
static NSString *ZenJiggleDefaultsKey = @"ZenJiggle"; // version 1.7 and before, superseded by JiggleStyleDefaultsKey
static NSString *JiggleStyleDefaultsKey = @"JiggleStyle"; // version 1.8 and later: 0 is standard, 1 is Zen jiggle, 2 is click jiggle
static NSString *JiggleDistanceDefaultsKey = @"JiggleDistance";
static NSString *OnlyWithCPUUsageDefaultsKey = @"OnlyWithCPUUsage";
static NSString *CPUUsageThresholdDefaultsKey = @"CPUUsageThreshold";
static NSString *OnlyWithRemovableWritableDisksDefaultsKey = @"OnlyWithRemovableWritableDisks";
static NSString *OnlyWithITunesPlayingDefaultsKey = @"OnlyWithITunesPlaying";
static NSString *OnlyWithApplicationsNamedXDefaultsKey = @"OnlyWithApplicationsNamedX";
static NSString *OnlyWithIdentityDefaultsKey = @"OnlyWithIdentity";
static NSString *ApplicationNameComponentDefaultsKey = @"ApplicationNameComponent";
static NSString *NotWhenScreenLockedDefaultsKey = @"NotWhenScreenLocked";
static NSString *NotOnBatteryDefaultsKey = @"NotOnBattery";
static NSString *NotWithFrontAppsNamedXDefaultsKey = @"NotWithFrontAppsNamedX";
static NSString *FrontAppNameComponentDefaultsKey = @"FrontAppNameComponent";
@implementation PrefsController
static PrefsController *sharedPrefsController = nil;
+ (PrefsController *)sharedPrefsController
{
if (!sharedPrefsController)
[[[PrefsController alloc] init] autorelease];
return sharedPrefsController;
}
- (id)init
{
if (sharedPrefsController == nil)
{
if (self = [super init])
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// Set up our default values; should probably be in +initialize, but this gets called right away anyway...
[userDefaults registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:5], JiggleTimeDefaultsKey, // use the old key as a minimum time value
[NSNumber numberWithInt:-1], JiggleSecondsDefaultsKey, // use -1 as a flag value for "no value set for the new key"
@"YES", ShowIconWhenJigglingDefaultsKey,
@"YES", JiggleOnlyWhenIdleDefaultsKey,
@"NO", ZenJiggleDefaultsKey, // the default for the old key
[NSNumber numberWithInt:-1], JiggleStyleDefaultsKey, // use -1 as a flag value for "no value set for the new key"
[NSNumber numberWithFloat:2], JiggleDistanceDefaultsKey, // 0 was the old default in 1.7 and earlier, effectively
@"NO", OnlyWithCPUUsageDefaultsKey,
[NSNumber numberWithInt:20], CPUUsageThresholdDefaultsKey,
@"NO", OnlyWithRemovableWritableDisksDefaultsKey,
@"NO", OnlyWithITunesPlayingDefaultsKey,
@"NO", OnlyWithApplicationsNamedXDefaultsKey,
[NSNumber numberWithInt:0], OnlyWithIdentityDefaultsKey,
@"", ApplicationNameComponentDefaultsKey,
@"YES", NotWhenScreenLockedDefaultsKey,
@"NO", NotOnBatteryDefaultsKey,
@"NO", NotWithFrontAppsNamedXDefaultsKey,
@"", FrontAppNameComponentDefaultsKey,
[NSNumber numberWithBool:YES], @"NSAppSleepDisabled", // completely disable App Nap; we want to always be live, that's kind of the point
nil]];
// Read our de facto values into our caches
jiggleSeconds = (int)[userDefaults integerForKey:JiggleSecondsDefaultsKey];
if (jiggleSeconds < 0)
{
int jiggleTime; // old defaults key: -1 is 20 seconds, 0 is 40 seconds, positive integers are a number of minutes
jiggleTime = (int)[userDefaults integerForKey:JiggleTimeDefaultsKey];
if (jiggleTime == -1)
jiggleSeconds = 20;
else if (jiggleTime == 0)
jiggleSeconds = 40;
else
jiggleSeconds = jiggleTime * 60;
}
if (jiggleSeconds < 5)
jiggleSeconds = 5;
if (jiggleSeconds > 60 * 60 * 24)
jiggleSeconds = 60 * 60 * 24;
showJigglerIconWhenJiggling = [userDefaults boolForKey:ShowIconWhenJigglingDefaultsKey];
jiggleOnlyWhenIdle = [userDefaults boolForKey:JiggleOnlyWhenIdleDefaultsKey];
jiggleStyle = (int)[userDefaults integerForKey:JiggleStyleDefaultsKey];
if (jiggleStyle == -1)
jiggleStyle = ([userDefaults boolForKey:ZenJiggleDefaultsKey] ? 1 : 0);
if ((jiggleStyle < 0) || (jiggleStyle > 2)) jiggleStyle = 0;
jiggleDistance = [userDefaults floatForKey:JiggleDistanceDefaultsKey];
if (jiggleDistance < 0.0f)
jiggleDistance = 0.0f;
if (jiggleDistance > 20.0f)
jiggleDistance = 20.0f;
onlyWithCPUUsage = [userDefaults boolForKey:OnlyWithCPUUsageDefaultsKey];
cpuUsageThreshold = (int)[userDefaults integerForKey:CPUUsageThresholdDefaultsKey];
onlyWithRemovableWritableDisks = [userDefaults boolForKey:OnlyWithRemovableWritableDisksDefaultsKey];
onlyWithITunesPlaying = [userDefaults boolForKey:OnlyWithITunesPlayingDefaultsKey];
onlyWithApplicationsNamedX = [userDefaults boolForKey:OnlyWithApplicationsNamedXDefaultsKey];
onlyWithIdentityTag = (int)[userDefaults integerForKey:OnlyWithIdentityDefaultsKey];
applicationNameComponent = [[userDefaults stringForKey:ApplicationNameComponentDefaultsKey] retain];
[applicationNameComponents release];
applicationNameComponents = nil;
notWhenScreenLocked = [userDefaults boolForKey:NotWhenScreenLockedDefaultsKey];
notOnBattery = [userDefaults boolForKey:NotOnBatteryDefaultsKey];
notWithFrontAppsNamedX = [userDefaults boolForKey:NotWithFrontAppsNamedXDefaultsKey];
frontAppNameComponent = [[userDefaults stringForKey:FrontAppNameComponentDefaultsKey] retain];
[frontAppNameComponents release];
frontAppNameComponents = nil;
}
sharedPrefsController = [self retain];
}
else
{
[self dealloc];
}
return sharedPrefsController;
}
- (void)setJiggleTimeTextfieldString
{
if (jiggleSeconds < 60)
[jiggleTimeTextfield setStringValue:[NSString stringWithFormat:@"(Set to %d seconds)", jiggleSeconds]];
else if (jiggleSeconds == 60)
[jiggleTimeTextfield setStringValue:@"(Set to 1 minute)"];
else if (jiggleSeconds < 60 * 60)
[jiggleTimeTextfield setStringValue:[NSString stringWithFormat:@"(Set to %.1f minutes)", jiggleSeconds / 60.0]];
else if (jiggleSeconds == 60 * 60)
[jiggleTimeTextfield setStringValue:@"(Set to 1 hour)"];
else
[jiggleTimeTextfield setStringValue:[NSString stringWithFormat:@"(Set to %.1f hours)", jiggleSeconds / (60.0 * 60.0)]];
}
- (void)showWindow
{
if (!preferencesWindow)
{
[[NSBundle mainBundle] loadNibNamed:@"Preferences" owner:self topLevelObjects:NULL];
[preferencesWindow retain]; // we own this panel, so we retain it; could make it a retain property instead, whatever
// We want command-W to close us, but we have no File menu, so we use a transparent button to make it work
[invisibleCloseButton setTransparent:YES];
// Set up our launch on login checkbox, which is not backed by a pref kept by us
[launchOnLoginCheckbox setState:([self launchOnLogin] ? NSControlStateValueOn : NSControlStateValueOff)];
// Set the jiggle time slider. It has values from 0 to 5, with ticks at each integer, for 5 secs, 1 min, 5 min, 1 hour, 5 hours, and 24 hours.
// We treat each segment of that slider range as an independent linear scale, so it's a bit complicated...
if (jiggleSeconds < 60)
[jiggleTimeSlider setFloatValue:(jiggleSeconds - 5) / (60.0 - 5.0)]; // 5 seconds to 60 seconds
else if (jiggleSeconds < 60 * 5)
[jiggleTimeSlider setFloatValue:(jiggleSeconds - 60) / (60.0 * 5.0 - 60.0) + 1.0]; // 1 minute to 5 minutes
else if (jiggleSeconds < 60 * 60)
[jiggleTimeSlider setFloatValue:(jiggleSeconds - 60 * 5) / (60.0 * 60.0 - 60.0 * 5) + 2.0]; // 5 minutes to 1 hour
else if (jiggleSeconds < 60 * 60 * 5)
[jiggleTimeSlider setFloatValue:(jiggleSeconds - 60 * 60) / (60.0 * 60.0 * 5.0 - 60.0 * 60.0) + 3.0]; // 1 hour to 5 hours
else
[jiggleTimeSlider setFloatValue:(jiggleSeconds - 60 * 60 * 5.0) / (60.0 * 60.0 * 24.0 - 60.0 * 60.0 * 5.0) + 4.0]; // 5 hours to 24 hours
// Set other control states
[self setJiggleTimeTextfieldString];
[showJigglerIconWhenJigglingButton setState:showJigglerIconWhenJiggling];
[jiggleOnlyWhenIdleRadio selectCellWithTag:(jiggleOnlyWhenIdle ? 1 : 0)];
[jiggleStyleRadio selectCellWithTag:jiggleStyle];
[jiggleDistanceSlider setEnabled:(jiggleStyle == 0)];
[jiggleDistanceSlider setFloatValue:jiggleDistance];
[onlyWithCPUUsageCheckbox setState:onlyWithCPUUsage];
[cpuUsageSlider setEnabled:onlyWithCPUUsage];
[cpuUsageSlider setIntValue:cpuUsageThreshold];
[cpuUsageTextfield setStringValue:[NSString stringWithFormat:@"%d%%", cpuUsageThreshold]];
[onlyWithRemovableWritableDisksCheckbox setState:onlyWithRemovableWritableDisks];
[onlyWithITunesPlayingCheckbox setState:onlyWithITunesPlaying];
[onlyWithApplicationsNamedXCheckbox setState:(onlyWithApplicationsNamedX && [applicationNameComponent length])];
[onlyWithIdentityPopUp selectItemWithTag:onlyWithIdentityTag];
[onlyWithIdentityPopUp setEnabled:onlyWithApplicationsNamedX];
[applicationNameComponentTextfield setStringValue:applicationNameComponent];
[applicationNameComponentTextfield setEnabled:onlyWithApplicationsNamedX];
[notWhenScreenLockedCheckbox setState:notWhenScreenLocked];
[notOnBatteryCheckbox setState:notOnBattery];
[notWithFrontAppsNamedXCheckbox setState:(notWithFrontAppsNamedX && [frontAppNameComponent length])];
[frontAppsNameComponentTextfield setStringValue:frontAppNameComponent];
[frontAppsNameComponentTextfield setEnabled:notWithFrontAppsNamedX];
[preferencesWindow centerOnPrimaryScreen];
[preferencesWindow setReleasedWhenClosed:NO];
}
[NSApp activateIgnoringOtherApps:YES];
[preferencesWindow makeKeyAndOrderFront:nil];
[preferencesWindow makeFirstResponder:preferencesWindow];
}
- (void)closeWindow
{
[preferencesWindow close];
}
- (int)jiggleSeconds
{
return jiggleSeconds;
}
- (BOOL)showJigglerIconWhenJiggling
{
return showJigglerIconWhenJiggling;
}
- (BOOL)jiggleOnlyWhenIdle
{
return jiggleOnlyWhenIdle;
}
- (int)jiggleStyle
{
return jiggleStyle;
}
- (int)jiggleDistance
{
return (int)(jiggleDistance * jiggleDistance + 10);
}
- (BOOL)onlyWithCPUUsage
{
return onlyWithCPUUsage;
}
- (int)cpuUsageThreshold
{
return cpuUsageThreshold;
}
- (BOOL)onlyWithRemovableWritableDisks
{
return onlyWithRemovableWritableDisks;
}
- (BOOL)onlyWithITunesPlaying
{
return onlyWithITunesPlaying;
}
- (BOOL)onlyWithApplicationsNamedX
{
return onlyWithApplicationsNamedX;
}
- (int)onlyWithIdentityTag
{
return onlyWithIdentityTag;
}
- (NSArray *)applicationNameComponents
{
if (!applicationNameComponents && [applicationNameComponent length])
{
NSMutableArray *newAppNameComponents = [[NSMutableArray alloc] init];
NSArray *uncorrectedComponents = [applicationNameComponent componentsSeparatedByString:@","];
NSCharacterSet *trimSet = [NSCharacterSet characterSetWithCharactersInString:@" \t\""];
int i, c;
for (i = 0, c = (int)[uncorrectedComponents count]; i < c; ++i)
{
NSString *uncorrectedComponent = [uncorrectedComponents objectAtIndex:i];
NSString *correctedString = [uncorrectedComponent stringByTrimmingCharactersInSet:trimSet];
if ([correctedString length])
[newAppNameComponents addObject:correctedString];
}
applicationNameComponents = (NSArray *)newAppNameComponents;
}
return applicationNameComponents;
}
- (BOOL)notWhenScreenLocked
{
return notWhenScreenLocked;
}
- (BOOL)notOnBattery
{
return notOnBattery;
}
- (BOOL)notWithFrontAppsNamedX
{
return notWithFrontAppsNamedX;
}
- (NSArray *)frontAppNameComponents
{
if (!frontAppNameComponents && [frontAppNameComponent length])
{
NSMutableArray *newAppNameComponents = [[NSMutableArray alloc] init];
NSArray *uncorrectedComponents = [frontAppNameComponent componentsSeparatedByString:@","];
NSCharacterSet *trimSet = [NSCharacterSet characterSetWithCharactersInString:@" \t\""];
int i, c;
for (i = 0, c = (int)[uncorrectedComponents count]; i < c; ++i)
{
NSString *uncorrectedComponent = [uncorrectedComponents objectAtIndex:i];
NSString *correctedString = [uncorrectedComponent stringByTrimmingCharactersInSet:trimSet];
if ([correctedString length])
[newAppNameComponents addObject:correctedString];
}
frontAppNameComponents = (NSArray *)newAppNameComponents;
}
return frontAppNameComponents;
}
- (IBAction)jiggleTimeChanged:(id)sender
{
float newValue = [sender floatValue];
int newSeconds;
if (fabs(round(newValue) - newValue) < 0.006)
newValue = round(newValue);
if (newValue < 1.0)
newSeconds = round((newValue - 0.0) * 55.0 + 5.0); // 0 to 1 -> 5 seconds to 60 seconds
else if (newValue < 2.0)
newSeconds = round((newValue - 1.0) * 60.0 * 4.0 + 60.0); // 1 to 2 -> 1 minute to 5 minutes
else if (newValue < 3.0)
newSeconds = round((newValue - 2.0) * 60.0 * 55.0 + 60.0 * 5.0); // 2 to 3 -> 5 minutes to 60 minutes
else if (newValue < 4.0)
newSeconds = round((newValue - 3.0) * 60.0 * 60.0 * 4.0 + 60.0 * 60.0); // 3 to 4 -> 1 hour to 5 hours
else
newSeconds = round((newValue - 4.0) * 60.0 * 60.0 * 19.0 + 60.0 * 60.0 * 5.0); // 4 to 5 -> 5 hours to 24 hours
if (newSeconds != jiggleSeconds)
{
jiggleSeconds = newSeconds;
[[NSUserDefaults standardUserDefaults] setInteger:jiggleSeconds forKey:JiggleSecondsDefaultsKey];
[self setJiggleTimeTextfieldString];
}
}
- (IBAction)showJigglerIconWhenJigglingChanged:(id)sender
{
BOOL newState = [sender state];
if (newState != showJigglerIconWhenJiggling)
{
showJigglerIconWhenJiggling = newState;
[[NSUserDefaults standardUserDefaults] setBool:newState forKey:ShowIconWhenJigglingDefaultsKey];
}
}
- (IBAction)jiggleOnlyWhenIdleChanged:(id)sender
{
BOOL newState = ([[sender selectedCell] tag] ? YES : NO);
if (newState != jiggleOnlyWhenIdle)
{
jiggleOnlyWhenIdle = newState;
[[NSUserDefaults standardUserDefaults] setBool:newState forKey:JiggleOnlyWhenIdleDefaultsKey];
}
}
- (IBAction)jiggleStyleChanged:(id)sender
{
int newState = (int)[[sender selectedCell] tag];
if (newState != jiggleStyle)
{
jiggleStyle = newState;
[[NSUserDefaults standardUserDefaults] setInteger:newState forKey:JiggleStyleDefaultsKey];
[jiggleDistanceSlider setEnabled:(jiggleStyle == 0)];
}
}
- (IBAction)jiggleDistanceSliderChanged:(id)sender
{
float newValue = [jiggleDistanceSlider floatValue];
if (newValue != jiggleDistance)
{
jiggleDistance = newValue;
[[NSUserDefaults standardUserDefaults] setFloat:jiggleDistance forKey:JiggleDistanceDefaultsKey];
}
}
- (IBAction)onlyWithCPUUsageChanged:(id)sender
{
BOOL newState = [sender state];
if (newState != onlyWithCPUUsage)
{
onlyWithCPUUsage = newState;
[[NSUserDefaults standardUserDefaults] setBool:newState forKey:OnlyWithCPUUsageDefaultsKey];
[cpuUsageSlider setEnabled:onlyWithCPUUsage];
}
}
- (IBAction)cpuUsageSliderChanged:(id)sender
{
int newValue = [cpuUsageSlider intValue];
if (newValue != cpuUsageThreshold)
{
cpuUsageThreshold = newValue;
[[NSUserDefaults standardUserDefaults] setInteger:newValue forKey:CPUUsageThresholdDefaultsKey];
[cpuUsageTextfield setStringValue:[NSString stringWithFormat:@"%d%%", newValue]];
}
}
- (IBAction)onlyWithRemovableWritableDisksChanged:(id)sender
{
BOOL newState = [sender state];
if (newState != onlyWithRemovableWritableDisks)
{
onlyWithRemovableWritableDisks = newState;
[[NSUserDefaults standardUserDefaults] setBool:newState forKey:OnlyWithRemovableWritableDisksDefaultsKey];
}
}
- (IBAction)onlyWithITunesPlayingChanged:(id)sender
{
BOOL newState = [sender state];
if (newState != onlyWithITunesPlaying)
{
onlyWithITunesPlaying = newState;
[[NSUserDefaults standardUserDefaults] setBool:newState forKey:OnlyWithITunesPlayingDefaultsKey];
}
}
- (IBAction)onlyWithApplicationsNamedXChanged:(id)sender
{
BOOL newState = [sender state];
if (newState != onlyWithApplicationsNamedX)
{
onlyWithApplicationsNamedX = newState;
[[NSUserDefaults standardUserDefaults] setBool:newState forKey:OnlyWithApplicationsNamedXDefaultsKey];
[onlyWithIdentityPopUp setEnabled:onlyWithApplicationsNamedX];
[applicationNameComponentTextfield setEnabled:onlyWithApplicationsNamedX];
if (onlyWithApplicationsNamedX)
{
[applicationNameComponentTextfield selectText:nil];
[preferencesWindow makeFirstResponder:applicationNameComponentTextfield];
}
}
}
- (IBAction)onlyWithIdentityPopUpChanged:(id)sender
{
int newTag = (int)[[onlyWithIdentityPopUp selectedItem] tag];
if (newTag != onlyWithIdentityTag)
{
onlyWithIdentityTag = newTag;
[[NSUserDefaults standardUserDefaults] setInteger:newTag forKey:OnlyWithIdentityDefaultsKey];
}
}
- (IBAction)onlyWithFrontAppsNamedXChanged:(id)sender
{
BOOL newState = [sender state];
if (newState != notWithFrontAppsNamedX)
{
notWithFrontAppsNamedX = newState;
[[NSUserDefaults standardUserDefaults] setBool:newState forKey:NotWithFrontAppsNamedXDefaultsKey];
[frontAppsNameComponentTextfield setEnabled:notWithFrontAppsNamedX];
if (notWithFrontAppsNamedX)
{
[frontAppsNameComponentTextfield selectText:nil];
[preferencesWindow makeFirstResponder:frontAppsNameComponentTextfield];
}
}
}
- (void)controlTextDidChange:(NSNotification *)aNotification
{
id object = [aNotification object];
if (object == applicationNameComponentTextfield)
{
[applicationNameComponent release];
applicationNameComponent = [[applicationNameComponentTextfield stringValue] retain];
[applicationNameComponents release];
applicationNameComponents = nil;
[[NSUserDefaults standardUserDefaults] setObject:applicationNameComponent forKey:ApplicationNameComponentDefaultsKey];
}
if (object == frontAppsNameComponentTextfield)
{
[frontAppNameComponent release];
frontAppNameComponent = [[frontAppsNameComponentTextfield stringValue] retain];
[frontAppNameComponents release];
frontAppNameComponents = nil;
[[NSUserDefaults standardUserDefaults] setObject:frontAppNameComponent forKey:FrontAppNameComponentDefaultsKey];
}
}
- (IBAction)notWhenScreenLockedChanged:(id)sender
{
BOOL newState = [sender state];
if (newState != notWhenScreenLocked)
{
notWhenScreenLocked = newState;
[[NSUserDefaults standardUserDefaults] setBool:newState forKey:NotWhenScreenLockedDefaultsKey];
}
}
- (IBAction)notOnBatteryChanged:(id)sender
{
BOOL newState = [sender state];
if (newState != notOnBattery)
{
notOnBattery = newState;
[[NSUserDefaults standardUserDefaults] setBool:newState forKey:NotOnBatteryDefaultsKey];
}
}
#pragma Launch on Login
// This code courtesy of Catalin Stan at http://stackoverflow.com/questions/23625255/how-can-i-make-program-automatically-startup-on-login
// Note that it would not work with sandboxing, and would likely have problems on the Mac App Store.
- (BOOL)launchOnLogin
{
BOOL loginItemFound = FALSE;
LSSharedFileListRef loginItemsListRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginItemsListRef)
{
CFArrayRef snapshotRef = LSSharedFileListCopySnapshot(loginItemsListRef, NULL);
NSArray *loginItems = [NSMakeCollectable(snapshotRef) autorelease];
NSURL *bundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
for (id item in loginItems)
{
LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item;
CFURLRef itemURLRef = LSSharedFileListItemCopyResolvedURL(itemRef, 0, NULL);
NSURL *itemURL = (NSURL *)[(id)itemURLRef autorelease];
if ([itemURL isEqual:bundleURL])
{
loginItemFound = YES;
break;
}
}
CFRelease(loginItemsListRef);
}
return loginItemFound;
}
- (void)setLaunchOnLogin:(BOOL)launchOnLogin
{
LSSharedFileListRef loginItemsListRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginItemsListRef)
{
NSURL *bundleURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
if (launchOnLogin)
{
NSDictionary *properties = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:@"com.apple.loginitem.HideOnLaunch"];
LSSharedFileListItemRef itemRef = LSSharedFileListInsertItemURL(loginItemsListRef, kLSSharedFileListItemLast, NULL, NULL, (CFURLRef)bundleURL, (CFDictionaryRef)properties,NULL);
if (itemRef)
CFRelease(itemRef);
}
else
{
CFArrayRef snapshotRef = LSSharedFileListCopySnapshot(loginItemsListRef, NULL);
NSArray *loginItems = [(id)snapshotRef autorelease];
for (id item in loginItems)
{
LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item;
CFURLRef itemURLRef = LSSharedFileListItemCopyResolvedURL(itemRef, 0, NULL);
NSURL *itemURL = (NSURL *)[(id)itemURLRef autorelease];
if ([itemURL isEqual:bundleURL])
LSSharedFileListItemRemove(loginItemsListRef, itemRef);
}
}
CFRelease(loginItemsListRef);
}
}
- (IBAction)launchOnLoginChanged:(id)sender
{
BOOL newState = [sender state];
[self setLaunchOnLogin:newState];
}
@end