]> git.saurik.com Git - winterboard.git/blame - Settings.mm
Commit @3x logic (as it is getting in my way :/).
[winterboard.git] / Settings.mm
CommitLineData
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 34static void *libhide;
7c88a3f1
JF
35static BOOL (*IsIconHiddenDisplayId)(NSString *);
36static BOOL (*HideIconViaDisplayId)(NSString *);
37static BOOL (*UnHideIconViaDisplayId)(NSString *);
38
fdac1738 39static NSString *WinterBoardDisplayID = @"com.saurik.WinterBoard";
7c88a3f1 40
224d48e3
JF
41extern NSString *PSTableCellKey;
42extern "C" UIImage *_UIImageWithName(NSString *);
43
44static UIImage *checkImage;
45static UIImage *uncheckedImage;
46
47static BOOL settingsChanged;
48static NSMutableDictionary *_settings;
49static NSString *_plist;
50
deed0017
JF
51void 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 147/* Theme Settings Controller {{{ */
f4b021a2
JF
148@interface WBSThemesTableViewCell : UITableViewCell {
149}
150
151@end
152
153@implementation WBSThemesTableViewCell
154
155- (id) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuse {
156 if ((self = [super initWithFrame:frame reuseIdentifier:reuse]) != nil) {
157 } return self;
158}
159
160- (void) setTheme:(NSDictionary *)theme {
161 self.text = [theme objectForKey:@"Name"];
162 self.hidesAccessoryWhenEditing = NO;
163 NSNumber *active([theme objectForKey:@"Active"]);
164 BOOL inactive(active == nil || ![active boolValue]);
165 [self setImage:(inactive ? uncheckedImage : checkImage)];
166}
167
168@end
169
224d48e3
JF
170@interface WBSThemesController: PSViewController <UITableViewDelegate, UITableViewDataSource> {
171 UITableView *_tableView;
172 NSMutableArray *_themes;
173}
174
175@property (nonatomic, retain) NSMutableArray *themes;
176
224d48e3
JF
177- (id) initForContentSize:(CGSize)size;
178- (id) view;
179- (id) navigationTitle;
180- (void) themesChanged;
181
182- (int) numberOfSectionsInTableView:(UITableView *)tableView;
183- (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section;
184- (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section;
185- (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
186- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
187- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath;
188- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
189- (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
190- (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
7c88a3f1 191
224d48e3
JF
192@end
193
194@implementation WBSThemesController
195
196@synthesize themes = _themes;
197
06839c7d 198+ (void) initialize {
224d48e3
JF
199 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
200 checkImage = [_UIImageWithName(@"UIPreferencesBlueCheck.png") retain];
201 uncheckedImage = [[UIImage imageWithContentsOfFile:@"/System/Library/PreferenceBundles/WinterBoardSettings.bundle/SearchResultsCheckmarkClear.png"] retain];
202 [pool release];
203}
204
205- (id) initForContentSize:(CGSize)size {
206 if ((self = [super initForContentSize:size]) != nil) {
207 self.themes = [_settings objectForKey:@"Themes"];
208 if (!_themes) {
209 if (NSString *theme = [_settings objectForKey:@"Theme"]) {
210 self.themes = [NSMutableArray arrayWithObject:
211 [NSMutableDictionary dictionaryWithObjectsAndKeys:
212 theme, @"Name",
213 [NSNumber numberWithBool:YES], @"Active", nil]];
214 [_settings removeObjectForKey:@"Theme"];
215 }
216 if (!_themes)
217 self.themes = [NSMutableArray array];
218 [_settings setObject:_themes forKey:@"Themes"];
219 }
220
221 NSMutableArray *themesOnDisk([NSMutableArray array]);
deed0017
JF
222 AddThemes(themesOnDisk, @"/Library/Themes");
223 AddThemes(themesOnDisk, [NSString stringWithFormat:@"%@/Library/SummerBoard/Themes", NSHomeDirectory()]);
224d48e3
JF
224
225 for (int i = 0, count = [themesOnDisk count]; i < count; i++) {
226 NSString *theme = [themesOnDisk objectAtIndex:i];
227 if ([theme hasSuffix:@".theme"])
228 [themesOnDisk replaceObjectAtIndex:i withObject:[theme stringByDeletingPathExtension]];
229 }
230
231 NSMutableSet *themesSet([NSMutableSet set]);
232
233 for (int i = 0, count = [_themes count]; i < count; i++) {
234 NSDictionary *theme([_themes objectAtIndex:i]);
235 NSString *name([theme objectForKey:@"Name"]);
236
237 if (!name || ![themesOnDisk containsObject:name]) {
238 [_themes removeObjectAtIndex:i];
239 i--;
240 count--;
241 } else {
242 [themesSet addObject:name];
243 }
244 }
245
246 for (NSString *theme in themesOnDisk) {
247 if ([themesSet containsObject:theme])
248 continue;
249 [themesSet addObject:theme];
250
251 [_themes insertObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
252 theme, @"Name",
253 [NSNumber numberWithBool:NO], @"Active",
254 nil] atIndex:0];
255 }
256
257 _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-64) style:UITableViewStyleGrouped];
258 [_tableView setDataSource:self];
259 [_tableView setDelegate:self];
260 [_tableView setEditing:YES];
261 [_tableView setAllowsSelectionDuringEditing:YES];
b39b993d
DH
262 if ([self respondsToSelector:@selector(setView:)])
263 [self setView:_tableView];
224d48e3
JF
264 }
265 return self;
266}
267
268- (void) dealloc {
269 [_tableView release];
270 [_themes release];
271 [super dealloc];
272}
273
274- (id) navigationTitle {
275 return @"Themes";
276}
277
278- (id) view {
279 return _tableView;
280}
281
282- (void) themesChanged {
283 settingsChanged = YES;
284}
285
286/* UITableViewDelegate / UITableViewDataSource Methods {{{ */
287- (int) numberOfSectionsInTableView:(UITableView *)tableView {
288 return 1;
289}
290
291- (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section {
292 return nil;
293}
294
295- (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section {
296 return _themes.count;
297}
298
299- (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
f4b021a2 300 WBSThemesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThemeCell"];
224d48e3 301 if (!cell) {
f4b021a2 302 cell = [[[WBSThemesTableViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100) reuseIdentifier:@"ThemeCell"] autorelease];
224d48e3
JF
303 //[cell setTableViewStyle:UITableViewCellStyleDefault];
304 }
305
306 NSDictionary *theme([_themes objectAtIndex:indexPath.row]);
f4b021a2 307 [cell setTheme:theme];
224d48e3
JF
308 return cell;
309}
310
311- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
312 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
313 NSMutableDictionary *theme = [_themes objectAtIndex:indexPath.row];
314 NSNumber *active = [theme objectForKey:@"Active"];
315 BOOL inactive = active == nil || ![active boolValue];
316 [theme setObject:[NSNumber numberWithBool:inactive] forKey:@"Active"];
317 [cell setImage:(!inactive ? uncheckedImage : checkImage)];
318 [tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES];
319 [self themesChanged];
320}
321
322- (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
323 NSUInteger fromIndex = [fromIndexPath row];
324 NSUInteger toIndex = [toIndexPath row];
325 if (fromIndex == toIndex)
326 return;
327 NSMutableDictionary *theme = [[[_themes objectAtIndex:fromIndex] retain] autorelease];
328 [_themes removeObjectAtIndex:fromIndex];
329 [_themes insertObject:theme atIndex:toIndex];
330 [self themesChanged];
331}
332
333- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
334 return UITableViewCellEditingStyleNone;
335}
336
337- (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
338 return NO;
339}
340
341- (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
342 return YES;
343}
344/* }}} */
345@end
346/* }}} */
347
024f98ea 348@interface WBAdvancedController: PSListController {
224d48e3
JF
349}
350
224d48e3
JF
351- (id) specifiers;
352- (void) settingsChanged;
224d48e3
JF
353
354@end
355
024f98ea 356@implementation WBAdvancedController
224d48e3 357
024f98ea
JF
358- (id) specifiers {
359 if (!_specifiers)
360 _specifiers = [[self loadSpecifiersFromPlistName:@"Advanced" target:self] retain];
361 return _specifiers;
7c88a3f1
JF
362}
363
024f98ea
JF
364- (void) settingsChanged {
365 settingsChanged = YES;
366}
7c88a3f1 367
024f98ea
JF
368- (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec {
369 NSString *key([spec propertyForKey:@"key"]);
370 if ([[spec propertyForKey:@"negate"] boolValue])
371 value = [NSNumber numberWithBool:(![value boolValue])];
372 [_settings setValue:value forKey:key];
373 [self settingsChanged];
224d48e3
JF
374}
375
024f98ea
JF
376- (id) readPreferenceValue:(PSSpecifier *)spec {
377 NSString *key([spec propertyForKey:@"key"]);
378 id defaultValue([spec propertyForKey:@"default"]);
379 id plistValue([_settings objectForKey:key]);
380 if (!plistValue)
381 return defaultValue;
382 if ([[spec propertyForKey:@"negate"] boolValue])
383 plistValue = [NSNumber numberWithBool:(![plistValue boolValue])];
384 return plistValue;
224d48e3
JF
385}
386
43fd3a0b 387- (void) __optimizeThemes {
265e19b2
JF
388 system("/usr/libexec/winterboard/Optimize");
389}
390
391- (void) optimizeThemes {
54a4cf27 392 UIAlertView *alert([[[UIAlertView alloc]
43fd3a0b 393 initWithTitle:@"Optimize Themes"
54a4cf27 394 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 395 delegate:self
54a4cf27
JF
396 cancelButtonTitle:@"Cancel"
397 otherButtonTitles:@"Optimize", nil
43fd3a0b
JF
398 ] autorelease]);
399
54a4cf27
JF
400 [alert setContext:@"optimize"];
401 [alert setNumberOfRows:1];
402 [alert show];
43fd3a0b
JF
403}
404
405- (void) _optimizeThemes {
265e19b2
JF
406 UIView *view([self view]);
407 UIWindow *window([view window]);
408
409 UIProgressHUD *hud([[[UIProgressHUD alloc] initWithWindow:window] autorelease]);
410 [hud setText:@"Reticulating Splines\nPlease Wait (Minutes)"];
411
412 [window setUserInteractionEnabled:NO];
413
414 [window addSubview:hud];
415 [hud show:YES];
43fd3a0b 416 [self wb$yieldToSelector:@selector(__optimizeThemes)];
265e19b2
JF
417 [hud removeFromSuperview];
418
419 [window setUserInteractionEnabled:YES];
420
421 [self settingsChanged];
422}
423
54a4cf27
JF
424- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
425 NSString *context([alert context]);
43fd3a0b
JF
426
427 if ([context isEqualToString:@"optimize"]) {
54a4cf27
JF
428 if (button == [alert firstOtherButtonIndex]) {
429 [self performSelector:@selector(_optimizeThemes) withObject:nil afterDelay:0];
43fd3a0b
JF
430 }
431
54a4cf27
JF
432 [alert dismissWithClickedButtonIndex:-1 animated:YES];
433 }
434 /*else if ([super respondsToSelector:@selector(alertView:clickedButtonAtIndex:)])
435 [super alertView:alert clickedButtonAtIndex:button];*/
43fd3a0b
JF
436}
437
024f98ea
JF
438@end
439
440@interface WBSettingsController: PSListController {
441}
442
443- (id) initForContentSize:(CGSize)size;
444- (void) dealloc;
445- (void) suspend;
446- (void) navigationBarButtonClicked:(int)buttonIndex;
447- (void) viewWillRedisplay;
448- (void) pushController:(id)controller;
449- (id) specifiers;
450- (void) settingsChanged;
451- (NSString *) title;
452- (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec;
453- (id) readPreferenceValue:(PSSpecifier *)spec;
454
455@end
456
457@implementation WBSettingsController
458
459+ (void) load {
e0c66475 460 libhide = dlopen("/usr/lib/hide.dylib", RTLD_LAZY);
024f98ea
JF
461 IsIconHiddenDisplayId = reinterpret_cast<BOOL (*)(NSString *)>(dlsym(libhide, "IsIconHiddenDisplayId"));
462 HideIconViaDisplayId = reinterpret_cast<BOOL (*)(NSString *)>(dlsym(libhide, "HideIconViaDisplayId"));
463 UnHideIconViaDisplayId = reinterpret_cast<BOOL (*)(NSString *)>(dlsym(libhide, "UnHideIconViaDisplayId"));
464}
465
34b51da8
JF
466- (void) _wb$loadSettings {
467 _plist = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.WinterBoard.plist", NSHomeDirectory()] retain];
468 _settings = [NSMutableDictionary dictionaryWithContentsOfFile:_plist];
469
470 bool set;
471 if (_settings != nil)
472 set = true;
473 else {
474 set = false;
475 _settings = [NSMutableDictionary dictionary];
476 }
d236b808 477
34b51da8 478 _settings = [_settings retain];
d236b808 479
34b51da8
JF
480 if ([_settings objectForKey:@"SummerBoard"] == nil)
481 [_settings setObject:[NSNumber numberWithBool:set] forKey:@"SummerBoard"];
024f98ea 482
e0c66475
JF
483 if (libhide != NULL)
484 [_settings setObject:[NSNumber numberWithBool:IsIconHiddenDisplayId(WinterBoardDisplayID)] forKey:@"IconHidden"];
34b51da8
JF
485}
486
487- (id) initForContentSize:(CGSize)size {
488 if ((self = [super initForContentSize:size]) != nil) {
489 [self _wb$loadSettings];
024f98ea
JF
490 } return self;
491}
492
493- (void) dealloc {
494 [_settings release];
495 [_plist release];
496 [super dealloc];
497}
498
224d48e3
JF
499- (void) suspend {
500 if (!settingsChanged)
501 return;
502
503 NSData *data([NSPropertyListSerialization dataFromPropertyList:_settings format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL]);
504 if (!data)
505 return;
506 if (![data writeToFile:_plist options:NSAtomicWrite error:NULL])
507 return;
508
e0c66475
JF
509 if (libhide != NULL)
510 ([[_settings objectForKey:@"IconHidden"] boolValue] ? HideIconViaDisplayId : UnHideIconViaDisplayId)(WinterBoardDisplayID);
7c88a3f1 511
224d48e3
JF
512 unlink("/User/Library/Caches/com.apple.springboard-imagecache-icons");
513 unlink("/User/Library/Caches/com.apple.springboard-imagecache-icons.plist");
514 unlink("/User/Library/Caches/com.apple.springboard-imagecache-smallicons");
515 unlink("/User/Library/Caches/com.apple.springboard-imagecache-smallicons.plist");
bbea033b 516
546a205b
JF
517 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen");
518 unlink("/User/Library/Caches/com.apple.SpringBoard.notificationCenterLinen");
bbea033b 519
546a205b
JF
520 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.0");
521 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.1");
522 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.2");
523 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.3");
7c88a3f1
JF
524
525 system("rm -rf /User/Library/Caches/SpringBoardIconCache");
526 system("rm -rf /User/Library/Caches/SpringBoardIconCache-small");
f64efe8d 527 system("rm -rf /User/Library/Caches/com.apple.IconsCache");
546a205b 528 system("rm -rf /User/Library/Caches/com.apple.newsstand");
bbea033b 529 system("rm -rf /User/Library/Caches/com.apple.springboard.sharedimagecache");
9c3f0992 530 system("rm -rf /User/Library/Caches/com.apple.UIStatusBar");
7c88a3f1 531
3da4212c
JF
532 system("rm -rf /User/Library/Caches/BarDialer");
533 system("rm -rf /User/Library/Caches/BarDialer_selected");
534 system("rm -rf /User/Library/Caches/BarRecents");
535 system("rm -rf /User/Library/Caches/BarRecents_selected");
536 system("rm -rf /User/Library/Caches/BarVM");
537 system("rm -rf /User/Library/Caches/BarVM_selected");
538
26bd2bf2 539 system("killall -9 lsd");
c532a1cf
JF
540
541 if (kCFCoreFoundationVersionNumber > 700) // XXX: iOS 6.x
542 system("killall backboardd");
543 else
544 system("killall SpringBoard");
224d48e3
JF
545}
546
b39b993d
DH
547- (void) cancelChanges {
548 [_settings release];
549 [_plist release];
b39b993d 550
34b51da8
JF
551 [self _wb$loadSettings];
552
b39b993d
DH
553 [self reloadSpecifiers];
554 if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) {
1d3b613f
JF
555 [[self navigationItem] setLeftBarButtonItem:nil];
556 [[self navigationItem] setRightBarButtonItem:nil];
838dbf4c
DH
557 } else {
558 [self showLeftButton:nil withStyle:0 rightButton:nil withStyle:0];
b39b993d
DH
559 }
560 settingsChanged = NO;
561}
562
224d48e3
JF
563- (void) navigationBarButtonClicked:(int)buttonIndex {
564 if (!settingsChanged) {
565 [super navigationBarButtonClicked:buttonIndex];
566 return;
567 }
568
838dbf4c 569 if (buttonIndex == 0) {
b39b993d 570 [self cancelChanges];
838dbf4c
DH
571 return;
572 }
224d48e3
JF
573
574 [self suspend];
575 [self.rootController popController];
576}
577
b39b993d
DH
578- (void) settingsConfirmButtonClicked:(UIBarButtonItem *)button {
579 [self navigationBarButtonClicked:button.tag];
580}
581
224d48e3
JF
582- (void) viewWillRedisplay {
583 if (settingsChanged)
584 [self settingsChanged];
585 [super viewWillRedisplay];
586}
587
b39b993d
DH
588- (void) viewWillAppear:(BOOL)animated {
589 if (settingsChanged)
590 [self settingsChanged];
1d3b613f
JF
591 if ([super respondsToSelector:@selector(viewWillAppear:)])
592 [super viewWillAppear:animated];
b39b993d
DH
593}
594
224d48e3
JF
595- (void) pushController:(id)controller {
596 [self hideNavigationBarButtons];
597 [super pushController:controller];
598}
599
600- (id) specifiers {
6e9f4e02
JF
601 if (!_specifiers) {
602 NSMutableArray *specifiers([NSMutableArray array]);
603 for (PSSpecifier *specifier in [self loadSpecifiersFromPlistName:@"WinterBoard" target:self]) {
604 if (NSArray *version = [specifier propertyForKey:@"wb$filter"]) {
605 size_t count([version count]);
606 if (count == 0 || count > 2)
607 continue;
608
609 double lower([[version objectAtIndex:0] doubleValue]);
610 if (kCFCoreFoundationVersionNumber < lower)
611 continue;
612
613 if (count != 1) {
614 double upper([[version objectAtIndex:1] doubleValue]);
615 if (upper <= kCFCoreFoundationVersionNumber)
616 continue;
617 }
618 }
619 [specifiers addObject:specifier];
620 }
621 _specifiers = [specifiers retain];
622 }
224d48e3
JF
623 return _specifiers;
624}
625
626- (void) settingsChanged {
b39b993d
DH
627 if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) {
628 UIBarButtonItem *respringButton([[UIBarButtonItem alloc] initWithTitle:@"Respring" style:UIBarButtonItemStyleDone target:self action:@selector(settingsConfirmButtonClicked:)]);
629 UIBarButtonItem *cancelButton([[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(settingsConfirmButtonClicked:)]);
630 cancelButton.tag = 0;
631 respringButton.tag = 1;
1d3b613f
JF
632 [[self navigationItem] setLeftBarButtonItem:respringButton];
633 [[self navigationItem] setRightBarButtonItem:cancelButton];
b39b993d
DH
634 [respringButton release];
635 [cancelButton release];
636 } else {
637 [self showLeftButton:@"Respring" withStyle:2 rightButton:@"Cancel" withStyle:0];
638 }
224d48e3
JF
639 settingsChanged = YES;
640}
641
642- (NSString *) title {
643 return @"WinterBoard";
644}
645
646- (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec {
7c88a3f1 647 NSString *key([spec propertyForKey:@"key"]);
224d48e3
JF
648 if ([[spec propertyForKey:@"negate"] boolValue])
649 value = [NSNumber numberWithBool:(![value boolValue])];
7c88a3f1 650 [_settings setValue:value forKey:key];
224d48e3
JF
651 [self settingsChanged];
652}
653
654- (id) readPreferenceValue:(PSSpecifier *)spec {
655 NSString *key([spec propertyForKey:@"key"]);
656 id defaultValue([spec propertyForKey:@"default"]);
657 id plistValue([_settings objectForKey:key]);
658 if (!plistValue)
659 return defaultValue;
660 if ([[spec propertyForKey:@"negate"] boolValue])
661 plistValue = [NSNumber numberWithBool:(![plistValue boolValue])];
662 return plistValue;
663}
664
665@end
b39b993d
DH
666
667#define WBSAddMethod(_class, _sel, _imp, _type) \
668 if (![[_class class] instancesRespondToSelector:@selector(_sel)]) \
669 class_addMethod([_class class], @selector(_sel), (IMP)_imp, _type)
670void $PSRootController$popController(PSRootController *self, SEL _cmd) {
671 [self popViewControllerAnimated:YES];
672}
673
674void $PSViewController$hideNavigationBarButtons(PSRootController *self, SEL _cmd) {
675}
676
677id $PSViewController$initForContentSize$(PSRootController *self, SEL _cmd, CGRect contentSize) {
678 return [self init];
679}
680
681static __attribute__((constructor)) void __wbsInit() {
682 WBSAddMethod(PSRootController, popController, $PSRootController$popController, "v@:");
683 WBSAddMethod(PSViewController, hideNavigationBarButtons, $PSViewController$hideNavigationBarButtons, "v@:");
684 WBSAddMethod(PSViewController, initForContentSize:, $PSViewController$initForContentSize$, "@@:{ff}");
685}