-
Notifications
You must be signed in to change notification settings - Fork 3
/
RosterController.m
856 lines (808 loc) · 27.5 KB
/
RosterController.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
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
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
//
// RosterController.m
// Jabber
//
// Created by David Chisnall on Mon Apr 26 2004.
// Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//
#import <EtoileFoundation/EtoileFoundation.h>
#import <XMPPKit/XMPPRosterGroup.h>
#import <XMPPKit/XMPPPerson.h>
#import <XMPPKit/XMPPIdentity.h>
#import <XMPPKit/XMPPConversation.h>
#import "RosterController.h"
#import "JabberApp.h"
#import "CustomPresenceWindowController.h"
#import "TRUserDefaults.h"
#import "MessageWindowController.h"
#define AUTO_RESIZE
#ifdef NO_ATTRIBUTED_TITLES
#define setAttributedTitle(x) setTitle:[x string]
#else
#define setAttributedTitle(x) setAttributedTitle:x
#endif
//Don't animate the window on GNUstep; it breaks (Seems to work now. Delete
//this if it doesn't break)
#ifdef GNUSTEP
#define ANIMATE_WINDOW YES
#else
#define ANIMATE_WINDOW YES
#endif
#ifdef AUTO_RESIZE
#define RESIZE_ROSTER [[self window] setFrame:[self optimalSize] display:YES animate:ANIMATE_WINDOW]
#else
#define RESIZE_ROSTER
#endif
#ifdef GNUSTEP
/* Ugly hack to fix a GNUstep bug */
/*@implementation NSOutlineView (UglyHack)
- (id)itemAtRow: (int)row
{
if (row >= [_items count])
{
return [NSNull null];
}
return [_items objectAtIndex: row];
}
@end*/
#endif
NSMutableArray * rosterControllers = nil;
@implementation RosterController
+ (id) alloc
{
if(rosterControllers == nil)
{
rosterControllers = [[NSMutableArray alloc] init];
}
id rosterController = [super alloc];
if(rosterController != nil)
{
[rosterControllers addObject:rosterController];
}
return rosterController;
}
- (void)windowDidLoad
{
[super windowDidLoad];
[view registerForDraggedTypes:A(@"XMPPPerson",@"XMPPIdentity")];
#ifdef GNUSTEP
[view setHeaderView: nil];
[view setCornerView: nil];
avatarColumn= [[view tableColumns] objectAtIndex:0];
column = [[view tableColumns] objectAtIndex:1];
NSLog(@"Loaded roster window");
#endif
}
- (void) redraw:(NSNotification*)_notification
{
NSString * title;
NSMutableAttributedString * colouredTitle;
title = [[[presenceBox menu] itemAtIndex:0] title];
NSDictionary * colour = [NSDictionary dictionaryWithObject:[[NSUserDefaults standardUserDefaults]
colourForPresence:PRESENCE_CHAT]
forKey:NSForegroundColorAttributeName];
colouredTitle = [[NSMutableAttributedString alloc] initWithString:title
attributes:colour];
[[[presenceBox menu] itemAtIndex:0] setAttributedTitle(colouredTitle)];
title = [[[presenceBox menu] itemAtIndex:1] title];
colour = [NSDictionary dictionaryWithObject:[[NSUserDefaults standardUserDefaults]
colourForPresence:PRESENCE_ONLINE]
forKey:NSForegroundColorAttributeName];
colouredTitle = [[NSMutableAttributedString alloc] initWithString:title
attributes:colour];
[[[presenceBox menu] itemAtIndex:1] setAttributedTitle(colouredTitle)];
title = [[[presenceBox menu] itemAtIndex:2] title];
colour = [NSDictionary dictionaryWithObject:[[NSUserDefaults standardUserDefaults]
colourForPresence:PRESENCE_AWAY]
forKey:NSForegroundColorAttributeName];
colouredTitle = [[NSMutableAttributedString alloc] initWithString:title
attributes:colour];
[[[presenceBox menu] itemAtIndex:2] setAttributedTitle(colouredTitle)];
title = [[[presenceBox menu] itemAtIndex:3] title];
colour = [NSDictionary dictionaryWithObject:[[NSUserDefaults standardUserDefaults]
colourForPresence:PRESENCE_XA]
forKey:NSForegroundColorAttributeName];
colouredTitle = [[NSMutableAttributedString alloc] initWithString:title
attributes:colour];
[[[presenceBox menu] itemAtIndex:3] setAttributedTitle(colouredTitle)];
title = [[[presenceBox menu] itemAtIndex:4] title];
colour = [NSDictionary dictionaryWithObject:[[NSUserDefaults standardUserDefaults]
colourForPresence:PRESENCE_DND]
forKey:NSForegroundColorAttributeName];
colouredTitle = [[NSMutableAttributedString alloc] initWithString:title
attributes:colour];
[[[presenceBox menu] itemAtIndex:4] setAttributedTitle(colouredTitle)];
title = [[[presenceBox menu] itemAtIndex:6] title];
colour = [NSDictionary dictionaryWithObject:[[NSUserDefaults standardUserDefaults]
colourForPresence:PRESENCE_OFFLINE]
forKey:NSForegroundColorAttributeName];
colouredTitle = [[NSMutableAttributedString alloc] initWithString:title
attributes:colour];
[[[presenceBox menu] itemAtIndex:6] setAttributedTitle(colouredTitle)];
}
- (NSAttributedString*) displayStringForObject:(id)anObject
{
NSMutableDictionary * attributes = [[NSMutableDictionary alloc] init];
NSString * plainText;
if([anObject isKindOfClass:[XMPPRosterGroup class]])
{
plainText = [anObject groupName];
}
else if([anObject isKindOfClass:[XMPPPerson class]])
{
unsigned char onlineState = [[[anObject defaultIdentity] presence] show];
NSColor * foreground = [[NSUserDefaults standardUserDefaults] colourForPresence:onlineState];
if(foreground != nil)
{
[attributes setValue:foreground
forKey:NSForegroundColorAttributeName];
}
plainText = [NSString stringWithFormat:@"%C %@",
(unichar)PRESENCE_ICONS[(onlineState / 10) - 1], [(XMPPPerson*)anObject name]];
}
else if([anObject isKindOfClass:[XMPPIdentity class]])
{
NSColor * foreground = [[NSUserDefaults standardUserDefaults] colourForPresence:[[anObject presence] show]];
if(foreground != nil)
{
[attributes setValue:foreground
forKey:NSForegroundColorAttributeName];
}
plainText = [[anObject jid] jidString];
}
else
{
plainText = @"Wrgon!";
}
NSAttributedString * text = [[NSAttributedString alloc] initWithString:plainText attributes:attributes];
return text;
}
- (float) widthOfItemAndChildren:(id) anObject withIndent:(float)anIndent
{
NSAttributedString * attributedText= [self displayStringForObject:anObject];
float myWidth = [attributedText size].width;
if([view isItemExpanded:anObject])
{
for(int i=0 ; i<[self outlineView:view numberOfChildrenOfItem:anObject] ; i++)
{
float width = [self widthOfItemAndChildren:[self outlineView:view
child:i
ofItem:anObject]
withIndent:anIndent] + anIndent;
if(width > myWidth)
{
myWidth = width;
}
}
}
return myWidth;
}
- (int) rowsUnder:(id)anObject
{
int rows = 0;
if([view isItemExpanded:anObject])
{
rows = [self outlineView:view numberOfChildrenOfItem:anObject];
for(int i=0 ; i<[self outlineView:view numberOfChildrenOfItem:anObject] ; i++)
{
rows += [self rowsUnder:[self outlineView:view child:i ofItem:anObject]];
}
}
return rows;
}
- (NSSize) calculateRosterSize;
{
NSSize size;
//Calculate width
float interCellHorizontalSpacing = [view intercellSpacing].width;
// float indent = [view indentationPerLevel] + (interCellHorizontalSpacing);
size.width = [self widthOfItemAndChildren:nil withIndent:0.0f];
size.width += interCellHorizontalSpacing;
//Hack to ensure text doesn't get truncated.
[column setWidth:size.width + 200];
size.width += interCellHorizontalSpacing + 70.0f;
//Calculate height
#ifdef GNUSTEP
//numberOfRows doesn't seem to work correctly on GNUstep.
size.height = [self rowsUnder:nil] * ([view rowHeight] + [view intercellSpacing].height);
#else
size.height = [view numberOfRows] * ([view rowHeight] + [view intercellSpacing].height);
#endif
return size;
}
- (void) subscriptionChanged:(NSNotification*)_notification
{
if([[_notification name] isEqualToString:@"TRXMPPSubscriptionRequest"])
{
NSLog(@"Subscription request received");
//TODO: Ask permission first
[roster authorise:[(XMPPPresence*)[_notification object] jid]];
}
}
- (void) presenceChanged:(NSNotification *)notification
{
NSDictionary * dict = [notification userInfo];
[self setPresence:[[dict objectForKey:@"show"] unsignedCharValue]
withMessage:[dict objectForKey:@"status"]];
}
- (id) initWithNibName:(NSString*)_nib forAccount:(id)_account withRoster:(id)_roster
{
NSLog(@"Loading roster nib...");
self = [self initWithWindowNibName:_nib];
if(self == nil)
{
return nil;
}
NSNotificationCenter * defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:@selector(updatePresence:)
name:@"TRXMPPPresenceChanged"
object:nil];
[defaultCenter addObserver:self
selector:@selector(updateIdentities:)
name:@"TRXMPPIdentityPresenceChanged"
object:nil];
[defaultCenter addObserver:self
selector:@selector(redraw:)
name:@"NSUserDefaultsDidChangeNotification"
object:[NSUserDefaults standardUserDefaults]];
[defaultCenter addObserver:self
selector:@selector(subscriptionChanged:)
name:@"TRXMPPSubscriptionRequest"
object:nil];
[defaultCenter addObserver:self
selector:@selector(subscriptionChanged:)
name:@"TRXMPPSubscription"
object:nil];
[defaultCenter addObserver:self
selector:@selector(subscriptionChanged:)
name:@"TRXMPPUnsubscriptionRequest"
object:nil];
[defaultCenter addObserver:self
selector:@selector(subscriptionChanged:)
name:@"TRXMPPUnubscription"
object:nil];
[defaultCenter addObserver:self
selector:@selector(newConversation:)
name:@"XMPPNewConversationStartedNotification"
object:nil];
[[self window] setFrameFromString:@"Jabber Roster"];
[[self window] setFrameAutosaveName:@"Jabber Roster"];
roster = _roster;
account = _account;
//Note: nil must be changed to account if permitting multiple accounts
NSNotificationCenter * localCenter = [NSNotificationCenter defaultCenter];
[localCenter addObserver:self
selector:@selector(presenceChanged:)
name:@"LocalPresenceChangedNotification"
object:nil];
return self;
}
- (void) updateIdentities:(NSNotification*)aNotification
{
//TODO: It might be possible to optimise this,
//but the overhead of testing might be self-defeating
[self update:nil];
}
- (void) updatePresence:(NSNotification*)_notification
{
unsigned char old = (unsigned char)[[[_notification userInfo] objectForKey:@"OldPresence"] intValue];
unsigned char new = (unsigned char)[[[_notification userInfo] objectForKey:@"NewPresence"] intValue];
if(old >= PRESENCE_OFFLINE && new < PRESENCE_OFFLINE)
{
[[[NSUserDefaults standardUserDefaults] soundForKey:@"OnlineSound"] play];
}
else if(old < PRESENCE_OFFLINE && new >= PRESENCE_OFFLINE)
{
[[[NSUserDefaults standardUserDefaults] soundForKey:@"OfflineSound"] play];
}
id changedObject = [_notification object];
if([changedObject isKindOfClass:[XMPPPerson class]])
{
unsigned int hiddenPresence = [[NSUserDefaults standardUserDefaults] presenceForKey:@"HiddenPresences"];
XMPPRosterGroup * group = [[account roster] groupNamed:[changedObject group]];
//If we making a group appear
if(old >= hiddenPresence &&
[group numberOfPeopleInGroupMoreOnlineThan:hiddenPresence])
{
[self update:nil];
}
[self update:group];
}
[self update:[_notification object]];
}
- (NSRect)optimalSize
{
NSRect windowFrameDimensions = [[self window] frame];
float oldHeight = windowFrameDimensions.size.height;
NSSize rosterSize = [self calculateRosterSize];
windowFrameDimensions.size.height = rosterSize.height + (windowFrameDimensions.size.height - [[view superview] frame].size.height);
windowFrameDimensions.size.width = rosterSize.width;
NSSize minimumSize = [[self window] minSize];
if(windowFrameDimensions.size.height < minimumSize.height)
{
windowFrameDimensions.size.height = minimumSize.height;
}
if(windowFrameDimensions.size.width < minimumSize.width)
{
windowFrameDimensions.size.width = minimumSize.width;
}
windowFrameDimensions.origin.y -= windowFrameDimensions.size.height - oldHeight;
if(windowFrameDimensions.origin.y < 0)
{
windowFrameDimensions.origin.y = 0;
}
if(windowFrameDimensions.size.height > [[[self window] screen] visibleFrame].size.height)
{
windowFrameDimensions.size.height = [[[self window] screen] visibleFrame].size.height;
}
#ifdef GNUSTEP
//GNUstep doesn't respect minimum window size for some reason
if(windowFrameDimensions.size.width < 137)
{
windowFrameDimensions.size.width = 137;
}
#endif
return windowFrameDimensions;
}
- (void) update:(id)_object
{
#ifdef GNUSTEP
/* GNUSTEP BUG:
* [NSOutlineView -reloadItem] is badly broken on GNUstep.
* Remove this work-around when it is fixed.
*/
[view reloadData];
#else
if(_object == nil)
{
[view reloadData];
}
else
{
[view reloadItem:_object reloadChildren:YES];
//TODO: Work out why I thought this crashed, and had it all commented out.
if([view isItemExpanded:_object])
{
[view reloadItem:_object reloadChildren:YES];
}
}
#endif
if([_object isKindOfClass:[XMPPRosterGroup class]] &&
[[NSUserDefaults standardUserDefaults] expandedGroup:[_object groupName]])
{
if([view isExpandable:_object])
{
[view expandItem:_object];
}
}
/* These exception handlers were a work around for a now-fixed bug.
* They can probably be removed.
*/
NS_DURING
RESIZE_ROSTER;
[view display];
NS_HANDLER
NSLog(@"Exception while displaying roster: %@", [localException reason]);
NS_ENDHANDLER
}
- (NSRect)windowWillUseStandardFrame:(NSWindow *)sender defaultFrame:(NSRect)defaultFrame
{
return [self optimalSize];
}
- (id) outlineView:(NSOutlineView *)_outlineView child:(NSInteger)_index ofItem:(id)_item
{
if(_item == nil)
{
return [roster groupForIndex:_index ignoringPeopleLessOnlineThan:[[NSUserDefaults standardUserDefaults] presenceForKey:@"HiddenPresences"]];
}
if([_item isKindOfClass:[XMPPRosterGroup class]])
{
return [_item personAtIndex:_index];
}
if([_item isKindOfClass:[XMPPPerson class]])
{
NSArray *list = [_item identityList];
if ((_index > -1) && (_index < (int)[list count]))
return [list objectAtIndex:_index];
}
return nil;
}
- (NSString *)outlineView:(NSOutlineView *)ov
toolTipForCell:(NSCell *)cell
rect:(NSRectPointer)rect
tableColumn:(NSTableColumn *)tc
item:(id)item
mouseLocation:(NSPoint)mouseLocation
{
if([item isKindOfClass:[XMPPRosterGroup class]])
{
return [NSString stringWithFormat:@"%d/%d %@",
[item numberOfPeopleInGroupMoreOnlineThan:PRESENCE_OFFLINE],
[item numberOfPeopleInGroupMoreOnlineThan:PRESENCE_UNKNOWN + 10],
[XMPPPresence displayStringForPresence:PRESENCE_ONLINE]];
}
NSString * toolTipMessage = nil;
XMPPPresence * selectedPresence = nil;
if([item isKindOfClass:[XMPPPerson class]])
{
selectedPresence = [[(XMPPPerson*)item defaultIdentity] presence];
}
else if([item isKindOfClass:[XMPPIdentity class]])
{
selectedPresence = [(XMPPIdentity*)item presence];
}
if(selectedPresence != nil)
{
toolTipMessage = [selectedPresence status];
if(toolTipMessage == nil)
{
toolTipMessage = [XMPPPresence displayStringForPresence:[selectedPresence show]];
}
}
return toolTipMessage;
}
- (BOOL) outlineView:(NSOutlineView *)_outlineView isItemExpandable:(id)_item
{
if(roster == nil)
{
return NO;
}
if([_item isKindOfClass:[XMPPRosterGroup class]])
{
if([_item numberOfPeopleInGroupMoreOnlineThan:[[NSUserDefaults standardUserDefaults] presenceForKey:@"HiddenPresences"]] > 0)
{
return YES;
}
else
{
return NO;
}
}
if(_item == nil)
{
return YES;
}
if([_item isKindOfClass:[XMPPPerson class]])
{
if([_item identities] > 1)
{
return YES;
}
}
return NO;
}
- (void) confirmDeleteDidEnd:(NSAlert *)alert returnCode:(int)returnCode contact:(id)contact
{
if(returnCode == NSAlertFirstButtonReturn)
{
if([contact isKindOfClass:[XMPPPerson class]])
{
FOREACH([contact identityList], identity, XMPPIdentity*)
{
NSLog(@"Deleting %@", [[identity jid] jidString]);
[roster unsubscribe:[identity jid]];
}
}
else if([contact isKindOfClass:[XMPPIdentity class]])
{
NSLog(@"Deleting %@", [[contact jid] jidString]);
[roster unsubscribe:[contact jid]];
}
}
}
- (IBAction) remove:(id)sender
{
id item = [view itemAtRow:[view selectedRow]];
if([item isKindOfClass:[XMPPPerson class]])
{
NSAlert * alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"Delete Contact"];
[alert addButtonWithTitle:@"Cancel"];
[alert setMessageText:[NSString stringWithFormat:@"Are you sure you want to delete %@?", [item name]]];
[alert setInformativeText:@"All identities for this contact will be deleted."];
[alert setAlertStyle:NSWarningAlertStyle];
[alert beginSheetModalForWindow:[self window]
modalDelegate:self
didEndSelector:@selector(confirmDeleteDidEnd:returnCode:contact:)
contextInfo:(__bridge void*)item];
}
else if([item isKindOfClass:[XMPPIdentity class]])
{
NSAlert * alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"Delete Contact"];
[alert addButtonWithTitle:@"Cancel"];
[alert setMessageText:[NSString stringWithFormat:@"Are you sure you want to delete %@?", [[item jid] jidString]]];
[alert setInformativeText:@"Other identities belonging to this contact will be unaffected."];
[alert setAlertStyle:NSWarningAlertStyle];
[alert beginSheetModalForWindow:[self window]
modalDelegate:self
didEndSelector:@selector(confirmDeleteDidEnd:returnCode:contact:)
contextInfo:(__bridge void*)item];
}
}
- (NSInteger)outlineView:(NSOutlineView *)_outlineView numberOfChildrenOfItem:(id)_item
{
//Root node. Children are all groups
if(_item == nil)
{
return [roster numberOfGroupsContainingPeopleMoreOnlineThan:[[NSUserDefaults standardUserDefaults] presenceForKey:@"HiddenPresences"]];
}
if([_item isKindOfClass:[XMPPRosterGroup class]])
{
return [_item numberOfPeopleInGroupMoreOnlineThan:[[NSUserDefaults standardUserDefaults] presenceForKey:@"HiddenPresences"]];
}
if([_item isKindOfClass:[XMPPPerson class]])
{
return [_item identities];
}
return 0;
}
- (id)outlineView:(NSOutlineView *)_outlineView objectValueForTableColumn:(NSTableColumn *)_tableColumn byItem:(id)_item
{
if(_tableColumn == column)
{
return [self displayStringForObject:_item];
}
else
{
if([_item isKindOfClass:[XMPPPerson class]])
{
return [(XMPPPerson*)_item avatar];
}
}
return nil;
}
- (void)outlineViewItemDidExpand:(NSNotification *)notification
{
id group = [[notification userInfo] objectForKey:@"NSObject"];
if([group isKindOfClass:[XMPPRosterGroup class]])
{
[[NSUserDefaults standardUserDefaults] setExpanded:[group groupName] to:YES];
}
RESIZE_ROSTER;
}
- (void)outlineViewItemDidCollapse:(NSNotification *)notification
{
id group = [[notification userInfo] objectForKey:@"NSObject"];
if([group isKindOfClass:[XMPPRosterGroup class]])
{
[[NSUserDefaults standardUserDefaults] setExpanded:[group groupName] to:NO];
}
RESIZE_ROSTER;
}
inline static XMPPConversation * createChatWithPerson(id self, XMPPPerson* person, XMPPAccount * account)
{
JID * destinationJID = [[person defaultIdentity] jid];
XMPPConversation * conversation = [XMPPConversation conversationForPerson:person];
if(conversation != nil)
{
[conversation setJID:destinationJID];
[[conversation delegate] activate:self];
}
else
{
conversation = [XMPPConversation conversationWithPerson:person
forAccount:account];
MessageWindowController * chatWindow = [[MessageWindowController alloc] initWithWindowNibName:@"MessageWindow"];
[chatWindow conversation:conversation];
[conversation setDelegate:chatWindow];
}
return conversation;
}
- (IBAction) click:(id)sender
{
id item = [view itemAtRow:[view clickedRow]];
if([item isKindOfClass:[XMPPPerson class]])
{
createChatWithPerson(self,(XMPPPerson*) item, account);
}
else if([item isKindOfClass:[XMPPIdentity class]])
{
JID * destinationJID = [(XMPPIdentity*)item jid];
[createChatWithPerson(self, [roster personForJID:destinationJID], account) setJID:destinationJID];
}
}
- (IBAction) changePresence:(id)sender
{
NSString * status = [statusBox stringValue];
switch([presenceBox indexOfSelectedItem])
{
case 0:
[(JabberApp*)[NSApp delegate] setPresence:PRESENCE_CHAT withMessage:status];
break;
case 1:
[(JabberApp*)[NSApp delegate] setPresence:PRESENCE_ONLINE withMessage:status];
break;
case 2:
[(JabberApp*)[NSApp delegate] setPresence:PRESENCE_AWAY withMessage:status];
break;
case 3:
[(JabberApp*)[NSApp delegate] setPresence:PRESENCE_XA withMessage:status];
break;
case 4:
[(JabberApp*)[NSApp delegate] setPresence:PRESENCE_DND withMessage:status];
break;
case 6:
[(JabberApp*)[NSApp delegate] setPresence:PRESENCE_OFFLINE withMessage:status];
break;
case 8:
[(JabberApp*)[NSApp delegate] setCustomPresence:status];
break;
}
}
- (void) setPresence:(unsigned char)_status withMessage:(NSString*)_message
{
if(_message == nil)
{
_message = [statusBox stringValue];
}
if(presence != _status)
{
presence = _status;
[presenceBox selectItemWithTitle:[XMPPPresence displayStringForPresence:_status]];
}
if(_message == nil)
{
_message = @"";
}
if(![[statusBox stringValue] isEqualToString:_message])
{
[statusBox setStringValue:_message];
}
[statusBox setTextColor:PRESENCE_COLOUR(_status)];
}
/*- (void) update
{
if([[self window] isZoomed])
{
[[self window] setFrame:[self windowWillUseStandardFrame:[self window] defaultFrame:NSMakeRect(0,0,0,0)] display:YES];
}
}*/
- (NSString*) currentStatusMessage
{
return [statusBox stringValue];
}
//Drag and drop operations
//Drag
- (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
{
id item = [items objectAtIndex:0];
if([item isKindOfClass:[XMPPPerson class]])
{
[pboard declareTypes:[NSArray arrayWithObject:@"XMPPPerson"] owner:self];
[pboard setPropertyList:D([item name], @"name", [item group], @"group")
forType:@"XMPPPerson"];
return YES;
}
if([item isKindOfClass:[XMPPPerson class]])
{
[pboard declareTypes:[NSArray arrayWithObject:@"XMPPIdentity"] owner:self];
[pboard setString:[[item jid] jidString] forType:@"XMPPIdentity"];
return YES;
}
return NO;
}
//Can drop?
- (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id < NSDraggingInfo >)info proposedItem:(id)item proposedChildIndex:(NSInteger)anIndex
{
NSPasteboard * pboard = [info draggingPasteboard];
if([item isKindOfClass:[XMPPPerson class]])
{
if([pboard availableTypeFromArray:A(@"XMPPPerson", @"XMPPIdentity")])
{
return NSDragOperationMove;
}
}
if([item isKindOfClass:[XMPPRosterGroup class]])
{
if([pboard availableTypeFromArray:A(@"XMPPPerson")])
{
return NSDragOperationMove;
}
}
return NSDragOperationNone;
}
//Drop
- (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id < NSDraggingInfo >)info item:(id)item childIndex:(NSInteger)anIndex
{
NSPasteboard * pboard = [info draggingPasteboard];
[pboard types];
BOOL dropped = NO;
if([item isKindOfClass:[XMPPPerson class]])
{
id object = nil;
NSString * newGroup = [item group];
NSString * newName = [item name];
if((object = [pboard propertyListForType:@"XMPPPerson"]) != nil)
{
NSString * groupName = [object objectForKey:@"group"];
NSString * personName = [object objectForKey:@"name"];
//Make sure it's not dealloc'd when we remove the last identity from it
XMPPPerson * person = [[roster groupNamed:groupName] personNamed:personName];
NSArray * identities = [person identityList];
FOREACH(identities, identity, XMPPIdentity*)
{
[roster setName:newName group:newGroup forIdentity:identity];
}
dropped = YES;
}
else if((object = [pboard propertyListForType:@"XMPPIdentity"]) != nil)
{
JID * jid = [JID jidWithString:object];
XMPPIdentity * identity = [[roster personForJID:jid] identityForJID:jid];
//Make sure it's not dealloc'd when we remove the last identity from it
[roster setName:newName group:newGroup forIdentity:identity];
dropped = YES;
}
}
else if([item isKindOfClass:[XMPPRosterGroup class]])
{
id object = nil;
if((object = [pboard propertyListForType:@"XMPPPerson"]) != nil)
{
NSString * groupName = [object objectForKey:@"group"];
NSString * personName = [object objectForKey:@"name"];
//Make sure it's not dealloc'd when we remove the last identity from it
XMPPPerson * person = [[roster groupNamed:groupName] personNamed:personName];
NSArray * identities = [person identityList];
NSString * newGroup = [item groupName];
FOREACH(identities, identity, XMPPIdentity*)
{
[roster setGroup:newGroup forIdentity:identity];
}
dropped = YES;
}
}
if(dropped)
{
[self update:nil];
}
return dropped;
}
- (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
if([item isKindOfClass:[XMPPPerson class]])
{
return YES;
}
return NO;
}
- (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
if([item isKindOfClass:[XMPPPerson class]])
{
NSString * group = [item group];
if([object length] > 2)
{
int p = ([[[item defaultIdentity] presence] show] / 10) - 1;
NSString * prefix = [NSString stringWithFormat:@"%C ",
(unichar)PRESENCE_ICONS[p]];
if([prefix isEqualToString:[object substringToIndex:2]])
{
object = [object substringFromIndex:2];
}
}
if(![object isEqualToString:[item name]])
{
NSArray * identities = [item identityList];
FOREACH(identities, identity, XMPPIdentity*)
{
[roster setName:object group:group forIdentity:identity];
}
}
}
}
- (void) newConversation:(NSNotification*)_notification
{
XMPPConversation * conversation = [[_notification userInfo] valueForKey:@"XMPPConversation"];
id chatWindow = [[MessageWindowController alloc] initWithWindowNibName:@"MessageWindow"];
[chatWindow conversation:conversation];
[conversation setDelegate:chatWindow];
}
- (void) dealloc
{
[rosterControllers removeObject:self];
[[self window] close];
}
@end