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 <libempathy/empathy-tp-roomlist.h>
36 #include <libempathy/empathy-chatroom.h>
37 #include <libempathy/empathy-utils.h>
38 #include <libempathy/empathy-dispatcher.h>
40 #include <libempathy-gtk/empathy-account-chooser.h>
41 #include <libempathy-gtk/empathy-ui-utils.h>
43 #include "empathy-new-chatroom-dialog.h"
44 #include "ephy-spinner.h"
46 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
47 #include <libempathy/empathy-debug.h>
50 EmpathyTpRoomlist *room_list;
53 GtkWidget *vbox_widgets;
54 GtkWidget *table_info;
55 GtkWidget *label_account;
56 GtkWidget *account_chooser;
57 GtkWidget *label_server;
58 GtkWidget *entry_server;
59 GtkWidget *label_room;
60 GtkWidget *entry_room;
61 GtkWidget *expander_browse;
65 GtkWidget *button_join;
66 GtkWidget *label_error_message;
67 GtkWidget *viewport_error;
68 } EmpathyNewChatroomDialog;
81 static void new_chatroom_dialog_response_cb (GtkWidget *widget,
83 EmpathyNewChatroomDialog *dialog);
84 static void new_chatroom_dialog_destroy_cb (GtkWidget *widget,
85 EmpathyNewChatroomDialog *dialog);
86 static void new_chatroom_dialog_model_setup (EmpathyNewChatroomDialog *dialog);
87 static void new_chatroom_dialog_model_add_columns (EmpathyNewChatroomDialog *dialog);
88 static void new_chatroom_dialog_update_widgets (EmpathyNewChatroomDialog *dialog);
89 static void new_chatroom_dialog_account_changed_cb (GtkComboBox *combobox,
90 EmpathyNewChatroomDialog *dialog);
91 static void new_chatroom_dialog_roomlist_destroy_cb (EmpathyTpRoomlist *room_list,
92 EmpathyNewChatroomDialog *dialog);
93 static void new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist *room_list,
94 EmpathyChatroom *chatroom,
95 EmpathyNewChatroomDialog *dialog);
96 static void new_chatroom_dialog_listing_cb (EmpathyTpRoomlist *room_list,
98 EmpathyNewChatroomDialog *dialog);
99 static void start_listing_error_cb (EmpathyTpRoomlist *room_list,
101 EmpathyNewChatroomDialog *dialog);
102 static void stop_listing_error_cb (EmpathyTpRoomlist *room_list,
104 EmpathyNewChatroomDialog *dialog);
105 static void new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog);
106 static void new_chatroom_dialog_model_row_activated_cb (GtkTreeView *tree_view,
108 GtkTreeViewColumn *column,
109 EmpathyNewChatroomDialog *dialog);
110 static void new_chatroom_dialog_model_selection_changed (GtkTreeSelection *selection,
111 EmpathyNewChatroomDialog *dialog);
112 static void new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog);
113 static void new_chatroom_dialog_entry_changed_cb (GtkWidget *entry,
114 EmpathyNewChatroomDialog *dialog);
115 static void new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog);
116 static void new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog);
117 static void new_chatroom_dialog_entry_server_activate_cb (GtkWidget *widget,
118 EmpathyNewChatroomDialog *dialog);
119 static void new_chatroom_dialog_expander_browse_activate_cb (GtkWidget *widget,
120 EmpathyNewChatroomDialog *dialog);
121 static gboolean new_chatroom_dialog_entry_server_focus_out_cb (GtkWidget *widget,
122 GdkEventFocus *event,
123 EmpathyNewChatroomDialog *dialog);
124 static void new_chatroom_dialog_button_close_error_clicked_cb (GtkButton *button,
125 EmpathyNewChatroomDialog *dialog);
127 static EmpathyNewChatroomDialog *dialog_p = NULL;
130 empathy_new_chatroom_dialog_show (GtkWindow *parent)
132 EmpathyNewChatroomDialog *dialog;
134 GtkSizeGroup *size_group;
138 gtk_window_present (GTK_WINDOW (dialog_p->window));
142 dialog_p = dialog = g_new0 (EmpathyNewChatroomDialog, 1);
144 filename = empathy_file_lookup ("empathy-new-chatroom-dialog.ui", "src");
145 gui = empathy_builder_get_file (filename,
146 "new_chatroom_dialog", &dialog->window,
147 "table_info", &dialog->table_info,
148 "label_account", &dialog->label_account,
149 "label_server", &dialog->label_server,
150 "label_room", &dialog->label_room,
151 "entry_server", &dialog->entry_server,
152 "entry_room", &dialog->entry_room,
153 "treeview", &dialog->treeview,
154 "button_join", &dialog->button_join,
155 "expander_browse", &dialog->expander_browse,
156 "label_error_message", &dialog->label_error_message,
157 "viewport_error", &dialog->viewport_error,
161 empathy_builder_connect (gui, dialog,
162 "new_chatroom_dialog", "response", new_chatroom_dialog_response_cb,
163 "new_chatroom_dialog", "destroy", new_chatroom_dialog_destroy_cb,
164 "entry_server", "changed", new_chatroom_dialog_entry_changed_cb,
165 "entry_server", "activate", new_chatroom_dialog_entry_server_activate_cb,
166 "entry_server", "focus-out-event", new_chatroom_dialog_entry_server_focus_out_cb,
167 "entry_room", "changed", new_chatroom_dialog_entry_changed_cb,
168 "expander_browse", "activate", new_chatroom_dialog_expander_browse_activate_cb,
169 "button_close_error", "clicked", new_chatroom_dialog_button_close_error_clicked_cb,
172 g_object_unref (gui);
174 g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog_p);
176 /* Label alignment */
177 size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
179 gtk_size_group_add_widget (size_group, dialog->label_account);
180 gtk_size_group_add_widget (size_group, dialog->label_server);
181 gtk_size_group_add_widget (size_group, dialog->label_room);
183 g_object_unref (size_group);
185 /* Set up chatrooms treeview */
186 new_chatroom_dialog_model_setup (dialog);
189 dialog->throbber = ephy_spinner_new ();
190 ephy_spinner_set_size (EPHY_SPINNER (dialog->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
191 gtk_widget_show (dialog->throbber);
192 gtk_table_attach (GTK_TABLE (dialog->table_info),
197 /* Account chooser for custom */
198 dialog->account_chooser = empathy_account_chooser_new ();
199 empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser),
200 empathy_account_chooser_filter_is_connected,
202 gtk_table_attach_defaults (GTK_TABLE (dialog->table_info),
203 dialog->account_chooser,
205 gtk_widget_show (dialog->account_chooser);
207 g_signal_connect (GTK_COMBO_BOX (dialog->account_chooser), "changed",
208 G_CALLBACK (new_chatroom_dialog_account_changed_cb),
210 new_chatroom_dialog_account_changed_cb (GTK_COMBO_BOX (dialog->account_chooser),
214 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
215 GTK_WINDOW (parent));
218 gtk_widget_show (dialog->window);
222 new_chatroom_dialog_response_cb (GtkWidget *widget,
224 EmpathyNewChatroomDialog *dialog)
226 if (response == GTK_RESPONSE_OK) {
227 new_chatroom_dialog_join (dialog);
230 gtk_widget_destroy (widget);
234 new_chatroom_dialog_destroy_cb (GtkWidget *widget,
235 EmpathyNewChatroomDialog *dialog)
237 if (dialog->room_list) {
238 g_object_unref (dialog->room_list);
240 g_object_unref (dialog->model);
246 new_chatroom_dialog_model_setup (EmpathyNewChatroomDialog *dialog)
250 GtkTreeSelection *selection;
253 view = GTK_TREE_VIEW (dialog->treeview);
255 g_signal_connect (view, "row-activated",
256 G_CALLBACK (new_chatroom_dialog_model_row_activated_cb),
260 store = gtk_list_store_new (COL_COUNT,
261 G_TYPE_STRING, /* Invite */
262 G_TYPE_STRING, /* Password */
263 G_TYPE_STRING, /* Name */
264 G_TYPE_STRING, /* Room */
265 G_TYPE_STRING, /* Member count */
266 G_TYPE_INT, /* Member count int */
267 G_TYPE_STRING); /* Tool tip */
269 dialog->model = GTK_TREE_MODEL (store);
270 gtk_tree_view_set_model (view, dialog->model);
271 gtk_tree_view_set_tooltip_column (view, COL_TOOLTIP);
272 gtk_tree_view_set_search_column (view, COL_NAME);
275 selection = gtk_tree_view_get_selection (view);
276 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
277 COL_NAME, GTK_SORT_ASCENDING);
279 g_signal_connect (selection, "changed",
280 G_CALLBACK (new_chatroom_dialog_model_selection_changed),
284 new_chatroom_dialog_model_add_columns (dialog);
288 new_chatroom_dialog_model_add_columns (EmpathyNewChatroomDialog *dialog)
291 GtkTreeViewColumn *column;
292 GtkCellRenderer *cell;
295 gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &width, &height);
297 view = GTK_TREE_VIEW (dialog->treeview);
299 cell = gtk_cell_renderer_pixbuf_new ();
303 "stock-size", GTK_ICON_SIZE_MENU,
305 column = gtk_tree_view_column_new_with_attributes (NULL,
307 "stock-id", COL_INVITE_ONLY,
310 gtk_tree_view_column_set_sort_column_id (column, COL_INVITE_ONLY);
311 gtk_tree_view_append_column (view, column);
313 column = gtk_tree_view_column_new_with_attributes (NULL,
315 "stock-id", COL_NEED_PASSWORD,
318 gtk_tree_view_column_set_sort_column_id (column, COL_NEED_PASSWORD);
319 gtk_tree_view_append_column (view, column);
321 cell = gtk_cell_renderer_text_new ();
325 "ellipsize", PANGO_ELLIPSIZE_END,
328 column = gtk_tree_view_column_new_with_attributes (_("Chat Room"),
333 gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
334 gtk_tree_view_column_set_expand (column, TRUE);
335 gtk_tree_view_append_column (view, column);
337 cell = gtk_cell_renderer_text_new ();
341 "ellipsize", PANGO_ELLIPSIZE_END,
342 "alignment", PANGO_ALIGN_RIGHT,
344 column = gtk_tree_view_column_new_with_attributes (_("Members"),
349 gtk_tree_view_column_set_sort_column_id (column, COL_MEMBERS_INT);
350 gtk_tree_view_append_column (view, column);
354 new_chatroom_dialog_update_widgets (EmpathyNewChatroomDialog *dialog)
356 EmpathyAccountChooser *account_chooser;
358 const gchar *protocol;
361 account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
362 account = empathy_account_chooser_dup_account (account_chooser);
367 protocol = tp_account_get_protocol (account);
369 gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), "");
371 /* hardcode here known protocols */
372 if (strcmp (protocol, "jabber") == 0) {
373 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
375 else if (strcmp (protocol, "local-xmpp") == 0) {
376 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
378 else if (strcmp (protocol, "irc") == 0) {
379 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
382 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
385 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
386 gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
388 /* Final set up of the dialog */
389 gtk_widget_grab_focus (dialog->entry_room);
391 g_object_unref (account);
395 new_chatroom_dialog_account_changed_cb (GtkComboBox *combobox,
396 EmpathyNewChatroomDialog *dialog)
398 EmpathyAccountChooser *account_chooser;
400 gboolean listing = FALSE;
401 gboolean expanded = FALSE;
403 if (dialog->room_list) {
404 g_object_unref (dialog->room_list);
405 dialog->room_list = NULL;
408 ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
409 new_chatroom_dialog_model_clear (dialog);
411 account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
412 account = empathy_account_chooser_dup_account (account_chooser);
416 dialog->room_list = empathy_tp_roomlist_new (account);
418 if (dialog->room_list) {
419 g_signal_connect (dialog->room_list, "destroy",
420 G_CALLBACK (new_chatroom_dialog_roomlist_destroy_cb),
422 g_signal_connect (dialog->room_list, "new-room",
423 G_CALLBACK (new_chatroom_dialog_new_room_cb),
425 g_signal_connect (dialog->room_list, "notify::is-listing",
426 G_CALLBACK (new_chatroom_dialog_listing_cb),
428 g_signal_connect (dialog->room_list, "error::start",
429 G_CALLBACK (start_listing_error_cb),
431 g_signal_connect (dialog->room_list, "error::stop",
432 G_CALLBACK (stop_listing_error_cb),
435 expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
437 gtk_widget_hide (dialog->viewport_error);
438 gtk_widget_set_sensitive (dialog->treeview, TRUE);
439 new_chatroom_dialog_browse_start (dialog);
442 listing = empathy_tp_roomlist_is_listing (dialog->room_list);
444 ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
448 g_object_unref (account);
451 new_chatroom_dialog_update_widgets (dialog);
455 new_chatroom_dialog_button_close_error_clicked_cb (GtkButton *button,
456 EmpathyNewChatroomDialog *dialog)
458 gtk_widget_hide (dialog->viewport_error);
462 new_chatroom_dialog_roomlist_destroy_cb (EmpathyTpRoomlist *room_list,
463 EmpathyNewChatroomDialog *dialog)
465 g_object_unref (dialog->room_list);
466 dialog->room_list = NULL;
470 new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist *room_list,
471 EmpathyChatroom *chatroom,
472 EmpathyNewChatroomDialog *dialog)
475 GtkTreeSelection *selection;
480 const gchar *need_password;
481 const gchar *invite_only;
483 DEBUG ("New chatroom listed: %s (%s)",
484 empathy_chatroom_get_name (chatroom),
485 empathy_chatroom_get_room (chatroom));
488 view = GTK_TREE_VIEW (dialog->treeview);
489 selection = gtk_tree_view_get_selection (view);
490 store = GTK_LIST_STORE (dialog->model);
491 members = g_strdup_printf ("%d", empathy_chatroom_get_members_count (chatroom));
492 tooltip = g_strdup_printf (C_("Room/Join's roomlist tooltip. Parameters"
493 "are a channel name, yes/no, yes/no and a number.",
494 "<b>%s</b>\nInvite required: %s\nPassword required: %s\nMembers: %s"),
495 empathy_chatroom_get_name (chatroom),
496 empathy_chatroom_get_invite_only (chatroom) ? _("Yes") : _("No"),
497 empathy_chatroom_get_need_password (chatroom) ? _("Yes") : _("No"),
499 invite_only = (empathy_chatroom_get_invite_only (chatroom) ?
500 GTK_STOCK_INDEX : NULL);
501 need_password = (empathy_chatroom_get_need_password (chatroom) ?
502 GTK_STOCK_DIALOG_AUTHENTICATION : NULL);
504 gtk_list_store_append (store, &iter);
505 gtk_list_store_set (store, &iter,
506 COL_NEED_PASSWORD, need_password,
507 COL_INVITE_ONLY, invite_only,
508 COL_NAME, empathy_chatroom_get_name (chatroom),
509 COL_ROOM, empathy_chatroom_get_room (chatroom),
510 COL_MEMBERS, members,
511 COL_MEMBERS_INT, empathy_chatroom_get_members_count (chatroom),
512 COL_TOOLTIP, tooltip,
520 start_listing_error_cb (EmpathyTpRoomlist *room_list,
522 EmpathyNewChatroomDialog *dialog)
524 gtk_label_set_text (GTK_LABEL (dialog->label_error_message), _("Could not start room listing"));
525 gtk_widget_show_all (dialog->viewport_error);
526 gtk_widget_set_sensitive (dialog->treeview, FALSE);
530 stop_listing_error_cb (EmpathyTpRoomlist *room_list,
532 EmpathyNewChatroomDialog *dialog)
534 gtk_label_set_text (GTK_LABEL (dialog->label_error_message), _("Could not stop room listing"));
535 gtk_widget_show_all (dialog->viewport_error);
536 gtk_widget_set_sensitive (dialog->treeview, FALSE);
540 new_chatroom_dialog_listing_cb (EmpathyTpRoomlist *room_list,
542 EmpathyNewChatroomDialog *dialog)
546 listing = empathy_tp_roomlist_is_listing (room_list);
548 /* Update the throbber */
550 ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
552 ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
557 new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog)
561 store = GTK_LIST_STORE (dialog->model);
562 gtk_list_store_clear (store);
566 new_chatroom_dialog_model_row_activated_cb (GtkTreeView *tree_view,
568 GtkTreeViewColumn *column,
569 EmpathyNewChatroomDialog *dialog)
571 gtk_widget_activate (dialog->button_join);
575 new_chatroom_dialog_model_selection_changed (GtkTreeSelection *selection,
576 EmpathyNewChatroomDialog *dialog)
581 gchar *server = NULL;
583 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
587 gtk_tree_model_get (model, &iter, COL_ROOM, &room, -1);
588 server = strstr (room, "@");
594 gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), server ? server : "");
595 gtk_entry_set_text (GTK_ENTRY (dialog->entry_room), room ? room : "");
601 new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog)
603 EmpathyAccountChooser *account_chooser;
604 TpConnection *connection;
606 const gchar *server = NULL;
607 gchar *room_name = NULL;
609 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
610 server = gtk_entry_get_text (GTK_ENTRY (dialog->entry_server));
612 account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
613 connection = empathy_account_chooser_get_connection (account_chooser);
615 if (!EMP_STR_EMPTY (server)) {
616 room_name = g_strconcat (room, "@", server, NULL);
618 room_name = g_strdup (room);
621 DEBUG ("Requesting channel for '%s'", room_name);
622 empathy_dispatcher_join_muc (connection, room_name, NULL, NULL);
628 new_chatroom_dialog_entry_changed_cb (GtkWidget *entry,
629 EmpathyNewChatroomDialog *dialog)
631 if (entry == dialog->entry_room) {
634 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
635 gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
636 /* FIXME: Select the room in the list */
641 new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog)
643 new_chatroom_dialog_model_clear (dialog);
644 if (dialog->room_list) {
645 empathy_tp_roomlist_start (dialog->room_list);
650 new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog)
652 if (dialog->room_list) {
653 empathy_tp_roomlist_stop (dialog->room_list);
658 new_chatroom_dialog_entry_server_activate_cb (GtkWidget *widget,
659 EmpathyNewChatroomDialog *dialog)
661 new_chatroom_dialog_browse_start (dialog);
665 new_chatroom_dialog_expander_browse_activate_cb (GtkWidget *widget,
666 EmpathyNewChatroomDialog *dialog)
670 expanded = gtk_expander_get_expanded (GTK_EXPANDER (widget));
672 new_chatroom_dialog_browse_stop (dialog);
673 gtk_window_set_resizable (GTK_WINDOW (dialog->window), FALSE);
675 gtk_widget_hide (dialog->viewport_error);
676 gtk_widget_set_sensitive (dialog->treeview, TRUE);
677 new_chatroom_dialog_browse_start (dialog);
678 gtk_window_set_resizable (GTK_WINDOW (dialog->window), TRUE);
683 new_chatroom_dialog_entry_server_focus_out_cb (GtkWidget *widget,
684 GdkEventFocus *event,
685 EmpathyNewChatroomDialog *dialog)
689 expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
691 new_chatroom_dialog_browse_start (dialog);