[21] | 1 | //
|
---|
| 2 | // PSAlarmSetController.m
|
---|
| 3 | // Pester
|
---|
| 4 | //
|
---|
| 5 | // Created by Nicholas Riley on Tue Oct 08 2002.
|
---|
| 6 | // Copyright (c) 2002 Nicholas Riley. All rights reserved.
|
---|
| 7 | //
|
---|
| 8 |
|
---|
| 9 | #import "PSAlarmSetController.h"
|
---|
[34] | 10 | #import "PSAlarmAlertController.h"
|
---|
[53] | 11 | #import "PSPowerManager.h"
|
---|
[21] | 12 | #import "NJRDateFormatter.h"
|
---|
[34] | 13 | #import "NJRFSObjectSelector.h"
|
---|
[53] | 14 | #import "NJRIntervalField.h"
|
---|
[39] | 15 | #import "NJRQTMediaPopUpButton.h"
|
---|
[34] | 16 | #import "NJRVoicePopUpButton.h"
|
---|
[53] | 17 | #import "NSString-NJRExtensions.h"
|
---|
| 18 | #import "NSAttributedString-NJRExtensions.h"
|
---|
| 19 | #import "NSCalendarDate-NJRExtensions.h"
|
---|
[34] | 20 | #import <Carbon/Carbon.h>
|
---|
[21] | 21 |
|
---|
[53] | 22 | #import "PSAlerts.h"
|
---|
[34] | 23 | #import "PSDockBounceAlert.h"
|
---|
| 24 | #import "PSScriptAlert.h"
|
---|
| 25 | #import "PSNotifierAlert.h"
|
---|
| 26 | #import "PSBeepAlert.h"
|
---|
| 27 | #import "PSMovieAlert.h"
|
---|
| 28 | #import "PSSpeechAlert.h"
|
---|
[61] | 29 | #import "PSWakeAlert.h"
|
---|
[34] | 30 |
|
---|
[28] | 31 | /* Bugs to file:
|
---|
[21] | 32 |
|
---|
[28] | 33 | ¥ any trailing spaces: -> exception for +[NSCalendarDate dateWithNaturalLanguageString]:
|
---|
| 34 | > NSCalendarDate dateWithNaturalLanguageString: '12 '
|
---|
| 35 | format error: internal error
|
---|
[21] | 36 |
|
---|
[28] | 37 | ¥ NSDate natural language stuff in NSCalendarDate (why?), misspelled category name
|
---|
| 38 | ¥ NSCalendarDate natural language stuff behaves differently from NSDateFormatter (AM/PM has no effect, shouldn't they share code?)
|
---|
[43] | 39 | ¥ descriptionWithCalendarFormat:, dateWithNaturalLanguageString: does not default to current locale, instead it defaults to US unless you tell it otherwise
|
---|
[28] | 40 | ¥ NSDateFormatter doc class description gives two examples for natural language that are incorrect, no link to NSDate doc that describes exactly how natural language dates are parsed
|
---|
| 41 | ¥ NSTimeFormatString does not include %p when it should, meaning that AM/PM is stripped yet 12-hour time is still used
|
---|
| 42 | ¥ NSNextDayDesignations, NSNextNextDayDesignations are noted as 'a string' in NSUserDefaults docs, but maybe they are actually an array, or either an array or a string, given their names?
|
---|
| 43 | ¥ "Setting the Format for Dates" does not document how to get 1:15 AM, the answer is %1I - strftime has no exact equivalent; the closest is %l. strftime does not permit numeric prefixes. It also refers to "NSCalendar" when no such class exists.
|
---|
[43] | 44 | ¥ none of many mentions of NSAMPMDesignation indicates that they include the leading spaces (" AM", " PM"). In "Setting the Format for Dates", needs to mention that the leading spaces are not included in %p with strftime. But if you use the NSCalendarDate stuff, it appears %p doesn't include the space (because it doesn't use the locale dictionary).
|
---|
| 45 | ¥ If you feed NSCalendarDate dateWithNaturalLanguageString: an " AM"/" PM" locale, it doesn't accept that date format.
|
---|
[28] | 46 | ¥ descriptions for %X and %x are reversed (time zone is in %X, not %x)
|
---|
[53] | 47 | ¥ NSComboBox data source issues, canÕt have it appear as ÒtodayÓ because the formatter doesnÕt like that. Should be able to enter text into the data source and have the formatter process it without altering it.
|
---|
[28] | 48 | ¥ too hard to implement date-only or time-only formatters
|
---|
| 49 | ¥ should be able to specify that natural language favors date or time (10 = 10th of month, not 10am)
|
---|
| 50 | ¥ please expose the iCal controls!
|
---|
| 51 |
|
---|
| 52 | */
|
---|
| 53 |
|
---|
[53] | 54 | static NSString * const PSAlertsSelected = @"Pester alerts selected"; // NSUserDefaults key
|
---|
| 55 | static NSString * const PSAlertsEditing = @"Pester alerts editing"; // NSUserDefaults key
|
---|
| 56 |
|
---|
[26] | 57 | @interface PSAlarmSetController (Private)
|
---|
[21] | 58 |
|
---|
[53] | 59 | - (void)_readAlerts:(PSAlerts *)alerts;
|
---|
| 60 | - (BOOL)_setAlerts;
|
---|
[26] | 61 | - (void)_stopUpdateTimer;
|
---|
| 62 |
|
---|
| 63 | @end
|
---|
| 64 |
|
---|
[21] | 65 | @implementation PSAlarmSetController
|
---|
| 66 |
|
---|
| 67 | - (void)awakeFromNib;
|
---|
| 68 | {
|
---|
[53] | 69 | NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
---|
[60] | 70 | NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
|
---|
[26] | 71 | alarm = [[PSAlarm alloc] init];
|
---|
[21] | 72 | [[self window] center];
|
---|
[43] | 73 | // XXX excessive retention of formatters? check later...
|
---|
| 74 | [timeOfDay setFormatter: [[NJRDateFormatter alloc] initWithDateFormat: [NJRDateFormatter localizedTimeFormatIncludingSeconds: NO] allowNaturalLanguage: YES]];
|
---|
| 75 | [timeDate setFormatter: [[NJRDateFormatter alloc] initWithDateFormat: [NJRDateFormatter localizedDateFormatIncludingWeekday: NO] allowNaturalLanguage: YES]];
|
---|
| 76 | {
|
---|
[53] | 77 | NSArray *dayNames = [defaults arrayForKey:
|
---|
[43] | 78 | NSWeekDayNameArray];
|
---|
| 79 | NSArray *completions = [timeDateCompletions itemTitles];
|
---|
| 80 | NSEnumerator *e = [completions objectEnumerator];
|
---|
| 81 | NSString *title;
|
---|
| 82 | int itemIndex = 0;
|
---|
| 83 | NSRange matchingRange;
|
---|
| 84 | while ( (title = [e nextObject]) != nil) {
|
---|
| 85 | matchingRange = [title rangeOfString: @"ÇdayÈ"];
|
---|
| 86 | if (matchingRange.location != NSNotFound) {
|
---|
| 87 | NSMutableString *format = [title mutableCopy];
|
---|
| 88 | NSEnumerator *we = [dayNames objectEnumerator];
|
---|
| 89 | NSString *dayName;
|
---|
| 90 | [format deleteCharactersInRange: matchingRange];
|
---|
| 91 | [format insertString: @"%@" atIndex: matchingRange.location];
|
---|
| 92 | [timeDateCompletions removeItemAtIndex: itemIndex];
|
---|
| 93 | while ( (dayName = [we nextObject]) != nil) {
|
---|
| 94 | [timeDateCompletions insertItemWithTitle: [NSString stringWithFormat: format, dayName] atIndex: itemIndex];
|
---|
| 95 | itemIndex++;
|
---|
| 96 | }
|
---|
| 97 | } else itemIndex++;
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
[53] | 100 | [editAlert setIntValue: [defaults boolForKey: PSAlertsEditing]];
|
---|
| 101 | {
|
---|
| 102 | NSDictionary *plAlerts = [defaults dictionaryForKey: PSAlertsSelected];
|
---|
| 103 | PSAlerts *alerts;
|
---|
| 104 | if (plAlerts == nil) {
|
---|
| 105 | alerts = [[PSAlerts alloc] initWithPesterVersion1Alerts];
|
---|
| 106 | } else {
|
---|
| 107 | NS_DURING
|
---|
| 108 | alerts = [[PSAlerts alloc] initWithPropertyList: plAlerts];
|
---|
| 109 | NS_HANDLER
|
---|
| 110 | NSRunAlertPanel(@"Unable to restore alerts", @"Pester could not restore recent alert information for one or more alerts in the Set Alarm window. The default set of alerts will be used instead.\n\n%@", nil, nil, nil, [localException reason]);
|
---|
| 111 | alerts = [[PSAlerts alloc] initWithPesterVersion1Alerts];
|
---|
| 112 | NS_ENDHANDLER
|
---|
| 113 | }
|
---|
| 114 | [self _readAlerts: alerts];
|
---|
| 115 | }
|
---|
[45] | 116 | [timeDate setObjectValue: [NSDate date]];
|
---|
[53] | 117 | [self inAtChanged: nil]; // by convention, if sender is nil, we're initializing
|
---|
[34] | 118 | [self playSoundChanged: nil];
|
---|
| 119 | [self doScriptChanged: nil];
|
---|
| 120 | [self doSpeakChanged: nil];
|
---|
[53] | 121 | [self editAlertChanged: nil];
|
---|
[34] | 122 | [script setFileTypes: [NSArray arrayWithObjects: @"applescript", @"script", NSFileTypeForHFSTypeCode(kOSAFileType), NSFileTypeForHFSTypeCode('TEXT'), nil]];
|
---|
[60] | 123 | [notificationCenter addObserver: self selector: @selector(silence:) name: PSAlarmAlertStopNotification object: nil];
|
---|
| 124 | [notificationCenter addObserver: self selector: @selector(playSoundChanged:) name: NJRQTMediaPopUpButtonMovieChangedNotification object: sound];
|
---|
| 125 | [notificationCenter addObserver: self selector: @selector(applicationWillHide:) name: NSApplicationWillHideNotification object: NSApp];
|
---|
| 126 | [notificationCenter addObserver: self selector: @selector(applicationDidUnhide:) name: NSApplicationDidUnhideNotification object: NSApp];
|
---|
[61] | 127 | [notificationCenter addObserver: self selector: @selector(applicationWillTerminate:) name: NSApplicationWillTerminateNotification object: NSApp];
|
---|
[53] | 128 | [voice setDelegate: self]; // XXX why don't we do this in IB? It should use the accessor...
|
---|
| 129 | [wakeUp setEnabled: [PSPowerManager autoWakeSupported]];
|
---|
| 130 | // XXX workaround for 10.1.x and 10.2.x bug which sets the first responder to the wrong field alternately, but it works if I set the initial first responder to nil... go figure.
|
---|
| 131 | [[self window] setInitialFirstResponder: nil];
|
---|
[24] | 132 | [[self window] makeKeyAndOrderFront: nil];
|
---|
[21] | 133 | }
|
---|
| 134 |
|
---|
| 135 | - (void)setStatus:(NSString *)aString;
|
---|
| 136 | {
|
---|
[26] | 137 | // NSLog(@"%@", alarm);
|
---|
[21] | 138 | if (aString != status) {
|
---|
| 139 | [status release]; status = nil;
|
---|
| 140 | status = [aString retain];
|
---|
| 141 | [timeSummary setStringValue: status];
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[53] | 145 | // XXX with -[NSControl currentEditor] don't need to compare? Also check -[NSControl validateEditing]
|
---|
[21] | 146 | - (id)objectValueForTextField:(NSTextField *)field whileEditing:(id)sender;
|
---|
| 147 | {
|
---|
| 148 | if (sender == field) {
|
---|
| 149 | NSString *stringValue = [[[self window] fieldEditor: NO forObject: field] string];
|
---|
| 150 | id obj = nil;
|
---|
| 151 | [[field formatter] getObjectValue: &obj forString: stringValue errorDescription: NULL];
|
---|
| 152 | // NSLog(@"from field editor: %@", obj);
|
---|
| 153 | return obj;
|
---|
| 154 | } else {
|
---|
| 155 | // NSLog(@"from field: %@", [field objectValue]);
|
---|
| 156 | return [field objectValue];
|
---|
| 157 | }
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[53] | 160 | #pragma mark date/interval setting
|
---|
| 161 |
|
---|
[21] | 162 | - (void)setAlarmDateAndInterval:(id)sender;
|
---|
| 163 | {
|
---|
[26] | 164 | if (isInterval) {
|
---|
[53] | 165 | [alarm setInterval: [timeInterval interval]];
|
---|
[21] | 166 | } else {
|
---|
[24] | 167 | [alarm setForDate: [self objectValueForTextField: timeDate whileEditing: sender]
|
---|
| 168 | atTime: [self objectValueForTextField: timeOfDay whileEditing: sender]];
|
---|
[21] | 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[26] | 172 | - (void)_stopUpdateTimer;
|
---|
| 173 | {
|
---|
[28] | 174 | [updateTimer invalidate]; [updateTimer release]; updateTimer = nil;
|
---|
[26] | 175 | }
|
---|
[21] | 176 |
|
---|
[53] | 177 | // XXX use OACalendar in popup like Palm Desktop?
|
---|
[21] | 178 |
|
---|
[24] | 179 | - (IBAction)updateDateDisplay:(id)sender;
|
---|
[21] | 180 | {
|
---|
[26] | 181 | // NSLog(@"updateDateDisplay: %@", sender);
|
---|
[24] | 182 | if ([alarm isValid]) {
|
---|
[43] | 183 | [self setStatus: [NSString stringWithFormat: @"Alarm will be set for %@ on %@", [alarm timeString], [alarm dateString]]];
|
---|
[21] | 184 | [setButton setEnabled: YES];
|
---|
[26] | 185 | if (updateTimer == nil || ![updateTimer isValid]) {
|
---|
| 186 | // XXX this logic (and the timer) should really go into PSAlarm, to send notifications for status updates instead. Timer starts when people are watching, stops when people aren't.
|
---|
| 187 | // NSLog(@"setting timer");
|
---|
| 188 | if (isInterval) {
|
---|
| 189 | updateTimer = [NSTimer scheduledTimerWithTimeInterval: 1 target: self selector: @selector(updateDateDisplay:) userInfo: nil repeats: YES];
|
---|
| 190 | } else {
|
---|
| 191 | updateTimer = [NSTimer scheduledTimerWithTimeInterval: [alarm interval] target: self selector: @selector(updateDateDisplay:) userInfo: nil repeats: NO];
|
---|
| 192 | }
|
---|
| 193 | [updateTimer retain];
|
---|
| 194 | }
|
---|
[21] | 195 | } else {
|
---|
| 196 | [setButton setEnabled: NO];
|
---|
[26] | 197 | [self setStatus: [alarm invalidMessage]];
|
---|
| 198 | [self _stopUpdateTimer];
|
---|
[21] | 199 | }
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[26] | 202 | // Be careful not to hook up any of the text fields' actions to update: because we handle them in controlTextDidChange: instead. If we could get the active text field somehow via public API (guess we could use controlTextDidBegin/controlTextDidEndEditing) then we'd not need to overload the update sender for this purpose. Or, I guess, we could use another method other than update. It should not be this hard to implement what is essentially standard behavior. Sigh.
|
---|
[43] | 203 | // Note: finding out whether a given control is editing is easier. See: <http://cocoa.mamasam.com/COCOADEV/2002/03/2/28501.php>.
|
---|
[26] | 204 |
|
---|
[24] | 205 | - (IBAction)update:(id)sender;
|
---|
| 206 | {
|
---|
| 207 | // NSLog(@"update: %@", sender);
|
---|
| 208 | [self setAlarmDateAndInterval: sender];
|
---|
| 209 | [self updateDateDisplay: sender];
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[21] | 212 | - (IBAction)inAtChanged:(id)sender;
|
---|
| 213 | {
|
---|
[43] | 214 | NSButtonCell *new = [inAtMatrix selectedCell], *old;
|
---|
[26] | 215 | isInterval = ([inAtMatrix selectedTag] == 0);
|
---|
[43] | 216 | old = [inAtMatrix cellWithTag: isInterval];
|
---|
| 217 | NSAssert(new != old, @"in and at buttons should be distinct!");
|
---|
| 218 | [old setKeyEquivalent: [new keyEquivalent]];
|
---|
| 219 | [old setKeyEquivalentModifierMask: [new keyEquivalentModifierMask]];
|
---|
| 220 | [new setKeyEquivalent: @""];
|
---|
| 221 | [new setKeyEquivalentModifierMask: 0];
|
---|
[26] | 222 | [timeInterval setEnabled: isInterval];
|
---|
| 223 | [timeIntervalUnits setEnabled: isInterval];
|
---|
[34] | 224 | [timeIntervalRepeats setEnabled: isInterval];
|
---|
[26] | 225 | [timeOfDay setEnabled: !isInterval];
|
---|
| 226 | [timeDate setEnabled: !isInterval];
|
---|
| 227 | [timeDateCompletions setEnabled: !isInterval];
|
---|
[21] | 228 | if (sender != nil)
|
---|
[53] | 229 | [[self window] makeFirstResponder: isInterval ? (NSTextField *)timeInterval : timeOfDay];
|
---|
[21] | 230 | // NSLog(@"UPDATING FROM inAtChanged");
|
---|
| 231 | [self update: nil];
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[53] | 234 | - (IBAction)dateCompleted:(NSPopUpButton *)sender;
|
---|
| 235 | {
|
---|
| 236 | [timeDate setStringValue: [sender titleOfSelectedItem]];
|
---|
| 237 | [self update: sender];
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | #pragma mark alert editing
|
---|
| 241 |
|
---|
| 242 | - (IBAction)editAlertChanged:(id)sender;
|
---|
| 243 | {
|
---|
| 244 | BOOL editAlertSelected = [editAlert intValue];
|
---|
| 245 | NSView *editAlertControl = [editAlert controlView];
|
---|
| 246 | NSWindow *window = [self window];
|
---|
| 247 | NSRect frame = [window frame];
|
---|
| 248 | if (editAlertSelected) {
|
---|
| 249 | NSSize editWinSize = [window maxSize];
|
---|
| 250 | [editAlertControl setNextKeyView: [displayMessage controlView]];
|
---|
| 251 | frame.origin.y += frame.size.height - editWinSize.height;
|
---|
| 252 | frame.size = editWinSize;
|
---|
| 253 | [window setFrame: frame display: (sender != nil) animate: (sender != nil)];
|
---|
| 254 | [self updateDateDisplay: sender];
|
---|
| 255 | [alertTabs selectTabViewItemWithIdentifier: @"edit"];
|
---|
| 256 | } else {
|
---|
| 257 | NSSize viewWinSize = [window minSize];
|
---|
| 258 | NSRect textFrame = [alertView frame];
|
---|
| 259 | float textHeight;
|
---|
| 260 | if (![self _setAlerts]) {
|
---|
| 261 | [alertView setStringValue: [NSString stringWithFormat: @"CouldnÕt process alert information.\n%@", status]];
|
---|
| 262 | } else {
|
---|
| 263 | NSAttributedString *string = [[alarm alerts] prettyList];
|
---|
| 264 | if (string == nil) {
|
---|
| 265 | [alertView setStringValue: @"Do nothing. Click the button labeled ÒEditÓ to add an alert."];
|
---|
| 266 | } else {
|
---|
| 267 | [alertView setAttributedStringValue: string];
|
---|
| 268 | [self updateDateDisplay: sender];
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
| 271 | if (sender != nil) { // nil == we're initializing, don't mess with focus
|
---|
| 272 | NSResponder *oldResponder = [window firstResponder];
|
---|
| 273 | // make sure focus doesn't get stuck in the edit tab: it is confusing and leaves behind artifacts
|
---|
| 274 | if (oldResponder == editAlertControl || [oldResponder isKindOfClass: [NSView class]] && [(NSView *)oldResponder isDescendantOf: alertTabs])
|
---|
| 275 | [window makeFirstResponder: messageField]; // would use editAlertControl, but can't get it to display anomaly-free.
|
---|
| 276 | [self silence: sender];
|
---|
| 277 | }
|
---|
| 278 | // allow height to expand, though not arbitrarily (should still fit on an 800x600 screen)
|
---|
| 279 | textHeight = [[alertView cell] cellSizeForBounds: NSMakeRect(0, 0, textFrame.size.width, 400)].height;
|
---|
| 280 | textFrame.origin.y += textFrame.size.height - textHeight;
|
---|
| 281 | textFrame.size.height = textHeight;
|
---|
| 282 | [alertView setFrame: textFrame];
|
---|
| 283 | viewWinSize.height += textHeight;
|
---|
| 284 | [alertTabs selectTabViewItemWithIdentifier: @"view"];
|
---|
| 285 | frame.origin.y += frame.size.height - viewWinSize.height;
|
---|
| 286 | frame.size = viewWinSize;
|
---|
| 287 | [window setFrame: frame display: (sender != nil) animate: (sender != nil)];
|
---|
| 288 | [editAlertControl setNextKeyView: cancelButton];
|
---|
| 289 | }
|
---|
| 290 | if (sender != nil) {
|
---|
| 291 | [[NSUserDefaults standardUserDefaults] setBool: editAlertSelected forKey: PSAlertsEditing];
|
---|
| 292 | }
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 |
|
---|
[34] | 296 | - (IBAction)playSoundChanged:(id)sender;
|
---|
| 297 | {
|
---|
| 298 | BOOL playSoundSelected = [playSound intValue];
|
---|
[41] | 299 | BOOL canRepeat = playSoundSelected ? [sound canRepeat] : NO;
|
---|
[34] | 300 | [sound setEnabled: playSoundSelected];
|
---|
[41] | 301 | [soundRepetitions setEnabled: canRepeat];
|
---|
| 302 | [soundRepetitionStepper setEnabled: canRepeat];
|
---|
| 303 | [soundRepetitionsLabel setTextColor: canRepeat ? [NSColor controlTextColor] : [NSColor disabledControlTextColor]];
|
---|
[53] | 304 | if (playSoundSelected && sender == playSound) {
|
---|
[34] | 305 | [[self window] makeFirstResponder: sound];
|
---|
[53] | 306 | }
|
---|
[34] | 307 | }
|
---|
| 308 |
|
---|
[41] | 309 | - (IBAction)setSoundRepetitionCount:(id)sender;
|
---|
| 310 | {
|
---|
[43] | 311 | NSTextView *fieldEditor = (NSTextView *)[soundRepetitions currentEditor];
|
---|
| 312 | BOOL isEditing = (fieldEditor != nil);
|
---|
| 313 | int newReps = [sender intValue], oldReps;
|
---|
| 314 | if (isEditing) {
|
---|
| 315 | // XXX work around bug where if you ask soundRepetitions for its intValue too often while it's editing, the field begins to flash
|
---|
| 316 | oldReps = [[[fieldEditor textStorage] string] intValue];
|
---|
| 317 | } else oldReps = [soundRepetitions intValue];
|
---|
| 318 | if (newReps != oldReps) {
|
---|
[41] | 319 | [soundRepetitions setIntValue: newReps];
|
---|
[43] | 320 | // NSLog(@"updating: new value %d, old value %d%@", newReps, oldReps, isEditing ? @", is editing" : @"");
|
---|
| 321 | // XXX work around 10.1 bug, otherwise field only displays every second value
|
---|
| 322 | if (isEditing) [soundRepetitions selectText: self];
|
---|
| 323 | }
|
---|
[41] | 324 | }
|
---|
| 325 |
|
---|
[34] | 326 | // XXX should check the 'Do script:' button when someone drops a script on the button
|
---|
| 327 |
|
---|
| 328 | - (IBAction)doScriptChanged:(id)sender;
|
---|
| 329 | {
|
---|
| 330 | BOOL doScriptSelected = [doScript intValue];
|
---|
| 331 | [script setEnabled: doScriptSelected];
|
---|
| 332 | [scriptSelectButton setEnabled: doScriptSelected];
|
---|
[53] | 333 | if (doScriptSelected && sender != nil) {
|
---|
[34] | 334 | [[self window] makeFirstResponder: scriptSelectButton];
|
---|
[53] | 335 | if ([script alias] == nil) [scriptSelectButton performClick: sender];
|
---|
| 336 | }
|
---|
[34] | 337 | }
|
---|
| 338 |
|
---|
| 339 | - (IBAction)doSpeakChanged:(id)sender;
|
---|
| 340 | {
|
---|
[53] | 341 | BOOL doSpeakSelected = [doSpeak state] == NSOnState;
|
---|
[34] | 342 | [voice setEnabled: doSpeakSelected];
|
---|
| 343 | if (doSpeakSelected && sender != nil)
|
---|
| 344 | [[self window] makeFirstResponder: voice];
|
---|
| 345 | }
|
---|
| 346 |
|
---|
[53] | 347 | - (void)_readAlerts:(PSAlerts *)alerts;
|
---|
[21] | 348 | {
|
---|
[53] | 349 | NSEnumerator *e = [alerts alertEnumerator];
|
---|
| 350 | PSAlert *alert;
|
---|
| 351 |
|
---|
| 352 | [alarm setAlerts: alerts];
|
---|
| 353 |
|
---|
| 354 | // turn off all alerts
|
---|
| 355 | [bounceDockIcon setState: NSOffState];
|
---|
| 356 | [doScript setIntValue: NO];
|
---|
| 357 | [displayMessage setIntValue: NO];
|
---|
| 358 | [playSound setIntValue: NO];
|
---|
| 359 | [doSpeak setIntValue: NO];
|
---|
| 360 |
|
---|
| 361 | while ( (alert = [e nextObject]) != nil) {
|
---|
| 362 | if ([alert isKindOfClass: [PSDockBounceAlert class]]) {
|
---|
| 363 | [bounceDockIcon setState: NSOnState];
|
---|
| 364 | } else if ([alert isKindOfClass: [PSScriptAlert class]]) {
|
---|
| 365 | [doScript setIntValue: YES];
|
---|
| 366 | [script setAlias: [(PSScriptAlert *)alert scriptFileAlias]];
|
---|
| 367 | } else if ([alert isKindOfClass: [PSNotifierAlert class]]) {
|
---|
| 368 | [displayMessage setIntValue: YES];
|
---|
| 369 | } else if ([alert isKindOfClass: [PSBeepAlert class]]) {
|
---|
| 370 | unsigned int repetitions = [(PSBeepAlert *)alert repetitions];
|
---|
| 371 | [playSound setIntValue: YES];
|
---|
| 372 | [sound setAlias: nil];
|
---|
| 373 | [soundRepetitions setIntValue: repetitions];
|
---|
| 374 | [soundRepetitionStepper setIntValue: repetitions];
|
---|
| 375 | } else if ([alert isKindOfClass: [PSMovieAlert class]]) {
|
---|
| 376 | unsigned int repetitions = [(PSMovieAlert *)alert repetitions];
|
---|
| 377 | [playSound setIntValue: YES];
|
---|
| 378 | [sound setAlias: [(PSMovieAlert *)alert movieFileAlias]];
|
---|
| 379 | [soundRepetitions setIntValue: repetitions];
|
---|
| 380 | [soundRepetitionStepper setIntValue: repetitions];
|
---|
| 381 | } else if ([alert isKindOfClass: [PSSpeechAlert class]]) {
|
---|
| 382 | [doSpeak setIntValue: YES];
|
---|
| 383 | [voice setVoice: [(PSSpeechAlert *)alert voice]];
|
---|
[61] | 384 | } else if ([alert isKindOfClass: [PSWakeAlert class]]) {
|
---|
| 385 | [wakeUp setIntValue: YES];
|
---|
[53] | 386 | }
|
---|
[21] | 387 | }
|
---|
[61] | 388 | }
|
---|
[21] | 389 |
|
---|
[53] | 390 | - (BOOL)_setAlerts;
|
---|
| 391 | {
|
---|
| 392 | PSAlerts *alerts = [alarm alerts];
|
---|
| 393 |
|
---|
| 394 | [alerts removeAlerts];
|
---|
| 395 | NS_DURING
|
---|
| 396 | // dock bounce alert
|
---|
| 397 | if ([bounceDockIcon state] == NSOnState)
|
---|
| 398 | [alerts addAlert: [PSDockBounceAlert alert]];
|
---|
| 399 | // script alert
|
---|
| 400 | if ([doScript intValue]) {
|
---|
| 401 | BDAlias *scriptFileAlias = [script alias];
|
---|
| 402 | if (scriptFileAlias == nil) {
|
---|
| 403 | [self setStatus: @"Unable to set script alert (no script specified?)"];
|
---|
| 404 | return NO;
|
---|
| 405 | }
|
---|
| 406 | [alerts addAlert: [PSScriptAlert alertWithScriptFileAlias: scriptFileAlias]];
|
---|
| 407 | }
|
---|
| 408 | // notifier alert
|
---|
| 409 | if ([displayMessage intValue])
|
---|
| 410 | [alerts addAlert: [PSNotifierAlert alert]];
|
---|
| 411 | // sound alerts
|
---|
| 412 | if ([playSound intValue]) {
|
---|
| 413 | BDAlias *soundAlias = [sound selectedAlias];
|
---|
| 414 | unsigned short numReps = [soundRepetitions intValue];
|
---|
| 415 | if (soundAlias == nil) // beep alert
|
---|
| 416 | [alerts addAlert: [PSBeepAlert alertWithRepetitions: numReps]];
|
---|
| 417 | else // movie alert
|
---|
| 418 | [alerts addAlert: [PSMovieAlert alertWithMovieFileAlias: soundAlias repetitions: numReps]];
|
---|
| 419 | }
|
---|
| 420 | // speech alert
|
---|
| 421 | if ([doSpeak intValue])
|
---|
| 422 | [alerts addAlert: [PSSpeechAlert alertWithVoice: [voice titleOfSelectedItem]]];
|
---|
[61] | 423 | // wake alert
|
---|
| 424 | if ([wakeUp intValue])
|
---|
| 425 | [alerts addAlert: [PSWakeAlert alert]];
|
---|
[53] | 426 | [[NSUserDefaults standardUserDefaults] setObject: [alerts propertyListRepresentation] forKey: PSAlertsSelected];
|
---|
| 427 | NS_HANDLER
|
---|
| 428 | [self setStatus: [localException reason]];
|
---|
| 429 | NS_VALUERETURN(NO, BOOL);
|
---|
| 430 | NS_ENDHANDLER
|
---|
| 431 | return YES;
|
---|
| 432 | }
|
---|
| 433 |
|
---|
| 434 | #pragma mark actions
|
---|
| 435 |
|
---|
[21] | 436 | // to ensure proper updating of interval, this should be the only method by which the window is shown (e.g. from the Alarm menu)
|
---|
| 437 | - (IBAction)showWindow:(id)sender;
|
---|
| 438 | {
|
---|
| 439 | if (![[self window] isVisible]) {
|
---|
[53] | 440 | NSDate *today = [NSCalendarDate dateForDay: [NSDate date]];
|
---|
| 441 | if ([(NSDate *)[timeDate objectValue] compare: today] == NSOrderedAscending) {
|
---|
| 442 | [timeDate setObjectValue: today];
|
---|
| 443 | }
|
---|
[21] | 444 | [self update: self];
|
---|
[53] | 445 | // XXX bug workaround - otherwise, first responder appears to alternate every time the window is shown. And if you set the initial first responder, you can't tab in the window. :(
|
---|
[28] | 446 | [[self window] makeFirstResponder: [[self window] initialFirstResponder]];
|
---|
[21] | 447 | }
|
---|
| 448 | [super showWindow: sender];
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | - (IBAction)setAlarm:(NSButton *)sender;
|
---|
| 452 | {
|
---|
[45] | 453 | // set alerts before setting alarm...
|
---|
[53] | 454 | if (![self _setAlerts]) return;
|
---|
[45] | 455 |
|
---|
| 456 | // set alarm
|
---|
| 457 | [self setAlarmDateAndInterval: sender];
|
---|
[53] | 458 | [alarm setRepeating: [timeIntervalRepeats state] == NSOnState];
|
---|
[45] | 459 | [alarm setMessage: [messageField stringValue]];
|
---|
| 460 | if (![alarm setTimer]) {
|
---|
| 461 | [self setStatus: [@"Unable to set alarm. " stringByAppendingString: [alarm invalidMessage]]];
|
---|
| 462 | return;
|
---|
| 463 | }
|
---|
[34] | 464 |
|
---|
[24] | 465 | [self setStatus: [[alarm date] descriptionWithCalendarFormat: @"Alarm set for %x at %X" timeZone: nil locale: nil]];
|
---|
[21] | 466 | [[self window] close];
|
---|
[26] | 467 | [alarm release];
|
---|
| 468 | alarm = [[PSAlarm alloc] init];
|
---|
[21] | 469 | }
|
---|
| 470 |
|
---|
[34] | 471 | - (IBAction)silence:(id)sender;
|
---|
| 472 | {
|
---|
| 473 | [sound stopSoundPreview: self];
|
---|
| 474 | [voice stopVoicePreview: self];
|
---|
| 475 | }
|
---|
| 476 |
|
---|
[26] | 477 | @end
|
---|
| 478 |
|
---|
| 479 | @implementation PSAlarmSetController (NSControlSubclassDelegate)
|
---|
| 480 |
|
---|
[53] | 481 | - (BOOL)control:(NSControl *)control didFailToFormatString:(NSString *)string errorDescription:(NSString *)error;
|
---|
| 482 | {
|
---|
| 483 | if (control == timeInterval)
|
---|
| 484 | [timeInterval handleDidFailToFormatString: string errorDescription: error label: @"alarm interval"];
|
---|
| 485 | return NO;
|
---|
| 486 | }
|
---|
| 487 |
|
---|
[26] | 488 | - (void)control:(NSControl *)control didFailToValidatePartialString:(NSString *)string errorDescription:(NSString *)error;
|
---|
| 489 | {
|
---|
| 490 | // NSLog(@"UPDATING FROM validation");
|
---|
[53] | 491 | if (control == timeInterval) [self update: timeInterval]; // make sure we still examine the field editor, otherwise if the existing numeric string is invalid, it'll be cleared
|
---|
[26] | 492 | }
|
---|
| 493 |
|
---|
| 494 | @end
|
---|
| 495 |
|
---|
| 496 | @implementation PSAlarmSetController (NSWindowNotifications)
|
---|
| 497 |
|
---|
| 498 | - (void)windowWillClose:(NSNotification *)notification;
|
---|
| 499 | {
|
---|
| 500 | // NSLog(@"stopping update timer");
|
---|
[34] | 501 | [self silence: nil];
|
---|
[26] | 502 | [self _stopUpdateTimer];
|
---|
[53] | 503 | [self _setAlerts];
|
---|
[26] | 504 | }
|
---|
| 505 |
|
---|
| 506 | @end
|
---|
| 507 |
|
---|
| 508 | @implementation PSAlarmSetController (NSControlSubclassNotifications)
|
---|
| 509 |
|
---|
[21] | 510 | // called because we're the delegate
|
---|
| 511 |
|
---|
| 512 | - (void)controlTextDidChange:(NSNotification *)notification;
|
---|
| 513 | {
|
---|
[43] | 514 | // NSLog(@"UPDATING FROM controlTextDidChange: %@", [notification object]);
|
---|
[21] | 515 | [self update: [notification object]];
|
---|
| 516 | }
|
---|
| 517 |
|
---|
[34] | 518 | @end
|
---|
| 519 |
|
---|
| 520 | @implementation PSAlarmSetController (NJRVoicePopUpButtonDelegate)
|
---|
| 521 |
|
---|
| 522 | - (NSString *)voicePopUpButton:(NJRVoicePopUpButton *)sender previewStringForVoice:(NSString *)voice;
|
---|
| 523 | {
|
---|
[53] | 524 | NSString *message = [messageField stringValue];
|
---|
| 525 | if (message == nil || [message length] == 0)
|
---|
| 526 | message = [alarm message];
|
---|
| 527 | return message;
|
---|
[34] | 528 | }
|
---|
| 529 |
|
---|
[60] | 530 | @end
|
---|
| 531 |
|
---|
| 532 | @implementation PSAlarmSetController (NSApplicationNotifications)
|
---|
| 533 |
|
---|
[61] | 534 | - (void)applicationWillTerminate:(NSNotification *)notification;
|
---|
| 535 | {
|
---|
| 536 | [self _setAlerts];
|
---|
| 537 | }
|
---|
| 538 |
|
---|
[60] | 539 | - (void)applicationWillHide:(NSNotification *)notification;
|
---|
| 540 | {
|
---|
| 541 | if ([[self window] isVisible]) {
|
---|
| 542 | [self silence: nil];
|
---|
| 543 | [self _stopUpdateTimer];
|
---|
| 544 | }
|
---|
| 545 | }
|
---|
| 546 |
|
---|
| 547 | - (void)applicationDidUnhide:(NSNotification *)notification;
|
---|
| 548 | {
|
---|
| 549 | if ([[self window] isVisible]) {
|
---|
| 550 | [self update: self];
|
---|
| 551 | }
|
---|
| 552 | }
|
---|
| 553 |
|
---|
[26] | 554 | @end |
---|