forked from tinycode/xmppframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatController.m
153 lines (115 loc) · 3.87 KB
/
ChatController.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
#import "ChatController.h"
#import "XMPP.h"
@implementation ChatController
- (id)initWithXMPPClient:(XMPPClient *)client jid:(XMPPJID *)fullJID
{
return [self initWithXMPPClient:client jid:fullJID message:nil];
}
- (id)initWithXMPPClient:(XMPPClient *)client jid:(XMPPJID *)fullJID message:(XMPPMessage *)message
{
if(self = [super initWithWindowNibName:@"ChatWindow"])
{
xmppClient = [client retain];
jid = [fullJID retain];
firstMessage = [message retain];
}
return self;
}
- (void)awakeFromNib
{
[xmppClient addDelegate:self];
[messageView setString:@""];
[[self window] setTitle:[jid full]];
[[self window] makeFirstResponder:messageField];
if(firstMessage)
{
[self xmppClient:xmppClient didReceiveMessage:firstMessage];
[firstMessage release];
firstMessage = nil;
}
}
/**
* Called immediately before the window closes.
*
* This method's job is to release the WindowController (self)
* This is so that the nib file is released from memory.
**/
- (void)windowWillClose:(NSNotification *)aNotification
{
NSLog(@"ChatController: windowWillClose");
[xmppClient removeDelegate:self];
[self autorelease];
}
- (void)dealloc
{
NSLog(@"Destroying self: %@", self);
[xmppClient release];
[jid release];
[firstMessage release];
[super dealloc];
}
- (XMPPJID *)jid
{
return jid;
}
- (void)scrollToBottom
{
NSScrollView *scrollView = [messageView enclosingScrollView];
NSPoint newScrollOrigin;
if ([[scrollView documentView] isFlipped])
newScrollOrigin = NSMakePoint(0.0, NSMaxY([[scrollView documentView] frame]));
else
newScrollOrigin = NSMakePoint(0.0, 0.0);
[[scrollView documentView] scrollPoint:newScrollOrigin];
}
- (void)xmppClientDidAuthenticate:(XMPPClient *)sender
{
[messageField setEnabled:YES];
}
- (void)xmppClient:(XMPPClient *)sender didReceiveMessage:(XMPPMessage *)message
{
if(![jid isEqual:[message from]]) return;
if([message isChatMessageWithBody])
{
NSString *messageStr = [[message elementForName:@"body"] stringValue];
NSString *paragraph = [NSString stringWithFormat:@"%@\n\n", messageStr];
NSMutableParagraphStyle *mps = [[[NSMutableParagraphStyle alloc] init] autorelease];
[mps setAlignment:NSLeftTextAlignment];
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithCapacity:2];
[attributes setObject:mps forKey:NSParagraphStyleAttributeName];
[attributes setObject:[NSColor colorWithCalibratedRed:250 green:250 blue:250 alpha:1] forKey:NSBackgroundColorAttributeName];
NSAttributedString *as = [[NSAttributedString alloc] initWithString:paragraph attributes:attributes];
[as autorelease];
[[messageView textStorage] appendAttributedString:as];
}
}
- (void)xmppClientDidDisconnect:(XMPPClient *)sender
{
[messageField setEnabled:NO];
}
- (IBAction)sendMessage:(id)sender
{
NSString *messageStr = [messageField stringValue];
if([messageStr length] > 0)
{
NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
[body setStringValue:messageStr];
NSXMLElement *message = [NSXMLElement elementWithName:@"message"];
[message addAttributeWithName:@"type" stringValue:@"chat"];
[message addAttributeWithName:@"to" stringValue:[jid full]];
[message addChild:body];
[xmppClient sendElement:message];
NSString *paragraph = [NSString stringWithFormat:@"%@\n\n", messageStr];
NSMutableParagraphStyle *mps = [[[NSMutableParagraphStyle alloc] init] autorelease];
[mps setAlignment:NSRightTextAlignment];
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithCapacity:2];
[attributes setObject:mps forKey:NSParagraphStyleAttributeName];
NSAttributedString *as = [[NSAttributedString alloc] initWithString:paragraph attributes:attributes];
[as autorelease];
[[messageView textStorage] appendAttributedString:as];
[self scrollToBottom];
[messageField setStringValue:@""];
[[self window] makeFirstResponder:messageField];
}
}
@end