]>
Commit | Line | Data |
---|---|---|
224d48e3 | 1 | /* WinterBoard - Theme Manager for the iPhone |
900dc9a5 | 2 | * Copyright (C) 2008-2014 Jay Freeman (saurik) |
224d48e3 JF |
3 | */ |
4 | ||
900dc9a5 | 5 | /* GNU Lesser General Public License, Version 3 {{{ */ |
224d48e3 | 6 | /* |
900dc9a5 JF |
7 | * WinterBoard is free software: you can redistribute it and/or modify it under |
8 | * the terms of the GNU Lesser General Public License as published by the | |
9 | * Free Software Foundation, either version 3 of the License, or (at your | |
10 | * option) any later version. | |
224d48e3 | 11 | * |
900dc9a5 JF |
12 | * WinterBoard is distributed in the hope that it will be useful, but WITHOUT |
13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | |
15 | * License for more details. | |
224d48e3 | 16 | * |
900dc9a5 JF |
17 | * You should have received a copy of the GNU Lesser General Public License |
18 | * along with WinterBoard. If not, see <http://www.gnu.org/licenses/>. | |
19 | **/ | |
20 | /* }}} */ | |
224d48e3 JF |
21 | |
22 | #import <Foundation/Foundation.h> | |
23 | #import <UIKit/UIKit.h> | |
b39b993d DH |
24 | #import <Preferences/PSRootController.h> |
25 | #import <Preferences/PSViewController.h> | |
224d48e3 JF |
26 | #import <Preferences/PSListController.h> |
27 | #import <Preferences/PSSpecifier.h> | |
28 | #import <Preferences/PSTableCell.h> | |
29 | #import <UIKit/UINavigationButton.h> | |
30 | ||
7c88a3f1 | 31 | #include <dlfcn.h> |
b39b993d | 32 | #include <objc/runtime.h> |
7c88a3f1 | 33 | |
e0c66475 | 34 | static void *libhide; |
7c88a3f1 JF |
35 | static BOOL (*IsIconHiddenDisplayId)(NSString *); |
36 | static BOOL (*HideIconViaDisplayId)(NSString *); | |
37 | static BOOL (*UnHideIconViaDisplayId)(NSString *); | |
38 | ||
fdac1738 | 39 | static NSString *WinterBoardDisplayID = @"com.saurik.WinterBoard"; |
7c88a3f1 | 40 | |
224d48e3 JF |
41 | extern NSString *PSTableCellKey; |
42 | extern "C" UIImage *_UIImageWithName(NSString *); | |
43 | ||
44 | static UIImage *checkImage; | |
45 | static UIImage *uncheckedImage; | |
46 | ||
47 | static BOOL settingsChanged; | |
48 | static NSMutableDictionary *_settings; | |
49 | static NSString *_plist; | |
50 | ||
deed0017 JF |
51 | void AddThemes(NSMutableArray *themesOnDisk, NSString *folder) { |
52 | NSArray *themes([[NSFileManager defaultManager] contentsOfDirectoryAtPath:folder error:NULL]); | |
53 | for (NSString *theme in themes) { | |
54 | if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@/%@/Info.plist", folder, theme]]) { | |
55 | if (NSArray *version = [info objectForKey:@"CoreFoundationVersion"]) { | |
56 | size_t count([version count]); | |
57 | if (count == 0 || count > 2) | |
58 | continue; | |
59 | ||
60 | double lower([[version objectAtIndex:0] doubleValue]); | |
61 | if (kCFCoreFoundationVersionNumber < lower) | |
62 | continue; | |
63 | ||
64 | if (count != 1) { | |
65 | double upper([[version objectAtIndex:1] doubleValue]); | |
66 | if (upper <= kCFCoreFoundationVersionNumber) | |
67 | continue; | |
68 | } | |
69 | } | |
70 | } | |
71 | ||
72 | [themesOnDisk addObject:theme]; | |
73 | } | |
74 | } | |
75 | ||
265e19b2 JF |
76 | /* [NSObject yieldToSelector:(withObject:)] {{{*/ |
77 | @interface NSObject (wb$yieldToSelector) | |
78 | - (id) wb$yieldToSelector:(SEL)selector withObject:(id)object; | |
79 | - (id) wb$yieldToSelector:(SEL)selector; | |
80 | @end | |
81 | ||
82 | @implementation NSObject (Cydia) | |
83 | ||
84 | - (void) wb$doNothing { | |
85 | } | |
86 | ||
87 | - (void) wb$_yieldToContext:(NSMutableArray *)context { | |
88 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); | |
89 | ||
90 | SEL selector(reinterpret_cast<SEL>([[context objectAtIndex:0] pointerValue])); | |
91 | id object([[context objectAtIndex:1] nonretainedObjectValue]); | |
92 | volatile bool &stopped(*reinterpret_cast<bool *>([[context objectAtIndex:2] pointerValue])); | |
93 | ||
94 | /* XXX: deal with exceptions */ | |
95 | id value([self performSelector:selector withObject:object]); | |
96 | ||
97 | NSMethodSignature *signature([self methodSignatureForSelector:selector]); | |
98 | [context removeAllObjects]; | |
99 | if ([signature methodReturnLength] != 0 && value != nil) | |
100 | [context addObject:value]; | |
101 | ||
102 | stopped = true; | |
103 | ||
104 | [self | |
105 | performSelectorOnMainThread:@selector(wb$doNothing) | |
106 | withObject:nil | |
107 | waitUntilDone:NO | |
108 | ]; | |
109 | ||
110 | [pool release]; | |
111 | } | |
112 | ||
113 | - (id) wb$yieldToSelector:(SEL)selector withObject:(id)object { | |
114 | /*return [self performSelector:selector withObject:object];*/ | |
115 | ||
116 | volatile bool stopped(false); | |
117 | ||
118 | NSMutableArray *context([NSMutableArray arrayWithObjects: | |
119 | [NSValue valueWithPointer:selector], | |
120 | [NSValue valueWithNonretainedObject:object], | |
121 | [NSValue valueWithPointer:const_cast<bool *>(&stopped)], | |
122 | nil]); | |
123 | ||
124 | NSThread *thread([[[NSThread alloc] | |
125 | initWithTarget:self | |
126 | selector:@selector(wb$_yieldToContext:) | |
127 | object:context | |
128 | ] autorelease]); | |
129 | ||
130 | [thread start]; | |
131 | ||
132 | NSRunLoop *loop([NSRunLoop currentRunLoop]); | |
133 | NSDate *future([NSDate distantFuture]); | |
134 | ||
135 | while (!stopped && [loop runMode:NSDefaultRunLoopMode beforeDate:future]); | |
136 | ||
137 | return [context count] == 0 ? nil : [context objectAtIndex:0]; | |
138 | } | |
139 | ||
140 | - (id) wb$yieldToSelector:(SEL)selector { | |
141 | return [self wb$yieldToSelector:selector withObject:nil]; | |
142 | } | |
143 | ||
144 | @end | |
145 | /* }}} */ | |
146 | ||
224d48e3 JF |
147 | /* Theme Settings Controller {{{ */ |
148 | @interface WBSThemesController: PSViewController <UITableViewDelegate, UITableViewDataSource> { | |
149 | UITableView *_tableView; | |
150 | NSMutableArray *_themes; | |
151 | } | |
152 | ||
153 | @property (nonatomic, retain) NSMutableArray *themes; | |
154 | ||
224d48e3 JF |
155 | - (id) initForContentSize:(CGSize)size; |
156 | - (id) view; | |
157 | - (id) navigationTitle; | |
158 | - (void) themesChanged; | |
159 | ||
160 | - (int) numberOfSectionsInTableView:(UITableView *)tableView; | |
161 | - (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section; | |
162 | - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section; | |
163 | - (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; | |
164 | - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; | |
165 | - (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath; | |
166 | - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath; | |
167 | - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath; | |
168 | - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath; | |
7c88a3f1 | 169 | |
224d48e3 JF |
170 | @end |
171 | ||
172 | @implementation WBSThemesController | |
173 | ||
174 | @synthesize themes = _themes; | |
175 | ||
06839c7d | 176 | + (void) initialize { |
224d48e3 JF |
177 | NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); |
178 | checkImage = [_UIImageWithName(@"UIPreferencesBlueCheck.png") retain]; | |
179 | uncheckedImage = [[UIImage imageWithContentsOfFile:@"/System/Library/PreferenceBundles/WinterBoardSettings.bundle/SearchResultsCheckmarkClear.png"] retain]; | |
180 | [pool release]; | |
181 | } | |
182 | ||
183 | - (id) initForContentSize:(CGSize)size { | |
184 | if ((self = [super initForContentSize:size]) != nil) { | |
185 | self.themes = [_settings objectForKey:@"Themes"]; | |
186 | if (!_themes) { | |
187 | if (NSString *theme = [_settings objectForKey:@"Theme"]) { | |
188 | self.themes = [NSMutableArray arrayWithObject: | |
189 | [NSMutableDictionary dictionaryWithObjectsAndKeys: | |
190 | theme, @"Name", | |
191 | [NSNumber numberWithBool:YES], @"Active", nil]]; | |
192 | [_settings removeObjectForKey:@"Theme"]; | |
193 | } | |
194 | if (!_themes) | |
195 | self.themes = [NSMutableArray array]; | |
196 | [_settings setObject:_themes forKey:@"Themes"]; | |
197 | } | |
198 | ||
199 | NSMutableArray *themesOnDisk([NSMutableArray array]); | |
deed0017 JF |
200 | AddThemes(themesOnDisk, @"/Library/Themes"); |
201 | AddThemes(themesOnDisk, [NSString stringWithFormat:@"%@/Library/SummerBoard/Themes", NSHomeDirectory()]); | |
224d48e3 JF |
202 | |
203 | for (int i = 0, count = [themesOnDisk count]; i < count; i++) { | |
204 | NSString *theme = [themesOnDisk objectAtIndex:i]; | |
205 | if ([theme hasSuffix:@".theme"]) | |
206 | [themesOnDisk replaceObjectAtIndex:i withObject:[theme stringByDeletingPathExtension]]; | |
207 | } | |
208 | ||
209 | NSMutableSet *themesSet([NSMutableSet set]); | |
210 | ||
211 | for (int i = 0, count = [_themes count]; i < count; i++) { | |
212 | NSDictionary *theme([_themes objectAtIndex:i]); | |
213 | NSString *name([theme objectForKey:@"Name"]); | |
214 | ||
215 | if (!name || ![themesOnDisk containsObject:name]) { | |
216 | [_themes removeObjectAtIndex:i]; | |
217 | i--; | |
218 | count--; | |
219 | } else { | |
220 | [themesSet addObject:name]; | |
221 | } | |
222 | } | |
223 | ||
224 | for (NSString *theme in themesOnDisk) { | |
225 | if ([themesSet containsObject:theme]) | |
226 | continue; | |
227 | [themesSet addObject:theme]; | |
228 | ||
229 | [_themes insertObject:[NSMutableDictionary dictionaryWithObjectsAndKeys: | |
230 | theme, @"Name", | |
231 | [NSNumber numberWithBool:NO], @"Active", | |
232 | nil] atIndex:0]; | |
233 | } | |
234 | ||
235 | _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-64) style:UITableViewStyleGrouped]; | |
236 | [_tableView setDataSource:self]; | |
237 | [_tableView setDelegate:self]; | |
238 | [_tableView setEditing:YES]; | |
239 | [_tableView setAllowsSelectionDuringEditing:YES]; | |
b39b993d DH |
240 | if ([self respondsToSelector:@selector(setView:)]) |
241 | [self setView:_tableView]; | |
224d48e3 JF |
242 | } |
243 | return self; | |
244 | } | |
245 | ||
246 | - (void) dealloc { | |
247 | [_tableView release]; | |
248 | [_themes release]; | |
249 | [super dealloc]; | |
250 | } | |
251 | ||
252 | - (id) navigationTitle { | |
253 | return @"Themes"; | |
254 | } | |
255 | ||
256 | - (id) view { | |
257 | return _tableView; | |
258 | } | |
259 | ||
260 | - (void) themesChanged { | |
261 | settingsChanged = YES; | |
262 | } | |
263 | ||
264 | /* UITableViewDelegate / UITableViewDataSource Methods {{{ */ | |
265 | - (int) numberOfSectionsInTableView:(UITableView *)tableView { | |
266 | return 1; | |
267 | } | |
268 | ||
269 | - (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section { | |
270 | return nil; | |
271 | } | |
272 | ||
273 | - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section { | |
274 | return _themes.count; | |
275 | } | |
276 | ||
277 | - (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
278 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThemeCell"]; | |
279 | if (!cell) { | |
280 | cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100) reuseIdentifier:@"ThemeCell"] autorelease]; | |
281 | //[cell setTableViewStyle:UITableViewCellStyleDefault]; | |
282 | } | |
283 | ||
284 | NSDictionary *theme([_themes objectAtIndex:indexPath.row]); | |
285 | cell.text = [theme objectForKey:@"Name"]; | |
286 | cell.hidesAccessoryWhenEditing = NO; | |
287 | NSNumber *active([theme objectForKey:@"Active"]); | |
288 | BOOL inactive(active == nil || ![active boolValue]); | |
289 | [cell setImage:(inactive ? uncheckedImage : checkImage)]; | |
290 | return cell; | |
291 | } | |
292 | ||
293 | - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { | |
294 | UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; | |
295 | NSMutableDictionary *theme = [_themes objectAtIndex:indexPath.row]; | |
296 | NSNumber *active = [theme objectForKey:@"Active"]; | |
297 | BOOL inactive = active == nil || ![active boolValue]; | |
298 | [theme setObject:[NSNumber numberWithBool:inactive] forKey:@"Active"]; | |
299 | [cell setImage:(!inactive ? uncheckedImage : checkImage)]; | |
300 | [tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES]; | |
301 | [self themesChanged]; | |
302 | } | |
303 | ||
304 | - (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { | |
305 | NSUInteger fromIndex = [fromIndexPath row]; | |
306 | NSUInteger toIndex = [toIndexPath row]; | |
307 | if (fromIndex == toIndex) | |
308 | return; | |
309 | NSMutableDictionary *theme = [[[_themes objectAtIndex:fromIndex] retain] autorelease]; | |
310 | [_themes removeObjectAtIndex:fromIndex]; | |
311 | [_themes insertObject:theme atIndex:toIndex]; | |
312 | [self themesChanged]; | |
313 | } | |
314 | ||
315 | - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { | |
316 | return UITableViewCellEditingStyleNone; | |
317 | } | |
318 | ||
319 | - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath { | |
320 | return NO; | |
321 | } | |
322 | ||
323 | - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { | |
324 | return YES; | |
325 | } | |
326 | /* }}} */ | |
327 | @end | |
328 | /* }}} */ | |
329 | ||
024f98ea | 330 | @interface WBAdvancedController: PSListController { |
224d48e3 JF |
331 | } |
332 | ||
224d48e3 JF |
333 | - (id) specifiers; |
334 | - (void) settingsChanged; | |
224d48e3 JF |
335 | |
336 | @end | |
337 | ||
024f98ea | 338 | @implementation WBAdvancedController |
224d48e3 | 339 | |
024f98ea JF |
340 | - (id) specifiers { |
341 | if (!_specifiers) | |
342 | _specifiers = [[self loadSpecifiersFromPlistName:@"Advanced" target:self] retain]; | |
343 | return _specifiers; | |
7c88a3f1 JF |
344 | } |
345 | ||
024f98ea JF |
346 | - (void) settingsChanged { |
347 | settingsChanged = YES; | |
348 | } | |
7c88a3f1 | 349 | |
024f98ea JF |
350 | - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec { |
351 | NSString *key([spec propertyForKey:@"key"]); | |
352 | if ([[spec propertyForKey:@"negate"] boolValue]) | |
353 | value = [NSNumber numberWithBool:(![value boolValue])]; | |
354 | [_settings setValue:value forKey:key]; | |
355 | [self settingsChanged]; | |
224d48e3 JF |
356 | } |
357 | ||
024f98ea JF |
358 | - (id) readPreferenceValue:(PSSpecifier *)spec { |
359 | NSString *key([spec propertyForKey:@"key"]); | |
360 | id defaultValue([spec propertyForKey:@"default"]); | |
361 | id plistValue([_settings objectForKey:key]); | |
362 | if (!plistValue) | |
363 | return defaultValue; | |
364 | if ([[spec propertyForKey:@"negate"] boolValue]) | |
365 | plistValue = [NSNumber numberWithBool:(![plistValue boolValue])]; | |
366 | return plistValue; | |
224d48e3 JF |
367 | } |
368 | ||
43fd3a0b | 369 | - (void) __optimizeThemes { |
265e19b2 JF |
370 | system("/usr/libexec/winterboard/Optimize"); |
371 | } | |
372 | ||
373 | - (void) optimizeThemes { | |
54a4cf27 | 374 | UIAlertView *alert([[[UIAlertView alloc] |
43fd3a0b | 375 | initWithTitle:@"Optimize Themes" |
54a4cf27 | 376 | message:@"Please note that this setting /replaces/ the PNG files that came with the theme. PNG files that have been iPhone-optimized cannot be viewed on a normal computer unless they are first deoptimized. You can use Cydia to reinstall themes that have been optimized in order to revert to the original PNG files." |
43fd3a0b | 377 | delegate:self |
54a4cf27 JF |
378 | cancelButtonTitle:@"Cancel" |
379 | otherButtonTitles:@"Optimize", nil | |
43fd3a0b JF |
380 | ] autorelease]); |
381 | ||
54a4cf27 JF |
382 | [alert setContext:@"optimize"]; |
383 | [alert setNumberOfRows:1]; | |
384 | [alert show]; | |
43fd3a0b JF |
385 | } |
386 | ||
387 | - (void) _optimizeThemes { | |
265e19b2 JF |
388 | UIView *view([self view]); |
389 | UIWindow *window([view window]); | |
390 | ||
391 | UIProgressHUD *hud([[[UIProgressHUD alloc] initWithWindow:window] autorelease]); | |
392 | [hud setText:@"Reticulating Splines\nPlease Wait (Minutes)"]; | |
393 | ||
394 | [window setUserInteractionEnabled:NO]; | |
395 | ||
396 | [window addSubview:hud]; | |
397 | [hud show:YES]; | |
43fd3a0b | 398 | [self wb$yieldToSelector:@selector(__optimizeThemes)]; |
265e19b2 JF |
399 | [hud removeFromSuperview]; |
400 | ||
401 | [window setUserInteractionEnabled:YES]; | |
402 | ||
403 | [self settingsChanged]; | |
404 | } | |
405 | ||
54a4cf27 JF |
406 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
407 | NSString *context([alert context]); | |
43fd3a0b JF |
408 | |
409 | if ([context isEqualToString:@"optimize"]) { | |
54a4cf27 JF |
410 | if (button == [alert firstOtherButtonIndex]) { |
411 | [self performSelector:@selector(_optimizeThemes) withObject:nil afterDelay:0]; | |
43fd3a0b JF |
412 | } |
413 | ||
54a4cf27 JF |
414 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
415 | } | |
416 | /*else if ([super respondsToSelector:@selector(alertView:clickedButtonAtIndex:)]) | |
417 | [super alertView:alert clickedButtonAtIndex:button];*/ | |
43fd3a0b JF |
418 | } |
419 | ||
024f98ea JF |
420 | @end |
421 | ||
422 | @interface WBSettingsController: PSListController { | |
423 | } | |
424 | ||
425 | - (id) initForContentSize:(CGSize)size; | |
426 | - (void) dealloc; | |
427 | - (void) suspend; | |
428 | - (void) navigationBarButtonClicked:(int)buttonIndex; | |
429 | - (void) viewWillRedisplay; | |
430 | - (void) pushController:(id)controller; | |
431 | - (id) specifiers; | |
432 | - (void) settingsChanged; | |
433 | - (NSString *) title; | |
434 | - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec; | |
435 | - (id) readPreferenceValue:(PSSpecifier *)spec; | |
436 | ||
437 | @end | |
438 | ||
439 | @implementation WBSettingsController | |
440 | ||
441 | + (void) load { | |
e0c66475 | 442 | libhide = dlopen("/usr/lib/hide.dylib", RTLD_LAZY); |
024f98ea JF |
443 | IsIconHiddenDisplayId = reinterpret_cast<BOOL (*)(NSString *)>(dlsym(libhide, "IsIconHiddenDisplayId")); |
444 | HideIconViaDisplayId = reinterpret_cast<BOOL (*)(NSString *)>(dlsym(libhide, "HideIconViaDisplayId")); | |
445 | UnHideIconViaDisplayId = reinterpret_cast<BOOL (*)(NSString *)>(dlsym(libhide, "UnHideIconViaDisplayId")); | |
446 | } | |
447 | ||
34b51da8 JF |
448 | - (void) _wb$loadSettings { |
449 | _plist = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.WinterBoard.plist", NSHomeDirectory()] retain]; | |
450 | _settings = [NSMutableDictionary dictionaryWithContentsOfFile:_plist]; | |
451 | ||
452 | bool set; | |
453 | if (_settings != nil) | |
454 | set = true; | |
455 | else { | |
456 | set = false; | |
457 | _settings = [NSMutableDictionary dictionary]; | |
458 | } | |
d236b808 | 459 | |
34b51da8 | 460 | _settings = [_settings retain]; |
d236b808 | 461 | |
34b51da8 JF |
462 | if ([_settings objectForKey:@"SummerBoard"] == nil) |
463 | [_settings setObject:[NSNumber numberWithBool:set] forKey:@"SummerBoard"]; | |
024f98ea | 464 | |
e0c66475 JF |
465 | if (libhide != NULL) |
466 | [_settings setObject:[NSNumber numberWithBool:IsIconHiddenDisplayId(WinterBoardDisplayID)] forKey:@"IconHidden"]; | |
34b51da8 JF |
467 | } |
468 | ||
469 | - (id) initForContentSize:(CGSize)size { | |
470 | if ((self = [super initForContentSize:size]) != nil) { | |
471 | [self _wb$loadSettings]; | |
024f98ea JF |
472 | } return self; |
473 | } | |
474 | ||
475 | - (void) dealloc { | |
476 | [_settings release]; | |
477 | [_plist release]; | |
478 | [super dealloc]; | |
479 | } | |
480 | ||
224d48e3 JF |
481 | - (void) suspend { |
482 | if (!settingsChanged) | |
483 | return; | |
484 | ||
485 | NSData *data([NSPropertyListSerialization dataFromPropertyList:_settings format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL]); | |
486 | if (!data) | |
487 | return; | |
488 | if (![data writeToFile:_plist options:NSAtomicWrite error:NULL]) | |
489 | return; | |
490 | ||
e0c66475 JF |
491 | if (libhide != NULL) |
492 | ([[_settings objectForKey:@"IconHidden"] boolValue] ? HideIconViaDisplayId : UnHideIconViaDisplayId)(WinterBoardDisplayID); | |
7c88a3f1 | 493 | |
224d48e3 JF |
494 | unlink("/User/Library/Caches/com.apple.springboard-imagecache-icons"); |
495 | unlink("/User/Library/Caches/com.apple.springboard-imagecache-icons.plist"); | |
496 | unlink("/User/Library/Caches/com.apple.springboard-imagecache-smallicons"); | |
497 | unlink("/User/Library/Caches/com.apple.springboard-imagecache-smallicons.plist"); | |
bbea033b | 498 | |
546a205b JF |
499 | unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen"); |
500 | unlink("/User/Library/Caches/com.apple.SpringBoard.notificationCenterLinen"); | |
bbea033b | 501 | |
546a205b JF |
502 | unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.0"); |
503 | unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.1"); | |
504 | unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.2"); | |
505 | unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.3"); | |
7c88a3f1 JF |
506 | |
507 | system("rm -rf /User/Library/Caches/SpringBoardIconCache"); | |
508 | system("rm -rf /User/Library/Caches/SpringBoardIconCache-small"); | |
f64efe8d | 509 | system("rm -rf /User/Library/Caches/com.apple.IconsCache"); |
546a205b | 510 | system("rm -rf /User/Library/Caches/com.apple.newsstand"); |
bbea033b | 511 | system("rm -rf /User/Library/Caches/com.apple.springboard.sharedimagecache"); |
7c88a3f1 | 512 | |
26bd2bf2 | 513 | system("killall -9 lsd"); |
c532a1cf JF |
514 | |
515 | if (kCFCoreFoundationVersionNumber > 700) // XXX: iOS 6.x | |
516 | system("killall backboardd"); | |
517 | else | |
518 | system("killall SpringBoard"); | |
224d48e3 JF |
519 | } |
520 | ||
b39b993d DH |
521 | - (void) cancelChanges { |
522 | [_settings release]; | |
523 | [_plist release]; | |
b39b993d | 524 | |
34b51da8 JF |
525 | [self _wb$loadSettings]; |
526 | ||
b39b993d DH |
527 | [self reloadSpecifiers]; |
528 | if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) { | |
1d3b613f JF |
529 | [[self navigationItem] setLeftBarButtonItem:nil]; |
530 | [[self navigationItem] setRightBarButtonItem:nil]; | |
838dbf4c DH |
531 | } else { |
532 | [self showLeftButton:nil withStyle:0 rightButton:nil withStyle:0]; | |
b39b993d DH |
533 | } |
534 | settingsChanged = NO; | |
535 | } | |
536 | ||
224d48e3 JF |
537 | - (void) navigationBarButtonClicked:(int)buttonIndex { |
538 | if (!settingsChanged) { | |
539 | [super navigationBarButtonClicked:buttonIndex]; | |
540 | return; | |
541 | } | |
542 | ||
838dbf4c | 543 | if (buttonIndex == 0) { |
b39b993d | 544 | [self cancelChanges]; |
838dbf4c DH |
545 | return; |
546 | } | |
224d48e3 JF |
547 | |
548 | [self suspend]; | |
549 | [self.rootController popController]; | |
550 | } | |
551 | ||
b39b993d DH |
552 | - (void) settingsConfirmButtonClicked:(UIBarButtonItem *)button { |
553 | [self navigationBarButtonClicked:button.tag]; | |
554 | } | |
555 | ||
224d48e3 JF |
556 | - (void) viewWillRedisplay { |
557 | if (settingsChanged) | |
558 | [self settingsChanged]; | |
559 | [super viewWillRedisplay]; | |
560 | } | |
561 | ||
b39b993d DH |
562 | - (void) viewWillAppear:(BOOL)animated { |
563 | if (settingsChanged) | |
564 | [self settingsChanged]; | |
1d3b613f JF |
565 | if ([super respondsToSelector:@selector(viewWillAppear:)]) |
566 | [super viewWillAppear:animated]; | |
b39b993d DH |
567 | } |
568 | ||
224d48e3 JF |
569 | - (void) pushController:(id)controller { |
570 | [self hideNavigationBarButtons]; | |
571 | [super pushController:controller]; | |
572 | } | |
573 | ||
574 | - (id) specifiers { | |
6e9f4e02 JF |
575 | if (!_specifiers) { |
576 | NSMutableArray *specifiers([NSMutableArray array]); | |
577 | for (PSSpecifier *specifier in [self loadSpecifiersFromPlistName:@"WinterBoard" target:self]) { | |
578 | if (NSArray *version = [specifier propertyForKey:@"wb$filter"]) { | |
579 | size_t count([version count]); | |
580 | if (count == 0 || count > 2) | |
581 | continue; | |
582 | ||
583 | double lower([[version objectAtIndex:0] doubleValue]); | |
584 | if (kCFCoreFoundationVersionNumber < lower) | |
585 | continue; | |
586 | ||
587 | if (count != 1) { | |
588 | double upper([[version objectAtIndex:1] doubleValue]); | |
589 | if (upper <= kCFCoreFoundationVersionNumber) | |
590 | continue; | |
591 | } | |
592 | } | |
593 | [specifiers addObject:specifier]; | |
594 | } | |
595 | _specifiers = [specifiers retain]; | |
596 | } | |
224d48e3 JF |
597 | return _specifiers; |
598 | } | |
599 | ||
600 | - (void) settingsChanged { | |
b39b993d DH |
601 | if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) { |
602 | UIBarButtonItem *respringButton([[UIBarButtonItem alloc] initWithTitle:@"Respring" style:UIBarButtonItemStyleDone target:self action:@selector(settingsConfirmButtonClicked:)]); | |
603 | UIBarButtonItem *cancelButton([[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(settingsConfirmButtonClicked:)]); | |
604 | cancelButton.tag = 0; | |
605 | respringButton.tag = 1; | |
1d3b613f JF |
606 | [[self navigationItem] setLeftBarButtonItem:respringButton]; |
607 | [[self navigationItem] setRightBarButtonItem:cancelButton]; | |
b39b993d DH |
608 | [respringButton release]; |
609 | [cancelButton release]; | |
610 | } else { | |
611 | [self showLeftButton:@"Respring" withStyle:2 rightButton:@"Cancel" withStyle:0]; | |
612 | } | |
224d48e3 JF |
613 | settingsChanged = YES; |
614 | } | |
615 | ||
616 | - (NSString *) title { | |
617 | return @"WinterBoard"; | |
618 | } | |
619 | ||
620 | - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec { | |
7c88a3f1 | 621 | NSString *key([spec propertyForKey:@"key"]); |
224d48e3 JF |
622 | if ([[spec propertyForKey:@"negate"] boolValue]) |
623 | value = [NSNumber numberWithBool:(![value boolValue])]; | |
7c88a3f1 | 624 | [_settings setValue:value forKey:key]; |
224d48e3 JF |
625 | [self settingsChanged]; |
626 | } | |
627 | ||
628 | - (id) readPreferenceValue:(PSSpecifier *)spec { | |
629 | NSString *key([spec propertyForKey:@"key"]); | |
630 | id defaultValue([spec propertyForKey:@"default"]); | |
631 | id plistValue([_settings objectForKey:key]); | |
632 | if (!plistValue) | |
633 | return defaultValue; | |
634 | if ([[spec propertyForKey:@"negate"] boolValue]) | |
635 | plistValue = [NSNumber numberWithBool:(![plistValue boolValue])]; | |
636 | return plistValue; | |
637 | } | |
638 | ||
639 | @end | |
b39b993d DH |
640 | |
641 | #define WBSAddMethod(_class, _sel, _imp, _type) \ | |
642 | if (![[_class class] instancesRespondToSelector:@selector(_sel)]) \ | |
643 | class_addMethod([_class class], @selector(_sel), (IMP)_imp, _type) | |
644 | void $PSRootController$popController(PSRootController *self, SEL _cmd) { | |
645 | [self popViewControllerAnimated:YES]; | |
646 | } | |
647 | ||
648 | void $PSViewController$hideNavigationBarButtons(PSRootController *self, SEL _cmd) { | |
649 | } | |
650 | ||
651 | id $PSViewController$initForContentSize$(PSRootController *self, SEL _cmd, CGRect contentSize) { | |
652 | return [self init]; | |
653 | } | |
654 | ||
655 | static __attribute__((constructor)) void __wbsInit() { | |
656 | WBSAddMethod(PSRootController, popController, $PSRootController$popController, "v@:"); | |
657 | WBSAddMethod(PSViewController, hideNavigationBarButtons, $PSViewController$hideNavigationBarButtons, "v@:"); | |
658 | WBSAddMethod(PSViewController, initForContentSize:, $PSViewController$initForContentSize$, "@@:{ff}"); | |
659 | } |