]> git.0d.be Git - empathy.git/blob - src/empathy-chatrooms-window.c
Move should_create_salut_account to local-xmpp-assistant-widget
[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_close;
54 } EmpathyChatroomsWindow;
55
56 static void             chatrooms_window_destroy_cb                      (GtkWidget             *widget,
57                                                                           EmpathyChatroomsWindow *window);
58 static void             chatrooms_window_model_setup                     (EmpathyChatroomsWindow *window);
59 static void             chatrooms_window_model_add_columns               (EmpathyChatroomsWindow *window);
60 static void             chatrooms_window_model_refresh_data              (EmpathyChatroomsWindow *window,
61                                                                           gboolean               first_time);
62 static void             chatrooms_window_model_add                       (EmpathyChatroomsWindow *window,
63                                                                           EmpathyChatroom        *chatroom,
64                                                                           gboolean               set_active);
65 static void             chatrooms_window_model_cell_auto_connect_toggled (GtkCellRendererToggle  *cell,
66                                                                           gchar                  *path_string,
67                                                                           EmpathyChatroomsWindow  *window);
68 static void             chatrooms_window_button_remove_clicked_cb        (GtkWidget             *widget,
69                                                                           EmpathyChatroomsWindow *window);
70 static void             chatrooms_window_button_close_clicked_cb         (GtkWidget             *widget,
71                                                                           EmpathyChatroomsWindow *window);
72 static void             chatrooms_window_chatroom_added_cb               (EmpathyChatroomManager *manager,
73                                                                           EmpathyChatroom        *chatroom,
74                                                                           EmpathyChatroomsWindow *window);
75 static void             chatrooms_window_chatroom_removed_cb             (EmpathyChatroomManager *manager,
76                                                                           EmpathyChatroom        *chatroom,
77                                                                           EmpathyChatroomsWindow *window);
78 static gboolean         chatrooms_window_remove_chatroom_foreach         (GtkTreeModel          *model,
79                                                                           GtkTreePath           *path,
80                                                                           GtkTreeIter           *iter,
81                                                                           EmpathyChatroom        *chatroom);
82 static void             chatrooms_window_account_changed_cb              (GtkWidget             *combo_box,
83                                                                           EmpathyChatroomsWindow *window);
84
85 enum {
86         COL_IMAGE,
87         COL_NAME,
88         COL_ROOM,
89         COL_AUTO_CONNECT,
90         COL_POINTER,
91         COL_COUNT
92 };
93
94 void
95 empathy_chatrooms_window_show (GtkWindow *parent)
96 {
97         static EmpathyChatroomsWindow *window = NULL;
98         GtkBuilder                    *gui;
99         gchar                         *filename;
100         GtkWidget                     *sw, *toolbar;
101         GtkStyleContext               *context;
102
103         if (window) {
104                 gtk_window_present (GTK_WINDOW (window->window));
105                 return;
106         }
107
108         window = g_new0 (EmpathyChatroomsWindow, 1);
109
110         filename = empathy_file_lookup ("empathy-chatrooms-window.ui", "src");
111         gui = empathy_builder_get_file (filename,
112                                        "chatrooms_window", &window->window,
113                                        "hbox_account", &window->hbox_account,
114                                        "label_account", &window->label_account,
115                                        "sw_room_list", &sw,
116                                        "treeview", &window->treeview,
117                                        "toolbar_remove", &toolbar,
118                                        "button_remove", &window->button_remove,
119                                        "button_close", &window->button_close,
120                                        NULL);
121         g_free (filename);
122
123         /* join the remove toolbar to the treeview */
124         context = gtk_widget_get_style_context (sw);
125         gtk_style_context_set_junction_sides (context, GTK_JUNCTION_BOTTOM);
126         context = gtk_widget_get_style_context (toolbar);
127         gtk_style_context_set_junction_sides (context, GTK_JUNCTION_TOP);
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_close", "clicked", chatrooms_window_button_close_clicked_cb,
133                               NULL);
134
135         g_object_unref (gui);
136
137         g_object_add_weak_pointer (G_OBJECT (window->window), (gpointer) &window);
138
139         /* Get the session and chat room manager */
140         window->manager = empathy_chatroom_manager_dup_singleton (NULL);
141
142         g_signal_connect (window->manager, "chatroom-added",
143                           G_CALLBACK (chatrooms_window_chatroom_added_cb),
144                           window);
145         g_signal_connect (window->manager, "chatroom-removed",
146                           G_CALLBACK (chatrooms_window_chatroom_removed_cb),
147                           window);
148
149         /* Account chooser for chat rooms */
150         window->account_chooser = empathy_account_chooser_new ();
151         empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (window->account_chooser),
152                                             empathy_account_chooser_filter_supports_chatrooms,
153                                             NULL);
154         g_object_set (window->account_chooser,
155                       "has-all-option", TRUE,
156                       NULL);
157         empathy_account_chooser_set_account (EMPATHY_ACCOUNT_CHOOSER (window->account_chooser), NULL);
158
159         gtk_box_pack_start (GTK_BOX (window->hbox_account),
160                             window->account_chooser,
161                             TRUE, TRUE, 0);
162
163         g_signal_connect (window->account_chooser, "changed",
164                           G_CALLBACK (chatrooms_window_account_changed_cb),
165                           window);
166
167         gtk_widget_show (window->account_chooser);
168
169         /* Set up chatrooms */
170         chatrooms_window_model_setup (window);
171
172         /* Set focus */
173         gtk_widget_grab_focus (window->treeview);
174
175         /* Last touches */
176         if (parent) {
177                 gtk_window_set_transient_for (GTK_WINDOW (window->window),
178                                               GTK_WINDOW (parent));
179         }
180
181         gtk_widget_show (window->window);
182 }
183
184 static void
185 chatrooms_window_destroy_cb (GtkWidget             *widget,
186                              EmpathyChatroomsWindow *window)
187 {
188         g_signal_handlers_disconnect_by_func (window->manager,
189                                               chatrooms_window_chatroom_added_cb,
190                                               window);
191         g_signal_handlers_disconnect_by_func (window->manager,
192                                               chatrooms_window_chatroom_removed_cb,
193                                               window);
194         g_object_unref (window->manager);
195         g_free (window);
196 }
197
198 static void
199 chatrooms_window_model_setup (EmpathyChatroomsWindow *window)
200 {
201         GtkTreeView      *view;
202         GtkListStore     *store;
203         GtkTreeSelection *selection;
204
205         /* View */
206         view = GTK_TREE_VIEW (window->treeview);
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                                     EMPATHY_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 (EmpathyChatroomsWindow *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
275         /* Chatroom auto connect */
276         cell = gtk_cell_renderer_toggle_new ();
277         column = gtk_tree_view_column_new_with_attributes (_("Auto-Connect"), cell,
278                                                            "active", COL_AUTO_CONNECT,
279                                                            NULL);
280         count = gtk_tree_view_append_column (view, column);
281         gtk_tree_view_column_set_sort_column_id (column, count - 1);
282
283         g_signal_connect (cell, "toggled",
284                           G_CALLBACK (chatrooms_window_model_cell_auto_connect_toggled),
285                           window);
286
287         /* Sort model */
288         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model), 0,
289                                               GTK_SORT_ASCENDING);
290 }
291
292 static void
293 chatrooms_window_model_refresh_data (EmpathyChatroomsWindow *window,
294                                      gboolean               first_time)
295 {
296         GtkTreeView           *view;
297         GtkTreeSelection      *selection;
298         GtkTreeModel          *model;
299         GtkListStore          *store;
300         GtkTreeIter            iter;
301         EmpathyAccountChooser  *account_chooser;
302         TpAccount             *account;
303         GList                 *chatrooms, *l;
304
305         view = GTK_TREE_VIEW (window->treeview);
306         selection = gtk_tree_view_get_selection (view);
307         model = gtk_tree_view_get_model (view);
308         store = GTK_LIST_STORE (model);
309
310         /* Look up chatrooms */
311         account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
312         account = empathy_account_chooser_dup_account (account_chooser);
313
314         chatrooms = empathy_chatroom_manager_get_chatrooms (window->manager, account);
315
316         /* Clean out the store */
317         gtk_list_store_clear (store);
318
319         /* Populate with chatroom list. */
320         for (l = chatrooms; l; l = l->next) {
321                 chatrooms_window_model_add (window, l->data,  FALSE);
322         }
323
324         if (gtk_tree_model_get_iter_first (model, &iter)) {
325                 gtk_tree_selection_select_iter (selection, &iter);
326         }
327
328         if (account) {
329                 g_object_unref (account);
330         }
331
332         g_list_free (chatrooms);
333 }
334
335 static void
336 chatrooms_window_model_add (EmpathyChatroomsWindow *window,
337                             EmpathyChatroom        *chatroom,
338                             gboolean               set_active)
339 {
340         GtkTreeView      *view;
341         GtkTreeModel     *model;
342         GtkTreeSelection *selection;
343         GtkListStore     *store;
344         GtkTreeIter       iter;
345
346         view = GTK_TREE_VIEW (window->treeview);
347         selection = gtk_tree_view_get_selection (view);
348         model = gtk_tree_view_get_model (view);
349         store = GTK_LIST_STORE (model);
350
351         gtk_list_store_append (store, &iter);
352         gtk_list_store_set (store, &iter,
353                             COL_NAME, empathy_chatroom_get_name (chatroom),
354                             COL_ROOM, empathy_chatroom_get_room (chatroom),
355                             COL_AUTO_CONNECT, empathy_chatroom_get_auto_connect (chatroom),
356                             COL_POINTER, chatroom,
357                             -1);
358
359         if (set_active) {
360                 gtk_tree_selection_select_iter (selection, &iter);
361         }
362 }
363
364 static void
365 chatrooms_window_model_cell_auto_connect_toggled (GtkCellRendererToggle  *cell,
366                                                   gchar                  *path_string,
367                                                   EmpathyChatroomsWindow  *window)
368 {
369         EmpathyChatroom *chatroom;
370         gboolean        enabled;
371         GtkTreeView    *view;
372         GtkTreeModel   *model;
373         GtkListStore   *store;
374         GtkTreePath    *path;
375         GtkTreeIter     iter;
376
377         view = GTK_TREE_VIEW (window->treeview);
378         model = gtk_tree_view_get_model (view);
379         store = GTK_LIST_STORE (model);
380
381         path = gtk_tree_path_new_from_string (path_string);
382
383         gtk_tree_model_get_iter (model, &iter, path);
384         gtk_tree_model_get (model, &iter,
385                             COL_AUTO_CONNECT, &enabled,
386                             COL_POINTER, &chatroom,
387                             -1);
388
389         enabled = !enabled;
390
391         empathy_chatroom_set_auto_connect (chatroom, enabled);
392
393         gtk_list_store_set (store, &iter, COL_AUTO_CONNECT, enabled, -1);
394         gtk_tree_path_free (path);
395         g_object_unref (chatroom);
396 }
397
398 static void
399 chatrooms_window_button_remove_clicked_cb (GtkWidget             *widget,
400                                            EmpathyChatroomsWindow *window)
401 {
402         EmpathyChatroom        *chatroom;
403         GtkTreeView           *view;
404         GtkTreeModel          *model;
405         GtkTreeSelection      *selection;
406         GtkTreeIter            iter;
407
408         /* Remove from treeview */
409         view = GTK_TREE_VIEW (window->treeview);
410         selection = gtk_tree_view_get_selection (view);
411
412         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
413                 return;
414         }
415
416         gtk_tree_model_get (model, &iter, COL_POINTER, &chatroom, -1);
417         gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
418
419         /* Remove from config */
420         empathy_chatroom_manager_remove (window->manager, chatroom);
421
422         g_object_unref (chatroom);
423 }
424
425 static void
426 chatrooms_window_button_close_clicked_cb (GtkWidget             *widget,
427                                           EmpathyChatroomsWindow *window)
428 {
429         gtk_widget_destroy (window->window);
430 }
431
432 static void
433 chatrooms_window_chatroom_added_cb (EmpathyChatroomManager *manager,
434                                     EmpathyChatroom        *chatroom,
435                                     EmpathyChatroomsWindow *window)
436 {
437         EmpathyAccountChooser *account_chooser;
438         TpAccount             *account;
439
440         account_chooser = EMPATHY_ACCOUNT_CHOOSER (window->account_chooser);
441         account = empathy_account_chooser_dup_account (account_chooser);
442
443         if (!account) {
444                 chatrooms_window_model_add (window, chatroom, FALSE);
445         } else {
446                 if (account == empathy_chatroom_get_account (chatroom)) {
447                         chatrooms_window_model_add (window, chatroom, FALSE);
448                 }
449
450                 g_object_unref (account);
451         }
452 }
453
454 static void
455 chatrooms_window_chatroom_removed_cb (EmpathyChatroomManager *manager,
456                                       EmpathyChatroom        *chatroom,
457                                       EmpathyChatroomsWindow *window)
458 {
459         GtkTreeModel *model;
460
461         model = gtk_tree_view_get_model (GTK_TREE_VIEW (window->treeview));
462
463         gtk_tree_model_foreach (model,
464                                 (GtkTreeModelForeachFunc) chatrooms_window_remove_chatroom_foreach,
465                                 chatroom);
466 }
467
468 static gboolean
469 chatrooms_window_remove_chatroom_foreach (GtkTreeModel   *model,
470                                           GtkTreePath    *path,
471                                           GtkTreeIter    *iter,
472                                           EmpathyChatroom *chatroom)
473 {
474         EmpathyChatroom *this_chatroom;
475
476         gtk_tree_model_get (model, iter, COL_POINTER, &this_chatroom, -1);
477
478         if (empathy_chatroom_equal (chatroom, this_chatroom)) {
479                 gtk_list_store_remove (GTK_LIST_STORE (model), iter);
480                 g_object_unref (this_chatroom);
481                 return TRUE;
482         }
483
484         g_object_unref (this_chatroom);
485
486         return FALSE;
487 }
488
489 static void
490 chatrooms_window_account_changed_cb (GtkWidget             *combo_box,
491                                      EmpathyChatroomsWindow *window)
492 {
493         chatrooms_window_model_refresh_data (window, FALSE);
494 }
495