1 /* WinterBoard - Theme Manager for the iPhone
2 * Copyright (C) 2008-2014 Jay Freeman (saurik)
5 /* GNU Lesser General Public License, Version 3 {{{ */
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.
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.
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/>.
22 #import <Foundation/Foundation.h>
23 #import <UIKit/UIKit.h>
24 #import <Preferences/PSRootController.h>
25 #import <Preferences/PSViewController.h>
26 #import <Preferences/PSListController.h>
27 #import <Preferences/PSSpecifier.h>
28 #import <Preferences/PSTableCell.h>
29 #import <UIKit/UINavigationButton.h>
32 #include <objc/runtime.h>
35 static BOOL (*IsIconHiddenDisplayId)(NSString *);
36 static BOOL (*HideIconViaDisplayId)(NSString *);
37 static BOOL (*UnHideIconViaDisplayId)(NSString *);
39 static NSString *WinterBoardDisplayID = @"com.saurik.WinterBoard";
41 extern NSString *PSTableCellKey;
42 extern "C" UIImage *_UIImageWithName(NSString *);
44 static UIImage *checkImage;
45 static UIImage *uncheckedImage;
47 static BOOL settingsChanged;
48 static NSMutableDictionary *_settings;
49 static NSString *_plist;
51 /* [NSObject yieldToSelector:(withObject:)] {{{*/
52 @interface NSObject (wb$yieldToSelector)
53 - (id) wb$yieldToSelector:(SEL)selector withObject:(id)object;
54 - (id) wb$yieldToSelector:(SEL)selector;
57 @implementation NSObject (Cydia)
59 - (void) wb$doNothing {
62 - (void) wb$_yieldToContext:(NSMutableArray *)context {
63 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
65 SEL selector(reinterpret_cast<SEL>([[context objectAtIndex:0] pointerValue]));
66 id object([[context objectAtIndex:1] nonretainedObjectValue]);
67 volatile bool &stopped(*reinterpret_cast<bool *>([[context objectAtIndex:2] pointerValue]));
69 /* XXX: deal with exceptions */
70 id value([self performSelector:selector withObject:object]);
72 NSMethodSignature *signature([self methodSignatureForSelector:selector]);
73 [context removeAllObjects];
74 if ([signature methodReturnLength] != 0 && value != nil)
75 [context addObject:value];
80 performSelectorOnMainThread:@selector(wb$doNothing)
88 - (id) wb$yieldToSelector:(SEL)selector withObject:(id)object {
89 /*return [self performSelector:selector withObject:object];*/
91 volatile bool stopped(false);
93 NSMutableArray *context([NSMutableArray arrayWithObjects:
94 [NSValue valueWithPointer:selector],
95 [NSValue valueWithNonretainedObject:object],
96 [NSValue valueWithPointer:const_cast<bool *>(&stopped)],
99 NSThread *thread([[[NSThread alloc]
101 selector:@selector(wb$_yieldToContext:)
107 NSRunLoop *loop([NSRunLoop currentRunLoop]);
108 NSDate *future([NSDate distantFuture]);
110 while (!stopped && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
112 return [context count] == 0 ? nil : [context objectAtIndex:0];
115 - (id) wb$yieldToSelector:(SEL)selector {
116 return [self wb$yieldToSelector:selector withObject:nil];
122 /* Theme Settings Controller {{{ */
123 @interface WBSThemesController: PSViewController <UITableViewDelegate, UITableViewDataSource> {
124 UITableView *_tableView;
125 NSMutableArray *_themes;
128 @property (nonatomic, retain) NSMutableArray *themes;
132 - (id) initForContentSize:(CGSize)size;
134 - (id) navigationTitle;
135 - (void) themesChanged;
137 - (int) numberOfSectionsInTableView:(UITableView *)tableView;
138 - (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section;
139 - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section;
140 - (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
141 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
142 - (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath;
143 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
144 - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
145 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
149 @implementation WBSThemesController
151 @synthesize themes = _themes;
154 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
155 checkImage = [_UIImageWithName(@"UIPreferencesBlueCheck.png") retain];
156 uncheckedImage = [[UIImage imageWithContentsOfFile:@"/System/Library/PreferenceBundles/WinterBoardSettings.bundle/SearchResultsCheckmarkClear.png"] retain];
160 - (id) initForContentSize:(CGSize)size {
161 if ((self = [super initForContentSize:size]) != nil) {
162 self.themes = [_settings objectForKey:@"Themes"];
164 if (NSString *theme = [_settings objectForKey:@"Theme"]) {
165 self.themes = [NSMutableArray arrayWithObject:
166 [NSMutableDictionary dictionaryWithObjectsAndKeys:
168 [NSNumber numberWithBool:YES], @"Active", nil]];
169 [_settings removeObjectForKey:@"Theme"];
172 self.themes = [NSMutableArray array];
173 [_settings setObject:_themes forKey:@"Themes"];
176 NSMutableArray *themesOnDisk([NSMutableArray array]);
179 addObjectsFromArray:[[NSFileManager defaultManager]
180 contentsOfDirectoryAtPath:@"/Library/Themes" error:NULL]
183 [themesOnDisk addObjectsFromArray:[[NSFileManager defaultManager]
184 contentsOfDirectoryAtPath:[NSString stringWithFormat:@"%@/Library/SummerBoard/Themes", NSHomeDirectory()]
188 for (int i = 0, count = [themesOnDisk count]; i < count; i++) {
189 NSString *theme = [themesOnDisk objectAtIndex:i];
190 if ([theme hasSuffix:@".theme"])
191 [themesOnDisk replaceObjectAtIndex:i withObject:[theme stringByDeletingPathExtension]];
194 NSMutableSet *themesSet([NSMutableSet set]);
196 for (int i = 0, count = [_themes count]; i < count; i++) {
197 NSDictionary *theme([_themes objectAtIndex:i]);
198 NSString *name([theme objectForKey:@"Name"]);
200 if (!name || ![themesOnDisk containsObject:name]) {
201 [_themes removeObjectAtIndex:i];
205 [themesSet addObject:name];
209 for (NSString *theme in themesOnDisk) {
210 if ([themesSet containsObject:theme])
212 [themesSet addObject:theme];
214 [_themes insertObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
216 [NSNumber numberWithBool:NO], @"Active",
220 _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-64) style:UITableViewStyleGrouped];
221 [_tableView setDataSource:self];
222 [_tableView setDelegate:self];
223 [_tableView setEditing:YES];
224 [_tableView setAllowsSelectionDuringEditing:YES];
225 if ([self respondsToSelector:@selector(setView:)])
226 [self setView:_tableView];
232 [_tableView release];
237 - (id) navigationTitle {
245 - (void) themesChanged {
246 settingsChanged = YES;
249 /* UITableViewDelegate / UITableViewDataSource Methods {{{ */
250 - (int) numberOfSectionsInTableView:(UITableView *)tableView {
254 - (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section {
258 - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section {
259 return _themes.count;
262 - (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
263 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThemeCell"];
265 cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100) reuseIdentifier:@"ThemeCell"] autorelease];
266 //[cell setTableViewStyle:UITableViewCellStyleDefault];
269 NSDictionary *theme([_themes objectAtIndex:indexPath.row]);
270 cell.text = [theme objectForKey:@"Name"];
271 cell.hidesAccessoryWhenEditing = NO;
272 NSNumber *active([theme objectForKey:@"Active"]);
273 BOOL inactive(active == nil || ![active boolValue]);
274 [cell setImage:(inactive ? uncheckedImage : checkImage)];
278 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
279 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
280 NSMutableDictionary *theme = [_themes objectAtIndex:indexPath.row];
281 NSNumber *active = [theme objectForKey:@"Active"];
282 BOOL inactive = active == nil || ![active boolValue];
283 [theme setObject:[NSNumber numberWithBool:inactive] forKey:@"Active"];
284 [cell setImage:(!inactive ? uncheckedImage : checkImage)];
285 [tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES];
286 [self themesChanged];
289 - (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
290 NSUInteger fromIndex = [fromIndexPath row];
291 NSUInteger toIndex = [toIndexPath row];
292 if (fromIndex == toIndex)
294 NSMutableDictionary *theme = [[[_themes objectAtIndex:fromIndex] retain] autorelease];
295 [_themes removeObjectAtIndex:fromIndex];
296 [_themes insertObject:theme atIndex:toIndex];
297 [self themesChanged];
300 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
301 return UITableViewCellEditingStyleNone;
304 - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
308 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
315 @interface WBAdvancedController: PSListController {
319 - (void) settingsChanged;
323 @implementation WBAdvancedController
327 _specifiers = [[self loadSpecifiersFromPlistName:@"Advanced" target:self] retain];
331 - (void) settingsChanged {
332 settingsChanged = YES;
335 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec {
336 NSString *key([spec propertyForKey:@"key"]);
337 if ([[spec propertyForKey:@"negate"] boolValue])
338 value = [NSNumber numberWithBool:(![value boolValue])];
339 [_settings setValue:value forKey:key];
340 [self settingsChanged];
343 - (id) readPreferenceValue:(PSSpecifier *)spec {
344 NSString *key([spec propertyForKey:@"key"]);
345 id defaultValue([spec propertyForKey:@"default"]);
346 id plistValue([_settings objectForKey:key]);
349 if ([[spec propertyForKey:@"negate"] boolValue])
350 plistValue = [NSNumber numberWithBool:(![plistValue boolValue])];
354 - (void) __optimizeThemes {
355 system("/usr/libexec/winterboard/Optimize");
358 - (void) optimizeThemes {
359 UIAlertView *alert([[[UIAlertView alloc]
360 initWithTitle:@"Optimize Themes"
361 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."
363 cancelButtonTitle:@"Cancel"
364 otherButtonTitles:@"Optimize", nil
367 [alert setContext:@"optimize"];
368 [alert setNumberOfRows:1];
372 - (void) _optimizeThemes {
373 UIView *view([self view]);
374 UIWindow *window([view window]);
376 UIProgressHUD *hud([[[UIProgressHUD alloc] initWithWindow:window] autorelease]);
377 [hud setText:@"Reticulating Splines\nPlease Wait (Minutes)"];
379 [window setUserInteractionEnabled:NO];
381 [window addSubview:hud];
383 [self wb$yieldToSelector:@selector(__optimizeThemes)];
384 [hud removeFromSuperview];
386 [window setUserInteractionEnabled:YES];
388 [self settingsChanged];
391 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
392 NSString *context([alert context]);
394 if ([context isEqualToString:@"optimize"]) {
395 if (button == [alert firstOtherButtonIndex]) {
396 [self performSelector:@selector(_optimizeThemes) withObject:nil afterDelay:0];
399 [alert dismissWithClickedButtonIndex:-1 animated:YES];
401 /*else if ([super respondsToSelector:@selector(alertView:clickedButtonAtIndex:)])
402 [super alertView:alert clickedButtonAtIndex:button];*/
407 @interface WBSettingsController: PSListController {
410 - (id) initForContentSize:(CGSize)size;
413 - (void) navigationBarButtonClicked:(int)buttonIndex;
414 - (void) viewWillRedisplay;
415 - (void) pushController:(id)controller;
417 - (void) settingsChanged;
418 - (NSString *) title;
419 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec;
420 - (id) readPreferenceValue:(PSSpecifier *)spec;
424 @implementation WBSettingsController
427 libhide = dlopen("/usr/lib/hide.dylib", RTLD_LAZY);
428 IsIconHiddenDisplayId = reinterpret_cast<BOOL (*)(NSString *)>(dlsym(libhide, "IsIconHiddenDisplayId"));
429 HideIconViaDisplayId = reinterpret_cast<BOOL (*)(NSString *)>(dlsym(libhide, "HideIconViaDisplayId"));
430 UnHideIconViaDisplayId = reinterpret_cast<BOOL (*)(NSString *)>(dlsym(libhide, "UnHideIconViaDisplayId"));
433 - (void) _wb$loadSettings {
434 _plist = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.WinterBoard.plist", NSHomeDirectory()] retain];
435 _settings = [NSMutableDictionary dictionaryWithContentsOfFile:_plist];
438 if (_settings != nil)
442 _settings = [NSMutableDictionary dictionary];
445 _settings = [_settings retain];
447 if ([_settings objectForKey:@"SummerBoard"] == nil)
448 [_settings setObject:[NSNumber numberWithBool:set] forKey:@"SummerBoard"];
451 [_settings setObject:[NSNumber numberWithBool:IsIconHiddenDisplayId(WinterBoardDisplayID)] forKey:@"IconHidden"];
454 - (id) initForContentSize:(CGSize)size {
455 if ((self = [super initForContentSize:size]) != nil) {
456 [self _wb$loadSettings];
467 if (!settingsChanged)
470 NSData *data([NSPropertyListSerialization dataFromPropertyList:_settings format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL]);
473 if (![data writeToFile:_plist options:NSAtomicWrite error:NULL])
477 ([[_settings objectForKey:@"IconHidden"] boolValue] ? HideIconViaDisplayId : UnHideIconViaDisplayId)(WinterBoardDisplayID);
479 unlink("/User/Library/Caches/com.apple.springboard-imagecache-icons");
480 unlink("/User/Library/Caches/com.apple.springboard-imagecache-icons.plist");
481 unlink("/User/Library/Caches/com.apple.springboard-imagecache-smallicons");
482 unlink("/User/Library/Caches/com.apple.springboard-imagecache-smallicons.plist");
484 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen");
485 unlink("/User/Library/Caches/com.apple.SpringBoard.notificationCenterLinen");
487 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.0");
488 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.1");
489 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.2");
490 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.3");
492 system("rm -rf /User/Library/Caches/SpringBoardIconCache");
493 system("rm -rf /User/Library/Caches/SpringBoardIconCache-small");
494 system("rm -rf /User/Library/Caches/com.apple.IconsCache");
495 system("rm -rf /User/Library/Caches/com.apple.newsstand");
496 system("rm -rf /User/Library/Caches/com.apple.springboard.sharedimagecache");
498 system("killall -9 lsd");
500 if (kCFCoreFoundationVersionNumber > 700) // XXX: iOS 6.x
501 system("killall backboardd");
503 system("killall SpringBoard");
506 - (void) cancelChanges {
510 [self _wb$loadSettings];
512 [self reloadSpecifiers];
513 if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) {
514 [[self navigationItem] setLeftBarButtonItem:nil];
515 [[self navigationItem] setRightBarButtonItem:nil];
517 [self showLeftButton:nil withStyle:0 rightButton:nil withStyle:0];
519 settingsChanged = NO;
522 - (void) navigationBarButtonClicked:(int)buttonIndex {
523 if (!settingsChanged) {
524 [super navigationBarButtonClicked:buttonIndex];
528 if (buttonIndex == 0) {
529 [self cancelChanges];
534 [self.rootController popController];
537 - (void) settingsConfirmButtonClicked:(UIBarButtonItem *)button {
538 [self navigationBarButtonClicked:button.tag];
541 - (void) viewWillRedisplay {
543 [self settingsChanged];
544 [super viewWillRedisplay];
547 - (void) viewWillAppear:(BOOL)animated {
549 [self settingsChanged];
550 if ([super respondsToSelector:@selector(viewWillAppear:)])
551 [super viewWillAppear:animated];
554 - (void) pushController:(id)controller {
555 [self hideNavigationBarButtons];
556 [super pushController:controller];
561 _specifiers = [[self loadSpecifiersFromPlistName:@"WinterBoard" target:self] retain];
565 - (void) settingsChanged {
566 if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) {
567 UIBarButtonItem *respringButton([[UIBarButtonItem alloc] initWithTitle:@"Respring" style:UIBarButtonItemStyleDone target:self action:@selector(settingsConfirmButtonClicked:)]);
568 UIBarButtonItem *cancelButton([[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(settingsConfirmButtonClicked:)]);
569 cancelButton.tag = 0;
570 respringButton.tag = 1;
571 [[self navigationItem] setLeftBarButtonItem:respringButton];
572 [[self navigationItem] setRightBarButtonItem:cancelButton];
573 [respringButton release];
574 [cancelButton release];
576 [self showLeftButton:@"Respring" withStyle:2 rightButton:@"Cancel" withStyle:0];
578 settingsChanged = YES;
581 - (NSString *) title {
582 return @"WinterBoard";
585 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec {
586 NSString *key([spec propertyForKey:@"key"]);
587 if ([[spec propertyForKey:@"negate"] boolValue])
588 value = [NSNumber numberWithBool:(![value boolValue])];
589 [_settings setValue:value forKey:key];
590 [self settingsChanged];
593 - (id) readPreferenceValue:(PSSpecifier *)spec {
594 NSString *key([spec propertyForKey:@"key"]);
595 id defaultValue([spec propertyForKey:@"default"]);
596 id plistValue([_settings objectForKey:key]);
599 if ([[spec propertyForKey:@"negate"] boolValue])
600 plistValue = [NSNumber numberWithBool:(![plistValue boolValue])];
606 #define WBSAddMethod(_class, _sel, _imp, _type) \
607 if (![[_class class] instancesRespondToSelector:@selector(_sel)]) \
608 class_addMethod([_class class], @selector(_sel), (IMP)_imp, _type)
609 void $PSRootController$popController(PSRootController *self, SEL _cmd) {
610 [self popViewControllerAnimated:YES];
613 void $PSViewController$hideNavigationBarButtons(PSRootController *self, SEL _cmd) {
616 id $PSViewController$initForContentSize$(PSRootController *self, SEL _cmd, CGRect contentSize) {
620 static __attribute__((constructor)) void __wbsInit() {
621 WBSAddMethod(PSRootController, popController, $PSRootController$popController, "v@:");
622 WBSAddMethod(PSViewController, hideNavigationBarButtons, $PSViewController$hideNavigationBarButtons, "v@:");
623 WBSAddMethod(PSViewController, initForContentSize:, $PSViewController$initForContentSize$, "@@:{ff}");