forked from codykrieger/gfxCardStatus
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathGSPreferences.m
193 lines (150 loc) · 5.52 KB
/
GSPreferences.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
//
// GSPreferences.m
// gfxCardStatus
//
// Created by Cody Krieger on 9/26/10.
// Copyright 2010 Cody Krieger. All rights reserved.
//
#import "GSPreferences.h"
#import "GSStartup.h"
#import "GSGPU.h"
// Unfortunately this value needs to stay misspelled unless there is a desire to
// migrate it to a correctly spelled version instead, since getting and setting
// the existing preferences depend on it.
#define kPowerSourceBasedSwitchingACMode @"GPUSetting_ACAdaptor"
#define kPowerSourceBasedSwitchingBatteryMode @"GPUSetting_Battery"
#define kShouldStartAtLoginKey @"shouldStartAtLogin"
#define kShouldUseImageIconsKey @"shouldUseImageIcons"
#define kShouldCheckForUpdatesOnStartupKey @"shouldCheckForUpdatesOnStartup"
#define kShouldUsePowerSourceBasedSwitchingKey @"shouldUsePowerSourceBasedSwitching"
#define kShouldUseSmartMenuBarIconsKey @"shouldUseSmartMenuBarIcons"
// This used to be called "shouldGrowl"
#define kShouldDisplayNotificationsKey @"shouldGrowl"
// Why aren't we just using NSUserDefaults? Because it was unbelievably
// unreliable. This works all the time, no questions asked.
#define kPreferencesPlistPath [@"~/Library/Preferences/com.codykrieger.gfxCardStatus-Preferences.plist" stringByExpandingTildeInPath]
@interface GSPreferences (Internal)
- (NSString *)_getPrefsPath;
@end
@implementation GSPreferences
@synthesize prefsDict = _prefsDict;
#pragma mark - Initializers
- (id)init
{
if (!(self = [super init]))
return nil;
GTMLoggerDebug(@"Initializing GSPreferences...");
[self setUpPreferences];
return self;
}
+ (GSPreferences *)sharedInstance
{
static dispatch_once_t pred = 0;
__strong static GSPreferences *_sharedObject = nil;
dispatch_once(&pred, ^{
_sharedObject = [[self alloc] init];
});
return _sharedObject;
}
#pragma mark - GSPreferences API
- (void)setUpPreferences
{
GTMLoggerDebug(@"Loading preferences and defaults...");
// Load the preferences dictionary from disk.
_prefsDict = [[NSMutableDictionary alloc] initWithContentsOfFile:[self _getPrefsPath]];
if (!_prefsDict) {
// If preferences file doesn't exist, set the defaults.
_prefsDict = [[NSMutableDictionary alloc] init];
[self setDefaults];
} else
_prefsDict[kShouldStartAtLoginKey] = @([GSStartup existsInStartupItems]);
// Ensure that application will be loaded at startup.
if ([self shouldStartAtLogin])
[GSStartup loadAtStartup:YES];
// If an "integrated" image is available in our bundle, assume the user has
// custom icons that we should use.
_prefsDict[kShouldUseImageIconsKey] = @(!![[NSBundle mainBundle] pathForResource:@"integrated" ofType:@"png"]);
// Since we removed this preference from v2.2 prematurely, some new users
// might not have had it set in their defaults. If the key doesn't exist in
// the prefs dictionary, default this sucker to enabled, because
// notifications are helpful.
if (_prefsDict[kShouldDisplayNotificationsKey] == nil)
_prefsDict[kShouldDisplayNotificationsKey] = @YES;
}
- (void)setDefaults
{
GTMLoggerDebug(@"Setting initial defaults...");
_prefsDict[kShouldCheckForUpdatesOnStartupKey] = @YES;
_prefsDict[kShouldStartAtLoginKey] = @YES;
_prefsDict[kShouldDisplayNotificationsKey] = @YES;
_prefsDict[kShouldUsePowerSourceBasedSwitchingKey] = @NO;
_prefsDict[kShouldUseSmartMenuBarIconsKey] = @NO;
_prefsDict[kPowerSourceBasedSwitchingBatteryMode] = @(GSPowerSourceBasedSwitchingModeIntegrated);
if ([GSGPU isLegacyMachine])
_prefsDict[kPowerSourceBasedSwitchingACMode] = @(GSPowerSourceBasedSwitchingModeDiscrete);
else
_prefsDict[kPowerSourceBasedSwitchingACMode] = @(GSPowerSourceBasedSwitchingModeDynamic);
[self savePreferences];
}
- (void)savePreferences
{
GTMLoggerDebug(@"Writing preferences to disk...");
if ([_prefsDict writeToFile:[self _getPrefsPath] atomically:YES])
GTMLoggerDebug(@"Successfully wrote preferences to disk.");
else
GTMLoggerDebug(@"Failed to write preferences to disk. Permissions problem in ~/Library/Preferences?");
}
- (void)setBool:(BOOL)value forKey:(NSString *)key
{
_prefsDict[key] = @(value);
[self savePreferences];
}
- (BOOL)boolForKey:(NSString *)key
{
return [_prefsDict[key] boolValue];
}
- (BOOL)shouldCheckForUpdatesOnStartup
{
return [_prefsDict[kShouldCheckForUpdatesOnStartupKey] boolValue];
}
- (BOOL)shouldStartAtLogin
{
return [_prefsDict[kShouldStartAtLoginKey] boolValue];
}
- (BOOL)shouldDisplayNotifications
{
return [_prefsDict[kShouldDisplayNotificationsKey] boolValue];
}
- (BOOL)shouldUsePowerSourceBasedSwitching
{
return [_prefsDict [kShouldUsePowerSourceBasedSwitchingKey] boolValue];
}
- (BOOL)shouldUseImageIcons
{
return [_prefsDict[kShouldUseImageIconsKey] boolValue];
}
- (BOOL)shouldUseSmartMenuBarIcons
{
return [_prefsDict[kShouldUseSmartMenuBarIconsKey] boolValue];
}
- (GSPowerSourceBasedSwitchingMode)modeForACAdapter
{
return [_prefsDict[kPowerSourceBasedSwitchingACMode] intValue];
}
- (GSPowerSourceBasedSwitchingMode)modeForBattery
{
return [_prefsDict[kPowerSourceBasedSwitchingBatteryMode] intValue];
}
#pragma mark - NSWindowDelegate protocol
- (void)windowWillClose:(NSNotification *)notification
{
[self savePreferences];
}
@end
#pragma mark - Private helpers
@implementation GSPreferences (Internal)
- (NSString *)_getPrefsPath
{
return kPreferencesPlistPath;
}
@end