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>
33 #include <objc/runtime.h>
35 extern NSString *PSTableCellKey;
36 extern "C" UIImage *_UIImageWithName(NSString *);
38 static UIImage *checkImage;
39 static UIImage *uncheckedImage;
41 static BOOL settingsChanged;
42 static NSMutableDictionary *_settings;
43 static NSString *_plist;
45 void AddThemes(NSMutableArray *themesOnDisk, NSString *folder) {
46 NSArray *themes([[NSFileManager defaultManager] contentsOfDirectoryAtPath:folder error:NULL]);
47 for (NSString *theme in themes) {
48 if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"%@/%@/Info.plist", folder, theme]]) {
49 if (NSArray *version = [info objectForKey:@"CoreFoundationVersion"]) {
50 size_t count([version count]);
51 if (count == 0 || count > 2)
54 double lower([[version objectAtIndex:0] doubleValue]);
55 if (kCFCoreFoundationVersionNumber < lower)
59 double upper([[version objectAtIndex:1] doubleValue]);
60 if (upper <= kCFCoreFoundationVersionNumber)
66 [themesOnDisk addObject:theme];
70 static void RestartSpringBoard() {
71 unlink("/User/Library/Caches/com.apple.springboard-imagecache-icons");
72 unlink("/User/Library/Caches/com.apple.springboard-imagecache-icons.plist");
73 unlink("/User/Library/Caches/com.apple.springboard-imagecache-smallicons");
74 unlink("/User/Library/Caches/com.apple.springboard-imagecache-smallicons.plist");
76 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen");
77 unlink("/User/Library/Caches/com.apple.SpringBoard.notificationCenterLinen");
79 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.0");
80 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.1");
81 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.2");
82 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen.3");
84 system("rm -rf /User/Library/Caches/SpringBoardIconCache");
85 system("rm -rf /User/Library/Caches/SpringBoardIconCache-small");
86 system("rm -rf /User/Library/Caches/com.apple.IconsCache");
87 system("rm -rf /User/Library/Caches/com.apple.newsstand");
88 system("rm -rf /User/Library/Caches/com.apple.springboard.sharedimagecache");
89 system("rm -rf /User/Library/Caches/com.apple.UIStatusBar");
91 system("rm -rf /User/Library/Caches/BarDialer");
92 system("rm -rf /User/Library/Caches/BarDialer_selected");
93 system("rm -rf /User/Library/Caches/BarRecents");
94 system("rm -rf /User/Library/Caches/BarRecents_selected");
95 system("rm -rf /User/Library/Caches/BarVM");
96 system("rm -rf /User/Library/Caches/BarVM_selected");
98 system("killall -9 lsd");
100 if (kCFCoreFoundationVersionNumber > 700) // XXX: iOS 6.x
101 system("killall backboardd");
103 system("killall SpringBoard");
106 /* [NSObject yieldToSelector:(withObject:)] {{{*/
107 @interface NSObject (wb$yieldToSelector)
108 - (id) wb$yieldToSelector:(SEL)selector withObject:(id)object;
109 - (id) wb$yieldToSelector:(SEL)selector;
112 @implementation NSObject (Cydia)
114 - (void) wb$doNothing {
117 - (void) wb$_yieldToContext:(NSMutableArray *)context {
118 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
120 SEL selector(reinterpret_cast<SEL>([[context objectAtIndex:0] pointerValue]));
121 id object([[context objectAtIndex:1] nonretainedObjectValue]);
122 volatile bool &stopped(*reinterpret_cast<bool *>([[context objectAtIndex:2] pointerValue]));
124 /* XXX: deal with exceptions */
125 id value([self performSelector:selector withObject:object]);
127 NSMethodSignature *signature([self methodSignatureForSelector:selector]);
128 [context removeAllObjects];
129 if ([signature methodReturnLength] != 0 && value != nil)
130 [context addObject:value];
135 performSelectorOnMainThread:@selector(wb$doNothing)
143 - (id) wb$yieldToSelector:(SEL)selector withObject:(id)object {
144 /*return [self performSelector:selector withObject:object];*/
146 volatile bool stopped(false);
148 NSMutableArray *context([NSMutableArray arrayWithObjects:
149 [NSValue valueWithPointer:selector],
150 [NSValue valueWithNonretainedObject:object],
151 [NSValue valueWithPointer:const_cast<bool *>(&stopped)],
154 NSThread *thread([[[NSThread alloc]
156 selector:@selector(wb$_yieldToContext:)
162 NSRunLoop *loop([NSRunLoop currentRunLoop]);
163 NSDate *future([NSDate distantFuture]);
165 while (!stopped && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
167 return [context count] == 0 ? nil : [context objectAtIndex:0];
170 - (id) wb$yieldToSelector:(SEL)selector {
171 return [self wb$yieldToSelector:selector withObject:nil];
177 /* Theme Settings Controller {{{ */
178 @interface WBSThemesTableViewCell : UITableViewCell {
179 UIImageView *checkmark_;
186 @implementation WBSThemesTableViewCell
189 [checkmark_ release];
195 - (id) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuse {
196 if ((self = [super initWithFrame:frame reuseIdentifier:reuse]) != nil) {
197 CGFloat border(48), check(40), icon(64);
198 UIView *content([self contentView]);
199 CGSize size([content frame].size);
201 checkmark_ = [[UIImageView alloc] initWithFrame:CGRectMake(std::floor((border - check) / 2), 0, check, size.height)];
202 [checkmark_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
203 [content addSubview:checkmark_];
205 name_ = [[UILabel alloc] initWithFrame:CGRectMake(border, 0, 0, size.height)];
206 [name_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
207 [content addSubview:name_];
209 icon_ = [[UIImageView alloc] initWithFrame:CGRectMake(size.width - icon - 48, 0, icon, icon)];
210 [content addSubview:icon_];
214 - (void) setCheck:(bool)inactive {
215 [self setImage:(inactive ? uncheckedImage : checkImage)];
218 - (void) setTheme:(NSDictionary *)theme {
219 [name_ setText:[theme objectForKey:@"Name"]];
221 NSNumber *active([theme objectForKey:@"Active"]);
222 BOOL inactive(active == nil || ![active boolValue]);
223 [self setCheck:inactive];
225 CGRect area([name_ frame]);
226 area.size.width = ([icon_ image] == nil ? self.contentView.frame.size.width : icon_.frame.origin.x) - area.origin.x - 9;
227 [name_ setFrame:area];
232 @interface WBSThemesController: PSViewController <UITableViewDelegate, UITableViewDataSource> {
233 UITableView *_tableView;
234 NSMutableArray *_themes;
237 @property (nonatomic, retain) NSMutableArray *themes;
239 - (id) initForContentSize:(CGSize)size;
241 - (id) navigationTitle;
242 - (void) themesChanged;
244 - (int) numberOfSectionsInTableView:(UITableView *)tableView;
245 - (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section;
246 - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section;
247 - (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
248 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
249 - (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath;
250 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
251 - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
252 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
256 @implementation WBSThemesController
258 @synthesize themes = _themes;
260 + (void) initialize {
261 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
262 checkImage = [_UIImageWithName(@"UIPreferencesBlueCheck.png") retain];
263 uncheckedImage = [[UIImage imageWithContentsOfFile:@"/System/Library/PreferenceBundles/WinterBoardSettings.bundle/SearchResultsCheckmarkClear.png"] retain];
267 - (id) initForContentSize:(CGSize)size {
268 if ((self = [super initForContentSize:size]) != nil) {
269 self.themes = [_settings objectForKey:@"Themes"];
271 if (NSString *theme = [_settings objectForKey:@"Theme"]) {
272 self.themes = [NSMutableArray arrayWithObject:
273 [NSMutableDictionary dictionaryWithObjectsAndKeys:
275 [NSNumber numberWithBool:YES], @"Active", nil]];
276 [_settings removeObjectForKey:@"Theme"];
279 self.themes = [NSMutableArray array];
280 [_settings setObject:_themes forKey:@"Themes"];
283 NSMutableArray *themesOnDisk([NSMutableArray array]);
284 AddThemes(themesOnDisk, @"/Library/Themes");
285 AddThemes(themesOnDisk, [NSString stringWithFormat:@"%@/Library/SummerBoard/Themes", NSHomeDirectory()]);
287 for (int i = 0, count = [themesOnDisk count]; i < count; i++) {
288 NSString *theme = [themesOnDisk objectAtIndex:i];
289 if ([theme hasSuffix:@".theme"])
290 [themesOnDisk replaceObjectAtIndex:i withObject:[theme stringByDeletingPathExtension]];
293 NSMutableSet *themesSet([NSMutableSet set]);
295 for (int i = 0, count = [_themes count]; i < count; i++) {
296 NSDictionary *theme([_themes objectAtIndex:i]);
297 NSString *name([theme objectForKey:@"Name"]);
299 if (!name || ![themesOnDisk containsObject:name]) {
300 [_themes removeObjectAtIndex:i];
304 [themesSet addObject:name];
308 for (NSString *theme in themesOnDisk) {
309 if ([themesSet containsObject:theme])
311 [themesSet addObject:theme];
313 [_themes insertObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
315 [NSNumber numberWithBool:NO], @"Active",
319 _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-64) style:UITableViewStylePlain];
320 [_tableView setRowHeight:48];
321 [_tableView setDataSource:self];
322 [_tableView setDelegate:self];
323 [_tableView setEditing:YES];
324 [_tableView setAllowsSelectionDuringEditing:YES];
325 if ([self respondsToSelector:@selector(setView:)])
326 [self setView:_tableView];
332 [_tableView release];
337 - (id) navigationTitle {
345 - (void) themesChanged {
346 settingsChanged = YES;
349 /* UITableViewDelegate / UITableViewDataSource Methods {{{ */
350 - (int) numberOfSectionsInTableView:(UITableView *)tableView {
354 - (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section {
358 - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section {
359 return _themes.count;
362 - (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
363 WBSThemesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThemeCell"];
365 cell = [[[WBSThemesTableViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100) reuseIdentifier:@"ThemeCell"] autorelease];
366 //[cell setTableViewStyle:UITableViewCellStyleDefault];
369 NSDictionary *theme([_themes objectAtIndex:indexPath.row]);
370 [cell setTheme:theme];
374 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
375 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
376 NSMutableDictionary *theme = [_themes objectAtIndex:indexPath.row];
377 NSNumber *active = [theme objectForKey:@"Active"];
378 BOOL inactive = active == nil || ![active boolValue];
379 [theme setObject:[NSNumber numberWithBool:inactive] forKey:@"Active"];
380 [cell setImage:(!inactive ? uncheckedImage : checkImage)];
381 [tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES];
382 [self themesChanged];
385 - (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
386 NSUInteger fromIndex = [fromIndexPath row];
387 NSUInteger toIndex = [toIndexPath row];
388 if (fromIndex == toIndex)
390 NSMutableDictionary *theme = [[[_themes objectAtIndex:fromIndex] retain] autorelease];
391 [_themes removeObjectAtIndex:fromIndex];
392 [_themes insertObject:theme atIndex:toIndex];
393 [self themesChanged];
396 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
397 return UITableViewCellEditingStyleNone;
400 - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
404 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
411 @interface WBAdvancedController: PSListController {
415 - (void) settingsChanged;
419 @implementation WBAdvancedController
423 _specifiers = [[self loadSpecifiersFromPlistName:@"Advanced" target:self] retain];
427 - (void) settingsChanged {
428 settingsChanged = YES;
431 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec {
432 NSString *key([spec propertyForKey:@"key"]);
433 if ([[spec propertyForKey:@"negate"] boolValue])
434 value = [NSNumber numberWithBool:(![value boolValue])];
435 [_settings setValue:value forKey:key];
436 [self settingsChanged];
439 - (id) readPreferenceValue:(PSSpecifier *)spec {
440 NSString *key([spec propertyForKey:@"key"]);
441 id defaultValue([spec propertyForKey:@"default"]);
442 id plistValue([_settings objectForKey:key]);
445 if ([[spec propertyForKey:@"negate"] boolValue])
446 plistValue = [NSNumber numberWithBool:(![plistValue boolValue])];
450 - (void) restartSpringBoard {
451 RestartSpringBoard();
454 - (void) __optimizeThemes {
455 system("/usr/libexec/winterboard/Optimize");
458 - (void) optimizeThemes {
459 UIAlertView *alert([[[UIAlertView alloc]
460 initWithTitle:@"Optimize Themes"
461 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."
463 cancelButtonTitle:@"Cancel"
464 otherButtonTitles:@"Optimize", nil
467 [alert setContext:@"optimize"];
468 [alert setNumberOfRows:1];
472 - (void) _optimizeThemes {
473 UIView *view([self view]);
474 UIWindow *window([view window]);
476 UIProgressHUD *hud([[[UIProgressHUD alloc] initWithWindow:window] autorelease]);
477 [hud setText:@"Reticulating Splines\nPlease Wait (Minutes)"];
479 [window setUserInteractionEnabled:NO];
481 [window addSubview:hud];
483 [self wb$yieldToSelector:@selector(__optimizeThemes)];
484 [hud removeFromSuperview];
486 [window setUserInteractionEnabled:YES];
488 [self settingsChanged];
491 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
492 NSString *context([alert context]);
494 if ([context isEqualToString:@"optimize"]) {
495 if (button == [alert firstOtherButtonIndex]) {
496 [self performSelector:@selector(_optimizeThemes) withObject:nil afterDelay:0];
499 [alert dismissWithClickedButtonIndex:-1 animated:YES];
501 /*else if ([super respondsToSelector:@selector(alertView:clickedButtonAtIndex:)])
502 [super alertView:alert clickedButtonAtIndex:button];*/
507 @interface WBSettingsController: PSListController {
510 - (id) initForContentSize:(CGSize)size;
513 - (void) navigationBarButtonClicked:(int)buttonIndex;
514 - (void) viewWillRedisplay;
515 - (void) pushController:(id)controller;
517 - (void) settingsChanged;
518 - (NSString *) title;
519 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec;
520 - (id) readPreferenceValue:(PSSpecifier *)spec;
524 @implementation WBSettingsController
526 - (void) _wb$loadSettings {
527 _plist = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.WinterBoard.plist", NSHomeDirectory()] retain];
528 _settings = [NSMutableDictionary dictionaryWithContentsOfFile:_plist];
531 if (_settings != nil)
535 _settings = [NSMutableDictionary dictionary];
538 _settings = [_settings retain];
540 if ([_settings objectForKey:@"SummerBoard"] == nil)
541 [_settings setObject:[NSNumber numberWithBool:set] forKey:@"SummerBoard"];
544 - (id) initForContentSize:(CGSize)size {
545 if ((self = [super initForContentSize:size]) != nil) {
546 [self _wb$loadSettings];
557 if (!settingsChanged)
560 NSData *data([NSPropertyListSerialization dataFromPropertyList:_settings format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL]);
563 if (![data writeToFile:_plist options:NSAtomicWrite error:NULL])
566 RestartSpringBoard();
569 - (void) cancelChanges {
573 [self _wb$loadSettings];
575 [self reloadSpecifiers];
576 if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) {
577 [[self navigationItem] setLeftBarButtonItem:nil];
578 [[self navigationItem] setRightBarButtonItem:nil];
580 [self showLeftButton:nil withStyle:0 rightButton:nil withStyle:0];
582 settingsChanged = NO;
585 - (void) navigationBarButtonClicked:(int)buttonIndex {
586 if (!settingsChanged) {
587 [super navigationBarButtonClicked:buttonIndex];
591 if (buttonIndex == 0) {
592 [self cancelChanges];
597 [self.rootController popController];
600 - (void) settingsConfirmButtonClicked:(UIBarButtonItem *)button {
601 [self navigationBarButtonClicked:button.tag];
604 - (void) viewWillRedisplay {
606 [self settingsChanged];
607 [super viewWillRedisplay];
610 - (void) viewWillAppear:(BOOL)animated {
612 [self settingsChanged];
613 if ([super respondsToSelector:@selector(viewWillAppear:)])
614 [super viewWillAppear:animated];
617 - (void) pushController:(id)controller {
618 [self hideNavigationBarButtons];
619 [super pushController:controller];
624 NSMutableArray *specifiers([NSMutableArray array]);
625 for (PSSpecifier *specifier in [self loadSpecifiersFromPlistName:@"WinterBoard" target:self]) {
626 if (NSArray *version = [specifier propertyForKey:@"wb$filter"]) {
627 size_t count([version count]);
628 if (count == 0 || count > 2)
631 double lower([[version objectAtIndex:0] doubleValue]);
632 if (kCFCoreFoundationVersionNumber < lower)
636 double upper([[version objectAtIndex:1] doubleValue]);
637 if (upper <= kCFCoreFoundationVersionNumber)
641 [specifiers addObject:specifier];
643 _specifiers = [specifiers retain];
648 - (void) settingsChanged {
649 if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) {
650 UIBarButtonItem *respringButton([[UIBarButtonItem alloc] initWithTitle:@"Respring" style:UIBarButtonItemStyleDone target:self action:@selector(settingsConfirmButtonClicked:)]);
651 UIBarButtonItem *cancelButton([[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(settingsConfirmButtonClicked:)]);
652 cancelButton.tag = 0;
653 respringButton.tag = 1;
654 [[self navigationItem] setLeftBarButtonItem:respringButton];
655 [[self navigationItem] setRightBarButtonItem:cancelButton];
656 [respringButton release];
657 [cancelButton release];
659 [self showLeftButton:@"Respring" withStyle:2 rightButton:@"Cancel" withStyle:0];
661 settingsChanged = YES;
664 - (NSString *) title {
665 return @"WinterBoard";
668 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec {
669 NSString *key([spec propertyForKey:@"key"]);
670 if ([[spec propertyForKey:@"negate"] boolValue])
671 value = [NSNumber numberWithBool:(![value boolValue])];
672 [_settings setValue:value forKey:key];
673 [self settingsChanged];
676 - (id) readPreferenceValue:(PSSpecifier *)spec {
677 NSString *key([spec propertyForKey:@"key"]);
678 id defaultValue([spec propertyForKey:@"default"]);
679 id plistValue([_settings objectForKey:key]);
682 if ([[spec propertyForKey:@"negate"] boolValue])
683 plistValue = [NSNumber numberWithBool:(![plistValue boolValue])];
689 #define WBSAddMethod(_class, _sel, _imp, _type) \
690 if (![[_class class] instancesRespondToSelector:@selector(_sel)]) \
691 class_addMethod([_class class], @selector(_sel), (IMP)_imp, _type)
692 void $PSRootController$popController(PSRootController *self, SEL _cmd) {
693 [self popViewControllerAnimated:YES];
696 void $PSViewController$hideNavigationBarButtons(PSRootController *self, SEL _cmd) {
699 id $PSViewController$initForContentSize$(PSRootController *self, SEL _cmd, CGRect contentSize) {
703 static __attribute__((constructor)) void __wbsInit() {
704 WBSAddMethod(PSRootController, popController, $PSRootController$popController, "v@:");
705 WBSAddMethod(PSViewController, hideNavigationBarButtons, $PSViewController$hideNavigationBarButtons, "v@:");
706 WBSAddMethod(PSViewController, initForContentSize:, $PSViewController$initForContentSize$, "@@:{ff}");