-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
TPConnectionController.m
299 lines (236 loc) · 9.03 KB
/
TPConnectionController.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
//
// TPConnectionController.m
// teleport
//
// Created by JuL on Thu Jan 08 2004.
// Copyright (c) 2003-2005 abyssoft. All rights reserved.
//
#import "TPConnectionController.h"
#import "TPPreferencesManager.h"
#import "TPAuthenticationManager.h"
#import "TPClientController.h"
#import "TPEventsController.h"
#import "TPHotBorder.h"
#import "TPHostAnimationController.h"
#import "TPNetworkConnection.h"
#import "TPLocalHost.h"
#import "TPRemoteHost.h"
#import "TPMessage.h"
#import "TPHostsManager.h"
#import "TPTransfersManager.h"
#import "TPTransfer.h"
#import "TPPasteboardTransfer.h"
#import "TPFileTransfer.h"
NSString * TPScreenIndexKey = @"TPScreenIndex";
NSString * TPScreenPlacementKey = @"TPScreenPlacement";
NSString * TPMousePositionKey = @"TPMousePosition";
NSString * TPSwitchOptionsKey = @"TPSwitchOptions";
NSString * TPDraggedPathsKey = @"TPDraggedPaths";
NSString * TPDragImageKey = @"TPDragImage";
NSString * TPDragImageLocationKey = @"TPDragImageLocation";
@class TPClientController;
@interface TPConnectionController (Internal)
- (void)_preheatSwitchSoundIfNeeded;
@end
@implementation TPConnectionController
- (instancetype) init
{
self = [super init];
_currentConnection = nil;
[self _preheatSwitchSoundIfNeeded];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_localHostDidChange:) name:TPHostDidUpdateNotification object:[TPLocalHost localHost]];
return self;
}
#pragma mark -
#pragma mark Connection
- (void)setCurrentConnection:(TPNetworkConnection*)networkConnection
{
if(_currentConnection != networkConnection) {
[[TPTransfersManager manager] abortAllTransferRequests];
_currentConnection = networkConnection;
[_currentConnection setDelegate:self];
}
}
- (TPNetworkConnection*)currentConnection
{
return _currentConnection;
}
- (void)updateEventsController
{
if(_currentConnection != nil) {
TPEventsController * eventsController = [TPEventsController eventsControllerForRemoteHost:[_currentConnection connectedHost]];
if(_eventsController != eventsController) {
_eventsController = eventsController;
}
}
else {
_eventsController = nil;
}
}
- (TPEventsController*)eventsController
{
return _eventsController;
}
- (void)stopControl
{
[self stopControlWithDisconnect:YES];
}
- (void)stopControlWithDisconnect:(BOOL)disconnect
{
if(disconnect)
[self setCurrentConnection:nil];
}
- (id)optionForRemoteHost:(TPRemoteHost*)remoteHost key:(NSString*)key
{
return [remoteHost optionForKey:key];
}
- (void)_localHostDidChange:(NSNotification*)notification
{
if(_currentConnection != nil && ([_currentConnection localHostCapabilities] != [[TPLocalHost localHost] capabilities])) {
DebugLog(@"localHost did change: disconnect");
[self stopControl];
}
}
#pragma mark -
#pragma mark Hot border
- (TPHotBorder*)currentHotBorder
{
return nil;
}
- (void)setupHotBorder:(TPHotBorder*)hotBorder forHost:(TPRemoteHost*)host
{
[hotBorder bind:@"doubleTap" toObject:host withKeyPath:@"options.switchWithDoubleTap" options:nil];
[hotBorder bind:@"acceptDrags" toObject:host withKeyPath:@"options.copyFiles" options:nil];
}
- (void)takeDownHotBorder:(TPHotBorder*)hotBorder
{
[hotBorder unbind:@"doubleTap"];
[hotBorder unbind:@"acceptDrags"];
}
- (BOOL)hotBorder:(TPHotBorder*)hotBorder firedAtLocation:(NSPoint)location withDraggingInfo:(id <NSDraggingInfo>)draggingInfo
{
if([[TPPreferencesManager sharedPreferencesManager] boolForPref:SHOW_SWITCH_ANIMATION]) {
if([[TPLocalHost localHost] osVersion] >= TPHostOSVersion(5)) {
TPRemoteHost * remoteHost = [[TPHostsManager defaultManager] hostWithIdentifier:[hotBorder identifier]];
[[TPHostAnimationController controller] showFireAnimationForHost:remoteHost atPoint:[hotBorder screenPointFromLocalPoint:location flipped:NO] onScreen:[hotBorder screen] side:[hotBorder side]];
#if DEBUG_BUILD
if([remoteHost isEqual:[TPLocalHost localHost]]) {
[hotBorder activate];
}
#endif
return NO;
}
else {
return YES;
}
}
else {
return NO;
}
}
static NSString * _switchSoundPath = nil;
static NSSound * _switchSound = nil;
+ (NSSound*)_switchSound
{
NSString * soundPath = [[TPPreferencesManager sharedPreferencesManager] valueForPref:SWITCH_SOUND_PATH];
if(soundPath == nil || ![[NSFileManager defaultManager] fileExistsAtPath:soundPath]) {
soundPath = [[NSBundle bundleForClass:[self class]] pathForSoundResource:@"switch-sound"];
}
if((_switchSoundPath == nil) || ![_switchSoundPath isEqualToString:soundPath]) {
_switchSoundPath = [soundPath copy];
_switchSound = [[NSSound alloc] initWithContentsOfFile:soundPath byReference:NO];
if([_switchSound respondsToSelector:@selector(setVolume:)]) {
[_switchSound setVolume:0.3]; // play it soft
}
}
return _switchSound;
}
- (void)_preheatSwitchSoundIfNeeded
{
if([[TPPreferencesManager sharedPreferencesManager] boolForPref:PLAY_SWITCH_SOUND]) {
[[self class] _switchSound];
}
}
- (void)playSwitchSound
{
[[[self class] _switchSound] play];
}
#pragma mark -
#pragma mark Transfers
- (void)addDraggingInfo:(id<NSDraggingInfo>)draggingInfo toInfoDict:(NSMutableDictionary*)infoDict
{
if(draggingInfo != nil && infoDict != nil) {
NSPasteboard * dragPasteboard = [draggingInfo draggingPasteboard];
if([[dragPasteboard types] containsObject:NSFilenamesPboardType]) {
NSArray * draggedPaths = [dragPasteboard propertyListForType:NSFilenamesPboardType];
NSImage * dragImage = [draggingInfo draggedImage];
NSPoint dragImageLocation = [draggingInfo draggedImageLocation];
if(dragImage != nil) {
infoDict[TPDragImageKey] = dragImage;
infoDict[TPDragImageLocationKey] = [NSValue valueWithPoint:dragImageLocation];
}
infoDict[TPDraggedPathsKey] = draggedPaths;
}
}
}
- (void)beginTransfersWithInfoDict:(NSDictionary*)infoDict
{
/* First abort all pending transfer requests - real transfers will continue */
[[TPTransfersManager manager] abortAllTransferRequests];
TPRemoteHost * remoteHost = [[self currentConnection] connectedHost];
BOOL sharePasteboard = [[self optionForRemoteHost:remoteHost key:SHARE_PASTEBOARD] boolValue];
BOOL sharingPasteboardRequiresKey = [[self optionForRemoteHost:remoteHost key:REQUIRE_PASTEBOARD_KEY] boolValue];
int sharingPastebardKeyTag = [[self optionForRemoteHost:remoteHost key:PASTEBOARD_KEY_TAG] intValue];
/* Send pasteboard */
if(sharePasteboard && [[TPEventsController defaultController] event:[NSApp currentEvent] hasRequiredKeyIfNeeded:sharingPasteboardRequiresKey withTag:sharingPastebardKeyTag]) {
/* General pasteboard */
TPOutgoingPasteboardTransfer * generalPasteboardTransfer = (TPOutgoingPasteboardTransfer*)[TPOutgoingPasteboardTransfer transfer];
if([[self optionForRemoteHost:remoteHost key:LIMIT_PASTEBOARD_SIZE] boolValue]) {
int maxKSize = [[self optionForRemoteHost:remoteHost key:MAX_PASTEBOARD_SIZE] intValue];
unsigned long long maxSize = ((unsigned long long)maxKSize)*1024;
[generalPasteboardTransfer setMaxSize:maxSize];
}
[[TPTransfersManager manager] beginTransfer:generalPasteboardTransfer usingConnection:[self currentConnection]];
/* Optionnally the find pasteboard too */
if([[self optionForRemoteHost:remoteHost key:SYNC_FIND_PASTEBOARD] boolValue]) {
TPOutgoingPasteboardTransfer * findPasteboardTransfer = (TPOutgoingPasteboardTransfer*)[TPOutgoingPasteboardTransfer transfer];
[findPasteboardTransfer setPasteboardName:NSFindPboard];
[[TPTransfersManager manager] beginTransfer:findPasteboardTransfer usingConnection:[self currentConnection]];
}
}
BOOL copyFiles = [[self optionForRemoteHost:remoteHost key:COPY_FILES] boolValue];
/* Send files */
if(copyFiles && [[TPLocalHost localHost] pairWithHost:remoteHost hasCapability:TPHostDragNDropCapability]) {
NSArray * draggedFilePaths = infoDict[TPDraggedPathsKey];
if(draggedFilePaths != nil && [draggedFilePaths count] > 0) {
TPOutgoingFileTransfer * fileTransfer = (TPOutgoingFileTransfer*)[TPOutgoingFileTransfer transfer];
[fileTransfer setFilePaths:draggedFilePaths dragImage:infoDict[TPDragImageKey] location:[infoDict[TPDragImageLocationKey] pointValue]];
[[TPTransfersManager manager] beginTransfer:fileTransfer usingConnection:[self currentConnection]];
}
}
}
#pragma mark -
#pragma mark Messages handling
- (void)connection:(TPNetworkConnection*)connection receivedMessage:(TPMessage*)message
{
TPMsgType type = [message msgType];
switch(type) {
case TPTransferRequestMsgType:
[[TPTransfersManager manager] receiveTransferRequestWithInfoDict:[message infoDict] onConnection:connection isClient:[self isKindOfClass:[TPClientController class]]];
break;
case TPTransferSuccessMsgType:
[[TPTransfersManager manager] startTransferWithUID:[message infoDict][TPTransferUIDKey] usingConnection:connection onPort:[[message infoDict][TPTransferPortKey] intValue]];
break;
case TPTransferFailureMsgType:
[[TPTransfersManager manager] abortTransferWithUID:[message string]];
break;
default:
DebugLog(@"unknown message type: %ld", type);
}
}
- (void)connectionDisconnected:(TPNetworkConnection*)connection
{
DebugLog(@"connection broken - disconnecting");
[self stopControl];
}
@end