Avoid modifying stylesheets when updating @medias.
[cydget.git] / CydgetSettings.mm
1 /* CydgetScript - open-source lock screen multiplexer
2  * Copyright (C) 2009-2015  Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
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.
11  *
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.
16  *
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/>.
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                 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)
102                             continue;
103
104                         double lower([[version objectAtIndex:0] doubleValue]);
105                         if (kCFCoreFoundationVersionNumber < lower)
106                             continue;
107
108                         if (count != 1) {
109                             double upper([[version objectAtIndex:1] doubleValue]);
110                             if (upper <= kCFCoreFoundationVersionNumber)
111                                 continue;
112                         }
113                     }
114
115                     [themesOnDisk addObject:[theme stringByDeletingPathExtension]];
116                 }
117
118         NSMutableSet *themesSet([NSMutableSet set]);
119
120         for (int i = 0, count = [_themes count]; i < count; i++) {
121             NSDictionary *theme([_themes objectAtIndex:i]);
122             NSString *name([theme objectForKey:@"Name"]);
123
124             if (!name || ![themesOnDisk containsObject:name]) {
125                 [_themes removeObjectAtIndex:i];
126                 i--;
127                 count--;
128             } else {
129                 [themesSet addObject:name];
130             }
131         }
132
133         for (NSString *theme in themesOnDisk) {
134             if ([themesSet containsObject:theme])
135                 continue;
136             [themesSet addObject:theme];
137
138             [_themes insertObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:
139                     theme, @"Name",
140                     [NSNumber numberWithBool:NO], @"Active",
141             nil] atIndex:0];
142         }
143
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];
151     }
152     return self;
153 }
154
155 - (void) dealloc {
156     [_tableView release];
157     [_themes release];
158     [super dealloc];
159 }
160
161 - (id) navigationTitle {
162     return @"Lock Cydgets";
163 }
164
165 - (id) view {
166     return _tableView;
167 }
168
169 - (void) themesChanged {
170     settingsChanged = YES;
171 }
172
173 /* UITableViewDelegate / UITableViewDataSource Methods {{{ */
174 - (int) numberOfSectionsInTableView:(UITableView *)tableView {
175     return 1;
176 }
177
178 - (id) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section {
179     return nil;
180 }
181
182 - (int) tableView:(UITableView *)tableView numberOfRowsInSection:(int)section {
183     return _themes.count;
184 }
185
186 - (id) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
187     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ThemeCell"];
188     if (!cell) {
189         cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 100, 100) reuseIdentifier:@"ThemeCell"] autorelease];
190         //[cell setTableViewStyle:UITableViewCellStyleDefault];
191     }
192
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)];
199     return cell;
200 }
201
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];
211 }
212
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)
217         return;
218     NSMutableDictionary *theme = [[[_themes objectAtIndex:fromIndex] retain] autorelease];
219     [_themes removeObjectAtIndex:fromIndex];
220     [_themes insertObject:theme atIndex:toIndex];
221     [self themesChanged];
222 }
223
224 - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
225     return UITableViewCellEditingStyleNone;
226 }
227
228 - (BOOL) tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
229     return NO;
230 }
231
232 - (BOOL) tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
233     return YES;
234 }
235 /* }}} */
236 @end
237 /* }}} */
238
239 @interface CydgetSettingsController: PSListController {
240 }
241
242 - (id) initForContentSize:(CGSize)size;
243 - (void) dealloc;
244 - (void) suspend;
245 - (void) navigationBarButtonClicked:(int)buttonIndex;
246 - (void) viewWillRedisplay;
247 - (void) pushController:(id)controller;
248 - (id) specifiers;
249 - (void) settingsChanged;
250 - (NSString *) title;
251 - (void) setPreferenceValue:(id)value specifier:(PSSpecifier *)spec;
252 - (id) readPreferenceValue:(PSSpecifier *)spec;
253
254 @end
255
256 @implementation CydgetSettingsController
257
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];
262     } return self;
263 }
264
265 - (void) dealloc {
266     [_settings release];
267     [_plist release];
268     [super dealloc];
269 }
270
271 - (void) suspend {
272     if (!settingsChanged)
273         return;
274
275     NSData *data([NSPropertyListSerialization dataFromPropertyList:_settings format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL]);
276     if (!data)
277         return;
278     if (![data writeToFile:_plist options:NSAtomicWrite error:NULL])
279         return;
280
281     if (kCFCoreFoundationVersionNumber >= 700) // XXX: iOS 6.x
282         system("killall backboardd");
283     else
284         system("killall SpringBoard");
285 }
286
287 - (void) cancelChanges {
288     [_settings release];
289     [_plist release];
290     _plist = [[NSString stringWithFormat:@"%@/Library/Preferences/com.saurik.Cydget.plist", NSHomeDirectory()] retain];
291     _settings = [([NSMutableDictionary dictionaryWithContentsOfFile:_plist] ?: [NSMutableDictionary dictionary]) retain];
292
293     [self reloadSpecifiers];
294     if (![[PSViewController class] instancesRespondToSelector:@selector(showLeftButton:withStyle:rightButton:withStyle:)]) {
295         [[self navigationItem] setLeftBarButtonItem:nil];
296         [[self navigationItem] setRightBarButtonItem:nil];
297     } else {
298         [self showLeftButton:nil withStyle:0 rightButton:nil withStyle:0];
299     }
300     settingsChanged = NO;
301 }
302
303 - (void) navigationBarButtonClicked:(int)buttonIndex {
304     if (!settingsChanged) {
305         [super navigationBarButtonClicked:buttonIndex];
306         return;
307     }
308
309     if (buttonIndex == 0) {
310         [self cancelChanges];
311         return;
312     }
313
314     [self suspend];
315     [self.rootController popController];
316 }
317
318 - (void) settingsConfirmButtonClicked:(UIBarButtonItem *)button {
319     [self navigationBarButtonClicked:button.tag];
320 }
321
322 - (void) viewWillRedisplay {
323     if (settingsChanged)
324         [self settingsChanged];
325     [super viewWillRedisplay];
326 }
327
328 - (void) viewWillAppear:(BOOL)animated {
329     if (settingsChanged)
330         [self settingsChanged];
331     if ([super respondsToSelector:@selector(viewWillAppear:)])
332         [super viewWillAppear:animated];
333 }
334
335 - (void) pushController:(id)controller {
336     [self hideNavigationBarButtons];
337     [super pushController:controller];
338 }
339
340 - (id) specifiers {
341     if (!_specifiers) {
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)
347                     continue;
348
349                 double lower([[version objectAtIndex:0] doubleValue]);
350                 if (kCFCoreFoundationVersionNumber < lower)
351                     continue;
352
353                 if (count != 1) {
354                     double upper([[version objectAtIndex:1] doubleValue]);
355                     if (upper <= kCFCoreFoundationVersionNumber)
356                         continue;
357                 }
358             }
359             [specifiers addObject:specifier];
360         }
361         _specifiers = [specifiers retain];
362     }
363     return _specifiers;
364 }
365
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];
376     } else {
377         [self showLeftButton:@"Respring" withStyle:2 rightButton:@"Cancel" withStyle:0];
378     }
379     settingsChanged = YES;
380 }
381
382 - (NSString *) title {
383     return @"Cydget";
384 }
385
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];
392 }
393
394 - (id) readPreferenceValue:(PSSpecifier *)spec {
395     NSString *key([spec propertyForKey:@"key"]);
396     id defaultValue([spec propertyForKey:@"default"]);
397     id plistValue([_settings objectForKey:key]);
398     if (!plistValue)
399         return defaultValue;
400     if ([[spec propertyForKey:@"negate"] boolValue])
401         plistValue = [NSNumber numberWithBool:(![plistValue boolValue])];
402     return plistValue;
403 }
404
405 @end
406
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];
412 }
413
414 void $PSViewController$hideNavigationBarButtons(PSRootController *self, SEL _cmd) {
415 }
416
417 id $PSViewController$initForContentSize$(PSRootController *self, SEL _cmd, CGRect contentSize) {
418     return [self init];
419 }
420
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}");
425 }