]> cgit.babelmonkeys.de Git - jubjub.git/commitdiff
Move chat window creation to JubChatClient
authorFlorian Zeitz <florob@babelmonkeys.de>
Wed, 30 Jan 2013 20:09:34 +0000 (21:09 +0100)
committerFlorian Zeitz <florob@babelmonkeys.de>
Wed, 30 Jan 2013 20:09:34 +0000 (21:09 +0100)
src/core/JubChatClient.h
src/core/JubChatClient.m
src/gui/common/JubChatUI.h [new file with mode: 0644]
src/gui/common/JubUI.h
src/gui/gtk/JubGtkChatUI.h
src/gui/gtk/JubGtkRosterUI.h
src/gui/gtk/JubGtkRosterUI.m
src/gui/gtk/JubGtkUI.m

index 4f6205bb9199bc51e4bdb2a4503d47b0b24de853..c6a1da8b6df16fbe0f433312e9573163e2f6d703 100644 (file)
@@ -2,10 +2,12 @@
 #import <ObjFW/ObjFW.h>
 
 #import "JubUI.h"
+#import "JubChatUI.h"
 #import "JubConfig.h"
 
 @interface JubChatClient : OFObject <XMPPConnectionDelegate, XMPPRosterDelegate>
 {
+       OFMutableDictionary *chatMap;
        XMPPConnection *connection;
        XMPPRoster *roster;
        XMPPStreamManagement *streamManagement;
@@ -20,4 +22,5 @@
 @property (assign) id<JubUI> ui;
 
 - initWithConfig: (JubConfig*)config;
+- (id<JubChatUI>)chatForContact: (XMPPContact*)contact;
 @end
index b4a1481fb4b76683e7aa039d106abadec4f83fb6..3c563f5e9dd1a9f267b9b09ad63787a8be9de1ea 100644 (file)
@@ -12,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;
        [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
 {
diff --git a/src/gui/common/JubChatUI.h b/src/gui/common/JubChatUI.h
new file mode 100644 (file)
index 0000000..b65491c
--- /dev/null
@@ -0,0 +1,13 @@
+@class OFString;
+
+typedef void (^jub_send_block_t)(OFString *);
+typedef void (^jub_close_block_t)(void);
+
+@protocol JubChatUI
+- initWithTitle: (OFString*)title
+     closeBlock: (jub_close_block_t)closeBlock
+      sendBlock: (jub_send_block_t)sendBlock;
+
+- (void)addMessage: (OFString*)text
+           sender: (OFString*)sender;
+@end
index 5d76aadbec2ecc5c1cbbefb9e506df1089855528..f24bcac76e4a5b234c665f40a67cdf2ba92e65e3 100644 (file)
@@ -6,4 +6,5 @@
 - (void)startUIThread;
 -      (void)client: (JubChatClient*)client
   didChangePresence: (XMPPPresence*)presence;
+- (Class)chatUIClass;
 @end
index 351eb6d02c10cf91943442836ed9493770733d5a..37e47ff8066df99186307fdd05258c6760185298 100644 (file)
@@ -2,10 +2,9 @@
 #import <ObjXMPP/ObjXMPP.h>
 #include <gtk/gtk.h>
 
-typedef void (^jub_send_block_t)(OFString *);
-typedef void (^jub_close_block_t)(void);
+#import "JubChatUI.h"
 
-@interface JubGtkChatUI: OFObject
+@interface JubGtkChatUI: OFObject <JubChatUI>
 {
        GtkWidget *chat_window;
        GtkTextView *chat_view;
@@ -14,11 +13,4 @@ typedef void (^jub_close_block_t)(void);
        jub_close_block_t closeBlock;
        BOOL bufferEmpty;
 }
-
-- initWithTitle: (OFString*)title
-     closeBlock: (jub_close_block_t)closeBlock
-      sendBlock: (jub_send_block_t)sendBlock;
-
-- (void)addMessage: (OFString*)text
-           sender: (OFString*)sender;
 @end
index 5dd36826daf2c311829755badb87e52e9ae5fdde..4f3d7ac826a4735bcba4a84027242cf15c9f8fdc 100644 (file)
        gulong presence_combo_changed_handler_id;
        OFMapTable *groupMap;
        OFMutableDictionary *contactMap;
-       OFMutableDictionary *chatMap;
        JubChatClient *client;
 }
 
 - initWithClient: (JubChatClient*)client;
-- (JubGtkChatUI*)chatForJID: (XMPPJID*)jid;
 -      (void)client: (JubChatClient*)client
   didChangePresence: (XMPPPresence*)presence;
 @end
index 4102e3432e3cf0307ffb90c9ff4908a613b1d4a2..e474bf9bc2438b946860a277f5e477960a27ea73 100644 (file)
@@ -9,12 +9,13 @@
 static void roster_row_activated(GtkTreeView *tree_view, GtkTreePath *path,
     GtkTreeViewColumn *column, gpointer data)
 {
-       JubGtkRosterUI *roster = data;
+       OFAutoreleasePool *pool;
+       XMPPContact *contact;
+       JubChatClient *client = data;
        GtkTreeIter row_iter;
        GtkTreeModel *tree_model;
        gchar *jid_s;
-       XMPPJID *jid;
-       OFAutoreleasePool *pool;
+       OFString *jid;
 
        tree_model = gtk_tree_view_get_model(tree_view);
        gtk_tree_model_get_iter(tree_model, &row_iter, path);
@@ -24,10 +25,12 @@ static void roster_row_activated(GtkTreeView *tree_view, GtkTreePath *path,
        if (!jid_s) return;
 
        pool = [OFAutoreleasePool new];
-       jid = [XMPPJID JIDWithString: [OFString stringWithUTF8String: jid_s]];
 
-       [roster performSelectorOnMainThread: @selector(chatForJID:)
-                                withObject: jid
+       jid = [OFString stringWithUTF8String: jid_s];
+       contact = [client.contactManager.contacts objectForKey: jid];
+
+       [client performSelectorOnMainThread: @selector(chatForContact:)
+                                withObject: contact
                              waitUntilDone: NO];
        [pool release];
 }
@@ -82,7 +85,6 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
                    initWithKeyFunctions: keyFunctions
                          valueFunctions: rowRefFunctions];
                contactMap = [[OFMutableDictionary alloc] init];
-               chatMap = [[OFMutableDictionary alloc] init];
                client = [client_ retain];
 
                [client.contactManager addDelegate: self];
@@ -109,7 +111,7 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
                        "RosterTreeView"));
 
                g_signal_connect(roster_view, "row_activated",
-                   G_CALLBACK(roster_row_activated), self);
+                   G_CALLBACK(roster_row_activated), client);
 
                presence_combo = GTK_COMBO_BOX(gtk_builder_get_object(builder,
                        "PresenceComboBox"));
@@ -132,7 +134,6 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
        [client.contactManager removeDelegate: self];
        [groupMap release];
        [contactMap release];
-       [chatMap release];
        [client release];
 
        gtk_widget_destroy(roster_window);
@@ -140,46 +141,13 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
        [super dealloc];
 }
 
-// FIXME: This needs to move somewhere else
-- (JubGtkChatUI*)chatForJID: (XMPPJID*)jid
-{
-       OFAutoreleasePool *pool = [OFAutoreleasePool new];
-       JubGtkChatUI *chat =
-           [chatMap objectForKey: [jid bareJID]];
-       if (chat == nil) {
-               OFString * title = [@"Chat with " stringByAppendingString:
-                   [jid bareJID]];
-
-               chat = [[[JubGtkChatUI alloc]
-                   initWithTitle: title
-                      closeBlock: ^{
-                               [chatMap removeObjectForKey: [jid bareJID]];
-                       }
-                       sendBlock: ^(OFString *text) {
-                               XMPPMessage *msg =
-                                   [XMPPMessage messageWithType: @"chat"];
-                               msg.to = jid;
-                               msg.body = text;
-                               [client.connection sendStanza: msg];
-                       }
-               ] autorelease];
-
-               [chatMap setObject: chat
-                           forKey: [jid bareJID]];
-       }
-
-       [pool release];
-
-       return chat;
-}
-
 -  (void)contact: (XMPPContact*)contact
   didSendMessage: (XMPPMessage*)message
 {
        if (message.body == nil || ![message.type isEqual: @"chat"])
                return;
 
-       JubGtkChatUI *chat = [self chatForJID: message.from];
+       id<JubChatUI> chat = [client chatForContact: contact];
        [chat addMessage: message.body
                  sender: [message.from bareJID]];
 }
index 7aef46c052503fe510ebf1cdf74683c1ce3765b2..d3595a9840089648e8d9d8bbbff9f485e0ae7dac 100644 (file)
@@ -2,6 +2,7 @@
 #include <gtk/gtk.h>
 
 #import "JubGtkUI.h"
+#import "JubGtkChatUI.h"
 #import "JubGtkRosterUI.h"
 
 void on_roster_window_destroy(GObject *object, gpointer user_data)
@@ -50,6 +51,11 @@ void on_roster_window_destroy(GObject *object, gpointer user_data)
        }] start];
 }
 
+- (Class)chatUIClass
+{
+       return [JubGtkChatUI class];
+}
+
 -      (void)client: (JubChatClient*)client
   didChangePresence: (XMPPPresence*)presence
 {