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 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: Xavier Claessens <xclaesse@gmail.com>
22 * Martyn Russell <martyn@imendio.com>
23 * Mikael Hallendal <micke@imendio.com>
32 #include <glade/glade.h>
34 #include <glib/gi18n.h>
36 #include <libempathy/empathy-chatroom-manager.h>
37 #include <libempathy/empathy-utils.h>
39 #include "empathy-account-chooser.h"
40 #include "empathy-chatrooms-window.h"
41 #include "empathy-new-chatroom-dialog.h"
42 #include "empathy-ui-utils.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;
57 } EmpathyChatroomsWindow;
59 static void chatrooms_window_destroy_cb (GtkWidget *widget,
60 EmpathyChatroomsWindow *window);
61 static void chatrooms_window_model_setup (EmpathyChatroomsWindow *window);
62 static void chatrooms_window_model_add_columns (EmpathyChatroomsWindow *window);
63 static void chatrooms_window_model_refresh_data (EmpathyChatroomsWindow *window,
65 static void chatrooms_window_model_add (EmpathyChatroomsWindow *window,
66 EmpathyChatroom *chatroom,
68 static void chatrooms_window_model_cell_auto_connect_toggled (GtkCellRendererToggle *cell,
70 EmpathyChatroomsWindow *window);
71 static EmpathyChatroom * chatrooms_window_model_get_selected (EmpathyChatroomsWindow *window);
72 static void chatrooms_window_model_action_selected (EmpathyChatroomsWindow *window);
73 static void chatrooms_window_row_activated_cb (GtkTreeView *tree_view,
75 GtkTreeViewColumn *column,
76 EmpathyChatroomsWindow *window);
77 static void chatrooms_window_button_remove_clicked_cb (GtkWidget *widget,
78 EmpathyChatroomsWindow *window);
79 static void chatrooms_window_button_edit_clicked_cb (GtkWidget *widget,
80 EmpathyChatroomsWindow *window);
81 static void chatrooms_window_button_close_clicked_cb (GtkWidget *widget,
82 EmpathyChatroomsWindow *window);
83 static void chatrooms_window_chatroom_added_cb (EmpathyChatroomManager *manager,
84 EmpathyChatroom *chatroom,
85 EmpathyChatroomsWindow *window);
86 static void chatrooms_window_chatroom_removed_cb (EmpathyChatroomManager *manager,
87 EmpathyChatroom *chatroom,
88 EmpathyChatroomsWindow *window);
89 static gboolean chatrooms_window_remove_chatroom_foreach (GtkTreeModel *model,
92 EmpathyChatroom *chatroom);
93 static void chatrooms_window_account_changed_cb (GtkWidget *combo_box,
94 EmpathyChatroomsWindow *window);
106 empathy_chatrooms_window_show (GtkWindow *parent)
108 static EmpathyChatroomsWindow *window = NULL;
112 gtk_window_present (GTK_WINDOW (window->window));
116 window = g_new0 (EmpathyChatroomsWindow, 1);
118 glade = empathy_glade_get_file ("empathy-chatrooms-window.glade",
121 "chatrooms_window", &window->window,
122 "hbox_account", &window->hbox_account,
123 "label_account", &window->label_account,
124 "treeview", &window->treeview,
125 "button_edit", &window->button_edit,
126 "button_remove", &window->button_remove,
127 "button_close", &window->button_close,
130 empathy_glade_connect (glade,
132 "chatrooms_window", "destroy", chatrooms_window_destroy_cb,
133 "button_remove", "clicked", chatrooms_window_button_remove_clicked_cb,
134 "button_edit", "clicked", chatrooms_window_button_edit_clicked_cb,
135 "button_close", "clicked", chatrooms_window_button_close_clicked_cb,
138 g_object_unref (glade);
140 g_object_add_weak_pointer (G_OBJECT (window->window), (gpointer) &window);
142 /* Get the session and chat room manager */
143 window->manager = empathy_chatroom_manager_new ();
145 g_signal_connect (window->manager, "chatroom-added",
146 G_CALLBACK (chatrooms_window_chatroom_added_cb),
148 g_signal_connect (window->manager, "chatroom-removed",
149 G_CALLBACK (chatrooms_window_chatroom_removed_cb),
152 /* Account chooser for chat rooms */
153 window->account_chooser = empathy_account_chooser_new ();
154 empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (window->account_chooser),
155 empathy_account_chooser_filter_is_connected,
157 empathy_account_chooser_set_account (EMPATHY_ACCOUNT_CHOOSER (window->account_chooser), NULL);
158 g_object_set (window->account_chooser,
159 "has-all-option", TRUE,
162 gtk_box_pack_start (GTK_BOX (window->hbox_account),
163 window->account_chooser,
166 g_signal_connect (window->account_chooser, "changed",
167 G_CALLBACK (chatrooms_window_account_changed_cb),
170 gtk_widget_show (window->account_chooser);
172 /* Set up chatrooms */
173 chatrooms_window_model_setup (window);
176 gtk_widget_grab_focus (window->treeview);
180 gtk_window_set_transient_for (GTK_WINDOW (window->window),
181 GTK_WINDOW (parent));
184 gtk_widget_show (window->window);
188 chatrooms_window_destroy_cb (GtkWidget *widget,
189 EmpathyChatroomsWindow *window)
191 g_object_unref (window->manager);
196 chatrooms_window_model_setup (EmpathyChatroomsWindow *window)
200 GtkTreeSelection *selection;
203 view = GTK_TREE_VIEW (window->treeview);
205 g_signal_connect (view, "row-activated",
206 G_CALLBACK (chatrooms_window_row_activated_cb),
210 store = gtk_list_store_new (COL_COUNT,
211 G_TYPE_STRING, /* Image */
212 G_TYPE_STRING, /* Name */
213 G_TYPE_STRING, /* Room */
214 G_TYPE_BOOLEAN, /* Auto start */
215 EMPATHY_TYPE_CHATROOM); /* Chatroom */
217 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
220 selection = gtk_tree_view_get_selection (view);
221 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
224 chatrooms_window_model_add_columns (window);
227 chatrooms_window_model_refresh_data (window, TRUE);
230 g_object_unref (store);
234 chatrooms_window_model_add_columns (EmpathyChatroomsWindow *window)
238 GtkTreeViewColumn *column;
239 GtkCellRenderer *cell;
242 view = GTK_TREE_VIEW (window->treeview);
243 model = gtk_tree_view_get_model (view);
245 gtk_tree_view_set_headers_visible (view, TRUE);
246 gtk_tree_view_set_headers_clickable (view, TRUE);
249 column = gtk_tree_view_column_new ();
250 count = gtk_tree_view_append_column (view, column);
252 gtk_tree_view_column_set_title (column, _("Name"));
253 gtk_tree_view_column_set_expand (column, TRUE);
254 gtk_tree_view_column_set_sort_column_id (column, count - 1);
256 cell = gtk_cell_renderer_pixbuf_new ();
257 gtk_tree_view_column_pack_start (column, cell, FALSE);
258 gtk_tree_view_column_add_attribute (column, cell, "icon-name", COL_IMAGE);
260 cell = gtk_cell_renderer_text_new ();
265 gtk_tree_view_column_pack_start (column, cell, TRUE);
266 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
269 cell = gtk_cell_renderer_text_new ();
270 column = gtk_tree_view_column_new_with_attributes (_("Room"), cell,
273 count = gtk_tree_view_append_column (view, column);
274 gtk_tree_view_column_set_sort_column_id (column, count - 1);
275 window->room_column = count - 1;
277 /* Chatroom auto connect */
278 cell = gtk_cell_renderer_toggle_new ();
279 column = gtk_tree_view_column_new_with_attributes (_("Auto Connect"), cell,
280 "active", COL_AUTO_CONNECT,
282 count = gtk_tree_view_append_column (view, column);
283 gtk_tree_view_column_set_sort_column_id (column, count - 1);
285 g_signal_connect (cell, "toggled",
286 G_CALLBACK (chatrooms_window_model_cell_auto_connect_toggled),
290 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model), 0,
295 chatrooms_window_model_refresh_data (EmpathyChatroomsWindow *window,
299 GtkTreeSelection *selection;
303 GtkTreeViewColumn *column;
304 EmpathyAccountChooser *account_chooser;
306 GList *chatrooms, *l;
308 view = GTK_TREE_VIEW (window->treeview);
309 selection = gtk_tree_view_get_selection (view);
310 model = gtk_tree_view_get_model (view);
311 store = GTK_LIST_STORE (model);
313 /* Look up chatrooms */
314 account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
315 account = empathy_account_chooser_get_account (account_chooser);
317 chatrooms = empathy_chatroom_manager_get_chatrooms (window->manager, account);
319 /* Sort out columns, we only show the server column for
320 * selected protocol types, such as Jabber.
323 column = gtk_tree_view_get_column (view, window->room_column);
324 gtk_tree_view_column_set_visible (column, TRUE);
326 column = gtk_tree_view_get_column (view, window->room_column);
327 gtk_tree_view_column_set_visible (column, FALSE);
330 /* Clean out the store */
331 gtk_list_store_clear (store);
333 /* Populate with chatroom list. */
334 for (l = chatrooms; l; l = l->next) {
335 chatrooms_window_model_add (window, l->data, FALSE);
338 if (gtk_tree_model_get_iter_first (model, &iter)) {
339 gtk_tree_selection_select_iter (selection, &iter);
343 g_object_unref (account);
346 g_list_free (chatrooms);
350 chatrooms_window_model_add (EmpathyChatroomsWindow *window,
351 EmpathyChatroom *chatroom,
356 GtkTreeSelection *selection;
360 view = GTK_TREE_VIEW (window->treeview);
361 selection = gtk_tree_view_get_selection (view);
362 model = gtk_tree_view_get_model (view);
363 store = GTK_LIST_STORE (model);
365 gtk_list_store_append (store, &iter);
366 gtk_list_store_set (store, &iter,
367 COL_NAME, empathy_chatroom_get_name (chatroom),
368 COL_ROOM, empathy_chatroom_get_room (chatroom),
369 COL_AUTO_CONNECT, empathy_chatroom_get_auto_connect (chatroom),
370 COL_POINTER, chatroom,
374 gtk_tree_selection_select_iter (selection, &iter);
379 chatrooms_window_model_cell_auto_connect_toggled (GtkCellRendererToggle *cell,
381 EmpathyChatroomsWindow *window)
383 EmpathyChatroom *chatroom;
391 view = GTK_TREE_VIEW (window->treeview);
392 model = gtk_tree_view_get_model (view);
393 store = GTK_LIST_STORE (model);
395 path = gtk_tree_path_new_from_string (path_string);
397 gtk_tree_model_get_iter (model, &iter, path);
398 gtk_tree_model_get (model, &iter,
399 COL_AUTO_CONNECT, &enabled,
400 COL_POINTER, &chatroom,
405 empathy_chatroom_set_auto_connect (chatroom, enabled);
406 empathy_chatroom_manager_store (window->manager);
408 gtk_list_store_set (store, &iter, COL_AUTO_CONNECT, enabled, -1);
409 gtk_tree_path_free (path);
410 g_object_unref (chatroom);
413 static EmpathyChatroom *
414 chatrooms_window_model_get_selected (EmpathyChatroomsWindow *window)
418 GtkTreeSelection *selection;
420 EmpathyChatroom *chatroom = NULL;
422 view = GTK_TREE_VIEW (window->treeview);
423 selection = gtk_tree_view_get_selection (view);
425 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
426 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
433 chatrooms_window_model_action_selected (EmpathyChatroomsWindow *window)
435 EmpathyChatroom *chatroom;
439 view = GTK_TREE_VIEW (window->treeview);
440 model = gtk_tree_view_get_model (view);
442 chatroom = chatrooms_window_model_get_selected (window);
447 //empathy_edit_chatroom_dialog_show (GTK_WINDOW (window->window), chatroom);
449 g_object_unref (chatroom);
453 chatrooms_window_row_activated_cb (GtkTreeView *tree_view,
455 GtkTreeViewColumn *column,
456 EmpathyChatroomsWindow *window)
458 if (GTK_WIDGET_IS_SENSITIVE (window->button_edit)) {
459 chatrooms_window_model_action_selected (window);
464 chatrooms_window_button_remove_clicked_cb (GtkWidget *widget,
465 EmpathyChatroomsWindow *window)
467 EmpathyChatroom *chatroom;
470 GtkTreeSelection *selection;
473 /* Remove from treeview */
474 view = GTK_TREE_VIEW (window->treeview);
475 selection = gtk_tree_view_get_selection (view);
477 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
481 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
482 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
484 /* Remove from config */
485 empathy_chatroom_manager_remove (window->manager, chatroom);
487 g_object_unref (chatroom);
491 chatrooms_window_button_edit_clicked_cb (GtkWidget *widget,
492 EmpathyChatroomsWindow *window)
494 EmpathyChatroom *chatroom;
496 chatroom = chatrooms_window_model_get_selected (window);
501 //empathy_edit_chatroom_dialog_show (GTK_WINDOW (window->window), chatroom);
503 g_object_unref (chatroom);
507 chatrooms_window_button_close_clicked_cb (GtkWidget *widget,
508 EmpathyChatroomsWindow *window)
510 gtk_widget_destroy (window->window);
514 chatrooms_window_chatroom_added_cb (EmpathyChatroomManager *manager,
515 EmpathyChatroom *chatroom,
516 EmpathyChatroomsWindow *window)
518 EmpathyAccountChooser *account_chooser;
521 account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
522 account = empathy_account_chooser_get_account (account_chooser);
525 chatrooms_window_model_add (window, chatroom, FALSE);
527 if (empathy_account_equal (account, empathy_chatroom_get_account (chatroom))) {
528 chatrooms_window_model_add (window, chatroom, FALSE);
531 g_object_unref (account);
536 chatrooms_window_chatroom_removed_cb (EmpathyChatroomManager *manager,
537 EmpathyChatroom *chatroom,
538 EmpathyChatroomsWindow *window)
542 model = gtk_tree_view_get_model (GTK_TREE_VIEW (window->treeview));
544 gtk_tree_model_foreach (model,
545 (GtkTreeModelForeachFunc) chatrooms_window_remove_chatroom_foreach,
550 chatrooms_window_remove_chatroom_foreach (GtkTreeModel *model,
553 EmpathyChatroom *chatroom)
555 EmpathyChatroom *this_chatroom;
557 gtk_tree_model_get (model, iter, COL_POINTER, &this_chatroom, -1);
559 if (empathy_chatroom_equal (chatroom, this_chatroom)) {
560 gtk_list_store_remove (GTK_LIST_STORE (model), iter);
561 g_object_unref (this_chatroom);
565 g_object_unref (this_chatroom);
571 chatrooms_window_account_changed_cb (GtkWidget *combo_box,
572 EmpathyChatroomsWindow *window)
574 chatrooms_window_model_refresh_data (window, FALSE);