]> cgit.babelmonkeys.de Git - jubjub.git/blob - src/core/JubChatClient.m
Move adding messages to the ChatUI to JubChatClient
[jubjub.git] / src / core / JubChatClient.m
1 #import "JubChatClient.h"
2
3 @implementation JubChatClient
4 @synthesize connection = _connection;
5 @synthesize roster = _roster;
6 @synthesize contactManager = _contactManager;
7 @synthesize presence = _presence;
8 @synthesize ui = _ui;
9
10 - initWithConfig: (JubConfig*)config
11 {
12         self = [super init];
13
14         @try {
15                 _chatMap = [[OFMutableDictionary alloc] init];
16
17                 _connection = [XMPPConnection new];
18                 _connection.username = config.username;
19                 _connection.domain = config.domain;
20                 _connection.server = config.server;
21                 _connection.password = config.password;
22                 [_connection addDelegate: self];
23
24                 _roster = [[XMPPRoster alloc] initWithConnection: _connection];
25                 [_roster addDelegate: self];
26
27                 _contactManager = [[XMPPContactManager alloc]
28                     initWithConnection: _connection
29                                 roster: _roster];
30                 [_contactManager addDelegate: self];
31
32                 _streamManagement = [[XMPPStreamManagement alloc]
33                     initWithConnection: _connection];
34
35                 [_connection asyncConnectAndHandle];
36         } @catch (id e) {
37                 [self release];
38                 @throw e;
39         }
40
41         return self;
42 }
43
44 - (void)dealloc
45 {
46         [_roster removeDelegate: self];
47         [_contactManager removeDelegate: self];
48         [_connection removeDelegate: self];
49
50         [_roster release];
51         [_contactManager release];
52         [_streamManagement release];
53         [_connection release];
54         [_presence release];
55         [_chatMap release];
56
57         [super dealloc];
58 }
59
60 - (id<JubChatUI>)chatForContact: (XMPPContact*)contact
61 {
62         OFAutoreleasePool *pool = [OFAutoreleasePool new];
63         OFString *bareJID = [contact.rosterItem.JID bareJID];
64
65         id<JubChatUI> chat = [_chatMap objectForKey: bareJID];
66         if (chat == nil) {
67                 OFString * title =
68                     [@"Chat with " stringByAppendingString: bareJID];
69
70                 chat = [[[[_ui chatUIClass] alloc]
71                     initWithTitle: title
72                        closeBlock: ^{
73                                 [_chatMap removeObjectForKey: bareJID];
74                         }
75                         sendBlock: ^(OFString *text) {
76                                 XMPPMessage *msg =
77                                     [XMPPMessage messageWithType: @"chat"];
78                                 msg.body = text;
79                                 [contact sendMessage: msg
80                                           connection: _connection];
81                         }
82                 ] autorelease];
83
84                 [_chatMap setObject: chat
85                              forKey: bareJID];
86         }
87
88         [pool release];
89
90         return chat;
91 }
92
93 - (void)connection: (XMPPConnection*)connection
94      wasBoundToJID: (XMPPJID*)jid
95 {
96         of_log(@"Bound to JID: %@", [jid fullJID]);
97
98         [_roster requestRoster];
99 }
100
101 -   (void)connection: (XMPPConnection*)connection
102   didReceivePresence: (XMPPPresence*)presence
103 {
104         if ([presence.from isEqual: connection.JID]) {
105                 [_ui         client: self
106                   didChangePresence: presence];
107                 OF_SETTER(_presence, presence, YES, 0);
108         }
109 }
110
111 -  (void)contact: (XMPPContact*)contact
112   didSendMessage: (XMPPMessage*)message
113 {
114         if (message.body == nil || ![message.type isEqual: @"chat"])
115                 return;
116
117         id<JubChatUI> chat = [self chatForContact: contact];
118         [chat addMessage: message.body
119                   sender: [message.from bareJID]];
120 }
121
122 - (void)rosterWasReceived: (XMPPRoster*)roster
123 {
124         XMPPPresence *pres = [XMPPPresence presence];
125         [pres setStatus: @"Hello from JubJub"];
126         [_connection sendStanza: pres];
127 }
128 @end