1 | //␊ |
2 | // StartupPrefPanePref.mm␊ |
3 | //␊ |
4 | // Created by Rekursor on 1/16/10.␊ |
5 | //␊ |
6 | ␊ |
7 | #import "ChameleonPrefPane.h"␊ |
8 | #import "BootSetupController.h"␊ |
9 | #import "BootFlagsController.h"␊ |
10 | #import "PeripheralsController.h"␊ |
11 | #import "AdvancedSetupController.h"␊ |
12 | #import "TableViewsController.h"␊ |
13 | ␊ |
14 | #include "ShellProcess.h"␊ |
15 | #import "ChameleonPropertyList.h"␊ |
16 | #include <string>␊ |
17 | ␊ |
18 | //--------------------------------------------------------------------------␊ |
19 | // Constants␊ |
20 | //--------------------------------------------------------------------------␊ |
21 | static const char * const szBootPaths[]= {␊ |
22 | ␉"/",␊ |
23 | ␉"/Extra/",␊ |
24 | ␉"/Volumes/EFI/Extra/",␊ |
25 | ␉"/Volumes/Cham/Extra/",␊ |
26 | ␉"/Volumes/BootLoaders/Extra/",␊ |
27 | ␉"/Volumes/RX0/Extra/",␊ |
28 | ␉"/Library/Preferences/SystemConfiguration/",␊ |
29 | ␉NULL␊ |
30 | };␊ |
31 | ␊ |
32 | static const char* const szPropFileName = "com.apple.Boot.plist";␊ |
33 | static const int CurrentPreferencesFileVersion = 0x02; // for future back compatibility␊ |
34 | ␊ |
35 | // TODO move table views handling code to a dedicated controller␊ |
36 | ␊ |
37 | //--------------------------------------------------------------------------␊ |
38 | //--------------------------------------------------------------------------␊ |
39 | // Static file variables␊ |
40 | //--------------------------------------------------------------------------␊ |
41 | static std::string sCurrentDefaultPartition;␊ |
42 | ␊ |
43 | static ChameleonPrefPane * _prefPaneInstance = NULL;␊ |
44 | ␊ |
45 | ␊ |
46 | //--------------------------------------------------------------------------␊ |
47 | ␊ |
48 | @implementation ChameleonPrefPane␊ |
49 | ␊ |
50 | + (ChameleonPrefPane *)instance { return(_prefPaneInstance);}␊ |
51 | ␊ |
52 | //--------------------------------------------------------------------------␊ |
53 | - (id) init␊ |
54 | {␊ |
55 | ␉self = [super init];␊ |
56 | ␉_prefPaneInstance = self;␊ |
57 | ␉␊ |
58 | ␉␊ |
59 | ␉␊ |
60 | ␉␊ |
61 | ␉// Retrieve the com.chameleon.prefPane.plist config␊ |
62 | ␉return self;␊ |
63 | }␊ |
64 | - (void)dealloc␊ |
65 | {␊ |
66 | ␉// release the colors␊ |
67 | ␉[mOptionsDict release];␊ |
68 | ␉[mPartitionsDict release];␊ |
69 | ␉␊ |
70 | ␉[super dealloc];␊ |
71 | }␊ |
72 | ␊ |
73 | - (NSMutableDictionary*) preferencesFile {␉return mOptionsDict; }␊ |
74 | - (NSMutableDictionary*) preferencesParts {␉return mPartitionsDict; }␊ |
75 | ␊ |
76 | //--------------------------------------------------------------------------␊ |
77 | -(bool) savePreferences: (NSDictionary*) dict␊ |
78 | {␊ |
79 | ␉std::string sPath = [kPreferencesFilePath UTF8String];␊ |
80 | ␉␊ |
81 | ␉if(dict==nil || sPath.length()==0) return false;␊ |
82 | ␉␊ |
83 | ␉AuthorizationRef auth = [self isUnlocked] ? [[authView authorization] authorizationRef] : NULL;␊ |
84 | ␉␊ |
85 | ␉PropertyList::chmodFile(sPath.c_str(), "0777", auth);␊ |
86 | ␉[dict writeToFile:kPreferencesFilePath atomically:YES];␊ |
87 | ␉PropertyList::chmodFile(sPath.c_str(), "0644", auth);␊ |
88 | ␉return true;␊ |
89 | }␊ |
90 | ␊ |
91 | //--------------------------------------------------------------------------␊ |
92 | // SFAuthorization implementation␊ |
93 | //--------------------------------------------------------------------------␊ |
94 | ␊ |
95 | // SFAuthorization delegates␊ |
96 | - (void)authorizationViewDidAuthorize:(SFAuthorizationView *)view {␊ |
97 | [self selectDefaultPartition];␊ |
98 | ␉[self refreshLockStates];␊ |
99 | ␊ |
100 | } ␊ |
101 | ␊ |
102 | //--------------------------------------------------------------------------␊ |
103 | - (void)authorizationViewDidDeauthorize:(SFAuthorizationView *)view {␊ |
104 | [self refreshLockStates];␊ |
105 | }␊ |
106 | ␊ |
107 | //--------------------------------------------------------------------------␊ |
108 | // Setup security for changing boot options␊ |
109 | -(void) initAuthorization␊ |
110 | {␊ |
111 | AuthorizationItem items = {kAuthorizationRightExecute, 0, NULL, 0};␊ |
112 | AuthorizationRights rights = {1, &items};␊ |
113 | ␊ |
114 | ␉[authView setAuthorizationRights:&rights];␊ |
115 | authView.delegate = self;␊ |
116 | [authView updateStatus:nil];␊ |
117 | }␊ |
118 | //--------------------------------------------------------------------------␊ |
119 | - (␉AuthorizationRef) auth␊ |
120 | {␊ |
121 | ␉return [self isUnlocked] ? [[authView authorization] authorizationRef] : NULL;␊ |
122 | ␉␊ |
123 | }␊ |
124 | ␊ |
125 | //--------------------------------------------------------------------------␊ |
126 | - (void) refreshLockStates␊ |
127 | {␊ |
128 | [[TableViewsController instance]->mPartitionsTable setEnabled:[self isUnlocked]];␊ |
129 | [mStatusText setEnabled:[self isUnlocked]];␊ |
130 | ␉␊ |
131 | ␉// Refresh other panels␊ |
132 | ␉[PreferencesControllerBase doForEachGroup: RefreshLockStates withOption: nil];␊ |
133 | }␊ |
134 | ␊ |
135 | ␊ |
136 | //--------------------------------------------------------------------------␊ |
137 | - (bool)isUnlocked ␊ |
138 | {␊ |
139 | return [authView authorizationState] == SFAuthorizationViewUnlockedState;␊ |
140 | }␊ |
141 | ␊ |
142 | //--------------------------------------------------------------------------␊ |
143 | /** When called here, all outlets references are initialized */␊ |
144 | - (void)awakeFromNib␊ |
145 | { // called more than once, we only need one resource init␊ |
146 | ␉static bool ft=true;␊ |
147 | ␉if(ft)␊ |
148 | ␉{␊ |
149 | ␉␉ft=false;␊ |
150 | ␉␉[self␉loadPreferences];␊ |
151 | ␉␉[self initBootConfig];␊ |
152 | ␉}␊ |
153 | }␊ |
154 | ␊ |
155 | //--------------------------------------------------------------------------␊ |
156 | - (void) loadPreferences␊ |
157 | {␊ |
158 | //␉PrefsProp::instance().open([kPreferencesFilePath UTF8String], CreateIfNotExist,␊ |
159 | //␉␉␉␉␉␉␉␉␉␉␉ [self auth]);␊ |
160 | ␉␊ |
161 | ␉// test with preferences file already created and when no prefs exists␊ |
162 | ␉␊ |
163 | ␉id oldGlobalPreferences = [ [NSDictionary dictionaryWithContentsOfFile: ␊ |
164 | ␉␉␉␉␉␉␉␉ kPreferencesFilePath ] retain];␊ |
165 | ␉␊ |
166 | ␉mPartitionsDict = [[NSMutableDictionary alloc] init];␊ |
167 | ␉[mPartitionsDict retain];␊ |
168 | ␉␊ |
169 | ␉// Initialize bootConfig desc dict␊ |
170 | ␉[PreferencesControllerBase doForEachGroup: AddOptionsDesc withOption: nil];␊ |
171 | ␉␊ |
172 | ␉if (oldGlobalPreferences!=nil)␊ |
173 | ␉{␊ |
174 | ␉␉mPreferenceFileVersion= [[oldGlobalPreferences objectForKey: keyPreferencesFileVersion] intValue ];␊ |
175 | ␉␉[PreferencesControllerBase doForEachGroup: LoadPreferencesOptions withOption: oldGlobalPreferences];␊ |
176 | ␉␉[mPartitionsDict addEntriesFromDictionary: [oldGlobalPreferences objectForKey: keyPartitionsList] ];␊ |
177 | ␉}␊ |
178 | ␉else ␊ |
179 | ␉{ // Create a preference plist file with Defaults values␊ |
180 | ␉␉oldGlobalPreferences = [[NSMutableDictionary alloc] init];␊ |
181 | ␉␉[PreferencesControllerBase doForEachGroup: SetDefaultValues withOption: oldGlobalPreferences];␊ |
182 | ␉␉␊ |
183 | ␉␉// Initialize defaults␊ |
184 | ␉␉[oldGlobalPreferences setObject: [[NSNumber alloc] initWithInt: CurrentPreferencesFileVersion] ␊ |
185 | ␉␉␉␉␉␉␉␉ forKey: keyPreferencesFileVersion];␊ |
186 | ␉␉[oldGlobalPreferences setObject: mPartitionsDict forKey: keyPartitionsList];␊ |
187 | ␉␉␊ |
188 | ␉␉// Save the preferences file␊ |
189 | ␉␉[ self savePreferences:oldGlobalPreferences ];␊ |
190 | ␉}␊ |
191 | ␉␊ |
192 | ␉mOptionsDict = [[NSMutableDictionary alloc] init];␊ |
193 | ␉[mOptionsDict addEntriesFromDictionary:oldGlobalPreferences];␊ |
194 | ␉if (mPartitionsDict!=nil) [mPartitionsDict retain];␊ |
195 | ␉[oldGlobalPreferences release];␊ |
196 | }␊ |
197 | ␊ |
198 | //--------------------------------------------------------------------------␊ |
199 | - (void) initBootConfig␊ |
200 | {␊ |
201 | ␉static bool ft=true;␊ |
202 | ␊ |
203 | ␉[self initAuthorization];␊ |
204 | ␉␊ |
205 | ␉if (!BootProp::instance().isValid())␊ |
206 | ␉{␊ |
207 | ␉␉std::string sPath;␊ |
208 | ␉␉CFStringRef errorString=NULL;␊ |
209 | ␉␉bool cont =true;␊ |
210 | ␉␉␊ |
211 | ␉␉id sForcedPath = [mOptionsDict valueForKey: keyForceBootConfigPath];␊ |
212 | ␉␉const char * szForcedPath = sForcedPath!=nil ? [sForcedPath UTF8String] : NULL;␊ |
213 | ␉␉if (szForcedPath && *szForcedPath)␊ |
214 | ␉␉{␊ |
215 | ␉␉␉cont = !BootProp::instance().open(szForcedPath, [self auth]);␊ |
216 | ␉␉}␊ |
217 | ␉␉else {␊ |
218 | ␉␉␉for(int i=0; szBootPaths[i] && cont; i++)␊ |
219 | ␉␉␉{␊ |
220 | ␉␉␉␉sPath = szBootPaths[i];␊ |
221 | ␉␉␉␉sPath += szPropFileName;␊ |
222 | ␉␉␉␉cont = !BootProp::instance().open(sPath.c_str(), [self auth]);␊ |
223 | ␉␉␉}␊ |
224 | ␉␉}␊ |
225 | ␉␉if (cont)␊ |
226 | ␉␉{␊ |
227 | ␉␉␉if(!ft) return;␊ |
228 | ␉␉␉ft=false;␊ |
229 | ␉␉␉[mStatusText setTextColor: [NSColor redColor] ];␊ |
230 | ␉␉␉if (errorString)␊ |
231 | ␉␉␉␉[mStatusText setStringValue:[NSString stringWithFormat: @"Error while parsing com.apple.Boot.plist : %@",␊ |
232 | ␉␉␉␉␉␉␉␉␉␉␉ errorString] ];␊ |
233 | ␉␉␉else␊ |
234 | ␉␉␉␉[mStatusText setStringValue: @"Error while searching for com.apple.Boot.plist"];␊ |
235 | ␉␉␉␊ |
236 | ␉␉}␊ |
237 | ␉␉else␊ |
238 | ␉␉{ ␊ |
239 | ␉␉␉[mStatusText setTextColor: [NSColor grayColor] ];␊ |
240 | ␉␉␉NSString* ns = [ [NSString alloc] initWithUTF8String:BootProp::instance().propFilePath() ];␊ |
241 | ␉␉␉[mStatusText setStringValue: [NSString stringWithFormat: @"bootConfig: %@", ns] ];␊ |
242 | ␉␉␉[[BootSetupController instance]->mBootConfigPathText setStringValue: ␊ |
243 | ␉␉␉␉[[NSString alloc] initWithUTF8String: BootProp::instance().propFilePath()] ];␊ |
244 | ␊ |
245 | ␉␉}␊ |
246 | ␉␉␊ |
247 | ␉␉if (BootProp::instance().isValid())␊ |
248 | ␉␉{␊ |
249 | ␉␉␉// read options in the plist file␊ |
250 | ␉␉␉␊ |
251 | ␉␉␉PartitionExtractor::instance().hidePartitions(BootProp::instance().getStringForKey(kHidePartition));␊ |
252 | ␉␉␉PartitionExtractor::instance().renamedPartitions(BootProp::instance().getStringForKey(kRenamePartition));␊ |
253 | ␉␉␉␊ |
254 | ␉␉␉// PartitionExtractor::instance().resetSwapping();␊ |
255 | ␉␉␉id val = [mOptionsDict valueForKey: keySwapHD01];␊ |
256 | ␉␉␉[[BootSetupController instance]->mSwapHD01 setIntValue: [val intValue] ];␊ |
257 | ␉␉␉[[BootSetupController instance] doSwapHD: [val boolValue] save: false src:0 dst:1];␊ |
258 | ␉␉␉␊ |
259 | ␉␉␉val = [mOptionsDict valueForKey: keySwapHD02];␊ |
260 | ␉␉␉[[BootSetupController instance]->mSwapHD02 setIntValue: [val intValue] ];␊ |
261 | ␉␉␉[[BootSetupController instance] doSwapHD: [val boolValue] save: false src:0 dst:2];␊ |
262 | ␊ |
263 | ␉␉␉// Load all boot Options into the interface components␊ |
264 | ␉␉␉[PreferencesControllerBase loadOptionsFromBootFile];␊ |
265 | ␉␉␉[self selectDefaultPartition];␊ |
266 | ␉␉␉␉ ␊ |
267 | ␉␉}␊ |
268 | ␉␉␊ |
269 | ␉}␉␊ |
270 | }␊ |
271 | //--------------------------------------------------------------------------␊ |
272 | - (void) selectDefaultPartition␊ |
273 | {␊ |
274 | if(!authView) return;␊ |
275 | ␉␊ |
276 | ␉[self refreshLockStates];␊ |
277 | ␉␊ |
278 | ␉// try to get the current default partition if any␊ |
279 | ␉if(BootProp::instance().isValid())␊ |
280 | ␉{␊ |
281 | ␉␉const char *sdp = BootProp::instance().getStringForKey(kDefaultPartition);␊ |
282 | ␉␉sCurrentDefaultPartition = sdp ? sdp : ""; ␊ |
283 | ␉␉if (sCurrentDefaultPartition.size())␊ |
284 | ␉␉{␊ |
285 | ␉␉␉int index = PartitionExtractor::instance().getIndexFromHdStringSpec(sCurrentDefaultPartition.c_str());␊ |
286 | ␉␉␉if (index>=0)␊ |
287 | ␉␉␉{␊ |
288 | ␉␉␉␉[[TableViewsController instance ]->mPartitionsTable selectRowIndexes:␊ |
289 | ␉␉␉␉ [NSIndexSet indexSetWithIndex:index] byExtendingSelection:NO];␊ |
290 | ␉␉␉␉[TableViewsController instance ]->currentRowSel = index;␊ |
291 | ␉␉␉}␊ |
292 | ␉␉}␊ |
293 | ␉}␊ |
294 | ␉␊ |
295 | }␊ |
296 | ␊ |
297 | //--------------------------------------------------------------------------␊ |
298 | // following DieBuch recommendation : using applescript and system events (thanks!):␊ |
299 | - (IBAction)onRestart: (id)sender␊ |
300 | {␊ |
301 | ␉NSInteger n = NSRunAlertPanel(@"Restarting OS X", ␊ |
302 | ␉␉␉␉␉␉␉␉ @"Are you sure you want to restart your computer now ?",␊ |
303 | ␉␉␉␉␉␉␉␉ @"OK", @"Cancel", nil);␊ |
304 | ␉if (n==1)␊ |
305 | ␉{␊ |
306 | ␉␉AuthorizationRef auth = [[authView authorization] authorizationRef];␊ |
307 | ␉␉executePrivilegedCmd(auth,"/usr/bin/osascript","-e 'tell app \"System Events\" to restart'");␊ |
308 | ␉}␊ |
309 | ␉␊ |
310 | }␊ |
311 | //--------------------------------------------------------------------------␊ |
312 | - (IBAction)onShutdown: (id)sender␊ |
313 | {␊ |
314 | ␉NSInteger n = NSRunAlertPanel(@"Shutting Down OS X", ␊ |
315 | ␉␉␉␉␉␉␉␉ @"Are you sure you want to shut down your computer now ?",␊ |
316 | ␉␉␉␉␉␉␉␉ @"OK", @"Cancel", /*ThirdButtonHere:*/nil␊ |
317 | ␉␉␉␉␉␉␉␉ /*, args for a printf-style msg go here */);␊ |
318 | ␉if (n==1)␊ |
319 | ␉{␊ |
320 | ␉␉system("/usr/bin/osascript -e 'tell app \"System Events\" to shut down'");␊ |
321 | ␉}␊ |
322 | }␊ |
323 | ␊ |
324 | - (IBAction)onSleep: (id)sender␊ |
325 | {␊ |
326 | ␉system("/usr/bin/osascript -e 'tell app \"System Events\" to sleep'");␊ |
327 | }␊ |
328 | ␊ |
329 | //--------------------------------------------------------------------------␊ |
330 | ␊ |
331 | @end␊ |
332 | |