]> cgit.babelmonkeys.de Git - jubjub.git/blob - src/gui/gtk/JubGtkRosterUI.m
Adjust to ObjXMPP changes.
[jubjub.git] / src / gui / gtk / JubGtkRosterUI.m
1 #import <ObjXMPP/namespaces.h>
2 #include <string.h>
3
4 #import "JubGtkRosterUI.h"
5 #import "JubGObjectMap.h"
6 #import "JubGtkChatUI.h"
7 #import "JubGtkHelper.h"
8
9 static void roster_row_activated(GtkTreeView *tree_view, GtkTreePath *path,
10     GtkTreeViewColumn *column, gpointer data)
11 {
12         JubGtkRosterUI *roster = data;
13         GtkTreeIter row_iter;
14         GtkTreeModel *tree_model;
15         gchar *jid_s;
16         XMPPJID *jid;
17         OFAutoreleasePool *pool;
18
19         tree_model = gtk_tree_view_get_model(tree_view);
20         gtk_tree_model_get_iter(tree_model, &row_iter, path);
21         gtk_tree_model_get(tree_model, &row_iter, 1, &jid_s, -1);
22
23         // This was a group row
24         if (!jid_s) return;
25
26         pool = [OFAutoreleasePool new];
27         jid = [XMPPJID JIDWithString: [OFString stringWithUTF8String: jid_s]];
28
29         [roster performSelectorOnMainThread: @selector(chatForJID:)
30                                  withObject: jid
31                               waitUntilDone: NO];
32         [pool release];
33 }
34
35 static void presence_changed(GtkComboBox *combo_box, gpointer data)
36 {
37         XMPPPresence *pres;
38         XMPPConnection *connection = data;
39         OFAutoreleasePool *pool = [OFAutoreleasePool new];
40
41         const char *status = gtk_combo_box_get_active_id(combo_box);
42
43         if (!strcmp(status, "unavailable"))
44                 pres = [XMPPPresence presenceWithType: @"unavailable"];
45         else {
46                 pres = [XMPPPresence presence];
47                 if (strcmp(status, "available"))
48                         [pres setShow: @(status)];
49         }
50
51         [connection sendStanza: pres];
52
53         [pool release];
54 }
55
56 static gboolean filter_roster_by_presence(GtkTreeModel *model,
57     GtkTreeIter *iter, gpointer data)
58 {
59         char *jid_s;
60         OFString *jid;
61         OFCountedSet *presences = data;
62
63         gtk_tree_model_get(model, iter, 1, &jid_s, -1);
64
65         // Groups have no JID
66         if (!jid_s)
67                 return TRUE;
68
69         jid = [[OFString alloc] initWithUTF8String: jid_s];
70
71         g_free(jid_s);
72
73         int num = [presences countForObject: jid];
74         if (num) {
75                 [jid release];
76                 return TRUE;
77         } else {
78                 [jid release];
79                 return FALSE;
80         }
81 }
82
83 @implementation JubGtkRosterUI
84 - initWithClient: (JubChatClient*)client;
85 {
86         self = [super init];
87
88         @try {
89                 GtkTreeView *roster_view;
90                 GtkBuilder *builder;
91
92                 groupMap = [[OFMapTable alloc]
93                     initWithKeyFunctions: keyFunctions
94                           valueFunctions: rowRefFunctions];
95                 contactMap = [[OFMutableDictionary alloc] init];
96                 chatMap = [[OFMutableDictionary alloc] init];
97                 presences = [[OFCountedSet alloc] init];
98                 connection = [client.connection retain];
99
100                 [connection addDelegate: self];
101                 [client.roster addDelegate: self];
102
103                 builder = gtk_builder_new();
104                 gtk_builder_add_from_file(builder, "data/gtk/roster.ui", NULL);
105                 gtk_builder_connect_signals(builder, NULL);
106
107                 roster_window =
108                     GTK_WIDGET(gtk_builder_get_object(builder, "RosterWindow"));
109
110                 gtk_widget_show(roster_window);
111
112                 roster_model = GTK_TREE_STORE(gtk_builder_get_object(builder,
113                     "RosterTreeStore"));
114
115                 roster_filter = GTK_TREE_MODEL_FILTER(
116                     gtk_builder_get_object(builder, "RosterTreeModelFilter"));
117
118                 gtk_tree_model_filter_set_visible_func(roster_filter,
119                     filter_roster_by_presence, presences, NULL);
120
121                 roster_view = GTK_TREE_VIEW(gtk_builder_get_object(builder,
122                         "RosterTreeView"));
123
124                 g_signal_connect(roster_view, "row_activated",
125                     G_CALLBACK(roster_row_activated), self);
126
127                 presence_combo = GTK_COMBO_BOX(gtk_builder_get_object(builder,
128                         "PresenceComboBox"));
129
130                 presence_combo_changed_handler_id =
131                     g_signal_connect(presence_combo, "changed",
132                         G_CALLBACK(presence_changed), connection);
133
134                 g_object_unref(G_OBJECT(builder));
135         } @catch (id e) {
136                 [self release];
137                 @throw e;
138         }
139
140         return self;
141 }
142
143 - (void)dealloc
144 {
145         [groupMap release];
146         [contactMap release];
147         [chatMap release];
148         [presences release];
149         [connection release];
150
151         gtk_widget_destroy(roster_window);
152
153         [super dealloc];
154 }
155
156 /* Presence handling */
157 -   (void)connection: (XMPPConnection*)connection
158   didReceivePresence: (XMPPPresence*)presence
159 {
160         if ([presence.type isEqual: @"available"])
161                 [presences addObject: [presence.from bareJID]];
162         else if ([presence.type isEqual: @"unavailable"])
163                 [presences removeObject: [presence.from bareJID]];
164
165         g_idle_add_block(^{
166                 gtk_tree_model_filter_refilter(roster_filter);
167         });
168 }
169
170 // FIXME: This needs to move somewhere else
171 - (JubGtkChatUI*)chatForJID: (XMPPJID*)jid
172 {
173         OFAutoreleasePool *pool = [OFAutoreleasePool new];
174         JubGtkChatUI *chat =
175             [chatMap objectForKey: [jid bareJID]];
176         if (chat == nil) {
177                 OFString * title = [@"Chat with " stringByAppendingString:
178                     [jid bareJID]];
179
180                 chat = [[[JubGtkChatUI alloc]
181                     initWithTitle: title
182                        closeBlock: ^{
183                                 [chatMap removeObjectForKey: [jid bareJID]];
184                         }
185                         sendBlock: ^(OFString *text) {
186                                 XMPPMessage *msg =
187                                     [XMPPMessage messageWithType: @"chat"];
188                                 msg.to = jid;
189                                 msg.body = text;
190                                 [connection sendStanza: msg];
191                         }
192                 ] autorelease];
193
194                 [chatMap setObject: chat
195                             forKey: [jid bareJID]];
196         }
197
198         [pool release];
199
200         return chat;
201 }
202
203 -  (void)connection: (XMPPConnection*)connection
204   didReceiveMessage: (XMPPMessage*)message
205 {
206         JubGtkChatUI *chat = [self chatForJID: message.from];
207         [chat addMessage: message.body
208                   sender: [message.from bareJID]];
209 }
210
211 /* Roster Delegate methods */
212 - (void)Jub_addRosterItem: (XMPPRosterItem*)item
213                     group: (OFString*)group
214 {
215         g_idle_add_block(^{
216                 GtkTreeIter group_iter, contact_iter;
217                 GtkTreeRowReference *group_ref, *contact_ref;
218                 GtkTreePath *group_path, *contact_path;
219                 OFMapTable *contactRows =
220                     [contactMap objectForKey: [item.JID bareJID]];
221
222                 group_ref = [groupMap valueForKey: group];
223
224                 if (!group_ref) {
225                         // Create new group row
226                         gtk_tree_store_append(roster_model, &group_iter, NULL);
227                         gtk_tree_store_set(roster_model, &group_iter,
228                             0, [group UTF8String], -1);
229
230                         group_path = gtk_tree_model_get_path(GTK_TREE_MODEL(
231                                 roster_model), &group_iter);
232
233                         group_ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(
234                                 roster_model), group_path);
235
236                         [groupMap setValue: group_ref
237                                     forKey: group];
238                 } else {
239                         // Get iter for existing group row
240                         group_path = gtk_tree_row_reference_get_path(group_ref);
241
242                         gtk_tree_model_get_iter(GTK_TREE_MODEL(roster_model),
243                             &group_iter, group_path);
244                 }
245                 gtk_tree_path_free(group_path);
246
247                 // Create new contact row
248                 gtk_tree_store_append(roster_model, &contact_iter, &group_iter);
249                 if (item.name)
250                         gtk_tree_store_set(roster_model, &contact_iter,
251                             0, [item.name UTF8String],
252                             1, [[item.JID bareJID] UTF8String], -1);
253                 else
254                         gtk_tree_store_set(roster_model, &contact_iter,
255                             0, [item.JID.node UTF8String],
256                             1, [[item.JID bareJID] UTF8String], -1);
257
258                 contact_path = gtk_tree_model_get_path(GTK_TREE_MODEL(
259                         roster_model), &contact_iter);
260
261                 contact_ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(
262                         roster_model), contact_path);
263
264                 gtk_tree_path_free(contact_path);
265
266                 [contactRows setValue: contact_ref
267                                forKey: group];
268         });
269 }
270
271 - (void)Jub_removeRosterItem: (XMPPRosterItem*)item
272                        group: (OFString*)group
273 {
274         g_idle_add_block(^{
275                 GtkTreeIter contact_iter, group_iter;
276                 GtkTreePath *contact_path, *group_path;
277                 GtkTreeRowReference *contact_ref, *group_ref;
278                 OFMapTable *contactRows =
279                     [contactMap objectForKey: [item.JID bareJID]];
280
281                 contact_ref = [contactRows valueForKey: group];
282                 contact_path = gtk_tree_row_reference_get_path(contact_ref);
283                 gtk_tree_model_get_iter(GTK_TREE_MODEL(roster_model),
284                     &contact_iter, contact_path);
285
286                 gtk_tree_store_remove(roster_model, &contact_iter);
287
288                 group_ref = [groupMap valueForKey: group];
289                 group_path = gtk_tree_row_reference_get_path(group_ref);
290                 gtk_tree_model_get_iter(GTK_TREE_MODEL(roster_model),
291                     &group_iter, group_path);
292
293                 if (!gtk_tree_model_iter_has_child(GTK_TREE_MODEL(roster_model),
294                     &group_iter)) {
295                         gtk_tree_store_remove(roster_model, &group_iter);
296                         [groupMap removeValueForKey: group];
297                 }
298
299                 gtk_tree_path_free(group_path);
300         });
301 }
302
303 - (void)rosterWasReceived: (XMPPRoster*)roster_
304 {
305         of_log(@"Handling roster");
306         [[roster_ rosterItems] enumerateKeysAndObjectsUsingBlock:
307             ^(OFString *bareJID, XMPPRosterItem *item, BOOL *stop) {
308                 OFArray *groups;
309                 OFMapTable *contactRows = [OFMapTable
310                     mapTableWithKeyFunctions: keyFunctions
311                               valueFunctions: rowRefFunctions];
312
313                 [contactMap setObject: contactRows
314                                forKey: bareJID];
315
316                 if (item.groups != nil)
317                         groups = item.groups;
318                 else
319                         groups = @[@"General"];
320
321                 for (OFString *group in groups)
322                         [self Jub_addRosterItem: item
323                                           group: group];
324         }];
325 }
326
327 -         (void)roster: (XMPPRoster*)roster_
328   didReceiveRosterItem: (XMPPRosterItem*)item
329 {
330         OFArray *groups;
331         XMPPRosterItem *oldItem =
332             [roster_.rosterItems objectForKey: [item.JID bareJID]];
333
334         if (oldItem) {
335                 if (oldItem.groups != nil)
336                         groups = oldItem.groups;
337                 else
338                         groups = @[@"General"];
339
340                 for (OFString *group in groups)
341                         [self Jub_removeRosterItem: oldItem
342                                              group: group];
343
344                 [contactMap removeObjectForKey: [item.JID bareJID]];
345         }
346
347         if (![item.subscription isEqual: @"remove"]) {
348                 OFMapTable *contactRows = [OFMapTable
349                     mapTableWithKeyFunctions: keyFunctions
350                               valueFunctions: rowRefFunctions];
351
352                 [contactMap setObject: contactRows
353                                forKey: [item.JID bareJID]];
354
355                 if (item.groups != nil)
356                         groups = item.groups;
357                 else
358                         groups = @[@"General"];
359
360                 for (OFString *group in groups)
361                         [self Jub_addRosterItem: item
362                                           group: group];
363         }
364 }
365
366 -      (void)client: (JubChatClient*)client
367   didChangePresence: (XMPPPresence*)presence
368 {
369         OFString *tooltip = @"";
370         OFString *show =
371             [[presence elementForName: @"show"
372                             namespace: XMPP_NS_CLIENT] stringValue];
373         OFString *status =
374             [[presence elementForName: @"status"
375                             namespace: XMPP_NS_CLIENT] stringValue];
376
377         if (status != nil)
378                 tooltip = [@"<b>Status:</b> " stringByAppendingString: status];
379
380         g_idle_add_block(^{
381                 // Block the PresenceComboBox's changed handler, so it doesn't
382                 // fire and resend presence
383                 g_signal_handler_block(presence_combo,
384                     presence_combo_changed_handler_id);
385
386                 if ([presence.type isEqual: @"unavailable"])
387                         gtk_combo_box_set_active_id(presence_combo,
388                             "unavailable");
389                 else if (show == nil)
390                         gtk_combo_box_set_active_id(presence_combo,
391                             "available");
392                 else
393                         gtk_combo_box_set_active_id(presence_combo,
394                             [show UTF8String]);
395
396                 // Unblock the changed handler
397                 g_signal_handler_unblock(presence_combo,
398                     presence_combo_changed_handler_id);
399
400                 gtk_widget_set_tooltip_markup(GTK_WIDGET(presence_combo),
401                     [tooltip UTF8String]);
402         });
403 }
404 @end