forked from a3tweaks/Flipswitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FSSettingsController.m
357 lines (331 loc) · 13.1 KB
/
FSSettingsController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#import <UIKit/UIKit.h>
#import <notify.h>
#import "FSSwitchPanel.h"
#import "NSBundle+Flipswitch.h"
@interface PSSpecifier : NSObject {
@public
id target;
SEL getter;
SEL setter;
SEL action;
SEL cancel;
Class detailControllerClass;
int cellType;
Class editPaneClass;
int keyboardType;
int autoCapsType;
int autoCorrectionType;
unsigned textFieldType;
NSString *_name;
NSArray *_values;
NSDictionary *_titleDict;
NSDictionary *_shortTitleDict;
id _userInfo;
NSMutableDictionary *_properties;
@private
SEL _confirmationAction;
SEL _confirmationCancelAction;
SEL _buttonAction;
SEL _controllerLoadAction;
BOOL _showContentString;
}
+ (int)keyboardTypeForString:(NSString *)string;
+ (int)autoCapsTypeForString:(NSString *)string;
+ (int)autoCorrectionTypeForNumber:(NSNumber *)number;
+ (PSSpecifier *)emptyGroupSpecifier;
+ (PSSpecifier *)groupSpecifierWithName:(NSString *)name;
+ (PSSpecifier *)preferenceSpecifierNamed:(NSString *)name target:(id)target set:(SEL)set get:(SEL)get detail:(Class)detail cell:(int)cell edit:(Class)edit;
+ (PSSpecifier *)deleteButtonSpecifierWithName:(NSString *)name target:(id)target action:(SEL)action;
@property (assign, nonatomic) BOOL showContentString;
@property (assign, nonatomic) SEL controllerLoadAction;
@property (assign, nonatomic) SEL buttonAction;
@property (assign, nonatomic) SEL confirmationCancelAction;
@property (assign, nonatomic) SEL confirmationAction;
@property (assign, nonatomic) Class editPaneClass;
@property (assign, nonatomic) int cellType;
@property (assign, nonatomic) Class detailControllerClass;
@property (assign, nonatomic) id target;
@property (retain, nonatomic) NSString *identifier;
@property (retain, nonatomic) NSDictionary *shortTitleDictionary;
@property (retain, nonatomic) NSDictionary *titleDictionary;
@property (retain, nonatomic) id userInfo;
@property (retain, nonatomic) NSString *name;
@property (retain, nonatomic) NSArray *values;
- (NSComparisonResult)titleCompare:(PSSpecifier *)otherSpecifier;
- (void)setKeyboardType:(int)type autoCaps:(int)caps autoCorrection:(int)correction;
- (void)setupIconImageWithPath:(NSString *)path;
- (void)setupIconImageWithBundle:(NSBundle *)bundle;
- (void)setValues:(NSArray *)values titles:(NSArray *)titles shortTitles:(NSArray *)shortTitles usingLocalizedTitleSorting:(BOOL)usingLocalizedTitleSorting;
- (void)setValues:(NSArray *)values titles:(NSArray *)titles shortTitles:(NSArray *)shortTitles;
- (void)setValues:(NSArray *)values titles:(NSArray *)titles;
- (void)loadValuesAndTitlesFromDataSource;
- (NSDictionary *)properties;
- (void)setProperties:(NSDictionary *)properties;
- (void)removePropertyForKey:(NSString *)key;
- (void)setProperty:(id)property forKey:(NSString *)key;
- (id)propertyForKey:(NSString *)key;
@end
@interface PSViewController : UIViewController
@property (nonatomic, retain) PSSpecifier *specifier;
@end
typedef enum {
FSSettingsControllerReorderingMode = 0,
FSSettingsControllerEnablingMode = 1,
} FSSettingsControllerMode;
@interface FSSettingsController : PSViewController <UITableViewDataSource, UITableViewDelegate> {
@private
NSString *settingsFile;
CFStringRef preferencesApplicationID;
NSString *enabledKey;
NSMutableArray *enabledIdentifiers;
NSString *disabledKey;
NSMutableArray *disabledIdentifiers;
NSString *notificationName;
NSBundle *templateBundle;
CGFloat rowHeight;
FSSettingsControllerMode mode;
NSArray *allSwitches;
}
@end
@implementation FSSettingsController
- (void)dealloc
{
[allSwitches release];
[enabledIdentifiers release];
[disabledIdentifiers release];
[enabledKey release];
[disabledKey release];
[notificationName release];
[settingsFile release];
[templateBundle release];
if (preferencesApplicationID) {
CFRelease(preferencesApplicationID);
}
[super dealloc];
}
- (void)loadView
{
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, 0.0f) style:UITableViewStyleGrouped];
tableView.dataSource = self;
tableView.delegate = self;
tableView.rowHeight = rowHeight;
tableView.allowsSelectionDuringEditing = YES;
[tableView setEditing:mode == FSSettingsControllerReorderingMode animated:NO];
self.view = tableView;
[tableView release];
}
- (void)setSpecifier:(PSSpecifier *)specifier
{
[super setSpecifier:specifier];
self.navigationItem.title = [specifier name];
[settingsFile release];
settingsFile = [[specifier propertyForKey:@"flipswitchSettingsFile"] copy];
if (preferencesApplicationID) {
CFRelease(preferencesApplicationID);
preferencesApplicationID = NULL;
}
if ((kCFCoreFoundationVersionNumber >= 1000) && [settingsFile hasPrefix:@"/var/mobile/Library/Preferences/"]) {
preferencesApplicationID = (CFStringRef)[[[settingsFile lastPathComponent] stringByDeletingPathExtension] retain];
}
[notificationName release];
notificationName = [[specifier propertyForKey:@"flipswitchPostNotification"] copy];
[enabledKey release];
enabledKey = [[specifier propertyForKey:@"flipswitchEnabledKey"] copy];
[disabledKey release];
disabledKey = [[specifier propertyForKey:@"flipswitchDisabledKey"] copy];
NSString *stringMode = [specifier propertyForKey:@"flipswitchSettingsMode"];
if ([stringMode isEqual:@"enabling"]) {
mode = FSSettingsControllerEnablingMode;
} else if ([stringMode isEqual:@"reordering"]) {
mode = FSSettingsControllerReorderingMode;
} else {
mode = FSSettingsControllerReorderingMode;
}
// Reading Settings file
NSDictionary *settings = nil;
if (settingsFile) {
if (preferencesApplicationID) {
CFPreferencesAppSynchronize(preferencesApplicationID);
CFArrayRef keyList = CFPreferencesCopyKeyList(preferencesApplicationID, kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
if (keyList) {
settings = [(NSDictionary *)CFPreferencesCopyMultiple(keyList, preferencesApplicationID, kCFPreferencesCurrentUser, kCFPreferencesCurrentHost) autorelease];
CFRelease(keyList);
}
} else {
settings = [NSDictionary dictionaryWithContentsOfFile:settingsFile];
}
}
NSArray *originalEnabled = [settings objectForKey:enabledKey] ?: [specifier propertyForKey:@"flipswitchDefaultEnabled"] ?: [NSArray array];
[enabledIdentifiers release];
enabledIdentifiers = [originalEnabled mutableCopy];
NSArray *originalDisabled = [settings objectForKey:disabledKey] ?: [specifier propertyForKey:@"flipswitchDefaultDisabled"] ?: [NSArray array];
[disabledIdentifiers release];
disabledIdentifiers = [originalDisabled mutableCopy];
[allSwitches release];
allSwitches = [[FSSwitchPanel sharedPanel].sortedSwitchIdentifiers retain];
NSMutableArray *allIdentifiers = [[allSwitches mutableCopy] autorelease];
for (NSString *identifier in originalEnabled) {
if ([allIdentifiers containsObject:identifier]) {
[allIdentifiers removeObject:identifier];
[disabledIdentifiers removeObject:identifier];
} else {
[enabledIdentifiers removeObject:identifier];
}
}
for (NSString *identifier in originalDisabled) {
if ([allIdentifiers containsObject:identifier]) {
[allIdentifiers removeObject:identifier];
} else {
[disabledIdentifiers removeObject:identifier];
}
}
NSMutableArray *arrayToAddNewIdentifiers = [[specifier propertyForKey:@"flipswitchNewAreDisabled"] boolValue] ? disabledIdentifiers : enabledIdentifiers;
for (NSString *identifier in allIdentifiers) {
[arrayToAddNewIdentifiers addObject:identifier];
}
// Theming
NSString *bundlePath = [specifier propertyForKey:@"flipswitchTemplateBundle"];
if (bundlePath) {
[templateBundle release];
templateBundle = [[NSBundle alloc] initWithPath:bundlePath];
}
rowHeight = [[templateBundle.flipswitchThemedInfoDictionary objectForKey:@"height"] floatValue] + 2.0f;
if (rowHeight < 44.0f) {
rowHeight = 44.0f;
}
if ([self isViewLoaded]) {
[(UITableView *)self.view setRowHeight:rowHeight];
[(UITableView *)self.view setEditing:mode == FSSettingsControllerReorderingMode animated:NO];
[(UITableView *)self.view reloadData];
}
}
- (void)_flushSettings
{
if (preferencesApplicationID && (enabledKey || disabledKey)) {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
if (enabledKey) {
[dict setObject:enabledIdentifiers forKey:enabledKey];
}
if (disabledKey) {
[dict setObject:disabledIdentifiers forKey:disabledKey];
}
CFPreferencesSetMultiple((CFDictionaryRef)dict, NULL, preferencesApplicationID, kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
CFPreferencesAppSynchronize(preferencesApplicationID);
}
if (settingsFile) {
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:settingsFile] ?: [NSMutableDictionary dictionary];
if (enabledKey) {
[dict setObject:enabledIdentifiers forKey:enabledKey];
}
if (disabledKey) {
[dict setObject:disabledIdentifiers forKey:disabledKey];
}
NSData *data = [NSPropertyListSerialization dataFromPropertyList:dict format:NSPropertyListBinaryFormat_v1_0 errorDescription:NULL];
[data writeToFile:settingsFile atomically:YES];
}
if (notificationName) {
notify_post([notificationName UTF8String]);
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)table
{
return (mode == FSSettingsControllerReorderingMode) ? 2 : 1;
}
- (NSString *)tableView:(UITableView *)table titleForHeaderInSection:(NSInteger)section
{
switch (mode) {
case FSSettingsControllerEnablingMode:
return nil;
case FSSettingsControllerReorderingMode:
default:
return section ? @"Disabled" : @"Enabled";
}
}
- (NSString *)tableView:(UITableView *)table titleForFooterInSection:(NSInteger)section
{
switch (mode) {
case FSSettingsControllerEnablingMode:
return nil;
case FSSettingsControllerReorderingMode:
default:
return @" ";
}
}
- (NSArray *)arrayForSection:(NSInteger)section
{
switch (mode) {
case FSSettingsControllerEnablingMode:
return allSwitches;
case FSSettingsControllerReorderingMode:
default:
return section ? disabledIdentifiers : enabledIdentifiers;
}
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return [[self arrayForSection:section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"] ?: [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"] autorelease];
FSSwitchPanel *panel = [FSSwitchPanel sharedPanel];
NSString *switchIdentifier = [[self arrayForSection:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text = [panel titleForSwitchIdentifier:switchIdentifier];
cell.imageView.image = [panel imageOfSwitchState:FSSwitchStateIndeterminate controlState:UIControlStateNormal forSwitchIdentifier:switchIdentifier usingTemplate:templateBundle];
if (mode == FSSettingsControllerEnablingMode) {
cell.accessoryType = [enabledIdentifiers containsObject:switchIdentifier] ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;
} else {
Class _class = [panel settingsViewControllerClassForSwitchIdentifier:switchIdentifier];
cell.editingAccessoryType = _class ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
cell.selectionStyle = _class ? UITableViewCellSelectionStyleBlue : UITableViewCellSelectionStyleNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *switchIdentifier = [[self arrayForSection:indexPath.section] objectAtIndex:indexPath.row];
if (mode == FSSettingsControllerEnablingMode) {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
UITableViewCellAccessoryType accessoryType;
if ([enabledIdentifiers containsObject:switchIdentifier]) {
[disabledIdentifiers addObject:switchIdentifier];
[enabledIdentifiers removeObject:switchIdentifier];
accessoryType = UITableViewCellAccessoryNone;
} else {
[enabledIdentifiers addObject:switchIdentifier];
[disabledIdentifiers removeObject:switchIdentifier];
accessoryType = UITableViewCellAccessoryCheckmark;
}
cell.accessoryType = accessoryType;
[self _flushSettings];
} else {
UIViewController <FSSwitchSettingsViewController> *_controller = [[FSSwitchPanel sharedPanel] settingsViewControllerForSwitchIdentifier:switchIdentifier];
if (_controller) {
[self.navigationController pushViewController:_controller animated:YES];
}
}
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
NSMutableArray *fromArray = fromIndexPath.section ? disabledIdentifiers : enabledIdentifiers;
NSMutableArray *toArray = toIndexPath.section ? disabledIdentifiers : enabledIdentifiers;
NSString *identifier = [[fromArray objectAtIndex:fromIndexPath.row] retain];
[fromArray removeObjectAtIndex:fromIndexPath.row];
[toArray insertObject:identifier atIndex:toIndexPath.row];
[identifier release];
[self _flushSettings];
}
- (id)table
{
return nil;
}
@end