1 | //␊ |
2 | // PreferencesControllerBase.mm␊ |
3 | // ChameleonPrefPane␊ |
4 | //␊ |
5 | // Created by Rekursor on 1/22/10.␊ |
6 | //␊ |
7 | ␊ |
8 | #import "PreferencesControllerBase.h"␊ |
9 | #import "KernOptionsParser.h"␊ |
10 | #include <string>␊ |
11 | #include <list>␊ |
12 | #include <map>␊ |
13 | #include <ctype.h>␊ |
14 | ␊ |
15 | //--------------------------------------------------------------------------␊ |
16 | static std::list< id<GroupControllerProtocol> > groupList;␊ |
17 | ␊ |
18 | // for unix-like options types␊ |
19 | static std::map<void*, std::string> IdToUCmdList;␊ |
20 | static KernOptionsParser kernelFlags;␊ |
21 | ␊ |
22 | //--------------------------------------------------------------------------␊ |
23 | ␊ |
24 | ␊ |
25 | @implementation PreferencesControllerBase␊ |
26 | ␊ |
27 | -(ChameleonPrefPane*) chameleon { return [ChameleonPrefPane instance]; }␊ |
28 | ␊ |
29 | ␊ |
30 | //--------------------------------------------------------------------------␊ |
31 | - (id) init␊ |
32 | {␊ |
33 | ␉self = [super init];␊ |
34 | ␉␊ |
35 | ␉[PreferencesControllerBase registerPreferencesGroup: self];␊ |
36 | ␉␊ |
37 | ␉return self;␊ |
38 | }␉␊ |
39 | //--------------------------------------------------------------------------␊ |
40 | - (id) getResourcePath: (NSString *) str ofType: (NSString*) sType␊ |
41 | {␊ |
42 | ␉if(!str) return nil;␊ |
43 | ␉NSBundle * b = [NSBundle bundleForClass:[self class]];␊ |
44 | ␉id sRes = [b pathForResource: str ofType:sType ];␊ |
45 | ␉return sRes;␊ |
46 | }␊ |
47 | ␊ |
48 | ␊ |
49 | //--------------------------------------------------------------------------␊ |
50 | // get authorisation from main panel lock␊ |
51 | - (AuthorizationRef) getAuthorization␊ |
52 | {␊ |
53 | ␉AuthorizationRef auth= [[self chameleon] isUnlocked] ? ␊ |
54 | ␉[[ [self chameleon]->authView authorization] authorizationRef] : NULL;␊ |
55 | ␉return auth;␊ |
56 | }␊ |
57 | //--------------------------------------------------------------------------␊ |
58 | + (void) registerPreferencesGroup:(id) myGroup␊ |
59 | {␊ |
60 | ␉groupList.push_back(myGroup);␊ |
61 | }␊ |
62 | ␊ |
63 | - (void) synchronizeTextContent:(const BootOptionDesc*) bod withValue:(int) val␊ |
64 | {␊ |
65 | ␉if(!bod || !bod->contentID) return;␊ |
66 | ␉id item = (id) bod->contentID;␊ |
67 | if ( [item isKindOfClass: [NSTextField class]] ||␊ |
68 | ␉␉[item isKindOfClass: [NSComboBox class]])␊ |
69 | ␉{␊ |
70 | ␉␉[item setEnabled: val];␊ |
71 | ␉␉[item setEditable: val];␊ |
72 | ␉}␊ |
73 | ␉␊ |
74 | }␊ |
75 | //--------------------------------------------------------------------------␊ |
76 | + (void) refreshLockState: (id) item␊ |
77 | {␊ |
78 | ␉const BootOptionDesc * bod = BootProp::instance().findOption(item);␊ |
79 | ␉if(!bod) bod=BootProp::instance().findOptionContent(item);␊ |
80 | ␉if(bod) [PreferencesControllerBase␉refreshBodLockState: bod];␊ |
81 | ␉else␉[item setEnabled: [[ChameleonPrefPane instance] isUnlocked]];␊ |
82 | }␊ |
83 | //--------------------------------------------------------------------------␊ |
84 | + (void) refreshBodLockState: (const BootOptionDesc*) bod␊ |
85 | {␊ |
86 | ␉if (!bod) return;␊ |
87 | ␉bool isUnlocked = [[ChameleonPrefPane instance] isUnlocked];␊ |
88 | ␉if (bod->ID) [(id) bod->ID setEnabled: isUnlocked];␊ |
89 | ␉int val = bod->ID ? [(id) bod->ID intValue] : 0;␊ |
90 | if ( [(id) bod->contentID isKindOfClass: [NSTextField class]] ||␊ |
91 | ␉␉[(id) bod->contentID isKindOfClass: [NSComboBox class]])␊ |
92 | ␉{␊ |
93 | ␉␉[(id) bod->contentID setEnabled: (isUnlocked && val) ];␊ |
94 | ␉␉[(id) bod->contentID setEditable: (isUnlocked && val) ];␊ |
95 | ␉}␊ |
96 | ␉else␊ |
97 | ␉␉[(id) bod->contentID setEnabled: isUnlocked];␊ |
98 | }␊ |
99 | ␊ |
100 | //--------------------------------------------------------------------------␊ |
101 | + (void) refreshLockStates␊ |
102 | {␊ |
103 | ␉for (const BootOptionDesc* bod=BootProp::instance().firstOption(); ␊ |
104 | ␉␉ bod; ␊ |
105 | ␉␉ bod=BootProp::instance().nextOption())␊ |
106 | ␉{␊ |
107 | ␉␉[self refreshLockState: (id) bod->ID ];␊ |
108 | ␉␉if (bod->contentID) [self refreshLockState: (id) bod->contentID ];␊ |
109 | ␉}␊ |
110 | ␉␊ |
111 | }␊ |
112 | //--------------------------------------------------------------------------␊ |
113 | + (void) doForEachGroup: (GroupAction) action withOption:(id) option␊ |
114 | {␊ |
115 | ␉std::list< id<GroupControllerProtocol> >::iterator it;␊ |
116 | ␉for (it=groupList.begin(); it!=groupList.end(); it++)␊ |
117 | ␉{␊ |
118 | ␉␉switch (action) {␊ |
119 | ␉␉␉case SetDefaultValues:␊ |
120 | ␉␉␉␉[*it setDefaultsValues: option];␊ |
121 | ␉␉␉␉break;␊ |
122 | ␉␉␉case RefreshLockStates:␊ |
123 | ␉␉␉␉[*it refreshLockStates ];␊ |
124 | ␉␉␉␉[PreferencesControllerBase refreshLockStates];␊ |
125 | ␉␉␉␉break;␊ |
126 | ␉␉␉case LoadPreferencesOptions:␊ |
127 | ␉␉␉␉[*it loadOptionsFromPreferencesFile: option];␊ |
128 | ␉␉␉␉break;␊ |
129 | ␉␉␉case LoadBootConfigOptions:␊ |
130 | ␉␉␉␉[PreferencesControllerBase loadOptionsFromBootFile];␊ |
131 | ␉␉␉␉break;␊ |
132 | ␉␉␉case AddOptionsDesc:␊ |
133 | ␉␉␉␉[*it addOptionsDesc];␊ |
134 | ␉␉␉␉break;␊ |
135 | ␉␉␉case SaveBootConfigOptions:␊ |
136 | ␉␉␉␉break;␊ |
137 | ␉␉␉default:␊ |
138 | ␉␉␉␉break;␊ |
139 | ␉␉}␊ |
140 | ␉}␊ |
141 | }␊ |
142 | ␉␉ ␊ |
143 | //--------------------------------------------------------------------------␊ |
144 | + (void) loadOptionsFromBootFile␊ |
145 | {␊ |
146 | ␉// parse unix like command string:␊ |
147 | ␉kernelFlags.parseOptions(BootProp::instance().getStringForKey(kKernelFlags));␊ |
148 | ␉␊ |
149 | ␉for (const BootOptionDesc* bod=BootProp::instance().firstOption(); ␊ |
150 | ␉␉ bod; ␊ |
151 | ␉␉ bod=BootProp::instance().nextOption())␊ |
152 | ␉{␊ |
153 | ␉␉[PreferencesControllerBase loadOptionFromBootFile:(id)bod->ID ];␊ |
154 | ␉}␊ |
155 | }␊ |
156 | ␊ |
157 | //--------------------------------------------------------------------------␊ |
158 | ␊ |
159 | + (void) loadOptionFromBootFile:(id) optionID␊ |
160 | {␊ |
161 | ␉const BootOptionDesc* bod = BootProp::instance().findOption(optionID);␊ |
162 | ␉if (!bod) ␊ |
163 | ␉{␊ |
164 | ␉␉NSRunAlertPanel(@"Error Parsing Option",@"loadOptionFromBootFile failed",@"OK", nil, nil);␊ |
165 | ␉␉return;␊ |
166 | ␉} ␊ |
167 | ␉␊ |
168 | ␉const char * stringForKey = BootProp::instance().getStringForKey(bod->Name);␊ |
169 | ␉std::string s = stringForKey ? trim(stringForKey) : "";␊ |
170 | ␉std::string def = trim(bod->Default ? bod->Default : "");␊ |
171 | ␉␊ |
172 | ␉switch (bod->Type) ␊ |
173 | ␉{␊ |
174 | ␉␉case OptionYesNo:␊ |
175 | ␉␉␉if (s.length()>0) ␊ |
176 | ␉␉␉␉[(NSButton*)optionID setIntValue: (toupper(s[0])=='Y' ? 1 : 0 ) ];␊ |
177 | ␉␉␉else␊ |
178 | ␉␉␉␉[(NSButton*)optionID setIntValue: (toupper(def[0])=='Y' ? 1 : 0 ) ];␊ |
179 | ␉␉␉break;␊ |
180 | ␉␉␉␊ |
181 | ␉␉case OptionKernel1:␊ |
182 | ␉␉{␊ |
183 | ␉␉␉int val = (s.length()>0 ? 1 : 0 );␊ |
184 | ␉␉␉[(NSButton*)optionID setIntValue: val ];␊ |
185 | ␉␉␉[(NSTextField*) bod->contentID setStringValue: ␊ |
186 | ␉␉␉ [[NSString alloc] initWithUTF8String: s.c_str()] ];␊ |
187 | ␉␉␉[(NSTextField*) bod->contentID setEnabled: val ? true : false]; ␊ |
188 | ␉␉␉[(NSTextField*) bod->contentID setEditable: val ? true : false]; ␊ |
189 | ␉␉}␊ |
190 | ␉␉␉break;␊ |
191 | ␉␉case OptionFileString:␊ |
192 | ␉␉case OptionString:␊ |
193 | ␉␉{␊ |
194 | ␉␉␉int val = (s.length()>0 ? 1 : 0 );␊ |
195 | ␉␉␉[(NSButton*)optionID setIntValue: val ];␊ |
196 | ␉␉␉[(NSTextField*) bod->contentID setStringValue: ␊ |
197 | ␉␉␉␉[[NSString alloc] initWithUTF8String: s.c_str()] ];␊ |
198 | ␉␉␉[(NSTextField*) bod->contentID setEnabled: val ? true: false]; ␊ |
199 | ␉␉␉[(NSTextField*) bod->contentID setEditable: val ? true : false]; ␊ |
200 | ␉␉}␊ |
201 | ␉␉␉break;␊ |
202 | ␉␉␉␊ |
203 | ␉␉case OptionUnix:␊ |
204 | ␉␉case OptionKernel:␊ |
205 | ␉␉{␊ |
206 | ␉␉␉std::string s = kernelFlags.stringFromKey(bod->Name);␊ |
207 | ␉␉␉if (s.length()>0)␊ |
208 | ␉␉␉{␊ |
209 | ␉␉␉␉[(NSButton*)optionID setIntValue: 1 ];␊ |
210 | ␉␉␉␉if(bod->Type==OptionKernel)␊ |
211 | ␉␉␉␉{␊ |
212 | ␉␉␉␉␉[(NSTextField*) bod->contentID setStringValue: ␊ |
213 | ␉␉␉␉␉ [[NSString alloc] initWithUTF8String: ␊ |
214 | ␉␉␉␉␉␉kernelFlags.rightMember(s).c_str()] ];␊ |
215 | ␉␉␉␉␉[(NSTextField*) bod->contentID setEnabled: true]; ␊ |
216 | ␉␉␉␉␉[(NSTextField*) bod->contentID setEditable: true]; ␊ |
217 | ␉␉␉␉}␊ |
218 | ␊ |
219 | ␉␉␉}␊ |
220 | ␉␉␉else␊ |
221 | ␉␉␉{ // set the default for thiso option ␊ |
222 | ␉␉␉␉[(NSButton*)optionID setIntValue: (bod->Default[0] ? 1 :0) ];␊ |
223 | ␉␉␉␉if(bod->Type==OptionKernel)␊ |
224 | ␉␉␉␉{␊ |
225 | ␉␉␉␉␉[(NSTextField*) bod->contentID setEnabled: false]; ␊ |
226 | ␉␉␉␉␉[(NSTextField*) bod->contentID setEditable: false]; ␊ |
227 | ␉␉␉␉}␊ |
228 | ␊ |
229 | ␉␉␉}␊ |
230 | ␉␉}␊ |
231 | ␉␉␉break;␊ |
232 | ␉␉default:␊ |
233 | ␉␉␉break;␊ |
234 | ␉}␊ |
235 | ␉␊ |
236 | }␊ |
237 | ␊ |
238 | //--------------------------------------------------------------------------␊ |
239 | - (void) loadPreferences␊ |
240 | {␊ |
241 | ␉[ [ChameleonPrefPane instance] loadPreferences];␊ |
242 | }␊ |
243 | ␊ |
244 | //--------------------------------------------------------------------------␊ |
245 | - (bool) savePreferences␊ |
246 | {␊ |
247 | ␉return [ [ChameleonPrefPane instance] savePreferences: [self preferencesFile] ];␊ |
248 | ␉␊ |
249 | }␊ |
250 | //--------------------------------------------------------------------------␊ |
251 | // update the boot Config with one option change and its associated desc ␊ |
252 | - (bool) saveBootConfig: (id) sender withBootOptionDesc: (BootOptionDesc*) bod␊ |
253 | {␊ |
254 | ␉if(!bod) ␊ |
255 | ␉{␊ |
256 | ␉␉return false;␊ |
257 | ␉}␊ |
258 | ␉// load boot config file so that we don't risk to loose␊ |
259 | ␉// externally modified parameters␊ |
260 | ␉␊ |
261 | ␉int val = [(NSButton*) sender intValue ];␊ |
262 | ␉std::string sDefaultValue = trim(bod->Default ? bod->Default : "");␊ |
263 | ␉bool status = false;␊ |
264 | ␉std::string name = trim(bod->Name);␊ |
265 | ␉␊ |
266 | ␉switch (bod->Type) {␊ |
267 | ␉␉case OptionYesNo:␊ |
268 | ␉␉{␊ |
269 | ␉␉␉std::string sVal = val ? "Yes" : "No"; ␊ |
270 | ␉␉␉if (sDefaultValue.length()==0) sDefaultValue␉= "No";␊ |
271 | ␉␉␉// Avoid populating bootConfig with unnecessary options:␊ |
272 | ␉␉␉if (sVal == sDefaultValue) ␊ |
273 | ␉␉␉␉status = BootProp::instance().removeKeyAndValue(name.c_str());␊ |
274 | ␉␉␉else␊ |
275 | ␉␉␉␉status = BootProp::instance().setStringForKey(name, sVal.c_str());␊ |
276 | ␉␉}␊ |
277 | ␉␉␉break;␊ |
278 | ␉␉case OptionUnix:␊ |
279 | ␉␉␉if (!val)␉kernelFlags.removeFlag(name);␊ |
280 | ␉␉␉else␉␉kernelFlags.addFlag(name);␊ |
281 | ␉␉␉BootProp::instance().setStringForKey(kKernelFlags,kernelFlags.options());␊ |
282 | ␉␉␉status = true;␊ |
283 | ␉␉␉break;␊ |
284 | ␉␉case OptionKernel:␊ |
285 | ␉␉{␊ |
286 | ␉␉␉std::string contentValue = trim(␊ |
287 | ␉␉␉[ [(NSTextField*) bod->contentID stringValue] UTF8String ]);␊ |
288 | ␉␉␉kernelFlags.removeFlag(kernelFlags.stringFromKey(bod->Name));␊ |
289 | ␉␉␉if(val && contentValue.length()>0)␊ |
290 | ␉␉␉{␊ |
291 | ␉␉␉␉std::string concat = trim(name);␊ |
292 | ␉␉␉␉concat+= "=";␊ |
293 | ␉␉␉␉concat+= trim(contentValue);␊ |
294 | ␉␉␉␉␊ |
295 | ␉␉␉␉kernelFlags.addFlag(concat);␊ |
296 | ␉␉␉}␊ |
297 | ␉␉␉BootProp::instance().setStringForKey(kKernelFlags,kernelFlags.options());␊ |
298 | ␉␉␉status = true;␊ |
299 | ␉␉}␊ |
300 | ␉␉␉break;␊ |
301 | ␉␉case OptionKernel1:␊ |
302 | ␉␉case OptionFileString:␊ |
303 | ␉␉case OptionString:␊ |
304 | ␉␉␉// Avoid populating bootConfig with unnecessary options:␊ |
305 | ␉␉␉if (val == 0 && bod->Type!=OptionKernel1) ␊ |
306 | ␉␉␉␉status = BootProp::instance().removeKeyAndValue(bod->Name);␊ |
307 | ␉␉␉else␊ |
308 | ␉␉␉{␊ |
309 | ␉␉␉␉std::string contentValue =␊ |
310 | ␉␉␉␉␉[ [(NSTextField*) bod->contentID stringValue] UTF8String ];␊ |
311 | ␉␉␉␉if (contentValue.length()>0)␊ |
312 | ␉␉␉␉␉status = BootProp::instance().setStringForKey(bod->Name, contentValue.c_str());␊ |
313 | ␉␉␉␉else {␊ |
314 | ␉␉␉␉␉return false; // no content to save so don't save it␊ |
315 | ␉␉␉␉}␊ |
316 | ␊ |
317 | ␉␉␉}␊ |
318 | ␉␉␉break;␊ |
319 | ␉␉default:␊ |
320 | ␉␉␉break;␊ |
321 | ␉}␊ |
322 | ␉␊ |
323 | ␉// Now save the bootConfig␊ |
324 | ␉AuthorizationRef auth = [self getAuthorization ];␊ |
325 | ␉if (status)␉status = BootProp::instance().save(auth);␊ |
326 | ␊ |
327 | ␉return status;␊ |
328 | }␊ |
329 | //--------------------------------------------------------------------------␊ |
330 | -(NSMutableDictionary*) preferencesFile␊ |
331 | {␉␊ |
332 | ␉return [[ChameleonPrefPane instance] preferencesFile];␊ |
333 | }␊ |
334 | ␊ |
335 | //--------------------------------------------------------------------------␊ |
336 | -(NSMutableDictionary*) preferencesParts ␊ |
337 | { ␊ |
338 | ␉return [[ChameleonPrefPane instance] preferencesParts];␊ |
339 | }␊ |
340 | ␊ |
341 | //--------------------------------------------------------------------------␊ |
342 | - (bool) handleSender: (id) sender␊ |
343 | {␊ |
344 | ␊ |
345 | ␉const BootOptionDesc * bod = BootProp::instance().findOption(sender);␊ |
346 | ␉␊ |
347 | ␉if (!bod) {␊ |
348 | ␉␉bod = BootProp::instance().findOptionContent(sender);␊ |
349 | ␉␉NSTextField* textField = (NSTextField*) sender;␊ |
350 | ␉␉std::string content = [[textField stringValue] UTF8String ];␊ |
351 | ␉␉if(bod->ID!=nil) sender = (id) bod->ID;␊ |
352 | ␉}␊ |
353 | ␉else␊ |
354 | ␉{␊ |
355 | ␉␉␊ |
356 | ␉␉int state = [sender intValue];␊ |
357 | ␉␊ |
358 | ␉␉switch (bod->Type) {␊ |
359 | ␉␉␉case OptionKernel:␊ |
360 | ␉␉␉case OptionKernel1:␊ |
361 | ␉␉␉case OptionFileString:␊ |
362 | ␉␉␉case OptionString:␊ |
363 | ␉␉␉[(NSTextField*) bod->contentID setEnabled: state ? true : false];␊ |
364 | ␉␉␉[(NSTextField*) bod->contentID setEditable: state ? true : false];␊ |
365 | ␉␉␉if (state && bod->Type==OptionFileString)␊ |
366 | ␉␉␉{␊ |
367 | ␉␉␉␉NSString* f = [self selectAnyFile ];␊ |
368 | ␉␉␉␉if (f) ␊ |
369 | ␉␉␉␉{␊ |
370 | ␉␉␉␉␉[(NSTextField*)bod->contentID setStringValue: f];␊ |
371 | ␉␉␉␉␉[f release];␉␊ |
372 | ␉␉␉␉}␊ |
373 | ␉␉␉}␊ |
374 | ␉␉␉[self synchronizeTextContent: bod withValue: state];␊ |
375 | ␉␉␉␉break;␊ |
376 | ␉␉␉default:␊ |
377 | ␉␉␉␉break;␊ |
378 | ␉␉}␊ |
379 | ␉}␉␊ |
380 | ␉if(![self saveBootConfig: sender withBootOptionDesc: (BootOptionDesc*) bod] && !bod->contentID )␊ |
381 | ␉{ // Couldn't save, so warn user ...␊ |
382 | ␉␉NSRunAlertPanel(@"Error saving bootConfig", @"Could not save org.chameleon.Boot.plist",␊ |
383 | ␉␉␉␉␉␉␉@"OK", nil, nil);␊ |
384 | ␉}␊ |
385 | ␉return true;␊ |
386 | }␊ |
387 | ␊ |
388 | - (bool) executeTaskAndWaitForTermination: (NSString*)taskPath␊ |
389 | {␊ |
390 | ␉return [ self executeTaskAndWaitForTermination:taskPath withArgs:nil ];␊ |
391 | }␊ |
392 | ␊ |
393 | - (bool) executeTaskAndWaitForTermination: (NSString*)taskPath withArgs:(NSArray*) args␊ |
394 | {␊ |
395 | ␉NSTask *task = [[NSTask alloc] init];␊ |
396 | ␉[task setLaunchPath:taskPath];␊ |
397 | ␉if (args) [task setArguments: args];␊ |
398 | ␉[task launch];␊ |
399 | ␉␊ |
400 | ␉[task waitUntilExit]; // wait for process termination␊ |
401 | ␉int status = [task terminationStatus];␊ |
402 | ␉␊ |
403 | ␉return (status == 0) ? true : false;␊ |
404 | }␊ |
405 | ␊ |
406 | -(NSString*) selectDirectory:(NSString*) dirName␊ |
407 | {␊ |
408 | ␉NSOpenPanel *oPanel = [NSOpenPanel openPanel]; ␊ |
409 | [oPanel setAllowsMultipleSelection:NO];␊ |
410 | ␉[oPanel setCanChooseFiles:NO];␊ |
411 | ␉[oPanel setCanChooseDirectories:YES]; ␊ |
412 | ␉[oPanel setDirectoryURL:[NSURL fileURLWithPath:dirName isDirectory: YES] ]; ␊ |
413 | ␊ |
414 | ␉BOOL result = [oPanel runModal];␊ |
415 | if (result == NSOKButton) ␊ |
416 | ␉{␊ |
417 | NSArray *dirToOpen = [oPanel filenames];␊ |
418 | int count = [dirToOpen count];␊ |
419 | if(count>0)␊ |
420 | ␉␉{␊ |
421 | NSString *aDir = [dirToOpen objectAtIndex:0];␊ |
422 | ␉␉␉return aDir;␊ |
423 | ␉␉}␊ |
424 | }␊ |
425 | ␉ return nil;␊ |
426 | }␊ |
427 | ␊ |
428 | - (NSString*) selectPlistFile:(NSString*) name␊ |
429 | {␊ |
430 | ␉NSArray* fileTypes = [NSArray arrayWithObjects: @"plist", nil];␊ |
431 | ␉NSString * s= [self selectFileWithFileTypes: @"/" withName: name ␊ |
432 | ␉␉␉␉␉␉␉ withTypes: fileTypes];␊ |
433 | ␉[fileTypes release];␊ |
434 | ␉return s;␊ |
435 | }␊ |
436 | ␊ |
437 | - (NSString*) selectAnyFile␊ |
438 | {␊ |
439 | ␉return [self selectFileWithFileTypes: @"/" withName: nil withTypes:nil];␊ |
440 | }␊ |
441 | ␊ |
442 | - (NSString*) selectFileWithFileTypes:(NSString*) dir withName: (NSString*) name withTypes:(NSArray*) fileTypes␊ |
443 | {␊ |
444 | int result;␊ |
445 | NSOpenPanel *oPanel = [NSOpenPanel openPanel];␊ |
446 | [oPanel setAllowsMultipleSelection:NO];␊ |
447 | ␉[oPanel setCanChooseFiles:YES];␊ |
448 | ␉[oPanel setCanChooseDirectories:NO];␊ |
449 | ␉[oPanel setAllowedFileTypes: fileTypes];␊ |
450 | ␉if (dir!=nil && [dir length] > 0 ) ␊ |
451 | ␉␉[oPanel setDirectoryURL:[NSURL fileURLWithPath:dir isDirectory: YES] ]; ␊ |
452 | ␉[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"AppleShowAllFiles"];␊ |
453 | ␊ |
454 | result = [oPanel runModal];␊ |
455 | if (result == NSOKButton) ␊ |
456 | ␉{␊ |
457 | NSArray *filesToOpen = [oPanel filenames];␊ |
458 | int count = [filesToOpen count];␊ |
459 | if(count>0)␊ |
460 | ␉␉{␊ |
461 | NSString *aFile = [filesToOpen objectAtIndex:0];␊ |
462 | ␉␉␉return aFile;␊ |
463 | ␉␉␉␊ |
464 | ␉␉}␊ |
465 | }␊ |
466 | ␊ |
467 | ␉return nil;␊ |
468 | }␊ |
469 | ␊ |
470 | -(void) msgBoxInfo:(NSString*) msg␊ |
471 | {␊ |
472 | ␉NSAlert *alert = [[NSAlert alloc] init];␊ |
473 | ␉[alert setAlertStyle: NSInformationalAlertStyle];␊ |
474 | ␉[alert setMessageText:msg ];␊ |
475 | ␉[alert runModal];␊ |
476 | ␉[alert release];␊ |
477 | }␊ |
478 | ␊ |
479 | -(void) msgBoxError:(NSString*) msg␊ |
480 | {␊ |
481 | ␉NSAlert *alert = [[NSAlert alloc] init];␊ |
482 | ␉[alert setAlertStyle: NSCriticalAlertStyle];␊ |
483 | ␉[alert setMessageText: msg ];␊ |
484 | ␉[alert runModal];␊ |
485 | ␉[alert release];␊ |
486 | }␊ |
487 | ␊ |
488 | @end␊ |
489 | |