-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
TPHost.m
334 lines (274 loc) · 8.08 KB
/
TPHost.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
//
// TPHost.m
// Teleport
//
// Created by JuL on Sun Dec 07 2003.
// Copyright (c) 2003-2005 abyssoft. All rights reserved.
//
#import "TPHost.h"
#import "TPPreferencesManager.h"
#define MAX_BACKGROUND_IMAGE_WIDTH 240
NSString * TPHostDidUpdateNotification = @"TPHostDidUpdateNotification";
NSString * TPHostIdentifierKey = @"identifier";
NSString * TPHostComputerNameKey = @"computerName";
NSString * TPHostBackgroundImageDataKey = @"backgroundImage";
NSString * TPHostMacAddressKey = @"macAddress";
NSString * TPHostCapabilitiesKey = @"capabilities";
NSString * TPHostOSVersionKey = @"osVersion";
@implementation TPHost
- (instancetype) init
{
self = [super init];
_computerName = nil;
[self invalidateMACAddress];
return self;
}
- (BOOL)isEqual:(id)object
{
if(![object isKindOfClass:[TPHost class]])
return NO;
TPHost * otherObject = (TPHost*)object;
return [[self identifier] isEqualToString:[otherObject identifier]];
}
#if LEGACY_BUILD
- (unsigned)hash
#else
- (NSUInteger)hash
#endif
{
return [[self identifier] hash];
}
- (instancetype) initWithCoder:(NSCoder *)coder
{
self = [super init];
_computerName = [[coder decodeObjectForKey:TPHostComputerNameKey] copy];
if([coder containsValueForKey:TPHostMacAddressKey]) {
const uint8_t * tempBuffer = [coder decodeBytesForKey:TPHostMacAddressKey returnedLength:NULL];
memcpy(_macAddress.bytes, tempBuffer, kIOEthernetAddressSize);
}
else
[self invalidateMACAddress];
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:[self identifier] forKey:TPHostIdentifierKey];
[coder encodeObject:[self computerName] forKey:TPHostComputerNameKey];
IOEthernetAddress address = [self MACAddress];
[coder encodeBytes:address.bytes length:kIOEthernetAddressSize forKey:TPHostMacAddressKey];
[coder encodeInt:[self capabilities] forKey:TPHostCapabilitiesKey];
[coder encodeInt32:[self osVersion] forKey:TPHostOSVersionKey];
#if 0 // sent with TPBackgroundImageTransfer
if([self hasCustomBackgroundImage]) {
NSData * backgroundImageData = [self backgroundImageData];
if(backgroundImageData != nil)
[coder encodeBytes:[backgroundImageData bytes] length:[backgroundImageData length] forKey:TPHostBackgroundImageDataKey];
}
#endif
}
- (NSString*)identifier
{
return nil;
}
- (SInt32)osVersion
{
return 0;
}
- (NSString*)address
{
return nil;
}
- (NSString*)computerName
{
if(_computerName == nil)
return NSLocalizedString(@"unnamed", @"Name for unamed server");
return _computerName;
}
- (void)setComputerName:(NSString*)computerName
{
if(computerName != _computerName) {
_computerName = [computerName copy];
[self notifyChange];
}
}
- (BOOL)hasValidMACAddress
{
int i;
for(i=0; i<kIOEthernetAddressSize; i++) {
if(_macAddress.bytes[i] != 0)
return YES;
}
return NO;
}
- (void)invalidateMACAddress
{
memset(_macAddress.bytes, 0, kIOEthernetAddressSize);
[self notifyChange];
}
- (IOEthernetAddress)MACAddress
{
return _macAddress;
}
- (void)setMACAddress:(IOEthernetAddress)macAddress
{
_macAddress = macAddress;
}
#pragma mark -
#pragma mark Screen
- (NSArray*)screens
{
return nil;
}
- (NSScreen*)screenAtIndex:(unsigned)screenIndex
{
NSArray * screens = [self screens];
if(screenIndex < [screens count])
return screens[screenIndex];
return nil;
}
#pragma mark -
#pragma mark Background image
+ (NSImage*)backgroundImageFromDesktopPicture:(NSImage*)desktopPicture
{
NSSize size = [desktopPicture size];
if(size.width <= MAX_BACKGROUND_IMAGE_WIDTH)
return desktopPicture;
NSRect backgroundImageRect = NSZeroRect;
backgroundImageRect.size = size;
backgroundImageRect.size.height *= (float)MAX_BACKGROUND_IMAGE_WIDTH/NSWidth(backgroundImageRect);
backgroundImageRect.size.width = MAX_BACKGROUND_IMAGE_WIDTH;
NSImage * backgroundImage = [[NSImage alloc] initWithSize:backgroundImageRect.size];
[backgroundImage lockFocus];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[desktopPicture drawInRect:backgroundImageRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[backgroundImage unlockFocus];
return backgroundImage;
}
- (NSImage*)defaultBackgroundImage
{
int osVersion = [self osVersion];
NSString * imageName = nil;
NSImage * image = nil;
if(osVersion < TPHostOSVersion(3)) {
imageName = @"10.2-Jaguar";
}
else if(osVersion < TPHostOSVersion(4)) {
imageName = @"10.3-Panther";
}
else if(osVersion < TPHostOSVersion(5)) {
imageName = @"10.4-Tiger";
}
else if(osVersion < TPHostOSVersion(6)) {
imageName = @"10.5-Leopard";
}
else if(osVersion < TPHostOSVersion(7)) {
imageName = @"10.6-SnowLeopard";
}
else if(osVersion < TPHostOSVersion(8)) {
imageName = @"10.7-Lion";
} // Makes sure that a desktop picture will be available whether or not it is available locally
else if(osVersion < TPHostOSVersion(9)) {
imageName = @"10.8-MountainLion";
}
else if(osVersion < TPHostOSVersion(10)) {
imageName = @"10.9-Mavericks";
}
else if(osVersion < TPHostOSVersion(11)) {
imageName = @"10.10-Yosemite";
}
else if(osVersion < TPHostOSVersion(12)) {
imageName = @"10.11-ElCapitan";
}
else if(osVersion < TPHostOSVersion(13)) {
imageName = @"10.12-Sierra";
}
else if(osVersion < TPHostOSVersion(14)) {
imageName = @"10.13-HighSierra";
}
else if(osVersion < TPHostOSVersion(15)) {
imageName = @"10.14-Mojave";
}
else {
// Make sure an image exists at this path as well
NSString *defaultDesktopImage = @"/System/Library/CoreServices/DefaultDesktop.jpg";
if(![[NSFileManager defaultManager] fileExistsAtPath:defaultDesktopImage]){
// The file name is different in some systems (e.g. Mojave)
defaultDesktopImage = @"/System/Library/CoreServices/DefaultBackground.jpg";
}
if(![[NSFileManager defaultManager] fileExistsAtPath:defaultDesktopImage]){
NSLog(@"Unable to set icon to default desktop image. Default does not exist at path: %@", defaultDesktopImage);
}
NSImage * fullImage = [[NSImage alloc] initWithContentsOfFile:defaultDesktopImage];
image = [TPHost backgroundImageFromDesktopPicture:fullImage];
}
if(image == nil) {
image = [NSImage imageNamed:imageName];
}
return image;
}
- (NSImage*)backgroundImage
{
return nil;
}
- (NSData*)backgroundImageData
{
NSImage * backgroundImage = [self backgroundImage];
if(backgroundImage != nil) {
#if 0
NSBitmapImageRep * imageRep = [[backgroundImage representations] objectAtIndex:0];
NSData * backgroundImageData = [imageRep representationUsingType:NSJPEGFileType properties:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1.0] forKey:NSImageCompressionFactor]];
#else
NSData * backgroundImageData = [backgroundImage TIFFRepresentationUsingCompression:NSTIFFCompressionLZW factor:1.0];
#endif
return backgroundImageData;
}
else
return nil;
}
- (BOOL)hasCustomBackgroundImage
{
return NO;
}
#pragma mark -
#pragma mark Capabilities
- (TPHostCapability)capabilities
{
return TPHostNoCapability;
}
- (BOOL)hasCapability:(TPHostCapability)capability
{
return NO;
}
- (BOOL)pairWithHost:(TPHost*)host hasCapability:(TPHostCapability)capability
{
return [host hasCapability:capability] && [self hasCapability:capability];
}
+ (TPHost*)hostFromHostData:(NSData*)hostData
{
NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:hostData];
TPHost * host = [[self alloc] initWithCoder:unarchiver];
[unarchiver finishDecoding];
return host;
}
- (NSData*)hostData
{
NSMutableData * hostData = [[NSMutableData alloc] init];
NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:hostData];
[self encodeWithCoder:archiver];
[archiver finishEncoding];
return hostData;
}
- (void)notifyChange
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(notifyChangeNow) object:nil];
[self performSelector:@selector(notifyChangeNow) withObject:nil afterDelay:0.1];
}
- (void)notifyChangeNow
{
[[NSNotificationCenter defaultCenter] postNotificationName:TPHostDidUpdateNotification object:self];
}
- (NSString*)description
{
return [NSString stringWithFormat:@"identifier=%@ computerName=%@ address=%@", [self identifier], [self computerName], [self address]];
}
@end