-
Notifications
You must be signed in to change notification settings - Fork 6
/
CBRPrefsManager.m
141 lines (116 loc) · 5.42 KB
/
CBRPrefsManager.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
#import "CBRPrefsManager.h"
#import "Defines.h"
#import "NSDistributedNotificationCenter.h"
#define PREFS_NAME "com.golddavid.colorbanners"
#define BANNERS_KEY @"BannersEnabled"
#define BANNERS_GRADIENT_KEY @"BannerGradient"
#define BANNER_ALPHA_KEY @"BannerAlpha"
#define BANNER_BG_KEY @"BannerBackgroundColor"
#define BANNER_CONSTANT_KEY @"BannerUseConstant"
#define BANNER_DEEP_ANALYSIS_KEY @"WantsDeepBannerAnalyzing"
#define BANNER_LIVE_ANALYSIS_KEY @"WantsLiveAnalysis"
#define BANNERS_BLUR_KEY @"RemoveBannersBlur"
#define RECT_KEY @"HideQRRect"
#define GRABBER_KEY @"HideGrabber"
#define LS_KEY @"LSEnabled"
#define LS_GRADIENT_KEY @"LSGradient"
#define LS_ALPHA_KEY @"LSAlpha"
#define LS_BG_KEY @"LSBackgroundColor"
#define LS_CONSTANT_KEY @"LSUseConstant"
#define CORNERS_KEY @"RoundCorners"
#define BLUR_KEY @"RemoveBlur"
#define SEPARATORS_KEY @"ShowSeparators"
#define DIMMING_KEY @"DisableDimming"
#define COLOR_BUTTON_KEY @"ColorDismissButton"
#define WHITE_TEXT_KEY @"PrefersWhiteText"
#define NC_KEY @"NCEnabled"
#define NC_GRADIENT_KEY @"NCGradient"
#define NC_ALPHA_KEY @"NCAlpha"
#define NC_BG_KEY @"NCBackgroundColor"
#define NC_CONSTANT_KEY @"NCUseConstant"
// From ColorBadges.h.
#define GETRED(rgb) ((rgb >> 16) & 0xFF)
#define GETGREEN(rgb) ((rgb >> 8) & 0xFF)
#define GETBLUE(rgb) (rgb & 0xFF)
// Default to white.
#define DEFAULT_COLOR 0xFFFFFF
// Expected format: #<hex int>.
static int RGBColorFromNSString(NSString *str) {
unsigned hexColor = DEFAULT_COLOR;
NSScanner *scanner = [NSScanner scannerWithString:str];
[scanner setScanLocation:1]; // Skip over the '#'.
[scanner scanHexInt:&hexColor];
return (int)hexColor;
}
@implementation CBRPrefsManager
+ (instancetype)sharedInstance {
static dispatch_once_t onceToken;
static CBRPrefsManager *cache;
dispatch_once(&onceToken, ^{ cache = [[CBRPrefsManager alloc] init]; } );
return cache;
}
- (instancetype)init {
self = [super init];
if (self) {
[self reload];
[[NSDistributedNotificationCenter defaultCenter] addObserver:self
selector:@selector(reload)
name:INTERNAL_NOTIFICATION_NAME
object:nil];
}
return self;
}
- (NSDictionary *)prefsDictionary {
CFStringRef appID = CFSTR(PREFS_NAME);
CFArrayRef keyList = CFPreferencesCopyKeyList(appID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
if (!keyList) {
CBRLOG(@"Unable to obtain preferences keyList!");
return nil;
}
NSDictionary *dictionary = (NSDictionary *)CFPreferencesCopyMultiple(keyList, appID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
CFRelease(keyList);
return [dictionary autorelease];
}
- (void)reload {
NSDictionary *prefs = [self prefsDictionary];
_bannersEnabled = [self boolForValue:prefs[BANNERS_KEY] withDefault:YES];
_useBannerGradient = [self boolForValue:prefs[BANNERS_GRADIENT_KEY] withDefault:YES];
_bannerAlpha = [self floatForValue:prefs[BANNER_ALPHA_KEY] withDefault:0.7];
_bannerBackgroundColor = [self rgbColorForNSString:prefs[BANNER_BG_KEY] withDefault:DEFAULT_COLOR];
_bannersUseConstantColor = [self boolForValue:prefs[BANNER_CONSTANT_KEY] withDefault:NO];
_wantsDeepBannerAnalyzing = [self boolForValue:prefs[BANNER_DEEP_ANALYSIS_KEY] withDefault:YES];
_wantsLiveAnalysis = [self boolForValue:prefs[BANNER_LIVE_ANALYSIS_KEY] withDefault:YES];
_removeBannersBlur = [self boolForValue:prefs[BANNERS_BLUR_KEY] withDefault:NO];
_hideQRRect = [self boolForValue:prefs[RECT_KEY] withDefault:NO];
_hideGrabber = [self boolForValue:prefs[GRABBER_KEY] withDefault:NO];
_lsEnabled = [self boolForValue:prefs[LS_KEY] withDefault:YES];
_useLSGradient = [self boolForValue:prefs[LS_GRADIENT_KEY] withDefault:YES];
_lsAlpha = [self floatForValue:prefs[LS_ALPHA_KEY] withDefault:0.7];
_lsBackgroundColor = [self rgbColorForNSString:prefs[LS_BG_KEY] withDefault:DEFAULT_COLOR];
_lsUseConstantColor = [self boolForValue:prefs[LS_CONSTANT_KEY] withDefault:NO];
_roundCorners = [self boolForValue:prefs[CORNERS_KEY] withDefault:NO];
_removeLSBlur = [self boolForValue:prefs[BLUR_KEY] withDefault:NO];
_showSeparators = [self boolForValue:prefs[SEPARATORS_KEY] withDefault:NO];
_disableDimming = [self boolForValue:prefs[DIMMING_KEY] withDefault:YES];
_colorDismissButton = [self boolForValue:prefs[COLOR_BUTTON_KEY] withDefault:YES];
_prefersWhiteText = [self boolForValue:prefs[WHITE_TEXT_KEY] withDefault:NO];
_ncEnabled = [self boolForValue:prefs[NC_KEY] withDefault:YES];
_useNCGradient = [self boolForValue:prefs[NC_GRADIENT_KEY] withDefault:YES];
_ncAlpha = [self floatForValue:prefs[NC_ALPHA_KEY] withDefault:0.7];
_ncBackgroundColor = [self rgbColorForNSString:prefs[NC_BG_KEY] withDefault:DEFAULT_COLOR];
_ncUseConstantColor = [self boolForValue:prefs[NC_CONSTANT_KEY] withDefault:NO];
}
- (BOOL)boolForValue:(NSNumber *)value withDefault:(BOOL)defaultValue {
return (value) ? [value boolValue] : defaultValue;
}
- (CGFloat)floatForValue:(NSNumber *)value withDefault:(CGFloat)defaultValue {
return (value) ? (CGFloat)[value floatValue] : defaultValue;
}
- (int)rgbColorForNSString:(NSString *)string withDefault:(int)defaultValue {
return (string) ? RGBColorFromNSString(string) : defaultValue;
}
- (void)dealloc {
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:INTERNAL_NOTIFICATION_NAME object:nil];
[super dealloc];
}
@end