]> cgit.babelmonkeys.de Git - jubjub.git/blob - src/gui/gtk/JubGtkRosterUI.m
Close subscription dialogs, when answered on another client
[jubjub.git] / src / gui / gtk / JubGtkRosterUI.m
1 #import <ObjXMPP/namespaces.h>
2 #include <string.h>
3 #include <gdk-pixbuf/gdk-pixbuf.h>
4
5 #import "JubGtkRosterUI.h"
6 #import "JubGObjectMap.h"
7 #import "JubGtkChatUI.h"
8 #import "JubGtkHelper.h"
9
10 static void roster_row_activated(GtkTreeView *tree_view, GtkTreePath *path,
11     GtkTreeViewColumn *column, gpointer data)
12 {
13         OFAutoreleasePool *pool;
14         XMPPContact *contact;
15         JubChatClient *client = data;
16         GtkTreeIter row_iter;
17         GtkTreeModel *tree_model;
18         gchar *jid_s;
19         OFString *jid;
20
21         tree_model = gtk_tree_view_get_model(tree_view);
22         gtk_tree_model_get_iter(tree_model, &row_iter, path);
23         gtk_tree_model_get(tree_model, &row_iter, 1, &jid_s, -1);
24
25         // This was a group row
26         if (!jid_s) return;
27
28         pool = [OFAutoreleasePool new];
29
30         jid = [OFString stringWithUTF8String: jid_s];
31         contact = [client.contactManager.contacts objectForKey: jid];
32
33         [client performSelectorOnMainThread: @selector(chatForContact:)
34                                  withObject: contact
35                               waitUntilDone: NO];
36         [pool release];
37 }
38
39 static void presence_changed(GtkComboBox *combo_box, gpointer data)
40 {
41         JubChatClient *client = data;
42         OFAutoreleasePool *pool = [OFAutoreleasePool new];
43         const char *status = gtk_combo_box_get_active_id(combo_box);
44
45         [client sendPresenceWithStatus: @(status)];
46
47         [pool release];
48 }
49
50 static gboolean filter_roster_by_presence(GtkTreeModel *model,
51     GtkTreeIter *iter, gpointer data)
52 {
53         char *status;
54         gtk_tree_model_get(model, iter, 2, &status, -1);
55
56         // Groups have no status
57         if (!status)
58                 return TRUE;
59
60         if (!strcmp(status, "unavailable"))
61                 return FALSE;
62
63         return TRUE;
64 }
65
66 static void dialog_response_callback(GtkDialog *dialog, gint response_id,
67     gpointer user_data)
68 {
69         void (^block)(gint) = user_data;
70         block(response_id);
71         [block release];
72 }
73
74 @implementation JubGtkRosterUI
75 - initWithClient: (JubChatClient*)client
76 {
77         self = [super init];
78
79         @try {
80                 GtkTreeView *roster_view;
81                 GtkBuilder *builder;
82
83                 _groupMap = [[OFMapTable alloc]
84                     initWithKeyFunctions: keyFunctions
85                           valueFunctions: rowRefFunctions];
86                 _subDialogMap = [[OFMapTable alloc]
87                     initWithKeyFunctions: keyFunctions
88                     valueFunctions: gObjectFunctions];
89                 _contactMap = [[OFMutableDictionary alloc] init];
90                 _client = [client retain];
91
92                 [_client.contactManager addDelegate: self];
93                 [_client.avatarManager setDelegate: self];
94
95                 builder = gtk_builder_new();
96                 gtk_builder_add_from_file(builder, "data/gtk/roster.ui", NULL);
97                 gtk_builder_connect_signals(builder, NULL);
98
99                 _roster_window =
100                     GTK_WIDGET(gtk_builder_get_object(builder, "RosterWindow"));
101
102                 gtk_widget_show(_roster_window);
103
104                 _roster_model = GTK_TREE_STORE(gtk_builder_get_object(builder,
105                     "RosterTreeStore"));
106
107                 _roster_filter = GTK_TREE_MODEL_FILTER(
108                     gtk_builder_get_object(builder, "RosterTreeModelFilter"));
109
110                 gtk_tree_model_filter_set_visible_func(_roster_filter,
111                     filter_roster_by_presence, NULL, NULL);
112
113                 roster_view = GTK_TREE_VIEW(gtk_builder_get_object(builder,
114                         "RosterTreeView"));
115
116                 g_signal_connect(roster_view, "row_activated",
117                     G_CALLBACK(roster_row_activated), client);
118
119                 _presence_combo = GTK_COMBO_BOX(gtk_builder_get_object(builder,
120                         "PresenceComboBox"));
121
122                 _presence_combo_changed_handler_id =
123                     g_signal_connect(_presence_combo, "changed",
124                         G_CALLBACK(presence_changed), client);
125
126                 g_object_unref(G_OBJECT(builder));
127         } @catch (id e) {
128                 [self release];
129                 @throw e;
130         }
131
132         return self;
133 }
134
135 - (void)dealloc
136 {
137         [_client.avatarManager setDelegate: nil];
138         [_client.contactManager removeDelegate: self];
139         [_subDialogMap release];
140         [_groupMap release];
141         [_contactMap release];
142         [_client release];
143
144         gtk_widget_destroy(_roster_window);
145
146         [super dealloc];
147 }
148
149 - (void)Jub_closeSubscribeDialogForRosterItem: (XMPPRosterItem*)rosterItem
150 {
151         // Close subscripton dialogs, answered on another client
152         GtkDialog *dialog;
153         OFString *subscription = rosterItem.subscription;
154         OFString *bareJID = [rosterItem.JID bareJID];
155
156         if (([subscription isEqual: @"from"] ||
157              [subscription isEqual: @"both"]) &&
158             (dialog = [_subDialogMap valueForKey: bareJID]) != NULL)
159                 gtk_dialog_response(dialog, GTK_RESPONSE_NONE);
160 }
161
162 /* Roster Delegate methods */
163 - (void)Jub_addRosterItem: (XMPPRosterItem*)item
164                     group: (OFString*)group
165 {
166         GtkTreeIter group_iter, contact_iter;
167         GtkTreeRowReference *group_ref, *contact_ref;
168         GtkTreePath *group_path, *contact_path;
169         OFString *bareJID = [item.JID bareJID];
170         OFMapTable *contactRows;
171
172         if (!(contactRows = [_contactMap objectForKey: bareJID])) {
173                 contactRows = [OFMapTable
174                     mapTableWithKeyFunctions: keyFunctions
175                               valueFunctions: rowRefFunctions];
176
177                 [_contactMap setObject: contactRows
178                                 forKey: bareJID];
179         }
180
181         group_ref = [_groupMap valueForKey: group];
182
183         if (!group_ref) {
184                 // Create new group row
185                 gtk_tree_store_append(_roster_model, &group_iter, NULL);
186                 gtk_tree_store_set(_roster_model, &group_iter,
187                     0, [group UTF8String], -1);
188
189                 group_path = gtk_tree_model_get_path(GTK_TREE_MODEL(
190                     _roster_model), &group_iter);
191
192                 group_ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(
193                     _roster_model), group_path);
194
195                 [_groupMap setValue: group_ref
196                              forKey: group];
197         } else {
198                 // Get iter for existing group row
199                 group_path = gtk_tree_row_reference_get_path(group_ref);
200
201                 gtk_tree_model_get_iter(GTK_TREE_MODEL(_roster_model),
202                     &group_iter, group_path);
203         }
204         gtk_tree_path_free(group_path);
205
206         // Create new contact row
207         gtk_tree_store_append(_roster_model, &contact_iter, &group_iter);
208         if (item.name)
209                 gtk_tree_store_set(_roster_model, &contact_iter,
210                     0, [item.name UTF8String],
211                     1, [bareJID UTF8String],
212                     2, "unavailable", -1);
213         else if (item.JID.node)
214                 gtk_tree_store_set(_roster_model, &contact_iter,
215                     0, [item.JID.node UTF8String],
216                     1, [bareJID UTF8String],
217                     2, "unavailable", -1);
218         else
219                 gtk_tree_store_set(_roster_model, &contact_iter,
220                     0, [item.JID.domain UTF8String],
221                     1, [bareJID UTF8String],
222                     2, "unavailable", -1);
223
224         contact_path = gtk_tree_model_get_path(GTK_TREE_MODEL(_roster_model),
225             &contact_iter);
226
227         contact_ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(_roster_model),
228             contact_path);
229
230         gtk_tree_path_free(contact_path);
231
232         [contactRows setValue: contact_ref
233                        forKey: group];
234 }
235
236 - (void)Jub_removeRosterItem: (XMPPRosterItem*)item
237                        group: (OFString*)group
238 {
239         GtkTreeIter contact_iter, group_iter;
240         GtkTreePath *contact_path, *group_path;
241         GtkTreeRowReference *contact_ref, *group_ref;
242         OFString *bareJID = [item.JID bareJID];
243         OFMapTable *contactRows = [_contactMap objectForKey: bareJID];
244
245         contact_ref = [contactRows valueForKey: group];
246         contact_path = gtk_tree_row_reference_get_path(contact_ref);
247         gtk_tree_model_get_iter(GTK_TREE_MODEL(_roster_model), &contact_iter,
248             contact_path);
249         gtk_tree_path_free(contact_path);
250
251         gtk_tree_store_remove(_roster_model, &contact_iter);
252
253         group_ref = [_groupMap valueForKey: group];
254         group_path = gtk_tree_row_reference_get_path(group_ref);
255         gtk_tree_model_get_iter(GTK_TREE_MODEL(_roster_model), &group_iter,
256             group_path);
257
258         if (!gtk_tree_model_iter_has_child(GTK_TREE_MODEL(_roster_model),
259             &group_iter)) {
260                 gtk_tree_store_remove(_roster_model, &group_iter);
261                 [_groupMap removeValueForKey: group];
262         }
263
264         gtk_tree_path_free(group_path);
265
266         [contactRows removeValueForKey: group];
267         if([contactRows count] == 0)
268                 [_contactMap removeObjectForKey: bareJID];
269 }
270
271 - (void)contactManager: (XMPPContactManager*)manager
272          didAddContact: (XMPPContact*)contact
273 {
274         XMPPRosterItem *rosterItem = contact.rosterItem;
275         OFArray *groups = rosterItem.groups;;
276
277         if (groups == nil)
278                 groups = @[ @"General" ];
279
280         for (OFString *group in groups)
281                 [self performSelectorOnGLibThread: @selector(
282                     Jub_addRosterItem:group:)
283                                        withObject: rosterItem
284                                        withObject: group];
285
286         [self Jub_closeSubscribeDialogForRosterItem: rosterItem];
287 }
288
289 - (void)contactManager: (XMPPContactManager*)manager
290       didRemoveContact: (XMPPContact*)contact
291 {
292         XMPPRosterItem *rosterItem = contact.rosterItem;
293         OFArray *groups = rosterItem.groups;
294
295         if (groups == nil)
296                 groups = @[ @"General" ];
297
298         for (OFString *group in groups)
299                 [self performSelectorOnGLibThread: @selector(
300                     Jub_removeRosterItem:group:)
301                                        withObject: rosterItem
302                                        withObject: group];
303 }
304
305 -          (void)contactManager: (XMPPContactManager*)manager
306   didReceiveSubscriptionRequest: (XMPPPresence*)presence
307 {
308         XMPPJID *JID = presence.from;
309         OFString *bareJID = [JID bareJID];
310         OFString *message = [OFString stringWithFormat: @"<b>%@</b> would like "
311             @"to subscribe to your presence.", bareJID];
312         g_idle_add_block(^{
313                 GtkWidget *dialog, *content_area, *label;
314
315                 if ([_subDialogMap valueForKey: JID] != NULL)
316                         return;
317
318                 dialog = gtk_dialog_new_with_buttons("Subscription Request",
319                     GTK_WINDOW(_roster_window), GTK_DIALOG_DESTROY_WITH_PARENT,
320                     "Accept", GTK_RESPONSE_ACCEPT,
321                     "Deny", GTK_RESPONSE_REJECT, NULL);
322
323                 content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
324                 label = gtk_label_new(NULL);
325                 gtk_label_set_markup(GTK_LABEL(label), [message UTF8String]);
326                 gtk_container_add(GTK_CONTAINER(content_area), label);
327
328                 g_signal_connect(dialog, "response",
329                     G_CALLBACK(dialog_response_callback),
330                     [^(gint response_id) {
331                         if (response_id == GTK_RESPONSE_ACCEPT)
332                                 [manager sendSubscribedToJID: JID];
333                         else if (response_id == GTK_RESPONSE_REJECT)
334                                 [manager sendUnsubscribedToJID: JID];
335                         [_subDialogMap removeValueForKey: bareJID];
336                         gtk_widget_destroy(GTK_WIDGET(dialog));
337                 } copy]);
338
339                 [_subDialogMap setValue: dialog
340                                  forKey: bareJID];
341
342                 gtk_widget_show_all(dialog);
343         });
344 }
345
346 -            (void)contact: (XMPPContact*)contact
347   willUpdateWithRosterItem: (XMPPRosterItem*)rosterItem;
348 {
349         [self Jub_closeSubscribeDialogForRosterItem: rosterItem];
350
351         // Remove contact from old set of groups
352         XMPPRosterItem *oldItem = contact.rosterItem;
353         OFArray *groups = oldItem.groups;
354
355         if (groups == nil)
356                 groups = @[ @"General" ];
357
358         for (OFString *group in groups)
359                 [self performSelectorOnGLibThread: @selector(
360                     Jub_removeRosterItem:group:)
361                                        withObject: oldItem
362                                        withObject: group];
363
364         // Add contact to new set of groups
365         groups = rosterItem.groups;
366
367         if (groups == nil)
368                 groups = @[ @"General" ];
369
370         for (OFString *group in groups)
371                 [self performSelectorOnGLibThread: @selector(
372                     Jub_addRosterItem:group:)
373                                        withObject: rosterItem
374                                        withObject: group];
375 }
376
377 -   (void)contact: (XMPPContact*)contact
378   didSendPresence: (XMPPPresence*)presence
379 {
380         OFDictionary *allPresences = [contact presences];
381         XMPPPresence *highPresence = [[[allPresences allObjects] sortedArray]
382                                          lastObject];
383         OFMutableString *tooltip =
384             [OFMutableString stringWithString: @"<b>Resources:</b>"];
385
386         [allPresences enumerateKeysAndObjectsUsingBlock:
387             ^(OFString *resource, XMPPPresence *pres, bool *stop) {
388                 [tooltip appendString: @"\n"];
389                 [tooltip appendString: resource];
390                 if ([pres.type isEqual: @"available"]) {
391                         if (pres.show != nil)
392                                 [tooltip appendFormat: @" (%@)", pres.show];
393                         else
394                                 [tooltip appendString: @" (available)"];
395                 } else
396                         [tooltip appendString: @" (unavailable)"];
397
398                 if (pres.status)
399                         [tooltip appendFormat: @": <i>%@</i>", pres.status];
400         }];
401
402         g_idle_add_block(^{
403                 GtkTreeIter iter;
404                 GtkTreePath *path;
405                 GtkTreeRowReference *ref;
406                 OFString *bareJID = [contact.rosterItem.JID bareJID];
407                 OFMapTable *contactRows = [_contactMap objectForKey: bareJID];
408                 OFArray *groups = contact.rosterItem.groups;;
409
410                 if (groups == nil)
411                         groups = @[ @"General" ];
412
413                 for (OFString *group in groups) {
414                         ref = [contactRows valueForKey: group];
415                         path = gtk_tree_row_reference_get_path(ref);
416                         gtk_tree_model_get_iter(GTK_TREE_MODEL(_roster_model),
417                             &iter, path);
418                         gtk_tree_path_free(path);
419
420                         if ([highPresence.type isEqual: @"available"]) {
421                                 if (highPresence.show != nil)
422                                         gtk_tree_store_set(_roster_model, &iter,
423                                             2, [highPresence.show UTF8String],
424                                             -1);
425                                 else
426                                         gtk_tree_store_set(_roster_model, &iter,
427                                             2, "available", -1);
428                         } else
429                                 gtk_tree_store_set(_roster_model, &iter,
430                                     2, "unavailable", -1);
431
432                         gtk_tree_store_set(_roster_model, &iter,
433                             3, [tooltip UTF8String], -1);
434                 }
435
436                 gtk_tree_model_filter_refilter(_roster_filter);
437         });
438 }
439
440 - (void)contact: (XMPPContact*)contact
441    didSetAvatar: (OFString*)avatarFile
442 {
443         of_log(@"Got an avatar from %@", contact.rosterItem.JID);
444         g_idle_add_block(^{
445                 GtkTreeIter iter;
446                 GtkTreePath *path;
447                 GtkTreeRowReference *ref;
448                 OFString *bareJID = [contact.rosterItem.JID bareJID];
449                 OFMapTable *contactRows = [_contactMap objectForKey: bareJID];
450                 OFArray *groups = contact.rosterItem.groups;;
451
452                 GdkPixbuf *avatar =
453                     gdk_pixbuf_new_from_file([avatarFile UTF8String], NULL);
454
455                 if (groups == nil)
456                         groups = @[ @"General" ];
457
458                 for (OFString *group in groups) {
459                         ref = [contactRows valueForKey: group];
460                         path = gtk_tree_row_reference_get_path(ref);
461                         gtk_tree_model_get_iter(GTK_TREE_MODEL(_roster_model),
462                             &iter, path);
463                         gtk_tree_path_free(path);
464
465                         gtk_tree_store_set(_roster_model, &iter,
466                             4, avatar, -1);
467                 }
468                 g_object_unref(G_OBJECT(avatar));
469         });
470 }
471
472 -      (void)client: (JubChatClient*)client_
473   didChangePresence: (XMPPPresence*)presence
474 {
475         OFString *tooltip = @"";
476         OFString *show =
477             [[presence elementForName: @"show"
478                             namespace: XMPP_NS_CLIENT] stringValue];
479         OFString *status =
480             [[presence elementForName: @"status"
481                             namespace: XMPP_NS_CLIENT] stringValue];
482
483         if (status != nil)
484                 tooltip = [@"<b>Status:</b> " stringByAppendingString: status];
485
486         g_idle_add_block(^{
487                 // Block the PresenceComboBox's changed handler, so it doesn't
488                 // fire and resend presence
489                 g_signal_handler_block(_presence_combo,
490                     _presence_combo_changed_handler_id);
491
492                 if ([presence.type isEqual: @"unavailable"])
493                         gtk_combo_box_set_active_id(_presence_combo,
494                             "unavailable");
495                 else if (show == nil)
496                         gtk_combo_box_set_active_id(_presence_combo,
497                             "available");
498                 else
499                         gtk_combo_box_set_active_id(_presence_combo,
500                             [show UTF8String]);
501
502                 // Unblock the changed handler
503                 g_signal_handler_unblock(_presence_combo,
504                     _presence_combo_changed_handler_id);
505
506                 gtk_widget_set_tooltip_markup(GTK_WIDGET(_presence_combo),
507                     [tooltip UTF8String]);
508         });
509 }
510 @end