forked from revned/proximity
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ProximityAppController.m
427 lines (339 loc) · 12.1 KB
/
ProximityAppController.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#import "ProximityAppController.h"
#import "NSWorkspace+runFileAtPath.h"
#include <pwd.h>
#include <sys/types.h>
@implementation ProximityAppController
#pragma mark -
#pragma mark Delegate Methods
- (void)applicationWillTerminate:(NSNotification *)aNotification
{
[monitor stop];
}
- (void)awakeFromNib
{
NSBundle *bundle = [NSBundle mainBundle];
inRangeImage = [[NSImage alloc] initWithContentsOfFile: [bundle pathForResource: @"inRange" ofType: @"png"]];
inRangeAltImage = [[NSImage alloc] initWithContentsOfFile: [bundle pathForResource: @"inRangeAlt" ofType: @"png"]];
outOfRangeImage = [[NSImage alloc] initWithContentsOfFile: [bundle pathForResource: @"outRange" ofType: @"png"]];
outOfRangeAltImage = [[NSImage alloc] initWithContentsOfFile: [bundle pathForResource: @"outOfRange" ofType: @"png"]];
monitor = [[ProximityBluetoothMonitor alloc] init];
monitor.delegate = self;
[self createMenuBar];
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Defaults" ofType:@"plist"]]];
[self userDefaultsLoad];
}
- (void)windowWillClose:(NSNotification *)aNotification
{
[self userDefaultsSave];
if(monitoringEnabled.state == NSOnState && monitor.device) {
monitor.requiredSignalStrength = requiredSignalStrength.integerValue;
monitor.timeInterval = timerInterval.doubleValue;
[monitor refresh];
[monitor start];
} else {
[monitor stop];
}
}
- (void)proximityBluetoothMonitor:(ProximityBluetoothMonitor *)monitor foundDevice:(IOBluetoothDevice *)device {
[self setMenuIconInRange];
[self runInRangeScript:YES];
}
- (void)proximityBluetoothMonitor:(ProximityBluetoothMonitor *)monitor lostDevice:(IOBluetoothDevice *)device {
[self setMenuIconOutOfRange];
[self runOutOfRangeScript:YES];
}
#pragma mark -
#pragma mark AppController Methods
- (void)setStartAtLogin:(BOOL)enabled {
#ifdef DEBUG
NSLog(@"Cant start at login when in DEBUG mode");
#else
// Creating helper app complete URL
NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];
NSURL *url = [bundleURL URLByAppendingPathComponent:
@"Contents/Library/LoginItems/ProximityLoginHelper.app"];
// Registering helper app
if (LSRegisterURL((__bridge CFURLRef)url, true) != noErr) {
NSLog(@"LSRegisterURL failed!");
}
// Setting login
if (!SMLoginItemSetEnabled((CFStringRef)@"info.pich.proximityLoginHelper", enabled ? true : false)) {
NSLog(@"SMLoginItemSetEnabled failed!");
}
#endif
}
- (void)createMenuBar
{
NSMenu *myMenu;
NSMenuItem *menuItem;
// Menu for status bar item
myMenu = [[NSMenu alloc] init];
// Prefences menu item
menuItem = [myMenu addItemWithTitle:@"Preferences" action:@selector(showWindow:) keyEquivalent:@""];
[menuItem setTarget:self];
// Quit menu item
[myMenu addItemWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@""];
// Space on status bar
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
// Attributes of space on status bar
[statusItem setHighlightMode:YES];
[statusItem setMenu:myMenu];
[self setMenuIconOutOfRange];
}
- (void)setMenuIconInRange
{
[statusItem setImage:inRangeImage];
[statusItem setAlternateImage:inRangeAltImage];
//[statusItem setTitle:@"O"];
}
- (void)setMenuIconOutOfRange
{
[statusItem setImage:outOfRangeImage];
[statusItem setAlternateImage:outOfRangeAltImage];
// [statusItem setTitle:@"X"];
}
- (BOOL)isNewVersionAvailable
{
NSDictionary *dict = [[NSBundle mainBundle] infoDictionary];
NSArray *version = [[dict valueForKey:@"CFBundleVersion"] componentsSeparatedByString:@"."];
int thisVersionMajor = [[version objectAtIndex:0] intValue];
int thisVersionMinor = [[version objectAtIndex:1] intValue];
NSURL *url = [NSURL URLWithString:@"https://raw.github.com/Daij-Djan/proximity/master/Info.plist"];
dict = [NSDictionary dictionaryWithContentsOfURL:url];
version = [[dict valueForKey:@"CFBundleVersion"] componentsSeparatedByString:@"."];
int newVersionMajor = [[version objectAtIndex:0] intValue];
int newVersionMinor = [[version objectAtIndex:1] intValue];
if( thisVersionMajor < newVersionMajor || thisVersionMinor < newVersionMinor )
return YES;
return NO;
}
- (void)runScript:(NSURL*)pathUrl arguments:(NSArray*)args silent:(BOOL)silent
{
if(!pathUrl)
return;
NSError *error = nil;
BOOL b = [[NSWorkspace sharedWorkspace] runFileAtPath:pathUrl.path arguments:args error:&error];
if(!silent && !b) {
if(!error)
error = [NSError errorWithDomain:@"NSWorkspace" code:0 userInfo:@{ NSLocalizedDescriptionKey : @"unknown error" }];
[NSApp presentError:error];
}
}
- (void)runInRangeScript:(BOOL)silent {
[self runScript:inRangeScriptURL arguments:@[@"inRange"] silent:silent];
}
- (void)runOutOfRangeScript:(BOOL)silent {
[self runScript:outOfRangeScriptURL arguments:@[@"outOfRange"] silent:silent];
}
- (void)userDefaultsLoad
{
NSUserDefaults *defaults;
NSData *deviceAsData;
defaults = [NSUserDefaults standardUserDefaults];
//Timer interval
if( [[defaults stringForKey:@"timerInterval"] length] > 0 ) {
[timerInterval setStringValue:[defaults stringForKey:@"timerInterval"]];
monitor.timeInterval = timerInterval.doubleValue;
}
//require StrongSignal
[requiredSignalStrength setIntegerValue:[defaults integerForKey:@"requiredSignalStrength"]];
monitor.requiredSignalStrength = requiredSignalStrength.integerValue;
// Device
deviceAsData = [defaults objectForKey:@"device"];
if( [deviceAsData length] > 0 )
{
id device = [NSKeyedUnarchiver unarchiveObjectWithData:deviceAsData];
[deviceName setStringValue:[NSString stringWithFormat:@"%@ (%@)",
[device name], [device addressString]]];
monitor.device = device;
//update icon
[monitor refresh];
}
// Out of range script path
if( [defaults URLForKey:@"outOfRangeScriptURL"] ) {
outOfRangeScriptURL = [defaults URLForKey:@"outOfRangeScriptURL"];
[outOfRangeScriptPath setStringValue:outOfRangeScriptURL.path];
}
// In range script path
if( [defaults URLForKey:@"inRangeScriptURL"] ) {
inRangeScriptURL = [defaults URLForKey:@"inRangeScriptURL"];
[inRangeScriptPath setStringValue:inRangeScriptURL.path];
}
// Check for updates on startup
BOOL updating = [defaults boolForKey:@"updating"];
[checkUpdatesOnStartup setState:updating ? NSOnState : NSOffState];
if( updating ) {
[self checkForUpdates:nil silent:YES];
}
// Monitoring enabled
BOOL monitoring = [defaults boolForKey:@"enabled"];
[monitoringEnabled setState:monitoring ? NSOnState : NSOffState];
if( monitoring && monitor.device ) {
[monitor start];
}
// Run scripts on startup
BOOL startup = [defaults boolForKey:@"executeOnStartup"];
[runScriptsOnStartup setState:startup ? NSOnState : NSOffState];
if( startup && monitor.device )
{
if( monitoring )
{
[monitor refresh];
}
}
//autostart
BOOL autostart = [defaults boolForKey:@"startOnSystemStartup"];
[startOnSystemStartup setState:autostart ? NSOnState : NSOffState];
[self setStartAtLogin:autostart];
}
- (void)userDefaultsSave
{
NSUserDefaults *defaults;
NSData *deviceAsData;
defaults = [NSUserDefaults standardUserDefaults];
// Monitoring enabled
BOOL monitoring = ( [monitoringEnabled state] == NSOnState ? TRUE : FALSE );
[defaults setBool:monitoring forKey:@"enabled"];
// Update checking
BOOL updating = ( [checkUpdatesOnStartup state] == NSOnState ? TRUE : FALSE );
[defaults setBool:updating forKey:@"updating"];
// Execute scripts on startup
BOOL startup = ( [runScriptsOnStartup state] == NSOnState ? TRUE : FALSE );
[defaults setBool:startup forKey:@"executeOnStartup"];
// Timer interval
[defaults setObject:[timerInterval stringValue] forKey:@"timerInterval"];
// In range script
[defaults setURL:inRangeScriptURL forKey:@"inRangeScriptURL"];
// Out of range script
[defaults setURL:outOfRangeScriptURL forKey:@"outOfRangeScriptURL"];
// Device
if( monitor.device ) {
deviceAsData = [NSKeyedArchiver archivedDataWithRootObject:monitor.device];
[defaults setObject:deviceAsData forKey:@"device"];
}
// autostart
[defaults setBool:[startOnSystemStartup state] == NSOnState ? TRUE : FALSE forKey:@"executeOnStartup"];
[defaults setInteger:requiredSignalStrength.integerValue forKey:@"requiredSignalStrength"];
//persist
[defaults synchronize];
}
#pragma mark -
#pragma mark Interface Methods
- (IBAction)changeDevice:(id)sender
{
IOBluetoothDeviceSelectorController *deviceSelector;
deviceSelector = [IOBluetoothDeviceSelectorController deviceSelector];
[deviceSelector runModal];
NSArray *results;
results = [deviceSelector getResults];
if( !results )
return;
id device = [results objectAtIndex:0];
[deviceName setStringValue:[NSString stringWithFormat:@"%@ (%@)",
[device name],
[device addressString]]];
monitor.device = device;
}
- (IBAction)checkConnectivity:(id)sender
{
if(!monitor.device) {
NSRunAlertPanel( @"Unknown", @"Please select a bluetooth device", nil, nil, nil, nil );
return;
}
[progressIndicator startAnimation:nil];
ProximityBluetoothMonitor *testMon = [[ProximityBluetoothMonitor alloc] init];
testMon.device = monitor.device;
testMon.requiredSignalStrength = monitor.requiredSignalStrength;
[testMon refresh];
if( testMon.status == ProximityBluetoothStatusInRange )
{
NSRunAlertPanel( @"Found", @"Device is powered on and in range", nil, nil, nil, nil );
}
else
{
NSRunAlertPanel( @"Not Found", @"Device is powered off or out of range", nil, nil, nil, nil );
}
[progressIndicator stopAnimation:nil];
}
- (void)checkForUpdates:(id)sender silent:(BOOL)silent
{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
BOOL br = [self isNewVersionAvailable];
dispatch_sync(dispatch_get_main_queue(), ^{
if( br ) {
if( NSRunAlertPanel( @"Proximity", @"A new version of Proximity is available for download.",
@"Close", @"Download", nil, nil ) == NSAlertAlternateReturn )
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://github.com/Daij-Djan/proximity"]];
}
}
else {
if(!silent) {
NSRunAlertPanel( @"Proximity", @"You have the latest version.", @"Close", nil, nil, nil );
}
}
});
});
}
- (IBAction)checkForUpdates:(id)sender
{
[self checkForUpdates:sender silent:NO];
}
- (IBAction)toggleStartOnSystemStartup:(id)sender {
BOOL autostart = ([startOnSystemStartup state]==NSOnState);
[self setStartAtLogin:autostart];
}
- (IBAction)about:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://github.com/Daij-Djan/proximity/README"]];
}
- (NSURL*)chooseScript {
NSString *homePath = [NSString stringWithUTF8String:getpwuid(getuid())->pw_dir];
NSOpenPanel *op = [NSOpenPanel openPanel];
[op setDirectoryURL:[NSURL fileURLWithPath:homePath]];
[op setAllowsMultipleSelection:NO];
if([op runModal]==NSOKButton) {
NSArray *files = [op URLs];
if([files count])
return [files objectAtIndex:0];
}
return nil;
}
- (IBAction)inRangeScriptChange:(id)sender
{
NSURL *file = [self chooseScript];
if(file)
[inRangeScriptPath setStringValue:file.path];
}
- (IBAction)inRangeScriptClear:(id)sender
{
[inRangeScriptPath setStringValue:@""];
}
- (IBAction)inRangeScriptTest:(id)sender
{
[self runInRangeScript:NO];
}
- (IBAction)outOfRangeScriptChange:(id)sender
{
NSURL *file = [self chooseScript];
if(file) {
outOfRangeScriptURL = file;
[outOfRangeScriptPath setStringValue:file.path];
}
}
- (IBAction)outOfRangeScriptClear:(id)sender
{
[outOfRangeScriptPath setStringValue:@""];
}
- (IBAction)outOfRangeScriptTest:(id)sender
{
[self runOutOfRangeScript:NO];
}
- (void)showWindow:(id)sender
{
[NSApp activateIgnoringOtherApps:YES];
[prefsWindow makeKeyAndOrderFront:self];
[prefsWindow center];
[monitor stop];
}
@end