]> cgit.babelmonkeys.de Git - jubjub.git/blobdiff - src/gui/gtk/JubGtkRosterUI.m
Make the PresenceComboBox's changed handler only fire when the user changed it
[jubjub.git] / src / gui / gtk / JubGtkRosterUI.m
index b3899e34a1d750edf1e57d0d6961d4a8f4baf326..ae0d54d778d78ab296d876413f93fe7e5d0f19be 100644 (file)
@@ -1,10 +1,11 @@
+#import <ObjXMPP/namespaces.h>
+#include <string.h>
+
 #import "JubGtkRosterUI.h"
 #import "JubGObjectMap.h"
 #import "JubGtkChatUI.h"
 #import "JubGtkHelper.h"
 
-#include <string.h>
-
 static void roster_row_activated(GtkTreeView *tree_view, GtkTreePath *path,
     GtkTreeViewColumn *column, gpointer data)
 {
@@ -80,13 +81,12 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
 }
 
 @implementation JubGtkRosterUI
-- initWithConnection: (XMPPConnection*)connection_
+- initWithClient: (JubChatClient*)client;
 {
        self = [super init];
 
        @try {
                GtkTreeView *roster_view;
-               GtkComboBox *presence_combo;
                GtkBuilder *builder;
 
                groupMap = [[OFMapTable alloc]
@@ -95,7 +95,10 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
                contactMap = [[OFMutableDictionary alloc] init];
                chatMap = [[OFMutableDictionary alloc] init];
                presences = [[OFCountedSet alloc] init];
-               connection = [connection_ retain];
+               connection = [client.connection retain];
+
+               [connection addDelegate: self];
+               [client.roster addDelegate: self];
 
                builder = gtk_builder_new();
                gtk_builder_add_from_file(builder, "data/gtk/roster.ui", NULL);
@@ -124,8 +127,9 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
                presence_combo = GTK_COMBO_BOX(gtk_builder_get_object(builder,
                        "PresenceComboBox"));
 
-               g_signal_connect(presence_combo, "changed",
-                   G_CALLBACK(presence_changed), connection);
+               presence_combo_changed_handler_id =
+                   g_signal_connect(presence_combo, "changed",
+                       G_CALLBACK(presence_changed), connection);
 
                g_object_unref(G_OBJECT(builder));
        } @catch (id e) {
@@ -359,4 +363,42 @@ static gboolean filter_roster_by_presence(GtkTreeModel *model,
        }
 }
 
+-      (void)client: (JubChatClient*)client
+  didChangePresence: (XMPPPresence*)presence
+{
+       OFString *tooltip = @"";
+       OFString *show =
+           [[presence elementForName: @"show"
+                           namespace: XMPP_NS_CLIENT] stringValue];
+       OFString *status =
+           [[presence elementForName: @"status"
+                           namespace: XMPP_NS_CLIENT] stringValue];
+
+       if (status != nil)
+               tooltip = [@"<b>Status:</b> " stringByAppendingString: status];
+
+       g_idle_add_block(^{
+               // Block the PresenceComboBox's changed handler, so it doesn't
+               // fire and resend presence
+               g_signal_handler_block(presence_combo,
+                   presence_combo_changed_handler_id);
+
+               if ([presence.type isEqual: @"unavailable"])
+                       gtk_combo_box_set_active_id(presence_combo,
+                           "unavailable");
+               else if (show == nil)
+                       gtk_combo_box_set_active_id(presence_combo,
+                           "available");
+               else
+                       gtk_combo_box_set_active_id(presence_combo,
+                           [show UTF8String]);
+
+               // Unblock the changed handler
+               g_signal_handler_unblock(presence_combo,
+                   presence_combo_changed_handler_id);
+
+               gtk_widget_set_tooltip_markup(GTK_WIDGET(presence_combo),
+                   [tooltip UTF8String]);
+       });
+}
 @end