-
Notifications
You must be signed in to change notification settings - Fork 39
/
PrefsController.m
542 lines (439 loc) · 17.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
#import "PrefsController.h"
#import "Prefs.h"
#import "AlarmTasks.h"
#import "AppleRemote.h"
#import <Sparkle/Sparkle.h>
@interface PrefsController (PrivateAPI)
- (BOOL)isLoginItem;
- (void)switchViews:(NSToolbarItem *)item;
- (void)addLoginItem;
- (void)deleteLoginItem;
@end
@implementation PrefsController
- (void)awakeFromNib
{
// Initialize items dictionary
// This will hold all the toolbar items
items = [[NSMutableDictionary alloc] init];
// Create all toolbar items and add them to our items dictionary
NSToolbarItem *item1 = [[[NSToolbarItem alloc] initWithItemIdentifier:@"General"] autorelease];
[item1 setLabel:NSLocalizedString(@"General", @"Preference Pane Option")];
[item1 setImage:[NSImage imageNamed:@"General.png"]];
[item1 setTarget:self];
[item1 setAction:@selector(switchViews:)];
NSToolbarItem *item2 = [[[NSToolbarItem alloc] initWithItemIdentifier:@"Easy Wake"] autorelease];
[item2 setLabel:NSLocalizedString(@"Easy Wake", @"Preference Pane Option")];
[item2 setImage:[NSImage imageNamed:@"Sound.png"]];
[item2 setTarget:self];
[item2 setAction:@selector(switchViews:)];
NSToolbarItem *item3 = [[[NSToolbarItem alloc] initWithItemIdentifier:@"Advanced"] autorelease];
[item3 setLabel:NSLocalizedString(@"Advanced", @"Preference Pane Option")];
[item3 setImage:[NSImage imageNamed:@"Advanced.png"]];
[item3 setTarget:self];
[item3 setAction:@selector(switchViews:)];
NSToolbarItem *item4 = [[[NSToolbarItem alloc] initWithItemIdentifier:@"Software Update"] autorelease];
[item4 setLabel:NSLocalizedString(@"Software Update", @"Preference Pane Option")];
[item4 setImage:[NSImage imageNamed:@"SoftwareUpdate.png"]];
[item4 setTarget:self];
[item4 setAction:@selector(switchViews:)];
[items setObject:item1 forKey:[item1 itemIdentifier]];
[items setObject:item2 forKey:[item2 itemIdentifier]];
[items setObject:item3 forKey:[item3 itemIdentifier]];
[items setObject:item4 forKey:[item4 itemIdentifier]];
// Any other items you want to add, do so here.
// After you are done, just do all the toolbar stuff.
toolbar = [[[NSToolbar alloc] initWithIdentifier:@"PreferencePanes"] autorelease];
[toolbar setDelegate:self];
[toolbar setAllowsUserCustomization:NO];
[toolbar setAutosavesConfiguration:NO];
[window setToolbar:toolbar];
[window setShowsToolbarButton:NO];
// Now setup all the controls
// Alarms
[coloredIconsButton setState:([Prefs useColoredIcons] ? NSOnState : NSOffState)];
[prefVolumeSlider setIntValue:([Prefs prefVolume] * 100)];
[self setPrefVolume:prefVolumeSlider];
[snoozeDurationSlider setIntValue:[Prefs snoozeDuration]];
[self setSnoozeDuration:snoozeDurationSlider];
[killDurationSlider setIntValue:[Prefs killDuration]];
[self setKillDuration:killDurationSlider];
// Easy wake
[easyWakeDefaultButton setState:([Prefs useEasyWakeByDefault] ? NSOnState : NSOffState)];
[minVolumeSlider setIntValue:([Prefs minVolume] * 100)];
[self setMinVolume:minVolumeSlider];
[maxVolumeSlider setIntValue:([Prefs maxVolume] * 100)];
[self setMaxVolume:maxVolumeSlider];
[easyWakeDurationSlider setIntValue:[Prefs easyWakeDuration]];
[self setEasyWakeDuration:easyWakeDurationSlider];
// Advanced
[wakeFromSleepButton setState:([Prefs wakeFromSleep] ? NSOnState: NSOffState)];
[deauthenticateButton setEnabled:[Prefs wakeFromSleep]];
[keyboardType selectCellAtRow:([Prefs anyKeyStops] ? 0 : 1) column:0];
[appleRemoteButton setState:([Prefs supportAppleRemote] ? NSOnState : NSOffState)];
[appleRemoteButton setEnabled:[[AppleRemote sharedRemote] isRemoteAvailable]];
[loginButton setState:([Prefs launchAtLogin] ? NSOnState: NSOffState)];
// Software Update
int updateInterval = [[NSUserDefaults standardUserDefaults] integerForKey:SUScheduledCheckIntervalKey];
[checkForUpdatesButton setState:(updateInterval > 0) ? NSOnState : NSOffState];
if(updateInterval == 0) {
// Select default item "Weekly"
[updateIntervalPopup setEnabled:NO];
[updateIntervalPopup selectItemAtIndex:1];
}
else if(updateInterval == 86400) {
[updateIntervalPopup selectItemAtIndex:0];
}
else if(updateInterval == 604800) {
[updateIntervalPopup selectItemAtIndex:1];
}
else if(updateInterval == 2592000) {
[updateIntervalPopup selectItemAtIndex:2];
}
else {
// The updateInterval has been corrupted - reset to zero
updateInterval = 0;
[[NSUserDefaults standardUserDefaults] setInteger:updateInterval forKey:SUScheduledCheckIntervalKey];
[updateIntervalPopup setEnabled:NO];
[updateIntervalPopup selectItemAtIndex:1];
}
// Switch to the default view
[self switchViews:nil];
// Don't center the window til after we've switched the view, or else it will center that small window stub
[window center];
// Check that wake from sleep is possible if "wake from sleep" option is selected
if([Prefs wakeFromSleep] && ![AlarmTasks isAuthenticated])
{
// Preferences do not match system! Bring to users attention
// Deslect the wake from sleep option
[wakeFromSleepButton setState:NSOffState];
// Disable deauthenticate button and change preference
[self toggleWakeFromSleep:wakeFromSleepButton];
// Display the prefs window
[window makeKeyAndOrderFront:nil];
// Bring application to the front
[NSApp activateIgnoringOtherApps:YES];
// Display the information window
NSString *title = NSLocalizedString(@"Reauthentication Required", @"Dialog Title");
NSString *message = NSLocalizedString(@"Internal components of the application have changed. Reauthentication is required to wake the computer from sleep.", @"Dialog Message");
NSString *okButton = NSLocalizedString(@"OK", @"Dialog Button");
NSBeginAlertSheet(title, okButton, nil, nil, window, self, NULL, NULL, nil, message);
// Also, this means that the user is upgrading their app
// So turn off isFirstRun because they don't need the welcome message
[Prefs setIsFirstRun:NO];
}
// Check that loginButton state matches loginItem
if([self isLoginItem])
{
if(![Prefs launchAtLogin])
{
// User manually added login item
// Check box, but do not set preference
[loginButton setState:NSOnState];
}
}
else
{
if([Prefs launchAtLogin])
{
// User manually removed login item
// Preferences do not match system! Bring to users attention
// Deselect login button
[loginButton setState:NSOffState];
// Change preference
[self toggleLogin:loginButton];
// Display the prefs window
[window makeKeyAndOrderFront:nil];
// Bring application to the front
[NSApp activateIgnoringOtherApps:YES];
}
}
}
- (BOOL)isLoginItem
{
/* Execute the following AppleScript command:
tell application "System Events"
if "Alarm Clock" is in (name of every login item) then
return yes
else
return no
end if
end tell
*/
NSMutableString *command = [NSMutableString string];
[command appendString:@"tell application \"System Events\" \n"];
[command appendString:@"if \"Alarm Clock\" is in (name of every login item) then \n"];
[command appendString:@"return yes \n"];
[command appendString:@"else \n"];
[command appendString:@"return no \n"];
[command appendString:@"end if \n"];
[command appendString:@"end tell"];
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:command];
NSAppleEventDescriptor *ae = [script executeAndReturnError:nil];
[script autorelease];
return [[ae stringValue] hasPrefix:@"yes"];
}
/**
* This method is called everytime a toolbar item is clicked.
* If item is nil, switch to the default toolbar item ("General")
**/
- (void)switchViews:(NSToolbarItem *)item
{
NSString *sender;
if(item == nil) {
// Set the pane to the default view
sender = @"Advanced";
[toolbar setSelectedItemIdentifier:sender];
// And set the item to be toolbar item for this view
item = [items objectForKey:sender];
}
else {
sender = [item itemIdentifier];
}
// Make a temp pointer.
NSView *prefsView = nil;
// Set the title to the name of the Preference Item.
[window setTitle:[item label]];
if([sender isEqualToString:@"General"]) {
prefsView = generalView;
}
else if([sender isEqualToString:@"Easy Wake"]) {
prefsView = easyWakeView;
}
else if([sender isEqualToString:@"Advanced"]) {
prefsView = advancedView;
}
else if([sender isEqualToString:@"Software Update"]) {
prefsView = softwareUpdateView;
}
// To stop flicker, we make a temp blank view.
NSView *tempView = [[NSView alloc] initWithFrame:[[window contentView] frame]];
[window setContentView:tempView];
[tempView release];
// Mojo to get the right frame for the new window.
NSRect newFrame = [window frame];
newFrame.size.height = [prefsView frame].size.height + ([window frame].size.height - [[window contentView] frame].size.height);
newFrame.size.width = [prefsView frame].size.width;
newFrame.origin.y += ([[window contentView] frame].size.height - [prefsView frame].size.height);
// Set the frame to newFrame and animate it. (change animate:YES to animate:NO if you don't want this)
[window setFrame:newFrame display:YES animate:YES];
// Set the main content view to the new view we have picked through the if structure above.
[window setContentView:prefsView];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Toolbar delegate methods:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
{
return [items objectForKey:itemIdentifier];
}
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)theToolbar
{
return [self toolbarDefaultItemIdentifiers:theToolbar];
}
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)theToolbar
{
// Make sure we arrange the identifiers in the correct order
return [NSArray arrayWithObjects:@"General", @"Easy Wake", @"Advanced", @"Software Update", nil];
}
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
{
// Make all of them selectable. This puts that little grey outline thing around an item when you select it.
return [items allKeys];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark General Options:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (IBAction)toggleColoredIcons:(id)sender
{
[Prefs setUseColoredIcons:[sender state]];
// Post notification for changed alarm
// This is to allow the menu to properly change it's icon
[[NSNotificationCenter defaultCenter] postNotificationName:@"AlarmChanged" object:self];
}
- (IBAction)setPrefVolume:(id)sender
{
NSString *format = NSLocalizedString(@"%i%%", @"Label in Preferences panel");
[prefVolumeLabel setStringValue:[NSString stringWithFormat:format, [sender intValue]]];
float value = [sender intValue] / 100.0;
[Prefs setPrefVolume:value];
}
- (IBAction)setSnoozeDuration:(id)sender
{
NSString *format = NSLocalizedString(@"%i minutes", @"Label in Preferences panel");
[snoozeDurationLabel setStringValue:[NSString stringWithFormat:format, [sender intValue]]];
[Prefs setSnoozeDuration:[sender intValue]];
}
- (IBAction)setKillDuration:(id)sender
{
NSString *format = NSLocalizedString(@"%i minutes", @"Label in Preferences panel");
[killDurationLabel setStringValue:[NSString stringWithFormat:format, [sender intValue]]];
[Prefs setKillDuration:[sender intValue]];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Easy Wake Options:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (IBAction)toggleEasyWakeDefault:(id)sender
{
[Prefs setUseEasyWakeByDefault:[sender state]];
}
- (IBAction)setMinVolume:(id)sender
{
NSString *format = NSLocalizedString(@"%i%%", @"Label in Preferences panel");
[minVolumeLabel setStringValue:[NSString stringWithFormat:format, [sender intValue]]];
float value = [sender intValue] / 100.0;
[Prefs setMinVolume:value];
if([Prefs minVolume] > [Prefs maxVolume])
{
[maxVolumeSlider setIntValue:[sender intValue]];
[self setMaxVolume:maxVolumeSlider];
}
}
- (IBAction)setMaxVolume:(id)sender
{
NSString *format = NSLocalizedString(@"%i%%", @"Label in Preferences panel");
[maxVolumeLabel setStringValue:[NSString stringWithFormat:format, [sender intValue]]];
float value = [sender intValue] / 100.0;
[Prefs setMaxVolume:value];
if([Prefs maxVolume] < [Prefs minVolume])
{
[minVolumeSlider setIntValue:[sender intValue]];
[self setMinVolume:minVolumeSlider];
}
}
- (IBAction)setEasyWakeDuration:(id)sender
{
NSString *format = NSLocalizedString(@"%i minutes", @"Label in Preferences panel");
[easyWakeDurationLabel setStringValue:[NSString stringWithFormat:format, [sender intValue]]];
[Prefs setEasyWakeDuration:[sender intValue]];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Advanced Options:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (IBAction)toggleWakeFromSleep:(id)sender
{
BOOL flag = [sender state];
if(flag)
{
if([AlarmTasks authenticate])
{
[Prefs setWakeFromSleep:YES];
[deauthenticateButton setEnabled:YES];
}
else
{
[sender setState:NO];
}
}
else
{
[Prefs setWakeFromSleep:NO];
[deauthenticateButton setEnabled:NO];
}
// For some reason, the authentication dialog doesn't return focus to our application
// Bring application and window back to the front
[NSApp activateIgnoringOtherApps:YES];
[window makeKeyAndOrderFront:self];
// Post notification for changed alarm
// This lets the menu know to change it's icon, based on wake from sleep status
[[NSNotificationCenter defaultCenter] postNotificationName:@"AlarmChanged" object:self];
}
- (IBAction)deauthenticate:(id)sender
{
if([AlarmTasks deauthenticate])
{
[Prefs setWakeFromSleep:NO];
[wakeFromSleepButton setState:NSOffState];
[deauthenticateButton setEnabled:NO];
}
// For some reason, the authentication dialog doesn't return focus to our application
// Bring application back to the front
[NSApp activateIgnoringOtherApps:YES];
[window makeKeyAndOrderFront:self];
// Post notification for changed alarm
// This lets the menu know to change it's icon, based on wake from sleep status
[[NSNotificationCenter defaultCenter] postNotificationName:@"AlarmChanged" object:self];
}
- (IBAction)toggleKeyboard:(id)sender
{
[Prefs setAnyKeyStops:([sender selectedRow] == 0)];
}
- (IBAction)toggleAppleRemote:(id)sender
{
[Prefs setSupportAppleRemote:[sender state]];
}
- (IBAction)toggleLogin:(id)sender
{
if([sender state])
{
[Prefs setLaunchAtLogin:YES];
[self addLoginItem];
}
else
{
[Prefs setLaunchAtLogin:NO];
[self deleteLoginItem];
}
}
- (void)addLoginItem
{
/* Execute the following AppleScript command:
set app_path to path to me
tell application "System Events"
if "Alarm Clock" is not in (name of every login item) then
make login item at end with properties {hidden:false, path:app_path}
end if
end tell
*/
NSMutableString *command = [NSMutableString string];
[command appendString:@"set app_path to path to me \n"];
[command appendString:@"tell application \"System Events\" \n"];
[command appendString:@"if \"Alarm Clock\" is not in (name of every login item) then \n"];
[command appendString:@"make login item at end with properties {hidden:false, path:app_path} \n"];
[command appendString:@"end if \n"];
[command appendString:@"end tell"];
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:command];
[script executeAndReturnError:nil];
[script release];
}
- (void)deleteLoginItem
{
/* Execute the following AppleScript command:
tell application "System Events"
if "Script Editor" is in (name of every login item) then
delete (every login item whose name is "Script Editor")
end if
end tell
*/
NSMutableString *command = [NSMutableString string];
[command appendString:@"tell application \"System Events\" \n"];
[command appendString:@"if \"Alarm Clock\" is in (name of every login item) then \n"];
[command appendString:@"delete (every login item whose name is \"Alarm Clock\") \n"];
[command appendString:@"end if \n"];
[command appendString:@"end tell"];
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:command];
[script executeAndReturnError:nil];
[script release];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark Software Update Options:
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (IBAction)toggleCheckForUpdates:(id)sender
{
if([sender state] == NSOffState)
{
[[NSUserDefaults standardUserDefaults] setInteger:0 forKey:SUScheduledCheckIntervalKey];
[updateIntervalPopup setEnabled:NO];
}
else
{
int updateInterval = [updateIntervalPopup selectedTag];
[[NSUserDefaults standardUserDefaults] setInteger:updateInterval forKey:SUScheduledCheckIntervalKey];
[updateIntervalPopup setEnabled:YES];
}
}
- (IBAction)setUpdateInterval:(id)sender
{
int updateInterval = [sender selectedTag];
[[NSUserDefaults standardUserDefaults] setInteger:updateInterval forKey:SUScheduledCheckIntervalKey];
}
@end