1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2004-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: Xavier Claessens <xclaesse@gmail.com>
22 * Martyn Russell <martyn@imendio.com>
23 * Mikael Hallendal <micke@imendio.com>
33 #include <glib/gi18n.h>
35 #include <libempathy/empathy-chatroom-manager.h>
36 #include <libempathy/empathy-utils.h>
38 #include <libempathy-gtk/empathy-account-chooser.h>
39 #include <libempathy-gtk/empathy-ui-utils.h>
41 #include "empathy-chatrooms-window.h"
42 #include "empathy-new-chatroom-dialog.h"
45 EmpathyChatroomManager *manager;
48 GtkWidget *hbox_account;
49 GtkWidget *label_account;
50 GtkWidget *account_chooser;
52 GtkWidget *button_remove;
53 GtkWidget *button_edit;
54 GtkWidget *button_close;
55 } EmpathyChatroomsWindow;
57 static void chatrooms_window_destroy_cb (GtkWidget *widget,
58 EmpathyChatroomsWindow *window);
59 static void chatrooms_window_model_setup (EmpathyChatroomsWindow *window);
60 static void chatrooms_window_model_add_columns (EmpathyChatroomsWindow *window);
61 static void chatrooms_window_model_refresh_data (EmpathyChatroomsWindow *window,
63 static void chatrooms_window_model_add (EmpathyChatroomsWindow *window,
64 EmpathyChatroom *chatroom,
66 static void chatrooms_window_model_cell_auto_connect_toggled (GtkCellRendererToggle *cell,
68 EmpathyChatroomsWindow *window);
69 static EmpathyChatroom * chatrooms_window_model_get_selected (EmpathyChatroomsWindow *window);
70 static void chatrooms_window_model_action_selected (EmpathyChatroomsWindow *window);
71 static void chatrooms_window_row_activated_cb (GtkTreeView *tree_view,
73 GtkTreeViewColumn *column,
74 EmpathyChatroomsWindow *window);
75 static void chatrooms_window_button_remove_clicked_cb (GtkWidget *widget,
76 EmpathyChatroomsWindow *window);
77 static void chatrooms_window_button_edit_clicked_cb (GtkWidget *widget,
78 EmpathyChatroomsWindow *window);
79 static void chatrooms_window_button_close_clicked_cb (GtkWidget *widget,
80 EmpathyChatroomsWindow *window);
81 static void chatrooms_window_chatroom_added_cb (EmpathyChatroomManager *manager,
82 EmpathyChatroom *chatroom,
83 EmpathyChatroomsWindow *window);
84 static void chatrooms_window_chatroom_removed_cb (EmpathyChatroomManager *manager,
85 EmpathyChatroom *chatroom,
86 EmpathyChatroomsWindow *window);
87 static gboolean chatrooms_window_remove_chatroom_foreach (GtkTreeModel *model,
90 EmpathyChatroom *chatroom);
91 static void chatrooms_window_account_changed_cb (GtkWidget *combo_box,
92 EmpathyChatroomsWindow *window);
104 empathy_chatrooms_window_show (GtkWindow *parent)
106 static EmpathyChatroomsWindow *window = NULL;
111 gtk_window_present (GTK_WINDOW (window->window));
115 window = g_new0 (EmpathyChatroomsWindow, 1);
117 filename = empathy_file_lookup ("empathy-chatrooms-window.ui", "src");
118 gui = empathy_builder_get_file (filename,
119 "chatrooms_window", &window->window,
120 "hbox_account", &window->hbox_account,
121 "label_account", &window->label_account,
122 "treeview", &window->treeview,
123 "button_edit", &window->button_edit,
124 "button_remove", &window->button_remove,
125 "button_close", &window->button_close,
129 empathy_builder_connect (gui, window,
130 "chatrooms_window", "destroy", chatrooms_window_destroy_cb,
131 "button_remove", "clicked", chatrooms_window_button_remove_clicked_cb,
132 "button_edit", "clicked", chatrooms_window_button_edit_clicked_cb,
133 "button_close", "clicked", chatrooms_window_button_close_clicked_cb,
136 g_object_unref (gui);
138 g_object_add_weak_pointer (G_OBJECT (window->window), (gpointer) &window);
140 /* Get the session and chat room manager */
141 window->manager = empathy_chatroom_manager_dup_singleton (NULL);
143 g_signal_connect (window->manager, "chatroom-added",
144 G_CALLBACK (chatrooms_window_chatroom_added_cb),
146 g_signal_connect (window->manager, "chatroom-removed",
147 G_CALLBACK (chatrooms_window_chatroom_removed_cb),
150 /* Account chooser for chat rooms */
151 window->account_chooser = empathy_account_chooser_new ();
152 empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (window->account_chooser),
153 empathy_account_chooser_filter_is_connected,
155 g_object_set (window->account_chooser,
156 "has-all-option", TRUE,
158 empathy_account_chooser_set_account (EMPATHY_ACCOUNT_CHOOSER (window->account_chooser), NULL);
160 gtk_box_pack_start (GTK_BOX (window->hbox_account),
161 window->account_chooser,
164 g_signal_connect (window->account_chooser, "changed",
165 G_CALLBACK (chatrooms_window_account_changed_cb),
168 gtk_widget_show (window->account_chooser);
170 /* Set up chatrooms */
171 chatrooms_window_model_setup (window);
174 gtk_widget_grab_focus (window->treeview);
178 gtk_window_set_transient_for (GTK_WINDOW (window->window),
179 GTK_WINDOW (parent));
182 gtk_widget_show (window->window);
186 chatrooms_window_destroy_cb (GtkWidget *widget,
187 EmpathyChatroomsWindow *window)
189 g_signal_handlers_disconnect_by_func (window->manager,
190 chatrooms_window_chatroom_added_cb,
192 g_signal_handlers_disconnect_by_func (window->manager,
193 chatrooms_window_chatroom_removed_cb,
195 g_object_unref (window->manager);
200 chatrooms_window_model_setup (EmpathyChatroomsWindow *window)
204 GtkTreeSelection *selection;
207 view = GTK_TREE_VIEW (window->treeview);
209 g_signal_connect (view, "row-activated",
210 G_CALLBACK (chatrooms_window_row_activated_cb),
214 store = gtk_list_store_new (COL_COUNT,
215 G_TYPE_STRING, /* Image */
216 G_TYPE_STRING, /* Name */
217 G_TYPE_STRING, /* Room */
218 G_TYPE_BOOLEAN, /* Auto start */
219 EMPATHY_TYPE_CHATROOM); /* Chatroom */
221 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
224 selection = gtk_tree_view_get_selection (view);
225 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
228 chatrooms_window_model_add_columns (window);
231 chatrooms_window_model_refresh_data (window, TRUE);
234 g_object_unref (store);
238 chatrooms_window_model_add_columns (EmpathyChatroomsWindow *window)
242 GtkTreeViewColumn *column;
243 GtkCellRenderer *cell;
246 view = GTK_TREE_VIEW (window->treeview);
247 model = gtk_tree_view_get_model (view);
249 gtk_tree_view_set_headers_visible (view, TRUE);
250 gtk_tree_view_set_headers_clickable (view, TRUE);
253 column = gtk_tree_view_column_new ();
254 count = gtk_tree_view_append_column (view, column);
256 gtk_tree_view_column_set_title (column, _("Name"));
257 gtk_tree_view_column_set_expand (column, TRUE);
258 gtk_tree_view_column_set_sort_column_id (column, count - 1);
260 cell = gtk_cell_renderer_pixbuf_new ();
261 gtk_tree_view_column_pack_start (column, cell, FALSE);
262 gtk_tree_view_column_add_attribute (column, cell, "icon-name", COL_IMAGE);
264 cell = gtk_cell_renderer_text_new ();
269 gtk_tree_view_column_pack_start (column, cell, TRUE);
270 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
273 cell = gtk_cell_renderer_text_new ();
274 column = gtk_tree_view_column_new_with_attributes (_("Room"), cell,
277 count = gtk_tree_view_append_column (view, column);
278 gtk_tree_view_column_set_sort_column_id (column, count - 1);
280 /* Chatroom auto connect */
281 cell = gtk_cell_renderer_toggle_new ();
282 column = gtk_tree_view_column_new_with_attributes (_("Auto-Connect"), cell,
283 "active", COL_AUTO_CONNECT,
285 count = gtk_tree_view_append_column (view, column);
286 gtk_tree_view_column_set_sort_column_id (column, count - 1);
288 g_signal_connect (cell, "toggled",
289 G_CALLBACK (chatrooms_window_model_cell_auto_connect_toggled),
293 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model), 0,
298 chatrooms_window_model_refresh_data (EmpathyChatroomsWindow *window,
302 GtkTreeSelection *selection;
306 EmpathyAccountChooser *account_chooser;
308 GList *chatrooms, *l;
310 view = GTK_TREE_VIEW (window->treeview);
311 selection = gtk_tree_view_get_selection (view);
312 model = gtk_tree_view_get_model (view);
313 store = GTK_LIST_STORE (model);
315 /* Look up chatrooms */
316 account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
317 account = empathy_account_chooser_dup_account (account_chooser);
319 chatrooms = empathy_chatroom_manager_get_chatrooms (window->manager, account);
321 /* Clean out the store */
322 gtk_list_store_clear (store);
324 /* Populate with chatroom list. */
325 for (l = chatrooms; l; l = l->next) {
326 chatrooms_window_model_add (window, l->data, FALSE);
329 if (gtk_tree_model_get_iter_first (model, &iter)) {
330 gtk_tree_selection_select_iter (selection, &iter);
334 g_object_unref (account);
337 g_list_free (chatrooms);
341 chatrooms_window_model_add (EmpathyChatroomsWindow *window,
342 EmpathyChatroom *chatroom,
347 GtkTreeSelection *selection;
351 view = GTK_TREE_VIEW (window->treeview);
352 selection = gtk_tree_view_get_selection (view);
353 model = gtk_tree_view_get_model (view);
354 store = GTK_LIST_STORE (model);
356 gtk_list_store_append (store, &iter);
357 gtk_list_store_set (store, &iter,
358 COL_NAME, empathy_chatroom_get_name (chatroom),
359 COL_ROOM, empathy_chatroom_get_room (chatroom),
360 COL_AUTO_CONNECT, empathy_chatroom_get_auto_connect (chatroom),
361 COL_POINTER, chatroom,
365 gtk_tree_selection_select_iter (selection, &iter);
370 chatrooms_window_model_cell_auto_connect_toggled (GtkCellRendererToggle *cell,
372 EmpathyChatroomsWindow *window)
374 EmpathyChatroom *chatroom;
382 view = GTK_TREE_VIEW (window->treeview);
383 model = gtk_tree_view_get_model (view);
384 store = GTK_LIST_STORE (model);
386 path = gtk_tree_path_new_from_string (path_string);
388 gtk_tree_model_get_iter (model, &iter, path);
389 gtk_tree_model_get (model, &iter,
390 COL_AUTO_CONNECT, &enabled,
391 COL_POINTER, &chatroom,
396 empathy_chatroom_set_auto_connect (chatroom, enabled);
398 gtk_list_store_set (store, &iter, COL_AUTO_CONNECT, enabled, -1);
399 gtk_tree_path_free (path);
400 g_object_unref (chatroom);
403 static EmpathyChatroom *
404 chatrooms_window_model_get_selected (EmpathyChatroomsWindow *window)
408 GtkTreeSelection *selection;
410 EmpathyChatroom *chatroom = NULL;
412 view = GTK_TREE_VIEW (window->treeview);
413 selection = gtk_tree_view_get_selection (view);
415 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
416 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
423 chatrooms_window_model_action_selected (EmpathyChatroomsWindow *window)
425 EmpathyChatroom *chatroom;
427 /* FIXME: This is still not implemented since Gossip fork */
428 chatroom = chatrooms_window_model_get_selected (window);
429 g_object_unref (chatroom);
433 chatrooms_window_row_activated_cb (GtkTreeView *tree_view,
435 GtkTreeViewColumn *column,
436 EmpathyChatroomsWindow *window)
438 if (gtk_widget_is_sensitive (window->button_edit)) {
439 chatrooms_window_model_action_selected (window);
444 chatrooms_window_button_remove_clicked_cb (GtkWidget *widget,
445 EmpathyChatroomsWindow *window)
447 EmpathyChatroom *chatroom;
450 GtkTreeSelection *selection;
453 /* Remove from treeview */
454 view = GTK_TREE_VIEW (window->treeview);
455 selection = gtk_tree_view_get_selection (view);
457 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
461 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
462 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
464 /* Remove from config */
465 empathy_chatroom_manager_remove (window->manager, chatroom);
467 g_object_unref (chatroom);
471 chatrooms_window_button_edit_clicked_cb (GtkWidget *widget,
472 EmpathyChatroomsWindow *window)
474 EmpathyChatroom *chatroom;
476 /* FIXME: This is still not implemented since Gossip fork */
477 chatroom = chatrooms_window_model_get_selected (window);
478 g_object_unref (chatroom);
482 chatrooms_window_button_close_clicked_cb (GtkWidget *widget,
483 EmpathyChatroomsWindow *window)
485 gtk_widget_destroy (window->window);
489 chatrooms_window_chatroom_added_cb (EmpathyChatroomManager *manager,
490 EmpathyChatroom *chatroom,
491 EmpathyChatroomsWindow *window)
493 EmpathyAccountChooser *account_chooser;
496 account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
497 account = empathy_account_chooser_dup_account (account_chooser);
500 chatrooms_window_model_add (window, chatroom, FALSE);
502 if (account == empathy_chatroom_get_account (chatroom)) {
503 chatrooms_window_model_add (window, chatroom, FALSE);
506 g_object_unref (account);
511 chatrooms_window_chatroom_removed_cb (EmpathyChatroomManager *manager,
512 EmpathyChatroom *chatroom,
513 EmpathyChatroomsWindow *window)
517 model = gtk_tree_view_get_model (GTK_TREE_VIEW (window->treeview));
519 gtk_tree_model_foreach (model,
520 (GtkTreeModelForeachFunc) chatrooms_window_remove_chatroom_foreach,
525 chatrooms_window_remove_chatroom_foreach (GtkTreeModel *model,
528 EmpathyChatroom *chatroom)
530 EmpathyChatroom *this_chatroom;
532 gtk_tree_model_get (model, iter, COL_POINTER, &this_chatroom, -1);
534 if (empathy_chatroom_equal (chatroom, this_chatroom)) {
535 gtk_list_store_remove (GTK_LIST_STORE (model), iter);
536 g_object_unref (this_chatroom);
540 g_object_unref (this_chatroom);
546 chatrooms_window_account_changed_cb (GtkWidget *combo_box,
547 EmpathyChatroomsWindow *window)
549 chatrooms_window_model_refresh_data (window, FALSE);