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