File: PreferencesController.m

package info (click to toggle)
gnustep-examples 1%3A1.2.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,952 kB
  • ctags: 270
  • sloc: objc: 14,381; makefile: 65
file content (369 lines) | stat: -rw-r--r-- 10,375 bytes parent folder | download | duplicates (7)
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
/*
 *  PreferencesController.m: Finger.app Preferences Panel
 *
 *  Copyright (c) 2000 Free Software Foundation, Inc.
 *  
 *  Author: Nicola Pero
 *  Date: February 2000
 *
 *  This sample program is part of GNUstep.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#import <FingerIncludeAll.h>
#import "PreferencesController.h"

/* We cache here the shared file manager for speed */
static NSFileManager *fm;

/*
 * The Preferences Panel
 *
 */
static NSBox * 
setup_box (NSString *command, NSTextField *field, int tag, id target)
{
  NSBox *box;
  NSButton *button;
  GSHbox *hbox;

  [field setStringValue: @"Very Big, Long Long Sentence"];
  [field sizeToFit];
  [field setStringValue: @""];
  [field setDelegate: target];
  [field setTag: tag];
  [field setEditable: YES];
  [field setAutoresizingMask: (NSViewWidthSizable 
			       | NSViewMinYMargin | NSViewMaxYMargin)];

  button = AUTORELEASE ([NSButton new]);
  [button setTitle: @"File Panel..."];
  [button sizeToFit];
  [button setAutoresizingMask: NSViewNotSizable];
  [button setTag: tag];
  [button setTarget: target];
  [button setAction: @selector(changePreference:)];

  hbox = AUTORELEASE ([GSHbox new]);
  [hbox setBorder: 0];
  [hbox setDefaultMinXMargin: 8];
  [hbox addView: field];
  [hbox addView: button enablingXResizing: NO];
  [hbox setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin];

  box = AUTORELEASE ([NSBox new]);
  [box setTitlePosition: NSAtTop];
  [box setTitle: command];
  [box setBorderType: NSGrooveBorder];
  [box addSubview: hbox];
  [box sizeToFit];
  [box setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)];  
  return box;
}

@implementation PreferencesController

-(id)init
{
  GSVbox *vbox;
  NSBox *box;
  GSHbox *hbox, *bshbox;
  NSButton *defaultButton;
  NSRect panFrame;
  NSSize buttonSize;

  if (fm == nil)
    {
      fm = [NSFileManager defaultManager];
    }

  vbox = AUTORELEASE ([GSVbox new]);
  [vbox setBorder: 10];
  [vbox setDefaultMinYMargin: 10];
  [vbox setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];

  /* Buttons */
  hbox = AUTORELEASE ([GSHbox new]);
  [hbox setDefaultMinXMargin: 10];

  defaultButton = AUTORELEASE ([NSButton new]);
  [defaultButton setTitle: @"Default"];
  [defaultButton sizeToFit];
  buttonSize = [defaultButton frame].size;
  [defaultButton setAutoresizingMask: NSViewMaxXMargin];
  [defaultButton setTarget: self];
  [defaultButton setAction: @selector (resetToDefault:)];

#if SET_BUTTON
  setButton = AUTORELEASE ([NSButton new]);
  [setButton setTitle: @"Set"];
  [setButton sizeToFit];
  [setButton setAutoresizingMask: NSViewMinXMargin];
  [setButton setTarget: self];
  /* We have a nice trick here.  When the user presses the 'set'
     button, the first responder is changed, so that editing ends -- and
     this saves the setting.  We need to do nothing more.  */
  [setButton setAction: NULL];

  if ([setButton frame].size.width > buttonSize.width)
    {
      buttonSize.width = [setButton frame].size.width;
    }

  [setButton setFrameSize: buttonSize];

#endif 
  
  [defaultButton setFrameSize: buttonSize];

  [hbox addView: defaultButton];
#if SET_BUTTON
  [hbox addView: setButton]; 
#endif

  [hbox setAutoresizingMask: NSViewWidthSizable];
  [vbox addView: hbox
	enablingYResizing: NO];

  /* Preferences */

  buttonsSize = AUTORELEASE ([NSPopUpButton new]);
  [buttonsSize setPullsDown: NO];
  [buttonsSize addItemWithTitle: @"Large"];
  [buttonsSize addItemWithTitle: @"Small"];
  [buttonsSize setTarget: self];
  [buttonsSize setAction: @selector(set:)];
  [buttonsSize sizeToFit];
  [buttonsSize setAutoresizingMask: (NSViewWidthSizable | NSViewMinYMargin 
				     | NSViewMaxYMargin)];

  bshbox = AUTORELEASE ([GSHbox new]);
  [bshbox setBorder: 0];
  [bshbox setDefaultMinXMargin: 8];
  [bshbox addView: buttonsSize];
  [bshbox setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin];  

  box = AUTORELEASE ([NSBox new]);
  [box setTitlePosition: NSAtTop];
  [box setTitle: @"Button Size"];
  [box setBorderType: NSGrooveBorder];
  [box addSubview: bshbox];
  [box sizeToFit];
  [box setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)];  

  [vbox addView: box];

  tracerouteCommand = AUTORELEASE ([NSTextField new]);
  box = setup_box (@"Traceroute Command", tracerouteCommand, 
		   TRACEROUTE_TAG, self);
  [vbox addView: box];

  pingCommand = AUTORELEASE ([NSTextField new]);
  box = setup_box (@"Ping Command", pingCommand, PING_TAG, self);
  [vbox addView: box];

  whoisCommand = AUTORELEASE ([NSTextField new]);
  box = setup_box (@"Whois Command", whoisCommand, WHOIS_TAG, self);
  [vbox addView: box];

  fingerCommand = AUTORELEASE ([NSTextField new]);
  box = setup_box (@"Finger Command", fingerCommand, FINGER_TAG, self);
  [vbox addView: box];

  [fingerCommand setNextText: whoisCommand];
  [whoisCommand setNextText: pingCommand];
  [pingCommand setNextText: tracerouteCommand];
  [tracerouteCommand setNextText: fingerCommand];

  panFrame.size = [vbox frame].size;
  panFrame.origin = NSMakePoint (150, 150);
  
  pan = [[NSPanel alloc] initWithContentRect: panFrame
			 styleMask: (NSTitledWindowMask 
				     | NSClosableWindowMask
				     | NSResizableWindowMask)	
			 backing: NSBackingStoreBuffered
			 defer: YES];
  [pan setTitle: @"Preferences"];
  [pan setContentView: vbox];
  // Set min Size ?
  return [super init];
}
-(void) reset
{   
  NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
  
  [fingerCommand setStringValue: [ud stringForKey: @"FingerCommand"]];
  [whoisCommand setStringValue: [ud stringForKey: @"WhoisCommand"]];
  [pingCommand setStringValue: [ud stringForKey: @"PingCommand"]];
  [tracerouteCommand setStringValue: [ud stringForKey: 
					   @"TracerouteCommand"]];
  [buttonsSize selectItemWithTitle: [ud stringForKey: @"ButtonSize"]];
}
-(void) resetToDefault:(id)sender
{
  NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

  [ud removeObjectForKey: @"FingerCommand"];
  [ud removeObjectForKey: @"WhoisCommand"];
  [ud removeObjectForKey: @"PingCommand"];
  [ud removeObjectForKey: @"TracerouteCommand"];
  [ud removeObjectForKey: @"ButtonSize"];
  [self reset];
}
-(void) set:(id)sender
{
  NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

  [ud setObject: [fingerCommand stringValue] forKey: @"FingerCommand"];
  [ud setObject: [whoisCommand stringValue] forKey: @"WhoisCommand"];
  [ud setObject: [pingCommand stringValue] forKey: @"PingCommand"];
  [ud setObject: [tracerouteCommand stringValue] forKey: 
	@"TracerouteCommand"];
  [ud setObject: [buttonsSize titleOfSelectedItem] forKey: @"ButtonSize"];
  [ud synchronize];
}
-(void) changePreference: (id)sender
{
  NSOpenPanel *openPanel;
  NSString    *file;
  int result;

  openPanel = [NSOpenPanel openPanel];
  
  [openPanel setTitle: @"Choose Command"];
  [openPanel setAllowsMultipleSelection: NO];

  switch ([sender tag]) 
    {
    case PING_TAG: 
      [openPanel setPrompt: @"Ping Command:"];
      file = [pingCommand stringValue];
      break;
    case WHOIS_TAG:
      [openPanel setPrompt: @"Whois Command:"];
      file = [whoisCommand stringValue];
      break;
    case FINGER_TAG:
      [openPanel setPrompt: @"Finger Command:"];
      file = [fingerCommand stringValue];
      break;
    case TRACEROUTE_TAG:
      [openPanel setPrompt: @"Traceroute Command:"];
      file = [tracerouteCommand stringValue];
      break;
    default:
      return;
    }

  [openPanel setDelegate: self];

  result = [openPanel 
	     runModalForDirectory: [file stringByDeletingLastPathComponent]
	     file: [file lastPathComponent]
	     types: nil];

  if (result == NSOKButton)
    {
      switch ([sender tag])
	{
	case PING_TAG: 
	  [pingCommand setStringValue: [openPanel filename]];
	  break;
        case WHOIS_TAG:
          [whoisCommand setStringValue: [openPanel filename]];
          break;
	case FINGER_TAG:
	  [fingerCommand setStringValue: [openPanel filename]];
	  break;
	case TRACEROUTE_TAG:
	  [tracerouteCommand setStringValue: [openPanel filename]];
	  break;
	}
      [self set: self];
    }
}
- (void)controlTextDidEndEditing: (NSNotification *)aNotification
{
  NSTextField *sender = (NSTextField *)[aNotification object];
  NSString  *filename = [sender stringValue];
  BOOL      bip, bop;

  bip = [fm fileExistsAtPath: filename isDirectory: &bop]; 
  
  if (bip)
    bip = [fm isExecutableFileAtPath: filename]; 
  
  if ((bip == YES) && (bop == NO))
    {
#if SET_BUTTON
      [setButton performClick: self];
#endif
      [self set: self];
    }
  else
    {
      NSString *alert;
      NSString *pref;
      NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

      alert = [filename stringByAppendingString: @" is not executable!"];
      NSRunAlertPanel (NULL, alert, @"OK", NULL, NULL);
      switch ([sender tag])
	{
	case PING_TAG: 
	  pref = @"PingCommand";
	  break;
        case WHOIS_TAG:
          pref = @"WhoisCommand";
          break;
	case FINGER_TAG:
	  pref = @"FingerCommand";
	  break;
	case TRACEROUTE_TAG:
	  pref = @"TracerouteCommand";
	  break;
	default:
	  return;
	}
      [sender setStringValue: [ud stringForKey: pref]];
    }
}
-(void) orderFrontPanel
{
  [self reset];
  [pan orderFront: self];
}
-(void) dealloc
{
  RELEASE (pan);
  [super dealloc];
}
- (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename
{
  /* NB: We show directories here */
  return [fm isExecutableFileAtPath: filename]; 
}
- (BOOL)panel:(id)sender isValidFilename:(NSString *)filename
{
  BOOL bip, bop;

  /* Do not accept not existing files or directories */
  bip = [fm fileExistsAtPath: filename isDirectory: &bop]; 
  return ((bip) && (!bop));
}
@end