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/gossip-chatroom-manager.h>
37 #include <libempathy/gossip-utils.h>
39 #include "gossip-account-chooser.h"
40 #include "gossip-chatrooms-window.h"
41 //#include "gossip-edit-chatroom-dialog.h"
42 #include "gossip-new-chatroom-dialog.h"
43 #include "gossip-ui-utils.h"
46 GossipChatroomManager *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 } GossipChatroomsWindow;
60 static void chatrooms_window_destroy_cb (GtkWidget *widget,
61 GossipChatroomsWindow *window);
62 static void chatrooms_window_model_setup (GossipChatroomsWindow *window);
63 static void chatrooms_window_model_add_columns (GossipChatroomsWindow *window);
64 static void chatrooms_window_model_refresh_data (GossipChatroomsWindow *window,
66 static void chatrooms_window_model_add (GossipChatroomsWindow *window,
67 GossipChatroom *chatroom,
69 static void chatrooms_window_model_cell_auto_connect_toggled (GtkCellRendererToggle *cell,
71 GossipChatroomsWindow *window);
72 static GossipChatroom * chatrooms_window_model_get_selected (GossipChatroomsWindow *window);
73 static void chatrooms_window_model_action_selected (GossipChatroomsWindow *window);
74 static void chatrooms_window_row_activated_cb (GtkTreeView *tree_view,
76 GtkTreeViewColumn *column,
77 GossipChatroomsWindow *window);
78 static void chatrooms_window_button_remove_clicked_cb (GtkWidget *widget,
79 GossipChatroomsWindow *window);
80 static void chatrooms_window_button_edit_clicked_cb (GtkWidget *widget,
81 GossipChatroomsWindow *window);
82 static void chatrooms_window_button_close_clicked_cb (GtkWidget *widget,
83 GossipChatroomsWindow *window);
84 static void chatrooms_window_chatroom_added_cb (GossipChatroomManager *manager,
85 GossipChatroom *chatroom,
86 GossipChatroomsWindow *window);
87 static void chatrooms_window_account_changed_cb (GtkWidget *combo_box,
88 GossipChatroomsWindow *window);
100 gossip_chatrooms_window_show (GtkWindow *parent)
102 static GossipChatroomsWindow *window = NULL;
106 gtk_window_present (GTK_WINDOW (window->window));
110 window = g_new0 (GossipChatroomsWindow, 1);
112 glade = gossip_glade_get_file ("gossip-chatrooms-window.glade",
115 "chatrooms_window", &window->window,
116 "hbox_account", &window->hbox_account,
117 "label_account", &window->label_account,
118 "treeview", &window->treeview,
119 "button_edit", &window->button_edit,
120 "button_remove", &window->button_remove,
121 "button_close", &window->button_close,
124 gossip_glade_connect (glade,
126 "chatrooms_window", "destroy", chatrooms_window_destroy_cb,
127 "button_remove", "clicked", chatrooms_window_button_remove_clicked_cb,
128 "button_edit", "clicked", chatrooms_window_button_edit_clicked_cb,
129 "button_close", "clicked", chatrooms_window_button_close_clicked_cb,
132 g_object_unref (glade);
134 g_object_add_weak_pointer (G_OBJECT (window->window), (gpointer) &window);
136 /* Get the session and chat room manager */
137 window->manager = gossip_chatroom_manager_new ();
139 g_signal_connect (window->manager, "chatroom-added",
140 G_CALLBACK (chatrooms_window_chatroom_added_cb),
143 /* Account chooser for chat rooms */
144 window->account_chooser = gossip_account_chooser_new ();
145 gossip_account_chooser_set_account (GOSSIP_ACCOUNT_CHOOSER (window->account_chooser), NULL);
146 g_object_set (window->account_chooser,
147 "can-select-all", TRUE,
148 "has-all-option", TRUE,
151 gtk_box_pack_start (GTK_BOX (window->hbox_account),
152 window->account_chooser,
155 g_signal_connect (window->account_chooser, "changed",
156 G_CALLBACK (chatrooms_window_account_changed_cb),
159 gtk_widget_show (window->account_chooser);
161 /* Set up chatrooms */
162 chatrooms_window_model_setup (window);
165 gtk_widget_grab_focus (window->treeview);
169 gtk_window_set_transient_for (GTK_WINDOW (window->window),
170 GTK_WINDOW (parent));
173 gtk_widget_show (window->window);
177 chatrooms_window_destroy_cb (GtkWidget *widget,
178 GossipChatroomsWindow *window)
180 g_object_unref (window->manager);
185 chatrooms_window_model_setup (GossipChatroomsWindow *window)
189 GtkTreeSelection *selection;
192 view = GTK_TREE_VIEW (window->treeview);
194 g_signal_connect (view, "row-activated",
195 G_CALLBACK (chatrooms_window_row_activated_cb),
199 store = gtk_list_store_new (COL_COUNT,
200 G_TYPE_STRING, /* Image */
201 G_TYPE_STRING, /* Name */
202 G_TYPE_STRING, /* Room */
203 G_TYPE_BOOLEAN, /* Auto start */
204 GOSSIP_TYPE_CHATROOM); /* Chatroom */
206 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
209 selection = gtk_tree_view_get_selection (view);
210 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
213 chatrooms_window_model_add_columns (window);
216 chatrooms_window_model_refresh_data (window, TRUE);
219 g_object_unref (store);
223 chatrooms_window_model_add_columns (GossipChatroomsWindow *window)
227 GtkTreeViewColumn *column;
228 GtkCellRenderer *cell;
231 view = GTK_TREE_VIEW (window->treeview);
232 model = gtk_tree_view_get_model (view);
234 gtk_tree_view_set_headers_visible (view, TRUE);
235 gtk_tree_view_set_headers_clickable (view, TRUE);
238 column = gtk_tree_view_column_new ();
239 count = gtk_tree_view_append_column (view, column);
241 gtk_tree_view_column_set_title (column, _("Name"));
242 gtk_tree_view_column_set_expand (column, TRUE);
243 gtk_tree_view_column_set_sort_column_id (column, count - 1);
245 cell = gtk_cell_renderer_pixbuf_new ();
246 gtk_tree_view_column_pack_start (column, cell, FALSE);
247 gtk_tree_view_column_add_attribute (column, cell, "icon-name", COL_IMAGE);
249 cell = gtk_cell_renderer_text_new ();
254 gtk_tree_view_column_pack_start (column, cell, TRUE);
255 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
258 cell = gtk_cell_renderer_text_new ();
259 column = gtk_tree_view_column_new_with_attributes (_("Room"), cell,
262 count = gtk_tree_view_append_column (view, column);
263 gtk_tree_view_column_set_sort_column_id (column, count - 1);
264 window->room_column = count - 1;
266 /* Chatroom auto connect */
267 cell = gtk_cell_renderer_toggle_new ();
268 column = gtk_tree_view_column_new_with_attributes (_("Auto Connect"), cell,
269 "active", COL_AUTO_CONNECT,
271 count = gtk_tree_view_append_column (view, column);
272 gtk_tree_view_column_set_sort_column_id (column, count - 1);
274 g_signal_connect (cell, "toggled",
275 G_CALLBACK (chatrooms_window_model_cell_auto_connect_toggled),
279 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model), 0,
284 chatrooms_window_model_refresh_data (GossipChatroomsWindow *window,
288 GtkTreeSelection *selection;
292 GtkTreeViewColumn *column;
293 GossipAccountChooser *account_chooser;
295 GList *chatrooms, *l;
297 view = GTK_TREE_VIEW (window->treeview);
298 selection = gtk_tree_view_get_selection (view);
299 model = gtk_tree_view_get_model (view);
300 store = GTK_LIST_STORE (model);
302 /* Look up chatrooms */
303 account_chooser = GOSSIP_ACCOUNT_CHOOSER (window->account_chooser);
304 account = gossip_account_chooser_get_account (account_chooser);
306 chatrooms = gossip_chatroom_manager_get_chatrooms (window->manager, account);
308 /* Sort out columns, we only show the server column for
309 * selected protocol types, such as Jabber.
312 column = gtk_tree_view_get_column (view, window->room_column);
313 gtk_tree_view_column_set_visible (column, TRUE);
315 column = gtk_tree_view_get_column (view, window->room_column);
316 gtk_tree_view_column_set_visible (column, FALSE);
319 /* Clean out the store */
320 gtk_list_store_clear (store);
322 /* Populate with chatroom list. */
323 for (l = chatrooms; l; l = l->next) {
324 chatrooms_window_model_add (window, l->data, FALSE);
327 if (gtk_tree_model_get_iter_first (model, &iter)) {
328 gtk_tree_selection_select_iter (selection, &iter);
332 g_object_unref (account);
335 g_list_free (chatrooms);
339 chatrooms_window_model_add (GossipChatroomsWindow *window,
340 GossipChatroom *chatroom,
345 GtkTreeSelection *selection;
349 view = GTK_TREE_VIEW (window->treeview);
350 selection = gtk_tree_view_get_selection (view);
351 model = gtk_tree_view_get_model (view);
352 store = GTK_LIST_STORE (model);
354 gtk_list_store_append (store, &iter);
355 gtk_list_store_set (store, &iter,
356 COL_NAME, gossip_chatroom_get_name (chatroom),
357 COL_ROOM, gossip_chatroom_get_room (chatroom),
358 COL_AUTO_CONNECT, gossip_chatroom_get_auto_connect (chatroom),
359 COL_POINTER, chatroom,
363 gtk_tree_selection_select_iter (selection, &iter);
368 chatrooms_window_model_cell_auto_connect_toggled (GtkCellRendererToggle *cell,
370 GossipChatroomsWindow *window)
372 GossipChatroom *chatroom;
380 view = GTK_TREE_VIEW (window->treeview);
381 model = gtk_tree_view_get_model (view);
382 store = GTK_LIST_STORE (model);
384 path = gtk_tree_path_new_from_string (path_string);
386 gtk_tree_model_get_iter (model, &iter, path);
387 gtk_tree_model_get (model, &iter,
388 COL_AUTO_CONNECT, &enabled,
389 COL_POINTER, &chatroom,
394 gossip_chatroom_set_auto_connect (chatroom, enabled);
395 gossip_chatroom_manager_store (window->manager);
397 gtk_list_store_set (store, &iter, COL_AUTO_CONNECT, enabled, -1);
398 gtk_tree_path_free (path);
399 g_object_unref (chatroom);
402 static GossipChatroom *
403 chatrooms_window_model_get_selected (GossipChatroomsWindow *window)
407 GtkTreeSelection *selection;
409 GossipChatroom *chatroom = NULL;
411 view = GTK_TREE_VIEW (window->treeview);
412 selection = gtk_tree_view_get_selection (view);
414 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
415 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
422 chatrooms_window_model_action_selected (GossipChatroomsWindow *window)
424 GossipChatroom *chatroom;
428 view = GTK_TREE_VIEW (window->treeview);
429 model = gtk_tree_view_get_model (view);
431 chatroom = chatrooms_window_model_get_selected (window);
436 //gossip_edit_chatroom_dialog_show (GTK_WINDOW (window->window), chatroom);
438 g_object_unref (chatroom);
442 chatrooms_window_row_activated_cb (GtkTreeView *tree_view,
444 GtkTreeViewColumn *column,
445 GossipChatroomsWindow *window)
447 if (GTK_WIDGET_IS_SENSITIVE (window->button_edit)) {
448 chatrooms_window_model_action_selected (window);
453 chatrooms_window_button_remove_clicked_cb (GtkWidget *widget,
454 GossipChatroomsWindow *window)
456 GossipChatroom *chatroom;
459 GtkTreeSelection *selection;
462 /* Remove from treeview */
463 view = GTK_TREE_VIEW (window->treeview);
464 selection = gtk_tree_view_get_selection (view);
466 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
470 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
471 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
473 /* Remove from config */
474 gossip_chatroom_manager_remove (window->manager, chatroom);
476 g_object_unref (chatroom);
480 chatrooms_window_button_edit_clicked_cb (GtkWidget *widget,
481 GossipChatroomsWindow *window)
483 GossipChatroom *chatroom;
485 chatroom = chatrooms_window_model_get_selected (window);
490 //gossip_edit_chatroom_dialog_show (GTK_WINDOW (window->window), chatroom);
492 g_object_unref (chatroom);
496 chatrooms_window_button_close_clicked_cb (GtkWidget *widget,
497 GossipChatroomsWindow *window)
499 gtk_widget_destroy (window->window);
503 chatrooms_window_chatroom_added_cb (GossipChatroomManager *manager,
504 GossipChatroom *chatroom,
505 GossipChatroomsWindow *window)
507 GossipAccountChooser *account_chooser;
510 account_chooser = GOSSIP_ACCOUNT_CHOOSER (window->account_chooser);
511 account = gossip_account_chooser_get_account (account_chooser);
514 chatrooms_window_model_add (window, chatroom, FALSE);
516 if (gossip_account_equal (account, gossip_chatroom_get_account (chatroom))) {
517 chatrooms_window_model_add (window, chatroom, FALSE);
520 g_object_unref (account);
525 chatrooms_window_account_changed_cb (GtkWidget *combo_box,
526 GossipChatroomsWindow *window)
528 chatrooms_window_model_refresh_data (window, FALSE);