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>
28 #include <glib/gi18n.h>
30 #include "empathy-chatroom-manager.h"
31 #include "empathy-utils.h"
33 #include "empathy-account-chooser.h"
34 #include "empathy-ui-utils.h"
36 #include "empathy-chatrooms-window.h"
39 EmpathyChatroomManager *manager;
42 GtkWidget *hbox_account;
43 GtkWidget *label_account;
44 GtkWidget *account_chooser;
46 GtkWidget *button_remove;
47 GtkWidget *button_close;
48 } EmpathyChatroomsWindow;
50 static void chatrooms_window_destroy_cb (GtkWidget *widget,
51 EmpathyChatroomsWindow *window);
52 static void chatrooms_window_model_setup (EmpathyChatroomsWindow *window);
53 static void chatrooms_window_model_add_columns (EmpathyChatroomsWindow *window);
54 static void chatrooms_window_model_refresh_data (EmpathyChatroomsWindow *window,
56 static void chatrooms_window_model_add (EmpathyChatroomsWindow *window,
57 EmpathyChatroom *chatroom,
59 static void chatrooms_window_model_cell_auto_connect_toggled (GtkCellRendererToggle *cell,
61 EmpathyChatroomsWindow *window);
62 static void chatrooms_window_button_remove_clicked_cb (GtkWidget *widget,
63 EmpathyChatroomsWindow *window);
64 static void chatrooms_window_button_close_clicked_cb (GtkWidget *widget,
65 EmpathyChatroomsWindow *window);
66 static void chatrooms_window_chatroom_added_cb (EmpathyChatroomManager *manager,
67 EmpathyChatroom *chatroom,
68 EmpathyChatroomsWindow *window);
69 static void chatrooms_window_chatroom_removed_cb (EmpathyChatroomManager *manager,
70 EmpathyChatroom *chatroom,
71 EmpathyChatroomsWindow *window);
72 static gboolean chatrooms_window_remove_chatroom_foreach (GtkTreeModel *model,
75 EmpathyChatroom *chatroom);
76 static void chatrooms_window_account_changed_cb (GtkWidget *combo_box,
77 EmpathyChatroomsWindow *window);
89 empathy_chatrooms_window_show (GtkWindow *parent)
91 static EmpathyChatroomsWindow *window = NULL;
94 GtkWidget *sw, *toolbar;
95 GtkStyleContext *context;
98 gtk_window_present (GTK_WINDOW (window->window));
102 window = g_new0 (EmpathyChatroomsWindow, 1);
104 filename = empathy_file_lookup ("empathy-chatrooms-window.ui", "src");
105 gui = empathy_builder_get_file (filename,
106 "chatrooms_window", &window->window,
107 "hbox_account", &window->hbox_account,
108 "label_account", &window->label_account,
110 "treeview", &window->treeview,
111 "toolbar_remove", &toolbar,
112 "button_remove", &window->button_remove,
113 "button_close", &window->button_close,
117 /* join the remove toolbar to the treeview */
118 context = gtk_widget_get_style_context (sw);
119 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
120 context = gtk_widget_get_style_context (toolbar);
121 gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
123 empathy_builder_connect (gui, window,
124 "chatrooms_window", "destroy", chatrooms_window_destroy_cb,
125 "button_remove", "clicked", chatrooms_window_button_remove_clicked_cb,
126 "button_close", "clicked", chatrooms_window_button_close_clicked_cb,
129 g_object_unref (gui);
131 g_object_add_weak_pointer (G_OBJECT (window->window), (gpointer) &window);
133 /* Get the session and chat room manager */
134 window->manager = empathy_chatroom_manager_dup_singleton (NULL);
136 g_signal_connect (window->manager, "chatroom-added",
137 G_CALLBACK (chatrooms_window_chatroom_added_cb),
139 g_signal_connect (window->manager, "chatroom-removed",
140 G_CALLBACK (chatrooms_window_chatroom_removed_cb),
143 /* Account chooser for chat rooms */
144 window->account_chooser = empathy_account_chooser_new ();
145 empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (window->account_chooser),
146 empathy_account_chooser_filter_supports_chatrooms,
148 g_object_set (window->account_chooser,
149 "has-all-option", TRUE,
151 empathy_account_chooser_set_account (EMPATHY_ACCOUNT_CHOOSER (window->account_chooser), NULL);
153 gtk_box_pack_start (GTK_BOX (window->hbox_account),
154 window->account_chooser,
157 g_signal_connect (window->account_chooser, "changed",
158 G_CALLBACK (chatrooms_window_account_changed_cb),
161 gtk_widget_show (window->account_chooser);
163 /* Set up chatrooms */
164 chatrooms_window_model_setup (window);
167 gtk_widget_grab_focus (window->treeview);
171 gtk_window_set_transient_for (GTK_WINDOW (window->window),
172 GTK_WINDOW (parent));
175 gtk_widget_show (window->window);
179 chatrooms_window_destroy_cb (GtkWidget *widget,
180 EmpathyChatroomsWindow *window)
182 g_signal_handlers_disconnect_by_func (window->manager,
183 chatrooms_window_chatroom_added_cb,
185 g_signal_handlers_disconnect_by_func (window->manager,
186 chatrooms_window_chatroom_removed_cb,
188 g_object_unref (window->manager);
193 chatrooms_window_model_setup (EmpathyChatroomsWindow *window)
197 GtkTreeSelection *selection;
200 view = GTK_TREE_VIEW (window->treeview);
203 store = gtk_list_store_new (COL_COUNT,
204 G_TYPE_STRING, /* Image */
205 G_TYPE_STRING, /* Name */
206 G_TYPE_STRING, /* Room */
207 G_TYPE_BOOLEAN, /* Auto start */
208 EMPATHY_TYPE_CHATROOM); /* Chatroom */
210 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
213 selection = gtk_tree_view_get_selection (view);
214 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
217 chatrooms_window_model_add_columns (window);
220 chatrooms_window_model_refresh_data (window, TRUE);
223 g_object_unref (store);
227 chatrooms_window_model_add_columns (EmpathyChatroomsWindow *window)
231 GtkTreeViewColumn *column;
232 GtkCellRenderer *cell;
235 view = GTK_TREE_VIEW (window->treeview);
236 model = gtk_tree_view_get_model (view);
238 gtk_tree_view_set_headers_visible (view, TRUE);
239 gtk_tree_view_set_headers_clickable (view, TRUE);
242 column = gtk_tree_view_column_new ();
243 count = gtk_tree_view_append_column (view, column);
245 gtk_tree_view_column_set_title (column, _("Name"));
246 gtk_tree_view_column_set_expand (column, TRUE);
247 gtk_tree_view_column_set_sort_column_id (column, count - 1);
249 cell = gtk_cell_renderer_pixbuf_new ();
250 gtk_tree_view_column_pack_start (column, cell, FALSE);
251 gtk_tree_view_column_add_attribute (column, cell, "icon-name", COL_IMAGE);
253 cell = gtk_cell_renderer_text_new ();
258 gtk_tree_view_column_pack_start (column, cell, TRUE);
259 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
262 cell = gtk_cell_renderer_text_new ();
263 column = gtk_tree_view_column_new_with_attributes (_("Room"), cell,
266 count = gtk_tree_view_append_column (view, column);
267 gtk_tree_view_column_set_sort_column_id (column, count - 1);
269 /* Chatroom auto connect */
270 cell = gtk_cell_renderer_toggle_new ();
271 column = gtk_tree_view_column_new_with_attributes (_("Auto-Connect"), cell,
272 "active", COL_AUTO_CONNECT,
274 count = gtk_tree_view_append_column (view, column);
275 gtk_tree_view_column_set_sort_column_id (column, count - 1);
277 g_signal_connect (cell, "toggled",
278 G_CALLBACK (chatrooms_window_model_cell_auto_connect_toggled),
282 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model), 0,
287 chatrooms_window_model_refresh_data (EmpathyChatroomsWindow *window,
291 GtkTreeSelection *selection;
295 EmpathyAccountChooser *account_chooser;
297 GList *chatrooms, *l;
299 view = GTK_TREE_VIEW (window->treeview);
300 selection = gtk_tree_view_get_selection (view);
301 model = gtk_tree_view_get_model (view);
302 store = GTK_LIST_STORE (model);
304 /* Look up chatrooms */
305 account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
306 account = empathy_account_chooser_dup_account (account_chooser);
308 chatrooms = empathy_chatroom_manager_get_chatrooms (window->manager, account);
310 /* Clean out the store */
311 gtk_list_store_clear (store);
313 /* Populate with chatroom list. */
314 for (l = chatrooms; l; l = l->next) {
315 chatrooms_window_model_add (window, l->data, FALSE);
318 if (gtk_tree_model_get_iter_first (model, &iter)) {
319 gtk_tree_selection_select_iter (selection, &iter);
323 g_object_unref (account);
326 g_list_free (chatrooms);
330 chatrooms_window_model_add (EmpathyChatroomsWindow *window,
331 EmpathyChatroom *chatroom,
336 GtkTreeSelection *selection;
340 view = GTK_TREE_VIEW (window->treeview);
341 selection = gtk_tree_view_get_selection (view);
342 model = gtk_tree_view_get_model (view);
343 store = GTK_LIST_STORE (model);
345 gtk_list_store_insert_with_values (store, &iter, -1,
346 COL_NAME, empathy_chatroom_get_name (chatroom),
347 COL_ROOM, empathy_chatroom_get_room (chatroom),
348 COL_AUTO_CONNECT, empathy_chatroom_get_auto_connect (chatroom),
349 COL_POINTER, chatroom,
353 gtk_tree_selection_select_iter (selection, &iter);
358 chatrooms_window_model_cell_auto_connect_toggled (GtkCellRendererToggle *cell,
360 EmpathyChatroomsWindow *window)
362 EmpathyChatroom *chatroom;
370 view = GTK_TREE_VIEW (window->treeview);
371 model = gtk_tree_view_get_model (view);
372 store = GTK_LIST_STORE (model);
374 path = gtk_tree_path_new_from_string (path_string);
376 gtk_tree_model_get_iter (model, &iter, path);
377 gtk_tree_model_get (model, &iter,
378 COL_AUTO_CONNECT, &enabled,
379 COL_POINTER, &chatroom,
384 empathy_chatroom_set_auto_connect (chatroom, enabled);
386 gtk_list_store_set (store, &iter, COL_AUTO_CONNECT, enabled, -1);
387 gtk_tree_path_free (path);
388 g_object_unref (chatroom);
392 chatrooms_window_button_remove_clicked_cb (GtkWidget *widget,
393 EmpathyChatroomsWindow *window)
395 EmpathyChatroom *chatroom;
398 GtkTreeSelection *selection;
401 /* Remove from treeview */
402 view = GTK_TREE_VIEW (window->treeview);
403 selection = gtk_tree_view_get_selection (view);
405 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
409 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
410 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
412 /* Remove from config */
413 empathy_chatroom_manager_remove (window->manager, chatroom);
415 g_object_unref (chatroom);
419 chatrooms_window_button_close_clicked_cb (GtkWidget *widget,
420 EmpathyChatroomsWindow *window)
422 gtk_widget_destroy (window->window);
426 chatrooms_window_chatroom_added_cb (EmpathyChatroomManager *manager,
427 EmpathyChatroom *chatroom,
428 EmpathyChatroomsWindow *window)
430 EmpathyAccountChooser *account_chooser;
433 account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
434 account = empathy_account_chooser_dup_account (account_chooser);
437 chatrooms_window_model_add (window, chatroom, FALSE);
439 if (account == empathy_chatroom_get_account (chatroom)) {
440 chatrooms_window_model_add (window, chatroom, FALSE);
443 g_object_unref (account);
448 chatrooms_window_chatroom_removed_cb (EmpathyChatroomManager *manager,
449 EmpathyChatroom *chatroom,
450 EmpathyChatroomsWindow *window)
454 model = gtk_tree_view_get_model (GTK_TREE_VIEW (window->treeview));
456 gtk_tree_model_foreach (model,
457 (GtkTreeModelForeachFunc) chatrooms_window_remove_chatroom_foreach,
462 chatrooms_window_remove_chatroom_foreach (GtkTreeModel *model,
465 EmpathyChatroom *chatroom)
467 EmpathyChatroom *this_chatroom;
469 gtk_tree_model_get (model, iter, COL_POINTER, &this_chatroom, -1);
471 if (empathy_chatroom_equal (chatroom, this_chatroom)) {
472 gtk_list_store_remove (GTK_LIST_STORE (model), iter);
473 g_object_unref (this_chatroom);
477 g_object_unref (this_chatroom);
483 chatrooms_window_account_changed_cb (GtkWidget *combo_box,
484 EmpathyChatroomsWindow *window)
486 chatrooms_window_model_refresh_data (window, FALSE);