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