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-edit-chatroom-dialog.h"
42 #include "empathy-new-chatroom-dialog.h"
43 #include "empathy-ui-utils.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_account (EMPATHY_ACCOUNT_CHOOSER (window->account_chooser), NULL);
156 g_object_set (window->account_chooser,
157 "can-select-all", TRUE,
158 "has-all-option", TRUE,
161 gtk_box_pack_start (GTK_BOX (window->hbox_account),
162 window->account_chooser,
165 g_signal_connect (window->account_chooser, "changed",
166 G_CALLBACK (chatrooms_window_account_changed_cb),
169 gtk_widget_show (window->account_chooser);
171 /* Set up chatrooms */
172 chatrooms_window_model_setup (window);
175 gtk_widget_grab_focus (window->treeview);
179 gtk_window_set_transient_for (GTK_WINDOW (window->window),
180 GTK_WINDOW (parent));
183 gtk_widget_show (window->window);
187 chatrooms_window_destroy_cb (GtkWidget *widget,
188 EmpathyChatroomsWindow *window)
190 g_object_unref (window->manager);
195 chatrooms_window_model_setup (EmpathyChatroomsWindow *window)
199 GtkTreeSelection *selection;
202 view = GTK_TREE_VIEW (window->treeview);
204 g_signal_connect (view, "row-activated",
205 G_CALLBACK (chatrooms_window_row_activated_cb),
209 store = gtk_list_store_new (COL_COUNT,
210 G_TYPE_STRING, /* Image */
211 G_TYPE_STRING, /* Name */
212 G_TYPE_STRING, /* Room */
213 G_TYPE_BOOLEAN, /* Auto start */
214 EMPATHY_TYPE_CHATROOM); /* Chatroom */
216 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
219 selection = gtk_tree_view_get_selection (view);
220 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
223 chatrooms_window_model_add_columns (window);
226 chatrooms_window_model_refresh_data (window, TRUE);
229 g_object_unref (store);
233 chatrooms_window_model_add_columns (EmpathyChatroomsWindow *window)
237 GtkTreeViewColumn *column;
238 GtkCellRenderer *cell;
241 view = GTK_TREE_VIEW (window->treeview);
242 model = gtk_tree_view_get_model (view);
244 gtk_tree_view_set_headers_visible (view, TRUE);
245 gtk_tree_view_set_headers_clickable (view, TRUE);
248 column = gtk_tree_view_column_new ();
249 count = gtk_tree_view_append_column (view, column);
251 gtk_tree_view_column_set_title (column, _("Name"));
252 gtk_tree_view_column_set_expand (column, TRUE);
253 gtk_tree_view_column_set_sort_column_id (column, count - 1);
255 cell = gtk_cell_renderer_pixbuf_new ();
256 gtk_tree_view_column_pack_start (column, cell, FALSE);
257 gtk_tree_view_column_add_attribute (column, cell, "icon-name", COL_IMAGE);
259 cell = gtk_cell_renderer_text_new ();
264 gtk_tree_view_column_pack_start (column, cell, TRUE);
265 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
268 cell = gtk_cell_renderer_text_new ();
269 column = gtk_tree_view_column_new_with_attributes (_("Room"), cell,
272 count = gtk_tree_view_append_column (view, column);
273 gtk_tree_view_column_set_sort_column_id (column, count - 1);
274 window->room_column = count - 1;
276 /* Chatroom auto connect */
277 cell = gtk_cell_renderer_toggle_new ();
278 column = gtk_tree_view_column_new_with_attributes (_("Auto Connect"), cell,
279 "active", COL_AUTO_CONNECT,
281 count = gtk_tree_view_append_column (view, column);
282 gtk_tree_view_column_set_sort_column_id (column, count - 1);
284 g_signal_connect (cell, "toggled",
285 G_CALLBACK (chatrooms_window_model_cell_auto_connect_toggled),
289 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model), 0,
294 chatrooms_window_model_refresh_data (EmpathyChatroomsWindow *window,
298 GtkTreeSelection *selection;
302 GtkTreeViewColumn *column;
303 EmpathyAccountChooser *account_chooser;
305 GList *chatrooms, *l;
307 view = GTK_TREE_VIEW (window->treeview);
308 selection = gtk_tree_view_get_selection (view);
309 model = gtk_tree_view_get_model (view);
310 store = GTK_LIST_STORE (model);
312 /* Look up chatrooms */
313 account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
314 account = empathy_account_chooser_get_account (account_chooser);
316 chatrooms = empathy_chatroom_manager_get_chatrooms (window->manager, account);
318 /* Sort out columns, we only show the server column for
319 * selected protocol types, such as Jabber.
322 column = gtk_tree_view_get_column (view, window->room_column);
323 gtk_tree_view_column_set_visible (column, TRUE);
325 column = gtk_tree_view_get_column (view, window->room_column);
326 gtk_tree_view_column_set_visible (column, FALSE);
329 /* Clean out the store */
330 gtk_list_store_clear (store);
332 /* Populate with chatroom list. */
333 for (l = chatrooms; l; l = l->next) {
334 chatrooms_window_model_add (window, l->data, FALSE);
337 if (gtk_tree_model_get_iter_first (model, &iter)) {
338 gtk_tree_selection_select_iter (selection, &iter);
342 g_object_unref (account);
345 g_list_free (chatrooms);
349 chatrooms_window_model_add (EmpathyChatroomsWindow *window,
350 EmpathyChatroom *chatroom,
355 GtkTreeSelection *selection;
359 view = GTK_TREE_VIEW (window->treeview);
360 selection = gtk_tree_view_get_selection (view);
361 model = gtk_tree_view_get_model (view);
362 store = GTK_LIST_STORE (model);
364 gtk_list_store_append (store, &iter);
365 gtk_list_store_set (store, &iter,
366 COL_NAME, empathy_chatroom_get_name (chatroom),
367 COL_ROOM, empathy_chatroom_get_room (chatroom),
368 COL_AUTO_CONNECT, empathy_chatroom_get_auto_connect (chatroom),
369 COL_POINTER, chatroom,
373 gtk_tree_selection_select_iter (selection, &iter);
378 chatrooms_window_model_cell_auto_connect_toggled (GtkCellRendererToggle *cell,
380 EmpathyChatroomsWindow *window)
382 EmpathyChatroom *chatroom;
390 view = GTK_TREE_VIEW (window->treeview);
391 model = gtk_tree_view_get_model (view);
392 store = GTK_LIST_STORE (model);
394 path = gtk_tree_path_new_from_string (path_string);
396 gtk_tree_model_get_iter (model, &iter, path);
397 gtk_tree_model_get (model, &iter,
398 COL_AUTO_CONNECT, &enabled,
399 COL_POINTER, &chatroom,
404 empathy_chatroom_set_auto_connect (chatroom, enabled);
405 empathy_chatroom_manager_store (window->manager);
407 gtk_list_store_set (store, &iter, COL_AUTO_CONNECT, enabled, -1);
408 gtk_tree_path_free (path);
409 g_object_unref (chatroom);
412 static EmpathyChatroom *
413 chatrooms_window_model_get_selected (EmpathyChatroomsWindow *window)
417 GtkTreeSelection *selection;
419 EmpathyChatroom *chatroom = NULL;
421 view = GTK_TREE_VIEW (window->treeview);
422 selection = gtk_tree_view_get_selection (view);
424 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
425 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
432 chatrooms_window_model_action_selected (EmpathyChatroomsWindow *window)
434 EmpathyChatroom *chatroom;
438 view = GTK_TREE_VIEW (window->treeview);
439 model = gtk_tree_view_get_model (view);
441 chatroom = chatrooms_window_model_get_selected (window);
446 //empathy_edit_chatroom_dialog_show (GTK_WINDOW (window->window), chatroom);
448 g_object_unref (chatroom);
452 chatrooms_window_row_activated_cb (GtkTreeView *tree_view,
454 GtkTreeViewColumn *column,
455 EmpathyChatroomsWindow *window)
457 if (GTK_WIDGET_IS_SENSITIVE (window->button_edit)) {
458 chatrooms_window_model_action_selected (window);
463 chatrooms_window_button_remove_clicked_cb (GtkWidget *widget,
464 EmpathyChatroomsWindow *window)
466 EmpathyChatroom *chatroom;
469 GtkTreeSelection *selection;
472 /* Remove from treeview */
473 view = GTK_TREE_VIEW (window->treeview);
474 selection = gtk_tree_view_get_selection (view);
476 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
480 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
481 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
483 /* Remove from config */
484 empathy_chatroom_manager_remove (window->manager, chatroom);
486 g_object_unref (chatroom);
490 chatrooms_window_button_edit_clicked_cb (GtkWidget *widget,
491 EmpathyChatroomsWindow *window)
493 EmpathyChatroom *chatroom;
495 chatroom = chatrooms_window_model_get_selected (window);
500 //empathy_edit_chatroom_dialog_show (GTK_WINDOW (window->window), chatroom);
502 g_object_unref (chatroom);
506 chatrooms_window_button_close_clicked_cb (GtkWidget *widget,
507 EmpathyChatroomsWindow *window)
509 gtk_widget_destroy (window->window);
513 chatrooms_window_chatroom_added_cb (EmpathyChatroomManager *manager,
514 EmpathyChatroom *chatroom,
515 EmpathyChatroomsWindow *window)
517 EmpathyAccountChooser *account_chooser;
520 account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
521 account = empathy_account_chooser_get_account (account_chooser);
524 chatrooms_window_model_add (window, chatroom, FALSE);
526 if (empathy_account_equal (account, empathy_chatroom_get_account (chatroom))) {
527 chatrooms_window_model_add (window, chatroom, FALSE);
530 g_object_unref (account);
535 chatrooms_window_chatroom_removed_cb (EmpathyChatroomManager *manager,
536 EmpathyChatroom *chatroom,
537 EmpathyChatroomsWindow *window)
541 model = gtk_tree_view_get_model (GTK_TREE_VIEW (window->treeview));
543 gtk_tree_model_foreach (model,
544 (GtkTreeModelForeachFunc) chatrooms_window_remove_chatroom_foreach,
549 chatrooms_window_remove_chatroom_foreach (GtkTreeModel *model,
552 EmpathyChatroom *chatroom)
554 EmpathyChatroom *this_chatroom;
556 gtk_tree_model_get (model, iter, COL_POINTER, &this_chatroom, -1);
558 if (empathy_chatroom_equal (chatroom, this_chatroom)) {
559 gtk_list_store_remove (GTK_LIST_STORE (model), iter);
560 g_object_unref (this_chatroom);
564 g_object_unref (this_chatroom);
570 chatrooms_window_account_changed_cb (GtkWidget *combo_box,
571 EmpathyChatroomsWindow *window)
573 chatrooms_window_model_refresh_data (window, FALSE);