forked from johndbritton/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TPMainController.m
458 lines (376 loc) · 16 KB
/
TPMainController.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//
// TPMainController.m
// Teleport
//
// Created by JuL on Thu Dec 25 2003.
// Copyright (c) 2003-2005 abyssoft. All rights reserved.
//
#import "TPMainController.h"
#import "TPClientController.h"
#import "TPServerController.h"
#import "TPNetworkConfigurationWatcher.h"
#import "TPStatusItemController.h"
#import "TPAuthenticationManager.h"
#import "TPRemoteHost.h"
#import "TPLocalHost.h"
#import "TPMessage.h"
#import "TPBonjourController.h"
#import "TPHostsManager.h"
#import "TPHostSnapping.h"
#import "TPPreferencesManager.h"
#import "TPTransfersManager.h"
#import "TPBezelController.h"
#import "TPPreferencePane.h"
#import <unistd.h>
#import <signal.h>
#import <sys/types.h>
#import <sys/sysctl.h>
#import <sys/param.h>
#import <sys/mount.h>
#import <Security/AuthSession.h>
#import <Sparkle/Sparkle.h>
#import "TPNetworkConnection.h"
#import "TPTestsController.h"
typedef CGError CGSError;
extern CGSError CPSGetFrontProcess( CPSProcessSerNum *pPSN);
extern CGSError CPSSetFrontProcess( const CPSProcessSerNum *pPSN);
#define VERSION_CHECK_URL @"http://www.abyssoft.com/software/teleport/version.plist"
static TPMainController * _mainController = nil;
@interface TPMainController (Internal)
- (void)_checkAccessibility;
- (void)_checkEncryption;
@end
@implementation TPMainController
+ (TPMainController*)sharedController
{
return _mainController;
}
- (BOOL)canBeControlledByHostWithIdentifier:(NSString*)identifier
{
if([[TPClientController defaultController] isControlling]) {
TPRemoteHost * controlledHost = [[[TPClientController defaultController] currentConnection] connectedHost];
return ![[controlledHost identifier] isEqualToString:identifier];
}
else
return YES;
}
- (BOOL)canControlHostWithIdentifier:(NSString*)identifier
{
if([[TPServerController defaultController] isControlled]) {
TPRemoteHost * controllingHost = [[[TPServerController defaultController] currentConnection] connectedHost];
return ![[controllingHost identifier] isEqualToString:identifier];
}
else
return YES;
}
#pragma mark -
#pragma mark Application delegate
- (void)applicationDidFinishLaunching:(NSNotification*)notification
{
#if RUN_TESTS
[TPTestsController runTests];
#else
_mainController = self;
_frontProcessNum.lo = 0;
/* Setup Notifications */
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(_prefDidChange:) name:TPDefaultsDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_prefsDidUpgrade:) name:TPPreferencesDidUpgradeNotification object:nil];
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(screenDidLock:) name:@"com.apple.screenIsLocked" object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(sessionDidResignActive:) name:NSWorkspaceSessionDidResignActiveNotification object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(sessionDidBecomeActive:) name:NSWorkspaceSessionDidBecomeActiveNotification object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(computerWillSleep:) name:NSWorkspaceWillSleepNotification object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(computerDidWake:) name:NSWorkspaceDidWakeNotification object:nil];
/* Waken controllers */
[TPPreferencesManager sharedPreferencesManager];
[TPClientController defaultController];
[TPServerController defaultController];
[TPStatusItemController defaultController];
/* Watch the network configuration */
_networkConfigurationWatcher = [[TPNetworkConfigurationWatcher alloc] init];
[_networkConfigurationWatcher setDelegate:self];
[_networkConfigurationWatcher startWatching];
/* Data loading */
[[TPHostsManager defaultManager] loadHosts];
[[TPAuthenticationManager defaultManager] loadHosts];
/* Browse on Bonjour */
[[TPBonjourController defaultController] browse];
/* Misc */
[[TPTransfersManager manager] setDelegate:[TPBezelController defaultController]];
/* Warning dialogs */
[self performSelector:@selector(_showWarningDialogs) withObject:nil afterDelay:0.0];
/* Auto-check version */
if([[TPPreferencesManager sharedPreferencesManager] boolForPref:AUTOCHECK_VERSION])
[self checkVersionsAndConfirm:NO];
/* Setup initial host borders */
[[TPClientController defaultController] updateTriggersAndShowVisualHint:YES];
/* Notify that we're ready */
[[TPHostsManager defaultManager] notifyChanges];
NSArray *peeredHosts = [[TPHostsManager defaultManager] hostsWithState:TPHostPeeredState];
if (peeredHosts.count == 0 && ![[TPPreferencesManager sharedPreferencesManager] boolForPref:ALLOW_CONTROL]) {
[[TPPreferencePane preferencePane] showWindow:nil];
}
#if 0
[TPTestsController runFakeHostsTest];
#endif
#endif
}
- (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag
{
[[TPPreferencePane preferencePane] showWindow:nil];
return YES;
}
- (void)_showWarningDialogs
{
[self _checkAccessibility];
[self _checkEncryption];
}
- (void)applicationWillTerminate:(NSNotification *)notification
{
[[TPHostsManager defaultManager] saveHosts];
[[TPAuthenticationManager defaultManager] saveHosts];
if([[TPClientController defaultController] isControlling])
[[TPClientController defaultController] stopControl];
if([[TPServerController defaultController] allowControl])
[[TPServerController defaultController] stopSharing];
}
- (void)applicationWillResignActive:(NSNotification *)aNotification
{
if([[TPClientController defaultController] isControlling])
[NSApp activateIgnoringOtherApps:YES];
}
//- (void)applicationDidResignActive:(NSNotification *)aNotification
//{
// NSLog(@"did resign");
// if([[TPClientController defaultController] isControlling]) {
// NSLog(@"ACTIVATE");
// [NSApp activateIgnoringOtherApps:YES];
// }
//}
- (void)applicationDidChangeScreenParameters:(NSNotification *)aNotification
{
[[TPHostsManager defaultManager] notifyChanges];
if([[TPServerController defaultController] allowControl]) {
[[TPBonjourController defaultController] updateTXTRecordOfPublishService];
}
[[TPClientController defaultController] updateTriggersAndShowVisualHint:YES];
}
- (void)screenDidLock:(NSNotification*)notification
{
TPClientController * clientController = [TPClientController defaultController];
if([clientController isControlling])
[clientController stopControl];
}
- (void)sessionDidResignActive:(NSNotification*)notification
{
TPClientController * clientController = [TPClientController defaultController];
if([clientController isControlling])
[clientController stopControl];
else
[clientController setCurrentConnection:nil];
TPServerController * serverController = [TPServerController defaultController];
if([serverController isControlled])
[serverController stopControl];
else
[serverController setCurrentConnection:nil];
if([serverController allowControl])
[serverController stopSharing];
}
- (void)sessionDidBecomeActive:(NSNotification*)notification
{
if([[TPServerController defaultController] allowControl])
[[TPServerController defaultController] startSharing];
}
- (void)computerWillSleep:(NSNotification*)notification
{
if([[TPClientController defaultController] isControlling])
[[TPClientController defaultController] stopControl];
if([[TPServerController defaultController] allowControl])
[[TPServerController defaultController] stopSharing];
[[TPHostsManager defaultManager] saveHosts];
[[TPAuthenticationManager defaultManager] saveHosts];
}
- (void)computerDidWake:(NSNotification*)notification
{
[[TPHostsManager defaultManager] notifyChanges];
[[TPClientController defaultController] updateTriggersAndShowVisualHint:YES];
CFDictionaryRef sessionInfoDict = CGSessionCopyCurrentDictionary();
if(CFDictionaryGetValue(sessionInfoDict, kCGSessionOnConsoleKey) == kCFBooleanTrue && [[TPServerController defaultController] allowControl])
[[TPServerController defaultController] startSharing];
CFRelease(sessionInfoDict);
}
- (void)networkConfigurationDidChange:(TPNetworkConfigurationWatcher*)watcher
{
DebugLog(@"Network configuration did change");
if([[TPPreferencesManager sharedPreferencesManager] boolForPref:DISCONNECT_ON_NETWORK_CONFIG_CHANGE]) {
TPServerController * server = [TPServerController defaultController];
if([server allowControl]) {
[server stopSharing];
[server startSharing];
}
[[TPBonjourController defaultController] stopBrowsing];
[[TPBonjourController defaultController] browse];
[[TPClientController defaultController] stopControl];
}
}
#pragma mark -
#pragma mark Misc
- (void)_checkAccessibility
{
if (![[TPLocalHost localHost] checkAccessibility]) {
NSAlert * alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Disabled access for assistive devices", @"Text in dialog about accessibility") defaultButton:NSLocalizedString(@"OK", @"Button title") alternateButton:nil otherButton:nil informativeTextWithFormat:NSLocalizedString(@"The access for assistive devices has been disabled although teleport needs this to operate.\nPlease allow teleport and launch it again.", @"Explanation in dialog for accessibility")];
int returnCode = [self presentAlert:alert];
[NSApp terminate:nil];
}
}
- (void)_checkEncryption
{
if([[TPPreferencesManager sharedPreferencesManager] boolForPref:ENABLED_ENCRYPTION]) {
if(![[TPLocalHost localHost] hasIdentity]) {
NSAlert * alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Encryption requires a certificate", @"Text in dialog for certificate missing error") defaultButton:NSLocalizedString(@"Launch Assistant", @"Button title") alternateButton:NSLocalizedString(@"Disable Encryption", @"Button title") otherButton:nil informativeTextWithFormat:NSLocalizedString(@"In order to activate encryption, teleport needs a valid certificate, but couldn't find one in your Keychain.\nYou'll need to create a certificate and re-activate encryption. Note that the certificate algorithm and key size must match between the server and the clients.\n\nWould you like to launch Certificate Assistant to create a new self-signed certificate?\nRecommended settings: RSA 1024 bits, all others to default values.", @"Explanation in dialog for certificate missing error")];
int returnCode = [self presentAlert:alert];
[[TPPreferencesManager sharedPreferencesManager] setValue:@NO forKey:ENABLED_ENCRYPTION];
switch(returnCode) {
case NSAlertDefaultReturn:
[[NSWorkspace sharedWorkspace] launchAppWithBundleIdentifier:@"com.apple.CertificateAssistant" options:NSWorkspaceLaunchDefault additionalEventParamDescriptor:nil launchIdentifier:NULL];
break;
case NSAlertAlternateReturn:
break;
}
}
}
else
[[TPLocalHost localHost] resetIdentity];
}
- (void)_prefDidChange:(NSNotification*)notification
{
if([[notification object] isEqualToString:ENABLED_ENCRYPTION]) {
[self _checkEncryption];
[[TPLocalHost localHost] notifyChange];
}
else if([[notification object] isEqualToString:WAKE_ON_LAN]) {
[[[TPHostsManager defaultManager] hostsWithState:TPHostPeeredOfflineState] makeObjectsPerformSelector:@selector(notifyChange)];
}
else if([[notification object] isEqualToString:CERTIFICATE_IDENTIFIER]) {
[[TPLocalHost localHost] reloadIdentity];
}
else if([[notification object] isEqualToString:SHARED_SCREEN_INDEX]) {
[[TPLocalHost localHost] notifyChange];
}
}
#define TPTerminateDaemonNotification @"TPTerminateDaemonNotification"
- (void)_prefsDidUpgrade:(NSNotification*)notification
{
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:TPTerminateDaemonNotification object:nil userInfo:nil deliverImmediately:YES]; // be sure to quit previous teleport
}
#pragma mark -
#pragma mark UI
- (void)goFrontmost
{
if(CPSGetFrontProcess(&_frontProcessNum) != kCGErrorSuccess) {
_frontProcessNum.lo = 0;
DebugLog(@"get front process failed");
}
else {
DebugLog(@"get front process succeded: hi=%d low=%d", _frontProcessNum.hi, _frontProcessNum.lo);
}
// ProcessSerialNumber psn;
// GetCurrentProcess(&psn);
// SetFrontProcess(&psn);
[NSApp activateIgnoringOtherApps:TRUE];
}
- (void)leaveFrontmost
{
if(_frontProcessNum.lo > 0) {
CPSSetFrontProcess(&_frontProcessNum);
DebugLog(@"switched back to process hi=%d low=%d", _frontProcessNum.hi, _frontProcessNum.lo);
_frontProcessNum.lo = 0;
}
}
- (int)presentAlert:(NSAlert*)alert
{
[self goFrontmost];
int result = [alert runModal];
[self leaveFrontmost];
return result;
}
#pragma mark -
#pragma mark Version checking
- (void)checkVersionFromNotification:(NSNotification*)notification
{
[self checkVersionsAndConfirm:YES];
}
- (void)checkVersionsAndConfirm:(BOOL)confirm
{
if(confirm) {
[[SUUpdater sharedUpdater] checkForUpdates:nil];
}
else {
[[SUUpdater sharedUpdater] checkForUpdatesInBackground];
}
}
//- (void)_handleCheckVersionWithDictionary:(NSDictionary*)productVersionDict
//{
//
//
//// NSNumber * confirm = [productVersionDict objectForKey:@"TPConfirm"];
//// NSString * currentBuildString = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"];
//// if(currentBuildString == nil)
//// return;
//// int currentBuild = [currentBuildString intValue];
//// int latestBuild = [[productVersionDict objectForKey:@"build"] intValue];
//// NSString * latestVersionString = [productVersionDict objectForKey:@"version"];
////
//// if(latestBuild > currentBuild) {
//// NSString * description = [productVersionDict valueForKey:@"description"];
//// NSString * info = [NSString stringWithFormat:NSLocalizedString(@"teleport %@ is now available.\n%@\n\nWould you like to download the new version now?", nil), latestVersionString, description];
////
//// NSAlert * alert = [NSAlert alertWithMessageText:NSLocalizedString(@"A new version of teleport is available!", nil) defaultButton:NSLocalizedString(@"Yes", nil) alternateButton:NSLocalizedString(@"No", nil) otherButton:NSLocalizedString(@"Don't show again", nil) informativeTextWithFormat:info];
//// int result = [(TPMainController*)[NSApp delegate] presentAlert:alert];
////
//// switch(result) {
//// case NSAlertDefaultReturn:
//// [self updateToVersion:latestVersionString fromURL:[productVersionDict objectForKey:@"url"]];
//// break;
//// case NSAlertAlternateReturn:
//// break;
//// case NSAlertOtherReturn:
//// [[TPPreferencesManager sharedPreferencesManager] setValue:[NSNumber numberWithBool:NO] forKey:AUTOCHECK_VERSION];
//// break;
//// }
//// }
//// else if(latestBuild < currentBuild) {
//// if([confirm boolValue]) {
//// NSAlert * alert = [NSAlert alertWithMessageText:NSLocalizedString(@"Betatester!", nil) defaultButton:NSLocalizedString(@"OK", nil) alternateButton:nil otherButton:nil informativeTextWithFormat:[NSString stringWithFormat:NSLocalizedString(@"Your build (%d) is newer than the latest available (%d), so you must be a betatester. Congratulations!", nil), currentBuild, latestBuild]];
//// [(TPMainController*)[NSApp delegate] presentAlert:alert];
//// }
//// }
//// else {
//// if([confirm boolValue]) {
//// NSAlert * alert = [NSAlert alertWithMessageText:NSLocalizedString(@"teleport is up-to-date.", nil) defaultButton:NSLocalizedString(@"OK", nil) alternateButton:nil otherButton:nil informativeTextWithFormat:NSLocalizedString(@"You have the most recent version of teleport.", nil)];
//// [(TPMainController*)[NSApp delegate] presentAlert:alert];
//// }
//// }
//}
//- (void)threadedCheckVersionAndConfirm:(NSNumber*)confirm
//{
// NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
//
// NSMutableDictionary * productVersionDict = [NSMutableDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:VERSION_CHECK_URL]];
// if(productVersionDict == nil) {
// [pool release];
// return;
// }
//
// [productVersionDict setObject:confirm forKey:@"TPConfirm"];
//
// [self performSelectorOnMainThread:@selector(_handleCheckVersionWithDictionary:) withObject:productVersionDict waitUntilDone:YES];
//
// [pool release];
//}
//
//- (void)updateToVersion:(NSString*)version fromURL:(NSString*)urlString
//{
// NSURL * url = [NSURL URLWithString:urlString];
// [[NSWorkspace sharedWorkspace] openURL:url];
//}
@end