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>
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 <libempathy-gtk/empathy-account-chooser.h>
40 #include <libempathy-gtk/empathy-ui-utils.h>
42 #include "empathy-chatrooms-window.h"
43 #include "empathy-new-chatroom-dialog.h"
46 EmpathyChatroomManager *manager;
49 GtkWidget *hbox_account;
50 GtkWidget *label_account;
51 GtkWidget *account_chooser;
53 GtkWidget *button_remove;
54 GtkWidget *button_edit;
55 GtkWidget *button_close;
58 } EmpathyChatroomsWindow;
60 static void chatrooms_window_destroy_cb (GtkWidget *widget,
61 EmpathyChatroomsWindow *window);
62 static void chatrooms_window_model_setup (EmpathyChatroomsWindow *window);
63 static void chatrooms_window_model_add_columns (EmpathyChatroomsWindow *window);
64 static void chatrooms_window_model_refresh_data (EmpathyChatroomsWindow *window,
66 static void chatrooms_window_model_add (EmpathyChatroomsWindow *window,
67 EmpathyChatroom *chatroom,
69 static void chatrooms_window_model_cell_auto_connect_toggled (GtkCellRendererToggle *cell,
71 EmpathyChatroomsWindow *window);
72 static EmpathyChatroom * chatrooms_window_model_get_selected (EmpathyChatroomsWindow *window);
73 static void chatrooms_window_model_action_selected (EmpathyChatroomsWindow *window);
74 static void chatrooms_window_row_activated_cb (GtkTreeView *tree_view,
76 GtkTreeViewColumn *column,
77 EmpathyChatroomsWindow *window);
78 static void chatrooms_window_button_remove_clicked_cb (GtkWidget *widget,
79 EmpathyChatroomsWindow *window);
80 static void chatrooms_window_button_edit_clicked_cb (GtkWidget *widget,
81 EmpathyChatroomsWindow *window);
82 static void chatrooms_window_button_close_clicked_cb (GtkWidget *widget,
83 EmpathyChatroomsWindow *window);
84 static void chatrooms_window_chatroom_added_cb (EmpathyChatroomManager *manager,
85 EmpathyChatroom *chatroom,
86 EmpathyChatroomsWindow *window);
87 static void chatrooms_window_chatroom_removed_cb (EmpathyChatroomManager *manager,
88 EmpathyChatroom *chatroom,
89 EmpathyChatroomsWindow *window);
90 static gboolean chatrooms_window_remove_chatroom_foreach (GtkTreeModel *model,
93 EmpathyChatroom *chatroom);
94 static void chatrooms_window_account_changed_cb (GtkWidget *combo_box,
95 EmpathyChatroomsWindow *window);
107 empathy_chatrooms_window_show (GtkWindow *parent)
109 static EmpathyChatroomsWindow *window = NULL;
113 gtk_window_present (GTK_WINDOW (window->window));
117 window = g_new0 (EmpathyChatroomsWindow, 1);
119 glade = empathy_glade_get_file ("empathy-chatrooms-window.glade",
122 "chatrooms_window", &window->window,
123 "hbox_account", &window->hbox_account,
124 "label_account", &window->label_account,
125 "treeview", &window->treeview,
126 "button_edit", &window->button_edit,
127 "button_remove", &window->button_remove,
128 "button_close", &window->button_close,
131 empathy_glade_connect (glade,
133 "chatrooms_window", "destroy", chatrooms_window_destroy_cb,
134 "button_remove", "clicked", chatrooms_window_button_remove_clicked_cb,
135 "button_edit", "clicked", chatrooms_window_button_edit_clicked_cb,
136 "button_close", "clicked", chatrooms_window_button_close_clicked_cb,
139 g_object_unref (glade);
141 g_object_add_weak_pointer (G_OBJECT (window->window), (gpointer) &window);
143 /* Get the session and chat room manager */
144 window->manager = empathy_chatroom_manager_new ();
146 g_signal_connect (window->manager, "chatroom-added",
147 G_CALLBACK (chatrooms_window_chatroom_added_cb),
149 g_signal_connect (window->manager, "chatroom-removed",
150 G_CALLBACK (chatrooms_window_chatroom_removed_cb),
153 /* Account chooser for chat rooms */
154 window->account_chooser = empathy_account_chooser_new ();
155 empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (window->account_chooser),
156 empathy_account_chooser_filter_is_connected,
158 g_object_set (window->account_chooser,
159 "has-all-option", TRUE,
161 empathy_account_chooser_set_account (EMPATHY_ACCOUNT_CHOOSER (window->account_chooser), NULL);
163 gtk_box_pack_start (GTK_BOX (window->hbox_account),
164 window->account_chooser,
167 g_signal_connect (window->account_chooser, "changed",
168 G_CALLBACK (chatrooms_window_account_changed_cb),
171 gtk_widget_show (window->account_chooser);
173 /* Set up chatrooms */
174 chatrooms_window_model_setup (window);
177 gtk_widget_grab_focus (window->treeview);
181 gtk_window_set_transient_for (GTK_WINDOW (window->window),
182 GTK_WINDOW (parent));
185 gtk_widget_show (window->window);
189 chatrooms_window_destroy_cb (GtkWidget *widget,
190 EmpathyChatroomsWindow *window)
192 g_signal_handlers_disconnect_by_func (window->manager,
193 chatrooms_window_chatroom_added_cb,
195 g_signal_handlers_disconnect_by_func (window->manager,
196 chatrooms_window_chatroom_removed_cb,
198 g_object_unref (window->manager);
203 chatrooms_window_model_setup (EmpathyChatroomsWindow *window)
207 GtkTreeSelection *selection;
210 view = GTK_TREE_VIEW (window->treeview);
212 g_signal_connect (view, "row-activated",
213 G_CALLBACK (chatrooms_window_row_activated_cb),
217 store = gtk_list_store_new (COL_COUNT,
218 G_TYPE_STRING, /* Image */
219 G_TYPE_STRING, /* Name */
220 G_TYPE_STRING, /* Room */
221 G_TYPE_BOOLEAN, /* Auto start */
222 EMPATHY_TYPE_CHATROOM); /* Chatroom */
224 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
227 selection = gtk_tree_view_get_selection (view);
228 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
231 chatrooms_window_model_add_columns (window);
234 chatrooms_window_model_refresh_data (window, TRUE);
237 g_object_unref (store);
241 chatrooms_window_model_add_columns (EmpathyChatroomsWindow *window)
245 GtkTreeViewColumn *column;
246 GtkCellRenderer *cell;
249 view = GTK_TREE_VIEW (window->treeview);
250 model = gtk_tree_view_get_model (view);
252 gtk_tree_view_set_headers_visible (view, TRUE);
253 gtk_tree_view_set_headers_clickable (view, TRUE);
256 column = gtk_tree_view_column_new ();
257 count = gtk_tree_view_append_column (view, column);
259 gtk_tree_view_column_set_title (column, _("Name"));
260 gtk_tree_view_column_set_expand (column, TRUE);
261 gtk_tree_view_column_set_sort_column_id (column, count - 1);
263 cell = gtk_cell_renderer_pixbuf_new ();
264 gtk_tree_view_column_pack_start (column, cell, FALSE);
265 gtk_tree_view_column_add_attribute (column, cell, "icon-name", COL_IMAGE);
267 cell = gtk_cell_renderer_text_new ();
272 gtk_tree_view_column_pack_start (column, cell, TRUE);
273 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
276 cell = gtk_cell_renderer_text_new ();
277 column = gtk_tree_view_column_new_with_attributes (_("Room"), cell,
280 count = gtk_tree_view_append_column (view, column);
281 gtk_tree_view_column_set_sort_column_id (column, count - 1);
282 window->room_column = count - 1;
284 /* Chatroom auto connect */
285 cell = gtk_cell_renderer_toggle_new ();
286 column = gtk_tree_view_column_new_with_attributes (_("Auto Connect"), cell,
287 "active", COL_AUTO_CONNECT,
289 count = gtk_tree_view_append_column (view, column);
290 gtk_tree_view_column_set_sort_column_id (column, count - 1);
292 g_signal_connect (cell, "toggled",
293 G_CALLBACK (chatrooms_window_model_cell_auto_connect_toggled),
297 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model), 0,
302 chatrooms_window_model_refresh_data (EmpathyChatroomsWindow *window,
306 GtkTreeSelection *selection;
310 GtkTreeViewColumn *column;
311 EmpathyAccountChooser *account_chooser;
313 GList *chatrooms, *l;
315 view = GTK_TREE_VIEW (window->treeview);
316 selection = gtk_tree_view_get_selection (view);
317 model = gtk_tree_view_get_model (view);
318 store = GTK_LIST_STORE (model);
320 /* Look up chatrooms */
321 account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
322 account = empathy_account_chooser_get_account (account_chooser);
324 chatrooms = empathy_chatroom_manager_get_chatrooms (window->manager, account);
326 /* Sort out columns, we only show the server column for
327 * selected protocol types, such as Jabber.
330 column = gtk_tree_view_get_column (view, window->room_column);
331 gtk_tree_view_column_set_visible (column, TRUE);
333 column = gtk_tree_view_get_column (view, window->room_column);
334 gtk_tree_view_column_set_visible (column, FALSE);
337 /* Clean out the store */
338 gtk_list_store_clear (store);
340 /* Populate with chatroom list. */
341 for (l = chatrooms; l; l = l->next) {
342 chatrooms_window_model_add (window, l->data, FALSE);
345 if (gtk_tree_model_get_iter_first (model, &iter)) {
346 gtk_tree_selection_select_iter (selection, &iter);
350 g_object_unref (account);
353 g_list_free (chatrooms);
357 chatrooms_window_model_add (EmpathyChatroomsWindow *window,
358 EmpathyChatroom *chatroom,
363 GtkTreeSelection *selection;
367 view = GTK_TREE_VIEW (window->treeview);
368 selection = gtk_tree_view_get_selection (view);
369 model = gtk_tree_view_get_model (view);
370 store = GTK_LIST_STORE (model);
372 gtk_list_store_append (store, &iter);
373 gtk_list_store_set (store, &iter,
374 COL_NAME, empathy_chatroom_get_name (chatroom),
375 COL_ROOM, empathy_chatroom_get_room (chatroom),
376 COL_AUTO_CONNECT, empathy_chatroom_get_auto_connect (chatroom),
377 COL_POINTER, chatroom,
381 gtk_tree_selection_select_iter (selection, &iter);
386 chatrooms_window_model_cell_auto_connect_toggled (GtkCellRendererToggle *cell,
388 EmpathyChatroomsWindow *window)
390 EmpathyChatroom *chatroom;
398 view = GTK_TREE_VIEW (window->treeview);
399 model = gtk_tree_view_get_model (view);
400 store = GTK_LIST_STORE (model);
402 path = gtk_tree_path_new_from_string (path_string);
404 gtk_tree_model_get_iter (model, &iter, path);
405 gtk_tree_model_get (model, &iter,
406 COL_AUTO_CONNECT, &enabled,
407 COL_POINTER, &chatroom,
412 empathy_chatroom_set_auto_connect (chatroom, enabled);
413 empathy_chatroom_manager_store (window->manager);
415 gtk_list_store_set (store, &iter, COL_AUTO_CONNECT, enabled, -1);
416 gtk_tree_path_free (path);
417 g_object_unref (chatroom);
420 static EmpathyChatroom *
421 chatrooms_window_model_get_selected (EmpathyChatroomsWindow *window)
425 GtkTreeSelection *selection;
427 EmpathyChatroom *chatroom = NULL;
429 view = GTK_TREE_VIEW (window->treeview);
430 selection = gtk_tree_view_get_selection (view);
432 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
433 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
440 chatrooms_window_model_action_selected (EmpathyChatroomsWindow *window)
442 EmpathyChatroom *chatroom;
446 view = GTK_TREE_VIEW (window->treeview);
447 model = gtk_tree_view_get_model (view);
449 chatroom = chatrooms_window_model_get_selected (window);
454 //empathy_edit_chatroom_dialog_show (GTK_WINDOW (window->window), chatroom);
456 g_object_unref (chatroom);
460 chatrooms_window_row_activated_cb (GtkTreeView *tree_view,
462 GtkTreeViewColumn *column,
463 EmpathyChatroomsWindow *window)
465 if (GTK_WIDGET_IS_SENSITIVE (window->button_edit)) {
466 chatrooms_window_model_action_selected (window);
471 chatrooms_window_button_remove_clicked_cb (GtkWidget *widget,
472 EmpathyChatroomsWindow *window)
474 EmpathyChatroom *chatroom;
477 GtkTreeSelection *selection;
480 /* Remove from treeview */
481 view = GTK_TREE_VIEW (window->treeview);
482 selection = gtk_tree_view_get_selection (view);
484 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
488 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
489 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
491 /* Remove from config */
492 empathy_chatroom_manager_remove (window->manager, chatroom);
494 g_object_unref (chatroom);
498 chatrooms_window_button_edit_clicked_cb (GtkWidget *widget,
499 EmpathyChatroomsWindow *window)
501 EmpathyChatroom *chatroom;
503 chatroom = chatrooms_window_model_get_selected (window);
508 //empathy_edit_chatroom_dialog_show (GTK_WINDOW (window->window), chatroom);
510 g_object_unref (chatroom);
514 chatrooms_window_button_close_clicked_cb (GtkWidget *widget,
515 EmpathyChatroomsWindow *window)
517 gtk_widget_destroy (window->window);
521 chatrooms_window_chatroom_added_cb (EmpathyChatroomManager *manager,
522 EmpathyChatroom *chatroom,
523 EmpathyChatroomsWindow *window)
525 EmpathyAccountChooser *account_chooser;
528 account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
529 account = empathy_account_chooser_get_account (account_chooser);
532 chatrooms_window_model_add (window, chatroom, FALSE);
534 if (empathy_account_equal (account, empathy_chatroom_get_account (chatroom))) {
535 chatrooms_window_model_add (window, chatroom, FALSE);
538 g_object_unref (account);
543 chatrooms_window_chatroom_removed_cb (EmpathyChatroomManager *manager,
544 EmpathyChatroom *chatroom,
545 EmpathyChatroomsWindow *window)
549 model = gtk_tree_view_get_model (GTK_TREE_VIEW (window->treeview));
551 gtk_tree_model_foreach (model,
552 (GtkTreeModelForeachFunc) chatrooms_window_remove_chatroom_foreach,
557 chatrooms_window_remove_chatroom_foreach (GtkTreeModel *model,
560 EmpathyChatroom *chatroom)
562 EmpathyChatroom *this_chatroom;
564 gtk_tree_model_get (model, iter, COL_POINTER, &this_chatroom, -1);
566 if (empathy_chatroom_equal (chatroom, this_chatroom)) {
567 gtk_list_store_remove (GTK_LIST_STORE (model), iter);
568 g_object_unref (this_chatroom);
572 g_object_unref (this_chatroom);
578 chatrooms_window_account_changed_cb (GtkWidget *combo_box,
579 EmpathyChatroomsWindow *window)
581 chatrooms_window_model_refresh_data (window, FALSE);