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 void 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)
60 double lower([[version objectAtIndex:0] doubleValue]);
61 if (kCFCoreFoundationVersionNumber < lower)
65 double upper([[version objectAtIndex:1] doubleValue]);
66 if (upper <= kCFCoreFoundationVersionNumber)
72 [themesOnDisk addObject:theme];
76 /* [NSObject yieldToSelector:(withObject:)] {{{*/
77 @interface NSObject (wb$yieldToSelector)
78 - (id) wb$yieldToSelector:(SEL)selector withObject:(id)object;
79 - (id) wb$yieldToSelector:(SEL)selector;
82 @implementation NSObject (Cydia)
84 - (void) wb$doNothing {
87 - (void) wb$_yieldToContext:(NSMutableArray *)context {
88 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
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]));
94 /* XXX: deal with exceptions */
95 id value([self performSelector:selector withObject:object]);
97 NSMethodSignature *signature([self methodSignatureForSelector:selector]);
98 [context removeAllObjects];
99 if ([signature methodReturnLength] != 0 && value != nil)
100 [context addObject:value];
105 performSelectorOnMainThread:@selector(wb$doNothing)
113 - (id) wb$yieldToSelector:(SEL)selector withObject:(id)object {
114 /*return [self performSelector:selector withObject:object];*/
116 volatile bool stopped(false);
118 NSMutableArray *context([NSMutableArray arrayWithObjects:
119 [NSValue valueWithPointer:selector],
120 [NSValue valueWithNonretainedObject:object],
121 [NSValue valueWithPointer:const_cast<bool *>(&stopped)],
124 NSThread *thread([[[NSThread alloc]
126 selector:@selector(wb$_yieldToContext:)
132 NSRunLoop *loop([NSRunLoop currentRunLoop]);
133 NSDate *future([NSDate distantFuture]);
135 while (!stopped && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
137 return [context count] == 0 ? nil : [context objectAtIndex:0];
140 - (id) wb$yieldToSelector:(SEL)selector {
141 return [self wb$yieldToSelector:selector withObject:nil];
147 /* Theme Settings Controller {{{ */
148 @interface WBSThemesTableViewCell : UITableViewCell {
153 @implementation WBSThemesTableViewCell
155 - (id) initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuse {
156 if ((self = [super initWithFrame:frame reuseIdentifier:reuse]) != nil) {
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)];
170 @interface WBSThemesController: PSViewController <UITableViewDelegate, UITableViewDataSource> {
171 UITableView *_tableView;
172 NSMutableArray *_themes;
175 @property (nonatomic, retain) NSMutableArray *themes;
177 - (id) initForContentSize:(CGSize)size;
179 - (id) navigationTitle;
180 - (void) themesChanged;
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;
194 @implementation WBSThemesController
196 @synthesize themes = _themes;
198 + (void) initialize {
199 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
200 checkImage = [_UIImageWithName(@"UIPreferencesBlueCheck.png") retain];
201 uncheckedImage = [[UIImage imageWithContentsOfFile:@"/System/Library/PreferenceBundles/WinterBoardSettings.bundle/SearchResultsCheckmarkClear.png"] retain];
205 - (id) initForContentSize:(CGSize)size {
206 if ((self = [super initForContentSize:size]) != nil) {
207 self.themes = [_settings objectForKey:@"Themes"];
209 if (NSString *theme = [_settings objectForKey:@"Theme"]) {
210 self.themes = [NSMutableArray arrayWithObject:
211 [NSMutableDictionary dictionaryWithObjectsAndKeys:
213 [NSNumber numberWithBool:YES], @"Active", nil]];
214 [_settings removeObjectForKey:@"Theme"];
217 self.themes = [NSMutableArray array];
218 [_settings setObject:_themes forKey:@"Themes"];
221 NSMutableArray *themesOnDisk([NSMutableArray array]);
222 AddThemes(themesOnDisk, @"/Library/Themes");
223 AddThemes(themesOnDisk, [NSString stringWithFormat:@"%@/Library/SummerBoard/Themes", NSHomeDirectory()]);
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]];
231 NSMutableSet *themesSet([NSMutableSet set]);
233 for (int i = 0, count = [_themes count]; i < count; i++) {
234 NSDictionary *theme([_themes objectAtIndex:i]);
235 NSString *name([theme objectForKey:@"Name"]);
237 if (!name || ![themesOnDisk containsObject:name]) {
238 [_themes removeObjectAtIndex:i];
242 [themesSet addObject:name];
246 for (NSString *theme in themesOnDisk) {
247 if ([themesSet containsObject:theme])
249 [themesSet addObject:theme];
251 [_themes insertObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
253 [NSNumber numberWithBool:NO], @"Active",
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];
262 if ([self respondsToSelector:@selector(setView:)])
263 [self setView:_tableView];
269 [_tableView release];
274 - (id) navigationTitle {
282 - (void) themesChanged {
283 settingsChanged = YES;
286 /* UITableViewDelegate / UITableViewDataSource Methods {{{ */
287 - (int) numberOfSectionsInTableView:(UITableView *)tableView {
291 - (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section {
295 - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section {
296 return _themes.count;
299 - (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
300 WBSThemesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThemeCell"];
302 cell = [[[WBSThemesTableViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100) reuseIdentifier:@"ThemeCell"] autorelease];
303 //[cell setTableViewStyle:UITableViewCellStyleDefault];
306 NSDictionary *theme([_themes objectAtIndex:indexPath.row]);
307 [cell setTheme:theme];
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];
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)
327 NSMutableDictionary *theme = [[[_themes objectAtIndex:fromIndex] retain] autorelease];
328 [_themes removeObjectAtIndex:fromIndex];
329 [_themes insertObject:theme atIndex:toIndex];
330 [self themesChanged];
333 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
334 return UITableViewCellEditingStyleNone;
337 - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
341 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
348 @interface WBAdvancedController: PSListController {
352 - (void) settingsChanged;
356 @implementation WBAdvancedController
360 _specifiers = [[self loadSpecifiersFromPlistName:@"Advanced" target:self] retain];
364 - (void) settingsChanged {
365 settingsChanged = YES;
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];
376 - (id) readPreferenceValue:(PSSpecifier *)spec {
377 NSString *key([spec propertyForKey:@"key"]);
378 id defaultValue([spec propertyForKey:@"default"]);
379 id plistValue([_settings objectForKey:key]);
382 if ([[spec propertyForKey:@"negate"] boolValue])
383 plistValue = [NSNumber numberWithBool:(![plistValue boolValue])];
387 - (void) __optimizeThemes {
388 system("/usr/libexec/winterboard/Optimize");
391 - (void) optimizeThemes {
392 UIAlertView *alert([[[UIAlertView alloc]
393 initWithTitle:@"Optimize Themes"
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."
396 cancelButtonTitle:@"Cancel"
397 otherButtonTitles:@"Optimize", nil
400 [alert setContext:@"optimize"];
401 [alert setNumberOfRows:1];
405 - (void) _optimizeThemes {
406 UIView *view([self view]);
407 UIWindow *window([view window]);
409 UIProgressHUD *hud([[[UIProgressHUD alloc] initWithWindow:window] autorelease]);
410 [hud setText:@"Reticulating Splines\nPlease Wait (Minutes)"];
412 [window setUserInteractionEnabled:NO];
414 [window addSubview:hud];
416 [self wb$yieldToSelector:@selector(__optimizeThemes)];
417 [hud removeFromSuperview];
419 [window setUserInteractionEnabled:YES];
421 [self settingsChanged];
424 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
425 NSString *context([alert context]);
427 if ([context isEqualToString:@"optimize"]) {
428 if (button == [alert firstOtherButtonIndex]) {
429 [self performSelector:@selector(_optimizeThemes) withObject:nil afterDelay:0];
432 [alert dismissWithClickedButtonIndex:-1 animated:YES];
434 /*else if ([super respondsToSelector:@selector(alertView:clickedButtonAtIndex:)])
435 [super alertView:alert clickedButtonAtIndex:button];*/
440 @interface WBSettingsController: PSListController {
443 - (id) initForContentSize:(CGSize)size;
446 - (void) navigationBarButtonClicked:(int)buttonIndex;
447 - (void) viewWillRedisplay;
448 - (void) pushController:(id)controller;
450 - (void) settingsChanged;
451 - (NSString *) title;
452 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec;
453 - (id) readPreferenceValue:(PSSpecifier *)spec;
457 @implementation WBSettingsController
460 libhide = dlopen("/usr/lib/hide.dylib", RTLD_LAZY);
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"));
466 - (void) _wb$loadSettings {
467 _plist = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.WinterBoard.plist", NSHomeDirectory()] retain];
468 _settings = [NSMutableDictionary dictionaryWithContentsOfFile:_plist];
471 if (_settings != nil)
475 _settings = [NSMutableDictionary dictionary];
478 _settings = [_settings retain];
480 if ([_settings objectForKey:@"SummerBoard"] == nil)
481 [_settings setObject:[NSNumber numberWithBool:set] forKey:@"SummerBoard"];
484 [_settings setObject:[NSNumber numberWithBool:IsIconHiddenDisplayId(WinterBoardDisplayID)] forKey:@"IconHidden"];
487 - (id) initForContentSize:(CGSize)size {
488 if ((self = [super initForContentSize:size]) != nil) {
489 [self _wb$loadSettings];
500 if (!settingsChanged)
503 NSData *data([NSPropertyListSerialization dataFromPropertyList:_settings format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL]);
506 if (![data writeToFile:_plist options:NSAtomicWrite error:NULL])
510 ([[_settings objectForKey:@"IconHidden"] boolValue] ? HideIconViaDisplayId : UnHideIconViaDisplayId)(WinterBoardDisplayID);
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");
517 unlink("/User/Library/Caches/com.apple.SpringBoard.folderSwitcherLinen");
518 unlink("/User/Library/Caches/com.apple.SpringBoard.notificationCenterLinen");
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");
525 system("rm -rf /User/Library/Caches/SpringBoardIconCache");
526 system("rm -rf /User/Library/Caches/SpringBoardIconCache-small");
527 system("rm -rf /User/Library/Caches/com.apple.IconsCache");
528 system("rm -rf /User/Library/Caches/com.apple.newsstand");
529 system("rm -rf /User/Library/Caches/com.apple.springboard.sharedimagecache");
530 system("rm -rf /User/Library/Caches/com.apple.UIStatusBar");
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");
539 system("killall -9 lsd");
541 if (kCFCoreFoundationVersionNumber > 700) // XXX: iOS 6.x
542 system("killall backboardd");
544 system("killall SpringBoard");
547 - (void) cancelChanges {
551 [self _wb$loadSettings];
553 [self reloadSpecifiers];
554 if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) {
555 [[self navigationItem] setLeftBarButtonItem:nil];
556 [[self navigationItem] setRightBarButtonItem:nil];
558 [self showLeftButton:nil withStyle:0 rightButton:nil withStyle:0];
560 settingsChanged = NO;
563 - (void) navigationBarButtonClicked:(int)buttonIndex {
564 if (!settingsChanged) {
565 [super navigationBarButtonClicked:buttonIndex];
569 if (buttonIndex == 0) {
570 [self cancelChanges];
575 [self.rootController popController];
578 - (void) settingsConfirmButtonClicked:(UIBarButtonItem *)button {
579 [self navigationBarButtonClicked:button.tag];
582 - (void) viewWillRedisplay {
584 [self settingsChanged];
585 [super viewWillRedisplay];
588 - (void) viewWillAppear:(BOOL)animated {
590 [self settingsChanged];
591 if ([super respondsToSelector:@selector(viewWillAppear:)])
592 [super viewWillAppear:animated];
595 - (void) pushController:(id)controller {
596 [self hideNavigationBarButtons];
597 [super pushController:controller];
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)
609 double lower([[version objectAtIndex:0] doubleValue]);
610 if (kCFCoreFoundationVersionNumber < lower)
614 double upper([[version objectAtIndex:1] doubleValue]);
615 if (upper <= kCFCoreFoundationVersionNumber)
619 [specifiers addObject:specifier];
621 _specifiers = [specifiers retain];
626 - (void) settingsChanged {
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;
632 [[self navigationItem] setLeftBarButtonItem:respringButton];
633 [[self navigationItem] setRightBarButtonItem:cancelButton];
634 [respringButton release];
635 [cancelButton release];
637 [self showLeftButton:@"Respring" withStyle:2 rightButton:@"Cancel" withStyle:0];
639 settingsChanged = YES;
642 - (NSString *) title {
643 return @"WinterBoard";
646 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec {
647 NSString *key([spec propertyForKey:@"key"]);
648 if ([[spec propertyForKey:@"negate"] boolValue])
649 value = [NSNumber numberWithBool:(![value boolValue])];
650 [_settings setValue:value forKey:key];
651 [self settingsChanged];
654 - (id) readPreferenceValue:(PSSpecifier *)spec {
655 NSString *key([spec propertyForKey:@"key"]);
656 id defaultValue([spec propertyForKey:@"default"]);
657 id plistValue([_settings objectForKey:key]);
660 if ([[spec propertyForKey:@"negate"] boolValue])
661 plistValue = [NSNumber numberWithBool:(![plistValue boolValue])];
667 #define WBSAddMethod(_class, _sel, _imp, _type) \
668 if (![[_class class] instancesRespondToSelector:@selector(_sel)]) \
669 class_addMethod([_class class], @selector(_sel), (IMP)_imp, _type)
670 void $PSRootController$popController(PSRootController *self, SEL _cmd) {
671 [self popViewControllerAnimated:YES];
674 void $PSViewController$hideNavigationBarButtons(PSRootController *self, SEL _cmd) {
677 id $PSViewController$initForContentSize$(PSRootController *self, SEL _cmd, CGRect contentSize) {
681 static __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}");