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 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-debug.h>
44 #include "empathy-new-chatroom-dialog.h"
45 #include "empathy-account-chooser.h"
46 #include "empathy-ui-utils.h"
47 #include "ephy-spinner.h"
49 #define DEBUG_DOMAIN "NewChatroomDialog"
52 EmpathyTpRoomlist *room_list;
55 GtkWidget *vbox_widgets;
56 GtkWidget *table_info;
57 GtkWidget *label_account;
58 GtkWidget *account_chooser;
59 GtkWidget *label_server;
60 GtkWidget *entry_server;
61 GtkWidget *togglebutton_refresh;
62 GtkWidget *label_room;
63 GtkWidget *entry_room;
64 GtkWidget *vbox_browse;
65 GtkWidget *image_status;
66 GtkWidget *label_status;
67 GtkWidget *hbox_status;
71 GtkWidget *button_join;
72 GtkWidget *button_close;
73 } EmpathyNewChatroomDialog;
82 static void new_chatroom_dialog_response_cb (GtkWidget *widget,
84 EmpathyNewChatroomDialog *dialog);
85 static void new_chatroom_dialog_destroy_cb (GtkWidget *widget,
86 EmpathyNewChatroomDialog *dialog);
87 static void new_chatroom_dialog_model_setup (EmpathyNewChatroomDialog *dialog);
88 static void new_chatroom_dialog_model_add_columns (EmpathyNewChatroomDialog *dialog);
89 static void new_chatroom_dialog_update_widgets (EmpathyNewChatroomDialog *dialog);
90 static void new_chatroom_dialog_account_changed_cb (GtkComboBox *combobox,
91 EmpathyNewChatroomDialog *dialog);
92 static void new_chatroom_dialog_roomlist_destroy_cb (EmpathyTpRoomlist *room_list,
93 EmpathyNewChatroomDialog *dialog);
94 static void new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist *room_list,
95 EmpathyChatroom *chatroom,
96 EmpathyNewChatroomDialog *dialog);
97 static void new_chatroom_dialog_listing_cb (EmpathyTpRoomlist *room_list,
99 EmpathyNewChatroomDialog *dialog);
100 static void new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog);
101 static void new_chatroom_dialog_model_row_activated_cb (GtkTreeView *tree_view,
103 GtkTreeViewColumn *column,
104 EmpathyNewChatroomDialog *dialog);
105 static void new_chatroom_dialog_model_selection_changed (GtkTreeSelection *selection,
106 EmpathyNewChatroomDialog *dialog);
107 static void new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog);
108 static void new_chatroom_dialog_entry_changed_cb (GtkWidget *entry,
109 EmpathyNewChatroomDialog *dialog);
110 static void new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog);
111 static void new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog);
112 static void new_chatroom_dialog_entry_server_activate_cb (GtkWidget *widget,
113 EmpathyNewChatroomDialog *dialog);
114 static void new_chatroom_dialog_togglebutton_refresh_toggled_cb (GtkWidget *widget,
115 EmpathyNewChatroomDialog *dialog);
117 static EmpathyNewChatroomDialog *dialog_p = NULL;
120 empathy_new_chatroom_dialog_show (GtkWindow *parent)
122 EmpathyNewChatroomDialog *dialog;
124 GtkSizeGroup *size_group;
127 gtk_window_present (GTK_WINDOW (dialog_p->window));
131 dialog_p = dialog = g_new0 (EmpathyNewChatroomDialog, 1);
133 glade = empathy_glade_get_file ("empathy-new-chatroom-dialog.glade",
134 "new_chatroom_dialog",
136 "new_chatroom_dialog", &dialog->window,
137 "table_info", &dialog->table_info,
138 "label_account", &dialog->label_account,
139 "label_server", &dialog->label_server,
140 "label_room", &dialog->label_room,
141 "entry_server", &dialog->entry_server,
142 "entry_room", &dialog->entry_room,
143 "togglebutton_refresh", &dialog->togglebutton_refresh,
144 "vbox_browse", &dialog->vbox_browse,
145 "image_status", &dialog->image_status,
146 "label_status", &dialog->label_status,
147 "hbox_status", &dialog->hbox_status,
148 "treeview", &dialog->treeview,
149 "button_join", &dialog->button_join,
152 empathy_glade_connect (glade,
154 "new_chatroom_dialog", "response", new_chatroom_dialog_response_cb,
155 "new_chatroom_dialog", "destroy", new_chatroom_dialog_destroy_cb,
156 "entry_server", "changed", new_chatroom_dialog_entry_changed_cb,
157 "entry_server", "activate", new_chatroom_dialog_entry_server_activate_cb,
158 "entry_room", "changed", new_chatroom_dialog_entry_changed_cb,
159 "togglebutton_refresh", "toggled", new_chatroom_dialog_togglebutton_refresh_toggled_cb,
162 g_object_unref (glade);
164 g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog_p);
166 /* Label alignment */
167 size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
169 gtk_size_group_add_widget (size_group, dialog->label_account);
170 gtk_size_group_add_widget (size_group, dialog->label_server);
171 gtk_size_group_add_widget (size_group, dialog->label_room);
173 g_object_unref (size_group);
175 /* Set up chatrooms treeview */
176 new_chatroom_dialog_model_setup (dialog);
179 dialog->throbber = ephy_spinner_new ();
180 ephy_spinner_set_size (EPHY_SPINNER (dialog->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
181 gtk_widget_show (dialog->throbber);
183 gtk_box_pack_start (GTK_BOX (dialog->hbox_status), dialog->throbber,
186 /* Account chooser for custom */
187 dialog->account_chooser = empathy_account_chooser_new ();
188 empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser),
189 empathy_account_chooser_filter_is_connected,
191 gtk_table_attach_defaults (GTK_TABLE (dialog->table_info),
192 dialog->account_chooser,
194 gtk_widget_show (dialog->account_chooser);
196 g_signal_connect (GTK_COMBO_BOX (dialog->account_chooser), "changed",
197 G_CALLBACK (new_chatroom_dialog_account_changed_cb),
199 new_chatroom_dialog_account_changed_cb (GTK_COMBO_BOX (dialog->account_chooser),
203 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
204 GTK_WINDOW (parent));
207 gtk_widget_show (dialog->window);
211 new_chatroom_dialog_response_cb (GtkWidget *widget,
213 EmpathyNewChatroomDialog *dialog)
215 if (response == GTK_RESPONSE_OK) {
216 new_chatroom_dialog_join (dialog);
219 gtk_widget_destroy (widget);
223 new_chatroom_dialog_destroy_cb (GtkWidget *widget,
224 EmpathyNewChatroomDialog *dialog)
226 if (dialog->room_list) {
227 g_object_unref (dialog->room_list);
229 g_object_unref (dialog->model);
235 new_chatroom_dialog_model_setup (EmpathyNewChatroomDialog *dialog)
239 GtkTreeSelection *selection;
242 view = GTK_TREE_VIEW (dialog->treeview);
244 g_signal_connect (view, "row-activated",
245 G_CALLBACK (new_chatroom_dialog_model_row_activated_cb),
249 store = gtk_list_store_new (COL_COUNT,
250 G_TYPE_STRING, /* Image */
251 G_TYPE_STRING, /* Text */
252 G_TYPE_STRING); /* Room */
254 dialog->model = GTK_TREE_MODEL (store);
255 gtk_tree_view_set_model (view, dialog->model);
258 selection = gtk_tree_view_get_selection (view);
259 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
260 COL_NAME, GTK_SORT_ASCENDING);
262 g_signal_connect (selection, "changed",
263 G_CALLBACK (new_chatroom_dialog_model_selection_changed),
267 new_chatroom_dialog_model_add_columns (dialog);
271 new_chatroom_dialog_model_add_columns (EmpathyNewChatroomDialog *dialog)
274 GtkTreeViewColumn *column;
275 GtkCellRenderer *cell;
277 view = GTK_TREE_VIEW (dialog->treeview);
278 gtk_tree_view_set_headers_visible (view, FALSE);
280 /* Chatroom pointer */
281 column = gtk_tree_view_column_new ();
282 gtk_tree_view_column_set_title (column, _("Chat Rooms"));
284 cell = gtk_cell_renderer_pixbuf_new ();
285 gtk_tree_view_column_pack_start (column, cell, FALSE);
287 cell = gtk_cell_renderer_text_new ();
291 "ellipsize", PANGO_ELLIPSIZE_END,
294 gtk_tree_view_column_pack_start (column, cell, TRUE);
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, "salut") == 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, !G_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, "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 empathy_debug (DEBUG_DOMAIN, "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)
422 /* Update the throbber */
424 ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
426 ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
429 /* Update the refresh toggle button */
430 g_signal_handlers_block_by_func (dialog->togglebutton_refresh,
431 new_chatroom_dialog_togglebutton_refresh_toggled_cb,
433 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->togglebutton_refresh),
435 g_signal_handlers_unblock_by_func (dialog->togglebutton_refresh,
436 new_chatroom_dialog_togglebutton_refresh_toggled_cb,
441 new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog)
445 store = GTK_LIST_STORE (dialog->model);
446 gtk_list_store_clear (store);
450 new_chatroom_dialog_model_row_activated_cb (GtkTreeView *tree_view,
452 GtkTreeViewColumn *column,
453 EmpathyNewChatroomDialog *dialog)
455 gtk_widget_activate (dialog->button_join);
459 new_chatroom_dialog_model_selection_changed (GtkTreeSelection *selection,
460 EmpathyNewChatroomDialog *dialog)
465 gchar *server = NULL;
467 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
471 gtk_tree_model_get (model, &iter, COL_ROOM, &room, -1);
472 server = strstr (room, "@");
478 gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), server ? server : "");
479 gtk_entry_set_text (GTK_ENTRY (dialog->entry_room), room ? room : "");
486 new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog)
489 EmpathyAccountChooser *account_chooser;
492 const gchar *server = NULL;
493 gchar *room_name = NULL;
495 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
496 server = gtk_entry_get_text (GTK_ENTRY (dialog->entry_server));
498 account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
499 account = empathy_account_chooser_get_account (account_chooser);
501 if (!G_STR_EMPTY (server)) {
502 room_name = g_strconcat (room, "@", server, NULL);
504 room_name = g_strdup (room);
507 empathy_debug (DEBUG_DOMAIN, "Requesting channel for '%s'", room_name);
509 mc = empathy_mission_control_new ();
510 mission_control_request_channel_with_string_handle (mc,
512 TP_IFACE_CHANNEL_TYPE_TEXT,
521 new_chatroom_dialog_entry_changed_cb (GtkWidget *entry,
522 EmpathyNewChatroomDialog *dialog)
524 if (entry == dialog->entry_room) {
527 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
528 gtk_widget_set_sensitive (dialog->button_join, !G_STR_EMPTY (room));
529 /* FIXME: Select the room in the list */
534 new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog)
536 new_chatroom_dialog_model_clear (dialog);
537 if (dialog->room_list) {
538 empathy_tp_roomlist_start (dialog->room_list);
543 new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog)
545 if (dialog->room_list) {
546 empathy_tp_roomlist_stop (dialog->room_list);
551 new_chatroom_dialog_entry_server_activate_cb (GtkWidget *widget,
552 EmpathyNewChatroomDialog *dialog)
554 new_chatroom_dialog_togglebutton_refresh_toggled_cb (dialog->togglebutton_refresh,
559 new_chatroom_dialog_togglebutton_refresh_toggled_cb (GtkWidget *widget,
560 EmpathyNewChatroomDialog *dialog)
564 toggled = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
567 new_chatroom_dialog_browse_start (dialog);
569 new_chatroom_dialog_browse_stop (dialog);