-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFFPrefPaneController.m
221 lines (190 loc) · 8.39 KB
/
FFPrefPaneController.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
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
//
// FFPrefPaneController.m
// FunctionFlip
//
// Created by Kevin Gessner on 9/2/08.
// Copyright (c) 2008-2010, Kevin Gessner, http://kevingessner.com
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import "FFDefs.h"
#import "FFPrefPaneController.h"
#import "FFKeyLibrary.h"
#import "FFPreferenceManager.h"
#import "FFKey.h"
#import "FFKeyboard.h"
#import "FFHelperAppController.h"
#import "FFUpdateSheetController.h"
#import "DDHidLib.h"
@interface FFPrefPaneController (Private)
- (void)setStartsAtLogin:(BOOL)flag;
@end
@implementation FFPrefPaneController
- (void) mainViewDidLoad {
// First load of a new version of the pref-pane
NSString *lastVersion = (NSString *)[[FFPreferenceManager sharedInstance] valueForKey:@"version"];
if(!lastVersion || ![lastVersion isEqualToString:[self bundleVersionNumber]]) {
[self restartHelperApp];
[[FFPreferenceManager sharedInstance] setValue:[self bundleVersionNumber] forKey:@"version"];
} else {
[self performSelector:@selector(updateHelperAppStatus) withObject:nil afterDelay:0.1];
}
[[FFPreferenceManager sharedInstance] addObserver:self forKeyPath:KG_LOGIN_KEYPATH options:NSKeyValueObservingOptionNew context:NULL];
[self setStartsAtLogin:[[[FFPreferenceManager sharedInstance] valueForKey:KG_LOGIN_KEYPATH] boolValue]];
}
//- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
//}
- (IBAction)startStopHelperApp:(id)sender {
[FFHelperAppController startStopHelperApp];
[helperAppProgress startAnimation:self];
[startStopHelperApp setEnabled:NO];
// give the app a moment to start, then check if it's running.
[self performSelector:@selector(updateHelperAppStatus) withObject:nil afterDelay:3.0];
}
- (void)restartHelperApp {
[FFHelperAppController restartHelperApp];
[helperAppStatus setStringValue:@"Upgrading FunctionFlip..."];
[helperAppProgress startAnimation:self];
[startStopHelperApp setEnabled:NO];
// give the app a moment to start, then check if it's running.
[self performSelector:@selector(updateHelperAppStatus) withObject:nil afterDelay:4.0];
}
- (void)updateHelperAppStatus {
if([FFHelperAppController isHelperAppRunning]) {
[helperAppStatus setStringValue:@"FunctionFlip is running."];
[startStopHelperApp setTitle:@"Stop FunctionFlip"];
} else {
[helperAppStatus setStringValue:@"FunctionFlip is stopped."];
[startStopHelperApp setTitle:@"Start FunctionFlip"];
}
[startStopHelperApp setEnabled:YES];
[helperAppProgress stopAnimation:self];
}
- (FFPreferenceManager *)ffPreferenceManager {
return [FFPreferenceManager sharedInstance];
}
// not cached since we want to ask DDHidKeyboard for any new keyboards every time
// could have caching with a check or notifcation of changes, but KISS
- (NSArray *)keyboards {
NSMutableArray *keyboards = [NSMutableArray arrayWithCapacity:1];
for(DDHidKeyboard *device in [DDHidKeyboard allKeyboards]) {
FFKeyboard *keyboard = [FFKeyboard keyboardWithDevice:device];
if(keyboard) [keyboards addObject:keyboard];
}
return keyboards;
}
- (IBAction)showAbout:(id)sender {
[NSApp beginSheet:aboutSheet modalForWindow:[[self mainView] window] modalDelegate:self didEndSelector:@selector(aboutSheetDidEnd:returnCode:contextInfo:) contextInfo:NULL];
}
- (IBAction)hideAbout:(id)sender {
[NSApp endSheet:aboutSheet];
}
- (void)aboutSheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo {
[aboutSheet orderOut:self];
}
- (IBAction)openHomePage:(id)sender {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://kevingessner.com/software/functionflip/"]];
}
- (NSString *)bundleVersionNumber {
return [[[NSBundle bundleWithIdentifier:FF_PREFPANE_BUNDLE_IDENTIFIER] infoDictionary] valueForKey:@"CFBundleVersion"];
}
- (IBAction)checkForUpdate:(id)sender {
[NSApp beginSheet:updateSheet modalForWindow:[[self mainView] window] modalDelegate:self didEndSelector:@selector(updateSheetDidEnd:returnCode:contextInfo:) contextInfo:NULL];
[updateSheetController beginUpdate];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:FF_UPDATE_URL] cachePolicy:NSURLRequestReloadRevalidatingCacheData timeoutInterval:15.0];
NSURLResponse *response; // required out-parameter for next line
NSError *error;
NSData *versionData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if(!versionData) {
NSLog(@"Couldn't get version: %@", error);
[updateSheetController updateFailed];
} else {
NSString *updateVersion = [[[NSString alloc] initWithData:versionData encoding:NSASCIIStringEncoding] autorelease];
NSLog(@"found version %@", updateVersion);
if([updateVersion isEqualToString:[self bundleVersionNumber]]) {
[updateSheetController updateNotAvailable];
} else {
[updateSheetController updateAvailable:updateVersion];
}
}
}
- (IBAction)hideUpdateSheet:(id)sender {
[NSApp endSheet:updateSheet];
}
- (void)updateSheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo {
[updateSheet orderOut:self];
}
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:KG_LOGIN_KEYPATH]) {
[self setStartsAtLogin:[[[FFPreferenceManager sharedInstance] valueForKey:KG_LOGIN_KEYPATH] boolValue]];
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
-(void) addAppAsLoginItem:(NSString *)appPath {
// This will retrieve the path for the application
// For example, /Applications/test.app
CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:appPath];
// Create a reference to the shared file list.
// We are adding it to the current user only.
// If we want to add it all users, use
// kLSSharedFileListGlobalLoginItems instead of
//kLSSharedFileListSessionLoginItems
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL,
kLSSharedFileListSessionLoginItems, NULL);
if (loginItems) {
//Insert an item to the list.
LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(loginItems,
kLSSharedFileListItemLast, NULL, NULL,
url, NULL, NULL);
if (item){
CFRelease(item);
}
}
CFRelease(loginItems);
}
-(void) deleteAppFromLoginItem:(NSString *)appPath {
CFURLRef url;
// Create a reference to the shared file list.
LSSharedFileListRef loginItems = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginItems) {
UInt32 seedValue;
NSArray *loginItemsArray = (NSArray *)LSSharedFileListCopySnapshot(loginItems, &seedValue);
for(id itemRef in loginItemsArray) {
//Resolve the item with URL
if (LSSharedFileListItemResolve((LSSharedFileListItemRef)itemRef, 0, &url, NULL) == noErr) {
NSString * urlPath = [(NSURL*)url path];
if ([urlPath isEqualToString:appPath]){
LSSharedFileListItemRemove(loginItems, (LSSharedFileListItemRef)itemRef);
}
}
}
[loginItemsArray release];
}
}
- (void)setStartsAtLogin:(BOOL)flag {
NSString *appPath = [FFHelperAppController pathToHelperApp];
if (flag) {
[self addAppAsLoginItem:appPath];
} else {
[self deleteAppFromLoginItem:appPath];
}
}
@end