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>
32 #include <glib/gi18n.h>
34 #include <libmissioncontrol/mission-control.h>
35 #include <libmissioncontrol/mc-account.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 *button_close;
70 } 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_expander_browse_activate_cb (GtkWidget *widget,
115 EmpathyNewChatroomDialog *dialog);
116 static gboolean new_chatroom_dialog_entry_server_focus_out_cb (GtkWidget *widget,
117 GdkEventFocus *event,
118 EmpathyNewChatroomDialog *dialog);
120 static EmpathyNewChatroomDialog *dialog_p = NULL;
123 empathy_new_chatroom_dialog_show (GtkWindow *parent)
125 EmpathyNewChatroomDialog *dialog;
127 GtkSizeGroup *size_group;
131 gtk_window_present (GTK_WINDOW (dialog_p->window));
135 dialog_p = dialog = g_new0 (EmpathyNewChatroomDialog, 1);
137 filename = empathy_file_lookup ("empathy-new-chatroom-dialog.ui", "src");
138 gui = empathy_builder_get_file (filename,
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 "treeview", &dialog->treeview,
147 "button_join", &dialog->button_join,
148 "expander_browse", &dialog->expander_browse,
152 empathy_builder_connect (gui, dialog,
153 "new_chatroom_dialog", "response", new_chatroom_dialog_response_cb,
154 "new_chatroom_dialog", "destroy", new_chatroom_dialog_destroy_cb,
155 "entry_server", "changed", new_chatroom_dialog_entry_changed_cb,
156 "entry_server", "activate", new_chatroom_dialog_entry_server_activate_cb,
157 "entry_server", "focus-out-event", new_chatroom_dialog_entry_server_focus_out_cb,
158 "entry_room", "changed", new_chatroom_dialog_entry_changed_cb,
159 "expander_browse", "activate", new_chatroom_dialog_expander_browse_activate_cb,
162 g_object_unref (gui);
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);
182 gtk_table_attach (GTK_TABLE (dialog->table_info),
187 /* Account chooser for custom */
188 dialog->account_chooser = empathy_account_chooser_new ();
189 empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser),
190 empathy_account_chooser_filter_is_connected,
192 gtk_table_attach_defaults (GTK_TABLE (dialog->table_info),
193 dialog->account_chooser,
195 gtk_widget_show (dialog->account_chooser);
197 g_signal_connect (GTK_COMBO_BOX (dialog->account_chooser), "changed",
198 G_CALLBACK (new_chatroom_dialog_account_changed_cb),
200 new_chatroom_dialog_account_changed_cb (GTK_COMBO_BOX (dialog->account_chooser),
204 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
205 GTK_WINDOW (parent));
208 gtk_widget_show (dialog->window);
212 new_chatroom_dialog_response_cb (GtkWidget *widget,
214 EmpathyNewChatroomDialog *dialog)
216 if (response == GTK_RESPONSE_OK) {
217 new_chatroom_dialog_join (dialog);
220 gtk_widget_destroy (widget);
224 new_chatroom_dialog_destroy_cb (GtkWidget *widget,
225 EmpathyNewChatroomDialog *dialog)
227 if (dialog->room_list) {
228 g_object_unref (dialog->room_list);
230 g_object_unref (dialog->model);
236 new_chatroom_dialog_model_setup (EmpathyNewChatroomDialog *dialog)
240 GtkTreeSelection *selection;
243 view = GTK_TREE_VIEW (dialog->treeview);
245 g_signal_connect (view, "row-activated",
246 G_CALLBACK (new_chatroom_dialog_model_row_activated_cb),
250 store = gtk_list_store_new (COL_COUNT,
251 G_TYPE_STRING, /* Invite */
252 G_TYPE_STRING, /* Password */
253 G_TYPE_STRING, /* Name */
254 G_TYPE_STRING, /* Room */
255 G_TYPE_STRING, /* Member count */
256 G_TYPE_STRING); /* Tool tip */
258 dialog->model = GTK_TREE_MODEL (store);
259 gtk_tree_view_set_model (view, dialog->model);
260 gtk_tree_view_set_tooltip_column (view, COL_TOOLTIP);
261 gtk_tree_view_set_search_column (view, COL_NAME);
264 selection = gtk_tree_view_get_selection (view);
265 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
266 COL_NAME, GTK_SORT_ASCENDING);
268 g_signal_connect (selection, "changed",
269 G_CALLBACK (new_chatroom_dialog_model_selection_changed),
273 new_chatroom_dialog_model_add_columns (dialog);
277 new_chatroom_dialog_model_add_columns (EmpathyNewChatroomDialog *dialog)
280 GtkTreeViewColumn *column;
281 GtkCellRenderer *cell;
284 gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &width, &height);
286 view = GTK_TREE_VIEW (dialog->treeview);
288 cell = gtk_cell_renderer_pixbuf_new ();
292 "stock-size", GTK_ICON_SIZE_MENU,
294 column = gtk_tree_view_column_new_with_attributes (NULL,
296 "stock-id", COL_INVITE_ONLY,
298 gtk_tree_view_append_column (view, column);
300 column = gtk_tree_view_column_new_with_attributes (NULL,
302 "stock-id", COL_NEED_PASSWORD,
304 gtk_tree_view_append_column (view, column);
306 cell = gtk_cell_renderer_text_new ();
310 "ellipsize", PANGO_ELLIPSIZE_END,
313 column = gtk_tree_view_column_new_with_attributes (_("Chat Room"),
318 gtk_tree_view_column_set_expand (column, TRUE);
319 gtk_tree_view_append_column (view, column);
321 cell = gtk_cell_renderer_text_new ();
325 "ellipsize", PANGO_ELLIPSIZE_END,
326 "alignment", PANGO_ALIGN_RIGHT,
328 column = gtk_tree_view_column_new_with_attributes (_("Members"),
332 gtk_tree_view_append_column (view, column);
336 new_chatroom_dialog_update_widgets (EmpathyNewChatroomDialog *dialog)
338 EmpathyAccountChooser *account_chooser;
341 const gchar *protocol;
344 account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
345 account = empathy_account_chooser_dup_account (account_chooser);
346 profile = mc_account_get_profile (account);
347 protocol = mc_profile_get_protocol_name (profile);
349 gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), "");
351 /* hardcode here known protocols */
352 if (strcmp (protocol, "jabber") == 0) {
353 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
355 else if (strcmp (protocol, "local-xmpp") == 0) {
356 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
358 else if (strcmp (protocol, "irc") == 0) {
359 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
362 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
365 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
366 gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
368 /* Final set up of the dialog */
369 gtk_widget_grab_focus (dialog->entry_room);
371 g_object_unref (account);
372 g_object_unref (profile);
376 new_chatroom_dialog_account_changed_cb (GtkComboBox *combobox,
377 EmpathyNewChatroomDialog *dialog)
379 EmpathyAccountChooser *account_chooser;
381 gboolean listing = FALSE;
383 if (dialog->room_list) {
384 g_object_unref (dialog->room_list);
387 ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
388 new_chatroom_dialog_model_clear (dialog);
390 account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
391 account = empathy_account_chooser_dup_account (account_chooser);
392 dialog->room_list = empathy_tp_roomlist_new (account);
394 if (dialog->room_list) {
395 g_signal_connect (dialog->room_list, "destroy",
396 G_CALLBACK (new_chatroom_dialog_roomlist_destroy_cb),
398 g_signal_connect (dialog->room_list, "new-room",
399 G_CALLBACK (new_chatroom_dialog_new_room_cb),
401 g_signal_connect (dialog->room_list, "notify::is-listing",
402 G_CALLBACK (new_chatroom_dialog_listing_cb),
405 listing = empathy_tp_roomlist_is_listing (dialog->room_list);
407 ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
411 new_chatroom_dialog_update_widgets (dialog);
413 g_object_unref (account);
417 new_chatroom_dialog_roomlist_destroy_cb (EmpathyTpRoomlist *room_list,
418 EmpathyNewChatroomDialog *dialog)
420 g_object_unref (dialog->room_list);
421 dialog->room_list = NULL;
425 new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist *room_list,
426 EmpathyChatroom *chatroom,
427 EmpathyNewChatroomDialog *dialog)
430 GtkTreeSelection *selection;
435 const gchar *need_password;
436 const gchar *invite_only;
438 DEBUG ("New chatroom listed: %s (%s)",
439 empathy_chatroom_get_name (chatroom),
440 empathy_chatroom_get_room (chatroom));
443 view = GTK_TREE_VIEW (dialog->treeview);
444 selection = gtk_tree_view_get_selection (view);
445 store = GTK_LIST_STORE (dialog->model);
446 members = g_strdup_printf ("%d", empathy_chatroom_get_members_count (chatroom));
447 tooltip = g_strdup_printf (C_("Room/Join's roomlist tooltip. Parameters"
448 "are a channel name, yes/no, yes/no and a number.",
449 "<b>%s</b>\nInvite required: %s\nPassword required: %s\nMembers: %s"),
450 empathy_chatroom_get_name (chatroom),
451 empathy_chatroom_get_invite_only (chatroom) ? _("Yes") : _("No"),
452 empathy_chatroom_get_need_password (chatroom) ? _("Yes") : _("No"),
454 invite_only = (empathy_chatroom_get_invite_only (chatroom) ?
455 GTK_STOCK_INDEX : NULL);
456 need_password = (empathy_chatroom_get_need_password (chatroom) ?
457 GTK_STOCK_DIALOG_AUTHENTICATION : NULL);
459 gtk_list_store_append (store, &iter);
460 gtk_list_store_set (store, &iter,
461 COL_NEED_PASSWORD, need_password,
462 COL_INVITE_ONLY, invite_only,
463 COL_NAME, empathy_chatroom_get_name (chatroom),
464 COL_ROOM, empathy_chatroom_get_room (chatroom),
465 COL_MEMBERS, members,
466 COL_TOOLTIP, tooltip,
474 new_chatroom_dialog_listing_cb (EmpathyTpRoomlist *room_list,
476 EmpathyNewChatroomDialog *dialog)
480 listing = empathy_tp_roomlist_is_listing (room_list);
482 /* Update the throbber */
484 ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
486 ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
491 new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog)
495 store = GTK_LIST_STORE (dialog->model);
496 gtk_list_store_clear (store);
500 new_chatroom_dialog_model_row_activated_cb (GtkTreeView *tree_view,
502 GtkTreeViewColumn *column,
503 EmpathyNewChatroomDialog *dialog)
505 gtk_widget_activate (dialog->button_join);
509 new_chatroom_dialog_model_selection_changed (GtkTreeSelection *selection,
510 EmpathyNewChatroomDialog *dialog)
515 gchar *server = NULL;
517 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
521 gtk_tree_model_get (model, &iter, COL_ROOM, &room, -1);
522 server = strstr (room, "@");
528 gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), server ? server : "");
529 gtk_entry_set_text (GTK_ENTRY (dialog->entry_room), room ? room : "");
535 new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog)
537 EmpathyAccountChooser *account_chooser;
538 TpConnection *connection;
540 const gchar *server = NULL;
541 gchar *room_name = NULL;
543 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
544 server = gtk_entry_get_text (GTK_ENTRY (dialog->entry_server));
546 account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
547 connection = empathy_account_chooser_get_connection (account_chooser);
549 if (!EMP_STR_EMPTY (server)) {
550 room_name = g_strconcat (room, "@", server, NULL);
552 room_name = g_strdup (room);
555 DEBUG ("Requesting channel for '%s'", room_name);
556 empathy_dispatcher_join_muc (connection, room_name, NULL, NULL);
562 new_chatroom_dialog_entry_changed_cb (GtkWidget *entry,
563 EmpathyNewChatroomDialog *dialog)
565 if (entry == dialog->entry_room) {
568 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
569 gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
570 /* FIXME: Select the room in the list */
575 new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog)
577 new_chatroom_dialog_model_clear (dialog);
578 if (dialog->room_list) {
579 empathy_tp_roomlist_start (dialog->room_list);
584 new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog)
586 if (dialog->room_list) {
587 empathy_tp_roomlist_stop (dialog->room_list);
592 new_chatroom_dialog_entry_server_activate_cb (GtkWidget *widget,
593 EmpathyNewChatroomDialog *dialog)
595 new_chatroom_dialog_browse_start (dialog);
599 new_chatroom_dialog_expander_browse_activate_cb (GtkWidget *widget,
600 EmpathyNewChatroomDialog *dialog)
604 expanded = gtk_expander_get_expanded (GTK_EXPANDER (widget));
606 new_chatroom_dialog_browse_stop (dialog);
607 gtk_window_set_resizable (GTK_WINDOW (dialog->window), FALSE);
609 new_chatroom_dialog_browse_start (dialog);
610 gtk_window_set_resizable (GTK_WINDOW (dialog->window), TRUE);
615 new_chatroom_dialog_entry_server_focus_out_cb (GtkWidget *widget,
616 GdkEventFocus *event,
617 EmpathyNewChatroomDialog *dialog)
621 expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
623 new_chatroom_dialog_browse_start (dialog);