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;
130 - (id) initForContentSize:(CGSize)size;
132 - (id) navigationTitle;
133 - (void) themesChanged;
135 - (int) numberOfSectionsInTableView:(UITableView *)tableView;
136 - (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section;
137 - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section;
138 - (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
139 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
140 - (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath;
141 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
142 - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
143 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
147 @implementation WBSThemesController
149 @synthesize themes = _themes;
151 + (void) initialize {
152 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
153 checkImage = [_UIImageWithName(@"UIPreferencesBlueCheck.png") retain];
154 uncheckedImage = [[UIImage imageWithContentsOfFile:@"/System/Library/PreferenceBundles/WinterBoardSettings.bundle/SearchResultsCheckmarkClear.png"] retain];
158 - (id) initForContentSize:(CGSize)size {
159 if ((self = [super initForContentSize:size]) != nil) {
160 self.themes = [_settings objectForKey:@"Themes"];
162 if (NSString *theme = [_settings objectForKey:@"Theme"]) {
163 self.themes = [NSMutableArray arrayWithObject:
164 [NSMutableDictionary dictionaryWithObjectsAndKeys:
166 [NSNumber numberWithBool:YES], @"Active", nil]];
167 [_settings removeObjectForKey:@"Theme"];
170 self.themes = [NSMutableArray array];
171 [_settings setObject:_themes forKey:@"Themes"];
174 NSMutableArray *themesOnDisk([NSMutableArray array]);
177 addObjectsFromArray:[[NSFileManager defaultManager]
178 contentsOfDirectoryAtPath:@"/Library/Themes" error:NULL]
181 [themesOnDisk addObjectsFromArray:[[NSFileManager defaultManager]
182 contentsOfDirectoryAtPath:[NSString stringWithFormat:@"%@/Library/SummerBoard/Themes", NSHomeDirectory()]
186 for (int i = 0, count = [themesOnDisk count]; i < count; i++) {
187 NSString *theme = [themesOnDisk objectAtIndex:i];
188 if ([theme hasSuffix:@".theme"])
189 [themesOnDisk replaceObjectAtIndex:i withObject:[theme stringByDeletingPathExtension]];
192 NSMutableSet *themesSet([NSMutableSet set]);
194 for (int i = 0, count = [_themes count]; i < count; i++) {
195 NSDictionary *theme([_themes objectAtIndex:i]);
196 NSString *name([theme objectForKey:@"Name"]);
198 if (!name || ![themesOnDisk containsObject:name]) {
199 [_themes removeObjectAtIndex:i];
203 [themesSet addObject:name];
207 for (NSString *theme in themesOnDisk) {
208 if ([themesSet containsObject:theme])
210 [themesSet addObject:theme];
212 [_themes insertObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
214 [NSNumber numberWithBool:NO], @"Active",
218 _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-64) style:UITableViewStyleGrouped];
219 [_tableView setDataSource:self];
220 [_tableView setDelegate:self];
221 [_tableView setEditing:YES];
222 [_tableView setAllowsSelectionDuringEditing:YES];
223 if ([self respondsToSelector:@selector(setView:)])
224 [self setView:_tableView];
230 [_tableView release];
235 - (id) navigationTitle {
243 - (void) themesChanged {
244 settingsChanged = YES;
247 /* UITableViewDelegate / UITableViewDataSource Methods {{{ */
248 - (int) numberOfSectionsInTableView:(UITableView *)tableView {
252 - (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section {
256 - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section {
257 return _themes.count;
260 - (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
261 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThemeCell"];
263 cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100) reuseIdentifier:@"ThemeCell"] autorelease];
264 //[cell setTableViewStyle:UITableViewCellStyleDefault];
267 NSDictionary *theme([_themes objectAtIndex:indexPath.row]);
268 cell.text = [theme objectForKey:@"Name"];
269 cell.hidesAccessoryWhenEditing = NO;
270 NSNumber *active([theme objectForKey:@"Active"]);
271 BOOL inactive(active == nil || ![active boolValue]);
272 [cell setImage:(inactive ? uncheckedImage : checkImage)];
276 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
277 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
278 NSMutableDictionary *theme = [_themes objectAtIndex:indexPath.row];
279 NSNumber *active = [theme objectForKey:@"Active"];
280 BOOL inactive = active == nil || ![active boolValue];
281 [theme setObject:[NSNumber numberWithBool:inactive] forKey:@"Active"];
282 [cell setImage:(!inactive ? uncheckedImage : checkImage)];
283 [tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES];
284 [self themesChanged];
287 - (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
288 NSUInteger fromIndex = [fromIndexPath row];
289 NSUInteger toIndex = [toIndexPath row];
290 if (fromIndex == toIndex)
292 NSMutableDictionary *theme = [[[_themes objectAtIndex:fromIndex] retain] autorelease];
293 [_themes removeObjectAtIndex:fromIndex];
294 [_themes insertObject:theme atIndex:toIndex];
295 [self themesChanged];
298 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
299 return UITableViewCellEditingStyleNone;
302 - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
306 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
313 @interface WBAdvancedController: PSListController {
317 - (void) settingsChanged;
321 @implementation WBAdvancedController
325 _specifiers = [[self loadSpecifiersFromPlistName:@"Advanced" target:self] retain];
329 - (void) settingsChanged {
330 settingsChanged = YES;
333 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec {
334 NSString *key([spec propertyForKey:@"key"]);
335 if ([[spec propertyForKey:@"negate"] boolValue])
336 value = [NSNumber numberWithBool:(![value boolValue])];
337 [_settings setValue:value forKey:key];
338 [self settingsChanged];
341 - (id) readPreferenceValue:(PSSpecifier *)spec {
342 NSString *key([spec propertyForKey:@"key"]);
343 id defaultValue([spec propertyForKey:@"default"]);
344 id plistValue([_settings objectForKey:key]);
347 if ([[spec propertyForKey:@"negate"] boolValue])
348 plistValue = [NSNumber numberWithBool:(![plistValue boolValue])];
352 - (void) __optimizeThemes {
353 system("/usr/libexec/winterboard/Optimize");
356 - (void) optimizeThemes {
357 UIAlertView *alert([[[UIAlertView alloc]
358 initWithTitle:@"Optimize Themes"
359 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."
361 cancelButtonTitle:@"Cancel"
362 otherButtonTitles:@"Optimize", nil
365 [alert setContext:@"optimize"];
366 [alert setNumberOfRows:1];
370 - (void) _optimizeThemes {
371 UIView *view([self view]);
372 UIWindow *window([view window]);
374 UIProgressHUD *hud([[[UIProgressHUD alloc] initWithWindow:window] autorelease]);
375 [hud setText:@"Reticulating Splines\nPlease Wait (Minutes)"];
377 [window setUserInteractionEnabled:NO];
379 [window addSubview:hud];
381 [self wb$yieldToSelector:@selector(__optimizeThemes)];
382 [hud removeFromSuperview];
384 [window setUserInteractionEnabled:YES];
386 [self settingsChanged];
389 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
390 NSString *context([alert context]);
392 if ([context isEqualToString:@"optimize"]) {
393 if (button == [alert firstOtherButtonIndex]) {
394 [self performSelector:@selector(_optimizeThemes) withObject:nil afterDelay:0];
397 [alert dismissWithClickedButtonIndex:-1 animated:YES];
399 /*else if ([super respondsToSelector:@selector(alertView:clickedButtonAtIndex:)])
400 [super alertView:alert clickedButtonAtIndex:button];*/
405 @interface WBSettingsController: PSListController {
408 - (id) initForContentSize:(CGSize)size;
411 - (void) navigationBarButtonClicked:(int)buttonIndex;
412 - (void) viewWillRedisplay;
413 - (void) pushController:(id)controller;
415 - (void) settingsChanged;
416 - (NSString *) title;
417 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec;
418 - (id) readPreferenceValue:(PSSpecifier *)spec;
422 @implementation WBSettingsController
425 libhide = dlopen("/usr/lib/hide.dylib", RTLD_LAZY);
426 IsIconHiddenDisplayId = reinterpret_cast<BOOL (*)(NSString *)>(dlsym(libhide, "IsIconHiddenDisplayId"));
427 HideIconViaDisplayId = reinterpret_cast<BOOL (*)(NSString *)>(dlsym(libhide, "HideIconViaDisplayId"));
428 UnHideIconViaDisplayId = reinterpret_cast<BOOL (*)(NSString *)>(dlsym(libhide, "UnHideIconViaDisplayId"));
431 - (void) _wb$loadSettings {
432 _plist = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.WinterBoard.plist", NSHomeDirectory()] retain];
433 _settings = [NSMutableDictionary dictionaryWithContentsOfFile:_plist];
436 if (_settings != nil)
440 _settings = [NSMutableDictionary dictionary];
443 _settings = [_settings retain];
445 if ([_settings objectForKey:@"SummerBoard"] == nil)
446 [_settings setObject:[NSNumber numberWithBool:set] forKey:@"SummerBoard"];
449 [_settings setObject:[NSNumber numberWithBool:IsIconHiddenDisplayId(WinterBoardDisplayID)] forKey:@"IconHidden"];
452 - (id) initForContentSize:(CGSize)size {
453 if ((self = [super initForContentSize:size]) != nil) {
454 [self _wb$loadSettings];
465 if (!settingsChanged)
468 NSData *data([NSPropertyListSerialization dataFromPropertyList:_settings format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL]);
471 if (![data writeToFile:_plist options:NSAtomicWrite error:NULL])
475 ([[_settings objectForKey:@"IconHidden"] boolValue] ? HideIconViaDisplayId : UnHideIconViaDisplayId)(WinterBoardDisplayID);
477 unlink("/User/Library/Caches/com.apple.springboard-imagecache-icons");
478 unlink("/User/Library/Caches/com.apple.springboard-imagecache-icons.plist");
479 unlink("/User/Library/Caches/com.apple.springboard-imagecache-smallicons");
480 unlink("/User/Library/Caches/com.apple.springboard-imagecache-smallicons.plist");
482 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen");
483 unlink("/User/Library/Caches/com.apple.SpringBoard.notificationCenterLinen");
485 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.0");
486 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.1");
487 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.2");
488 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.3");
490 system("rm -rf /User/Library/Caches/SpringBoardIconCache");
491 system("rm -rf /User/Library/Caches/SpringBoardIconCache-small");
492 system("rm -rf /User/Library/Caches/com.apple.IconsCache");
493 system("rm -rf /User/Library/Caches/com.apple.newsstand");
494 system("rm -rf /User/Library/Caches/com.apple.springboard.sharedimagecache");
496 system("killall -9 lsd");
498 if (kCFCoreFoundationVersionNumber > 700) // XXX: iOS 6.x
499 system("killall backboardd");
501 system("killall SpringBoard");
504 - (void) cancelChanges {
508 [self _wb$loadSettings];
510 [self reloadSpecifiers];
511 if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) {
512 [[self navigationItem] setLeftBarButtonItem:nil];
513 [[self navigationItem] setRightBarButtonItem:nil];
515 [self showLeftButton:nil withStyle:0 rightButton:nil withStyle:0];
517 settingsChanged = NO;
520 - (void) navigationBarButtonClicked:(int)buttonIndex {
521 if (!settingsChanged) {
522 [super navigationBarButtonClicked:buttonIndex];
526 if (buttonIndex == 0) {
527 [self cancelChanges];
532 [self.rootController popController];
535 - (void) settingsConfirmButtonClicked:(UIBarButtonItem *)button {
536 [self navigationBarButtonClicked:button.tag];
539 - (void) viewWillRedisplay {
541 [self settingsChanged];
542 [super viewWillRedisplay];
545 - (void) viewWillAppear:(BOOL)animated {
547 [self settingsChanged];
548 if ([super respondsToSelector:@selector(viewWillAppear:)])
549 [super viewWillAppear:animated];
552 - (void) pushController:(id)controller {
553 [self hideNavigationBarButtons];
554 [super pushController:controller];
559 _specifiers = [[self loadSpecifiersFromPlistName:@"WinterBoard" target:self] retain];
563 - (void) settingsChanged {
564 if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) {
565 UIBarButtonItem *respringButton([[UIBarButtonItem alloc] initWithTitle:@"Respring" style:UIBarButtonItemStyleDone target:self action:@selector(settingsConfirmButtonClicked:)]);
566 UIBarButtonItem *cancelButton([[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(settingsConfirmButtonClicked:)]);
567 cancelButton.tag = 0;
568 respringButton.tag = 1;
569 [[self navigationItem] setLeftBarButtonItem:respringButton];
570 [[self navigationItem] setRightBarButtonItem:cancelButton];
571 [respringButton release];
572 [cancelButton release];
574 [self showLeftButton:@"Respring" withStyle:2 rightButton:@"Cancel" withStyle:0];
576 settingsChanged = YES;
579 - (NSString *) title {
580 return @"WinterBoard";
583 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec {
584 NSString *key([spec propertyForKey:@"key"]);
585 if ([[spec propertyForKey:@"negate"] boolValue])
586 value = [NSNumber numberWithBool:(![value boolValue])];
587 [_settings setValue:value forKey:key];
588 [self settingsChanged];
591 - (id) readPreferenceValue:(PSSpecifier *)spec {
592 NSString *key([spec propertyForKey:@"key"]);
593 id defaultValue([spec propertyForKey:@"default"]);
594 id plistValue([_settings objectForKey:key]);
597 if ([[spec propertyForKey:@"negate"] boolValue])
598 plistValue = [NSNumber numberWithBool:(![plistValue boolValue])];
604 #define WBSAddMethod(_class, _sel, _imp, _type) \
605 if (![[_class class] instancesRespondToSelector:@selector(_sel)]) \
606 class_addMethod([_class class], @selector(_sel), (IMP)_imp, _type)
607 void $PSRootController$popController(PSRootController *self, SEL _cmd) {
608 [self popViewControllerAnimated:YES];
611 void $PSViewController$hideNavigationBarButtons(PSRootController *self, SEL _cmd) {
614 id $PSViewController$initForContentSize$(PSRootController *self, SEL _cmd, CGRect contentSize) {
618 static __attribute__((constructor)) void __wbsInit() {
619 WBSAddMethod(PSRootController, popController, $PSRootController$popController, "v@:");
620 WBSAddMethod(PSViewController, hideNavigationBarButtons, $PSViewController$hideNavigationBarButtons, "v@:");
621 WBSAddMethod(PSViewController, initForContentSize:, $PSViewController$initForContentSize$, "@@:{ff}");