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_account (EMPATHY_ACCOUNT_CHOOSER (window->account_chooser), NULL);
155 g_object_set (window->account_chooser,
156 "can-select-all", TRUE,
157 "has-all-option", TRUE,
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_object_unref (window->manager);
194 chatrooms_window_model_setup (EmpathyChatroomsWindow *window)
198 GtkTreeSelection *selection;
201 view = GTK_TREE_VIEW (window->treeview);
203 g_signal_connect (view, "row-activated",
204 G_CALLBACK (chatrooms_window_row_activated_cb),
208 store = gtk_list_store_new (COL_COUNT,
209 G_TYPE_STRING, /* Image */
210 G_TYPE_STRING, /* Name */
211 G_TYPE_STRING, /* Room */
212 G_TYPE_BOOLEAN, /* Auto start */
213 EMPATHY_TYPE_CHATROOM); /* Chatroom */
215 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
218 selection = gtk_tree_view_get_selection (view);
219 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
222 chatrooms_window_model_add_columns (window);
225 chatrooms_window_model_refresh_data (window, TRUE);
228 g_object_unref (store);
232 chatrooms_window_model_add_columns (EmpathyChatroomsWindow *window)
236 GtkTreeViewColumn *column;
237 GtkCellRenderer *cell;
240 view = GTK_TREE_VIEW (window->treeview);
241 model = gtk_tree_view_get_model (view);
243 gtk_tree_view_set_headers_visible (view, TRUE);
244 gtk_tree_view_set_headers_clickable (view, TRUE);
247 column = gtk_tree_view_column_new ();
248 count = gtk_tree_view_append_column (view, column);
250 gtk_tree_view_column_set_title (column, _("Name"));
251 gtk_tree_view_column_set_expand (column, TRUE);
252 gtk_tree_view_column_set_sort_column_id (column, count - 1);
254 cell = gtk_cell_renderer_pixbuf_new ();
255 gtk_tree_view_column_pack_start (column, cell, FALSE);
256 gtk_tree_view_column_add_attribute (column, cell, "icon-name", COL_IMAGE);
258 cell = gtk_cell_renderer_text_new ();
263 gtk_tree_view_column_pack_start (column, cell, TRUE);
264 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
267 cell = gtk_cell_renderer_text_new ();
268 column = gtk_tree_view_column_new_with_attributes (_("Room"), cell,
271 count = gtk_tree_view_append_column (view, column);
272 gtk_tree_view_column_set_sort_column_id (column, count - 1);
273 window->room_column = count - 1;
275 /* Chatroom auto connect */
276 cell = gtk_cell_renderer_toggle_new ();
277 column = gtk_tree_view_column_new_with_attributes (_("Auto Connect"), cell,
278 "active", COL_AUTO_CONNECT,
280 count = gtk_tree_view_append_column (view, column);
281 gtk_tree_view_column_set_sort_column_id (column, count - 1);
283 g_signal_connect (cell, "toggled",
284 G_CALLBACK (chatrooms_window_model_cell_auto_connect_toggled),
288 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model), 0,
293 chatrooms_window_model_refresh_data (EmpathyChatroomsWindow *window,
297 GtkTreeSelection *selection;
301 GtkTreeViewColumn *column;
302 EmpathyAccountChooser *account_chooser;
304 GList *chatrooms, *l;
306 view = GTK_TREE_VIEW (window->treeview);
307 selection = gtk_tree_view_get_selection (view);
308 model = gtk_tree_view_get_model (view);
309 store = GTK_LIST_STORE (model);
311 /* Look up chatrooms */
312 account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
313 account = empathy_account_chooser_get_account (account_chooser);
315 chatrooms = empathy_chatroom_manager_get_chatrooms (window->manager, account);
317 /* Sort out columns, we only show the server column for
318 * selected protocol types, such as Jabber.
321 column = gtk_tree_view_get_column (view, window->room_column);
322 gtk_tree_view_column_set_visible (column, TRUE);
324 column = gtk_tree_view_get_column (view, window->room_column);
325 gtk_tree_view_column_set_visible (column, FALSE);
328 /* Clean out the store */
329 gtk_list_store_clear (store);
331 /* Populate with chatroom list. */
332 for (l = chatrooms; l; l = l->next) {
333 chatrooms_window_model_add (window, l->data, FALSE);
336 if (gtk_tree_model_get_iter_first (model, &iter)) {
337 gtk_tree_selection_select_iter (selection, &iter);
341 g_object_unref (account);
344 g_list_free (chatrooms);
348 chatrooms_window_model_add (EmpathyChatroomsWindow *window,
349 EmpathyChatroom *chatroom,
354 GtkTreeSelection *selection;
358 view = GTK_TREE_VIEW (window->treeview);
359 selection = gtk_tree_view_get_selection (view);
360 model = gtk_tree_view_get_model (view);
361 store = GTK_LIST_STORE (model);
363 gtk_list_store_append (store, &iter);
364 gtk_list_store_set (store, &iter,
365 COL_NAME, empathy_chatroom_get_name (chatroom),
366 COL_ROOM, empathy_chatroom_get_room (chatroom),
367 COL_AUTO_CONNECT, empathy_chatroom_get_auto_connect (chatroom),
368 COL_POINTER, chatroom,
372 gtk_tree_selection_select_iter (selection, &iter);
377 chatrooms_window_model_cell_auto_connect_toggled (GtkCellRendererToggle *cell,
379 EmpathyChatroomsWindow *window)
381 EmpathyChatroom *chatroom;
389 view = GTK_TREE_VIEW (window->treeview);
390 model = gtk_tree_view_get_model (view);
391 store = GTK_LIST_STORE (model);
393 path = gtk_tree_path_new_from_string (path_string);
395 gtk_tree_model_get_iter (model, &iter, path);
396 gtk_tree_model_get (model, &iter,
397 COL_AUTO_CONNECT, &enabled,
398 COL_POINTER, &chatroom,
403 empathy_chatroom_set_auto_connect (chatroom, enabled);
404 empathy_chatroom_manager_store (window->manager);
406 gtk_list_store_set (store, &iter, COL_AUTO_CONNECT, enabled, -1);
407 gtk_tree_path_free (path);
408 g_object_unref (chatroom);
411 static EmpathyChatroom *
412 chatrooms_window_model_get_selected (EmpathyChatroomsWindow *window)
416 GtkTreeSelection *selection;
418 EmpathyChatroom *chatroom = NULL;
420 view = GTK_TREE_VIEW (window->treeview);
421 selection = gtk_tree_view_get_selection (view);
423 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
424 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
431 chatrooms_window_model_action_selected (EmpathyChatroomsWindow *window)
433 EmpathyChatroom *chatroom;
437 view = GTK_TREE_VIEW (window->treeview);
438 model = gtk_tree_view_get_model (view);
440 chatroom = chatrooms_window_model_get_selected (window);
445 //empathy_edit_chatroom_dialog_show (GTK_WINDOW (window->window), chatroom);
447 g_object_unref (chatroom);
451 chatrooms_window_row_activated_cb (GtkTreeView *tree_view,
453 GtkTreeViewColumn *column,
454 EmpathyChatroomsWindow *window)
456 if (GTK_WIDGET_IS_SENSITIVE (window->button_edit)) {
457 chatrooms_window_model_action_selected (window);
462 chatrooms_window_button_remove_clicked_cb (GtkWidget *widget,
463 EmpathyChatroomsWindow *window)
465 EmpathyChatroom *chatroom;
468 GtkTreeSelection *selection;
471 /* Remove from treeview */
472 view = GTK_TREE_VIEW (window->treeview);
473 selection = gtk_tree_view_get_selection (view);
475 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
479 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
480 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
482 /* Remove from config */
483 empathy_chatroom_manager_remove (window->manager, chatroom);
485 g_object_unref (chatroom);
489 chatrooms_window_button_edit_clicked_cb (GtkWidget *widget,
490 EmpathyChatroomsWindow *window)
492 EmpathyChatroom *chatroom;
494 chatroom = chatrooms_window_model_get_selected (window);
499 //empathy_edit_chatroom_dialog_show (GTK_WINDOW (window->window), chatroom);
501 g_object_unref (chatroom);
505 chatrooms_window_button_close_clicked_cb (GtkWidget *widget,
506 EmpathyChatroomsWindow *window)
508 gtk_widget_destroy (window->window);
512 chatrooms_window_chatroom_added_cb (EmpathyChatroomManager *manager,
513 EmpathyChatroom *chatroom,
514 EmpathyChatroomsWindow *window)
516 EmpathyAccountChooser *account_chooser;
519 account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
520 account = empathy_account_chooser_get_account (account_chooser);
523 chatrooms_window_model_add (window, chatroom, FALSE);
525 if (empathy_account_equal (account, empathy_chatroom_get_account (chatroom))) {
526 chatrooms_window_model_add (window, chatroom, FALSE);
529 g_object_unref (account);
534 chatrooms_window_chatroom_removed_cb (EmpathyChatroomManager *manager,
535 EmpathyChatroom *chatroom,
536 EmpathyChatroomsWindow *window)
540 model = gtk_tree_view_get_model (GTK_TREE_VIEW (window->treeview));
542 gtk_tree_model_foreach (model,
543 (GtkTreeModelForeachFunc) chatrooms_window_remove_chatroom_foreach,
548 chatrooms_window_remove_chatroom_foreach (GtkTreeModel *model,
551 EmpathyChatroom *chatroom)
553 EmpathyChatroom *this_chatroom;
555 gtk_tree_model_get (model, iter, COL_POINTER, &this_chatroom, -1);
557 if (empathy_chatroom_equal (chatroom, this_chatroom)) {
558 gtk_list_store_remove (GTK_LIST_STORE (model), iter);
559 g_object_unref (this_chatroom);
563 g_object_unref (this_chatroom);
569 chatrooms_window_account_changed_cb (GtkWidget *combo_box,
570 EmpathyChatroomsWindow *window)
572 chatrooms_window_model_refresh_data (window, FALSE);