source: trunk/Cocoa/DockCam/DockCamPrefs.m@ 207

Last change on this file since 207 was 5, checked in by Nicholas Riley, 20 years ago

Initial import.

File size: 5.9 KB
Line 
1//
2// DockCamPrefs.m
3// DockCam
4//
5// Created by Nicholas Riley on Thu Jun 27 2002.
6// Copyright (c) 2002 Nicholas Riley. All rights reserved.
7//
8
9#import "DockCamPrefs.h"
10
11NSString * const DCImageLocation = @"DCImageLocation";
12NSString * const DCImageSize = @"DCImageSize";
13NSString * const DCImageRefreshEnabled = @"DCImageRefreshEnabled";
14NSString * const DCImageRefreshInterval = @"DCImageRefreshInterval";
15NSString * const DCShowImageInBackground = @"DCShowImageInBackground";
16NSString * const DCShowStatus = @"DCShowStatus";
17
18NSString * const DCPrefChangedNotification = @"DCPrefChangedNotification";
19
20const NSTimeInterval DCDefaultImageRefreshInterval = 30;
21
22enum {
23 DCImageRetrieveOnOpen = 2000, DCImageRetrieveOnTimer = 2001
24};
25
26enum {
27 DCImageRefreshIntervalSeconds = 1, DCImageRefreshIntervalMinutes = 60, DCImageRefreshIntervalHours = 3600
28};
29
30NSUserDefaults *defaults;
31
32@implementation DockCamPrefs
33
34+ (void)initialize;
35{
36 NSDictionary *appDefaults =
37 [NSDictionary dictionaryWithObjectsAndKeys:
38 @"", DCImageLocation,
39 [NSNumber numberWithInt: DCDefaultImageRefreshInterval], DCImageRefreshInterval,
40 [NSNumber numberWithBool: NO], DCShowImageInBackground,
41 [NSNumber numberWithBool: YES], DCShowStatus,
42 nil];
43 defaults = [NSUserDefaults standardUserDefaults];
44 [defaults registerDefaults: appDefaults];
45}
46
47+ (void)_prefChanged:(NSString *)pref;
48{
49 [[NSNotificationCenter defaultCenter] postNotificationName: DCPrefChangedNotification object: pref];
50}
51
52#pragma mark preference getters
53
54+ (BOOL)boolForPref:(NSString *)pref;
55{
56 return [defaults boolForKey: pref];
57}
58
59+ (int)intForPref:(NSString *)pref;
60{
61 return [defaults integerForKey: pref];
62}
63
64+ (NSString *)stringForPref:(NSString *)pref;
65{
66 return [defaults stringForKey: pref];
67}
68
69+ (NSSize)sizeForPref:(NSString *)pref;
70{
71 return NSSizeFromString([defaults stringForKey: pref]);
72}
73
74#pragma mark preference setters
75
76+ (void)setBool:(BOOL)value forPref:(NSString *)pref;
77{
78 BOOL oldValue = [self boolForPref: pref];
79 if (oldValue != value) {
80 [defaults setBool: value forKey: pref];
81 [self _prefChanged: pref];
82 }
83}
84
85+ (void)setInt:(int)value forPref:(NSString *)pref;
86{
87 int oldValue = [self intForPref: pref];
88 if (oldValue != value) {
89 [defaults setInteger: value forKey: pref];
90 [self _prefChanged: pref];
91 }
92}
93
94+ (void)setString:(NSString *)value forPref:(NSString *)pref;
95{
96 NSString *oldValue = [self stringForPref: pref];
97 if (![oldValue isEqualToString: value]) {
98 [defaults setObject: value forKey: pref];
99 [self _prefChanged: pref];
100 }
101}
102
103+ (void)setSize:(NSSize)value forPref:(NSString *)pref;
104{
105 NSSize oldValue = [self sizeForPref: pref];
106 if (!NSEqualSizes(oldValue, value)) {
107 [defaults setObject: NSStringFromSize(value) forKey: pref];
108 [self _prefChanged: pref];
109 }
110}
111
112#pragma mark complex interface-preference mapping
113
114- (BOOL)retrieveOnTimer;
115{
116 return [[retrieveImageAutomatically selectedCell] tag] == DCImageRetrieveOnTimer;
117}
118
119- (NSTimeInterval)interval;
120{
121 return [imageRefreshInterval intValue] * [[imageRefreshIntervalUnits selectedItem] tag];
122}
123
124- (void)setInterval:(NSTimeInterval)interval;
125{
126 // we assume that the tags are in ascending order in the array
127 NSEnumerator *e = [[imageRefreshIntervalUnits itemArray] reverseObjectEnumerator];
128 NSMenuItem *i;
129 int multiplierTag;
130
131 while ( (i = [e nextObject]) != nil) {
132 multiplierTag = [i tag];
133 if (((int)interval % multiplierTag) == 0) {
134 [imageRefreshInterval setIntValue: interval / multiplierTag];
135 [imageRefreshIntervalUnits selectItem: i];
136 return;
137 }
138 }
139 [imageRefreshIntervalUnits selectItemAtIndex: 0];
140 [imageRefreshInterval setIntValue: DCDefaultImageRefreshInterval];
141}
142
143#pragma mark preference I/O
144
145- (void)readFromPrefs;
146{
147 [imageLocation setStringValue: [DockCamPrefs stringForPref: DCImageLocation]];
148 [retrieveImageAutomatically selectCellWithTag:
149 [DockCamPrefs boolForPref: DCImageRefreshEnabled] ? DCImageRetrieveOnTimer : DCImageRetrieveOnOpen];
150 [self setInterval: [DockCamPrefs intForPref: DCImageRefreshInterval]];
151 [showImageInBackground setState:
152 [DockCamPrefs boolForPref: DCShowImageInBackground] ? NSOnState : NSOffState];
153}
154
155- (IBAction)writeToPrefs:(id)sender;
156{
157 [DockCamPrefs setString: [imageLocation stringValue] forPref: DCImageLocation];
158 [DockCamPrefs setBool: [self retrieveOnTimer] forPref: DCImageRefreshEnabled];
159 [DockCamPrefs setInt: [self interval] forPref: DCImageRefreshInterval];
160 [DockCamPrefs setBool: [showImageInBackground state] == NSOnState forPref: DCShowImageInBackground];
161 [defaults synchronize];
162}
163
164#pragma mark interface maintenance
165
166- (IBAction)showWindow:(id)sender;
167{
168 // XXX workaround for weird bug where combo box and text field alternately become first responder
169 [super showWindow: sender];
170 [[[self window] initialFirstResponder] becomeFirstResponder];
171}
172
173- (void)update;
174{
175 BOOL enabled = [self retrieveOnTimer];
176 [imageRefreshInterval setEnabled: enabled];
177 [imageRefreshIntervalUnits setEnabled: enabled];
178}
179
180- (IBAction)retrieveImageAutomatically:(id)sender;
181{
182 [self update];
183 if ([self retrieveOnTimer]) [imageRefreshInterval becomeFirstResponder];
184 else [sender becomeFirstResponder];
185}
186
187#pragma mark initialize-release
188
189- (id)init {
190 self = [super initWithWindowNibName:@"Preferences"];
191 [self window]; // connect outlets
192 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(windowWillClose:)
193 name: NSWindowWillCloseNotification object: [self window]];
194 [self readFromPrefs];
195 [self update];
196 return self;
197}
198
199@end
200
201@implementation DockCamPrefs (NSWindowNotifications)
202
203- (void)windowWillClose:(NSNotification *)notification;
204{
205 [self writeToPrefs: self];
206}
207
208@end
Note: See TracBrowser for help on using the repository browser.