1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2006-2007 Imendio AB
4 * Copyright (C) 2007-2008 Collabora Ltd.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301 USA
21 * Authors: Martyn Russell <martyn@imendio.com>
22 * Xavier Claessens <xclaesse@gmail.com>
32 #include <glib/gi18n.h>
33 #include <glib/gprintf.h>
35 #include <libmissioncontrol/mission-control.h>
36 #include <libmissioncontrol/mc-account.h>
37 #include <libmissioncontrol/mc-profile.h>
39 #include <libempathy/empathy-tp-roomlist.h>
40 #include <libempathy/empathy-chatroom.h>
41 #include <libempathy/empathy-utils.h>
42 #include <libempathy/empathy-dispatcher.h>
44 #include <libempathy-gtk/empathy-account-chooser.h>
45 #include <libempathy-gtk/empathy-ui-utils.h>
47 #include "empathy-new-chatroom-dialog.h"
48 #include "ephy-spinner.h"
50 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
51 #include <libempathy/empathy-debug.h>
54 EmpathyTpRoomlist *room_list;
57 GtkWidget *vbox_widgets;
58 GtkWidget *table_info;
59 GtkWidget *label_account;
60 GtkWidget *account_chooser;
61 GtkWidget *label_server;
62 GtkWidget *entry_server;
63 GtkWidget *label_room;
64 GtkWidget *entry_room;
65 GtkWidget *expander_browse;
69 GtkWidget *button_join;
70 GtkWidget *label_error_message;
71 GtkWidget *viewport_error;
72 } EmpathyNewChatroomDialog;
85 static void new_chatroom_dialog_response_cb (GtkWidget *widget,
87 EmpathyNewChatroomDialog *dialog);
88 static void new_chatroom_dialog_destroy_cb (GtkWidget *widget,
89 EmpathyNewChatroomDialog *dialog);
90 static void new_chatroom_dialog_model_setup (EmpathyNewChatroomDialog *dialog);
91 static void new_chatroom_dialog_model_add_columns (EmpathyNewChatroomDialog *dialog);
92 static void new_chatroom_dialog_update_widgets (EmpathyNewChatroomDialog *dialog);
93 static void new_chatroom_dialog_account_changed_cb (GtkComboBox *combobox,
94 EmpathyNewChatroomDialog *dialog);
95 static void new_chatroom_dialog_roomlist_destroy_cb (EmpathyTpRoomlist *room_list,
96 EmpathyNewChatroomDialog *dialog);
97 static void new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist *room_list,
98 EmpathyChatroom *chatroom,
99 EmpathyNewChatroomDialog *dialog);
100 static void new_chatroom_dialog_listing_cb (EmpathyTpRoomlist *room_list,
102 EmpathyNewChatroomDialog *dialog);
103 static void start_listing_error_cb (EmpathyTpRoomlist *room_list,
105 EmpathyNewChatroomDialog *dialog);
106 static void stop_listing_error_cb (EmpathyTpRoomlist *room_list,
108 EmpathyNewChatroomDialog *dialog);
109 static void new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog);
110 static void new_chatroom_dialog_model_row_activated_cb (GtkTreeView *tree_view,
112 GtkTreeViewColumn *column,
113 EmpathyNewChatroomDialog *dialog);
114 static void new_chatroom_dialog_model_selection_changed (GtkTreeSelection *selection,
115 EmpathyNewChatroomDialog *dialog);
116 static void new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog);
117 static void new_chatroom_dialog_entry_changed_cb (GtkWidget *entry,
118 EmpathyNewChatroomDialog *dialog);
119 static void new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog);
120 static void new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog);
121 static void new_chatroom_dialog_entry_server_activate_cb (GtkWidget *widget,
122 EmpathyNewChatroomDialog *dialog);
123 static void new_chatroom_dialog_expander_browse_activate_cb (GtkWidget *widget,
124 EmpathyNewChatroomDialog *dialog);
125 static gboolean new_chatroom_dialog_entry_server_focus_out_cb (GtkWidget *widget,
126 GdkEventFocus *event,
127 EmpathyNewChatroomDialog *dialog);
128 static void new_chatroom_dialog_button_close_error_clicked_cb (GtkButton *button,
129 EmpathyNewChatroomDialog *dialog);
131 static EmpathyNewChatroomDialog *dialog_p = NULL;
134 empathy_new_chatroom_dialog_show (GtkWindow *parent)
136 EmpathyNewChatroomDialog *dialog;
138 GtkSizeGroup *size_group;
142 gtk_window_present (GTK_WINDOW (dialog_p->window));
146 dialog_p = dialog = g_new0 (EmpathyNewChatroomDialog, 1);
148 filename = empathy_file_lookup ("empathy-new-chatroom-dialog.ui", "src");
149 gui = empathy_builder_get_file (filename,
150 "new_chatroom_dialog", &dialog->window,
151 "table_info", &dialog->table_info,
152 "label_account", &dialog->label_account,
153 "label_server", &dialog->label_server,
154 "label_room", &dialog->label_room,
155 "entry_server", &dialog->entry_server,
156 "entry_room", &dialog->entry_room,
157 "treeview", &dialog->treeview,
158 "button_join", &dialog->button_join,
159 "expander_browse", &dialog->expander_browse,
160 "label_error_message", &dialog->label_error_message,
161 "viewport_error", &dialog->viewport_error,
165 empathy_builder_connect (gui, dialog,
166 "new_chatroom_dialog", "response", new_chatroom_dialog_response_cb,
167 "new_chatroom_dialog", "destroy", new_chatroom_dialog_destroy_cb,
168 "entry_server", "changed", new_chatroom_dialog_entry_changed_cb,
169 "entry_server", "activate", new_chatroom_dialog_entry_server_activate_cb,
170 "entry_server", "focus-out-event", new_chatroom_dialog_entry_server_focus_out_cb,
171 "entry_room", "changed", new_chatroom_dialog_entry_changed_cb,
172 "expander_browse", "activate", new_chatroom_dialog_expander_browse_activate_cb,
173 "button_close_error", "clicked", new_chatroom_dialog_button_close_error_clicked_cb,
176 g_object_unref (gui);
178 g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog_p);
180 /* Label alignment */
181 size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
183 gtk_size_group_add_widget (size_group, dialog->label_account);
184 gtk_size_group_add_widget (size_group, dialog->label_server);
185 gtk_size_group_add_widget (size_group, dialog->label_room);
187 g_object_unref (size_group);
189 /* Set up chatrooms treeview */
190 new_chatroom_dialog_model_setup (dialog);
193 dialog->throbber = ephy_spinner_new ();
194 ephy_spinner_set_size (EPHY_SPINNER (dialog->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
195 gtk_widget_show (dialog->throbber);
196 gtk_table_attach (GTK_TABLE (dialog->table_info),
201 /* Account chooser for custom */
202 dialog->account_chooser = empathy_account_chooser_new ();
203 empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser),
204 empathy_account_chooser_filter_is_connected,
206 gtk_table_attach_defaults (GTK_TABLE (dialog->table_info),
207 dialog->account_chooser,
209 gtk_widget_show (dialog->account_chooser);
211 g_signal_connect (GTK_COMBO_BOX (dialog->account_chooser), "changed",
212 G_CALLBACK (new_chatroom_dialog_account_changed_cb),
214 new_chatroom_dialog_account_changed_cb (GTK_COMBO_BOX (dialog->account_chooser),
218 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
219 GTK_WINDOW (parent));
222 gtk_widget_show (dialog->window);
226 new_chatroom_dialog_response_cb (GtkWidget *widget,
228 EmpathyNewChatroomDialog *dialog)
230 if (response == GTK_RESPONSE_OK) {
231 new_chatroom_dialog_join (dialog);
234 gtk_widget_destroy (widget);
238 new_chatroom_dialog_destroy_cb (GtkWidget *widget,
239 EmpathyNewChatroomDialog *dialog)
241 if (dialog->room_list) {
242 g_object_unref (dialog->room_list);
244 g_object_unref (dialog->model);
250 new_chatroom_dialog_model_setup (EmpathyNewChatroomDialog *dialog)
254 GtkTreeSelection *selection;
257 view = GTK_TREE_VIEW (dialog->treeview);
259 g_signal_connect (view, "row-activated",
260 G_CALLBACK (new_chatroom_dialog_model_row_activated_cb),
264 store = gtk_list_store_new (COL_COUNT,
265 G_TYPE_STRING, /* Invite */
266 G_TYPE_STRING, /* Password */
267 G_TYPE_STRING, /* Name */
268 G_TYPE_STRING, /* Room */
269 G_TYPE_STRING, /* Member count */
270 G_TYPE_INT, /* Member count int */
271 G_TYPE_STRING); /* Tool tip */
273 dialog->model = GTK_TREE_MODEL (store);
274 gtk_tree_view_set_model (view, dialog->model);
275 gtk_tree_view_set_tooltip_column (view, COL_TOOLTIP);
276 gtk_tree_view_set_search_column (view, COL_NAME);
279 selection = gtk_tree_view_get_selection (view);
280 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
281 COL_NAME, GTK_SORT_ASCENDING);
283 g_signal_connect (selection, "changed",
284 G_CALLBACK (new_chatroom_dialog_model_selection_changed),
288 new_chatroom_dialog_model_add_columns (dialog);
292 new_chatroom_dialog_model_add_columns (EmpathyNewChatroomDialog *dialog)
295 GtkTreeViewColumn *column;
296 GtkCellRenderer *cell;
299 gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &width, &height);
301 view = GTK_TREE_VIEW (dialog->treeview);
303 cell = gtk_cell_renderer_pixbuf_new ();
307 "stock-size", GTK_ICON_SIZE_MENU,
309 column = gtk_tree_view_column_new_with_attributes (NULL,
311 "stock-id", COL_INVITE_ONLY,
314 gtk_tree_view_column_set_sort_column_id (column, COL_INVITE_ONLY);
315 gtk_tree_view_append_column (view, column);
317 column = gtk_tree_view_column_new_with_attributes (NULL,
319 "stock-id", COL_NEED_PASSWORD,
322 gtk_tree_view_column_set_sort_column_id (column, COL_NEED_PASSWORD);
323 gtk_tree_view_append_column (view, column);
325 cell = gtk_cell_renderer_text_new ();
329 "ellipsize", PANGO_ELLIPSIZE_END,
332 column = gtk_tree_view_column_new_with_attributes (_("Chat Room"),
337 gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
338 gtk_tree_view_column_set_expand (column, TRUE);
339 gtk_tree_view_append_column (view, column);
341 cell = gtk_cell_renderer_text_new ();
345 "ellipsize", PANGO_ELLIPSIZE_END,
346 "alignment", PANGO_ALIGN_RIGHT,
348 column = gtk_tree_view_column_new_with_attributes (_("Members"),
353 gtk_tree_view_column_set_sort_column_id (column, COL_MEMBERS_INT);
354 gtk_tree_view_append_column (view, column);
358 new_chatroom_dialog_update_widgets (EmpathyNewChatroomDialog *dialog)
360 EmpathyAccountChooser *account_chooser;
363 const gchar *protocol;
366 account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
367 account = empathy_account_chooser_dup_account (account_chooser);
368 profile = mc_account_get_profile (account);
369 protocol = mc_profile_get_protocol_name (profile);
371 gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), "");
373 /* hardcode here known protocols */
374 if (strcmp (protocol, "jabber") == 0) {
375 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
377 else if (strcmp (protocol, "local-xmpp") == 0) {
378 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
380 else if (strcmp (protocol, "irc") == 0) {
381 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
384 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
387 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
388 gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
390 /* Final set up of the dialog */
391 gtk_widget_grab_focus (dialog->entry_room);
393 g_object_unref (account);
394 g_object_unref (profile);
398 new_chatroom_dialog_account_changed_cb (GtkComboBox *combobox,
399 EmpathyNewChatroomDialog *dialog)
401 EmpathyAccountChooser *account_chooser;
403 gboolean listing = FALSE;
404 gboolean expanded = FALSE;
406 if (dialog->room_list) {
407 g_object_unref (dialog->room_list);
410 ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
411 new_chatroom_dialog_model_clear (dialog);
413 account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
414 account = empathy_account_chooser_dup_account (account_chooser);
415 dialog->room_list = empathy_tp_roomlist_new (account);
417 if (dialog->room_list) {
418 g_signal_connect (dialog->room_list, "destroy",
419 G_CALLBACK (new_chatroom_dialog_roomlist_destroy_cb),
421 g_signal_connect (dialog->room_list, "new-room",
422 G_CALLBACK (new_chatroom_dialog_new_room_cb),
424 g_signal_connect (dialog->room_list, "notify::is-listing",
425 G_CALLBACK (new_chatroom_dialog_listing_cb),
427 g_signal_connect (dialog->room_list, "error::start",
428 G_CALLBACK (start_listing_error_cb),
430 g_signal_connect (dialog->room_list, "error::stop",
431 G_CALLBACK (stop_listing_error_cb),
434 expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
436 gtk_widget_hide (dialog->viewport_error);
437 gtk_widget_set_sensitive (dialog->treeview, TRUE);
438 new_chatroom_dialog_browse_start (dialog);
441 listing = empathy_tp_roomlist_is_listing (dialog->room_list);
443 ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
447 new_chatroom_dialog_update_widgets (dialog);
449 g_object_unref (account);
453 new_chatroom_dialog_button_close_error_clicked_cb (GtkButton *button,
454 EmpathyNewChatroomDialog *dialog)
456 gtk_widget_hide (dialog->viewport_error);
460 new_chatroom_dialog_roomlist_destroy_cb (EmpathyTpRoomlist *room_list,
461 EmpathyNewChatroomDialog *dialog)
463 g_object_unref (dialog->room_list);
464 dialog->room_list = NULL;
468 new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist *room_list,
469 EmpathyChatroom *chatroom,
470 EmpathyNewChatroomDialog *dialog)
473 GtkTreeSelection *selection;
478 const gchar *need_password;
479 const gchar *invite_only;
481 DEBUG ("New chatroom listed: %s (%s)",
482 empathy_chatroom_get_name (chatroom),
483 empathy_chatroom_get_room (chatroom));
486 view = GTK_TREE_VIEW (dialog->treeview);
487 selection = gtk_tree_view_get_selection (view);
488 store = GTK_LIST_STORE (dialog->model);
489 members = g_strdup_printf ("%d", empathy_chatroom_get_members_count (chatroom));
490 tooltip = g_strdup_printf (C_("Room/Join's roomlist tooltip. Parameters"
491 "are a channel name, yes/no, yes/no and a number.",
492 "<b>%s</b>\nInvite required: %s\nPassword required: %s\nMembers: %s"),
493 empathy_chatroom_get_name (chatroom),
494 empathy_chatroom_get_invite_only (chatroom) ? _("Yes") : _("No"),
495 empathy_chatroom_get_need_password (chatroom) ? _("Yes") : _("No"),
497 invite_only = (empathy_chatroom_get_invite_only (chatroom) ?
498 GTK_STOCK_INDEX : NULL);
499 need_password = (empathy_chatroom_get_need_password (chatroom) ?
500 GTK_STOCK_DIALOG_AUTHENTICATION : NULL);
502 gtk_list_store_append (store, &iter);
503 gtk_list_store_set (store, &iter,
504 COL_NEED_PASSWORD, need_password,
505 COL_INVITE_ONLY, invite_only,
506 COL_NAME, empathy_chatroom_get_name (chatroom),
507 COL_ROOM, empathy_chatroom_get_room (chatroom),
508 COL_MEMBERS, members,
509 COL_MEMBERS_INT, empathy_chatroom_get_members_count (chatroom),
510 COL_TOOLTIP, tooltip,
518 start_listing_error_cb (EmpathyTpRoomlist *room_list,
520 EmpathyNewChatroomDialog *dialog)
522 gtk_label_set_text (GTK_LABEL (dialog->label_error_message), _("Could not start room listing"));
523 gtk_widget_show_all (dialog->viewport_error);
524 gtk_widget_set_sensitive (dialog->treeview, FALSE);
528 stop_listing_error_cb (EmpathyTpRoomlist *room_list,
530 EmpathyNewChatroomDialog *dialog)
532 gtk_label_set_text (GTK_LABEL (dialog->label_error_message), _("Could not stop room listing"));
533 gtk_widget_show_all (dialog->viewport_error);
534 gtk_widget_set_sensitive (dialog->treeview, FALSE);
538 new_chatroom_dialog_listing_cb (EmpathyTpRoomlist *room_list,
540 EmpathyNewChatroomDialog *dialog)
544 listing = empathy_tp_roomlist_is_listing (room_list);
546 /* Update the throbber */
548 ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
550 ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
555 new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog)
559 store = GTK_LIST_STORE (dialog->model);
560 gtk_list_store_clear (store);
564 new_chatroom_dialog_model_row_activated_cb (GtkTreeView *tree_view,
566 GtkTreeViewColumn *column,
567 EmpathyNewChatroomDialog *dialog)
569 gtk_widget_activate (dialog->button_join);
573 new_chatroom_dialog_model_selection_changed (GtkTreeSelection *selection,
574 EmpathyNewChatroomDialog *dialog)
579 gchar *server = NULL;
581 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
585 gtk_tree_model_get (model, &iter, COL_ROOM, &room, -1);
586 server = strstr (room, "@");
592 gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), server ? server : "");
593 gtk_entry_set_text (GTK_ENTRY (dialog->entry_room), room ? room : "");
599 new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog)
601 EmpathyAccountChooser *account_chooser;
602 TpConnection *connection;
604 const gchar *server = NULL;
605 gchar *room_name = NULL;
607 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
608 server = gtk_entry_get_text (GTK_ENTRY (dialog->entry_server));
610 account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
611 connection = empathy_account_chooser_get_connection (account_chooser);
613 if (!EMP_STR_EMPTY (server)) {
614 room_name = g_strconcat (room, "@", server, NULL);
616 room_name = g_strdup (room);
619 DEBUG ("Requesting channel for '%s'", room_name);
620 empathy_dispatcher_join_muc (connection, room_name, NULL, NULL);
626 new_chatroom_dialog_entry_changed_cb (GtkWidget *entry,
627 EmpathyNewChatroomDialog *dialog)
629 if (entry == dialog->entry_room) {
632 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
633 gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
634 /* FIXME: Select the room in the list */
639 new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog)
641 new_chatroom_dialog_model_clear (dialog);
642 if (dialog->room_list) {
643 empathy_tp_roomlist_start (dialog->room_list);
648 new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog)
650 if (dialog->room_list) {
651 empathy_tp_roomlist_stop (dialog->room_list);
656 new_chatroom_dialog_entry_server_activate_cb (GtkWidget *widget,
657 EmpathyNewChatroomDialog *dialog)
659 new_chatroom_dialog_browse_start (dialog);
663 new_chatroom_dialog_expander_browse_activate_cb (GtkWidget *widget,
664 EmpathyNewChatroomDialog *dialog)
668 expanded = gtk_expander_get_expanded (GTK_EXPANDER (widget));
670 new_chatroom_dialog_browse_stop (dialog);
671 gtk_window_set_resizable (GTK_WINDOW (dialog->window), FALSE);
673 gtk_widget_hide (dialog->viewport_error);
674 gtk_widget_set_sensitive (dialog->treeview, TRUE);
675 new_chatroom_dialog_browse_start (dialog);
676 gtk_window_set_resizable (GTK_WINDOW (dialog->window), TRUE);
681 new_chatroom_dialog_entry_server_focus_out_cb (GtkWidget *widget,
682 GdkEventFocus *event,
683 EmpathyNewChatroomDialog *dialog)
687 expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
689 new_chatroom_dialog_browse_start (dialog);