1 /**
2  * @file gtkdisco.c GTK+ Service Discovery UI
3  * @ingroup pidgin
4  */
5 
6 /* pidgin
7  *
8  * Pidgin is the legal property of its developers, whose names are too numerous
9  * to list here.  Please refer to the COPYRIGHT file distributed with this
10  * source distribution.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
25  */
26 
27 #include "internal.h"
28 #include "debug.h"
29 #include "gtkutils.h"
30 #include "pidgin.h"
31 #include "request.h"
32 #include "pidgintooltip.h"
33 
34 #include "gtkdisco.h"
35 #include "xmppdisco.h"
36 
37 GList *dialogs = NULL;
38 
39 enum {
40 	PIXBUF_COLUMN = 0,
41 	NAME_COLUMN,
42 	DESCRIPTION_COLUMN,
43 	SERVICE_COLUMN,
44 	NUM_OF_COLUMNS
45 };
46 
47 static void
pidgin_disco_list_destroy(PidginDiscoList * list)48 pidgin_disco_list_destroy(PidginDiscoList *list)
49 {
50 	g_hash_table_destroy(list->services);
51 	if (list->dialog && list->dialog->discolist == list)
52 		list->dialog->discolist = NULL;
53 
54 	if (list->tree) {
55 		gtk_widget_destroy(list->tree);
56 		list->tree = NULL;
57 	}
58 
59 	g_free((gchar*)list->server);
60 	g_free(list);
61 }
62 
pidgin_disco_list_ref(PidginDiscoList * list)63 PidginDiscoList *pidgin_disco_list_ref(PidginDiscoList *list)
64 {
65 	g_return_val_if_fail(list != NULL, NULL);
66 
67 	++list->ref;
68 	purple_debug_misc("xmppdisco", "reffing list, ref count now %d\n", list->ref);
69 
70 	return list;
71 }
72 
pidgin_disco_list_unref(PidginDiscoList * list)73 void pidgin_disco_list_unref(PidginDiscoList *list)
74 {
75 	g_return_if_fail(list != NULL);
76 
77 	--list->ref;
78 
79 	purple_debug_misc("xmppdisco", "unreffing list, ref count now %d\n", list->ref);
80 	if (list->ref == 0)
81 		pidgin_disco_list_destroy(list);
82 }
83 
pidgin_disco_list_set_in_progress(PidginDiscoList * list,gboolean in_progress)84 void pidgin_disco_list_set_in_progress(PidginDiscoList *list, gboolean in_progress)
85 {
86 	PidginDiscoDialog *dialog = list->dialog;
87 
88 	if (!dialog)
89 		return;
90 
91 	list->in_progress = in_progress;
92 
93 	if (in_progress) {
94 		gtk_widget_set_sensitive(dialog->account_widget, FALSE);
95 		gtk_widget_set_sensitive(dialog->stop_button, TRUE);
96 		gtk_widget_set_sensitive(dialog->browse_button, FALSE);
97 	} else {
98 		gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(dialog->progress), 0.0);
99 
100 		gtk_widget_set_sensitive(dialog->account_widget, TRUE);
101 
102 		gtk_widget_set_sensitive(dialog->stop_button, FALSE);
103 		gtk_widget_set_sensitive(dialog->browse_button, TRUE);
104 /*
105 		gtk_widget_set_sensitive(dialog->register_button, FALSE);
106 		gtk_widget_set_sensitive(dialog->add_button, FALSE);
107 */
108 	}
109 }
110 
111 static GdkPixbuf *
pidgin_disco_load_icon(XmppDiscoService * service,const char * size)112 pidgin_disco_load_icon(XmppDiscoService *service, const char *size)
113 {
114 	GdkPixbuf *pixbuf = NULL;
115 	char *filename = NULL;
116 
117 	g_return_val_if_fail(service != NULL, NULL);
118 	g_return_val_if_fail(size != NULL, NULL);
119 
120 	if (service->type == XMPP_DISCO_SERVICE_TYPE_GATEWAY && service->gateway_type) {
121 		char *tmp = g_strconcat(service->gateway_type, ".png", NULL);
122 		filename = g_build_filename(DATADIR, "pixmaps", "pidgin", "protocols", size, tmp, NULL);
123 		g_free(tmp);
124 #if 0
125 	} else if (service->type == XMPP_DISCO_SERVICE_TYPE_USER) {
126 		filename = g_build_filename(DATADIR, "pixmaps", "pidgin", "status", size, "person.png", NULL);
127 #endif
128 	} else if (service->type == XMPP_DISCO_SERVICE_TYPE_CHAT)
129 		filename = g_build_filename(DATADIR, "pixmaps", "pidgin", "status", size, "chat.png", NULL);
130 
131 	if (filename) {
132 		pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
133 		g_free(filename);
134 	}
135 
136 	return pixbuf;
137 }
138 
139 static void pidgin_disco_create_tree(PidginDiscoList *pdl);
140 
dialog_select_account_cb(GObject * w,PurpleAccount * account,PidginDiscoDialog * dialog)141 static void dialog_select_account_cb(GObject *w, PurpleAccount *account,
142                                      PidginDiscoDialog *dialog)
143 {
144 	gboolean change = (account != dialog->account);
145 	dialog->account = account;
146 	gtk_widget_set_sensitive(dialog->browse_button, account != NULL);
147 
148 	if (change && dialog->discolist) {
149 		if (dialog->discolist->tree) {
150 			gtk_widget_destroy(dialog->discolist->tree);
151 			dialog->discolist->tree = NULL;
152 		}
153 		pidgin_disco_list_unref(dialog->discolist);
154 		dialog->discolist = NULL;
155 	}
156 }
157 
register_button_cb(GtkWidget * unused,PidginDiscoDialog * dialog)158 static void register_button_cb(GtkWidget *unused, PidginDiscoDialog *dialog)
159 {
160 	xmpp_disco_service_register(dialog->selected);
161 }
162 
discolist_cancel_cb(PidginDiscoList * pdl,const char * server)163 static void discolist_cancel_cb(PidginDiscoList *pdl, const char *server)
164 {
165 	pdl->dialog->prompt_handle = NULL;
166 
167 	pidgin_disco_list_set_in_progress(pdl, FALSE);
168 	pidgin_disco_list_unref(pdl);
169 }
170 
discolist_ok_cb(PidginDiscoList * pdl,const char * server)171 static void discolist_ok_cb(PidginDiscoList *pdl, const char *server)
172 {
173 	pdl->dialog->prompt_handle = NULL;
174 	gtk_widget_set_sensitive(pdl->dialog->browse_button, TRUE);
175 
176 	if (!server || !*server) {
177 		purple_notify_error(my_plugin, _("Invalid Server"), _("Invalid Server"),
178 		                    NULL);
179 
180 		pidgin_disco_list_set_in_progress(pdl, FALSE);
181 		pidgin_disco_list_unref(pdl);
182 		return;
183 	}
184 
185 	pdl->server = g_strdup(server);
186 	pidgin_disco_list_set_in_progress(pdl, TRUE);
187 	xmpp_disco_start(pdl);
188 }
189 
browse_button_cb(GtkWidget * button,PidginDiscoDialog * dialog)190 static void browse_button_cb(GtkWidget *button, PidginDiscoDialog *dialog)
191 {
192 	PurpleConnection *pc;
193 	PidginDiscoList *pdl;
194 	const char *username;
195 	const char *at, *slash;
196 	char *server = NULL;
197 
198 	pc = purple_account_get_connection(dialog->account);
199 	if (!pc)
200 		return;
201 
202 	gtk_widget_set_sensitive(dialog->browse_button, FALSE);
203 	gtk_widget_set_sensitive(dialog->add_button, FALSE);
204 	gtk_widget_set_sensitive(dialog->register_button, FALSE);
205 
206 	if (dialog->discolist != NULL) {
207 		if (dialog->discolist->tree) {
208 			gtk_widget_destroy(dialog->discolist->tree);
209 			dialog->discolist->tree = NULL;
210 		}
211 		pidgin_disco_list_unref(dialog->discolist);
212 	}
213 
214 	pdl = dialog->discolist = g_new0(PidginDiscoList, 1);
215 	pdl->services = g_hash_table_new_full(NULL, NULL, NULL,
216 			(GDestroyNotify)gtk_tree_row_reference_free);
217 	pdl->pc = pc;
218 	/* We keep a copy... */
219 	pidgin_disco_list_ref(pdl);
220 
221 	pdl->dialog = dialog;
222 	pidgin_disco_create_tree(pdl);
223 
224 	if (dialog->account_widget)
225 		gtk_widget_set_sensitive(dialog->account_widget, FALSE);
226 
227 	username = purple_account_get_username(dialog->account);
228 	at = strchr(username, '@');
229 	slash = strchr(username, '/');
230 	if (at && !slash) {
231 		server = g_strdup_printf("%s", at + 1);
232 	} else if (at && slash && at + 1 < slash) {
233 		server = g_strdup_printf("%.*s", (int)(slash - (at + 1)), at + 1);
234 	}
235 
236 	if (server == NULL)
237 		/* This shouldn't ever happen since the account is connected */
238 		server = g_strdup("jabber.org");
239 
240 	/* Note to translators: The string "Enter an XMPP Server" is asking the
241 	   user to type the name of an XMPP server which will then be queried */
242 	dialog->prompt_handle = purple_request_input(my_plugin, _("Server name request"), _("Enter an XMPP Server"),
243 			_("Select an XMPP server to query"),
244 			server, FALSE, FALSE, NULL,
245 			_("Find Services"), PURPLE_CALLBACK(discolist_ok_cb),
246 			_("Cancel"), PURPLE_CALLBACK(discolist_cancel_cb),
247 			purple_connection_get_account(pc), NULL, NULL, pdl);
248 
249 	g_free(server);
250 }
251 
add_to_blist_cb(GtkWidget * unused,PidginDiscoDialog * dialog)252 static void add_to_blist_cb(GtkWidget *unused, PidginDiscoDialog *dialog)
253 {
254 	XmppDiscoService *service = dialog->selected;
255 	PurpleAccount *account;
256 	const char *jid;
257 
258 	g_return_if_fail(service != NULL);
259 
260 	account = purple_connection_get_account(service->list->pc);
261 	jid = service->jid;
262 
263 	if (service->type == XMPP_DISCO_SERVICE_TYPE_CHAT)
264 		purple_blist_request_add_chat(account, NULL, NULL, jid);
265 	else
266 		purple_blist_request_add_buddy(account, jid, NULL, NULL);
267 }
268 
269 static gboolean
service_click_cb(GtkTreeView * tree,GdkEventButton * event,gpointer user_data)270 service_click_cb(GtkTreeView *tree, GdkEventButton *event, gpointer user_data)
271 {
272 	PidginDiscoList *pdl;
273 	XmppDiscoService *service;
274 	GtkWidget *menu;
275 
276 	GtkTreePath *path;
277 	GtkTreeIter iter;
278 	GValue val;
279 
280 	if (event->button != 3 || event->type != GDK_BUTTON_PRESS)
281 		return FALSE;
282 
283 	pdl = user_data;
284 
285 	/* Figure out what was clicked */
286 	if (!gtk_tree_view_get_path_at_pos(tree, event->x, event->y, &path,
287 		                               NULL, NULL, NULL))
288 		return FALSE;
289 	gtk_tree_model_get_iter(GTK_TREE_MODEL(pdl->model), &iter, path);
290 	gtk_tree_path_free(path);
291 	val.g_type = 0;
292 	gtk_tree_model_get_value(GTK_TREE_MODEL(pdl->model), &iter, SERVICE_COLUMN,
293 	                         &val);
294 	service = g_value_get_pointer(&val);
295 
296 	if (!service)
297 		return FALSE;
298 
299 	menu = gtk_menu_new();
300 
301 	if (service->flags & XMPP_DISCO_ADD)
302 		pidgin_new_item_from_stock(menu, _("Add to Buddy List"), GTK_STOCK_ADD,
303 		                           G_CALLBACK(add_to_blist_cb), pdl->dialog,
304 		                           0, 0, NULL);
305 
306 	if (service->flags & XMPP_DISCO_REGISTER) {
307 		GtkWidget *item = pidgin_new_item(menu, _("Register"));
308 		g_signal_connect(G_OBJECT(item), "activate",
309 		                 G_CALLBACK(register_button_cb), pdl->dialog);
310 	}
311 
312 	gtk_widget_show_all(menu);
313 	gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, event->button,
314 	               event->time);
315 	return FALSE;
316 }
317 
318 static void
selection_changed_cb(GtkTreeSelection * selection,PidginDiscoList * pdl)319 selection_changed_cb(GtkTreeSelection *selection, PidginDiscoList *pdl)
320 {
321 	GtkTreeIter iter;
322 	GValue val;
323 	PidginDiscoDialog *dialog = pdl->dialog;
324 
325 	if (gtk_tree_selection_get_selected(selection, NULL, &iter)) {
326 		val.g_type = 0;
327 		gtk_tree_model_get_value(GTK_TREE_MODEL(pdl->model), &iter, SERVICE_COLUMN, &val);
328 		dialog->selected = g_value_get_pointer(&val);
329 		if (!dialog->selected) {
330 			gtk_widget_set_sensitive(dialog->add_button, FALSE);
331 			gtk_widget_set_sensitive(dialog->register_button, FALSE);
332 			return;
333 		}
334 
335 		gtk_widget_set_sensitive(dialog->add_button, dialog->selected->flags & XMPP_DISCO_ADD);
336 		gtk_widget_set_sensitive(dialog->register_button, dialog->selected->flags & XMPP_DISCO_REGISTER);
337 	} else {
338 		gtk_widget_set_sensitive(dialog->add_button, FALSE);
339 		gtk_widget_set_sensitive(dialog->register_button, FALSE);
340 	}
341 }
342 
343 static void
row_expanded_cb(GtkTreeView * tree,GtkTreeIter * arg1,GtkTreePath * rg2,gpointer user_data)344 row_expanded_cb(GtkTreeView *tree, GtkTreeIter *arg1, GtkTreePath *rg2,
345                 gpointer user_data)
346 {
347 	PidginDiscoList *pdl;
348 	XmppDiscoService *service;
349 	GValue val;
350 
351 	pdl = user_data;
352 
353 	val.g_type = 0;
354 	gtk_tree_model_get_value(GTK_TREE_MODEL(pdl->model), arg1, SERVICE_COLUMN,
355 	                         &val);
356 	service = g_value_get_pointer(&val);
357 	xmpp_disco_service_expand(service);
358 }
359 
360 static void
row_activated_cb(GtkTreeView * tree_view,GtkTreePath * path,GtkTreeViewColumn * column,gpointer user_data)361 row_activated_cb(GtkTreeView       *tree_view,
362                  GtkTreePath       *path,
363                  GtkTreeViewColumn *column,
364                  gpointer           user_data)
365 {
366 	PidginDiscoList *pdl = user_data;
367 	GtkTreeIter iter;
368 	XmppDiscoService *service;
369 	GValue val;
370 
371 	if (!gtk_tree_model_get_iter(GTK_TREE_MODEL(pdl->model), &iter, path))
372 		return;
373 
374 	val.g_type = 0;
375 	gtk_tree_model_get_value(GTK_TREE_MODEL(pdl->model), &iter, SERVICE_COLUMN,
376 	                         &val);
377 	service = g_value_get_pointer(&val);
378 
379 	if (service->flags & XMPP_DISCO_BROWSE)
380 		if (gtk_tree_view_row_expanded(GTK_TREE_VIEW(pdl->tree), path))
381 			gtk_tree_view_collapse_row(GTK_TREE_VIEW(pdl->tree), path);
382 		else
383 			gtk_tree_view_expand_row(GTK_TREE_VIEW(pdl->tree), path, FALSE);
384 	else if (service->flags & XMPP_DISCO_REGISTER)
385 		register_button_cb(NULL, pdl->dialog);
386 	else if (service->flags & XMPP_DISCO_ADD)
387 		add_to_blist_cb(NULL, pdl->dialog);
388 }
389 
390 static void
destroy_win_cb(GtkWidget * window,gpointer d)391 destroy_win_cb(GtkWidget *window, gpointer d)
392 {
393 	PidginDiscoDialog *dialog = d;
394 	PidginDiscoList *list = dialog->discolist;
395 
396 	if (dialog->prompt_handle)
397 		purple_request_close(PURPLE_REQUEST_INPUT, dialog->prompt_handle);
398 
399 	if (list) {
400 		list->dialog = NULL;
401 
402 		if (list->in_progress)
403 			list->in_progress = FALSE;
404 
405 		pidgin_disco_list_unref(list);
406 	}
407 
408 	dialogs = g_list_remove(dialogs, d);
409 	g_free(dialog);
410 }
411 
stop_button_cb(GtkButton * button,PidginDiscoDialog * dialog)412 static void stop_button_cb(GtkButton *button, PidginDiscoDialog *dialog)
413 {
414 	pidgin_disco_list_set_in_progress(dialog->discolist, FALSE);
415 }
416 
close_button_cb(GtkButton * button,PidginDiscoDialog * dialog)417 static void close_button_cb(GtkButton *button, PidginDiscoDialog *dialog)
418 {
419 	GtkWidget *window = dialog->window;
420 
421 	gtk_widget_destroy(window);
422 }
423 
account_filter_func(PurpleAccount * account)424 static gboolean account_filter_func(PurpleAccount *account)
425 {
426 	return purple_strequal(purple_account_get_protocol_id(account), XMPP_PLUGIN_ID);
427 }
428 
429 static gboolean
disco_paint_tooltip(GtkWidget * tipwindow,gpointer data)430 disco_paint_tooltip(GtkWidget *tipwindow, gpointer data)
431 {
432 	PangoLayout *layout = g_object_get_data(G_OBJECT(tipwindow), "tooltip-plugin");
433 #if GTK_CHECK_VERSION(2,14,0)
434 	gtk_paint_layout(gtk_widget_get_style(tipwindow),
435 			gtk_widget_get_window(tipwindow),
436 			GTK_STATE_NORMAL, FALSE,
437 #else
438 	gtk_paint_layout(tipwindow->style, tipwindow->window, GTK_STATE_NORMAL, FALSE,
439 #endif
440 			NULL, tipwindow, "tooltip",
441 			6, 6, layout);
442 	return TRUE;
443 }
444 
445 static gboolean
disco_create_tooltip(GtkWidget * tipwindow,GtkTreePath * path,gpointer data,int * w,int * h)446 disco_create_tooltip(GtkWidget *tipwindow, GtkTreePath *path,
447 		gpointer data, int *w, int *h)
448 {
449 	PidginDiscoList *pdl = data;
450 	GtkTreeIter iter;
451 	PangoLayout *layout;
452 	int width, height;
453 	XmppDiscoService *service;
454 	GValue val;
455 	const char *type = NULL;
456 	char *markup, *jid, *name, *desc = NULL;
457 
458 	if (!gtk_tree_model_get_iter(GTK_TREE_MODEL(pdl->model), &iter, path))
459 		return FALSE;
460 
461 	val.g_type = 0;
462 	gtk_tree_model_get_value(GTK_TREE_MODEL(pdl->model), &iter, SERVICE_COLUMN,
463 	                         &val);
464 	service = g_value_get_pointer(&val);
465 	if (!service)
466 		return FALSE;
467 
468 	switch (service->type) {
469 		case XMPP_DISCO_SERVICE_TYPE_UNSET:
470 			type = _("Unknown");
471 			break;
472 
473 		case XMPP_DISCO_SERVICE_TYPE_GATEWAY:
474 			type = _("Gateway");
475 			break;
476 
477 		case XMPP_DISCO_SERVICE_TYPE_DIRECTORY:
478 			type = _("Directory");
479 			break;
480 
481 		case XMPP_DISCO_SERVICE_TYPE_CHAT:
482 			type = _("Chat");
483 			break;
484 
485 		case XMPP_DISCO_SERVICE_TYPE_PUBSUB_COLLECTION:
486 			type = _("PubSub Collection");
487 			break;
488 
489 		case XMPP_DISCO_SERVICE_TYPE_PUBSUB_LEAF:
490 			type = _("PubSub Leaf");
491 			break;
492 
493 		case XMPP_DISCO_SERVICE_TYPE_OTHER:
494 			type = _("Other");
495 			break;
496 	}
497 
498 	markup = g_strdup_printf("<span size='x-large' weight='bold'>%s</span>\n<b>%s:</b> %s%s%s",
499 	                         name = g_markup_escape_text(service->name, -1),
500 	                         type,
501 	                         jid = g_markup_escape_text(service->jid, -1),
502 	                         service->description ? _("\n<b>Description:</b> ") : "",
503 	                         service->description ? desc = g_markup_escape_text(service->description, -1) : "");
504 
505 	layout = gtk_widget_create_pango_layout(tipwindow, NULL);
506 	pango_layout_set_markup(layout, markup, -1);
507 	pango_layout_set_wrap(layout, PANGO_WRAP_WORD);
508 	pango_layout_set_width(layout, 500000);
509 	pango_layout_get_size(layout, &width, &height);
510 	g_object_set_data_full(G_OBJECT(tipwindow), "tooltip-plugin", layout, g_object_unref);
511 
512 	if (w)
513 		*w = PANGO_PIXELS(width) + 12;
514 	if (h)
515 		*h = PANGO_PIXELS(height) + 12;
516 
517 	g_free(markup);
518 	g_free(jid);
519 	g_free(name);
520 	g_free(desc);
521 
522 	return TRUE;
523 }
524 
pidgin_disco_create_tree(PidginDiscoList * pdl)525 static void pidgin_disco_create_tree(PidginDiscoList *pdl)
526 {
527 	GtkCellRenderer *text_renderer, *pixbuf_renderer;
528 	GtkTreeViewColumn *column;
529 	GtkTreeSelection *selection;
530 
531 	pdl->model = gtk_tree_store_new(NUM_OF_COLUMNS,
532 			GDK_TYPE_PIXBUF,	/* PIXBUF_COLUMN */
533 			G_TYPE_STRING,		/* NAME_COLUMN */
534 			G_TYPE_STRING,		/* DESCRIPTION_COLUMN */
535 			G_TYPE_POINTER		/* SERVICE_COLUMN */
536 	);
537 
538 	pdl->tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(pdl->model));
539 	gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(pdl->tree), TRUE);
540 
541 	selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(pdl->tree));
542 	g_signal_connect(G_OBJECT(selection), "changed",
543 					 G_CALLBACK(selection_changed_cb), pdl);
544 
545 	g_object_unref(pdl->model);
546 
547 	gtk_container_add(GTK_CONTAINER(pdl->dialog->sw), pdl->tree);
548 	gtk_widget_show(pdl->tree);
549 
550 	text_renderer = gtk_cell_renderer_text_new();
551 	pixbuf_renderer = gtk_cell_renderer_pixbuf_new();
552 
553 	column = gtk_tree_view_column_new();
554 	gtk_tree_view_column_set_title(column, _("Name"));
555 
556 	gtk_tree_view_column_pack_start(column, pixbuf_renderer, FALSE);
557 	gtk_tree_view_column_set_attributes(column, pixbuf_renderer,
558 			"pixbuf", PIXBUF_COLUMN, NULL);
559 
560 	gtk_tree_view_column_pack_start(column, text_renderer, TRUE);
561 	gtk_tree_view_column_set_attributes(column, text_renderer,
562 			"text", NAME_COLUMN, NULL);
563 
564 	gtk_tree_view_column_set_sizing(GTK_TREE_VIEW_COLUMN(column),
565 	                                GTK_TREE_VIEW_COLUMN_GROW_ONLY);
566 	gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
567 	gtk_tree_view_column_set_sort_column_id(GTK_TREE_VIEW_COLUMN(column), NAME_COLUMN);
568 	gtk_tree_view_column_set_reorderable(GTK_TREE_VIEW_COLUMN(column), TRUE);
569 	gtk_tree_view_append_column(GTK_TREE_VIEW(pdl->tree), column);
570 
571 	column = gtk_tree_view_column_new_with_attributes(_("Description"), text_renderer,
572 				"text", DESCRIPTION_COLUMN, NULL);
573 	gtk_tree_view_column_set_sizing(GTK_TREE_VIEW_COLUMN(column),
574 	                                GTK_TREE_VIEW_COLUMN_GROW_ONLY);
575 	gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
576 	gtk_tree_view_column_set_sort_column_id(GTK_TREE_VIEW_COLUMN(column), DESCRIPTION_COLUMN);
577 	gtk_tree_view_column_set_reorderable(GTK_TREE_VIEW_COLUMN(column), TRUE);
578 	gtk_tree_view_append_column(GTK_TREE_VIEW(pdl->tree), column);
579 
580 	g_signal_connect(G_OBJECT(pdl->tree), "button-press-event", G_CALLBACK(service_click_cb), pdl);
581 	g_signal_connect(G_OBJECT(pdl->tree), "row-expanded", G_CALLBACK(row_expanded_cb), pdl);
582 	g_signal_connect(G_OBJECT(pdl->tree), "row-activated", G_CALLBACK(row_activated_cb), pdl);
583 
584 	pidgin_tooltip_setup_for_treeview(pdl->tree, pdl,
585 	                                  disco_create_tooltip,
586 	                                  disco_paint_tooltip);
587 }
588 
pidgin_disco_signed_off_cb(PurpleConnection * pc)589 void pidgin_disco_signed_off_cb(PurpleConnection *pc)
590 {
591 	GList *node;
592 
593 	for (node = dialogs; node; node = node->next) {
594 		PidginDiscoDialog *dialog = node->data;
595 		PidginDiscoList *list = dialog->discolist;
596 
597 		if (list && list->pc == pc) {
598 			if (list->in_progress)
599 				pidgin_disco_list_set_in_progress(list, FALSE);
600 
601 			if (list->tree) {
602 				gtk_widget_destroy(list->tree);
603 				list->tree = NULL;
604 			}
605 
606 			pidgin_disco_list_unref(list);
607 			dialog->discolist = NULL;
608 
609 			gtk_widget_set_sensitive(dialog->browse_button,
610 					pidgin_account_option_menu_get_selected(dialog->account_widget) != NULL);
611 
612 			gtk_widget_set_sensitive(dialog->register_button, FALSE);
613 			gtk_widget_set_sensitive(dialog->add_button, FALSE);
614 		}
615 	}
616 }
617 
pidgin_disco_dialogs_destroy_all(void)618 void pidgin_disco_dialogs_destroy_all(void)
619 {
620 	while (dialogs) {
621 		PidginDiscoDialog *dialog = dialogs->data;
622 
623 		gtk_widget_destroy(dialog->window);
624 		/* destroy_win_cb removes the dialog from the list */
625 	}
626 }
627 
pidgin_disco_dialog_new(void)628 PidginDiscoDialog *pidgin_disco_dialog_new(void)
629 {
630 	PidginDiscoDialog *dialog;
631 	GtkWidget *window, *vbox, *vbox2, *bbox;
632 
633 	dialog = g_new0(PidginDiscoDialog, 1);
634 	dialogs = g_list_prepend(dialogs, dialog);
635 
636 	/* Create the window. */
637 	dialog->window = window = pidgin_create_dialog(_("Service Discovery"), PIDGIN_HIG_BORDER, "service discovery", TRUE);
638 
639 	g_signal_connect(G_OBJECT(window), "destroy",
640 					 G_CALLBACK(destroy_win_cb), dialog);
641 
642 	/* Create the parent vbox for everything. */
643 	vbox = pidgin_dialog_get_vbox_with_properties(GTK_DIALOG(window), FALSE, PIDGIN_HIG_BORDER);
644 
645 	vbox2 = gtk_vbox_new(FALSE, PIDGIN_HIG_BORDER);
646 	gtk_container_add(GTK_CONTAINER(vbox), vbox2);
647 	gtk_widget_show(vbox2);
648 
649 	/* accounts dropdown list */
650 	dialog->account_widget = pidgin_account_option_menu_new(NULL, FALSE,
651 	                         G_CALLBACK(dialog_select_account_cb), account_filter_func, dialog);
652 	dialog->account = pidgin_account_option_menu_get_selected(dialog->account_widget);
653 	pidgin_add_widget_to_vbox(GTK_BOX(vbox2), _("_Account:"), NULL, dialog->account_widget, TRUE, NULL);
654 
655 	/* scrolled window */
656 	dialog->sw = pidgin_make_scrollable(NULL, GTK_POLICY_ALWAYS, GTK_POLICY_ALWAYS, GTK_SHADOW_IN, -1, 250);
657 	gtk_box_pack_start(GTK_BOX(vbox2), dialog->sw, TRUE, TRUE, 0);
658 
659 	/* progress bar */
660 	dialog->progress = gtk_progress_bar_new();
661 	gtk_progress_bar_set_pulse_step(GTK_PROGRESS_BAR(dialog->progress), 0.1);
662 	gtk_box_pack_start(GTK_BOX(vbox2), dialog->progress, FALSE, FALSE, 0);
663 	gtk_widget_show(dialog->progress);
664 
665 	/* button box */
666 	bbox = pidgin_dialog_get_action_area(GTK_DIALOG(window));
667 	gtk_box_set_spacing(GTK_BOX(bbox), PIDGIN_HIG_BOX_SPACE);
668 	gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_END);
669 
670 	/* stop button */
671 	dialog->stop_button =
672 		pidgin_dialog_add_button(GTK_DIALOG(window), GTK_STOCK_STOP,
673 		                         G_CALLBACK(stop_button_cb), dialog);
674 	gtk_widget_set_sensitive(dialog->stop_button, FALSE);
675 
676 	/* browse button */
677 	dialog->browse_button =
678 		pidgin_pixbuf_button_from_stock(_("_Browse"), GTK_STOCK_REFRESH,
679 		                                PIDGIN_BUTTON_HORIZONTAL);
680 	gtk_box_pack_start(GTK_BOX(bbox), dialog->browse_button, FALSE, FALSE, 0);
681 	g_signal_connect(G_OBJECT(dialog->browse_button), "clicked",
682 	                 G_CALLBACK(browse_button_cb), dialog);
683 	gtk_widget_set_sensitive(dialog->browse_button, dialog->account != NULL);
684 	gtk_widget_show(dialog->browse_button);
685 
686 	/* register button */
687 	dialog->register_button =
688 		pidgin_dialog_add_button(GTK_DIALOG(dialog->window), _("Register"),
689 		                         G_CALLBACK(register_button_cb), dialog);
690 	gtk_widget_set_sensitive(dialog->register_button, FALSE);
691 
692 	/* add button */
693 	dialog->add_button =
694 		pidgin_pixbuf_button_from_stock(_("_Add"), GTK_STOCK_ADD,
695 	                                    PIDGIN_BUTTON_HORIZONTAL);
696 	gtk_box_pack_start(GTK_BOX(bbox), dialog->add_button, FALSE, FALSE, 0);
697 	g_signal_connect(G_OBJECT(dialog->add_button), "clicked",
698 	                 G_CALLBACK(add_to_blist_cb), dialog);
699 	gtk_widget_set_sensitive(dialog->add_button, FALSE);
700 	gtk_widget_show(dialog->add_button);
701 
702 	/* close button */
703 	dialog->close_button =
704 		pidgin_dialog_add_button(GTK_DIALOG(window), GTK_STOCK_CLOSE,
705 		                         G_CALLBACK(close_button_cb), dialog);
706 
707 	/* show the dialog window and return the dialog */
708 	gtk_widget_show(dialog->window);
709 
710 	return dialog;
711 }
712 
pidgin_disco_add_service(PidginDiscoList * pdl,XmppDiscoService * service,XmppDiscoService * parent)713 void pidgin_disco_add_service(PidginDiscoList *pdl, XmppDiscoService *service, XmppDiscoService *parent)
714 {
715 	PidginDiscoDialog *dialog;
716 	GtkTreeIter iter, parent_iter, child;
717 	GdkPixbuf *pixbuf = NULL;
718 	gboolean append = TRUE;
719 
720 	dialog = pdl->dialog;
721 	g_return_if_fail(dialog != NULL);
722 
723 	if (service != NULL)
724 		purple_debug_info("xmppdisco", "Adding service \"%s\"\n", service->name);
725 	else
726 		purple_debug_info("xmppdisco", "Service \"%s\" has no childrens\n", parent->name);
727 
728 	gtk_progress_bar_pulse(GTK_PROGRESS_BAR(dialog->progress));
729 
730 	if (parent) {
731 		GtkTreeRowReference *rr;
732 		GtkTreePath *path;
733 
734 		rr = g_hash_table_lookup(pdl->services, parent);
735 		path = gtk_tree_row_reference_get_path(rr);
736 		if (path) {
737 			gtk_tree_model_get_iter(GTK_TREE_MODEL(pdl->model), &parent_iter, path);
738 			gtk_tree_path_free(path);
739 
740 			if (gtk_tree_model_iter_children(GTK_TREE_MODEL(pdl->model), &child,
741 			                                 &parent_iter)) {
742 				PidginDiscoList *tmp;
743 				gtk_tree_model_get(GTK_TREE_MODEL(pdl->model), &child,
744 				                   SERVICE_COLUMN, &tmp, -1);
745 				if (!tmp)
746 					append = FALSE;
747 			}
748 		}
749 	}
750 
751 	if (service == NULL) {
752 		if (parent != NULL && !append)
753 			gtk_tree_store_remove(pdl->model, &child);
754 		return;
755 	}
756 
757 	if (append)
758 		gtk_tree_store_append(pdl->model, &iter, (parent ? &parent_iter : NULL));
759 	else
760 		iter = child;
761 
762 	if (service->flags & XMPP_DISCO_BROWSE) {
763 		GtkTreeRowReference *rr;
764 		GtkTreePath *path;
765 
766 		gtk_tree_store_append(pdl->model, &child, &iter);
767 
768 		path = gtk_tree_model_get_path(GTK_TREE_MODEL(pdl->model), &iter);
769 		rr = gtk_tree_row_reference_new(GTK_TREE_MODEL(pdl->model), path);
770 		g_hash_table_insert(pdl->services, service, rr);
771 		gtk_tree_path_free(path);
772 	}
773 
774 	pixbuf = pidgin_disco_load_icon(service, "16");
775 
776 	gtk_tree_store_set(pdl->model, &iter,
777 			PIXBUF_COLUMN, pixbuf,
778 			NAME_COLUMN, service->name,
779 			DESCRIPTION_COLUMN, service->description,
780 			SERVICE_COLUMN, service,
781 			-1);
782 
783 	if (pixbuf)
784 		g_object_unref(pixbuf);
785 }
786 
787