]> cgit.babelmonkeys.de Git - jubjub.git/blobdiff - src/gui/gtk/JubGtkRosterUI.m
Handle closing chat windows, fix a race and a retain cycle in the process
[jubjub.git] / src / gui / gtk / JubGtkRosterUI.m
index acdffb6c96dfdcbffd36a1252616472a0319f603..c5163e1ff0c8387619eb6e54b8458e8fb8087a2d 100644 (file)
@@ -1,5 +1,6 @@
 #import "JubGtkRosterUI.h"
 #import "JubGObjectMap.h"
+#import "JubGtkChatUI.h"
 
 static gboolean filter_roster_by_presence(GtkTreeModel *model,
     GtkTreeIter *iter, gpointer data)
@@ -38,6 +39,7 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
                    initWithKeyFunctions: keyFunctions
                          valueFunctions: rowRefFunctions];
                contactMap = [[OFMutableDictionary alloc] init];
+               chatMap = [[OFMutableDictionary alloc] init];
                presences = [[OFCountedSet alloc] init];
 
                builder = g_object_ref(builder_);
@@ -94,6 +96,39 @@ static gboolean refilter_roster(gpointer data)
        g_idle_add(refilter_roster, roster_filter);
 }
 
+// FIXME: This needs to move somewhere else
+-  (void)connection: (XMPPConnection*)connection
+  didReceiveMessage: (XMPPMessage*)message
+{
+       JubGtkChatUI *chat =
+           [chatMap objectForKey: [message.from bareJID]];
+       if (chat == nil) {
+               OFString * title = [@"Chat with " stringByAppendingString:
+                   [message.from bareJID]];
+
+               chat = [[[JubGtkChatUI alloc]
+                   initWithTitle: title
+                      closeBlock: ^{
+                               [chatMap removeObjectForKey:
+                                   [message.from bareJID]];
+                       }
+                       sendBlock: ^(OFString *text) {
+                               XMPPMessage *msg =
+                                   [XMPPMessage messageWithType: @"chat"];
+                               msg.to = message.from;
+                               msg.body = text;
+                               [connection sendStanza: msg];
+                       }
+               ] autorelease];
+
+               [chatMap setObject: chat
+                           forKey: [message.from bareJID]];
+       }
+
+       [chat addMessage: message.body
+                 sender: [message.from bareJID]];
+}
+
 /* Roster Delegate methods */
 struct add_roster_item_param {
        OFString *group;