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