forked from DpkgDan/Power-Tap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PTPreferences.m
executable file
·107 lines (90 loc) · 3.85 KB
/
PTPreferences.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
#import "PTPreferences.h"
#define DEFAULTS_PATH @"/Library/PreferenceBundles/PowerTapPrefs.bundle/Defaults.plist"
#define USER_PREFS_PATH @"/User/Library/Preferences/com.subdiox.powertap.plist"
@implementation PTPreferences
static void reconcilePreferences(NSMutableDictionary *defaults, NSMutableDictionary *customPrefs,
NSString *preferenceSpecifier, NSString *entryToReplace) {
NSArray *defaultModes = [defaults allKeys]; //Array of modes listed in defaults
for (int i = 0; i < [defaultModes count]; i++) {
NSString *mode = [defaultModes objectAtIndex: i];
NSString *customPrefsKey = [mode stringByAppendingString: preferenceSpecifier]; //Get key for given mode using
//preference specifier
id preferenceObject = [customPrefs objectForKey: customPrefsKey];
if (preferenceObject && !(([preferenceObject isKindOfClass: [NSString class]])
&& ([preferenceObject isEqualToString: @""]))) {
//Don't replace blank strings! Return to the placeholder (default)
[[defaults objectForKey: mode] setObject: preferenceObject forKey: entryToReplace];
}
}
}
- (PTPreferences*)init {
if ((self = [super init])) {
NSMutableDictionary *defaults = [[NSMutableDictionary alloc] initWithContentsOfFile: DEFAULTS_PATH];
NSMutableDictionary *customPrefs = [[NSMutableDictionary alloc] initWithContentsOfFile: USER_PREFS_PATH];
if (customPrefs) {
reconcilePreferences(defaults, customPrefs, @"-Toggle", @"enabled");
reconcilePreferences(defaults, customPrefs, @"-String", @"string");
}
_preferences = [[NSDictionary alloc] initWithDictionary: defaults];
_modes = [_preferences allKeys];
}
return self;
}
- (NSString*)modeForIndex:(int)index {
for (int i = 0; i < [self.modes count]; i++) {
int loopingIndex = [[self valueForSpecifier: @"index" mode: [self.modes objectAtIndex: i]] intValue];
if (loopingIndex == index) {
return [self.modes objectAtIndex: i];
}
}
return nil;
}
- (id)valueForSpecifier:(NSString*)specifier mode:(NSString*)mode {
return [[_preferences objectForKey: mode] objectForKey: specifier];
}
- (UIImage*)iconForMode:(NSString*)mode {
NSString *pathToIcon = [self valueForSpecifier: @"icon" mode: mode];
UIImage *icon = [UIImage imageWithContentsOfFile: pathToIcon];
return icon;
}
- (UIColor*)tintColorForMode:(NSString*)mode {
NSString *colorString = [self valueForSpecifier: @"color" mode: mode];
if ([colorString isEqualToString: @"red"]) {
return [UIColor redColor];
} else if ([colorString isEqualToString: @"orange"]) {
return [UIColor orangeColor];
} else if ([colorString isEqualToString: @"cyan"]) {
return [UIColor cyanColor];
} else if ([colorString isEqualToString: @"purple"]) {
return [UIColor purpleColor];
} else if ([colorString isEqualToString: @"gray"]) {
return [UIColor grayColor];
} else if ([colorString isEqualToString: @"black"]) {
return [UIColor blackColor];
} else if ([colorString isEqualToString: @"lightGray"]) {
return [UIColor lightGrayColor];
} else if ([colorString isEqualToString: @"blue"]) {
return [UIColor blueColor];
} else if ([colorString isEqualToString: @"magenta"]) {
return [UIColor magentaColor];
} else if ([colorString isEqualToString: @"brown"]) {
return [UIColor brownColor];
} else if ([colorString isEqualToString: @"green"]) {
return [UIColor greenColor];
} else {
return nil;
}
}
- (NSArray*)trackTexts {
NSMutableArray *_trackTexts = [NSMutableArray new];
for (int i = 0; i < [self.modes count]; i++) {
[_trackTexts addObject: [self valueForSpecifier: @"string" mode: [self modeForIndex: i]]];
}
return [[NSArray alloc] initWithArray: _trackTexts];
}
- (void)setPowerDownTrackText:(NSString*)trackText {
NSMutableDictionary *tempDict = [_preferences mutableCopy];
[[tempDict objectForKey: @"PowerDown"] setObject: trackText forKey: @"string"];
_preferences = [[NSDictionary alloc] initWithDictionary: tempDict];
}
@end