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/gossip-utils.h>
40 #include <libempathy/gossip-debug.h>
42 #include "gossip-new-chatroom-dialog.h"
43 #include "gossip-account-chooser.h"
44 //#include "gossip-chatrooms-window.h"
45 #include "gossip-ui-utils.h"
46 #include "ephy-spinner.h"
48 #define DEBUG_DOMAIN "NewChatroomDialog"
53 GtkWidget *vbox_widgets;
55 GtkWidget *hbox_account;
56 GtkWidget *label_account;
57 GtkWidget *account_chooser;
59 GtkWidget *hbox_server;
60 GtkWidget *label_server;
61 GtkWidget *entry_server;
62 GtkWidget *togglebutton_refresh;
65 GtkWidget *label_room;
66 GtkWidget *entry_room;
69 GtkWidget *label_nick;
70 GtkWidget *entry_nick;
72 GtkWidget *vbox_browse;
73 GtkWidget *image_status;
74 GtkWidget *label_status;
75 GtkWidget *hbox_status;
81 GtkWidget *button_join;
82 GtkWidget *button_close;
83 } GossipNewChatroomDialog;
90 } EmpathyRoomListItem;
99 static void new_chatroom_dialog_response_cb (GtkWidget *widget,
101 GossipNewChatroomDialog *dialog);
102 static void new_chatroom_dialog_destroy_cb (GtkWidget *widget,
103 GossipNewChatroomDialog *dialog);
104 static void new_chatroom_dialog_model_setup (GossipNewChatroomDialog *dialog);
105 static void new_chatroom_dialog_model_add_columns (GossipNewChatroomDialog *dialog);
106 static void new_chatroom_dialog_update_buttons (GossipNewChatroomDialog *dialog);
107 static void new_chatroom_dialog_update_widgets (GossipNewChatroomDialog *dialog);
108 static void new_chatroom_dialog_account_changed_cb (GtkComboBox *combobox,
109 GossipNewChatroomDialog *dialog);
110 static void new_chatroom_dialog_model_add (GossipNewChatroomDialog *dialog,
111 EmpathyRoomListItem *item);
112 static void new_chatroom_dialog_model_clear (GossipNewChatroomDialog *dialog);
113 static GList * new_chatroom_dialog_model_get_selected (GossipNewChatroomDialog *dialog);
114 static gboolean new_chatroom_dialog_model_filter_func (GtkTreeModel *model,
116 GossipNewChatroomDialog *dialog);
117 static void new_chatroom_dialog_model_row_activated_cb (GtkTreeView *tree_view,
119 GtkTreeViewColumn *column,
120 GossipNewChatroomDialog *dialog);
121 static void new_chatroom_dialog_model_row_inserted_cb (GtkTreeModel *model,
124 GossipNewChatroomDialog *dialog);
125 static void new_chatroom_dialog_model_row_deleted_cb (GtkTreeModel *model,
127 GossipNewChatroomDialog *dialog);
128 static void new_chatroom_dialog_model_selection_changed (GtkTreeSelection *selection,
129 GossipNewChatroomDialog *dialog);
130 static void new_chatroom_dialog_join (GossipNewChatroomDialog *dialog);
131 static void new_chatroom_dialog_request_handles_cb (DBusGProxy *proxy,
135 static void new_chatroom_dialog_entry_changed_cb (GtkWidget *entry,
136 GossipNewChatroomDialog *dialog);
137 static void new_chatroom_dialog_browse_start (GossipNewChatroomDialog *dialog);
138 static void new_chatroom_dialog_browse_stop (GossipNewChatroomDialog *dialog);
139 static void new_chatroom_dialog_entry_server_activate_cb (GtkWidget *widget,
140 GossipNewChatroomDialog *dialog);
141 static void new_chatroom_dialog_togglebutton_refresh_toggled_cb (GtkWidget *widget,
142 GossipNewChatroomDialog *dialog);
144 static GossipNewChatroomDialog *dialog_p = NULL;
147 gossip_new_chatroom_dialog_show (GtkWindow *parent)
149 GossipNewChatroomDialog *dialog;
153 GtkSizeGroup *size_group;
156 gtk_window_present (GTK_WINDOW (dialog_p->window));
160 dialog_p = dialog = g_new0 (GossipNewChatroomDialog, 1);
162 glade = gossip_glade_get_file ("gossip-new-chatroom-dialog.glade",
163 "new_chatroom_dialog",
165 "new_chatroom_dialog", &dialog->window,
166 "hbox_account", &dialog->hbox_account,
167 "label_account", &dialog->label_account,
168 "vbox_widgets", &dialog->vbox_widgets,
169 "label_server", &dialog->label_server,
170 "label_room", &dialog->label_room,
171 "label_nick", &dialog->label_nick,
172 "hbox_server", &dialog->hbox_server,
173 "hbox_room", &dialog->hbox_room,
174 "hbox_nick", &dialog->hbox_nick,
175 "entry_server", &dialog->entry_server,
176 "entry_room", &dialog->entry_room,
177 "entry_nick", &dialog->entry_nick,
178 "togglebutton_refresh", &dialog->togglebutton_refresh,
179 "vbox_browse", &dialog->vbox_browse,
180 "image_status", &dialog->image_status,
181 "label_status", &dialog->label_status,
182 "hbox_status", &dialog->hbox_status,
183 "treeview", &dialog->treeview,
184 "button_join", &dialog->button_join,
187 gossip_glade_connect (glade,
189 "new_chatroom_dialog", "response", new_chatroom_dialog_response_cb,
190 "new_chatroom_dialog", "destroy", new_chatroom_dialog_destroy_cb,
191 "entry_nick", "changed", new_chatroom_dialog_entry_changed_cb,
192 "entry_server", "changed", new_chatroom_dialog_entry_changed_cb,
193 "entry_server", "activate", new_chatroom_dialog_entry_server_activate_cb,
194 "entry_room", "changed", new_chatroom_dialog_entry_changed_cb,
195 "togglebutton_refresh", "toggled", new_chatroom_dialog_togglebutton_refresh_toggled_cb,
198 g_object_unref (glade);
200 g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog_p);
202 /* Label alignment */
203 size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
205 gtk_size_group_add_widget (size_group, dialog->label_account);
206 gtk_size_group_add_widget (size_group, dialog->label_server);
207 gtk_size_group_add_widget (size_group, dialog->label_room);
208 gtk_size_group_add_widget (size_group, dialog->label_nick);
210 g_object_unref (size_group);
212 /* Account chooser for custom */
213 dialog->account_chooser = gossip_account_chooser_new ();
214 gtk_box_pack_start (GTK_BOX (dialog->hbox_account),
215 dialog->account_chooser,
217 gtk_widget_show (dialog->account_chooser);
219 g_signal_connect (GTK_COMBO_BOX (dialog->account_chooser), "changed",
220 G_CALLBACK (new_chatroom_dialog_account_changed_cb),
224 accounts = mc_accounts_list ();
225 account_num = g_list_length (accounts);
227 g_list_foreach (accounts, (GFunc) g_object_unref, NULL);
228 g_list_free (accounts);
230 if (account_num > 1) {
231 gtk_widget_show (dialog->hbox_account);
233 /* Show no accounts combo box */
234 gtk_widget_hide (dialog->hbox_account);
238 dialog->throbber = ephy_spinner_new ();
239 ephy_spinner_set_size (EPHY_SPINNER (dialog->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
240 gtk_widget_show (dialog->throbber);
242 gtk_box_pack_start (GTK_BOX (dialog->hbox_status), dialog->throbber,
245 /* Set up chatrooms treeview */
246 new_chatroom_dialog_model_setup (dialog);
248 /* Set things up according to the account type */
249 new_chatroom_dialog_update_widgets (dialog);
252 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
253 GTK_WINDOW (parent));
256 gtk_widget_show (dialog->window);
260 new_chatroom_dialog_response_cb (GtkWidget *widget,
262 GossipNewChatroomDialog *dialog)
264 if (response == GTK_RESPONSE_OK) {
265 new_chatroom_dialog_join (dialog);
268 gtk_widget_destroy (widget);
272 new_chatroom_dialog_destroy_cb (GtkWidget *widget,
273 GossipNewChatroomDialog *dialog)
275 g_object_unref (dialog->model);
276 g_object_unref (dialog->filter);
282 new_chatroom_dialog_model_setup (GossipNewChatroomDialog *dialog)
286 GtkTreeSelection *selection;
289 view = GTK_TREE_VIEW (dialog->treeview);
291 g_signal_connect (view, "row-activated",
292 G_CALLBACK (new_chatroom_dialog_model_row_activated_cb),
296 store = gtk_list_store_new (COL_COUNT,
297 G_TYPE_STRING, /* Image */
298 G_TYPE_STRING, /* Text */
299 G_TYPE_POINTER); /* infos */
301 dialog->model = GTK_TREE_MODEL (store);
304 dialog->filter = gtk_tree_model_filter_new (dialog->model, NULL);
306 gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (dialog->filter),
307 (GtkTreeModelFilterVisibleFunc)
308 new_chatroom_dialog_model_filter_func,
312 gtk_tree_view_set_model (view, dialog->filter);
314 g_signal_connect (dialog->filter, "row-inserted",
315 G_CALLBACK (new_chatroom_dialog_model_row_inserted_cb),
317 g_signal_connect (dialog->filter, "row-deleted",
318 G_CALLBACK (new_chatroom_dialog_model_row_deleted_cb),
322 selection = gtk_tree_view_get_selection (view);
323 gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
324 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
325 COL_NAME, GTK_SORT_ASCENDING);
327 g_signal_connect (selection, "changed",
328 G_CALLBACK (new_chatroom_dialog_model_selection_changed), dialog);
331 new_chatroom_dialog_model_add_columns (dialog);
335 new_chatroom_dialog_model_add_columns (GossipNewChatroomDialog *dialog)
338 GtkTreeViewColumn *column;
339 GtkCellRenderer *cell;
341 view = GTK_TREE_VIEW (dialog->treeview);
342 gtk_tree_view_set_headers_visible (view, FALSE);
344 /* Chatroom pointer */
345 column = gtk_tree_view_column_new ();
346 gtk_tree_view_column_set_title (column, _("Chat Rooms"));
348 cell = gtk_cell_renderer_pixbuf_new ();
349 gtk_tree_view_column_pack_start (column, cell, FALSE);
351 cell = gtk_cell_renderer_text_new ();
355 "ellipsize", PANGO_ELLIPSIZE_END,
358 gtk_tree_view_column_pack_start (column, cell, TRUE);
360 gtk_tree_view_column_set_expand (column, TRUE);
361 gtk_tree_view_append_column (view, column);
365 new_chatroom_dialog_update_buttons (GossipNewChatroomDialog *dialog)
374 /* Sort out Join button. */
375 button = GTK_BUTTON (dialog->button_join);
377 image = gtk_button_get_image (button);
379 image = gtk_image_new ();
380 gtk_button_set_image (button, image);
382 //gtk_button_set_use_stock (button, FALSE);
384 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
386 /* Collect necessary information first. */
387 view = GTK_TREE_VIEW (dialog->treeview);
388 model = gtk_tree_view_get_model (view);
389 items = gtk_tree_model_iter_n_children (model, NULL);
391 if (items < 1 && !G_STR_EMPTY (room)) {
392 gtk_button_set_label (button, _("Create"));
393 gtk_image_set_from_stock (GTK_IMAGE (image),
395 GTK_ICON_SIZE_BUTTON);
397 gtk_button_set_label (button, _("Join"));
398 gtk_image_set_from_stock (GTK_IMAGE (image),
400 GTK_ICON_SIZE_BUTTON);
403 gtk_widget_set_sensitive (dialog->button_join, !G_STR_EMPTY (room));
407 new_chatroom_dialog_update_widgets (GossipNewChatroomDialog *dialog)
409 GossipAccountChooser *account_chooser;
412 const gchar *protocol;
414 account_chooser = GOSSIP_ACCOUNT_CHOOSER (dialog->account_chooser);
415 account = gossip_account_chooser_get_account (account_chooser);
416 profile = mc_account_get_profile (account);
417 protocol = mc_profile_get_protocol_name (profile);
419 /* hardcode here known protocols */
420 if (strcmp (protocol, "jabber") == 0) {
423 server = mc_profile_get_default_account_domain (profile);
425 gchar *conference_server;
427 conference_server = g_strconcat ("conference.",
429 gtk_entry_set_text (GTK_ENTRY (dialog->entry_server),
431 g_free (conference_server);
434 gtk_widget_show (dialog->hbox_server);
435 gtk_widget_show (dialog->hbox_nick);
436 gtk_widget_show (dialog->vbox_browse);
439 else if (strcmp (protocol, "salut") == 0) {
440 gtk_widget_hide (dialog->hbox_server);
441 gtk_widget_show (dialog->hbox_nick);
442 gtk_widget_show (dialog->vbox_browse);
444 else if (strcmp (protocol, "irc") == 0) {
445 gtk_widget_hide (dialog->hbox_server);
446 gtk_widget_hide (dialog->hbox_nick);
447 gtk_widget_show (dialog->vbox_browse);
449 gtk_widget_hide (dialog->hbox_server);
450 gtk_widget_hide (dialog->hbox_nick);
451 gtk_widget_hide (dialog->vbox_browse);
454 new_chatroom_dialog_update_buttons (dialog);
456 /* Final set up of the dialog */
457 gtk_widget_grab_focus (dialog->entry_room);
459 g_object_unref (account);
460 g_object_unref (profile);
464 new_chatroom_dialog_account_changed_cb (GtkComboBox *combobox,
465 GossipNewChatroomDialog *dialog)
467 new_chatroom_dialog_update_widgets (dialog);
471 new_chatroom_dialog_model_add (GossipNewChatroomDialog *dialog,
472 EmpathyRoomListItem *item)
475 GtkTreeSelection *selection;
480 view = GTK_TREE_VIEW (dialog->treeview);
481 selection = gtk_tree_view_get_selection (view);
482 store = GTK_LIST_STORE (dialog->model);
484 gtk_list_store_append (store, &iter);
486 gtk_list_store_set (store, &iter,
487 COL_NAME, item->name,
493 new_chatroom_dialog_model_clear (GossipNewChatroomDialog *dialog)
497 store = GTK_LIST_STORE (dialog->model);
498 gtk_list_store_clear (store);
502 new_chatroom_dialog_model_get_selected (GossipNewChatroomDialog *dialog)
506 GtkTreeSelection *selection;
508 GList *chatrooms = NULL;
510 view = GTK_TREE_VIEW (dialog->treeview);
511 selection = gtk_tree_view_get_selection (view);
512 model = gtk_tree_view_get_model (view);
514 rows = gtk_tree_selection_get_selected_rows (selection, NULL);
515 for (l = rows; l; l = l->next) {
517 EmpathyRoomListItem *chatroom;
519 if (!gtk_tree_model_get_iter (model, &iter, l->data)) {
523 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
524 chatrooms = g_list_append (chatrooms, chatroom);
527 g_list_foreach (rows, (GFunc) gtk_tree_path_free, NULL);
534 new_chatroom_dialog_model_filter_func (GtkTreeModel *model,
536 GossipNewChatroomDialog *dialog)
538 EmpathyRoomListItem *chatroom;
542 gboolean found = FALSE;
544 gtk_tree_model_get (model, iter, COL_POINTER, &chatroom, -1);
550 text = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
553 room_nocase = g_utf8_casefold (chatroom->id, -1);
554 text_nocase = g_utf8_casefold (text, -1);
557 if (g_utf8_strlen (text_nocase, -1) < 1 ||
558 strstr (room_nocase, text_nocase)) {
562 g_free (room_nocase);
563 g_free (text_nocase);
569 new_chatroom_dialog_model_row_activated_cb (GtkTreeView *tree_view,
571 GtkTreeViewColumn *column,
572 GossipNewChatroomDialog *dialog)
574 gtk_widget_activate (dialog->button_join);
578 new_chatroom_dialog_model_row_inserted_cb (GtkTreeModel *model,
581 GossipNewChatroomDialog *dialog)
583 new_chatroom_dialog_update_buttons (dialog);
587 new_chatroom_dialog_model_row_deleted_cb (GtkTreeModel *model,
589 GossipNewChatroomDialog *dialog)
591 new_chatroom_dialog_update_buttons (dialog);
595 new_chatroom_dialog_model_selection_changed (GtkTreeSelection *selection,
596 GossipNewChatroomDialog *dialog)
598 new_chatroom_dialog_update_buttons (dialog);
602 new_chatroom_dialog_join (GossipNewChatroomDialog *dialog)
605 GossipAccountChooser *account_chooser;
608 GList *chatrooms, *l;
610 const gchar *server = NULL;
611 gchar *room_name = NULL;
612 const gchar *room_names[2] = {NULL, NULL};
614 chatrooms = new_chatroom_dialog_model_get_selected (dialog);
616 for (l = chatrooms; l; l = l->next) {
619 g_list_free (chatrooms);
623 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
624 if (GTK_WIDGET_VISIBLE (dialog->hbox_server)) {
625 server = gtk_entry_get_text (GTK_ENTRY (dialog->entry_server));
627 account_chooser = GOSSIP_ACCOUNT_CHOOSER (dialog->account_chooser);
628 account = gossip_account_chooser_get_account (account_chooser);
629 mc = gossip_mission_control_new ();
630 tp_conn = mission_control_get_connection (mc, account, NULL);
637 if (!G_STR_EMPTY (server)) {
638 room_name = g_strconcat (room, "@", server, NULL);
639 room_names[0] = room_name;
641 room_names[0] = room;
644 gossip_debug (DEBUG_DOMAIN, "Requesting handle for room '%s'",
647 /* Gives the ref of account/tp_conn to the callback */
648 tp_conn_request_handles_async (DBUS_G_PROXY (tp_conn),
651 (tp_conn_request_handles_reply)
652 new_chatroom_dialog_request_handles_cb,
659 new_chatroom_dialog_request_handles_cb (DBusGProxy *proxy,
668 gossip_debug (DEBUG_DOMAIN,
669 "Error requesting room handle: %s",
670 error ? error->message : "No error given");
674 mc = gossip_mission_control_new ();
675 handle = g_array_index (handles, guint, 0);
677 gossip_debug (DEBUG_DOMAIN, "Got handle %d, requesting channel", handle);
678 mission_control_request_channel (mc,
680 TP_IFACE_CHANNEL_TYPE_TEXT,
687 g_object_unref (account);
688 g_object_unref (proxy);
692 new_chatroom_dialog_entry_changed_cb (GtkWidget *entry,
693 GossipNewChatroomDialog *dialog)
695 if (entry == dialog->entry_room) {
696 gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (dialog->filter));
699 new_chatroom_dialog_update_buttons (dialog);
703 new_chatroom_dialog_browse_start (GossipNewChatroomDialog *dialog)
706 new_chatroom_dialog_model_clear (dialog);
707 new_chatroom_dialog_model_add (dialog, NULL);
712 new_chatroom_dialog_browse_stop (GossipNewChatroomDialog *dialog)
717 new_chatroom_dialog_entry_server_activate_cb (GtkWidget *widget,
718 GossipNewChatroomDialog *dialog)
720 new_chatroom_dialog_togglebutton_refresh_toggled_cb (dialog->togglebutton_refresh,
725 new_chatroom_dialog_togglebutton_refresh_toggled_cb (GtkWidget *widget,
726 GossipNewChatroomDialog *dialog)
730 toggled = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
733 new_chatroom_dialog_browse_start (dialog);
735 new_chatroom_dialog_browse_stop (dialog);