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., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Martyn Russell <martyn@imendio.com>
22 * Xavier Claessens <xclaesse@gmail.com>
31 #include <glade/glade.h>
33 #include <glib/gi18n.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 *togglebutton_refresh;
64 GtkWidget *label_room;
65 GtkWidget *entry_room;
66 GtkWidget *vbox_browse;
67 GtkWidget *image_status;
68 GtkWidget *label_status;
69 GtkWidget *hbox_status;
73 GtkWidget *button_join;
74 GtkWidget *button_close;
75 } EmpathyNewChatroomDialog;
83 static void new_chatroom_dialog_response_cb (GtkWidget *widget,
85 EmpathyNewChatroomDialog *dialog);
86 static void new_chatroom_dialog_destroy_cb (GtkWidget *widget,
87 EmpathyNewChatroomDialog *dialog);
88 static void new_chatroom_dialog_model_setup (EmpathyNewChatroomDialog *dialog);
89 static void new_chatroom_dialog_model_add_columns (EmpathyNewChatroomDialog *dialog);
90 static void new_chatroom_dialog_update_widgets (EmpathyNewChatroomDialog *dialog);
91 static void new_chatroom_dialog_account_changed_cb (GtkComboBox *combobox,
92 EmpathyNewChatroomDialog *dialog);
93 static void new_chatroom_dialog_roomlist_destroy_cb (EmpathyTpRoomlist *room_list,
94 EmpathyNewChatroomDialog *dialog);
95 static void new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist *room_list,
96 EmpathyChatroom *chatroom,
97 EmpathyNewChatroomDialog *dialog);
98 static void new_chatroom_dialog_listing_cb (EmpathyTpRoomlist *room_list,
100 EmpathyNewChatroomDialog *dialog);
101 static void new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog);
102 static void new_chatroom_dialog_model_row_activated_cb (GtkTreeView *tree_view,
104 GtkTreeViewColumn *column,
105 EmpathyNewChatroomDialog *dialog);
106 static void new_chatroom_dialog_model_selection_changed (GtkTreeSelection *selection,
107 EmpathyNewChatroomDialog *dialog);
108 static void new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog);
109 static void new_chatroom_dialog_entry_changed_cb (GtkWidget *entry,
110 EmpathyNewChatroomDialog *dialog);
111 static void new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog);
112 static void new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog);
113 static void new_chatroom_dialog_entry_server_activate_cb (GtkWidget *widget,
114 EmpathyNewChatroomDialog *dialog);
115 static void new_chatroom_dialog_togglebutton_refresh_toggled_cb (GtkWidget *widget,
116 EmpathyNewChatroomDialog *dialog);
118 static EmpathyNewChatroomDialog *dialog_p = NULL;
121 empathy_new_chatroom_dialog_show (GtkWindow *parent)
123 EmpathyNewChatroomDialog *dialog;
125 GtkSizeGroup *size_group;
129 gtk_window_present (GTK_WINDOW (dialog_p->window));
133 dialog_p = dialog = g_new0 (EmpathyNewChatroomDialog, 1);
135 filename = empathy_file_lookup ("empathy-new-chatroom-dialog.glade", "src");
136 glade = empathy_glade_get_file (filename,
137 "new_chatroom_dialog",
139 "new_chatroom_dialog", &dialog->window,
140 "table_info", &dialog->table_info,
141 "label_account", &dialog->label_account,
142 "label_server", &dialog->label_server,
143 "label_room", &dialog->label_room,
144 "entry_server", &dialog->entry_server,
145 "entry_room", &dialog->entry_room,
146 "togglebutton_refresh", &dialog->togglebutton_refresh,
147 "vbox_browse", &dialog->vbox_browse,
148 "image_status", &dialog->image_status,
149 "label_status", &dialog->label_status,
150 "hbox_status", &dialog->hbox_status,
151 "treeview", &dialog->treeview,
152 "button_join", &dialog->button_join,
156 empathy_glade_connect (glade,
158 "new_chatroom_dialog", "response", new_chatroom_dialog_response_cb,
159 "new_chatroom_dialog", "destroy", new_chatroom_dialog_destroy_cb,
160 "entry_server", "changed", new_chatroom_dialog_entry_changed_cb,
161 "entry_server", "activate", new_chatroom_dialog_entry_server_activate_cb,
162 "entry_room", "changed", new_chatroom_dialog_entry_changed_cb,
163 "togglebutton_refresh", "toggled", new_chatroom_dialog_togglebutton_refresh_toggled_cb,
166 g_object_unref (glade);
168 g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog_p);
170 /* Label alignment */
171 size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
173 gtk_size_group_add_widget (size_group, dialog->label_account);
174 gtk_size_group_add_widget (size_group, dialog->label_server);
175 gtk_size_group_add_widget (size_group, dialog->label_room);
177 g_object_unref (size_group);
179 /* Set up chatrooms treeview */
180 new_chatroom_dialog_model_setup (dialog);
183 dialog->throbber = ephy_spinner_new ();
184 ephy_spinner_set_size (EPHY_SPINNER (dialog->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
185 gtk_widget_show (dialog->throbber);
187 gtk_box_pack_start (GTK_BOX (dialog->hbox_status), dialog->throbber,
190 /* Account chooser for custom */
191 dialog->account_chooser = empathy_account_chooser_new ();
192 empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser),
193 empathy_account_chooser_filter_is_connected,
195 gtk_table_attach_defaults (GTK_TABLE (dialog->table_info),
196 dialog->account_chooser,
198 gtk_widget_show (dialog->account_chooser);
200 g_signal_connect (GTK_COMBO_BOX (dialog->account_chooser), "changed",
201 G_CALLBACK (new_chatroom_dialog_account_changed_cb),
203 new_chatroom_dialog_account_changed_cb (GTK_COMBO_BOX (dialog->account_chooser),
207 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
208 GTK_WINDOW (parent));
211 gtk_widget_show (dialog->window);
215 new_chatroom_dialog_response_cb (GtkWidget *widget,
217 EmpathyNewChatroomDialog *dialog)
219 if (response == GTK_RESPONSE_OK) {
220 new_chatroom_dialog_join (dialog);
223 gtk_widget_destroy (widget);
227 new_chatroom_dialog_destroy_cb (GtkWidget *widget,
228 EmpathyNewChatroomDialog *dialog)
230 if (dialog->room_list) {
231 g_object_unref (dialog->room_list);
233 g_object_unref (dialog->model);
239 new_chatroom_dialog_model_setup (EmpathyNewChatroomDialog *dialog)
243 GtkTreeSelection *selection;
246 view = GTK_TREE_VIEW (dialog->treeview);
248 g_signal_connect (view, "row-activated",
249 G_CALLBACK (new_chatroom_dialog_model_row_activated_cb),
253 store = gtk_list_store_new (COL_COUNT,
254 G_TYPE_STRING, /* Image */
255 G_TYPE_STRING, /* Text */
256 G_TYPE_STRING); /* Room */
258 dialog->model = GTK_TREE_MODEL (store);
259 gtk_tree_view_set_model (view, dialog->model);
262 selection = gtk_tree_view_get_selection (view);
263 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
264 COL_NAME, GTK_SORT_ASCENDING);
266 g_signal_connect (selection, "changed",
267 G_CALLBACK (new_chatroom_dialog_model_selection_changed),
271 new_chatroom_dialog_model_add_columns (dialog);
275 new_chatroom_dialog_model_add_columns (EmpathyNewChatroomDialog *dialog)
278 GtkTreeViewColumn *column;
279 GtkCellRenderer *cell;
281 view = GTK_TREE_VIEW (dialog->treeview);
282 gtk_tree_view_set_headers_visible (view, FALSE);
284 cell = gtk_cell_renderer_text_new ();
288 "ellipsize", PANGO_ELLIPSIZE_END,
291 column = gtk_tree_view_column_new_with_attributes (_("Chat Rooms"),
296 gtk_tree_view_column_set_expand (column, TRUE);
297 gtk_tree_view_append_column (view, column);
301 new_chatroom_dialog_update_widgets (EmpathyNewChatroomDialog *dialog)
303 EmpathyAccountChooser *account_chooser;
306 const gchar *protocol;
309 account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
310 account = empathy_account_chooser_get_account (account_chooser);
311 profile = mc_account_get_profile (account);
312 protocol = mc_profile_get_protocol_name (profile);
314 gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), "");
316 /* hardcode here known protocols */
317 if (strcmp (protocol, "jabber") == 0) {
318 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
319 gtk_widget_show (dialog->vbox_browse);
322 else if (strcmp (protocol, "local-xmpp") == 0) {
323 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
324 gtk_widget_show (dialog->vbox_browse);
326 else if (strcmp (protocol, "irc") == 0) {
327 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
328 gtk_widget_show (dialog->vbox_browse);
330 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
331 gtk_widget_show (dialog->vbox_browse);
334 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
335 gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
337 /* Final set up of the dialog */
338 gtk_widget_grab_focus (dialog->entry_room);
340 g_object_unref (account);
341 g_object_unref (profile);
345 new_chatroom_dialog_account_changed_cb (GtkComboBox *combobox,
346 EmpathyNewChatroomDialog *dialog)
348 EmpathyAccountChooser *account_chooser;
350 gboolean listing = FALSE;
352 if (dialog->room_list) {
353 g_object_unref (dialog->room_list);
356 ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
357 new_chatroom_dialog_model_clear (dialog);
359 account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
360 account = empathy_account_chooser_get_account (account_chooser);
361 dialog->room_list = empathy_tp_roomlist_new (account);
363 if (dialog->room_list) {
364 g_signal_connect (dialog->room_list, "destroy",
365 G_CALLBACK (new_chatroom_dialog_roomlist_destroy_cb),
367 g_signal_connect (dialog->room_list, "new-room",
368 G_CALLBACK (new_chatroom_dialog_new_room_cb),
370 g_signal_connect (dialog->room_list, "notify::is-listing",
371 G_CALLBACK (new_chatroom_dialog_listing_cb),
374 listing = empathy_tp_roomlist_is_listing (dialog->room_list);
376 ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
380 new_chatroom_dialog_update_widgets (dialog);
384 new_chatroom_dialog_roomlist_destroy_cb (EmpathyTpRoomlist *room_list,
385 EmpathyNewChatroomDialog *dialog)
387 g_object_unref (dialog->room_list);
388 dialog->room_list = NULL;
392 new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist *room_list,
393 EmpathyChatroom *chatroom,
394 EmpathyNewChatroomDialog *dialog)
397 GtkTreeSelection *selection;
401 DEBUG ("New chatroom listed: %s (%s)",
402 empathy_chatroom_get_name (chatroom),
403 empathy_chatroom_get_room (chatroom));
406 view = GTK_TREE_VIEW (dialog->treeview);
407 selection = gtk_tree_view_get_selection (view);
408 store = GTK_LIST_STORE (dialog->model);
410 gtk_list_store_append (store, &iter);
411 gtk_list_store_set (store, &iter,
412 COL_NAME, empathy_chatroom_get_name (chatroom),
413 COL_ROOM, empathy_chatroom_get_room (chatroom),
418 new_chatroom_dialog_listing_cb (EmpathyTpRoomlist *room_list,
420 EmpathyNewChatroomDialog *dialog)
424 listing = empathy_tp_roomlist_is_listing (room_list);
426 /* Update the throbber */
428 ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
430 ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
433 /* Update the refresh toggle button */
434 g_signal_handlers_block_by_func (dialog->togglebutton_refresh,
435 new_chatroom_dialog_togglebutton_refresh_toggled_cb,
437 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->togglebutton_refresh),
439 g_signal_handlers_unblock_by_func (dialog->togglebutton_refresh,
440 new_chatroom_dialog_togglebutton_refresh_toggled_cb,
445 new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog)
449 store = GTK_LIST_STORE (dialog->model);
450 gtk_list_store_clear (store);
454 new_chatroom_dialog_model_row_activated_cb (GtkTreeView *tree_view,
456 GtkTreeViewColumn *column,
457 EmpathyNewChatroomDialog *dialog)
459 gtk_widget_activate (dialog->button_join);
463 new_chatroom_dialog_model_selection_changed (GtkTreeSelection *selection,
464 EmpathyNewChatroomDialog *dialog)
469 gchar *server = NULL;
471 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
475 gtk_tree_model_get (model, &iter, COL_ROOM, &room, -1);
476 server = strstr (room, "@");
482 gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), server ? server : "");
483 gtk_entry_set_text (GTK_ENTRY (dialog->entry_room), room ? room : "");
489 new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog)
491 EmpathyAccountChooser *account_chooser;
494 const gchar *server = NULL;
495 gchar *room_name = NULL;
497 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
498 server = gtk_entry_get_text (GTK_ENTRY (dialog->entry_server));
500 account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
501 account = empathy_account_chooser_get_account (account_chooser);
503 if (!EMP_STR_EMPTY (server)) {
504 room_name = g_strconcat (room, "@", server, NULL);
506 room_name = g_strdup (room);
509 DEBUG ("Requesting channel for '%s'", room_name);
510 empathy_dispatcher_join_muc (account, room_name, NULL, NULL);
516 new_chatroom_dialog_entry_changed_cb (GtkWidget *entry,
517 EmpathyNewChatroomDialog *dialog)
519 if (entry == dialog->entry_room) {
522 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
523 gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
524 /* FIXME: Select the room in the list */
529 new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog)
531 new_chatroom_dialog_model_clear (dialog);
532 if (dialog->room_list) {
533 empathy_tp_roomlist_start (dialog->room_list);
538 new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog)
540 if (dialog->room_list) {
541 empathy_tp_roomlist_stop (dialog->room_list);
546 new_chatroom_dialog_entry_server_activate_cb (GtkWidget *widget,
547 EmpathyNewChatroomDialog *dialog)
549 new_chatroom_dialog_togglebutton_refresh_toggled_cb (dialog->togglebutton_refresh,
554 new_chatroom_dialog_togglebutton_refresh_toggled_cb (GtkWidget *widget,
555 EmpathyNewChatroomDialog *dialog)
559 toggled = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
562 new_chatroom_dialog_browse_start (dialog);
564 new_chatroom_dialog_browse_stop (dialog);