File: PreferencesController.m

package info (click to toggle)
fortunate.app 3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 516 kB
  • sloc: ansic: 2,697; objc: 470; makefile: 17
file content (273 lines) | stat: -rw-r--r-- 8,080 bytes parent folder | download
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
/* PreferencesController.m created by chris on Wed 30-Jun-1999 */

#import "PreferencesController.h"

NSString* kUseBundledFortune   = @"UseBundledFortune";
NSString* kFortuneLocation     = @"FortuneLocation";
NSString* kUseBundledDatfiles  = @"UseBundledDatfiles";
NSString* kDatfilesLocation    = @"DatfilesLocation";
NSString* kShowOffensive       = @"ShowOffensive";
NSString* kFortuneFontName     = @"FontName";
NSString* kFortuneFontSize     = @"FontSize";

@implementation PreferencesController

+ (void)initialize
{
   NSMutableDictionary* standardDefs;
   NSUserDefaults*      defaults;

   [super initialize];

   // setup user defaults
   defaults = [NSUserDefaults standardUserDefaults];
   standardDefs = [NSMutableDictionary dictionary];

   [standardDefs setObject: @"YES" forKey: kUseBundledDatfiles];
   [standardDefs setObject: @"/usr/games/fortune" forKey: kFortuneLocation];
#ifdef WITH_FORTUNE_PROGRAM
   [standardDefs setObject: @"YES" forKey: kUseBundledFortune];
#else
   [standardDefs setObject: @"NO" forKey: kUseBundledFortune];
#endif
   [standardDefs setObject: @"YES" forKey: kShowOffensive];

   [defaults registerDefaults: standardDefs];
}

- (void)showPrefs:(id)sender
{
    [prefsPanel setFloatingPanel: YES];
    [self resetPrefs: nil];
    [prefsPanel makeKeyAndOrderFront: nil];
}

- (void)resetPrefs:(id)sender
{
   NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
   BOOL useBundledFortune;
   BOOL useBundledDatfiles;

   useBundledFortune = [defaults boolForKey: kUseBundledFortune];
   [useBundledFortuneCheckBox setState: (useBundledFortune ? NSOnState : NSOffState)];
   [self useBundledFortuneClicked: nil];

   useBundledDatfiles = [defaults boolForKey: kUseBundledDatfiles];
   [useBundledDatfilesCheckBox setState: (useBundledDatfiles ? NSOnState : NSOffState)];
   [self useBundledDatfilesClicked: nil];

   [offensiveCheckBox setState: 
      ([defaults boolForKey: kShowOffensive] ? NSOnState : NSOffState)];
      
   fontChanged = NO;
   newFont = nil;
   [fontField setStringValue: [NSString stringWithFormat:@"%@ %g",
        [[mainDoc fortuneFont] fontName], [[mainDoc fortuneFont] pointSize]]];
}

- (void)savePrefs:(id)sender
{
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   NSMutableString *errors = [NSMutableString string];
   NSString *datfilesLocation = nil;
   NSString *fortuneLocation = nil;
   BOOL ok = YES;
   BOOL showOffensive;
   
   fortuneLocation = [pathField stringValue];
   datfilesLocation = [datfilesPathField stringValue];

   // check settings
   if ([useBundledFortuneCheckBox state] == NSOffState &&
       ![[NSFileManager defaultManager] fileExistsAtPath: fortuneLocation])
   {
      [errors appendString: @"\nInvalid fortune program location."];
      ok = NO;
   }

   if ([useBundledDatfilesCheckBox state] == NSOffState &&
       ![[NSFileManager defaultManager] fileExistsAtPath: datfilesLocation])
   {
      [errors appendString: @"\nInvalid datafiles location."];
      ok = NO;
   }

   if (!ok)
   {
      [errors deleteCharactersInRange: NSMakeRange(0, 1)];
      NSRunAlertPanel(@"Un-Fortunate Error",
                      errors,
                      @"Ok",
                      nil,
                      nil);
      return;
   }

   // save settings
   if ([useBundledFortuneCheckBox state] == NSOnState)
   {
      [defaults setBool: YES forKey: kUseBundledFortune];
   }
   else
   {
      [defaults setBool: NO forKey: kUseBundledFortune];
      [defaults setObject: fortuneLocation forKey: kFortuneLocation];
   }

   if ([useBundledDatfilesCheckBox state] == NSOnState)
   {
      [defaults setBool: YES forKey: kUseBundledDatfiles];
   }
   else
   {
      [defaults setBool: NO forKey: kUseBundledDatfiles];
      [defaults setObject: datfilesLocation forKey: kDatfilesLocation];
   }
   
   showOffensive = ([offensiveCheckBox state] == NSOnState ? YES : NO);
   [defaults setBool: showOffensive forKey: kShowOffensive];
   
   if (fontChanged)
   {
      [defaults setObject: [newFont fontName] forKey: kFortuneFontName];
      [defaults setFloat: [newFont pointSize] forKey: kFortuneFontSize];
   }
   
   [[[NSFontManager sharedFontManager] fontPanel: NO] close];
   [prefsPanel close];
}

- (void)cancelPrefs:(id)sender
{
    [prefsPanel close];
}

- (void)showOpenFortunePanel:(id)sender
{
    NSOpenPanel *openPanel = [NSOpenPanel openPanel];
    int ret;
    
    [openPanel setAllowsMultipleSelection: NO];
    [openPanel setCanChooseFiles: YES];
    [openPanel setCanChooseDirectories: NO];
    [openPanel setTitle: @"Select fortune program"];

    ret = [openPanel runModalForDirectory: 
             [[NSBundle mainBundle] resourcePath] file: nil types: nil];
    
    if (ret == NSOKButton)
        [pathField setStringValue: [[openPanel filenames] objectAtIndex: 0]];
}

- (void)showOpenDatfilesPanel:(id)sender
{
    NSOpenPanel *openPanel = [NSOpenPanel openPanel];
    int ret;
    
    [openPanel setAllowsMultipleSelection: NO];
    [openPanel setCanChooseFiles: NO];
    [openPanel setCanChooseDirectories: YES];
    [openPanel setTitle: @"Select directory with fortune datafiles"];
    
    ret = [openPanel runModalForDirectory: 
             [[NSBundle mainBundle] resourcePath] file: nil types: nil];
    
    if (ret == NSOKButton)
        [datfilesPathField setStringValue: [[openPanel filenames] objectAtIndex: 0]];
}

- (void)showFontPanel:(id)sender
{
   //[prefsPanel makeFirstResponder: fontField];
   [prefsPanel makeFirstResponder: prefsPanel];
   [[NSFontManager sharedFontManager] setSelectedFont: [mainDoc fortuneFont]
                                      isMultiple: NO];
   [[NSFontManager sharedFontManager] orderFrontFontPanel: self];
}

- (void)changeFont:(id)fontManager
{
    [newFont autorelease];
    newFont = [[fontManager convertFont: [fontManager selectedFont]] retain];
    fontChanged = YES;
    [fontField setStringValue: [NSString stringWithFormat:@"%@ %g",
                                  [newFont fontName], [newFont pointSize]]];
    [fontField display];
}

- (void)offensiveClicked:(id)sender
{
   return;
}

- (void)useBundledFortuneClicked:(id)sender
{
   BOOL useBundled;
   
   useBundled = (([useBundledFortuneCheckBox state] == NSOnState) ? YES : NO);

   [pathField setEditable: !useBundled];
   [pathField setSelectable: !useBundled];
   [selectFortuneButton setEnabled: !useBundled];

   if (useBundled)
   {
      NSString* whereIsFortune =
         [[NSBundle mainBundle] pathForResource: @"fortune" ofType: @""];
         
      if (!whereIsFortune)
      {
         NSRunAlertPanel(@"Un-Fortunate Error",
                         @"No fortune program bundled with Fortunate!",
                         @"Ok",
                         nil,
                         nil);
         [useBundledFortuneCheckBox setState: NSOffState];
         return;
      }
      
      [pathField setStringValue: whereIsFortune];
   }
   else
   {
      [pathField setStringValue:
          [[NSUserDefaults standardUserDefaults] stringForKey: kFortuneLocation]];
   }
}

- (void)useBundledDatfilesClicked:(id)sender
{
   BOOL useBundled;
   
   useBundled = (([useBundledDatfilesCheckBox state] == NSOnState) ? YES : NO);

   [datfilesPathField setEditable: !useBundled];
   [datfilesPathField setSelectable: !useBundled];
   [selectDatfilesButton setEnabled: !useBundled];

   if (useBundled)
   {
      NSString* whereAreDatfiles =
         [[NSBundle mainBundle] pathForResource: @"datfiles" ofType: @""];
         
      if (!whereAreDatfiles)
      {
         NSRunAlertPanel(@"Un-Fortunate Error",
                         @"No datafiles bundled with Fortunate!",
                         @"Ok",
                         nil,
                         nil);
         [useBundledDatfilesCheckBox setState: NSOffState];
         return;
      }
      
      [datfilesPathField setStringValue: whereAreDatfiles];
   }
   else
   {
      [datfilesPathField setStringValue:
          [[NSUserDefaults standardUserDefaults] stringForKey: kDatfilesLocation]];
   }
}

@end