Update python binding
[empathy.git] / src / empathy-chatrooms-window.c
blobabd6447f64a01041f467e82ccf8976a353e22208
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
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>
26 #include <config.h>
28 #include <string.h>
29 #include <stdio.h>
31 #include <gtk/gtk.h>
32 #include <glib.h>
33 #include <glib/gi18n.h>
35 #include <libempathy/empathy-chatroom-manager.h>
36 #include <libempathy/empathy-utils.h>
38 #include <libempathy-gtk/empathy-account-chooser.h>
39 #include <libempathy-gtk/empathy-ui-utils.h>
41 #include "empathy-chatrooms-window.h"
42 #include "empathy-new-chatroom-dialog.h"
44 typedef struct {
45 EmpathyChatroomManager *manager;
47 GtkWidget *window;
48 GtkWidget *hbox_account;
49 GtkWidget *label_account;
50 GtkWidget *account_chooser;
51 GtkWidget *treeview;
52 GtkWidget *button_remove;
53 GtkWidget *button_edit;
54 GtkWidget *button_close;
56 gint room_column;
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,
64 gboolean first_time);
65 static void chatrooms_window_model_add (EmpathyChatroomsWindow *window,
66 EmpathyChatroom *chatroom,
67 gboolean set_active);
68 static void chatrooms_window_model_cell_auto_connect_toggled (GtkCellRendererToggle *cell,
69 gchar *path_string,
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,
74 GtkTreePath *path,
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,
90 GtkTreePath *path,
91 GtkTreeIter *iter,
92 EmpathyChatroom *chatroom);
93 static void chatrooms_window_account_changed_cb (GtkWidget *combo_box,
94 EmpathyChatroomsWindow *window);
96 enum {
97 COL_IMAGE,
98 COL_NAME,
99 COL_ROOM,
100 COL_AUTO_CONNECT,
101 COL_POINTER,
102 COL_COUNT
105 void
106 empathy_chatrooms_window_show (GtkWindow *parent)
108 static EmpathyChatroomsWindow *window = NULL;
109 GtkBuilder *gui;
110 gchar *filename;
112 if (window) {
113 gtk_window_present (GTK_WINDOW (window->window));
114 return;
117 window = g_new0 (EmpathyChatroomsWindow, 1);
119 filename = empathy_file_lookup ("empathy-chatrooms-window.ui", "src");
120 gui = empathy_builder_get_file (filename,
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,
128 NULL);
129 g_free (filename);
131 empathy_builder_connect (gui, window,
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,
136 NULL);
138 g_object_unref (gui);
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_dup_singleton (NULL);
145 g_signal_connect (window->manager, "chatroom-added",
146 G_CALLBACK (chatrooms_window_chatroom_added_cb),
147 window);
148 g_signal_connect (window->manager, "chatroom-removed",
149 G_CALLBACK (chatrooms_window_chatroom_removed_cb),
150 window);
152 /* Account chooser for chat rooms */
153 window->account_chooser = empathy_account_chooser_new ();
154 empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (window->account_chooser),
155 empathy_account_chooser_filter_is_connected,
156 NULL);
157 g_object_set (window->account_chooser,
158 "has-all-option", TRUE,
159 NULL);
160 empathy_account_chooser_set_account (EMPATHY_ACCOUNT_CHOOSER (window->account_chooser), NULL);
162 gtk_box_pack_start (GTK_BOX (window->hbox_account),
163 window->account_chooser,
164 TRUE, TRUE, 0);
166 g_signal_connect (window->account_chooser, "changed",
167 G_CALLBACK (chatrooms_window_account_changed_cb),
168 window);
170 gtk_widget_show (window->account_chooser);
172 /* Set up chatrooms */
173 chatrooms_window_model_setup (window);
175 /* Set focus */
176 gtk_widget_grab_focus (window->treeview);
178 /* Last touches */
179 if (parent) {
180 gtk_window_set_transient_for (GTK_WINDOW (window->window),
181 GTK_WINDOW (parent));
184 gtk_widget_show (window->window);
187 static void
188 chatrooms_window_destroy_cb (GtkWidget *widget,
189 EmpathyChatroomsWindow *window)
191 g_signal_handlers_disconnect_by_func (window->manager,
192 chatrooms_window_chatroom_added_cb,
193 window);
194 g_signal_handlers_disconnect_by_func (window->manager,
195 chatrooms_window_chatroom_removed_cb,
196 window);
197 g_object_unref (window->manager);
198 g_free (window);
201 static void
202 chatrooms_window_model_setup (EmpathyChatroomsWindow *window)
204 GtkTreeView *view;
205 GtkListStore *store;
206 GtkTreeSelection *selection;
208 /* View */
209 view = GTK_TREE_VIEW (window->treeview);
211 g_signal_connect (view, "row-activated",
212 G_CALLBACK (chatrooms_window_row_activated_cb),
213 window);
215 /* Store */
216 store = gtk_list_store_new (COL_COUNT,
217 G_TYPE_STRING, /* Image */
218 G_TYPE_STRING, /* Name */
219 G_TYPE_STRING, /* Room */
220 G_TYPE_BOOLEAN, /* Auto start */
221 EMPATHY_TYPE_CHATROOM); /* Chatroom */
223 gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
225 /* Selection */
226 selection = gtk_tree_view_get_selection (view);
227 gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
229 /* Columns */
230 chatrooms_window_model_add_columns (window);
232 /* Add data */
233 chatrooms_window_model_refresh_data (window, TRUE);
235 /* Clean up */
236 g_object_unref (store);
239 static void
240 chatrooms_window_model_add_columns (EmpathyChatroomsWindow *window)
242 GtkTreeView *view;
243 GtkTreeModel *model;
244 GtkTreeViewColumn *column;
245 GtkCellRenderer *cell;
246 gint count;
248 view = GTK_TREE_VIEW (window->treeview);
249 model = gtk_tree_view_get_model (view);
251 gtk_tree_view_set_headers_visible (view, TRUE);
252 gtk_tree_view_set_headers_clickable (view, TRUE);
254 /* Name & Status */
255 column = gtk_tree_view_column_new ();
256 count = gtk_tree_view_append_column (view, column);
258 gtk_tree_view_column_set_title (column, _("Name"));
259 gtk_tree_view_column_set_expand (column, TRUE);
260 gtk_tree_view_column_set_sort_column_id (column, count - 1);
262 cell = gtk_cell_renderer_pixbuf_new ();
263 gtk_tree_view_column_pack_start (column, cell, FALSE);
264 gtk_tree_view_column_add_attribute (column, cell, "icon-name", COL_IMAGE);
266 cell = gtk_cell_renderer_text_new ();
267 g_object_set (cell,
268 "xpad", 4,
269 "ypad", 1,
270 NULL);
271 gtk_tree_view_column_pack_start (column, cell, TRUE);
272 gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
274 /* Room */
275 cell = gtk_cell_renderer_text_new ();
276 column = gtk_tree_view_column_new_with_attributes (_("Room"), cell,
277 "text", COL_ROOM,
278 NULL);
279 count = gtk_tree_view_append_column (view, column);
280 gtk_tree_view_column_set_sort_column_id (column, count - 1);
281 window->room_column = count - 1;
283 /* Chatroom auto connect */
284 cell = gtk_cell_renderer_toggle_new ();
285 column = gtk_tree_view_column_new_with_attributes (_("Auto-Connect"), cell,
286 "active", COL_AUTO_CONNECT,
287 NULL);
288 count = gtk_tree_view_append_column (view, column);
289 gtk_tree_view_column_set_sort_column_id (column, count - 1);
291 g_signal_connect (cell, "toggled",
292 G_CALLBACK (chatrooms_window_model_cell_auto_connect_toggled),
293 window);
295 /* Sort model */
296 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model), 0,
297 GTK_SORT_ASCENDING);
300 static void
301 chatrooms_window_model_refresh_data (EmpathyChatroomsWindow *window,
302 gboolean first_time)
304 GtkTreeView *view;
305 GtkTreeSelection *selection;
306 GtkTreeModel *model;
307 GtkListStore *store;
308 GtkTreeIter iter;
309 GtkTreeViewColumn *column;
310 EmpathyAccountChooser *account_chooser;
311 McAccount *account;
312 GList *chatrooms, *l;
314 view = GTK_TREE_VIEW (window->treeview);
315 selection = gtk_tree_view_get_selection (view);
316 model = gtk_tree_view_get_model (view);
317 store = GTK_LIST_STORE (model);
319 /* Look up chatrooms */
320 account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
321 account = empathy_account_chooser_get_account (account_chooser);
323 chatrooms = empathy_chatroom_manager_get_chatrooms (window->manager, account);
325 /* Sort out columns, we only show the server column for
326 * selected protocol types, such as Jabber.
328 if (account) {
329 column = gtk_tree_view_get_column (view, window->room_column);
330 gtk_tree_view_column_set_visible (column, TRUE);
331 } else {
332 column = gtk_tree_view_get_column (view, window->room_column);
333 gtk_tree_view_column_set_visible (column, FALSE);
336 /* Clean out the store */
337 gtk_list_store_clear (store);
339 /* Populate with chatroom list. */
340 for (l = chatrooms; l; l = l->next) {
341 chatrooms_window_model_add (window, l->data, FALSE);
344 if (gtk_tree_model_get_iter_first (model, &iter)) {
345 gtk_tree_selection_select_iter (selection, &iter);
348 if (account) {
349 g_object_unref (account);
352 g_list_free (chatrooms);
355 static void
356 chatrooms_window_model_add (EmpathyChatroomsWindow *window,
357 EmpathyChatroom *chatroom,
358 gboolean set_active)
360 GtkTreeView *view;
361 GtkTreeModel *model;
362 GtkTreeSelection *selection;
363 GtkListStore *store;
364 GtkTreeIter iter;
366 view = GTK_TREE_VIEW (window->treeview);
367 selection = gtk_tree_view_get_selection (view);
368 model = gtk_tree_view_get_model (view);
369 store = GTK_LIST_STORE (model);
371 gtk_list_store_append (store, &iter);
372 gtk_list_store_set (store, &iter,
373 COL_NAME, empathy_chatroom_get_name (chatroom),
374 COL_ROOM, empathy_chatroom_get_room (chatroom),
375 COL_AUTO_CONNECT, empathy_chatroom_get_auto_connect (chatroom),
376 COL_POINTER, chatroom,
377 -1);
379 if (set_active) {
380 gtk_tree_selection_select_iter (selection, &iter);
384 static void
385 chatrooms_window_model_cell_auto_connect_toggled (GtkCellRendererToggle *cell,
386 gchar *path_string,
387 EmpathyChatroomsWindow *window)
389 EmpathyChatroom *chatroom;
390 gboolean enabled;
391 GtkTreeView *view;
392 GtkTreeModel *model;
393 GtkListStore *store;
394 GtkTreePath *path;
395 GtkTreeIter iter;
397 view = GTK_TREE_VIEW (window->treeview);
398 model = gtk_tree_view_get_model (view);
399 store = GTK_LIST_STORE (model);
401 path = gtk_tree_path_new_from_string (path_string);
403 gtk_tree_model_get_iter (model, &iter, path);
404 gtk_tree_model_get (model, &iter,
405 COL_AUTO_CONNECT, &enabled,
406 COL_POINTER, &chatroom,
407 -1);
409 enabled = !enabled;
411 empathy_chatroom_set_auto_connect (chatroom, enabled);
413 gtk_list_store_set (store, &iter, COL_AUTO_CONNECT, enabled, -1);
414 gtk_tree_path_free (path);
415 g_object_unref (chatroom);
418 static EmpathyChatroom *
419 chatrooms_window_model_get_selected (EmpathyChatroomsWindow *window)
421 GtkTreeView *view;
422 GtkTreeModel *model;
423 GtkTreeSelection *selection;
424 GtkTreeIter iter;
425 EmpathyChatroom *chatroom = NULL;
427 view = GTK_TREE_VIEW (window->treeview);
428 selection = gtk_tree_view_get_selection (view);
430 if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
431 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
434 return chatroom;
437 static void
438 chatrooms_window_model_action_selected (EmpathyChatroomsWindow *window)
440 /* FIXME: This is still not implemented since Gossip fork */
443 static void
444 chatrooms_window_row_activated_cb (GtkTreeView *tree_view,
445 GtkTreePath *path,
446 GtkTreeViewColumn *column,
447 EmpathyChatroomsWindow *window)
449 if (GTK_WIDGET_IS_SENSITIVE (window->button_edit)) {
450 chatrooms_window_model_action_selected (window);
454 static void
455 chatrooms_window_button_remove_clicked_cb (GtkWidget *widget,
456 EmpathyChatroomsWindow *window)
458 EmpathyChatroom *chatroom;
459 GtkTreeView *view;
460 GtkTreeModel *model;
461 GtkTreeSelection *selection;
462 GtkTreeIter iter;
464 /* Remove from treeview */
465 view = GTK_TREE_VIEW (window->treeview);
466 selection = gtk_tree_view_get_selection (view);
468 if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
469 return;
472 gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
473 gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
475 /* Remove from config */
476 empathy_chatroom_manager_remove (window->manager, chatroom);
478 g_object_unref (chatroom);
481 static void
482 chatrooms_window_button_edit_clicked_cb (GtkWidget *widget,
483 EmpathyChatroomsWindow *window)
485 /* FIXME: This is still not implemented since Gossip fork */
488 static void
489 chatrooms_window_button_close_clicked_cb (GtkWidget *widget,
490 EmpathyChatroomsWindow *window)
492 gtk_widget_destroy (window->window);
495 static void
496 chatrooms_window_chatroom_added_cb (EmpathyChatroomManager *manager,
497 EmpathyChatroom *chatroom,
498 EmpathyChatroomsWindow *window)
500 EmpathyAccountChooser *account_chooser;
501 McAccount *account;
503 account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
504 account = empathy_account_chooser_get_account (account_chooser);
506 if (!account) {
507 chatrooms_window_model_add (window, chatroom, FALSE);
508 } else {
509 if (empathy_account_equal (account, empathy_chatroom_get_account (chatroom))) {
510 chatrooms_window_model_add (window, chatroom, FALSE);
513 g_object_unref (account);
517 static void
518 chatrooms_window_chatroom_removed_cb (EmpathyChatroomManager *manager,
519 EmpathyChatroom *chatroom,
520 EmpathyChatroomsWindow *window)
522 GtkTreeModel *model;
524 model = gtk_tree_view_get_model (GTK_TREE_VIEW (window->treeview));
526 gtk_tree_model_foreach (model,
527 (GtkTreeModelForeachFunc) chatrooms_window_remove_chatroom_foreach,
528 chatroom);
531 static gboolean
532 chatrooms_window_remove_chatroom_foreach (GtkTreeModel *model,
533 GtkTreePath *path,
534 GtkTreeIter *iter,
535 EmpathyChatroom *chatroom)
537 EmpathyChatroom *this_chatroom;
539 gtk_tree_model_get (model, iter, COL_POINTER, &this_chatroom, -1);
541 if (empathy_chatroom_equal (chatroom, this_chatroom)) {
542 gtk_list_store_remove (GTK_LIST_STORE (model), iter);
543 g_object_unref (this_chatroom);
544 return TRUE;
547 g_object_unref (this_chatroom);
549 return FALSE;
552 static void
553 chatrooms_window_account_changed_cb (GtkWidget *combo_box,
554 EmpathyChatroomsWindow *window)
556 chatrooms_window_model_refresh_data (window, FALSE);