1 /* CydgetScript - open-source lock screen multiplexer
2 * Copyright (C) 2009-2014 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
7 * Cydia is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
12 * Cydia is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Cydia. 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 /* Theme Settings Controller {{{ */
45 @interface CydgetOrderController: PSViewController <UITableViewDelegate, UITableViewDataSource> {
46 UITableView *_tableView;
47 NSMutableArray *_themes;
50 @property (nonatomic, retain) NSMutableArray *themes;
54 - (id) initForContentSize:(CGSize)size;
56 - (id) navigationTitle;
57 - (void) themesChanged;
59 - (int) numberOfSectionsInTableView:(UITableView *)tableView;
60 - (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section;
61 - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section;
62 - (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
63 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
64 - (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath;
65 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;
66 - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath;
67 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
71 @implementation CydgetOrderController
73 @synthesize themes = _themes;
76 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
77 checkImage = [_UIImageWithName(@"UIPreferencesBlueCheck.png") retain];
78 uncheckedImage = [[UIImage imageWithContentsOfFile:@"/System/Library/PreferenceBundles/CydgetSettings.bundle/SearchResultsCheckmarkClear.png"] retain];
82 - (id) initForContentSize:(CGSize)size {
83 if ((self = [super initForContentSize:size]) != nil) {
84 self.themes = [_settings objectForKey:@"LockCydgets"];
86 self.themes = [NSMutableArray arrayWithObjects:[NSMutableDictionary dictionaryWithObjectsAndKeys:
87 @"Welcome", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
88 ], [NSMutableDictionary dictionaryWithObjectsAndKeys:
89 @"AwayView", @"Name", [NSNumber numberWithBool:YES], @"Active", nil
92 [_settings setObject:_themes forKey:@"LockCydgets"];
95 NSMutableArray *themesOnDisk([NSMutableArray arrayWithCapacity:4]);
96 for (NSString *theme in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"/System/Library/LockCydgets" error:NULL])
97 if ([theme hasSuffix:@".cydget"])
98 [themesOnDisk addObject:[theme stringByDeletingPathExtension]];
100 NSMutableSet *themesSet([NSMutableSet set]);
102 for (int i = 0, count = [_themes count]; i < count; i++) {
103 NSDictionary *theme([_themes objectAtIndex:i]);
104 NSString *name([theme objectForKey:@"Name"]);
106 if (!name || ![themesOnDisk containsObject:name]) {
107 [_themes removeObjectAtIndex:i];
111 [themesSet addObject:name];
115 for (NSString *theme in themesOnDisk) {
116 if ([themesSet containsObject:theme])
118 [themesSet addObject:theme];
120 [_themes insertObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
122 [NSNumber numberWithBool:NO], @"Active",
126 _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-64) style:UITableViewStyleGrouped];
127 [_tableView setDataSource:self];
128 [_tableView setDelegate:self];
129 [_tableView setEditing:YES];
130 [_tableView setAllowsSelectionDuringEditing:YES];
131 if ([self respondsToSelector:@selector(setView:)])
132 [self setView:_tableView];
138 [_tableView release];
143 - (id) navigationTitle {
144 return @"Lock Cydgets";
151 - (void) themesChanged {
152 settingsChanged = YES;
155 /* UITableViewDelegate / UITableViewDataSource Methods {{{ */
156 - (int) numberOfSectionsInTableView:(UITableView *)tableView {
160 - (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section {
164 - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section {
165 return _themes.count;
168 - (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
169 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThemeCell"];
171 cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100) reuseIdentifier:@"ThemeCell"] autorelease];
172 //[cell setTableViewStyle:UITableViewCellStyleDefault];
175 NSDictionary *theme([_themes objectAtIndex:indexPath.row]);
176 cell.text = [theme objectForKey:@"Name"];
177 cell.hidesAccessoryWhenEditing = NO;
178 NSNumber *active([theme objectForKey:@"Active"]);
179 BOOL inactive(active == nil || ![active boolValue]);
180 [cell setImage:(inactive ? uncheckedImage : checkImage)];
184 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
185 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
186 NSMutableDictionary *theme = [_themes objectAtIndex:indexPath.row];
187 NSNumber *active = [theme objectForKey:@"Active"];
188 BOOL inactive = active == nil || ![active boolValue];
189 [theme setObject:[NSNumber numberWithBool:inactive] forKey:@"Active"];
190 [cell setImage:(!inactive ? uncheckedImage : checkImage)];
191 [tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES];
192 [self themesChanged];
195 - (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
196 NSUInteger fromIndex = [fromIndexPath row];
197 NSUInteger toIndex = [toIndexPath row];
198 if (fromIndex == toIndex)
200 NSMutableDictionary *theme = [[[_themes objectAtIndex:fromIndex] retain] autorelease];
201 [_themes removeObjectAtIndex:fromIndex];
202 [_themes insertObject:theme atIndex:toIndex];
203 [self themesChanged];
206 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
207 return UITableViewCellEditingStyleNone;
210 - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
214 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
221 @interface CydgetSettingsController: PSListController {
224 - (id) initForContentSize:(CGSize)size;
227 - (void) navigationBarButtonClicked:(int)buttonIndex;
228 - (void) viewWillRedisplay;
229 - (void) pushController:(id)controller;
231 - (void) settingsChanged;
232 - (NSString *) title;
233 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec;
234 - (id) readPreferenceValue:(PSSpecifier *)spec;
238 @implementation CydgetSettingsController
240 - (id) initForContentSize:(CGSize)size {
241 if ((self = [super initForContentSize:size]) != nil) {
242 _plist = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Cydget.plist", NSHomeDirectory()] retain];
243 _settings = [([NSMutableDictionary dictionaryWithContentsOfFile:_plist] ?: [NSMutableDictionary dictionary]) retain];
254 if (!settingsChanged)
257 NSData *data([NSPropertyListSerialization dataFromPropertyList:_settings format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL]);
260 if (![data writeToFile:_plist options:NSAtomicWrite error:NULL])
263 if (kCFCoreFoundationVersionNumber >= 700) // XXX: iOS 6.x
264 system("killall backboardd");
266 system("killall SpringBoard");
269 - (void) cancelChanges {
272 _plist = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Cydget.plist", NSHomeDirectory()] retain];
273 _settings = [([NSMutableDictionary dictionaryWithContentsOfFile:_plist] ?: [NSMutableDictionary dictionary]) retain];
275 [self reloadSpecifiers];
276 if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) {
277 [[self navigationItem] setLeftBarButtonItem:nil];
278 [[self navigationItem] setRightBarButtonItem:nil];
280 [self showLeftButton:nil withStyle:0 rightButton:nil withStyle:0];
282 settingsChanged = NO;
285 - (void) navigationBarButtonClicked:(int)buttonIndex {
286 if (!settingsChanged) {
287 [super navigationBarButtonClicked:buttonIndex];
291 if (buttonIndex == 0) {
292 [self cancelChanges];
297 [self.rootController popController];
300 - (void) settingsConfirmButtonClicked:(UIBarButtonItem *)button {
301 [self navigationBarButtonClicked:button.tag];
304 - (void) viewWillRedisplay {
306 [self settingsChanged];
307 [super viewWillRedisplay];
310 - (void) viewWillAppear:(BOOL)animated {
312 [self settingsChanged];
313 if ([super respondsToSelector:@selector(viewWillAppear:)])
314 [super viewWillAppear:animated];
317 - (void) pushController:(id)controller {
318 [self hideNavigationBarButtons];
319 [super pushController:controller];
324 _specifiers = [[self loadSpecifiersFromPlistName:@"Cydget" target:self] retain];
328 - (void) settingsChanged {
329 if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) {
330 UIBarButtonItem *respringButton([[UIBarButtonItem alloc] initWithTitle:@"Respring" style:UIBarButtonItemStyleDone target:self action:@selector(settingsConfirmButtonClicked:)]);
331 UIBarButtonItem *cancelButton([[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(settingsConfirmButtonClicked:)]);
332 cancelButton.tag = 0;
333 respringButton.tag = 1;
334 [[self navigationItem] setLeftBarButtonItem:respringButton];
335 [[self navigationItem] setRightBarButtonItem:cancelButton];
336 [respringButton release];
337 [cancelButton release];
339 [self showLeftButton:@"Respring" withStyle:2 rightButton:@"Cancel" withStyle:0];
341 settingsChanged = YES;
344 - (NSString *) title {
348 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec {
349 NSString *key([spec propertyForKey:@"key"]);
350 if ([[spec propertyForKey:@"negate"] boolValue])
351 value = [NSNumber numberWithBool:(![value boolValue])];
352 [_settings setValue:value forKey:key];
353 [self settingsChanged];
356 - (id) readPreferenceValue:(PSSpecifier *)spec {
357 NSString *key([spec propertyForKey:@"key"]);
358 id defaultValue([spec propertyForKey:@"default"]);
359 id plistValue([_settings objectForKey:key]);
362 if ([[spec propertyForKey:@"negate"] boolValue])
363 plistValue = [NSNumber numberWithBool:(![plistValue boolValue])];
369 #define WBSAddMethod(_class, _sel, _imp, _type) \
370 if (![[_class class] instancesRespondToSelector:@selector(_sel)]) \
371 class_addMethod([_class class], @selector(_sel), (IMP)_imp, _type)
372 void $PSRootController$popController(PSRootController *self, SEL _cmd) {
373 [self popViewControllerAnimated:YES];
376 void $PSViewController$hideNavigationBarButtons(PSRootController *self, SEL _cmd) {
379 id $PSViewController$initForContentSize$(PSRootController *self, SEL _cmd, CGRect contentSize) {
383 static __attribute__((constructor)) void __wbsInit() {
384 WBSAddMethod(PSRootController, popController, $PSRootController$popController, "v@:");
385 WBSAddMethod(PSViewController, hideNavigationBarButtons, $PSViewController$hideNavigationBarButtons, "v@:");
386 WBSAddMethod(PSViewController, initForContentSize:, $PSViewController$initForContentSize$, "@@:{ff}");