Chameleon Applications

Chameleon Applications Svn Source Tree

Root/trunk/ChameleonPrefPane/Sources/PreferencesControllerBase.mm

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//--------------------------------------------------------------------------
17static std::list<id> groupList;
18
19// for unix-like options types
20static std::map<void*, std::string> IdToUCmdList;
21static KernOptionsParser kernelFlags;
22
23//--------------------------------------------------------------------------
24
25
26@implementation PreferencesControllerBase
27
28-(ChameleonPrefPane*) chameleon { return [ChameleonPrefPane instance]; }
29
30
31//--------------------------------------------------------------------------
32- (id) init
33{
34self = [super init];
35
36[PreferencesControllerBase registerPreferencesGroup: self];
37
38return self;
39}
40
41//--------------------------------------------------------------------------
42// from the id to desc map in prop, set all default values for dict
43+ (void) setDefaultValues: (NSMutableDictionary*) dict
44{
45for(const BootOptionDesc* bod = prop->firstOption();bod; bod=prop->nextOption())
46{
47switch (bod->Type)
48{
49case OptionYesNo:
50[(NSButton*)bod->ID setIntValue: (toupper(bod->Default[0])=='Y' ? 1 : 0) ];
51break;
52case OptionKernel:
53case OptionString:
54[(NSButton*)bod->ID setIntValue: 0 ];
55case OptionKernel1:
56[(NSTextField*)bod->contentID setStringValue:
57[[NSString alloc] initWithUTF8String: bod->Default] ];
58break;
59case OptionUnix:
60[(NSButton*)bod->ID setIntValue: 0 ];
61break;
62
63default:
64break;
65}
66}
67}
68
69//--------------------------------------------------------------------------
70+ (void) registerPreferencesGroup:(id) myGroup
71{
72groupList.push_back(myGroup);
73}
74
75//--------------------------------------------------------------------------
76+ (void) doForEachGroup: (GroupAction) action withOption:(id) option
77{
78std::list<id>::iterator it;
79for (it=groupList.begin(); it!=groupList.end(); it++)
80{
81switch (action) {
82case SetDefaultValues:
83[*it setDefaultValues: option];
84break;
85case RefreshLockStates:
86[*it refreshLockStates ];
87break;
88case LoadPreferencesOptions:
89[*it loadOptionsFromPreferencesFile: option];
90break;
91case LoadBootConfigOptions:
92[*it loadOptionsFromBootFile];
93break;
94case AddOptionsDesc:
95[*it addOptionsDesc];
96break;
97case SaveBootConfigOptions:
98break;
99default:
100break;
101}
102}
103}
104
105//--------------------------------------------------------------------------
106+ (void) loadOptionsFromBootFile
107{
108// *** TODO
109// parse unix like command string:
110kernelFlags.parseOptions(prop->getStringForKey(kKernelFlags));
111
112for (const BootOptionDesc* bod=prop->firstOption(); bod; bod=prop->nextOption())
113{
114[PreferencesControllerBase loadOptionFromBootFile:(id)bod->ID ];
115}
116}
117
118//--------------------------------------------------------------------------
119
120+ (void) loadOptionFromBootFile:(id) optionID
121{
122const BootOptionDesc* bod = prop->findOption(optionID);
123if (!bod)
124{
125NSRunAlertPanel(@"Error Parsing Option",@"loadOptionFromBootFile failed",@"OK", nil, nil);
126return;
127}
128
129const char * stringForKey = prop->getStringForKey(bod->Name);
130std::string s = stringForKey ? trim(stringForKey) : "";
131
132switch (bod->Type)
133{
134case OptionYesNo:
135if (stringForKey!=NULL)
136[(NSButton*)optionID setIntValue: (toupper(s[0])=='Y' ? 1 : 0 ) ];
137break;
138
139case OptionKernel1:
140{
141int val = (s.length()>0 ? 1 : 0 );
142[(NSButton*)optionID setIntValue: val ];
143[(NSTextField*) bod->contentID setStringValue:
144 [[NSString alloc] initWithUTF8String: s.c_str()] ];
145[(NSTextField*) bod->contentID setEnabled: val ? true : false];
146[(NSTextField*) bod->contentID setEditable: val ? true : false];
147}
148break;
149case OptionString:
150[(NSButton*)optionID setIntValue: (s.length()>0 ? 1 : 0 ) ];
151[(NSTextField*) bod->contentID setStringValue:
152[[NSString alloc] initWithUTF8String: s.c_str()] ];
153break;
154
155case OptionUnix:
156case OptionKernel:
157{
158std::string s = kernelFlags.stringFromKey(bod->Name);
159if (s.length()>0)
160{
161[(NSButton*)optionID setIntValue: 1 ];
162if(bod->Type==OptionKernel)
163{
164[(NSTextField*) bod->contentID setStringValue:
165 [[NSString alloc] initWithUTF8String:
166kernelFlags.rightMember(s).c_str()] ];
167[(NSTextField*) bod->contentID setEnabled: true];
168[(NSTextField*) bod->contentID setEditable: true];
169}
170
171}
172else
173{ // set the default for thiso option
174[(NSButton*)optionID setIntValue: (bod->Default[0] ? 1 :0) ];
175if(bod->Type==OptionKernel)
176{
177[(NSTextField*) bod->contentID setEnabled: false];
178[(NSTextField*) bod->contentID setEditable: false];
179}
180
181}
182}
183break;
184default:
185break;
186}
187
188}
189//--------------------------------------------------------------------------
190- (void) refreshLockState: (id) item
191{
192 [item setEnabled:[[ChameleonPrefPane instance] isUnlocked]];
193
194}
195
196//--------------------------------------------------------------------------
197- (void) loadPreferences
198{
199[ [ChameleonPrefPane instance] loadPreferences];
200}
201
202//--------------------------------------------------------------------------
203- (bool) savePreferences
204{
205return [ [ChameleonPrefPane instance] savePreferences: [self preferencesFile] ];
206
207}
208//--------------------------------------------------------------------------
209-(NSMutableDictionary*) preferencesFile
210{
211return [[ChameleonPrefPane instance] preferencesFile];
212}
213
214//--------------------------------------------------------------------------
215-(NSMutableDictionary*) preferencesParts
216{
217return [[ChameleonPrefPane instance] preferencesParts];
218}
219
220//--------------------------------------------------------------------------
221- (bool) handleSender: (id) sender
222{
223
224const BootOptionDesc * bod = prop->findOption(sender);
225
226if (!bod) {
227NSTextField* textField = (NSTextField*) sender;
228std::string content = [[textField stringValue] UTF8String ];
229}
230else
231{
232
233int state = [sender intValue];
234
235switch (bod->Type) {
236case OptionKernel:
237case OptionKernel1:
238case OptionString:
239[(NSTextField*) bod->contentID setEnabled: state ? true : false];
240[(NSTextField*) bod->contentID setEditable: state ? true : false];
241break;
242default:
243break;
244}
245}
246return true;
247}
248
249@end
250

Archive Download this file

Revision: 48