]> git.saurik.com Git - cydget.git/blob - CydgetSettings.mm
Port Cydget to iOS 7, ARM64, Xcode 5, and cycc :/.
[cydget.git] / CydgetSettings.mm
1 /* CydgetScript - open-source lock screen multiplexer
2 * Copyright (C) 2009-2014 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
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.
11 *
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.
16 *
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/>.
19 **/
20 /* }}} */
21
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>
30
31 #include <dlfcn.h>
32 #include <objc/runtime.h>
33
34 extern NSString *PSTableCellKey;
35 extern "C" UIImage *_UIImageWithName(NSString *);
36
37 static UIImage *checkImage;
38 static UIImage *uncheckedImage;
39
40 static BOOL settingsChanged;
41 static NSMutableDictionary *_settings;
42 static NSString *_plist;
43
44 /* Theme Settings Controller {{{ */
45 @interface CydgetOrderController: PSViewController <UITableViewDelegate, UITableViewDataSource> {
46 UITableView *_tableView;
47 NSMutableArray *_themes;
48 }
49
50 @property (nonatomic, retain) NSMutableArray *themes;
51
52 + (void) load;
53
54 - (id) initForContentSize:(CGSize)size;
55 - (id) view;
56 - (id) navigationTitle;
57 - (void) themesChanged;
58
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;
68
69 @end
70
71 @implementation CydgetOrderController
72
73 @synthesize themes = _themes;
74
75 + (void) load {
76 NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]);
77 checkImage = [_UIImageWithName(@"UIPreferencesBlueCheck.png") retain];
78 uncheckedImage = [[UIImage imageWithContentsOfFile:@"/System/Library/PreferenceBundles/CydgetSettings.bundle/SearchResultsCheckmarkClear.png"] retain];
79 [pool release];
80 }
81
82 - (id) initForContentSize:(CGSize)size {
83 if ((self = [super initForContentSize:size]) != nil) {
84 self.themes = [_settings objectForKey:@"LockCydgets"];
85 if (!_themes) {
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
90 ], nil];
91
92 [_settings setObject:_themes forKey:@"LockCydgets"];
93 }
94
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]];
99
100 NSMutableSet *themesSet([NSMutableSet set]);
101
102 for (int i = 0, count = [_themes count]; i < count; i++) {
103 NSDictionary *theme([_themes objectAtIndex:i]);
104 NSString *name([theme objectForKey:@"Name"]);
105
106 if (!name || ![themesOnDisk containsObject:name]) {
107 [_themes removeObjectAtIndex:i];
108 i--;
109 count--;
110 } else {
111 [themesSet addObject:name];
112 }
113 }
114
115 for (NSString *theme in themesOnDisk) {
116 if ([themesSet containsObject:theme])
117 continue;
118 [themesSet addObject:theme];
119
120 [_themes insertObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
121 theme, @"Name",
122 [NSNumber numberWithBool:NO], @"Active",
123 nil] atIndex:0];
124 }
125
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];
133 }
134 return self;
135 }
136
137 - (void) dealloc {
138 [_tableView release];
139 [_themes release];
140 [super dealloc];
141 }
142
143 - (id) navigationTitle {
144 return @"Lock Cydgets";
145 }
146
147 - (id) view {
148 return _tableView;
149 }
150
151 - (void) themesChanged {
152 settingsChanged = YES;
153 }
154
155 /* UITableViewDelegate / UITableViewDataSource Methods {{{ */
156 - (int) numberOfSectionsInTableView:(UITableView *)tableView {
157 return 1;
158 }
159
160 - (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section {
161 return nil;
162 }
163
164 - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section {
165 return _themes.count;
166 }
167
168 - (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
169 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThemeCell"];
170 if (!cell) {
171 cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100) reuseIdentifier:@"ThemeCell"] autorelease];
172 //[cell setTableViewStyle:UITableViewCellStyleDefault];
173 }
174
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)];
181 return cell;
182 }
183
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];
193 }
194
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)
199 return;
200 NSMutableDictionary *theme = [[[_themes objectAtIndex:fromIndex] retain] autorelease];
201 [_themes removeObjectAtIndex:fromIndex];
202 [_themes insertObject:theme atIndex:toIndex];
203 [self themesChanged];
204 }
205
206 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
207 return UITableViewCellEditingStyleNone;
208 }
209
210 - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
211 return NO;
212 }
213
214 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
215 return YES;
216 }
217 /* }}} */
218 @end
219 /* }}} */
220
221 @interface CydgetSettingsController: PSListController {
222 }
223
224 - (id) initForContentSize:(CGSize)size;
225 - (void) dealloc;
226 - (void) suspend;
227 - (void) navigationBarButtonClicked:(int)buttonIndex;
228 - (void) viewWillRedisplay;
229 - (void) pushController:(id)controller;
230 - (id) specifiers;
231 - (void) settingsChanged;
232 - (NSString *) title;
233 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec;
234 - (id) readPreferenceValue:(PSSpecifier *)spec;
235
236 @end
237
238 @implementation CydgetSettingsController
239
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];
244 } return self;
245 }
246
247 - (void) dealloc {
248 [_settings release];
249 [_plist release];
250 [super dealloc];
251 }
252
253 - (void) suspend {
254 if (!settingsChanged)
255 return;
256
257 NSData *data([NSPropertyListSerialization dataFromPropertyList:_settings format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL]);
258 if (!data)
259 return;
260 if (![data writeToFile:_plist options:NSAtomicWrite error:NULL])
261 return;
262
263 if (kCFCoreFoundationVersionNumber >= 700) // XXX: iOS 6.x
264 system("killall backboardd");
265 else
266 system("killall SpringBoard");
267 }
268
269 - (void) cancelChanges {
270 [_settings release];
271 [_plist release];
272 _plist = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Cydget.plist", NSHomeDirectory()] retain];
273 _settings = [([NSMutableDictionary dictionaryWithContentsOfFile:_plist] ?: [NSMutableDictionary dictionary]) retain];
274
275 [self reloadSpecifiers];
276 if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) {
277 [[self navigationItem] setLeftBarButtonItem:nil];
278 [[self navigationItem] setRightBarButtonItem:nil];
279 } else {
280 [self showLeftButton:nil withStyle:0 rightButton:nil withStyle:0];
281 }
282 settingsChanged = NO;
283 }
284
285 - (void) navigationBarButtonClicked:(int)buttonIndex {
286 if (!settingsChanged) {
287 [super navigationBarButtonClicked:buttonIndex];
288 return;
289 }
290
291 if (buttonIndex == 0) {
292 [self cancelChanges];
293 return;
294 }
295
296 [self suspend];
297 [self.rootController popController];
298 }
299
300 - (void) settingsConfirmButtonClicked:(UIBarButtonItem *)button {
301 [self navigationBarButtonClicked:button.tag];
302 }
303
304 - (void) viewWillRedisplay {
305 if (settingsChanged)
306 [self settingsChanged];
307 [super viewWillRedisplay];
308 }
309
310 - (void) viewWillAppear:(BOOL)animated {
311 if (settingsChanged)
312 [self settingsChanged];
313 if ([super respondsToSelector:@selector(viewWillAppear:)])
314 [super viewWillAppear:animated];
315 }
316
317 - (void) pushController:(id)controller {
318 [self hideNavigationBarButtons];
319 [super pushController:controller];
320 }
321
322 - (id) specifiers {
323 if (!_specifiers)
324 _specifiers = [[self loadSpecifiersFromPlistName:@"Cydget" target:self] retain];
325 return _specifiers;
326 }
327
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];
338 } else {
339 [self showLeftButton:@"Respring" withStyle:2 rightButton:@"Cancel" withStyle:0];
340 }
341 settingsChanged = YES;
342 }
343
344 - (NSString *) title {
345 return @"Cydget";
346 }
347
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];
354 }
355
356 - (id) readPreferenceValue:(PSSpecifier *)spec {
357 NSString *key([spec propertyForKey:@"key"]);
358 id defaultValue([spec propertyForKey:@"default"]);
359 id plistValue([_settings objectForKey:key]);
360 if (!plistValue)
361 return defaultValue;
362 if ([[spec propertyForKey:@"negate"] boolValue])
363 plistValue = [NSNumber numberWithBool:(![plistValue boolValue])];
364 return plistValue;
365 }
366
367 @end
368
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];
374 }
375
376 void $PSViewController$hideNavigationBarButtons(PSRootController *self, SEL _cmd) {
377 }
378
379 id $PSViewController$initForContentSize$(PSRootController *self, SEL _cmd, CGRect contentSize) {
380 return [self init];
381 }
382
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}");
387 }