-
Notifications
You must be signed in to change notification settings - Fork 3
/
PresenceLogController.m
92 lines (85 loc) · 2.88 KB
/
PresenceLogController.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
//
// PresenceLogController.m
// Jabber
//
// Created by David Chisnall on 22/05/2007.
// Copyright 2007 __MyCompanyName__. All rights reserved.
//
#import <XMPPKit/XMPPPresence.h>
#import "PresenceLogController.h"
#import "TRUserDefaults.h"
#import "NSTextView+ClickableLinks.h"
#define GET_METHOD(type, x) - (type) x { return x; }
#define COMPARE_METHOD(ivar, ivarName) - (NSComparisonResult) compareBy ## ivarName:(id)other { return [ivar compare:[other ivar]]; }
@interface PresenceLogEntry : NSObject {
NSDate * date;
NSString * user;
NSString * statusMessage;
}
@end
@implementation PresenceLogEntry
GET_METHOD(NSDate*, date)
GET_METHOD(NSString*,user)
GET_METHOD(NSString*,statusMessage)
COMPARE_METHOD(date,Date)
COMPARE_METHOD(user,User)
COMPARE_METHOD(statusMessage,Status)
@end
@implementation PresenceLogController
- (void) awakeFromNib
{
NSNotificationCenter * localCenter = [NSNotificationCenter defaultCenter];
lastStatus = [[NSMutableDictionary alloc] init];
[localCenter addObserver:self
selector:@selector(newPresence:)
name:@"TRXMPPPresenceChanged"
object:nil];
[localCenter addObserver:self
selector:@selector(presenceChanged:)
name:@"LocalPresenceChangedNotification"
object:nil];
}
- (void) presenceChanged:(NSNotification *)notification
{
NSDictionary * dict = [notification userInfo];
NSString * status = [dict objectForKey:@"status"];
if(status != myStatus)
{
myStatus = status;
}
}
- (void) newPresence:(NSNotification *)notification
{
NSString * name = [[notification object] name];
NSDictionary * dict = [notification userInfo];
NSString * oldMessage = [lastStatus objectForKey:name];
NSString * newMessage = [dict objectForKey:@"NewStatus"];
/* If the pressence has changed, and is not an echo */
if(newMessage != nil
&&
![oldMessage isEqualToString:newMessage]
&&
![newMessage isEqualToString:@""]
&&
![newMessage isEqualToString:myStatus]
&&
![newMessage isEqualToString:[XMPPPresence displayStringForPresence:
[[dict objectForKey:@"NewPresence"] unsignedCharValue]]]
)
{
[lastStatus setObject:newMessage forKey:name];
NSString * date = [[NSDate date] descriptionWithCalendarFormat:@"%H:%M"
timeZone:nil
locale:[[NSUserDefaults standardUserDefaults]
dictionaryRepresentation]];
NSAttributedString * headline = [[NSAttributedString alloc]
initWithString:[NSString stringWithFormat:@"%@ - %@:\n", date, name]
attributes:PRESENCE_COLOUR_DICTIONARY(([[dict objectForKey:@"NewPresence"] unsignedCharValue]))];
NSString * emoString = [NSString stringWithFormat:@"\t%@\n", newMessage];
NSAttributedString * emoText = [[NSAttributedString alloc] initWithString:emoString];
[[view textStorage] insertAttributedString:emoText atIndex:0];
[[view textStorage] insertAttributedString:headline atIndex:0];
[view makeLinksClickable];
}
}
@end