-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPreferences.mm
305 lines (253 loc) · 7.4 KB
/
Preferences.mm
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
#import "Tweak.h"
#import "Preferences.h"
#pragma mark #region [ ONApplicationIcon ]
@implementation ONApplicationIcon
+(id)createInstance { return [[ONApplicationIcon alloc] init]; }
+(id)createInstanceWithDictionary:(NSMutableDictionary*)aDictionary { return [[ONApplicationIcon alloc] initWithDictionary:aDictionary]; }
-(id)init
{
if (!(self = [super init])) return nil;
_dictionary = [[NSMutableDictionary alloc] init];
return self;
}
-(id)initWithDictionary:(NSMutableDictionary*)aDictionary
{
if (![self init]) return nil;
_dictionary = [aDictionary retain];
return self;
}
-(void)dealloc
{
[_dictionary release];
[super dealloc];
}
-(ONIconAlignment)alignment
{
return [_dictionary.allKeys containsObject:ONIconAlignmentKey]
? (ONIconAlignment)[[_dictionary objectForKey:ONIconAlignmentKey] intValue]
: ONIconAlignmentDefault;
}
-(void)setAlignment:(ONIconAlignment)value
{
[_dictionary setObject:[NSNumber numberWithUnsignedInt:value] forKey:ONIconAlignmentKey];
}
-(NSMutableDictionary*)toDictionary { return _dictionary; }
@end
#pragma mark #endregion
#pragma mark #region [ ONApplication ]
@implementation ONApplication
@synthesize icons;
+(id)createInstance
{
return [[ONApplication alloc] init];
}
-(id)init
{
if (!(self = [super init])) return nil;
self.icons = [NSMutableDictionary dictionary];
return self;
}
-(void)dealloc
{
[self.icons release];
[super dealloc];
}
-(NSMutableDictionary*)toDictionary
{
NSMutableDictionary* value = [NSMutableDictionary dictionary];
if (self.icons.allKeys.count > 0)
{
NSMutableDictionary* iconDict = [NSMutableDictionary dictionary];
for (NSString* name in self.icons.allKeys)
{
ONApplicationIcon* icon = [self.icons objectForKey:name];
if (icon) [iconDict setObject:[icon toDictionary] forKey:name];
}
[value setObject:iconDict forKey:ONIconsKey];
}
return value;
}
-(id)addIcon:(NSString*)iconName
{
[self.icons setObject:[ONApplicationIcon createInstance] forKey:iconName];
return [self.icons objectForKey:iconName];
}
-(void)removeIcon:(NSString*)iconName
{
[self.icons removeObjectForKey:iconName];
}
-(bool)containsIcon:(NSString*)iconName
{
return [self.icons.allKeys containsObject:iconName];
}
@end
#pragma mark #endregion
#pragma mark #region [ ONPreferences ]
static ONPreferences* _instance;
@implementation ONPreferences
+(id)sharedInstance
{
return (_instance = [[[ONPreferences alloc] init] retain]);
}
-(id)init
{
if (_instance) { [self release]; return _instance; }
if (!(self = [super init])) return nil;
[self reload];
return (_instance = self);
}
-(void)dealloc
{
[_data release];
[super dealloc];
}
-(int)schemaVersion
{
return [_data.allKeys containsObject:ONSchemaVersionKey] ? [[_data objectForKey:ONSchemaVersionKey] intValue] : 0;
}
-(void)loadAppsVersion00
{
if (![_data.allKeys containsObject:ONApplicationsKey]) return;
NSDictionary* appData = [_data objectForKey:ONApplicationsKey];
for (NSString* identifer in appData.allKeys)
{
ONApplication* app = [ONApplication createInstance];
NSArray* icons = [appData objectForKey:identifer];
for (NSString* iconName in icons)
{
[app.icons setObject:[ONApplicationIcon createInstance] forKey:iconName];
}
[_applications setObject:app forKey:identifer];
}
}
-(void)loadAppsVersion01
{
if (![_data.allKeys containsObject:ONApplicationsKey]) return;
NSDictionary* appData = [_data objectForKey:ONApplicationsKey];
for (NSString* identifer in appData.allKeys)
{
ONApplication* app = [ONApplication createInstance];
NSMutableDictionary* icons = [[appData objectForKey:identifer] objectForKey:ONIconsKey];
for (NSString* iconName in icons.allKeys)
{
[app.icons setObject:[ONApplicationIcon createInstanceWithDictionary:[icons objectForKey:iconName]] forKey:iconName];
}
[_applications setObject:app forKey:identifer];
}
}
-(NSMutableDictionary*)applications
{
if (_applications) return _applications;
_applications = [[NSMutableDictionary alloc] init];
switch (self.schemaVersion)
{
case 0: [self loadAppsVersion00]; break;
default: [self loadAppsVersion01]; break;
}
return _applications;
}
-(ONApplication*)getApplication:(NSString*)identifer
{
return [self.applications objectForKey:identifer];
}
-(void)setApplication:(ONApplication*)application named:(NSString*)identifer
{
[self.applications setObject:application forKey:identifer];
}
-(void)removeApplication:(NSString*)identifer
{
[self.applications removeObjectForKey:identifer];
}
-(id)addIcon:(NSString*)iconName forApplication:(NSString*)identifer
{
ONApplication* app = [self getApplication:identifer];
if (!app && !(app = [ONApplication createInstance]))
{
Log("Failed to get or create ONApplication");
return nil;
}
[app addIcon:iconName];
[self setApplication:app named:identifer];
return [app.icons objectForKey:iconName];
}
-(void)removeIcon:(NSString*)iconName fromApplication:(NSString*)identifer;
{
ONApplication* app = [self getApplication:identifer];
if (!app) return;
[app removeIcon:iconName];
if (app.icons.allKeys.count == 0) [self removeApplication:identifer];
}
-(ONApplicationIcon*)getIcon:(NSString*)iconName forApplication:(NSString*)identifer
{
ONApplication* app = [self getApplication:identifer];
if (!app) return nil;
return [app.icons objectForKey:iconName];
}
-(bool)iconsOnLeft
{
return [_data.allKeys containsObject:ONIconsLeftKey] ? [[_data objectForKey:ONIconsLeftKey] boolValue] : false;
}
-(void)setIconsOnLeft:(bool)value
{
[_data setObject:NSBool(value) forKey:ONIconsLeftKey];
[self save];
}
-(bool)enabled
{
return [_data.allKeys containsObject:ONEnabledKey] ? [[_data objectForKey:ONEnabledKey] boolValue] : true;
}
-(void)setEnabled:(bool)value
{
[_data setObject:NSBool(value) forKey:ONEnabledKey];
[self save];
}
-(void)reload
{
if (_applications) { [_applications release]; _applications = nil; }
if (_data) [_data release];
_data = [[NSMutableDictionary alloc] initWithContentsOfFile:ONPreferencesFile];
if (!_data) _data = [[NSMutableDictionary alloc] init]; // new setup
}
-(void)saveWithNotification:(NSString*)notification
{
[_data removeObjectForKey:@"pseudobadges"];
// Convert the objects back to a writeable dictionary
// to avoid having to use NSKeyArchiver
NSMutableDictionary* apps = [NSMutableDictionary dictionary];
for (NSString* identifer in self.applications.allKeys)
{
[apps setObject:[[self getApplication:identifer] toDictionary] forKey:identifer];
}
[_data setObject:apps forKey:ONApplicationsKey];
[_data setObject:[NSNumber numberWithUnsignedInt:ONSchemaVersion] forKey:ONSchemaVersionKey];
if (![_data writeToFile:ONPreferencesFile atomically:true])
{
Log("Failed to save settings");
return;
}
PostNotification((CFStringRef)notification);
}
-(void)save
{
[self saveWithNotification:IconSettingsChangedNotification];
}
-(bool)silentModeEnabled
{
return [_data.allKeys containsObject:ONSilentModeEnabledKey] ? [[_data objectForKey:ONSilentModeEnabledKey] boolValue] : true;
}
-(void)setSilentModeEnabled:(bool)value
{
[_data setObject:NSBool(value) forKey:ONSilentModeEnabledKey];
[self saveWithNotification:SilentModeChangedNotification];
}
-(bool)silentIconOnLeft
{
return [_data.allKeys containsObject:ONSilentIconLeftKey] ? [[_data objectForKey:ONSilentIconLeftKey] boolValue]: false;
}
-(void)setSilentIconOnLeft:(bool)value
{
[_data setObject:NSBool(value) forKey:ONSilentIconLeftKey];
[self saveWithNotification:SilentModeChangedNotification];
}
@end
#pragma mark #endregion