]> cgit.babelmonkeys.de Git - jubjub.git/blob - src/core/JubChatClient.m
JubGtkChat: Refer to the correct version of the close and send blocks
[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
31                 _streamManagement = [[XMPPStreamManagement alloc]
32                     initWithConnection: _connection];
33
34                 [_connection asyncConnectAndHandle];
35         } @catch (id e) {
36                 [self release];
37                 @throw e;
38         }
39
40         return self;
41 }
42
43 - (void)dealloc
44 {
45         [_roster release];
46         [_contactManager release];
47         [_streamManagement release];
48         [_connection release];
49         [_presence release];
50         [_chatMap release];
51
52         [super dealloc];
53 }
54
55 - (id<JubChatUI>)chatForContact: (XMPPContact*)contact
56 {
57         OFAutoreleasePool *pool = [OFAutoreleasePool new];
58         OFString *bareJID = [contact.rosterItem.JID bareJID];
59
60         id<JubChatUI> chat = [_chatMap objectForKey: bareJID];
61         if (chat == nil) {
62                 OFString * title =
63                     [@"Chat with " stringByAppendingString: bareJID];
64
65                 chat = [[[[_ui chatUIClass] alloc]
66                     initWithTitle: title
67                        closeBlock: ^{
68                                 [_chatMap removeObjectForKey: bareJID];
69                         }
70                         sendBlock: ^(OFString *text) {
71                                 XMPPMessage *msg =
72                                     [XMPPMessage messageWithType: @"chat"];
73                                 msg.body = text;
74                                 [contact sendMessage: msg
75                                           connection: _connection];
76                         }
77                 ] autorelease];
78
79                 [_chatMap setObject: chat
80                              forKey: bareJID];
81         }
82
83         [pool release];
84
85         return chat;
86 }
87
88 - (void)connection: (XMPPConnection*)connection
89      wasBoundToJID: (XMPPJID*)jid
90 {
91         of_log(@"Bound to JID: %@", [jid fullJID]);
92
93         [_roster requestRoster];
94 }
95
96 -   (void)connection: (XMPPConnection*)connection
97   didReceivePresence: (XMPPPresence*)presence
98 {
99         if ([presence.from isEqual: connection.JID]) {
100                 [_ui         client: self
101                   didChangePresence: presence];
102                 OF_SETTER(_presence, presence, YES, 0);
103         }
104 }
105
106 - (void)rosterWasReceived: (XMPPRoster*)roster
107 {
108         XMPPPresence *pres = [XMPPPresence presence];
109         [pres setStatus: @"Hello from JubJub"];
110         [_connection sendStanza: pres];
111 }
112 @end