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., 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>
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;
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;
113 gtk_window_present (GTK_WINDOW (window->window));
117 window = g_new0 (EmpathyChatroomsWindow, 1);
119 filename = empathy_file_lookup ("empathy-chatrooms-window.ui", "src");
120 gui = empathy_builder_get_file (filename,
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,
131 empathy_builder_connect (gui, window,
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 (gui);
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_dup_singleton (NULL);
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 g_object_set (window->account_chooser,
158 "has-all-option", TRUE,
160 empathy_account_chooser_set_account (EMPATHY_ACCOUNT_CHOOSER (window->account_chooser), NULL);
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_signal_handlers_disconnect_by_func (window->manager,
192 chatrooms_window_chatroom_added_cb,
194 g_signal_handlers_disconnect_by_func (window->manager,
195 chatrooms_window_chatroom_removed_cb,
197 g_object_unref (window->manager);
202 chatrooms_window_model_setup (EmpathyChatroomsWindow *window)
206 GtkTreeSelection *selection;
209 view = GTK_TREE_VIEW (window->treeview);
211 g_signal_connect (view, "row-activated",
212 G_CALLBACK (chatrooms_window_row_activated_cb),
216 store = gtk_list_store_new (COL_COUNT,
217 G_TYPE_STRING, /* Image */
218 G_TYPE_STRING, /* Name */
219 G_TYPE_STRING, /* Room */
220 G_TYPE_BOOLEAN, /* Auto start */
221 EMPATHY_TYPE_CHATROOM); /* Chatroom */
223 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
226 selection = gtk_tree_view_get_selection (view);
227 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
230 chatrooms_window_model_add_columns (window);
233 chatrooms_window_model_refresh_data (window, TRUE);
236 g_object_unref (store);
240 chatrooms_window_model_add_columns (EmpathyChatroomsWindow *window)
244 GtkTreeViewColumn *column;
245 GtkCellRenderer *cell;
248 view = GTK_TREE_VIEW (window->treeview);
249 model = gtk_tree_view_get_model (view);
251 gtk_tree_view_set_headers_visible (view, TRUE);
252 gtk_tree_view_set_headers_clickable (view, TRUE);
255 column = gtk_tree_view_column_new ();
256 count = gtk_tree_view_append_column (view, column);
258 gtk_tree_view_column_set_title (column, _("Name"));
259 gtk_tree_view_column_set_expand (column, TRUE);
260 gtk_tree_view_column_set_sort_column_id (column, count - 1);
262 cell = gtk_cell_renderer_pixbuf_new ();
263 gtk_tree_view_column_pack_start (column, cell, FALSE);
264 gtk_tree_view_column_add_attribute (column, cell, "icon-name", COL_IMAGE);
266 cell = gtk_cell_renderer_text_new ();
271 gtk_tree_view_column_pack_start (column, cell, TRUE);
272 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
275 cell = gtk_cell_renderer_text_new ();
276 column = gtk_tree_view_column_new_with_attributes (_("Room"), cell,
279 count = gtk_tree_view_append_column (view, column);
280 gtk_tree_view_column_set_sort_column_id (column, count - 1);
281 window->room_column = count - 1;
283 /* Chatroom auto connect */
284 cell = gtk_cell_renderer_toggle_new ();
285 column = gtk_tree_view_column_new_with_attributes (_("Auto-Connect"), cell,
286 "active", COL_AUTO_CONNECT,
288 count = gtk_tree_view_append_column (view, column);
289 gtk_tree_view_column_set_sort_column_id (column, count - 1);
291 g_signal_connect (cell, "toggled",
292 G_CALLBACK (chatrooms_window_model_cell_auto_connect_toggled),
296 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model), 0,
301 chatrooms_window_model_refresh_data (EmpathyChatroomsWindow *window,
305 GtkTreeSelection *selection;
309 GtkTreeViewColumn *column;
310 EmpathyAccountChooser *account_chooser;
312 GList *chatrooms, *l;
314 view = GTK_TREE_VIEW (window->treeview);
315 selection = gtk_tree_view_get_selection (view);
316 model = gtk_tree_view_get_model (view);
317 store = GTK_LIST_STORE (model);
319 /* Look up chatrooms */
320 account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
321 account = empathy_account_chooser_get_account (account_chooser);
323 chatrooms = empathy_chatroom_manager_get_chatrooms (window->manager, account);
325 /* Sort out columns, we only show the server column for
326 * selected protocol types, such as Jabber.
329 column = gtk_tree_view_get_column (view, window->room_column);
330 gtk_tree_view_column_set_visible (column, TRUE);
332 column = gtk_tree_view_get_column (view, window->room_column);
333 gtk_tree_view_column_set_visible (column, FALSE);
336 /* Clean out the store */
337 gtk_list_store_clear (store);
339 /* Populate with chatroom list. */
340 for (l = chatrooms; l; l = l->next) {
341 chatrooms_window_model_add (window, l->data, FALSE);
344 if (gtk_tree_model_get_iter_first (model, &iter)) {
345 gtk_tree_selection_select_iter (selection, &iter);
349 g_object_unref (account);
352 g_list_free (chatrooms);
356 chatrooms_window_model_add (EmpathyChatroomsWindow *window,
357 EmpathyChatroom *chatroom,
362 GtkTreeSelection *selection;
366 view = GTK_TREE_VIEW (window->treeview);
367 selection = gtk_tree_view_get_selection (view);
368 model = gtk_tree_view_get_model (view);
369 store = GTK_LIST_STORE (model);
371 gtk_list_store_append (store, &iter);
372 gtk_list_store_set (store, &iter,
373 COL_NAME, empathy_chatroom_get_name (chatroom),
374 COL_ROOM, empathy_chatroom_get_room (chatroom),
375 COL_AUTO_CONNECT, empathy_chatroom_get_auto_connect (chatroom),
376 COL_POINTER, chatroom,
380 gtk_tree_selection_select_iter (selection, &iter);
385 chatrooms_window_model_cell_auto_connect_toggled (GtkCellRendererToggle *cell,
387 EmpathyChatroomsWindow *window)
389 EmpathyChatroom *chatroom;
397 view = GTK_TREE_VIEW (window->treeview);
398 model = gtk_tree_view_get_model (view);
399 store = GTK_LIST_STORE (model);
401 path = gtk_tree_path_new_from_string (path_string);
403 gtk_tree_model_get_iter (model, &iter, path);
404 gtk_tree_model_get (model, &iter,
405 COL_AUTO_CONNECT, &enabled,
406 COL_POINTER, &chatroom,
411 empathy_chatroom_set_auto_connect (chatroom, enabled);
413 gtk_list_store_set (store, &iter, COL_AUTO_CONNECT, enabled, -1);
414 gtk_tree_path_free (path);
415 g_object_unref (chatroom);
418 static EmpathyChatroom *
419 chatrooms_window_model_get_selected (EmpathyChatroomsWindow *window)
423 GtkTreeSelection *selection;
425 EmpathyChatroom *chatroom = NULL;
427 view = GTK_TREE_VIEW (window->treeview);
428 selection = gtk_tree_view_get_selection (view);
430 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
431 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
438 chatrooms_window_model_action_selected (EmpathyChatroomsWindow *window)
440 EmpathyChatroom *chatroom;
442 /* FIXME: This is still not implemented since Gossip fork */
443 chatroom = chatrooms_window_model_get_selected (window);
444 g_object_unref (chatroom);
448 chatrooms_window_row_activated_cb (GtkTreeView *tree_view,
450 GtkTreeViewColumn *column,
451 EmpathyChatroomsWindow *window)
453 if (GTK_WIDGET_IS_SENSITIVE (window->button_edit)) {
454 chatrooms_window_model_action_selected (window);
459 chatrooms_window_button_remove_clicked_cb (GtkWidget *widget,
460 EmpathyChatroomsWindow *window)
462 EmpathyChatroom *chatroom;
465 GtkTreeSelection *selection;
468 /* Remove from treeview */
469 view = GTK_TREE_VIEW (window->treeview);
470 selection = gtk_tree_view_get_selection (view);
472 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
476 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
477 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
479 /* Remove from config */
480 empathy_chatroom_manager_remove (window->manager, chatroom);
482 g_object_unref (chatroom);
486 chatrooms_window_button_edit_clicked_cb (GtkWidget *widget,
487 EmpathyChatroomsWindow *window)
489 EmpathyChatroom *chatroom;
491 /* FIXME: This is still not implemented since Gossip fork */
492 chatroom = chatrooms_window_model_get_selected (window);
493 g_object_unref (chatroom);
497 chatrooms_window_button_close_clicked_cb (GtkWidget *widget,
498 EmpathyChatroomsWindow *window)
500 gtk_widget_destroy (window->window);
504 chatrooms_window_chatroom_added_cb (EmpathyChatroomManager *manager,
505 EmpathyChatroom *chatroom,
506 EmpathyChatroomsWindow *window)
508 EmpathyAccountChooser *account_chooser;
511 account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
512 account = empathy_account_chooser_get_account (account_chooser);
515 chatrooms_window_model_add (window, chatroom, FALSE);
517 if (empathy_account_equal (account, empathy_chatroom_get_account (chatroom))) {
518 chatrooms_window_model_add (window, chatroom, FALSE);
521 g_object_unref (account);
526 chatrooms_window_chatroom_removed_cb (EmpathyChatroomManager *manager,
527 EmpathyChatroom *chatroom,
528 EmpathyChatroomsWindow *window)
532 model = gtk_tree_view_get_model (GTK_TREE_VIEW (window->treeview));
534 gtk_tree_model_foreach (model,
535 (GtkTreeModelForeachFunc) chatrooms_window_remove_chatroom_foreach,
540 chatrooms_window_remove_chatroom_foreach (GtkTreeModel *model,
543 EmpathyChatroom *chatroom)
545 EmpathyChatroom *this_chatroom;
547 gtk_tree_model_get (model, iter, COL_POINTER, &this_chatroom, -1);
549 if (empathy_chatroom_equal (chatroom, this_chatroom)) {
550 gtk_list_store_remove (GTK_LIST_STORE (model), iter);
551 g_object_unref (this_chatroom);
555 g_object_unref (this_chatroom);
561 chatrooms_window_account_changed_cb (GtkWidget *combo_box,
562 EmpathyChatroomsWindow *window)
564 chatrooms_window_model_refresh_data (window, FALSE);