forked from tinycode/xmppframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatWindowManager.m
83 lines (65 loc) · 1.92 KB
/
ChatWindowManager.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
#import "ChatWindowManager.h"
#import "ChatController.h"
#import "XMPP.h"
@implementation ChatWindowManager
+ (ChatController *)chatControllerForJID:(XMPPJID *)jid matchResource:(BOOL)matchResource
{
// Loop through all the open windows, and see if any of them are the one we want...
NSArray *windows = [NSApp windows];
int i;
for(i = 0; i < [windows count]; i++)
{
NSWindow *currentWindow = [windows objectAtIndex:i];
ChatController *currentWC = [currentWindow windowController];
if([currentWC isKindOfClass:[ChatController class]])
{
if(matchResource)
{
XMPPJID *currentJID = [currentWC jid];
if([currentJID isEqual:jid])
{
return currentWC;
}
}
else
{
XMPPJID *currentJID = [[currentWC jid] bareJID];
if([currentJID isEqual:[jid bareJID]])
{
return currentWC;
}
}
}
}
return nil;
}
+ (void)openChatWindowWithXMPPClient:(XMPPClient *)client forXMPPUser:(XMPPUser *)user
{
ChatController *cc = [[self class] chatControllerForJID:[user jid] matchResource:NO];
if(cc)
{
[[cc window] makeKeyAndOrderFront:self];
}
else
{
// Create Manual Sync Window
XMPPJID *jid = [[user primaryResource] jid];
ChatController *temp = [[ChatController alloc] initWithXMPPClient:client jid:jid];
[temp showWindow:self];
// Note: MSWController will automatically release itself when the user closes the window
}
}
+ (void)handleChatMessage:(XMPPMessage *)message withXMPPClient:(XMPPClient *)client
{
NSLog(@"ChatWindowManager: handleChatMessage");
ChatController *cc = [[self class] chatControllerForJID:[message from] matchResource:YES];
if(!cc)
{
// Create new chat window
XMPPJID *jid = [message from];
ChatController *newCC = [[ChatController alloc] initWithXMPPClient:client jid:jid message:message];
[newCC showWindow:self];
// Note: ChatController will automatically release itself when the user closes the window.
}
}
@end