1 /* CydgetScript - open-source lock screen multiplexer
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
7 * Cydget 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 * Cydget 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 Cydget. 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 NSMutableArray *specifiers([NSMutableArray array]);
343 for (PSSpecifier *specifier in [self loadSpecifiersFromPlistName:@"Cydget" target:self]) {
344 if (NSArray *version = [specifier propertyForKey:@"wb$filter"]) {
345 size_t count([version count]);
346 if (count == 0 || count > 2)
349 double lower([[version objectAtIndex:0] doubleValue]);
350 if (kCFCoreFoundationVersionNumber < lower)
354 double upper([[version objectAtIndex:1] doubleValue]);
355 if (upper <= kCFCoreFoundationVersionNumber)
359 [specifiers addObject:specifier];
361 _specifiers = [specifiers retain];
366 - (void) settingsChanged {
367 if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) {
368 UIBarButtonItem *respringButton([[UIBarButtonItem alloc] initWithTitle:@"Respring" style:UIBarButtonItemStyleDone target:self action:@selector(settingsConfirmButtonClicked:)]);
369 UIBarButtonItem *cancelButton([[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(settingsConfirmButtonClicked:)]);
370 cancelButton.tag = 0;
371 respringButton.tag = 1;
372 [[self navigationItem] setLeftBarButtonItem:respringButton];
373 [[self navigationItem] setRightBarButtonItem:cancelButton];
374 [respringButton release];
375 [cancelButton release];
377 [self showLeftButton:@"Respring" withStyle:2 rightButton:@"Cancel" withStyle:0];
379 settingsChanged = YES;
382 - (NSString *) title {
386 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec {
387 NSString *key([spec propertyForKey:@"key"]);
388 if ([[spec propertyForKey:@"negate"] boolValue])
389 value = [NSNumber numberWithBool:(![value boolValue])];
390 [_settings setValue:value forKey:key];
391 [self settingsChanged];
394 - (id) readPreferenceValue:(PSSpecifier *)spec {
395 NSString *key([spec propertyForKey:@"key"]);
396 id defaultValue([spec propertyForKey:@"default"]);
397 id plistValue([_settings objectForKey:key]);
400 if ([[spec propertyForKey:@"negate"] boolValue])
401 plistValue = [NSNumber numberWithBool:(![plistValue boolValue])];
407 #define WBSAddMethod(_class, _sel, _imp, _type) \
408 if (![[_class class] instancesRespondToSelector:@selector(_sel)]) \
409 class_addMethod([_class class], @selector(_sel), (IMP)_imp, _type)
410 void $PSRootController$popController(PSRootController *self, SEL _cmd) {
411 [self popViewControllerAnimated:YES];
414 void $PSViewController$hideNavigationBarButtons(PSRootController *self, SEL _cmd) {
417 id $PSViewController$initForContentSize$(PSRootController *self, SEL _cmd, CGRect contentSize) {
421 static __attribute__((constructor)) void __wbsInit() {
422 WBSAddMethod(PSRootController, popController, $PSRootController$popController, "v@:");
423 WBSAddMethod(PSViewController, hideNavigationBarButtons, $PSViewController$hideNavigationBarButtons, "v@:");
424 WBSAddMethod(PSViewController, initForContentSize:, $PSViewController$initForContentSize$, "@@:{ff}");