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 if (NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:[NSString stringWithFormat:@"/System/Library/LockCydgets/%@/Info.plist", theme]]) {
99 if (NSArray *version = [info objectForKey:@"CYVersionFilter"]) {
100 size_t count([version count]);
101 if (count == 0 || count > 2)
104 double lower([[version objectAtIndex:0] doubleValue]);
105 if (kCFCoreFoundationVersionNumber < lower)
109 double upper([[version objectAtIndex:1] doubleValue]);
110 if (upper <= kCFCoreFoundationVersionNumber)
115 [themesOnDisk addObject:[theme stringByDeletingPathExtension]];
118 NSMutableSet *themesSet([NSMutableSet set]);
120 for (int i = 0, count = [_themes count]; i < count; i++) {
121 NSDictionary *theme([_themes objectAtIndex:i]);
122 NSString *name([theme objectForKey:@"Name"]);
124 if (!name || ![themesOnDisk containsObject:name]) {
125 [_themes removeObjectAtIndex:i];
129 [themesSet addObject:name];
133 for (NSString *theme in themesOnDisk) {
134 if ([themesSet containsObject:theme])
136 [themesSet addObject:theme];
138 [_themes insertObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
140 [NSNumber numberWithBool:NO], @"Active",
144 _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480-64) style:UITableViewStyleGrouped];
145 [_tableView setDataSource:self];
146 [_tableView setDelegate:self];
147 [_tableView setEditing:YES];
148 [_tableView setAllowsSelectionDuringEditing:YES];
149 if ([self respondsToSelector:@selector(setView:)])
150 [self setView:_tableView];
156 [_tableView release];
161 - (id) navigationTitle {
162 return @"Lock Cydgets";
169 - (void) themesChanged {
170 settingsChanged = YES;
173 /* UITableViewDelegate / UITableViewDataSource Methods {{{ */
174 - (int) numberOfSectionsInTableView:(UITableView *)tableView {
178 - (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section {
182 - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section {
183 return _themes.count;
186 - (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
187 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThemeCell"];
189 cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100) reuseIdentifier:@"ThemeCell"] autorelease];
190 //[cell setTableViewStyle:UITableViewCellStyleDefault];
193 NSDictionary *theme([_themes objectAtIndex:indexPath.row]);
194 cell.text = [theme objectForKey:@"Name"];
195 cell.hidesAccessoryWhenEditing = NO;
196 NSNumber *active([theme objectForKey:@"Active"]);
197 BOOL inactive(active == nil || ![active boolValue]);
198 [cell setImage:(inactive ? uncheckedImage : checkImage)];
202 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
203 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
204 NSMutableDictionary *theme = [_themes objectAtIndex:indexPath.row];
205 NSNumber *active = [theme objectForKey:@"Active"];
206 BOOL inactive = active == nil || ![active boolValue];
207 [theme setObject:[NSNumber numberWithBool:inactive] forKey:@"Active"];
208 [cell setImage:(!inactive ? uncheckedImage : checkImage)];
209 [tableView deselectRowAtIndexPath:(NSIndexPath *)indexPath animated:YES];
210 [self themesChanged];
213 - (void) tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
214 NSUInteger fromIndex = [fromIndexPath row];
215 NSUInteger toIndex = [toIndexPath row];
216 if (fromIndex == toIndex)
218 NSMutableDictionary *theme = [[[_themes objectAtIndex:fromIndex] retain] autorelease];
219 [_themes removeObjectAtIndex:fromIndex];
220 [_themes insertObject:theme atIndex:toIndex];
221 [self themesChanged];
224 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
225 return UITableViewCellEditingStyleNone;
228 - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
232 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
239 @interface CydgetSettingsController: PSListController {
242 - (id) initForContentSize:(CGSize)size;
245 - (void) navigationBarButtonClicked:(int)buttonIndex;
246 - (void) viewWillRedisplay;
247 - (void) pushController:(id)controller;
249 - (void) settingsChanged;
250 - (NSString *) title;
251 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec;
252 - (id) readPreferenceValue:(PSSpecifier *)spec;
256 @implementation CydgetSettingsController
258 - (id) initForContentSize:(CGSize)size {
259 if ((self = [super initForContentSize:size]) != nil) {
260 _plist = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Cydget.plist", NSHomeDirectory()] retain];
261 _settings = [([NSMutableDictionary dictionaryWithContentsOfFile:_plist] ?: [NSMutableDictionary dictionary]) retain];
272 if (!settingsChanged)
275 NSData *data([NSPropertyListSerialization dataFromPropertyList:_settings format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL]);
278 if (![data writeToFile:_plist options:NSAtomicWrite error:NULL])
281 if (kCFCoreFoundationVersionNumber >= 700) // XXX: iOS 6.x
282 system("killall backboardd");
284 system("killall SpringBoard");
287 - (void) cancelChanges {
290 _plist = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Cydget.plist", NSHomeDirectory()] retain];
291 _settings = [([NSMutableDictionary dictionaryWithContentsOfFile:_plist] ?: [NSMutableDictionary dictionary]) retain];
293 [self reloadSpecifiers];
294 if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) {
295 [[self navigationItem] setLeftBarButtonItem:nil];
296 [[self navigationItem] setRightBarButtonItem:nil];
298 [self showLeftButton:nil withStyle:0 rightButton:nil withStyle:0];
300 settingsChanged = NO;
303 - (void) navigationBarButtonClicked:(int)buttonIndex {
304 if (!settingsChanged) {
305 [super navigationBarButtonClicked:buttonIndex];
309 if (buttonIndex == 0) {
310 [self cancelChanges];
315 [self.rootController popController];
318 - (void) settingsConfirmButtonClicked:(UIBarButtonItem *)button {
319 [self navigationBarButtonClicked:button.tag];
322 - (void) viewWillRedisplay {
324 [self settingsChanged];
325 [super viewWillRedisplay];
328 - (void) viewWillAppear:(BOOL)animated {
330 [self settingsChanged];
331 if ([super respondsToSelector:@selector(viewWillAppear:)])
332 [super viewWillAppear:animated];
335 - (void) pushController:(id)controller {
336 [self hideNavigationBarButtons];
337 [super pushController:controller];
342 _specifiers = [[self loadSpecifiersFromPlistName:@"Cydget" target:self] retain];
346 - (void) settingsChanged {
347 if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) {
348 UIBarButtonItem *respringButton([[UIBarButtonItem alloc] initWithTitle:@"Respring" style:UIBarButtonItemStyleDone target:self action:@selector(settingsConfirmButtonClicked:)]);
349 UIBarButtonItem *cancelButton([[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(settingsConfirmButtonClicked:)]);
350 cancelButton.tag = 0;
351 respringButton.tag = 1;
352 [[self navigationItem] setLeftBarButtonItem:respringButton];
353 [[self navigationItem] setRightBarButtonItem:cancelButton];
354 [respringButton release];
355 [cancelButton release];
357 [self showLeftButton:@"Respring" withStyle:2 rightButton:@"Cancel" withStyle:0];
359 settingsChanged = YES;
362 - (NSString *) title {
366 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec {
367 NSString *key([spec propertyForKey:@"key"]);
368 if ([[spec propertyForKey:@"negate"] boolValue])
369 value = [NSNumber numberWithBool:(![value boolValue])];
370 [_settings setValue:value forKey:key];
371 [self settingsChanged];
374 - (id) readPreferenceValue:(PSSpecifier *)spec {
375 NSString *key([spec propertyForKey:@"key"]);
376 id defaultValue([spec propertyForKey:@"default"]);
377 id plistValue([_settings objectForKey:key]);
380 if ([[spec propertyForKey:@"negate"] boolValue])
381 plistValue = [NSNumber numberWithBool:(![plistValue boolValue])];
387 #define WBSAddMethod(_class, _sel, _imp, _type) \
388 if (![[_class class] instancesRespondToSelector:@selector(_sel)]) \
389 class_addMethod([_class class], @selector(_sel), (IMP)_imp, _type)
390 void $PSRootController$popController(PSRootController *self, SEL _cmd) {
391 [self popViewControllerAnimated:YES];
394 void $PSViewController$hideNavigationBarButtons(PSRootController *self, SEL _cmd) {
397 id $PSViewController$initForContentSize$(PSRootController *self, SEL _cmd, CGRect contentSize) {
401 static __attribute__((constructor)) void __wbsInit() {
402 WBSAddMethod(PSRootController, popController, $PSRootController$popController, "v@:");
403 WBSAddMethod(PSViewController, hideNavigationBarButtons, $PSViewController$hideNavigationBarButtons, "v@:");
404 WBSAddMethod(PSViewController, initForContentSize:, $PSViewController$initForContentSize$, "@@:{ff}");