-
Notifications
You must be signed in to change notification settings - Fork 71
/
AppController.m
416 lines (328 loc) · 9.78 KB
/
AppController.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
#import "AppController.h"
@implementation AppController
#pragma mark -
#pragma mark Delegate Methods
- (void)applicationWillTerminate:(NSNotification *)aNotification
{
[self stopMonitoring];
}
- (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"]];
priorStatus = OutOfRange;
[self createMenuBar];
[self userDefaultsLoad];
}
- (void)windowWillClose:(NSNotification *)aNotification
{
[self userDefaultsSave];
[self stopMonitoring];
[self startMonitoring];
}
#pragma mark -
#pragma mark AppController Methods
- (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];
[statusItem retain];
// Attributes of space on status bar
[statusItem setHighlightMode:YES];
[statusItem setMenu:myMenu];
[self menuIconOutOfRange];
}
- (void)handleTimer:(NSTimer *)theTimer
{
if( [self isInRange] )
{
if( priorStatus == OutOfRange )
{
priorStatus = InRange;
[self menuIconInRange];
[self runInRangeScript];
}
}
else
{
if( priorStatus == InRange )
{
priorStatus = OutOfRange;
[self menuIconOutOfRange];
[self runOutOfRangeScript];
}
}
[self startMonitoring];
}
- (BOOL)isInRange
{
if( device && [device remoteNameRequest:nil] == kIOReturnSuccess )
return true;
return false;
}
- (void)menuIconInRange
{
[statusItem setImage:inRangeImage];
[statusItem setAlternateImage:inRangeAltImage];
//[statusItem setTitle:@"O"];
}
- (void)menuIconOutOfRange
{
[statusItem setImage:outOfRangeImage];
[statusItem setAlternateImage:outOfRangeAltImage];
// [statusItem setTitle:@"X"];
}
- (BOOL)newVersionAvailable
{
NSURL *url = [NSURL URLWithString:@"http://reduxcomputing.com/download/Proximity.plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfURL:url];
NSArray *version = [[dict valueForKey:@"version"] componentsSeparatedByString:@"."];
int newVersionMajor = [[version objectAtIndex:0] intValue];
int newVersionMinor = [[version objectAtIndex:1] intValue];
if( thisVersionMajor < newVersionMajor || thisVersionMinor < newVersionMinor )
return YES;
return NO;
}
- (void)runInRangeScript
{
NSAppleScript *script;
NSDictionary *errDict;
NSAppleEventDescriptor *ae;
script = [[NSAppleScript alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:[inRangeScriptPath stringValue]]
error:&errDict];
ae = [script executeAndReturnError:&errDict];
}
- (void)runOutOfRangeScript
{
NSAppleScript *script;
NSDictionary *errDict;
NSAppleEventDescriptor *ae;
script = [[NSAppleScript alloc]
initWithContentsOfURL:[NSURL fileURLWithPath:[outOfRangeScriptPath stringValue]]
error:&errDict];
ae = [script executeAndReturnError:&errDict];
}
- (void)startMonitoring
{
if( [monitoringEnabled state] == NSOnState )
{
timer = [NSTimer scheduledTimerWithTimeInterval:[timerInterval intValue]
target:self
selector:@selector(handleTimer:)
userInfo:nil
repeats:NO];
[timer retain];
}
}
- (void)stopMonitoring
{
[timer invalidate];
}
- (void)userDefaultsLoad
{
NSUserDefaults *defaults;
NSData *deviceAsData;
defaults = [NSUserDefaults standardUserDefaults];
// Device
deviceAsData = [defaults objectForKey:@"device"];
if( [deviceAsData length] > 0 )
{
device = [NSKeyedUnarchiver unarchiveObjectWithData:deviceAsData];
[device retain];
[deviceName setStringValue:[NSString stringWithFormat:@"%@ (%@)",
[device name], [device addressString]]];
if( [self isInRange] )
{
priorStatus = InRange;
[self menuIconInRange];
}
else
{
priorStatus = OutOfRange;
[self menuIconOutOfRange];
}
}
//Timer interval
if( [[defaults stringForKey:@"timerInterval"] length] > 0 )
[timerInterval setStringValue:[defaults stringForKey:@"timerInterval"]];
// Out of range script path
if( [[defaults stringForKey:@"outOfRangeScriptPath"] length] > 0 )
[outOfRangeScriptPath setStringValue:[defaults stringForKey:@"outOfRangeScriptPath"]];
// In range script path
if( [[defaults stringForKey:@"inRangeScriptPath"] length] > 0 )
[inRangeScriptPath setStringValue:[defaults stringForKey:@"inRangeScriptPath"]];
// Check for updates on startup
BOOL updating = [defaults boolForKey:@"updating"];
if( updating ) {
[checkUpdatesOnStartup setState:NSOnState];
if( [self newVersionAvailable] )
{
if( NSRunAlertPanel( @"Proximity", @"A new version of Proximity is available for download.",
@"Close", @"Download", nil, nil ) == NSAlertAlternateReturn )
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://reduxcomputing.com/proximity/"]];
}
}
}
// Monitoring enabled
BOOL monitoring = [defaults boolForKey:@"enabled"];
if( monitoring ) {
[monitoringEnabled setState:NSOnState];
[self startMonitoring];
}
// Run scripts on startup
BOOL startup = [defaults boolForKey:@"executeOnStartup"];
if( startup )
{
[runScriptsOnStartup setState:NSOnState];
if( monitoring )
{
if( [self isInRange] ) {
[self runInRangeScript];
} else {
[self runOutOfRangeScript];
}
}
}
}
- (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 setObject:[inRangeScriptPath stringValue] forKey:@"inRangeScriptPath"];
// Out of range script
[defaults setObject:[outOfRangeScriptPath stringValue] forKey:@"outOfRangeScriptPath"];
// Device
if( device ) {
deviceAsData = [NSKeyedArchiver archivedDataWithRootObject:device];
[defaults setObject:deviceAsData forKey:@"device"];
}
[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;
device = [results objectAtIndex:0];
[device retain];
[deviceName setStringValue:[NSString stringWithFormat:@"%@ (%@)",
[device name],
[device addressString]]];
}
- (IBAction)checkConnectivity:(id)sender
{
[progressIndicator startAnimation:nil];
if( [self isInRange] )
{
[progressIndicator stopAnimation:nil];
NSRunAlertPanel( @"Found", @"Device is powered on and in range", nil, nil, nil, nil );
}
else
{
[progressIndicator stopAnimation:nil];
NSRunAlertPanel( @"Not Found", @"Device is powered off or out of range", nil, nil, nil, nil );
}
}
- (IBAction)checkForUpdates:(id)sender
{
if( [self newVersionAvailable] )
{
if( NSRunAlertPanel( @"Proximity", @"A new version of Proximity is available for download.",
@"Close", @"Download", nil, nil ) == NSAlertAlternateReturn )
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://reduxcomputing.com/proximity/"]];
}
}
else
{
NSRunAlertPanel( @"Proximity", @"You have the latest version.", @"Close", nil, nil, nil );
}
}
- (IBAction)donate:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://reduxcomputing.com/donate.php"]];
}
- (IBAction)enableMonitoring:(id)sender
{
// See windowWillClose: method
}
- (IBAction)inRangeScriptChange:(id)sender
{
NSOpenPanel *op = [NSOpenPanel openPanel];
NSArray *fileTypesArray;
fileTypesArray = [NSArray arrayWithObjects:@"scpt", nil];
[op setCanChooseFiles:YES];
[op setAllowedFileTypes:fileTypesArray];
if ( [op runModal] == NSOKButton ) {
NSArray *filenames = [op URLs];
[inRangeScriptPath setStringValue:[[filenames objectAtIndex:0] path]];
}
}
- (IBAction)inRangeScriptClear:(id)sender
{
[inRangeScriptPath setStringValue:@""];
}
- (IBAction)inRangeScriptTest:(id)sender
{
[self runInRangeScript];
}
- (IBAction)outOfRangeScriptChange:(id)sender
{
NSOpenPanel *op = [NSOpenPanel openPanel];
NSArray *fileTypesArray;
fileTypesArray = [NSArray arrayWithObjects:@"scpt", nil];
[op setCanChooseFiles:YES];
[op setAllowedFileTypes:fileTypesArray];
if ( [op runModal] == NSOKButton ) {
NSArray *filenames = [op URLs];
[outOfRangeScriptPath setStringValue:[[filenames objectAtIndex:0] path]];
}
}
- (IBAction)outOfRangeScriptClear:(id)sender
{
[outOfRangeScriptPath setStringValue:@""];
}
- (IBAction)outOfRangeScriptTest:(id)sender
{
[self runOutOfRangeScript];
}
- (void)showWindow:(id)sender
{
[prefsWindow makeKeyAndOrderFront:self];
[prefsWindow center];
[self stopMonitoring];
}
@end