]> git.0d.be Git - empathy.git/blob - src/empathy-chatrooms-window.c
83f6b99d7756bbb3113b12315a4cffb93dd8838b
[empathy.git] / src / empathy-chatrooms-window.c
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.
5  *
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.
10  *
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.
15  *
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., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  USA
20  *
21  * Authors: Xavier Claessens <xclaesse@gmail.com>
22  *          Martyn Russell <martyn@imendio.com>
23  *          Mikael Hallendal <micke@imendio.com>
24  */
25
26 #include "config.h"
27
28 #include <glib/gi18n.h>
29
30 #include "libempathy/empathy-chatroom-manager.h"
31 #include "libempathy/empathy-utils.h"
32
33 #include "libempathy-gtk/empathy-account-chooser.h"
34 #include "libempathy-gtk/empathy-ui-utils.h"
35
36 #include "empathy-chatrooms-window.h"
37
38 typedef struct {
39         EmpathyChatroomManager *manager;
40
41         GtkWidget             *window;
42         GtkWidget             *hbox_account;
43         GtkWidget             *label_account;
44         GtkWidget             *account_chooser;
45         GtkWidget             *treeview;
46         GtkWidget             *button_remove;
47         GtkWidget             *button_close;
48 } EmpathyChatroomsWindow;
49
50 static void             chatrooms_window_destroy_cb                      (GtkWidget             *widget,
51                                                                           EmpathyChatroomsWindow *window);
52 static void             chatrooms_window_model_setup                     (EmpathyChatroomsWindow *window);
53 static void             chatrooms_window_model_add_columns               (EmpathyChatroomsWindow *window);
54 static void             chatrooms_window_model_refresh_data              (EmpathyChatroomsWindow *window,
55                                                                           gboolean               first_time);
56 static void             chatrooms_window_model_add                       (EmpathyChatroomsWindow *window,
57                                                                           EmpathyChatroom        *chatroom,
58                                                                           gboolean               set_active);
59 static void             chatrooms_window_model_cell_auto_connect_toggled (GtkCellRendererToggle  *cell,
60                                                                           gchar                  *path_string,
61                                                                           EmpathyChatroomsWindow  *window);
62 static void             chatrooms_window_button_remove_clicked_cb        (GtkWidget             *widget,
63                                                                           EmpathyChatroomsWindow *window);
64 static void             chatrooms_window_button_close_clicked_cb         (GtkWidget             *widget,
65                                                                           EmpathyChatroomsWindow *window);
66 static void             chatrooms_window_chatroom_added_cb               (EmpathyChatroomManager *manager,
67                                                                           EmpathyChatroom        *chatroom,
68                                                                           EmpathyChatroomsWindow *window);
69 static void             chatrooms_window_chatroom_removed_cb             (EmpathyChatroomManager *manager,
70                                                                           EmpathyChatroom        *chatroom,
71                                                                           EmpathyChatroomsWindow *window);
72 static gboolean         chatrooms_window_remove_chatroom_foreach         (GtkTreeModel          *model,
73                                                                           GtkTreePath           *path,
74                                                                           GtkTreeIter           *iter,
75                                                                           EmpathyChatroom        *chatroom);
76 static void             chatrooms_window_account_changed_cb              (GtkWidget             *combo_box,
77                                                                           EmpathyChatroomsWindow *window);
78
79 enum {
80         COL_IMAGE,
81         COL_NAME,
82         COL_ROOM,
83         COL_AUTO_CONNECT,
84         COL_POINTER,
85         COL_COUNT
86 };
87
88 void
89 empathy_chatrooms_window_show (GtkWindow *parent)
90 {
91         static EmpathyChatroomsWindow *window = NULL;
92         GtkBuilder                    *gui;
93         gchar                         *filename;
94         GtkWidget                     *sw, *toolbar;
95         GtkStyleContext               *context;
96
97         if (window) {
98                 gtk_window_present (GTK_WINDOW (window->window));
99                 return;
100         }
101
102         window = g_new0 (EmpathyChatroomsWindow, 1);
103
104         filename = empathy_file_lookup ("empathy-chatrooms-window.ui", "src");
105         gui = empathy_builder_get_file (filename,
106                                        "chatrooms_window", &window->window,
107                                        "hbox_account", &window->hbox_account,
108                                        "label_account", &window->label_account,
109                                        "sw_room_list", &sw,
110                                        "treeview", &window->treeview,
111                                        "toolbar_remove", &toolbar,
112                                        "button_remove", &window->button_remove,
113                                        "button_close", &window->button_close,
114                                        NULL);
115         g_free (filename);
116
117         /* join the remove toolbar to the treeview */
118         context = gtk_widget_get_style_context (sw);
119         gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
120         context = gtk_widget_get_style_context (toolbar);
121         gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
122
123         empathy_builder_connect (gui, window,
124                               "chatrooms_window", "destroy", chatrooms_window_destroy_cb,
125                               "button_remove", "clicked", chatrooms_window_button_remove_clicked_cb,
126                               "button_close", "clicked", chatrooms_window_button_close_clicked_cb,
127                               NULL);
128
129         g_object_unref (gui);
130
131         g_object_add_weak_pointer (G_OBJECT (window->window), (gpointer) &window);
132
133         /* Get the session and chat room manager */
134         window->manager = empathy_chatroom_manager_dup_singleton (NULL);
135
136         g_signal_connect (window->manager, "chatroom-added",
137                           G_CALLBACK (chatrooms_window_chatroom_added_cb),
138                           window);
139         g_signal_connect (window->manager, "chatroom-removed",
140                           G_CALLBACK (chatrooms_window_chatroom_removed_cb),
141                           window);
142
143         /* Account chooser for chat rooms */
144         window->account_chooser = empathy_account_chooser_new ();
145         empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (window->account_chooser),
146                                             empathy_account_chooser_filter_supports_chatrooms,
147                                             NULL);
148         g_object_set (window->account_chooser,
149                       "has-all-option", TRUE,
150                       NULL);
151         empathy_account_chooser_set_account (EMPATHY_ACCOUNT_CHOOSER (window->account_chooser), NULL);
152
153         gtk_box_pack_start (GTK_BOX (window->hbox_account),
154                             window->account_chooser,
155                             TRUE, TRUE, 0);
156
157         g_signal_connect (window->account_chooser, "changed",
158                           G_CALLBACK (chatrooms_window_account_changed_cb),
159                           window);
160
161         gtk_widget_show (window->account_chooser);
162
163         /* Set up chatrooms */
164         chatrooms_window_model_setup (window);
165
166         /* Set focus */
167         gtk_widget_grab_focus (window->treeview);
168
169         /* Last touches */
170         if (parent) {
171                 gtk_window_set_transient_for (GTK_WINDOW (window->window),
172                                               GTK_WINDOW (parent));
173         }
174
175         gtk_widget_show (window->window);
176 }
177
178 static void
179 chatrooms_window_destroy_cb (GtkWidget             *widget,
180                              EmpathyChatroomsWindow *window)
181 {
182         g_signal_handlers_disconnect_by_func (window->manager,
183                                               chatrooms_window_chatroom_added_cb,
184                                               window);
185         g_signal_handlers_disconnect_by_func (window->manager,
186                                               chatrooms_window_chatroom_removed_cb,
187                                               window);
188         g_object_unref (window->manager);
189         g_free (window);
190 }
191
192 static void
193 chatrooms_window_model_setup (EmpathyChatroomsWindow *window)
194 {
195         GtkTreeView      *view;
196         GtkListStore     *store;
197         GtkTreeSelection *selection;
198
199         /* View */
200         view = GTK_TREE_VIEW (window->treeview);
201
202         /* Store */
203         store = gtk_list_store_new (COL_COUNT,
204                                     G_TYPE_STRING,         /* Image */
205                                     G_TYPE_STRING,         /* Name */
206                                     G_TYPE_STRING,         /* Room */
207                                     G_TYPE_BOOLEAN,        /* Auto start */
208                                     EMPATHY_TYPE_CHATROOM); /* Chatroom */
209
210         gtk_tree_view_set_model (view, GTK_TREE_MODEL (store));
211
212         /* Selection */
213         selection = gtk_tree_view_get_selection (view);
214         gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
215
216         /* Columns */
217         chatrooms_window_model_add_columns (window);
218
219         /* Add data */
220         chatrooms_window_model_refresh_data (window, TRUE);
221
222         /* Clean up */
223         g_object_unref (store);
224 }
225
226 static void
227 chatrooms_window_model_add_columns (EmpathyChatroomsWindow *window)
228 {
229         GtkTreeView       *view;
230         GtkTreeModel      *model;
231         GtkTreeViewColumn *column;
232         GtkCellRenderer   *cell;
233         gint               count;
234
235         view = GTK_TREE_VIEW (window->treeview);
236         model = gtk_tree_view_get_model (view);
237
238         gtk_tree_view_set_headers_visible (view, TRUE);
239         gtk_tree_view_set_headers_clickable (view, TRUE);
240
241         /* Name & Status */
242         column = gtk_tree_view_column_new ();
243         count = gtk_tree_view_append_column (view, column);
244
245         gtk_tree_view_column_set_title (column, _("Name"));
246         gtk_tree_view_column_set_expand (column, TRUE);
247         gtk_tree_view_column_set_sort_column_id (column, count - 1);
248
249         cell = gtk_cell_renderer_pixbuf_new ();
250         gtk_tree_view_column_pack_start (column, cell, FALSE);
251         gtk_tree_view_column_add_attribute (column, cell, "icon-name", COL_IMAGE);
252
253         cell = gtk_cell_renderer_text_new ();
254         g_object_set (cell,
255                       "xpad", 4,
256                       "ypad", 1,
257                       NULL);
258         gtk_tree_view_column_pack_start (column, cell, TRUE);
259         gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
260
261         /* Room */
262         cell = gtk_cell_renderer_text_new ();
263         column = gtk_tree_view_column_new_with_attributes (_("Room"), cell,
264                                                            "text", COL_ROOM,
265                                                            NULL);
266         count = gtk_tree_view_append_column (view, column);
267         gtk_tree_view_column_set_sort_column_id (column, count - 1);
268
269         /* Chatroom auto connect */
270         cell = gtk_cell_renderer_toggle_new ();
271         column = gtk_tree_view_column_new_with_attributes (_("Auto-Connect"), cell,
272                                                            "active", COL_AUTO_CONNECT,
273                                                            NULL);
274         count = gtk_tree_view_append_column (view, column);
275         gtk_tree_view_column_set_sort_column_id (column, count - 1);
276
277         g_signal_connect (cell, "toggled",
278                           G_CALLBACK (chatrooms_window_model_cell_auto_connect_toggled),
279                           window);
280
281         /* Sort model */
282         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model), 0,
283                                               GTK_SORT_ASCENDING);
284 }
285
286 static void
287 chatrooms_window_model_refresh_data (EmpathyChatroomsWindow *window,
288                                      gboolean               first_time)
289 {
290         GtkTreeView           *view;
291         GtkTreeSelection      *selection;
292         GtkTreeModel          *model;
293         GtkListStore          *store;
294         GtkTreeIter            iter;
295         EmpathyAccountChooser  *account_chooser;
296         TpAccount             *account;
297         GList                 *chatrooms, *l;
298
299         view = GTK_TREE_VIEW (window->treeview);
300         selection = gtk_tree_view_get_selection (view);
301         model = gtk_tree_view_get_model (view);
302         store = GTK_LIST_STORE (model);
303
304         /* Look up chatrooms */
305         account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
306         account = empathy_account_chooser_dup_account (account_chooser);
307
308         chatrooms = empathy_chatroom_manager_get_chatrooms (window->manager, account);
309
310         /* Clean out the store */
311         gtk_list_store_clear (store);
312
313         /* Populate with chatroom list. */
314         for (l = chatrooms; l; l = l->next) {
315                 chatrooms_window_model_add (window, l->data,  FALSE);
316         }
317
318         if (gtk_tree_model_get_iter_first (model, &iter)) {
319                 gtk_tree_selection_select_iter (selection, &iter);
320         }
321
322         if (account) {
323                 g_object_unref (account);
324         }
325
326         g_list_free (chatrooms);
327 }
328
329 static void
330 chatrooms_window_model_add (EmpathyChatroomsWindow *window,
331                             EmpathyChatroom        *chatroom,
332                             gboolean               set_active)
333 {
334         GtkTreeView      *view;
335         GtkTreeModel     *model;
336         GtkTreeSelection *selection;
337         GtkListStore     *store;
338         GtkTreeIter       iter;
339
340         view = GTK_TREE_VIEW (window->treeview);
341         selection = gtk_tree_view_get_selection (view);
342         model = gtk_tree_view_get_model (view);
343         store = GTK_LIST_STORE (model);
344
345         gtk_list_store_insert_with_values (store, &iter, -1,
346                             COL_NAME, empathy_chatroom_get_name (chatroom),
347                             COL_ROOM, empathy_chatroom_get_room (chatroom),
348                             COL_AUTO_CONNECT, empathy_chatroom_get_auto_connect (chatroom),
349                             COL_POINTER, chatroom,
350                             -1);
351
352         if (set_active) {
353                 gtk_tree_selection_select_iter (selection, &iter);
354         }
355 }
356
357 static void
358 chatrooms_window_model_cell_auto_connect_toggled (GtkCellRendererToggle  *cell,
359                                                   gchar                  *path_string,
360                                                   EmpathyChatroomsWindow  *window)
361 {
362         EmpathyChatroom *chatroom;
363         gboolean        enabled;
364         GtkTreeView    *view;
365         GtkTreeModel   *model;
366         GtkListStore   *store;
367         GtkTreePath    *path;
368         GtkTreeIter     iter;
369
370         view = GTK_TREE_VIEW (window->treeview);
371         model = gtk_tree_view_get_model (view);
372         store = GTK_LIST_STORE (model);
373
374         path = gtk_tree_path_new_from_string (path_string);
375
376         gtk_tree_model_get_iter (model, &iter, path);
377         gtk_tree_model_get (model, &iter,
378                             COL_AUTO_CONNECT, &enabled,
379                             COL_POINTER, &chatroom,
380                             -1);
381
382         enabled = !enabled;
383
384         empathy_chatroom_set_auto_connect (chatroom, enabled);
385
386         gtk_list_store_set (store, &iter, COL_AUTO_CONNECT, enabled, -1);
387         gtk_tree_path_free (path);
388         g_object_unref (chatroom);
389 }
390
391 static void
392 chatrooms_window_button_remove_clicked_cb (GtkWidget             *widget,
393                                            EmpathyChatroomsWindow *window)
394 {
395         EmpathyChatroom        *chatroom;
396         GtkTreeView           *view;
397         GtkTreeModel          *model;
398         GtkTreeSelection      *selection;
399         GtkTreeIter            iter;
400
401         /* Remove from treeview */
402         view = GTK_TREE_VIEW (window->treeview);
403         selection = gtk_tree_view_get_selection (view);
404
405         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
406                 return;
407         }
408
409         gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
410         gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
411
412         /* Remove from config */
413         empathy_chatroom_manager_remove (window->manager, chatroom);
414
415         g_object_unref (chatroom);
416 }
417
418 static void
419 chatrooms_window_button_close_clicked_cb (GtkWidget             *widget,
420                                           EmpathyChatroomsWindow *window)
421 {
422         gtk_widget_destroy (window->window);
423 }
424
425 static void
426 chatrooms_window_chatroom_added_cb (EmpathyChatroomManager *manager,
427                                     EmpathyChatroom        *chatroom,
428                                     EmpathyChatroomsWindow *window)
429 {
430         EmpathyAccountChooser *account_chooser;
431         TpAccount             *account;
432
433         account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
434         account = empathy_account_chooser_dup_account (account_chooser);
435
436         if (!account) {
437                 chatrooms_window_model_add (window, chatroom, FALSE);
438         } else {
439                 if (account == empathy_chatroom_get_account (chatroom)) {
440                         chatrooms_window_model_add (window, chatroom, FALSE);
441                 }
442
443                 g_object_unref (account);
444         }
445 }
446
447 static void
448 chatrooms_window_chatroom_removed_cb (EmpathyChatroomManager *manager,
449                                       EmpathyChatroom        *chatroom,
450                                       EmpathyChatroomsWindow *window)
451 {
452         GtkTreeModel *model;
453
454         model = gtk_tree_view_get_model (GTK_TREE_VIEW (window->treeview));
455
456         gtk_tree_model_foreach (model,
457                                 (GtkTreeModelForeachFunc) chatrooms_window_remove_chatroom_foreach,
458                                 chatroom);
459 }
460
461 static gboolean
462 chatrooms_window_remove_chatroom_foreach (GtkTreeModel   *model,
463                                           GtkTreePath    *path,
464                                           GtkTreeIter    *iter,
465                                           EmpathyChatroom *chatroom)
466 {
467         EmpathyChatroom *this_chatroom;
468
469         gtk_tree_model_get (model, iter, COL_POINTER, &this_chatroom, -1);
470
471         if (empathy_chatroom_equal (chatroom, this_chatroom)) {
472                 gtk_list_store_remove (GTK_LIST_STORE (model), iter);
473                 g_object_unref (this_chatroom);
474                 return TRUE;
475         }
476
477         g_object_unref (this_chatroom);
478
479         return FALSE;
480 }
481
482 static void
483 chatrooms_window_account_changed_cb (GtkWidget             *combo_box,
484                                      EmpathyChatroomsWindow *window)
485 {
486         chatrooms_window_model_refresh_data (window, FALSE);
487 }
488