]> git.babelmonkeys.de Git - jubjub.git/commitdiff
Adapt to use XMPPContactManager
authorFlorian Zeitz <florob@babelmonkeys.de>
Tue, 29 Jan 2013 18:25:22 +0000 (19:25 +0100)
committerFlorian Zeitz <florob@babelmonkeys.de>
Tue, 29 Jan 2013 18:25:22 +0000 (19:25 +0100)
data/gtk/chat.ui
src/core/JubChatClient.h
src/core/JubChatClient.m
src/gui/gtk/JubGtkChatUI.m
src/gui/gtk/JubGtkRosterUI.h
src/gui/gtk/JubGtkRosterUI.m

index 8c352cd9b88b9bf15fdd08720d4eaf8403fc23cc..bc23241d525b67ea092680a4de95be816235ffc2 100644 (file)
           <object class="GtkEntry" id="ChatEntry">
             <property name="visible">True</property>
             <property name="can_focus">True</property>
+            <property name="has_focus">True</property>
             <property name="invisible_char">●</property>
           </object>
           <packing>
index a73082dde1b475a5f77c7311594b7eee580578c3..4f6205bb9199bc51e4bdb2a4503d47b0b24de853 100644 (file)
@@ -9,11 +9,13 @@
        XMPPConnection *connection;
        XMPPRoster *roster;
        XMPPStreamManagement *streamManagement;
+       XMPPContactManager *contactManager;
        XMPPPresence *presence;
        id<JubUI> ui;
 }
 @property (readonly) XMPPConnection *connection;
 @property (readonly) XMPPRoster *roster;
+@property (readonly) XMPPContactManager *contactManager;
 @property (readonly) XMPPPresence *presence;
 @property (assign) id<JubUI> ui;
 
index 4efa89464e2935ea8e3527526e1a99b97fd8b6fd..b4a1481fb4b76683e7aa039d106abadec4f83fb6 100644 (file)
@@ -3,6 +3,7 @@
 @implementation JubChatClient
 @synthesize connection;
 @synthesize roster;
+@synthesize contactManager;
 @synthesize presence;
 @synthesize ui;
 
                roster = [[XMPPRoster alloc] initWithConnection: connection];
                [roster addDelegate: self];
 
+               contactManager = [[XMPPContactManager alloc]
+                                     initWithConnection: connection
+                                                 roster: roster];
+
                streamManagement = [[XMPPStreamManagement alloc]
                    initWithConnection: connection];
 
@@ -37,6 +42,7 @@
 - (void)dealloc
 {
        [roster release];
+       [contactManager release];
        [streamManagement release];
        [connection release];
        [presence release];
index 8fc633e067cfdbd46df680a6a8b4c2759883276a..7db4aef354823a39363865817a87176096c40c47 100644 (file)
@@ -15,10 +15,14 @@ static gboolean call_send_block(GtkEntry *entry, GdkEventKey *event,
 
        OFString *text =
            [[OFString alloc] initWithUTF8String: gtk_entry_get_text(entry)];
-       gtk_entry_set_text(entry, "");
-       params->block(text);
-       [params->chat addMessage: text
-                         sender: @"You"];
+
+       if ([text length] > 0) {
+               gtk_entry_set_text(entry, "");
+               params->block(text);
+               [params->chat addMessage: text
+                                 sender: @"You"];
+       }
+
        [text release];
 
        return TRUE;
index ec94383e4255e9050ad0aa6d136a3f41af84911e..4b2e50a43b7efff4191b6eb58879a75ca9c8f53c 100644 (file)
@@ -6,7 +6,7 @@
 
 @class JubGtkChatUI;
 
-@interface JubGtkRosterUI: OFObject <XMPPRosterDelegate, XMPPConnectionDelegate>
+@interface JubGtkRosterUI: OFObject <XMPPContactManagerDelegate>
 {
        GtkWidget *roster_window;
        GtkTreeStore *roster_model;
@@ -17,7 +17,7 @@
        OFMutableDictionary *contactMap;
        OFMutableDictionary *chatMap;
        OFCountedSet *presences;
-       XMPPConnection *connection;
+       JubChatClient *client;
 }
 
 - initWithClient: (JubChatClient*)client;
index ad54fd6462ec820c136309dac1f1327f0e17b471..5826aa1f66590fbe0cdbea9628fd58a179594dc0 100644 (file)
@@ -81,7 +81,7 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
 }
 
 @implementation JubGtkRosterUI
-- initWithClient: (JubChatClient*)client;
+- initWithClient: (JubChatClient*)client_
 {
        self = [super init];
 
@@ -95,10 +95,9 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
                contactMap = [[OFMutableDictionary alloc] init];
                chatMap = [[OFMutableDictionary alloc] init];
                presences = [[OFCountedSet alloc] init];
-               connection = [client.connection retain];
+               client = [client_ retain];
 
-               [connection addDelegate: self];
-               [client.roster addDelegate: self];
+               [client.contactManager addDelegate: self];
 
                builder = gtk_builder_new();
                gtk_builder_add_from_file(builder, "data/gtk/roster.ui", NULL);
@@ -129,7 +128,7 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
 
                presence_combo_changed_handler_id =
                    g_signal_connect(presence_combo, "changed",
-                       G_CALLBACK(presence_changed), connection);
+                       G_CALLBACK(presence_changed), client.connection);
 
                g_object_unref(G_OBJECT(builder));
        } @catch (id e) {
@@ -142,31 +141,18 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
 
 - (void)dealloc
 {
+       [client.contactManager removeDelegate: self];
        [groupMap release];
        [contactMap release];
        [chatMap release];
        [presences release];
-       [connection release];
+       [client release];
 
        gtk_widget_destroy(roster_window);
 
        [super dealloc];
 }
 
-/* Presence handling */
--   (void)connection: (XMPPConnection*)connection
-  didReceivePresence: (XMPPPresence*)presence
-{
-       if ([presence.type isEqual: @"available"])
-               [presences addObject: [presence.from bareJID]];
-       else if ([presence.type isEqual: @"unavailable"])
-               [presences removeObject: [presence.from bareJID]];
-
-       g_idle_add_block(^{
-               gtk_tree_model_filter_refilter(roster_filter);
-       });
-}
-
 // FIXME: This needs to move somewhere else
 - (JubGtkChatUI*)chatForJID: (XMPPJID*)jid
 {
@@ -187,7 +173,7 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
                                    [XMPPMessage messageWithType: @"chat"];
                                msg.to = jid;
                                msg.body = text;
-                               [connection sendStanza: msg];
+                               [client.connection sendStanza: msg];
                        }
                ] autorelease];
 
@@ -200,9 +186,12 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
        return chat;
 }
 
--  (void)connection: (XMPPConnection*)connection
-  didReceiveMessage: (XMPPMessage*)message
+-  (void)contact: (XMPPContact*)contact
+  didSendMessage: (XMPPMessage*)message
 {
+       if (message.body == nil || ![message.type isEqual: @"chat"])
+               return;
+
        JubGtkChatUI *chat = [self chatForJID: message.from];
        [chat addMessage: message.body
                  sender: [message.from bareJID]];
@@ -216,8 +205,17 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
                GtkTreeIter group_iter, contact_iter;
                GtkTreeRowReference *group_ref, *contact_ref;
                GtkTreePath *group_path, *contact_path;
-               OFMapTable *contactRows =
-                   [contactMap objectForKey: [item.JID bareJID]];
+               OFString *bareJID = [item.JID bareJID];
+               OFMapTable *contactRows;
+
+               if (!(contactRows = [contactMap objectForKey: bareJID])) {
+                       contactRows = [OFMapTable
+                           mapTableWithKeyFunctions: keyFunctions
+                                     valueFunctions: rowRefFunctions];
+
+                       [contactMap setObject: contactRows
+                                      forKey: bareJID];
+               }
 
                group_ref = [groupMap valueForKey: group];
 
@@ -249,11 +247,11 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
                if (item.name)
                        gtk_tree_store_set(roster_model, &contact_iter,
                            0, [item.name UTF8String],
-                           1, [[item.JID bareJID] UTF8String], -1);
+                           1, [bareJID UTF8String], -1);
                else
                        gtk_tree_store_set(roster_model, &contact_iter,
                            0, [item.JID.node UTF8String],
-                           1, [[item.JID bareJID] UTF8String], -1);
+                           1, [bareJID UTF8String], -1);
 
                contact_path = gtk_tree_model_get_path(GTK_TREE_MODEL(
                        roster_model), &contact_iter);
@@ -275,8 +273,8 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
                GtkTreeIter contact_iter, group_iter;
                GtkTreePath *contact_path, *group_path;
                GtkTreeRowReference *contact_ref, *group_ref;
-               OFMapTable *contactRows =
-                   [contactMap objectForKey: [item.JID bareJID]];
+               OFString *bareJID = [item.JID bareJID];
+               OFMapTable *contactRows = [contactMap objectForKey: bareJID];
 
                contact_ref = [contactRows valueForKey: group];
                contact_path = gtk_tree_row_reference_get_path(contact_ref);
@@ -297,73 +295,80 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
                }
 
                gtk_tree_path_free(group_path);
+
+               [contactRows removeValueForKey: group];
+               if([contactRows count] == 0)
+                       [contactMap removeObjectForKey: bareJID];
        });
 }
 
-- (void)rosterWasReceived: (XMPPRoster*)roster_
+- (void)contactManager: (XMPPContactManager*)manager
+        didAddContact: (XMPPContact*)contact
 {
-       of_log(@"Handling roster");
-       [[roster_ rosterItems] enumerateKeysAndObjectsUsingBlock:
-           ^(OFString *bareJID, XMPPRosterItem *item, BOOL *stop) {
-               OFArray *groups;
-               OFMapTable *contactRows = [OFMapTable
-                   mapTableWithKeyFunctions: keyFunctions
-                             valueFunctions: rowRefFunctions];
-
-               [contactMap setObject: contactRows
-                              forKey: bareJID];
-
-               if (item.groups != nil)
-                       groups = item.groups;
-               else
-                       groups = @[@"General"];
+       XMPPRosterItem *rosterItem = contact.rosterItem;
+       OFArray *groups = rosterItem.groups;;
+
+       if (groups == nil)
+               groups = @[ @"General" ];
 
-               for (OFString *group in groups)
-                       [self Jub_addRosterItem: item
-                                         group: group];
-       }];
+       for (OFString *group in groups)
+               [self Jub_addRosterItem: rosterItem
+                                 group: group];
 }
 
--         (void)roster: (XMPPRoster*)roster_
-  didReceiveRosterItem: (XMPPRosterItem*)item
+- (void)contactManager: (XMPPContactManager*)manager
+      didRemoveContact: (XMPPContact*)contact
 {
-       OFArray *groups;
-       XMPPRosterItem *oldItem =
-           [roster_.rosterItems objectForKey: [item.JID bareJID]];
+       XMPPRosterItem *rosterItem = contact.rosterItem;
+       OFArray *groups = rosterItem.groups;
 
-       if (oldItem) {
-               if (oldItem.groups != nil)
-                       groups = oldItem.groups;
-               else
-                       groups = @[@"General"];
+       if (groups == nil)
+               groups = @[ @"General" ];
 
-               for (OFString *group in groups)
-                       [self Jub_removeRosterItem: oldItem
-                                            group: group];
+       for (OFString *group in groups)
+               [self Jub_removeRosterItem: rosterItem
+                                    group: group];
+}
 
-               [contactMap removeObjectForKey: [item.JID bareJID]];
-       }
+-           (void)contact: (XMPPContact*)contact
+  willUpdateWithRosterItem: (XMPPRosterItem*)rosterItem;
+{
+       // Remove contact from old set of groups
+       XMPPRosterItem *oldItem = contact.rosterItem;
+       OFArray *groups = oldItem.groups;
 
-       if (![item.subscription isEqual: @"remove"]) {
-               OFMapTable *contactRows = [OFMapTable
-                   mapTableWithKeyFunctions: keyFunctions
-                             valueFunctions: rowRefFunctions];
+       if (groups == nil)
+               groups = @[ @"General" ];
 
-               [contactMap setObject: contactRows
-                              forKey: [item.JID bareJID]];
+       for (OFString *group in groups)
+               [self Jub_removeRosterItem: oldItem
+                                    group: group];
 
-               if (item.groups != nil)
-                       groups = item.groups;
-               else
-                       groups = @[@"General"];
+       // Add contact to new set of groups
+       groups = rosterItem.groups;
 
-               for (OFString *group in groups)
-                       [self Jub_addRosterItem: item
-                                         group: group];
-       }
+       if (groups == nil)
+               groups = @[ @"General" ];
+
+       for (OFString *group in groups)
+               [self Jub_addRosterItem: rosterItem
+                                 group: group];
+}
+
+-   (void)contact: (XMPPContact*)contact
+  didSendPresence: (XMPPPresence*)presence
+{
+       if ([presence.type isEqual: @"available"])
+               [presences addObject: [presence.from bareJID]];
+       else if ([presence.type isEqual: @"unavailable"])
+               [presences removeObject: [presence.from bareJID]];
+
+       g_idle_add_block(^{
+               gtk_tree_model_filter_refilter(roster_filter);
+       });
 }
 
--      (void)client: (JubChatClient*)client
+-      (void)client: (JubChatClient*)client_
   didChangePresence: (XMPPPresence*)presence
 {
        OFString *tooltip = @"";