forked from mgorbach/macfusion2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MFPreferences.m
160 lines (131 loc) · 4.46 KB
/
MFPreferences.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
//
// MFPreferences.m
// MacFusion2
//
// Created by Michael Gorbach on 3/15/08.
// Copyright 2008 Michael Gorbach. All rights reserved.
//
#import "MFPreferences.h"
#import "MFLogging.h"
#import <Carbon/Carbon.h>
#define PREFS_FILE_PATH @"~/Library/Application Support/Macfusion/Preferences/preferences.plist"
@interface MFPreferences(PrivateAPI)
- (void)writePrefs;
@end
@implementation MFPreferences
static MFPreferences *sharedPreferences = nil;
+ (MFPreferences *) sharedPreferences
{
if (sharedPreferences == nil) {
[[self alloc] init];
}
return sharedPreferences;
}
+ (id)allocWithZone:(NSZone *)zone {
if (sharedPreferences == nil) {
sharedPreferences = [super allocWithZone:zone];
return sharedPreferences;
}
return nil;
}
void prefsFSEventCallBack(ConstFSEventStreamRef streamRef,
void *clientCallBackInfo,
size_t numEvents,
void *eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[]) {
MFLogS([MFPreferences sharedPreferences],@"Prefs update FSEvents callback received");
[[MFPreferences sharedPreferences] readPrefsFromDisk];
}
- (id)init {
if (self = [super init]) {
NSString* fullPrefsFilePath = [PREFS_FILE_PATH stringByExpandingTildeInPath];
NSDictionary* readFromDisk = [NSDictionary dictionaryWithContentsOfFile: fullPrefsFilePath];
if (!readFromDisk) {
_firstTimeRun = YES;
}
_prefsDict = readFromDisk ? [readFromDisk mutableCopy] : [NSMutableDictionary dictionary];
FSEventStreamRef eventStream = FSEventStreamCreate(NULL,
prefsFSEventCallBack,
NULL,
(CFArrayRef)[NSArray arrayWithObject: [fullPrefsFilePath stringByDeletingLastPathComponent]],
kFSEventStreamEventIdSinceNow,
0,
kFSEventStreamCreateFlagUseCFTypes);
FSEventStreamScheduleWithRunLoop(eventStream, [[NSRunLoop currentRunLoop] getCFRunLoop],kCFRunLoopDefaultMode);
FSEventStreamStart(eventStream);
}
return self;
}
- (BOOL)firstTimeRun {
return _firstTimeRun;
}
- (void)readPrefsFromDisk {
NSString *fullPrefsFilePath = [PREFS_FILE_PATH stringByExpandingTildeInPath];
NSDictionary *readFromDisk = [NSDictionary dictionaryWithContentsOfFile: fullPrefsFilePath];
for(NSString *key in [readFromDisk allKeys]) {
id diskValue = [readFromDisk objectForKey: key];
if (![diskValue isEqualTo: [self getValueForPreference: key]]) {
[self setValue:diskValue forPreference:key];
}
}
}
- (void)setValue:(id)value forPreference:(NSString*)prefKey {
// MFLogS(self, @"Setting value %@ for key %@", value, prefKey);
if (value != [self getValueForPreference:prefKey]) {
[self willChangeValueForKey:prefKey];
[_prefsDict setObject:value forKey:prefKey];
[self didChangeValueForKey:prefKey];
[self writePrefs];
}
}
- (id)defaultValueForPrefKey:(NSString *)key {
if ([key isEqualToString:kMFPrefsAutoScrollLog]) {
return [NSNumber numberWithBool: YES];
}
if ([key isEqualToString:kMFPrefsTimeout]) {
return [NSNumber numberWithFloat: 5.0];
}
if ([key isEqualToString: kMFPrefsAutosize]) {
return [NSNumber numberWithBool: YES];
}
return nil;
}
- (id)getValueForPreference:(NSString *)prefKey {
id value = [_prefsDict objectForKey: prefKey];
if (value) {
return value;
} else {
return [self defaultValueForPrefKey: prefKey];
}
}
- (void)writePrefs {
NSString *fullPrefsFilePath = [PREFS_FILE_PATH stringByExpandingTildeInPath];
NSString *dirPath = [fullPrefsFilePath stringByDeletingLastPathComponent];
BOOL isDir;
if (!([[NSFileManager defaultManager] fileExistsAtPath:dirPath isDirectory:&isDir] && isDir)) {
NSError *dirCreateError;
BOOL dirCreateOK = [[NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&dirCreateError];
if (!dirCreateOK) {
MFLogS(self, @"Failed to create dir for prefs. Error %@", dirCreateError);
return;
}
}
BOOL ok = [_prefsDict writeToFile:fullPrefsFilePath atomically:YES];
if (!ok) {
MFLogS(self, @"Failed to write prefs");
}
}
- (BOOL)getBoolForPreference:(NSString*)prefKey {
return [[self getValueForPreference: prefKey] boolValue];
}
- (void)setBool:(BOOL)value forPreference:(NSString*)prefKey {
[self setValue:[NSNumber numberWithBool:value] forPreference:prefKey];
}
- (id)valueForUndefinedKey:(NSString *)key {
return [self getValueForPreference: key];
}
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
[self setValue:value forPreference:key];
}
@end