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