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;
114 gtk_window_present (GTK_WINDOW (window->window));
118 window = g_new0 (EmpathyChatroomsWindow, 1);
120 filename = empathy_file_lookup ("empathy-chatrooms-window.glade", "src");
121 glade = empathy_glade_get_file (filename,
124 "chatrooms_window", &window->window,
125 "hbox_account", &window->hbox_account,
126 "label_account", &window->label_account,
127 "treeview", &window->treeview,
128 "button_edit", &window->button_edit,
129 "button_remove", &window->button_remove,
130 "button_close", &window->button_close,
134 empathy_glade_connect (glade,
136 "chatrooms_window", "destroy", chatrooms_window_destroy_cb,
137 "button_remove", "clicked", chatrooms_window_button_remove_clicked_cb,
138 "button_edit", "clicked", chatrooms_window_button_edit_clicked_cb,
139 "button_close", "clicked", chatrooms_window_button_close_clicked_cb,
142 g_object_unref (glade);
144 g_object_add_weak_pointer (G_OBJECT (window->window), (gpointer) &window);
146 /* Get the session and chat room manager */
147 window->manager = empathy_chatroom_manager_dup_singleton (NULL);
149 g_signal_connect (window->manager, "chatroom-added",
150 G_CALLBACK (chatrooms_window_chatroom_added_cb),
152 g_signal_connect (window->manager, "chatroom-removed",
153 G_CALLBACK (chatrooms_window_chatroom_removed_cb),
156 /* Account chooser for chat rooms */
157 window->account_chooser = empathy_account_chooser_new ();
158 empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (window->account_chooser),
159 empathy_account_chooser_filter_is_connected,
161 g_object_set (window->account_chooser,
162 "has-all-option", TRUE,
164 empathy_account_chooser_set_account (EMPATHY_ACCOUNT_CHOOSER (window->account_chooser), NULL);
166 gtk_box_pack_start (GTK_BOX (window->hbox_account),
167 window->account_chooser,
170 g_signal_connect (window->account_chooser, "changed",
171 G_CALLBACK (chatrooms_window_account_changed_cb),
174 gtk_widget_show (window->account_chooser);
176 /* Set up chatrooms */
177 chatrooms_window_model_setup (window);
180 gtk_widget_grab_focus (window->treeview);
184 gtk_window_set_transient_for (GTK_WINDOW (window->window),
185 GTK_WINDOW (parent));
188 gtk_widget_show (window->window);
192 chatrooms_window_destroy_cb (GtkWidget *widget,
193 EmpathyChatroomsWindow *window)
195 g_signal_handlers_disconnect_by_func (window->manager,
196 chatrooms_window_chatroom_added_cb,
198 g_signal_handlers_disconnect_by_func (window->manager,
199 chatrooms_window_chatroom_removed_cb,
201 g_object_unref (window->manager);
206 chatrooms_window_model_setup (EmpathyChatroomsWindow *window)
210 GtkTreeSelection *selection;
213 view = GTK_TREE_VIEW (window->treeview);
215 g_signal_connect (view, "row-activated",
216 G_CALLBACK (chatrooms_window_row_activated_cb),
220 store = gtk_list_store_new (COL_COUNT,
221 G_TYPE_STRING, /* Image */
222 G_TYPE_STRING, /* Name */
223 G_TYPE_STRING, /* Room */
224 G_TYPE_BOOLEAN, /* Auto start */
225 EMPATHY_TYPE_CHATROOM); /* Chatroom */
227 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
230 selection = gtk_tree_view_get_selection (view);
231 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
234 chatrooms_window_model_add_columns (window);
237 chatrooms_window_model_refresh_data (window, TRUE);
240 g_object_unref (store);
244 chatrooms_window_model_add_columns (EmpathyChatroomsWindow *window)
248 GtkTreeViewColumn *column;
249 GtkCellRenderer *cell;
252 view = GTK_TREE_VIEW (window->treeview);
253 model = gtk_tree_view_get_model (view);
255 gtk_tree_view_set_headers_visible (view, TRUE);
256 gtk_tree_view_set_headers_clickable (view, TRUE);
259 column = gtk_tree_view_column_new ();
260 count = gtk_tree_view_append_column (view, column);
262 gtk_tree_view_column_set_title (column, _("Name"));
263 gtk_tree_view_column_set_expand (column, TRUE);
264 gtk_tree_view_column_set_sort_column_id (column, count - 1);
266 cell = gtk_cell_renderer_pixbuf_new ();
267 gtk_tree_view_column_pack_start (column, cell, FALSE);
268 gtk_tree_view_column_add_attribute (column, cell, "icon-name", COL_IMAGE);
270 cell = gtk_cell_renderer_text_new ();
275 gtk_tree_view_column_pack_start (column, cell, TRUE);
276 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
279 cell = gtk_cell_renderer_text_new ();
280 column = gtk_tree_view_column_new_with_attributes (_("Room"), cell,
283 count = gtk_tree_view_append_column (view, column);
284 gtk_tree_view_column_set_sort_column_id (column, count - 1);
285 window->room_column = count - 1;
287 /* Chatroom auto connect */
288 cell = gtk_cell_renderer_toggle_new ();
289 column = gtk_tree_view_column_new_with_attributes (_("Auto-Connect"), cell,
290 "active", COL_AUTO_CONNECT,
292 count = gtk_tree_view_append_column (view, column);
293 gtk_tree_view_column_set_sort_column_id (column, count - 1);
295 g_signal_connect (cell, "toggled",
296 G_CALLBACK (chatrooms_window_model_cell_auto_connect_toggled),
300 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model), 0,
305 chatrooms_window_model_refresh_data (EmpathyChatroomsWindow *window,
309 GtkTreeSelection *selection;
313 GtkTreeViewColumn *column;
314 EmpathyAccountChooser *account_chooser;
316 GList *chatrooms, *l;
318 view = GTK_TREE_VIEW (window->treeview);
319 selection = gtk_tree_view_get_selection (view);
320 model = gtk_tree_view_get_model (view);
321 store = GTK_LIST_STORE (model);
323 /* Look up chatrooms */
324 account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
325 account = empathy_account_chooser_get_account (account_chooser);
327 chatrooms = empathy_chatroom_manager_get_chatrooms (window->manager, account);
329 /* Sort out columns, we only show the server column for
330 * selected protocol types, such as Jabber.
333 column = gtk_tree_view_get_column (view, window->room_column);
334 gtk_tree_view_column_set_visible (column, TRUE);
336 column = gtk_tree_view_get_column (view, window->room_column);
337 gtk_tree_view_column_set_visible (column, FALSE);
340 /* Clean out the store */
341 gtk_list_store_clear (store);
343 /* Populate with chatroom list. */
344 for (l = chatrooms; l; l = l->next) {
345 chatrooms_window_model_add (window, l->data, FALSE);
348 if (gtk_tree_model_get_iter_first (model, &iter)) {
349 gtk_tree_selection_select_iter (selection, &iter);
353 g_object_unref (account);
356 g_list_free (chatrooms);
360 chatrooms_window_model_add (EmpathyChatroomsWindow *window,
361 EmpathyChatroom *chatroom,
366 GtkTreeSelection *selection;
370 view = GTK_TREE_VIEW (window->treeview);
371 selection = gtk_tree_view_get_selection (view);
372 model = gtk_tree_view_get_model (view);
373 store = GTK_LIST_STORE (model);
375 gtk_list_store_append (store, &iter);
376 gtk_list_store_set (store, &iter,
377 COL_NAME, empathy_chatroom_get_name (chatroom),
378 COL_ROOM, empathy_chatroom_get_room (chatroom),
379 COL_AUTO_CONNECT, empathy_chatroom_get_auto_connect (chatroom),
380 COL_POINTER, chatroom,
384 gtk_tree_selection_select_iter (selection, &iter);
389 chatrooms_window_model_cell_auto_connect_toggled (GtkCellRendererToggle *cell,
391 EmpathyChatroomsWindow *window)
393 EmpathyChatroom *chatroom;
401 view = GTK_TREE_VIEW (window->treeview);
402 model = gtk_tree_view_get_model (view);
403 store = GTK_LIST_STORE (model);
405 path = gtk_tree_path_new_from_string (path_string);
407 gtk_tree_model_get_iter (model, &iter, path);
408 gtk_tree_model_get (model, &iter,
409 COL_AUTO_CONNECT, &enabled,
410 COL_POINTER, &chatroom,
415 empathy_chatroom_set_auto_connect (chatroom, enabled);
417 gtk_list_store_set (store, &iter, COL_AUTO_CONNECT, enabled, -1);
418 gtk_tree_path_free (path);
419 g_object_unref (chatroom);
422 static EmpathyChatroom *
423 chatrooms_window_model_get_selected (EmpathyChatroomsWindow *window)
427 GtkTreeSelection *selection;
429 EmpathyChatroom *chatroom = NULL;
431 view = GTK_TREE_VIEW (window->treeview);
432 selection = gtk_tree_view_get_selection (view);
434 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
435 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
442 chatrooms_window_model_action_selected (EmpathyChatroomsWindow *window)
444 EmpathyChatroom *chatroom;
448 view = GTK_TREE_VIEW (window->treeview);
449 model = gtk_tree_view_get_model (view);
451 chatroom = chatrooms_window_model_get_selected (window);
456 //empathy_edit_chatroom_dialog_show (GTK_WINDOW (window->window), chatroom);
458 g_object_unref (chatroom);
462 chatrooms_window_row_activated_cb (GtkTreeView *tree_view,
464 GtkTreeViewColumn *column,
465 EmpathyChatroomsWindow *window)
467 if (GTK_WIDGET_IS_SENSITIVE (window->button_edit)) {
468 chatrooms_window_model_action_selected (window);
473 chatrooms_window_button_remove_clicked_cb (GtkWidget *widget,
474 EmpathyChatroomsWindow *window)
476 EmpathyChatroom *chatroom;
479 GtkTreeSelection *selection;
482 /* Remove from treeview */
483 view = GTK_TREE_VIEW (window->treeview);
484 selection = gtk_tree_view_get_selection (view);
486 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
490 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
491 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
493 /* Remove from config */
494 empathy_chatroom_manager_remove (window->manager, chatroom);
496 g_object_unref (chatroom);
500 chatrooms_window_button_edit_clicked_cb (GtkWidget *widget,
501 EmpathyChatroomsWindow *window)
503 EmpathyChatroom *chatroom;
505 chatroom = chatrooms_window_model_get_selected (window);
510 //empathy_edit_chatroom_dialog_show (GTK_WINDOW (window->window), chatroom);
512 g_object_unref (chatroom);
516 chatrooms_window_button_close_clicked_cb (GtkWidget *widget,
517 EmpathyChatroomsWindow *window)
519 gtk_widget_destroy (window->window);
523 chatrooms_window_chatroom_added_cb (EmpathyChatroomManager *manager,
524 EmpathyChatroom *chatroom,
525 EmpathyChatroomsWindow *window)
527 EmpathyAccountChooser *account_chooser;
530 account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
531 account = empathy_account_chooser_get_account (account_chooser);
534 chatrooms_window_model_add (window, chatroom, FALSE);
536 if (empathy_account_equal (account, empathy_chatroom_get_account (chatroom))) {
537 chatrooms_window_model_add (window, chatroom, FALSE);
540 g_object_unref (account);
545 chatrooms_window_chatroom_removed_cb (EmpathyChatroomManager *manager,
546 EmpathyChatroom *chatroom,
547 EmpathyChatroomsWindow *window)
551 model = gtk_tree_view_get_model (GTK_TREE_VIEW (window->treeview));
553 gtk_tree_model_foreach (model,
554 (GtkTreeModelForeachFunc) chatrooms_window_remove_chatroom_foreach,
559 chatrooms_window_remove_chatroom_foreach (GtkTreeModel *model,
562 EmpathyChatroom *chatroom)
564 EmpathyChatroom *this_chatroom;
566 gtk_tree_model_get (model, iter, COL_POINTER, &this_chatroom, -1);
568 if (empathy_chatroom_equal (chatroom, this_chatroom)) {
569 gtk_list_store_remove (GTK_LIST_STORE (model), iter);
570 g_object_unref (this_chatroom);
574 g_object_unref (this_chatroom);
580 chatrooms_window_account_changed_cb (GtkWidget *combo_box,
581 EmpathyChatroomsWindow *window)
583 chatrooms_window_model_refresh_data (window, FALSE);