+#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)
{
else {
pres = [XMPPPresence presence];
if (strcmp(status, "available"))
- [pres addShow: @(status)];
+ [pres setShow: @(status)];
}
[connection sendStanza: pres];
}
@implementation JubGtkRosterUI
-- initWithConnection: (XMPPConnection*)connection_
+- initWithClient: (JubChatClient*)client_
{
self = [super init];
@try {
GtkTreeView *roster_view;
- GtkComboBox *presence_combo;
GtkBuilder *builder;
groupMap = [[OFMapTable alloc]
contactMap = [[OFMutableDictionary alloc] init];
chatMap = [[OFMutableDictionary alloc] init];
presences = [[OFCountedSet alloc] init];
- connection = [connection_ retain];
+ client = [client_ retain];
+
+ [client.contactManager addDelegate: self];
builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "data/gtk/roster.ui", NULL);
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), client.connection);
g_object_unref(G_OBJECT(builder));
} @catch (id e) {
- (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
{
[XMPPMessage messageWithType: @"chat"];
msg.to = jid;
msg.body = text;
- [connection sendStanza: msg];
+ [client.connection sendStanza: msg];
}
] autorelease];
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]];
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];
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);
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);
}
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;;
- 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)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: rosterItem
+ group: group];
+}
- for (OFString *group in groups)
- [self Jub_removeRosterItem: oldItem
- group: group];
+- (void)contact: (XMPPContact*)contact
+ willUpdateWithRosterItem: (XMPPRosterItem*)rosterItem;
+{
+ // Remove contact from old set of groups
+ XMPPRosterItem *oldItem = contact.rosterItem;
+ OFArray *groups = oldItem.groups;
- [contactMap removeObjectForKey: [item.JID bareJID]];
- }
+ if (groups == nil)
+ groups = @[ @"General" ];
- if (![item.subscription isEqual: @"remove"]) {
- OFMapTable *contactRows = [OFMapTable
- mapTableWithKeyFunctions: keyFunctions
- valueFunctions: rowRefFunctions];
+ for (OFString *group in groups)
+ [self Jub_removeRosterItem: oldItem
+ group: group];
- [contactMap setObject: contactRows
- forKey: [item.JID bareJID]];
+ // Add contact to new set of groups
+ groups = rosterItem.groups;
- if (item.groups != nil)
- groups = item.groups;
- else
- groups = @[@"General"];
+ 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)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_
+ 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