-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathITAddressBookMgr.m
More file actions
721 lines (571 loc) · 22.1 KB
/
ITAddressBookMgr.m
File metadata and controls
721 lines (571 loc) · 22.1 KB
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/*
** ITAddressBookMgr.m
**
** Copyright (c) 2002, 2003
**
** Author: Fabian, Ujwal S. Setlur
** Initial code by Kiichi Kusama
**
** Project: iTerm
**
** Description: keeps track of the address book data.
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#import "ITAddressBookMgr.h"
#import <iTerm/PreferencePanel.h>
#import <iTerm/Tree.h>
#import <iTerm/iTermTerminalProfileMgr.h>
#import <iTerm/iTermKeyBindingMgr.h>
#import <iTerm/iTermDisplayProfileMgr.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SAFENODE(n) ((TreeNode*)((n)?(n):(bookmarks)))
static NSString* ADDRESS_BOOK_FILE = @"~/Library/Application Support/iTerm/AddressBook";
static TreeNode *defaultBookmark = nil;
@implementation ITAddressBookMgr
+ (id)sharedInstance;
{
static ITAddressBookMgr* shared = nil;
if (!shared)
shared = [[ITAddressBookMgr alloc] init];
return shared;
}
- (id)init;
{
self = [super init];
return self;
}
- (void)dealloc;
{
[bookmarks release];
[bonjourGroup release];
[bonjourServices removeAllObjects];
[bonjourServices release];
[sshBonjourBrowser stop];
[ftpBonjourBrowser stop];
[telnetBonjourBrowser stop];
[sshBonjourBrowser release];
[ftpBonjourBrowser release];
[telnetBonjourBrowser release];
[super dealloc];
}
- (void) locateBonjourServices
{
sshBonjourBrowser = [[NSNetServiceBrowser alloc] init];
ftpBonjourBrowser = [[NSNetServiceBrowser alloc] init];
telnetBonjourBrowser = [[NSNetServiceBrowser alloc] init];
bonjourServices = [[NSMutableArray alloc] init];
[sshBonjourBrowser setDelegate: self];
[ftpBonjourBrowser setDelegate: self];
[telnetBonjourBrowser setDelegate: self];
[sshBonjourBrowser searchForServicesOfType: @"_ssh._tcp." inDomain: @""];
[ftpBonjourBrowser searchForServicesOfType: @"_ftp._tcp." inDomain: @""];
[telnetBonjourBrowser searchForServicesOfType: @"_telnet._tcp." inDomain: @""];
}
- (void) setBookmarks: (NSDictionary *) aDict
{
//NSLog(@"%s: %@", __PRETTY_FUNCTION__, aDict);
[bookmarks release];
bookmarks = [TreeNode treeFromDictionary: aDict];
[bookmarks setIsLeaf: NO];
[bookmarks retain];
// make sure we have a default bookmark
if([self _checkForDefaultBookmark: bookmarks defaultBookmark: &defaultBookmark] == NO)
{
NSMutableDictionary *aDict;
char *userShell, *thisUser;
NSString *shell;
NSString *aName;
TreeNode *childNode;
aDict = [[NSMutableDictionary alloc] init];
// Get the user's default shell
if((thisUser = getenv("USER")) != NULL) {
shell = [NSString stringWithFormat: @"login -fp %s", thisUser];
} else if((userShell = getenv("SHELL")) != NULL) {
shell = [NSString stringWithCString: userShell];
} else {
shell = @"/bin/bash --login";
}
aName = NSLocalizedStringFromTableInBundle(@"Default",@"iTerm", [NSBundle bundleForClass: [self class]],
@"Terminal Profiles");
[aDict setObject: aName forKey: KEY_NAME];
[aDict setObject: shell forKey: KEY_COMMAND];
[aDict setObject: shell forKey: KEY_DESCRIPTION];
[aDict setObject: NSHomeDirectory() forKey: KEY_WORKING_DIRECTORY];
[aDict setObject: [[iTermTerminalProfileMgr singleInstance] defaultProfileName] forKey: KEY_TERMINAL_PROFILE];
[aDict setObject: [[iTermKeyBindingMgr singleInstance] globalProfileName] forKey: KEY_KEYBOARD_PROFILE];
[aDict setObject: [[iTermDisplayProfileMgr singleInstance] defaultProfileName] forKey: KEY_DISPLAY_PROFILE];
[aDict setObject: @"Yes" forKey: KEY_DEFAULT_BOOKMARK];
childNode = [[TreeNode alloc] initWithData: aDict parent: nil children: [NSArray array]];
[childNode setIsLeaf: YES];
[bookmarks insertChild: childNode atIndex: [bookmarks numberOfChildren]];
[aDict release];
[childNode release];
defaultBookmark = childNode;
}
// add any bonjour services if we have any
if([bonjourGroup numberOfChildren] > 0)
{
[bookmarks insertChild: bonjourGroup atIndex: [bookmarks numberOfChildren]];
}
//[bookmarks recursiveSortChildren];
}
- (NSDictionary *) bookmarks
{
NSDictionary *aDict;
int anIndex;
//NSLog(@"%s", __PRETTY_FUNCTION__);
// remove bonjour group since we do not want to save that
anIndex = [[bookmarks children] indexOfObject: bonjourGroup];
[bonjourGroup retain];
[bookmarks removeChild: bonjourGroup];
aDict = [bookmarks dictionary];
if(anIndex != NSNotFound)
[bookmarks insertChild: bonjourGroup atIndex: anIndex];
[bonjourGroup release];
return (aDict);
}
- (BOOL) mayDeleteBookmarkNode: (TreeNode *) aNode
{
BOOL mayDeleteNode = YES;
if([defaultBookmark isDescendantOfNode: aNode])
mayDeleteNode = NO;
if([aNode isDescendantOfNode: bonjourGroup])
mayDeleteNode = NO;
return (mayDeleteNode);
}
// Model for NSOutlineView tree structure
- (id) child:(int)index ofItem:(id)item
{
return ([SAFENODE(item) childAtIndex: index]);
}
- (BOOL) isExpandable:(id)item
{
//NSLog(@"%s: %@", __PRETTY_FUNCTION__, item);
return (![SAFENODE(item) isLeaf]);
}
- (int) numberOfChildrenOfItem:(id)item
{
//NSLog(@"%s", __PRETTY_FUNCTION__);
return ([SAFENODE(item) numberOfChildren]);
}
- (id) objectForKey: (id) key inItem: (id) item
{
NSDictionary *data = [SAFENODE(item) nodeData];
return ([data objectForKey: key]);
}
- (void) setObjectValue: (id) object forKey: (id) key inItem: (id) item
{
NSMutableDictionary *aDict;
aDict = [[NSMutableDictionary alloc] initWithDictionary: [SAFENODE(item) nodeData]];
[aDict setObject: object forKey: key];
[SAFENODE(item) setNodeData: aDict];
[aDict release];
// Post a notification for all listeners that bookmarks have changed
[[NSNotificationCenter defaultCenter] postNotificationName: @"iTermReloadAddressBook" object: nil userInfo: nil];
}
- (void) addFolder: (NSString *) folderName toNode: (TreeNode *) aNode
{
TreeNode *targetNode, *childNode;
NSMutableDictionary *aDict;
// NSLog(@"%s: %@", __PRETTY_FUNCTION__, folderName);
targetNode = SAFENODE(aNode);
aDict = [[NSMutableDictionary alloc] init];
[aDict setObject: folderName forKey: KEY_NAME];
[aDict setObject: @"" forKey: KEY_DESCRIPTION];
childNode = [[TreeNode alloc] initWithData: aDict parent: nil children: [NSArray array]];
[childNode setIsLeaf: NO];
[targetNode insertChild: childNode atIndex: [targetNode numberOfChildren]];
[aDict release];
[childNode release];
// Post a notification for all listeners that bookmarks have changed
[[NSNotificationCenter defaultCenter] postNotificationName: @"iTermReloadAddressBook" object: nil userInfo: nil];
}
- (void) addBookmarkWithData: (NSDictionary *) data toNode: (TreeNode *) aNode;
{
TreeNode *targetNode, *childNode;
NSMutableDictionary *aDict;
//NSLog(@"%s", __PRETTY_FUNCTION__);
if(data == nil)
return;
targetNode = SAFENODE(aNode);
aDict = [[NSMutableDictionary alloc] initWithDictionary: data];
childNode = [[TreeNode alloc] initWithData: aDict parent: nil children: [NSArray array]];
[childNode setIsLeaf: YES];
[targetNode insertChild: childNode atIndex: [targetNode numberOfChildren]];
[aDict release];
[childNode release];
// Post a notification for all listeners that bookmarks have changed
[[NSNotificationCenter defaultCenter] postNotificationName: @"iTermReloadAddressBook" object: nil userInfo: nil];
}
- (void) setBookmarkWithData: (NSDictionary *) data forNode: (TreeNode *) aNode
{
NSMutableDictionary *aDict;
aDict = [[NSMutableDictionary alloc] initWithDictionary: [SAFENODE(aNode) nodeData]];
[aDict addEntriesFromDictionary: data];
[SAFENODE(aNode) setNodeData: aDict];
[aDict release];
// Post a notification for all listeners that bookmarks have changed
[[NSNotificationCenter defaultCenter] postNotificationName: @"iTermReloadAddressBook" object: nil userInfo: nil];
}
- (void) deleteBookmarkNode: (TreeNode *) aNode
{
[aNode removeFromParent];
// Post a notification for all listeners that bookmarks have changed
[[NSNotificationCenter defaultCenter] postNotificationName: @"iTermReloadAddressBook" object: nil userInfo: nil];
}
- (TreeNode *) rootNode
{
return (bookmarks);
}
- (TreeNode *) defaultBookmark
{
return (defaultBookmark);
}
- (void) setDefaultBookmark: (TreeNode *) aNode
{
NSMutableDictionary *aMutableDict;
if(aNode == nil)
return;
// get the current default bookmark
aMutableDict = [NSMutableDictionary dictionaryWithDictionary: [defaultBookmark nodeData]];
[aMutableDict removeObjectForKey: KEY_DEFAULT_BOOKMARK];
[defaultBookmark setNodeData: aMutableDict];
// set the new default bookmark
aMutableDict = [NSMutableDictionary dictionaryWithDictionary: [aNode nodeData]];
[aMutableDict setObject: @"Yes" forKey: KEY_DEFAULT_BOOKMARK];
[aNode setNodeData: aMutableDict];
defaultBookmark = aNode;
}
- (NSDictionary *) defaultBookmarkData
{
return ([defaultBookmark nodeData]);
}
- (NSDictionary *) dataForBookmarkWithName: (NSString *) bookmarkName
{
NSArray *pathComponents;
NSEnumerator *anEnumerator;
NSString *aPathComponent;
TreeNode *theNode;
NSDictionary *theData;
// break up into path components
pathComponents = [bookmarkName componentsSeparatedByString: @"/"];
anEnumerator = [pathComponents objectEnumerator];
theNode = bookmarks;
while((aPathComponent = [anEnumerator nextObject]) != NULL && theNode != NULL)
{
theNode = [self _getBookmarkNodeWithName: aPathComponent searchFromNode: theNode];
}
if([theNode isGroup])
theData = nil;
else
theData = [theNode nodeData];
return (theData);
}
// migrate any old bookmarks in the old format we might have
- (void) migrateOldBookmarks
{
NSMutableArray *_addressBookArray = [[NSUnarchiver unarchiveObjectWithFile: [ADDRESS_BOOK_FILE stringByExpandingTildeInPath]] retain];
NSDictionary *_adEntry;
NSMutableDictionary *aBookmarkData;
int i;
NSFileManager *fileManager;
TreeNode *childNode;
NSString *shortcut;
for (i = 0; i < [_addressBookArray count]; i++)
{
_adEntry = [_addressBookArray objectAtIndex: i];
// add all entries except for default entry
if([[_adEntry objectForKey:@"DefaultEntry"] boolValue] == NO)
{
aBookmarkData = [[NSMutableDictionary alloc] init];
[aBookmarkData setObject: [_adEntry objectForKey: @"Name"] forKey: KEY_NAME];
[aBookmarkData setObject: [_adEntry objectForKey: @"Command"] forKey: KEY_DESCRIPTION];
[aBookmarkData setObject: [_adEntry objectForKey: @"Command"] forKey: KEY_COMMAND];
[aBookmarkData setObject: [_adEntry objectForKey: @"Directory"] forKey: KEY_WORKING_DIRECTORY];
[aBookmarkData setObject: [[iTermTerminalProfileMgr singleInstance] defaultProfileName] forKey: KEY_TERMINAL_PROFILE];
[aBookmarkData setObject: [[iTermKeyBindingMgr singleInstance] globalProfileName] forKey: KEY_KEYBOARD_PROFILE];
[aBookmarkData setObject: [[iTermDisplayProfileMgr singleInstance] defaultProfileName] forKey: KEY_DISPLAY_PROFILE];
shortcut=([[_adEntry objectForKey:@"Shortcut"] intValue]? [NSString stringWithFormat:@"%c",[[_adEntry objectForKey:@"Shortcut"] intValue]]:@"");
shortcut = [shortcut lowercaseString];
[aBookmarkData setObject: shortcut forKey: KEY_SHORTCUT];
childNode = [[TreeNode alloc] initWithData: aBookmarkData parent: nil children: [NSArray array]];
[childNode setIsLeaf: YES];
[bookmarks insertChild: childNode atIndex: [bookmarks numberOfChildren]];
[childNode release];
[aBookmarkData release];
}
}
// delete old addressbook file.
fileManager = [NSFileManager defaultManager];
if([fileManager isDeletableFileAtPath: [ADDRESS_BOOK_FILE stringByExpandingTildeInPath]])
[fileManager removeFileAtPath: [ADDRESS_BOOK_FILE stringByExpandingTildeInPath] handler: nil];
}
- (int) indexForBookmark: (NSDictionary *)bookmark
{
return [[self rootNode] indexForNode: bookmark];
}
- (NSDictionary *) bookmarkForIndex: (int)index
{
return [[self rootNode] nodeForIndex: index];
}
// NSNetServiceBrowser delegate methods
- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
{
NSMutableDictionary *aDict;
// Get the OS info without using Carbon. This method is recommended by Apple
NSDictionary *systemVersionDict = [NSDictionary dictionaryWithContentsOfFile:@"/System/Library/CoreServices/SystemVersion.plist"];
NSString *versionString = [systemVersionDict objectForKey:@"ProductVersion"];
NSArray *osArray = [versionString componentsSeparatedByString:@"."];
int osElementCount = [osArray count];
int osMajor = (osElementCount >= 1) ? [[osArray objectAtIndex:0] intValue] : 0;
int osMinor = (osElementCount >= 2) ? [[osArray objectAtIndex:1] intValue] : 0;
//int osBugfix = (osElementCount >= 3) ? [[osArray objectAtIndex:2] intValue] : 0;
//NSLog(@"%s: %@", __PRETTY_FUNCTION__, aNetService);
if(bonjourGroup == nil)
{
aDict = [[NSMutableDictionary alloc] init];
[aDict setObject: @"Bonjour" forKey: KEY_NAME];
[aDict setObject: @"" forKey: KEY_DESCRIPTION];
[aDict setObject: @"Yes" forKey: KEY_BONJOUR_GROUP];
bonjourGroup = [[TreeNode alloc] initWithData: aDict parent: nil children: [NSArray array]];
[bonjourGroup setIsLeaf: NO];
[aDict release];
}
// add a subgroup for this service if it does not already exist
[self _getBonjourServiceTypeNode: [aNetService type]];
// resolve the service
// add to temporary array to retain it so that resolving works.
[bonjourServices addObject: aNetService];
[aNetService setDelegate: self];
if (osMajor == 10 && osMinor >= 4)
[aNetService resolveWithTimeout: (NSTimeInterval)5];
else
[aNetService resolve];
}
- (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
{
TreeNode *serviceNode, *childNode, *parentNode;
NSDictionary *nodeData;
NSEnumerator *anEnumerator;
BOOL sshService = NO;
//NSLog(@"%s: %@", __PRETTY_FUNCTION__, aNetService);
if(aNetService == nil)
return;
// grab the service group node in the tree
serviceNode = [self _getBonjourServiceTypeNode: [aNetService type]];
// remove host entry from this group
anEnumerator = [[serviceNode children] objectEnumerator];
while ((childNode = [anEnumerator nextObject]))
{
nodeData = [childNode nodeData];
if([[nodeData objectForKey: KEY_NAME] isEqualToString: [aNetService name]])
{
// check for ssh service to remove sftp service below
if([[[serviceNode nodeData] objectForKey: KEY_BONJOUR_SERVICE] isEqualToString: @"ssh"])
sshService = YES;
parentNode = [childNode nodeParent];
[childNode removeFromParent];
if([parentNode numberOfChildren] == 0)
[parentNode removeFromParent];
break;
}
}
// if this was an ssh service, remove associated sftp service also
if(sshService == YES)
{
// grab the service group node in the tree
serviceNode = [self _getBonjourServiceTypeNode: @"_sftp.tcp."];
// remove host entry from this group
anEnumerator = [[serviceNode children] objectEnumerator];
while ((childNode = [anEnumerator nextObject]))
{
nodeData = [childNode nodeData];
if([[nodeData objectForKey: KEY_NAME] isEqualToString: [aNetService name]])
{
parentNode = [childNode nodeParent];
[childNode removeFromParent];
if([parentNode numberOfChildren] == 0)
[parentNode removeFromParent];
break;
}
}
}
// if bonjour group is empty, remove it
if([bonjourGroup numberOfChildren] == 0)
[bonjourGroup removeFromParent];
// Post a notification for all listeners that bookmarks have changed
[[NSNotificationCenter defaultCenter] postNotificationName: @"iTermReloadAddressBook" object: nil userInfo: nil];
}
// NSNetService delegate
- (void)netServiceDidResolveAddress:(NSNetService *)sender
{
NSMutableDictionary *aDict;
NSData *address = nil;
struct sockaddr_in *socketAddress;
NSString *ipAddressString = nil;
TreeNode *serviceNode;
//NSLog(@"%s: %@", __PRETTY_FUNCTION__, sender);
// cancel the resolution
[sender stop];
if([bonjourServices containsObject: sender] == NO)
return;
// now that we have at least one resolved service, add the bonjour group to the bookmarks.
if([[bookmarks children] containsObject: bonjourGroup] == NO)
{
[bookmarks insertChild: bonjourGroup atIndex: [bookmarks numberOfChildren]];
}
// grab the address
address = [[sender addresses] objectAtIndex: 0];
socketAddress = (struct sockaddr_in *)[address bytes];
ipAddressString = [NSString stringWithFormat:@"%s", inet_ntoa(socketAddress->sin_addr)];
aDict = [[NSMutableDictionary alloc] init];
serviceNode = [self _getBonjourServiceTypeNode: [sender type]];
[aDict setObject: [NSString stringWithFormat: @"%@", [sender name]] forKey: KEY_NAME];
[aDict setObject: [NSString stringWithFormat: @"%@", [sender name]] forKey: KEY_DESCRIPTION];
[aDict setObject: [NSString stringWithFormat: @"%@ %@",
[[serviceNode nodeData] objectForKey: KEY_BONJOUR_SERVICE], ipAddressString] forKey: KEY_COMMAND];
[aDict setObject: @"" forKey: KEY_WORKING_DIRECTORY];
[aDict setObject: [[iTermTerminalProfileMgr singleInstance] defaultProfileName] forKey: KEY_TERMINAL_PROFILE];
[aDict setObject: [[iTermKeyBindingMgr singleInstance] globalProfileName] forKey: KEY_KEYBOARD_PROFILE];
[aDict setObject: [[iTermDisplayProfileMgr singleInstance] defaultProfileName] forKey: KEY_DISPLAY_PROFILE];
[aDict setObject: ipAddressString forKey: KEY_BONJOUR_SERVICE_ADDRESS];
[[ITAddressBookMgr sharedInstance] addBookmarkWithData: aDict toNode: serviceNode];
// No bonjour service for sftp. Rides over ssh, so try to detect that
if([[[serviceNode nodeData] objectForKey: KEY_BONJOUR_SERVICE] isEqualToString: @"ssh"])
{
serviceNode = [self _getBonjourServiceTypeNode: @"_sftp._tcp."];
[aDict setObject: [NSString stringWithFormat: @"%@", [sender name]] forKey: KEY_NAME];
[aDict setObject: [NSString stringWithFormat: @"%@", [sender name]] forKey: KEY_DESCRIPTION];
[aDict setObject: [NSString stringWithFormat: @"%@ %@",
[[serviceNode nodeData] objectForKey: KEY_BONJOUR_SERVICE], ipAddressString] forKey: KEY_COMMAND];
[aDict setObject: @"" forKey: KEY_WORKING_DIRECTORY];
[aDict setObject: [[iTermTerminalProfileMgr singleInstance] defaultProfileName] forKey: KEY_TERMINAL_PROFILE];
[aDict setObject: [[iTermKeyBindingMgr singleInstance] globalProfileName] forKey: KEY_KEYBOARD_PROFILE];
[aDict setObject: [[iTermDisplayProfileMgr singleInstance] defaultProfileName] forKey: KEY_DISPLAY_PROFILE];
[[ITAddressBookMgr sharedInstance] addBookmarkWithData: aDict toNode: serviceNode];
}
[aDict release];
// remove from array now that resolving is done
if([bonjourServices containsObject: sender])
[bonjourServices removeObject: sender];
// Post a notification for all listeners that bookmarks have changed
[[NSNotificationCenter defaultCenter] postNotificationName: @"iTermReloadAddressBook" object: nil userInfo: nil];
}
- (void)netService:(NSNetService *)aNetService didNotResolve:(NSDictionary *)errorDict
{
//NSLog(@"%s: %@", __PRETTY_FUNCTION__, aNetService);
[aNetService stop];
}
- (void)netServiceWillResolve:(NSNetService *)aNetService
{
//NSLog(@"%s: %@", __PRETTY_FUNCTION__, aNetService);
}
- (void)netServiceDidStop:(NSNetService *)aNetService
{
//NSLog(@"%s: %@", __PRETTY_FUNCTION__, aNetService);
}
@end
@implementation ITAddressBookMgr (Private);
- (BOOL) _checkForDefaultBookmark: (TreeNode *) rootNode defaultBookmark: (TreeNode **) aNode
{
BOOL haveDefaultBookmark = NO;
NSEnumerator *entryEnumerator;
NSDictionary *dataDict;
TreeNode *entry;
entryEnumerator = [[rootNode children] objectEnumerator];
while ((entry = [entryEnumerator nextObject]))
{
if([entry isGroup])
haveDefaultBookmark = [self _checkForDefaultBookmark: entry defaultBookmark: aNode];
else
{
dataDict = [entry nodeData];
if([[dataDict objectForKey: KEY_DEFAULT_BOOKMARK] isEqualToString: @"Yes"])
{
if(aNode)
*aNode = entry;
haveDefaultBookmark = YES;
}
}
if(haveDefaultBookmark)
break;
}
return (haveDefaultBookmark);
}
- (TreeNode *) _getBookmarkNodeWithName: (NSString *) aName searchFromNode: (TreeNode *) aNode
{
NSEnumerator *entryEnumerator;
NSDictionary *dataDict;
TreeNode *entry, *theNode;
dataDict = nil;
entryEnumerator = [[aNode children] objectEnumerator];
while ((entry = [entryEnumerator nextObject]))
{
dataDict = [entry nodeData];
if([[dataDict objectForKey: KEY_NAME] isEqualToString: aName])
{
return (entry);
}
else if([entry isGroup])
{
theNode = [self _getBookmarkNodeWithName: aName searchFromNode: entry];
if(theNode != nil)
return (theNode);
}
}
return (nil);
}
- (TreeNode *) _getBonjourServiceTypeNode: (NSString *) aType
{
NSEnumerator *keyEnumerator;
BOOL aBool;
TreeNode *childNode;
NSRange aRange;
NSString *serviceType = aType;
NSMutableDictionary *aDict;
if([aType length] <= 0)
return (nil);
aRange = [serviceType rangeOfString: @"."];
if(aRange.location != NSNotFound)
{
serviceType = [serviceType substringWithRange: NSMakeRange(1, aRange.location - 1)];
}
aBool = NO;
keyEnumerator = [[bonjourGroup children] objectEnumerator];
while ((childNode = [keyEnumerator nextObject]))
{
if([[[childNode nodeData] objectForKey: KEY_NAME] isEqualToString: serviceType])
{
aBool = YES;
break;
}
}
if(aBool == NO)
{
aDict = [[NSMutableDictionary alloc] init];
[aDict setObject: serviceType forKey: KEY_NAME];
[aDict setObject: @"" forKey: KEY_DESCRIPTION];
[aDict setObject: serviceType forKey: KEY_BONJOUR_SERVICE];
childNode = [[TreeNode alloc] initWithData: aDict parent: nil children: [NSArray array]];
[childNode setIsLeaf: NO];
[aDict release];
[bonjourGroup insertChild: childNode atIndex: [bonjourGroup numberOfChildren]];
[childNode release];
}
return (childNode);
}
@end