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>
34 extern NSString *PSTableCellKey;
35 extern "C" UIImage *_UIImageWithName(NSString *);
37 static UIImage *checkImage;
38 static UIImage *uncheckedImage;
40 static BOOL settingsChanged;
41 static NSMutableDictionary *_settings;
42 static NSString *_plist;
44 void AddThemes(NSMutableArray *themesOnDisk, NSString *folder) {
45 NSArray *themes([[NSFileManager defaultManager] contentsOfDirectoryAtPath:folder error:NULL]);
46 for (NSString *theme in themes) {
47 if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@/%@/Info.plist", folder, theme]]) {
48 if (NSArray *version = [info objectForKey:@"CoreFoundationVersion"]) {
49 size_t count([version count]);
50 if (count == 0 || count > 2)
53 double lower([[version objectAtIndex:0] doubleValue]);
54 if (kCFCoreFoundationVersionNumber < lower)
58 double upper([[version objectAtIndex:1] doubleValue]);
59 if (upper <= kCFCoreFoundationVersionNumber)
65 [themesOnDisk addObject:theme];
69 /* [NSObject yieldToSelector:(withObject:)] {{{*/
70 @interface NSObject (wb$yieldToSelector)
71 - (id) wb$yieldToSelector:(SEL)selector withObject:(id)object;
72 - (id) wb$yieldToSelector:(SEL)selector;
75 @implementation NSObject (Cydia)
77 - (void) wb$doNothing {
80 - (void) wb$_yieldToContext:(NSMutableArray *)context {
81 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
83 SEL selector(reinterpret_cast<SEL>([[context objectAtIndex:0] pointerValue]));
84 id object([[context objectAtIndex:1] nonretainedObjectValue]);
85 volatile bool &stopped(*reinterpret_cast<bool *>([[context objectAtIndex:2] pointerValue]));
87 /* XXX: deal with exceptions */
88 id value([self performSelector:selector withObject:object]);
90 NSMethodSignature *signature([self methodSignatureForSelector:selector]);
91 [context removeAllObjects];
92 if ([signature methodReturnLength] != 0 && value != nil)
93 [context addObject:value];
98 performSelectorOnMainThread:@selector(wb$doNothing)
106 - (id) wb$yieldToSelector:(SEL)selector withObject:(id)object {
107 /*return [self performSelector:selector withObject:object];*/
109 volatile bool stopped(false);
111 NSMutableArray *context([NSMutableArray arrayWithObjects:
112 [NSValue valueWithPointer:selector],
113 [NSValue valueWithNonretainedObject:object],
114 [NSValue valueWithPointer:const_cast<bool *>(&stopped)],
117 NSThread *thread([[[NSThread alloc]
119 selector:@selector(wb$_yieldToContext:)
125 NSRunLoop *loop([NSRunLoop currentRunLoop]);
126 NSDate *future([NSDate distantFuture]);
128 while (!stopped && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
130 return [context count] == 0 ? nil : [context objectAtIndex:0];
133 - (id) wb$yieldToSelector:(SEL)selector {
134 return [self wb$yieldToSelector:selector withObject:nil];
140 /* Theme Settings Controller {{{ */
141 @interface WBSThemesTableViewCell : UITableViewCell {
146 @implementation WBSThemesTableViewCell
148 - (id) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuse {
149 if ((self = [super initWithFrame:frame reuseIdentifier:reuse]) != nil) {
153 - (void) setTheme:(NSDictionary *)theme {
154 self.text = [theme objectForKey:@"Name"];
155 self.hidesAccessoryWhenEditing = NO;
156 NSNumber *active([theme objectForKey:@"Active"]);
157 BOOL inactive(active == nil || ![active boolValue]);
158 [self setImage:(inactive ? uncheckedImage : checkImage)];
163 @interface WBSThemesController: PSViewController <UITableViewDelegate, UITableViewDataSource> {
164 UITableView *_tableView;
165 NSMutableArray *_themes;
168 @property (nonatomic, retain) NSMutableArray *themes;
170 - (id) initForContentSize:(CGSize)size;
172 - (id) navigationTitle;
173 - (void) themesChanged;
175 - (int) numberOfSectionsInTableView:(UITableView *)tableView;
176 - (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section;
177 - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section;
178 - (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
179 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
180 - (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath;
181 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
182 - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
183 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
187 @implementation WBSThemesController
189 @synthesize themes = _themes;
191 + (void) initialize {
192 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
193 checkImage = [_UIImageWithName(@"UIPreferencesBlueCheck.png") retain];
194 uncheckedImage = [[UIImage imageWithContentsOfFile:@"/System/Library/PreferenceBundles/WinterBoardSettings.bundle/SearchResultsCheckmarkClear.png"] retain];
198 - (id) initForContentSize:(CGSize)size {
199 if ((self = [super initForContentSize:size]) != nil) {
200 self.themes = [_settings objectForKey:@"Themes"];
202 if (NSString *theme = [_settings objectForKey:@"Theme"]) {
203 self.themes = [NSMutableArray arrayWithObject:
204 [NSMutableDictionary dictionaryWithObjectsAndKeys:
206 [NSNumber numberWithBool:YES], @"Active", nil]];
207 [_settings removeObjectForKey:@"Theme"];
210 self.themes = [NSMutableArray array];
211 [_settings setObject:_themes forKey:@"Themes"];
214 NSMutableArray *themesOnDisk([NSMutableArray array]);
215 AddThemes(themesOnDisk, @"/Library/Themes");
216 AddThemes(themesOnDisk, [NSString stringWithFormat:@"%@/Library/SummerBoard/Themes", NSHomeDirectory()]);
218 for (int i = 0, count = [themesOnDisk count]; i < count; i++) {
219 NSString *theme = [themesOnDisk objectAtIndex:i];
220 if ([theme hasSuffix:@".theme"])
221 [themesOnDisk replaceObjectAtIndex:i withObject:[theme stringByDeletingPathExtension]];
224 NSMutableSet *themesSet([NSMutableSet set]);
226 for (int i = 0, count = [_themes count]; i < count; i++) {
227 NSDictionary *theme([_themes objectAtIndex:i]);
228 NSString *name([theme objectForKey:@"Name"]);
230 if (!name || ![themesOnDisk containsObject:name]) {
231 [_themes removeObjectAtIndex:i];
235 [themesSet addObject:name];
239 for (NSString *theme in themesOnDisk) {
240 if ([themesSet containsObject:theme])
242 [themesSet addObject:theme];
244 [_themes insertObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
246 [NSNumber numberWithBool:NO], @"Active",
250 _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-64) style:UITableViewStyleGrouped];
251 [_tableView setDataSource:self];
252 [_tableView setDelegate:self];
253 [_tableView setEditing:YES];
254 [_tableView setAllowsSelectionDuringEditing:YES];
255 if ([self respondsToSelector:@selector(setView:)])
256 [self setView:_tableView];
262 [_tableView release];
267 - (id) navigationTitle {
275 - (void) themesChanged {
276 settingsChanged = YES;
279 /* UITableViewDelegate / UITableViewDataSource Methods {{{ */
280 - (int) numberOfSectionsInTableView:(UITableView *)tableView {
284 - (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section {
288 - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section {
289 return _themes.count;
292 - (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
293 WBSThemesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThemeCell"];
295 cell = [[[WBSThemesTableViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100) reuseIdentifier:@"ThemeCell"] autorelease];
296 //[cell setTableViewStyle:UITableViewCellStyleDefault];
299 NSDictionary *theme([_themes objectAtIndex:indexPath.row]);
300 [cell setTheme:theme];
304 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
305 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
306 NSMutableDictionary *theme = [_themes objectAtIndex:indexPath.row];
307 NSNumber *active = [theme objectForKey:@"Active"];
308 BOOL inactive = active == nil || ![active boolValue];
309 [theme setObject:[NSNumber numberWithBool:inactive] forKey:@"Active"];
310 [cell setImage:(!inactive ? uncheckedImage : checkImage)];
311 [tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES];
312 [self themesChanged];
315 - (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
316 NSUInteger fromIndex = [fromIndexPath row];
317 NSUInteger toIndex = [toIndexPath row];
318 if (fromIndex == toIndex)
320 NSMutableDictionary *theme = [[[_themes objectAtIndex:fromIndex] retain] autorelease];
321 [_themes removeObjectAtIndex:fromIndex];
322 [_themes insertObject:theme atIndex:toIndex];
323 [self themesChanged];
326 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
327 return UITableViewCellEditingStyleNone;
330 - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
334 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
341 @interface WBAdvancedController: PSListController {
345 - (void) settingsChanged;
349 @implementation WBAdvancedController
353 _specifiers = [[self loadSpecifiersFromPlistName:@"Advanced" target:self] retain];
357 - (void) settingsChanged {
358 settingsChanged = YES;
361 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec {
362 NSString *key([spec propertyForKey:@"key"]);
363 if ([[spec propertyForKey:@"negate"] boolValue])
364 value = [NSNumber numberWithBool:(![value boolValue])];
365 [_settings setValue:value forKey:key];
366 [self settingsChanged];
369 - (id) readPreferenceValue:(PSSpecifier *)spec {
370 NSString *key([spec propertyForKey:@"key"]);
371 id defaultValue([spec propertyForKey:@"default"]);
372 id plistValue([_settings objectForKey:key]);
375 if ([[spec propertyForKey:@"negate"] boolValue])
376 plistValue = [NSNumber numberWithBool:(![plistValue boolValue])];
380 - (void) __optimizeThemes {
381 system("/usr/libexec/winterboard/Optimize");
384 - (void) optimizeThemes {
385 UIAlertView *alert([[[UIAlertView alloc]
386 initWithTitle:@"Optimize Themes"
387 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."
389 cancelButtonTitle:@"Cancel"
390 otherButtonTitles:@"Optimize", nil
393 [alert setContext:@"optimize"];
394 [alert setNumberOfRows:1];
398 - (void) _optimizeThemes {
399 UIView *view([self view]);
400 UIWindow *window([view window]);
402 UIProgressHUD *hud([[[UIProgressHUD alloc] initWithWindow:window] autorelease]);
403 [hud setText:@"Reticulating Splines\nPlease Wait (Minutes)"];
405 [window setUserInteractionEnabled:NO];
407 [window addSubview:hud];
409 [self wb$yieldToSelector:@selector(__optimizeThemes)];
410 [hud removeFromSuperview];
412 [window setUserInteractionEnabled:YES];
414 [self settingsChanged];
417 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
418 NSString *context([alert context]);
420 if ([context isEqualToString:@"optimize"]) {
421 if (button == [alert firstOtherButtonIndex]) {
422 [self performSelector:@selector(_optimizeThemes) withObject:nil afterDelay:0];
425 [alert dismissWithClickedButtonIndex:-1 animated:YES];
427 /*else if ([super respondsToSelector:@selector(alertView:clickedButtonAtIndex:)])
428 [super alertView:alert clickedButtonAtIndex:button];*/
433 @interface WBSettingsController: PSListController {
436 - (id) initForContentSize:(CGSize)size;
439 - (void) navigationBarButtonClicked:(int)buttonIndex;
440 - (void) viewWillRedisplay;
441 - (void) pushController:(id)controller;
443 - (void) settingsChanged;
444 - (NSString *) title;
445 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec;
446 - (id) readPreferenceValue:(PSSpecifier *)spec;
450 @implementation WBSettingsController
452 - (void) _wb$loadSettings {
453 _plist = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.WinterBoard.plist", NSHomeDirectory()] retain];
454 _settings = [NSMutableDictionary dictionaryWithContentsOfFile:_plist];
457 if (_settings != nil)
461 _settings = [NSMutableDictionary dictionary];
464 _settings = [_settings retain];
466 if ([_settings objectForKey:@"SummerBoard"] == nil)
467 [_settings setObject:[NSNumber numberWithBool:set] forKey:@"SummerBoard"];
470 - (id) initForContentSize:(CGSize)size {
471 if ((self = [super initForContentSize:size]) != nil) {
472 [self _wb$loadSettings];
483 if (!settingsChanged)
486 NSData *data([NSPropertyListSerialization dataFromPropertyList:_settings format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL]);
489 if (![data writeToFile:_plist options:NSAtomicWrite error:NULL])
492 unlink("/User/Library/Caches/com.apple.springboard-imagecache-icons");
493 unlink("/User/Library/Caches/com.apple.springboard-imagecache-icons.plist");
494 unlink("/User/Library/Caches/com.apple.springboard-imagecache-smallicons");
495 unlink("/User/Library/Caches/com.apple.springboard-imagecache-smallicons.plist");
497 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen");
498 unlink("/User/Library/Caches/com.apple.SpringBoard.notificationCenterLinen");
500 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.0");
501 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.1");
502 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.2");
503 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.3");
505 system("rm -rf /User/Library/Caches/SpringBoardIconCache");
506 system("rm -rf /User/Library/Caches/SpringBoardIconCache-small");
507 system("rm -rf /User/Library/Caches/com.apple.IconsCache");
508 system("rm -rf /User/Library/Caches/com.apple.newsstand");
509 system("rm -rf /User/Library/Caches/com.apple.springboard.sharedimagecache");
510 system("rm -rf /User/Library/Caches/com.apple.UIStatusBar");
512 system("rm -rf /User/Library/Caches/BarDialer");
513 system("rm -rf /User/Library/Caches/BarDialer_selected");
514 system("rm -rf /User/Library/Caches/BarRecents");
515 system("rm -rf /User/Library/Caches/BarRecents_selected");
516 system("rm -rf /User/Library/Caches/BarVM");
517 system("rm -rf /User/Library/Caches/BarVM_selected");
519 system("killall -9 lsd");
521 if (kCFCoreFoundationVersionNumber > 700) // XXX: iOS 6.x
522 system("killall backboardd");
524 system("killall SpringBoard");
527 - (void) cancelChanges {
531 [self _wb$loadSettings];
533 [self reloadSpecifiers];
534 if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) {
535 [[self navigationItem] setLeftBarButtonItem:nil];
536 [[self navigationItem] setRightBarButtonItem:nil];
538 [self showLeftButton:nil withStyle:0 rightButton:nil withStyle:0];
540 settingsChanged = NO;
543 - (void) navigationBarButtonClicked:(int)buttonIndex {
544 if (!settingsChanged) {
545 [super navigationBarButtonClicked:buttonIndex];
549 if (buttonIndex == 0) {
550 [self cancelChanges];
555 [self.rootController popController];
558 - (void) settingsConfirmButtonClicked:(UIBarButtonItem *)button {
559 [self navigationBarButtonClicked:button.tag];
562 - (void) viewWillRedisplay {
564 [self settingsChanged];
565 [super viewWillRedisplay];
568 - (void) viewWillAppear:(BOOL)animated {
570 [self settingsChanged];
571 if ([super respondsToSelector:@selector(viewWillAppear:)])
572 [super viewWillAppear:animated];
575 - (void) pushController:(id)controller {
576 [self hideNavigationBarButtons];
577 [super pushController:controller];
582 NSMutableArray *specifiers([NSMutableArray array]);
583 for (PSSpecifier *specifier in [self loadSpecifiersFromPlistName:@"WinterBoard" target:self]) {
584 if (NSArray *version = [specifier propertyForKey:@"wb$filter"]) {
585 size_t count([version count]);
586 if (count == 0 || count > 2)
589 double lower([[version objectAtIndex:0] doubleValue]);
590 if (kCFCoreFoundationVersionNumber < lower)
594 double upper([[version objectAtIndex:1] doubleValue]);
595 if (upper <= kCFCoreFoundationVersionNumber)
599 [specifiers addObject:specifier];
601 _specifiers = [specifiers retain];
606 - (void) settingsChanged {
607 if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) {
608 UIBarButtonItem *respringButton([[UIBarButtonItem alloc] initWithTitle:@"Respring" style:UIBarButtonItemStyleDone target:self action:@selector(settingsConfirmButtonClicked:)]);
609 UIBarButtonItem *cancelButton([[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(settingsConfirmButtonClicked:)]);
610 cancelButton.tag = 0;
611 respringButton.tag = 1;
612 [[self navigationItem] setLeftBarButtonItem:respringButton];
613 [[self navigationItem] setRightBarButtonItem:cancelButton];
614 [respringButton release];
615 [cancelButton release];
617 [self showLeftButton:@"Respring" withStyle:2 rightButton:@"Cancel" withStyle:0];
619 settingsChanged = YES;
622 - (NSString *) title {
623 return @"WinterBoard";
626 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec {
627 NSString *key([spec propertyForKey:@"key"]);
628 if ([[spec propertyForKey:@"negate"] boolValue])
629 value = [NSNumber numberWithBool:(![value boolValue])];
630 [_settings setValue:value forKey:key];
631 [self settingsChanged];
634 - (id) readPreferenceValue:(PSSpecifier *)spec {
635 NSString *key([spec propertyForKey:@"key"]);
636 id defaultValue([spec propertyForKey:@"default"]);
637 id plistValue([_settings objectForKey:key]);
640 if ([[spec propertyForKey:@"negate"] boolValue])
641 plistValue = [NSNumber numberWithBool:(![plistValue boolValue])];
647 #define WBSAddMethod(_class, _sel, _imp, _type) \
648 if (![[_class class] instancesRespondToSelector:@selector(_sel)]) \
649 class_addMethod([_class class], @selector(_sel), (IMP)_imp, _type)
650 void $PSRootController$popController(PSRootController *self, SEL _cmd) {
651 [self popViewControllerAnimated:YES];
654 void $PSViewController$hideNavigationBarButtons(PSRootController *self, SEL _cmd) {
657 id $PSViewController$initForContentSize$(PSRootController *self, SEL _cmd, CGRect contentSize) {
661 static __attribute__((constructor)) void __wbsInit() {
662 WBSAddMethod(PSRootController, popController, $PSRootController$popController, "v@:");
663 WBSAddMethod(PSViewController, hideNavigationBarButtons, $PSViewController$hideNavigationBarButtons, "v@:");
664 WBSAddMethod(PSViewController, initForContentSize:, $PSViewController$initForContentSize$, "@@:{ff}");