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-profile.h>
38 #include <libempathy/empathy-tp-roomlist.h>
39 #include <libempathy/empathy-chatroom.h>
40 #include <libempathy/empathy-utils.h>
41 #include <libempathy/empathy-dispatcher.h>
43 #include <libempathy-gtk/empathy-account-chooser.h>
44 #include <libempathy-gtk/empathy-ui-utils.h>
46 #include "empathy-new-chatroom-dialog.h"
47 #include "ephy-spinner.h"
49 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
50 #include <libempathy/empathy-debug.h>
53 EmpathyTpRoomlist *room_list;
56 GtkWidget *vbox_widgets;
57 GtkWidget *table_info;
58 GtkWidget *label_account;
59 GtkWidget *account_chooser;
60 GtkWidget *label_server;
61 GtkWidget *entry_server;
62 GtkWidget *label_room;
63 GtkWidget *entry_room;
64 GtkWidget *expander_browse;
68 GtkWidget *button_join;
69 GtkWidget *label_error_message;
70 GtkWidget *viewport_error;
71 } EmpathyNewChatroomDialog;
84 static void new_chatroom_dialog_response_cb (GtkWidget *widget,
86 EmpathyNewChatroomDialog *dialog);
87 static void new_chatroom_dialog_destroy_cb (GtkWidget *widget,
88 EmpathyNewChatroomDialog *dialog);
89 static void new_chatroom_dialog_model_setup (EmpathyNewChatroomDialog *dialog);
90 static void new_chatroom_dialog_model_add_columns (EmpathyNewChatroomDialog *dialog);
91 static void new_chatroom_dialog_update_widgets (EmpathyNewChatroomDialog *dialog);
92 static void new_chatroom_dialog_account_changed_cb (GtkComboBox *combobox,
93 EmpathyNewChatroomDialog *dialog);
94 static void new_chatroom_dialog_roomlist_destroy_cb (EmpathyTpRoomlist *room_list,
95 EmpathyNewChatroomDialog *dialog);
96 static void new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist *room_list,
97 EmpathyChatroom *chatroom,
98 EmpathyNewChatroomDialog *dialog);
99 static void new_chatroom_dialog_listing_cb (EmpathyTpRoomlist *room_list,
101 EmpathyNewChatroomDialog *dialog);
102 static void start_listing_error_cb (EmpathyTpRoomlist *room_list,
104 EmpathyNewChatroomDialog *dialog);
105 static void stop_listing_error_cb (EmpathyTpRoomlist *room_list,
107 EmpathyNewChatroomDialog *dialog);
108 static void new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog);
109 static void new_chatroom_dialog_model_row_activated_cb (GtkTreeView *tree_view,
111 GtkTreeViewColumn *column,
112 EmpathyNewChatroomDialog *dialog);
113 static void new_chatroom_dialog_model_selection_changed (GtkTreeSelection *selection,
114 EmpathyNewChatroomDialog *dialog);
115 static void new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog);
116 static void new_chatroom_dialog_entry_changed_cb (GtkWidget *entry,
117 EmpathyNewChatroomDialog *dialog);
118 static void new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog);
119 static void new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog);
120 static void new_chatroom_dialog_entry_server_activate_cb (GtkWidget *widget,
121 EmpathyNewChatroomDialog *dialog);
122 static void new_chatroom_dialog_expander_browse_activate_cb (GtkWidget *widget,
123 EmpathyNewChatroomDialog *dialog);
124 static gboolean new_chatroom_dialog_entry_server_focus_out_cb (GtkWidget *widget,
125 GdkEventFocus *event,
126 EmpathyNewChatroomDialog *dialog);
127 static void new_chatroom_dialog_button_close_error_clicked_cb (GtkButton *button,
128 EmpathyNewChatroomDialog *dialog);
130 static EmpathyNewChatroomDialog *dialog_p = NULL;
133 empathy_new_chatroom_dialog_show (GtkWindow *parent)
135 EmpathyNewChatroomDialog *dialog;
137 GtkSizeGroup *size_group;
141 gtk_window_present (GTK_WINDOW (dialog_p->window));
145 dialog_p = dialog = g_new0 (EmpathyNewChatroomDialog, 1);
147 filename = empathy_file_lookup ("empathy-new-chatroom-dialog.ui", "src");
148 gui = empathy_builder_get_file (filename,
149 "new_chatroom_dialog", &dialog->window,
150 "table_info", &dialog->table_info,
151 "label_account", &dialog->label_account,
152 "label_server", &dialog->label_server,
153 "label_room", &dialog->label_room,
154 "entry_server", &dialog->entry_server,
155 "entry_room", &dialog->entry_room,
156 "treeview", &dialog->treeview,
157 "button_join", &dialog->button_join,
158 "expander_browse", &dialog->expander_browse,
159 "label_error_message", &dialog->label_error_message,
160 "viewport_error", &dialog->viewport_error,
164 empathy_builder_connect (gui, dialog,
165 "new_chatroom_dialog", "response", new_chatroom_dialog_response_cb,
166 "new_chatroom_dialog", "destroy", new_chatroom_dialog_destroy_cb,
167 "entry_server", "changed", new_chatroom_dialog_entry_changed_cb,
168 "entry_server", "activate", new_chatroom_dialog_entry_server_activate_cb,
169 "entry_server", "focus-out-event", new_chatroom_dialog_entry_server_focus_out_cb,
170 "entry_room", "changed", new_chatroom_dialog_entry_changed_cb,
171 "expander_browse", "activate", new_chatroom_dialog_expander_browse_activate_cb,
172 "button_close_error", "clicked", new_chatroom_dialog_button_close_error_clicked_cb,
175 g_object_unref (gui);
177 g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog_p);
179 /* Label alignment */
180 size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
182 gtk_size_group_add_widget (size_group, dialog->label_account);
183 gtk_size_group_add_widget (size_group, dialog->label_server);
184 gtk_size_group_add_widget (size_group, dialog->label_room);
186 g_object_unref (size_group);
188 /* Set up chatrooms treeview */
189 new_chatroom_dialog_model_setup (dialog);
192 dialog->throbber = ephy_spinner_new ();
193 ephy_spinner_set_size (EPHY_SPINNER (dialog->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
194 gtk_widget_show (dialog->throbber);
195 gtk_table_attach (GTK_TABLE (dialog->table_info),
200 /* Account chooser for custom */
201 dialog->account_chooser = empathy_account_chooser_new ();
202 empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser),
203 empathy_account_chooser_filter_is_connected,
205 gtk_table_attach_defaults (GTK_TABLE (dialog->table_info),
206 dialog->account_chooser,
208 gtk_widget_show (dialog->account_chooser);
210 g_signal_connect (GTK_COMBO_BOX (dialog->account_chooser), "changed",
211 G_CALLBACK (new_chatroom_dialog_account_changed_cb),
213 new_chatroom_dialog_account_changed_cb (GTK_COMBO_BOX (dialog->account_chooser),
217 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
218 GTK_WINDOW (parent));
221 gtk_widget_show (dialog->window);
225 new_chatroom_dialog_response_cb (GtkWidget *widget,
227 EmpathyNewChatroomDialog *dialog)
229 if (response == GTK_RESPONSE_OK) {
230 new_chatroom_dialog_join (dialog);
233 gtk_widget_destroy (widget);
237 new_chatroom_dialog_destroy_cb (GtkWidget *widget,
238 EmpathyNewChatroomDialog *dialog)
240 if (dialog->room_list) {
241 g_object_unref (dialog->room_list);
243 g_object_unref (dialog->model);
249 new_chatroom_dialog_model_setup (EmpathyNewChatroomDialog *dialog)
253 GtkTreeSelection *selection;
256 view = GTK_TREE_VIEW (dialog->treeview);
258 g_signal_connect (view, "row-activated",
259 G_CALLBACK (new_chatroom_dialog_model_row_activated_cb),
263 store = gtk_list_store_new (COL_COUNT,
264 G_TYPE_STRING, /* Invite */
265 G_TYPE_STRING, /* Password */
266 G_TYPE_STRING, /* Name */
267 G_TYPE_STRING, /* Room */
268 G_TYPE_STRING, /* Member count */
269 G_TYPE_INT, /* Member count int */
270 G_TYPE_STRING); /* Tool tip */
272 dialog->model = GTK_TREE_MODEL (store);
273 gtk_tree_view_set_model (view, dialog->model);
274 gtk_tree_view_set_tooltip_column (view, COL_TOOLTIP);
275 gtk_tree_view_set_search_column (view, COL_NAME);
278 selection = gtk_tree_view_get_selection (view);
279 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
280 COL_NAME, GTK_SORT_ASCENDING);
282 g_signal_connect (selection, "changed",
283 G_CALLBACK (new_chatroom_dialog_model_selection_changed),
287 new_chatroom_dialog_model_add_columns (dialog);
291 new_chatroom_dialog_model_add_columns (EmpathyNewChatroomDialog *dialog)
294 GtkTreeViewColumn *column;
295 GtkCellRenderer *cell;
298 gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &width, &height);
300 view = GTK_TREE_VIEW (dialog->treeview);
302 cell = gtk_cell_renderer_pixbuf_new ();
306 "stock-size", GTK_ICON_SIZE_MENU,
308 column = gtk_tree_view_column_new_with_attributes (NULL,
310 "stock-id", COL_INVITE_ONLY,
313 gtk_tree_view_column_set_sort_column_id (column, COL_INVITE_ONLY);
314 gtk_tree_view_append_column (view, column);
316 column = gtk_tree_view_column_new_with_attributes (NULL,
318 "stock-id", COL_NEED_PASSWORD,
321 gtk_tree_view_column_set_sort_column_id (column, COL_NEED_PASSWORD);
322 gtk_tree_view_append_column (view, column);
324 cell = gtk_cell_renderer_text_new ();
328 "ellipsize", PANGO_ELLIPSIZE_END,
331 column = gtk_tree_view_column_new_with_attributes (_("Chat Room"),
336 gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
337 gtk_tree_view_column_set_expand (column, TRUE);
338 gtk_tree_view_append_column (view, column);
340 cell = gtk_cell_renderer_text_new ();
344 "ellipsize", PANGO_ELLIPSIZE_END,
345 "alignment", PANGO_ALIGN_RIGHT,
347 column = gtk_tree_view_column_new_with_attributes (_("Members"),
352 gtk_tree_view_column_set_sort_column_id (column, COL_MEMBERS_INT);
353 gtk_tree_view_append_column (view, column);
357 new_chatroom_dialog_update_widgets (EmpathyNewChatroomDialog *dialog)
359 EmpathyAccountChooser *account_chooser;
360 EmpathyAccount *account;
362 const gchar *protocol;
365 account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
366 account = empathy_account_chooser_dup_account (account_chooser);
367 profile = empathy_account_get_profile (account);
368 protocol = mc_profile_get_protocol_name (profile);
370 gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), "");
372 /* hardcode here known protocols */
373 if (strcmp (protocol, "jabber") == 0) {
374 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
376 else if (strcmp (protocol, "local-xmpp") == 0) {
377 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
379 else if (strcmp (protocol, "irc") == 0) {
380 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
383 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
386 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
387 gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
389 /* Final set up of the dialog */
390 gtk_widget_grab_focus (dialog->entry_room);
392 g_object_unref (account);
393 g_object_unref (profile);
397 new_chatroom_dialog_account_changed_cb (GtkComboBox *combobox,
398 EmpathyNewChatroomDialog *dialog)
400 EmpathyAccountChooser *account_chooser;
401 EmpathyAccount *account;
402 gboolean listing = FALSE;
403 gboolean expanded = FALSE;
405 if (dialog->room_list) {
406 g_object_unref (dialog->room_list);
407 dialog->room_list = NULL;
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);
418 dialog->room_list = empathy_tp_roomlist_new (account);
420 if (dialog->room_list) {
421 g_signal_connect (dialog->room_list, "destroy",
422 G_CALLBACK (new_chatroom_dialog_roomlist_destroy_cb),
424 g_signal_connect (dialog->room_list, "new-room",
425 G_CALLBACK (new_chatroom_dialog_new_room_cb),
427 g_signal_connect (dialog->room_list, "notify::is-listing",
428 G_CALLBACK (new_chatroom_dialog_listing_cb),
430 g_signal_connect (dialog->room_list, "error::start",
431 G_CALLBACK (start_listing_error_cb),
433 g_signal_connect (dialog->room_list, "error::stop",
434 G_CALLBACK (stop_listing_error_cb),
437 expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
439 gtk_widget_hide (dialog->viewport_error);
440 gtk_widget_set_sensitive (dialog->treeview, TRUE);
441 new_chatroom_dialog_browse_start (dialog);
444 listing = empathy_tp_roomlist_is_listing (dialog->room_list);
446 ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
450 g_object_unref (account);
453 new_chatroom_dialog_update_widgets (dialog);
457 new_chatroom_dialog_button_close_error_clicked_cb (GtkButton *button,
458 EmpathyNewChatroomDialog *dialog)
460 gtk_widget_hide (dialog->viewport_error);
464 new_chatroom_dialog_roomlist_destroy_cb (EmpathyTpRoomlist *room_list,
465 EmpathyNewChatroomDialog *dialog)
467 g_object_unref (dialog->room_list);
468 dialog->room_list = NULL;
472 new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist *room_list,
473 EmpathyChatroom *chatroom,
474 EmpathyNewChatroomDialog *dialog)
477 GtkTreeSelection *selection;
482 const gchar *need_password;
483 const gchar *invite_only;
485 DEBUG ("New chatroom listed: %s (%s)",
486 empathy_chatroom_get_name (chatroom),
487 empathy_chatroom_get_room (chatroom));
490 view = GTK_TREE_VIEW (dialog->treeview);
491 selection = gtk_tree_view_get_selection (view);
492 store = GTK_LIST_STORE (dialog->model);
493 members = g_strdup_printf ("%d", empathy_chatroom_get_members_count (chatroom));
494 tooltip = g_strdup_printf (C_("Room/Join's roomlist tooltip. Parameters"
495 "are a channel name, yes/no, yes/no and a number.",
496 "<b>%s</b>\nInvite required: %s\nPassword required: %s\nMembers: %s"),
497 empathy_chatroom_get_name (chatroom),
498 empathy_chatroom_get_invite_only (chatroom) ? _("Yes") : _("No"),
499 empathy_chatroom_get_need_password (chatroom) ? _("Yes") : _("No"),
501 invite_only = (empathy_chatroom_get_invite_only (chatroom) ?
502 GTK_STOCK_INDEX : NULL);
503 need_password = (empathy_chatroom_get_need_password (chatroom) ?
504 GTK_STOCK_DIALOG_AUTHENTICATION : NULL);
506 gtk_list_store_append (store, &iter);
507 gtk_list_store_set (store, &iter,
508 COL_NEED_PASSWORD, need_password,
509 COL_INVITE_ONLY, invite_only,
510 COL_NAME, empathy_chatroom_get_name (chatroom),
511 COL_ROOM, empathy_chatroom_get_room (chatroom),
512 COL_MEMBERS, members,
513 COL_MEMBERS_INT, empathy_chatroom_get_members_count (chatroom),
514 COL_TOOLTIP, tooltip,
522 start_listing_error_cb (EmpathyTpRoomlist *room_list,
524 EmpathyNewChatroomDialog *dialog)
526 gtk_label_set_text (GTK_LABEL (dialog->label_error_message), _("Could not start room listing"));
527 gtk_widget_show_all (dialog->viewport_error);
528 gtk_widget_set_sensitive (dialog->treeview, FALSE);
532 stop_listing_error_cb (EmpathyTpRoomlist *room_list,
534 EmpathyNewChatroomDialog *dialog)
536 gtk_label_set_text (GTK_LABEL (dialog->label_error_message), _("Could not stop room listing"));
537 gtk_widget_show_all (dialog->viewport_error);
538 gtk_widget_set_sensitive (dialog->treeview, FALSE);
542 new_chatroom_dialog_listing_cb (EmpathyTpRoomlist *room_list,
544 EmpathyNewChatroomDialog *dialog)
548 listing = empathy_tp_roomlist_is_listing (room_list);
550 /* Update the throbber */
552 ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
554 ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
559 new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog)
563 store = GTK_LIST_STORE (dialog->model);
564 gtk_list_store_clear (store);
568 new_chatroom_dialog_model_row_activated_cb (GtkTreeView *tree_view,
570 GtkTreeViewColumn *column,
571 EmpathyNewChatroomDialog *dialog)
573 gtk_widget_activate (dialog->button_join);
577 new_chatroom_dialog_model_selection_changed (GtkTreeSelection *selection,
578 EmpathyNewChatroomDialog *dialog)
583 gchar *server = NULL;
585 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
589 gtk_tree_model_get (model, &iter, COL_ROOM, &room, -1);
590 server = strstr (room, "@");
596 gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), server ? server : "");
597 gtk_entry_set_text (GTK_ENTRY (dialog->entry_room), room ? room : "");
603 new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog)
605 EmpathyAccountChooser *account_chooser;
606 TpConnection *connection;
608 const gchar *server = NULL;
609 gchar *room_name = NULL;
611 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
612 server = gtk_entry_get_text (GTK_ENTRY (dialog->entry_server));
614 account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
615 connection = empathy_account_chooser_get_connection (account_chooser);
617 if (!EMP_STR_EMPTY (server)) {
618 room_name = g_strconcat (room, "@", server, NULL);
620 room_name = g_strdup (room);
623 DEBUG ("Requesting channel for '%s'", room_name);
624 empathy_dispatcher_join_muc (connection, room_name, NULL, NULL);
630 new_chatroom_dialog_entry_changed_cb (GtkWidget *entry,
631 EmpathyNewChatroomDialog *dialog)
633 if (entry == dialog->entry_room) {
636 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
637 gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
638 /* FIXME: Select the room in the list */
643 new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog)
645 new_chatroom_dialog_model_clear (dialog);
646 if (dialog->room_list) {
647 empathy_tp_roomlist_start (dialog->room_list);
652 new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog)
654 if (dialog->room_list) {
655 empathy_tp_roomlist_stop (dialog->room_list);
660 new_chatroom_dialog_entry_server_activate_cb (GtkWidget *widget,
661 EmpathyNewChatroomDialog *dialog)
663 new_chatroom_dialog_browse_start (dialog);
667 new_chatroom_dialog_expander_browse_activate_cb (GtkWidget *widget,
668 EmpathyNewChatroomDialog *dialog)
672 expanded = gtk_expander_get_expanded (GTK_EXPANDER (widget));
674 new_chatroom_dialog_browse_stop (dialog);
675 gtk_window_set_resizable (GTK_WINDOW (dialog->window), FALSE);
677 gtk_widget_hide (dialog->viewport_error);
678 gtk_widget_set_sensitive (dialog->treeview, TRUE);
679 new_chatroom_dialog_browse_start (dialog);
680 gtk_window_set_resizable (GTK_WINDOW (dialog->window), TRUE);
685 new_chatroom_dialog_entry_server_focus_out_cb (GtkWidget *widget,
686 GdkEventFocus *event,
687 EmpathyNewChatroomDialog *dialog)
691 expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
693 new_chatroom_dialog_browse_start (dialog);