This repository has been archived by the owner on Mar 22, 2019. It is now read-only.
/
SosumiAppDelegate.m
325 lines (265 loc) · 10.1 KB
/
SosumiAppDelegate.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
//
// SosumiAppDelegate.m
// Sosumi
//
// Created by Tyler Hall on 11/28/10.
// Copyright 2010 Click On Tyler, LLC. All rights reserved.
//
#import "SosumiAppDelegate.h"
#import "SSMAccount.h"
#import "SSMDevice.h"
#import "THImageRow.h"
#import "EMKeychainItem.h"
@implementation SosumiAppDelegate
@synthesize window;
@synthesize panelSyncActivity;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshOutline:) name:@"DEVICES_DID_UPDATE" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showSpinner:) name:@"SHOW_SPINNER" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideSpinner:) name:@"HIDE_SPINNER" object:nil];
NSArray *accounts = [[NSUserDefaults standardUserDefaults] objectForKey:@"accounts"];
if(accounts == nil) {
[self addAccountWasClicked:self];
} else {
for(NSDictionary *dict in accounts) {
NSTreeNode *treeNode;
SSMAccount *tmpAccount = [[SSMAccount alloc] init];
tmpAccount.username = dict[@"username"];
EMGenericKeychainItem *kcItem = [EMGenericKeychainItem genericKeychainItemForService:@"Sosumi" withUsername:tmpAccount.username];
tmpAccount.password = kcItem.password;
[tmpAccount beginUpdatingDevices];
treeNode = [NSTreeNode treeNodeWithRepresentedObject:tmpAccount];
tmpAccount.treeNode = treeNode;
[treeController insertObject:treeNode atArrangedObjectIndexPath:[NSIndexPath indexPathWithIndex:0]];
}
}
// In an old version we were storing the user's password in NSUserDefaults. After we switched to using the Keychain,
// we forgot to erase the old password from disk. This fixes that.
NSMutableArray *fixedAccounts = [NSMutableArray array];
for(int i = 0; i < [accounts count]; i++) {
NSDictionary *credentials = accounts[i];
NSDictionary *dict = @{@"username": credentials[@"username"]};
[fixedAccounts addObject:dict];
}
[[NSUserDefaults standardUserDefaults] setObject:fixedAccounts forKey:@"accounts"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self refreshOutline:nil];
NSString *html = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"map" ofType:@"html"] encoding:NSUTF8StringEncoding error:nil];
[[map mainFrame] loadHTMLString:html baseURL:[NSURL URLWithString:@"http://maps.google.com"]];
}
- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender
{
[window makeKeyAndOrderFront:self];
return NO;
}
#pragma mark -
#pragma mark Outline View Delegates
#pragma mark -
- (void)outlineView:(NSOutlineView *)outlineView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
id device = [[item representedObject] representedObject];
if([device isKindOfClass:[SSMDevice class]]) {
[(THImageRow *)cell setImageName:[(SSMDevice *)device statusImage]];
} else {
[(THImageRow *)cell setImageName:nil];
}
}
- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item
{
return [[[item representedObject] representedObject] isKindOfClass:[SSMAccount class]];
}
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldShowOutlineCellForItem:(id)item
{
return NO;
}
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item
{
return ![[[item representedObject] representedObject] isKindOfClass:[SSMAccount class]];
}
- (void)refreshOutline:(NSNotification *)notification
{
[outlineView reloadData];
[outlineView expandItem:nil expandChildren:YES];
[self refreshMap];
}
#pragma mark -
#pragma mark Split View Delegates
#pragma mark -
- (BOOL)splitView:(NSSplitView *)splitView shouldAdjustSizeOfSubview:(NSView *)subview
{
if(subview == [splitView subviews][0]) {
return NO;
}
return YES;
}
- (CGFloat)splitView:(NSSplitView *)splitView constrainMinCoordinate:(CGFloat)proposedMin ofSubviewAt:(NSInteger)dividerIndex
{
return 150.0;
}
- (CGFloat)splitView:(NSSplitView *)splitView constrainMaxCoordinate:(CGFloat)proposedMin ofSubviewAt:(NSInteger)dividerIndex
{
return 350.0;
}
#pragma mark -
#pragma mark Webview Delgates
#pragma mark -
- (void)refreshMap
{
NSArray *args;
WebScriptObject *ws = [map windowScriptObject];
for(NSTreeNode *accountTreeNode in [treeController content]) {
for(NSTreeNode *deviceTreeNode in [accountTreeNode childNodes]) {
SSMDevice *device = [deviceTreeNode representedObject];
if(device.latitude != nil && device.longitude != nil) {
// NSLog(@"mapped %@ %@ %@", device.name, device.latitude, device.longitude);
args = @[device.deviceId, device.name, device.latitude, device.longitude, device.imageURL];
[ws callWebScriptMethod:@"createMarker" withArguments:args];
}
}
}
}
#pragma mark -
#pragma mark IBActions
#pragma mark -
- (IBAction)addAccountWasClicked:(id)sender
{
[txtUsername setStringValue:@""];
[txtPassword setStringValue:@""];
[NSApp beginSheet:panelAddAccount modalForWindow:self.window modalDelegate:self didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:NULL];
}
- (IBAction)removeAccountWasClicked:(id)sender
{
if([[treeController selectedNodes] count] == 0) {
return;
}
NSTreeNode *treeNode = [treeController selectedNodes][0];
SSMAccount *account = [[[treeNode parentNode] representedObject] representedObject];
EMGenericKeychainItem *kcItem = [EMGenericKeychainItem genericKeychainItemForService:@"Sosumi" withUsername:account.username];
if(kcItem) {
[kcItem removeFromKeychain];
}
NSMutableArray *accounts = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"accounts"]];
if(accounts) {
for(int i = 0; i < [accounts count]; i++) {
NSDictionary *tmpAccount = accounts[i];
if([tmpAccount[@"username"] isEqualToString:account.username]) {
[accounts removeObject:tmpAccount];
}
}
}
[[NSUserDefaults standardUserDefaults] setObject:accounts forKey:@"accounts"];
[[NSUserDefaults standardUserDefaults] synchronize];
[treeController removeObjectAtArrangedObjectIndexPath:[[treeNode parentNode] indexPath]];
}
- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
}
- (IBAction)viewOnMapWasClicked:(id)sender
{
if([[treeController selectedNodes] count] == 0) {
return;
}
NSTreeNode *node = [treeController selectedNodes][0];
SSMDevice *device = [[node representedObject] representedObject];
WebScriptObject *ws = [map windowScriptObject];
NSArray *args = @[device.deviceId];
[ws callWebScriptMethod:@"viewOnMap" withArguments:args];
}
- (IBAction)refreshWasClicked:(id)sender
{
for(NSTreeNode *accountTreeNode in [treeController content]) {
SSMAccount *account = [accountTreeNode representedObject];
[account refresh];
}
}
- (IBAction)sendMessageWasClicked:(id)sender
{
[txtSubject setStringValue:@""];
[txtMessage setStringValue:@""];
[chkAlarm setState:NSOffState];
[NSApp beginSheet:panelSendMessage modalForWindow:self.window modalDelegate:self didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:NULL];
}
- (IBAction)lockDeviceWasClicked:(id)sender
{
if([[treeController selectedNodes] count] == 0) {
return;
}
NSTreeNode *node = [treeController selectedNodes][0];
SSMDevice *device = [[node representedObject] representedObject];
SSMAccount *account = [[[node parentNode] representedObject] representedObject];
[account remoteLockDevice:device.deviceId withPasscode:@"9911"];
}
- (IBAction)dismissPanel:(id)sender
{
[NSApp endSheet:panelSendMessage returnCode:NSCancelButton];
[panelSendMessage orderOut:nil];
[NSApp endSheet:panelAddAccount returnCode:NSCancelButton];
[panelAddAccount orderOut:nil];
}
- (IBAction)confirmAddAccount:(id)sender
{
EMGenericKeychainItem *kcItem = [EMGenericKeychainItem genericKeychainItemForService:@"Sosumi" withUsername:[txtUsername stringValue]];
if(kcItem) {
kcItem.password = [txtPassword stringValue];
} else {
kcItem = [EMGenericKeychainItem addGenericKeychainItemForService:@"Sosumi" withUsername:[txtUsername stringValue] password:[txtPassword stringValue]];
}
NSTreeNode *treeNode;
SSMAccount *tmpAccount = [[SSMAccount alloc] init];
tmpAccount.username = kcItem.username;
tmpAccount.password = kcItem.password;
treeNode = [NSTreeNode treeNodeWithRepresentedObject:tmpAccount];
tmpAccount.treeNode = treeNode;
[tmpAccount beginUpdatingDevices];
[treeController insertObject:treeNode atArrangedObjectIndexPath:[NSIndexPath indexPathWithIndex:0]];
NSMutableArray *accounts = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"accounts"]];
[accounts addObject:[NSDictionary dictionaryWithObjects:@[kcItem.username] forKeys:@[@"username"]]];
[[NSUserDefaults standardUserDefaults] setObject:accounts forKey:@"accounts"];
[[NSUserDefaults standardUserDefaults] synchronize];
[NSApp endSheet:panelAddAccount returnCode:NSOKButton];
[panelAddAccount orderOut:nil];
}
- (IBAction)confirmSendMessage:(id)sender
{
if([[treeController selectedNodes] count] == 0) {
return;
}
NSTreeNode *node = [treeController selectedNodes][0];
SSMDevice *device = [[node representedObject] representedObject];
SSMAccount *account = [[[node parentNode] representedObject] representedObject];
[account sendMessage:[txtMessage stringValue] withSubject:[txtSubject stringValue] andAlarm:[chkAlarm state] toDevice:device.deviceId];
[NSApp endSheet:panelSendMessage returnCode:NSOKButton];
[panelSendMessage orderOut:nil];
}
- (IBAction)copyCoordsWasClicked:(id)sender
{
if([[treeController selectedNodes] count] == 0) {
return;
}
NSTreeNode *node = [treeController selectedNodes][0];
SSMDevice *device = [[node representedObject] representedObject];
NSPasteboard *pb = [NSPasteboard generalPasteboard];
NSArray *types = @[NSStringPboardType];
[pb declareTypes:types owner:self];
[pb setString:[device coords] forType:NSStringPboardType];
}
- (IBAction)showSyncActivityPanel:(id)sender
{
if([self.panelSyncActivity isVisible]) {
[self.panelSyncActivity close];
} else {
[self.panelSyncActivity orderFront:nil];
}
}
#pragma mark -
#pragma mark Misc
#pragma mark -
- (void)showSpinner:(NSNotification *)notification
{
[piSpinner startAnimation:self];
}
- (void)hideSpinner:(NSNotification *)notification
{
[piSpinner stopAnimation:self];
}
@end