]> cgit.babelmonkeys.de Git - jubjub.git/blobdiff - src/core/JubChatClient.m
Implement connection:didThrowException:
[jubjub.git] / src / core / JubChatClient.m
index 4efa89464e2935ea8e3527526e1a99b97fd8b6fd..3c563f5e9dd1a9f267b9b09ad63787a8be9de1ea 100644 (file)
@@ -3,6 +3,7 @@
 @implementation JubChatClient
 @synthesize connection;
 @synthesize roster;
+@synthesize contactManager;
 @synthesize presence;
 @synthesize ui;
 
@@ -11,8 +12,9 @@
        self = [super init];
 
        @try {
-               connection = [XMPPConnection new];
+               chatMap = [[OFMutableDictionary alloc] init];
 
+               connection = [XMPPConnection new];
                connection.username = config.username;
                connection.domain = config.domain;
                connection.server = config.server;
                roster = [[XMPPRoster alloc] initWithConnection: connection];
                [roster addDelegate: self];
 
+               contactManager = [[XMPPContactManager alloc]
+                                     initWithConnection: connection
+                                                 roster: roster];
+
                streamManagement = [[XMPPStreamManagement alloc]
                    initWithConnection: connection];
 
 - (void)dealloc
 {
        [roster release];
+       [contactManager release];
        [streamManagement release];
        [connection release];
        [presence release];
+       [chatMap release];
 
        [super dealloc];
 }
 
+- (id<JubChatUI>)chatForContact: (XMPPContact*)contact
+{
+       OFAutoreleasePool *pool = [OFAutoreleasePool new];
+       OFString *bareJID = [contact.rosterItem.JID bareJID];
+
+       id<JubChatUI> chat = [chatMap objectForKey: bareJID];
+       if (chat == nil) {
+               OFString * title =
+                   [@"Chat with " stringByAppendingString: bareJID];
+
+               chat = [[[[ui chatUIClass] alloc]
+                   initWithTitle: title
+                      closeBlock: ^{
+                               [chatMap removeObjectForKey: bareJID];
+                       }
+                       sendBlock: ^(OFString *text) {
+                               XMPPMessage *msg =
+                                   [XMPPMessage messageWithType: @"chat"];
+                               msg.body = text;
+                               [contact sendMessage: msg
+                                         connection: connection];
+                       }
+               ] autorelease];
+
+               [chatMap setObject: chat
+                           forKey: bareJID];
+       }
+
+       [pool release];
+
+       return chat;
+}
+
 - (void)connection: (XMPPConnection*)connection_
      wasBoundToJID: (XMPPJID*)jid
 {