1 #import <ObjXMPP/ObjXMPP.h>
5 #import "JubGObjectMap.h"
7 void on_roster_window_destroy(GObject *object, gpointer user_data)
12 static gboolean filter_roster_by_presence(GtkTreeModel *model,
13 GtkTreeIter *iter, gpointer data)
17 OFCountedSet *presences = data;
19 gtk_tree_model_get(model, iter, 1, &jid_s, -1);
25 jid = [[OFString alloc] initWithUTF8String: jid_s];
29 if ([presences countForObject: jid]) {
38 @implementation JubGtkUI
44 groupMap = [[OFMapTable alloc]
45 initWithKeyFunctions: keyFunctions
46 valueFunctions: rowRefFunctions];
47 contactMap = [[OFMutableDictionary alloc] init];
48 presences = [[OFCountedSet alloc] init];
65 g_object_unref(roster_model);
76 GtkWidget *roster_window;
78 [[OFApplication sharedApplication] getArgumentCount: &argc
79 andArgumentValues: &argv];
83 builder = gtk_builder_new();
84 gtk_builder_add_from_file(builder, "data/gtk/roster.ui", NULL);
87 GTK_WIDGET(gtk_builder_get_object(builder, "RosterWindow"));
90 GTK_TREE_STORE(gtk_builder_get_object(builder, "RosterTreeStore"));
92 roster_filter = GTK_TREE_MODEL_FILTER(
93 gtk_builder_get_object(builder,"RosterTreeModelFilter"));
95 gtk_tree_model_filter_set_visible_func(roster_filter,
96 filter_roster_by_presence, presences, NULL);
98 gtk_builder_connect_signals(builder, NULL);
99 g_object_unref(G_OBJECT(builder));
101 gtk_widget_show(roster_window);
103 [[OFThread threadWithBlock: ^(void){
105 [OFApplication terminate];
112 - (id<XMPPRosterDelegate>)rosterDelegate
117 /* Presence handling */
118 static gboolean refilter_roster(gpointer data)
120 gtk_tree_model_filter_refilter(data);
125 - (void)connection: (XMPPConnection*)connection
126 didReceivePresence: (XMPPPresence*)presence
128 if ([presence.type isEqual: @"available"])
129 [presences addObject: [presence.from bareJID]];
131 [presences removeObject: [presence.from bareJID]];
133 g_idle_add(refilter_roster, roster_filter);
136 /* Roster Delegate methods */
137 struct add_roster_item_param {
141 OFMapTable *groupMap;
142 OFMapTable *contactRows;
143 GtkTreeStore *roster_model;
146 static gboolean add_roster_item(gpointer user_data)
148 struct add_roster_item_param *params = user_data;
149 GtkTreeIter group_iter, contact_iter;
150 GtkTreeRowReference *group_ref, *contact_ref;
151 GtkTreePath *group_path, *contact_path;
153 group_ref = [params->groupMap valueForKey: params->group];
156 // Create new group row
157 gtk_tree_store_append(params->roster_model, &group_iter, NULL);
158 gtk_tree_store_set(params->roster_model, &group_iter,
159 0, [params->group UTF8String], -1);
161 group_path = gtk_tree_model_get_path(GTK_TREE_MODEL(
162 params->roster_model), &group_iter);
164 group_ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(
165 params->roster_model), group_path);
167 [params->groupMap setValue: group_ref
168 forKey: params->group];
170 // Get iter for existing group row
171 group_path = gtk_tree_row_reference_get_path(group_ref);
173 gtk_tree_model_get_iter(GTK_TREE_MODEL(params->roster_model),
174 &group_iter, group_path);
176 gtk_tree_path_free(group_path);
178 // Create new contact row
179 gtk_tree_store_append(params->roster_model, &contact_iter, &group_iter);
180 gtk_tree_store_set(params->roster_model, &contact_iter,
181 0, [params->name UTF8String], 1, [params->jid UTF8String], -1);
183 contact_path = gtk_tree_model_get_path(GTK_TREE_MODEL(
184 params->roster_model), &contact_iter);
186 contact_ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(
187 params->roster_model), contact_path);
189 gtk_tree_path_free(contact_path);
191 [params->contactRows setValue: contact_ref
192 forKey: params->group];
194 [params->group release];
195 [params->name release];
196 [params->jid release];
197 [params->groupMap release];
198 [params->contactRows release];
199 g_object_unref(params->roster_model);
205 struct remove_roster_item_param {
207 OFMapTable *groupMap;
208 OFMapTable *contactRows;
209 GtkTreeStore *roster_model;
212 static gboolean remove_roster_item(gpointer user_data)
214 struct remove_roster_item_param *params = user_data;
215 GtkTreeIter contact_iter, group_iter;
216 GtkTreePath *contact_path, *group_path;
217 GtkTreeRowReference *contact_ref, *group_ref;
219 contact_ref = [params->contactRows valueForKey: params->group];
220 contact_path = gtk_tree_row_reference_get_path(contact_ref);
221 gtk_tree_model_get_iter(GTK_TREE_MODEL(params->roster_model),
222 &contact_iter, contact_path);
224 gtk_tree_store_remove(params->roster_model, &contact_iter);
226 group_ref = [params->groupMap valueForKey: params->group];
227 group_path = gtk_tree_row_reference_get_path(group_ref);
228 gtk_tree_model_get_iter(GTK_TREE_MODEL(params->roster_model),
229 &group_iter, group_path);
231 if (!gtk_tree_model_iter_has_child(GTK_TREE_MODEL(params->roster_model),
233 gtk_tree_store_remove(params->roster_model, &group_iter);
234 [params->groupMap removeValueForKey: params->group];
237 gtk_tree_path_free(group_path);
239 [params->group release];
240 [params->groupMap release];
241 [params->contactRows release];
242 g_object_unref(params->roster_model);
248 - (void)rosterWasReceived: (XMPPRoster*)roster_
250 [[roster_ rosterItems] enumerateKeysAndObjectsUsingBlock:
251 ^(OFString *bareJID, XMPPRosterItem *item, BOOL *stop) {
253 OFMapTable *contactRows = [OFMapTable
254 mapTableWithKeyFunctions: keyFunctions
255 valueFunctions: rowRefFunctions];
257 [contactMap setObject: contactRows
260 if (item.groups != nil)
261 groups = item.groups;
263 groups = @[@"General"];
265 for (OFString *group in groups) {
266 struct add_roster_item_param *params =
267 malloc(sizeof(*params));
268 params->group = [group retain];
269 params->name = [item.name retain];
270 params->jid = [bareJID retain];
271 params->groupMap = [groupMap retain];
272 params->contactRows = [contactRows retain];
273 params->roster_model = g_object_ref(roster_model);
274 g_idle_add(add_roster_item, params);
279 - (void)roster: (XMPPRoster*)roster_
280 didReceiveRosterItem: (XMPPRosterItem*)item
283 XMPPRosterItem *oldItem =
284 [roster_.rosterItems objectForKey: [item.JID bareJID]];
287 if (oldItem.groups != nil)
288 groups = oldItem.groups;
290 groups = @[@"General"];
292 for (OFString *group in groups) {
293 struct remove_roster_item_param *params =
294 malloc(sizeof(*params));
295 params->group = [group retain];
296 params->contactRows = [[contactMap objectForKey:
297 [oldItem.JID bareJID]] retain];
298 params->groupMap = [groupMap retain];
299 params->roster_model = g_object_ref(roster_model);
300 g_idle_add(remove_roster_item, params);
303 [contactMap removeObjectForKey: [item.JID bareJID]];
306 if (![item.subscription isEqual: @"remove"]) {
307 OFMapTable *contactRows = [OFMapTable
308 mapTableWithKeyFunctions: keyFunctions
309 valueFunctions: rowRefFunctions];
311 [contactMap setObject: contactRows
312 forKey: [item.JID bareJID]];
314 if (item.groups != nil)
315 groups = item.groups;
317 groups = @[@"General"];
319 for (OFString *group in groups) {
320 struct add_roster_item_param *params =
321 malloc(sizeof(*params));
322 params->group = [group retain];
323 params->name = [item.name retain];
324 params->jid = [[item.JID bareJID] retain];
325 params->groupMap = [groupMap retain];
326 params->contactRows = [contactRows retain];
327 params->roster_model = g_object_ref(roster_model);
328 g_idle_add(add_roster_item, params);