]> git.0d.be Git - empathy.git/blob - src/empathy-new-chatroom-dialog.c
Use the channel wrapper to log chats instead of connecting to the signals directly.
[empathy.git] / src / empathy-new-chatroom-dialog.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2006-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., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  * 
21  * Authors: Martyn Russell <martyn@imendio.com>
22  *          Xavier Claessens <xclaesse@gmail.com>
23  */
24
25 #include <config.h>
26
27 #include <string.h>
28 #include <stdio.h>
29
30 #include <gtk/gtk.h>
31 #include <glade/glade.h>
32 #include <glib.h>
33 #include <glib/gi18n.h>
34
35 #include <libmissioncontrol/mission-control.h>
36 #include <libmissioncontrol/mc-account.h>
37 #include <libmissioncontrol/mc-profile.h>
38
39 #include <libempathy/empathy-tp-roomlist.h>
40 #include <libempathy/empathy-chatroom.h>
41 #include <libempathy/empathy-utils.h>
42 #include <libempathy/empathy-dispatcher.h>
43
44 #include <libempathy-gtk/empathy-account-chooser.h>
45 #include <libempathy-gtk/empathy-ui-utils.h>
46
47 #include "empathy-new-chatroom-dialog.h"
48 #include "ephy-spinner.h"
49
50 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
51 #include <libempathy/empathy-debug.h>
52
53 typedef struct {
54         EmpathyTpRoomlist *room_list;
55
56         GtkWidget         *window;
57         GtkWidget         *vbox_widgets;
58         GtkWidget         *table_info;
59         GtkWidget         *label_account;
60         GtkWidget         *account_chooser;
61         GtkWidget         *label_server;
62         GtkWidget         *entry_server;
63         GtkWidget         *togglebutton_refresh;
64         GtkWidget         *label_room;
65         GtkWidget         *entry_room;
66         GtkWidget         *vbox_browse;
67         GtkWidget         *image_status;
68         GtkWidget         *label_status;
69         GtkWidget         *hbox_status;
70         GtkWidget         *throbber;
71         GtkWidget         *treeview;
72         GtkTreeModel      *model;
73         GtkWidget         *button_join;
74         GtkWidget         *button_close;
75 } EmpathyNewChatroomDialog;
76
77 enum {
78         COL_NAME,
79         COL_ROOM,
80         COL_COUNT
81 };
82
83 static void     new_chatroom_dialog_response_cb                     (GtkWidget               *widget,
84                                                                      gint                     response,
85                                                                      EmpathyNewChatroomDialog *dialog);
86 static void     new_chatroom_dialog_destroy_cb                      (GtkWidget               *widget,
87                                                                      EmpathyNewChatroomDialog *dialog);
88 static void     new_chatroom_dialog_model_setup                     (EmpathyNewChatroomDialog *dialog);
89 static void     new_chatroom_dialog_model_add_columns               (EmpathyNewChatroomDialog *dialog);
90 static void     new_chatroom_dialog_update_widgets                  (EmpathyNewChatroomDialog *dialog);
91 static void     new_chatroom_dialog_account_changed_cb              (GtkComboBox              *combobox,
92                                                                      EmpathyNewChatroomDialog *dialog);
93 static void     new_chatroom_dialog_roomlist_destroy_cb             (EmpathyTpRoomlist        *room_list,
94                                                                      EmpathyNewChatroomDialog *dialog);
95 static void     new_chatroom_dialog_new_room_cb                     (EmpathyTpRoomlist        *room_list,
96                                                                      EmpathyChatroom          *chatroom,
97                                                                      EmpathyNewChatroomDialog *dialog);
98 static void     new_chatroom_dialog_listing_cb                      (EmpathyTpRoomlist        *room_list,
99                                                                      gpointer                  unused,
100                                                                      EmpathyNewChatroomDialog *dialog);
101 static void     new_chatroom_dialog_model_clear                     (EmpathyNewChatroomDialog *dialog);
102 static void     new_chatroom_dialog_model_row_activated_cb          (GtkTreeView             *tree_view,
103                                                                      GtkTreePath             *path,
104                                                                      GtkTreeViewColumn       *column,
105                                                                      EmpathyNewChatroomDialog *dialog);
106 static void     new_chatroom_dialog_model_selection_changed         (GtkTreeSelection        *selection,
107                                                                      EmpathyNewChatroomDialog *dialog);
108 static void     new_chatroom_dialog_join                            (EmpathyNewChatroomDialog *dialog);
109 static void     new_chatroom_dialog_entry_changed_cb                (GtkWidget               *entry,
110                                                                      EmpathyNewChatroomDialog *dialog);
111 static void     new_chatroom_dialog_browse_start                    (EmpathyNewChatroomDialog *dialog);
112 static void     new_chatroom_dialog_browse_stop                     (EmpathyNewChatroomDialog *dialog);
113 static void     new_chatroom_dialog_entry_server_activate_cb        (GtkWidget               *widget,
114                                                                      EmpathyNewChatroomDialog *dialog);
115 static void     new_chatroom_dialog_togglebutton_refresh_toggled_cb (GtkWidget               *widget,
116                                                                      EmpathyNewChatroomDialog *dialog);
117
118 static EmpathyNewChatroomDialog *dialog_p = NULL;
119
120 void
121 empathy_new_chatroom_dialog_show (GtkWindow *parent)
122 {
123         EmpathyNewChatroomDialog *dialog;
124         GladeXML                 *glade;
125         GtkSizeGroup             *size_group;
126         gchar                    *filename;
127
128         if (dialog_p) {
129                 gtk_window_present (GTK_WINDOW (dialog_p->window));
130                 return;
131         }
132
133         dialog_p = dialog = g_new0 (EmpathyNewChatroomDialog, 1);
134
135         filename = empathy_file_lookup ("empathy-new-chatroom-dialog.glade", "src");
136         glade = empathy_glade_get_file (filename,
137                                        "new_chatroom_dialog",
138                                        NULL,
139                                        "new_chatroom_dialog", &dialog->window,
140                                        "table_info", &dialog->table_info,
141                                        "label_account", &dialog->label_account,
142                                        "label_server", &dialog->label_server,
143                                        "label_room", &dialog->label_room,
144                                        "entry_server", &dialog->entry_server,
145                                        "entry_room", &dialog->entry_room,
146                                        "togglebutton_refresh", &dialog->togglebutton_refresh,
147                                        "vbox_browse", &dialog->vbox_browse,
148                                        "image_status", &dialog->image_status,
149                                        "label_status", &dialog->label_status,
150                                        "hbox_status", &dialog->hbox_status,
151                                        "treeview", &dialog->treeview,
152                                        "button_join", &dialog->button_join,
153                                        NULL);
154         g_free (filename);
155
156         empathy_glade_connect (glade,
157                               dialog,
158                               "new_chatroom_dialog", "response", new_chatroom_dialog_response_cb,
159                               "new_chatroom_dialog", "destroy", new_chatroom_dialog_destroy_cb,
160                               "entry_server", "changed", new_chatroom_dialog_entry_changed_cb,
161                               "entry_server", "activate", new_chatroom_dialog_entry_server_activate_cb,
162                               "entry_room", "changed", new_chatroom_dialog_entry_changed_cb,
163                               "togglebutton_refresh", "toggled", new_chatroom_dialog_togglebutton_refresh_toggled_cb,
164                               NULL);
165
166         g_object_unref (glade);
167
168         g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog_p);
169
170         /* Label alignment */
171         size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
172
173         gtk_size_group_add_widget (size_group, dialog->label_account);
174         gtk_size_group_add_widget (size_group, dialog->label_server);
175         gtk_size_group_add_widget (size_group, dialog->label_room);
176
177         g_object_unref (size_group);
178
179         /* Set up chatrooms treeview */
180         new_chatroom_dialog_model_setup (dialog);
181
182         /* Add throbber */
183         dialog->throbber = ephy_spinner_new ();
184         ephy_spinner_set_size (EPHY_SPINNER (dialog->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
185         gtk_widget_show (dialog->throbber);
186
187         gtk_box_pack_start (GTK_BOX (dialog->hbox_status), dialog->throbber, 
188                             FALSE, FALSE, 0);
189
190         /* Account chooser for custom */
191         dialog->account_chooser = empathy_account_chooser_new ();
192         empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser),
193                                             empathy_account_chooser_filter_is_connected,
194                                             NULL);
195         gtk_table_attach_defaults (GTK_TABLE (dialog->table_info),
196                                    dialog->account_chooser,
197                                    1, 3, 0, 1);
198         gtk_widget_show (dialog->account_chooser);
199
200         g_signal_connect (GTK_COMBO_BOX (dialog->account_chooser), "changed",
201                           G_CALLBACK (new_chatroom_dialog_account_changed_cb),
202                           dialog);
203         new_chatroom_dialog_account_changed_cb (GTK_COMBO_BOX (dialog->account_chooser),
204                                                 dialog);
205
206         if (parent) {
207                 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
208                                               GTK_WINDOW (parent));
209         }
210
211         gtk_widget_show (dialog->window);
212 }
213
214 static void
215 new_chatroom_dialog_response_cb (GtkWidget               *widget,
216                                  gint                     response,
217                                  EmpathyNewChatroomDialog *dialog)
218 {
219         if (response == GTK_RESPONSE_OK) {
220                 new_chatroom_dialog_join (dialog);
221         }
222
223         gtk_widget_destroy (widget);
224 }
225
226 static void
227 new_chatroom_dialog_destroy_cb (GtkWidget               *widget,
228                                 EmpathyNewChatroomDialog *dialog)
229 {
230         if (dialog->room_list) {
231                 g_object_unref (dialog->room_list);
232         }
233         g_object_unref (dialog->model);  
234
235         g_free (dialog);
236 }
237
238 static void
239 new_chatroom_dialog_model_setup (EmpathyNewChatroomDialog *dialog)
240 {
241         GtkTreeView      *view;
242         GtkListStore     *store;
243         GtkTreeSelection *selection;
244
245         /* View */
246         view = GTK_TREE_VIEW (dialog->treeview);
247
248         g_signal_connect (view, "row-activated",
249                           G_CALLBACK (new_chatroom_dialog_model_row_activated_cb),
250                           dialog);
251
252         /* Store/Model */
253         store = gtk_list_store_new (COL_COUNT,
254                                     G_TYPE_STRING,       /* Image */
255                                     G_TYPE_STRING,       /* Text */
256                                     G_TYPE_STRING);      /* Room */
257
258         dialog->model = GTK_TREE_MODEL (store);
259         gtk_tree_view_set_model (view, dialog->model);
260
261         /* Selection */
262         selection = gtk_tree_view_get_selection (view);
263         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
264                                               COL_NAME, GTK_SORT_ASCENDING);
265
266         g_signal_connect (selection, "changed",
267                           G_CALLBACK (new_chatroom_dialog_model_selection_changed),
268                           dialog);
269
270         /* Columns */
271         new_chatroom_dialog_model_add_columns (dialog);
272 }
273
274 static void
275 new_chatroom_dialog_model_add_columns (EmpathyNewChatroomDialog *dialog)
276 {
277         GtkTreeView       *view;
278         GtkTreeViewColumn *column;
279         GtkCellRenderer   *cell;
280
281         view = GTK_TREE_VIEW (dialog->treeview);
282         gtk_tree_view_set_headers_visible (view, FALSE);
283
284         cell = gtk_cell_renderer_text_new ();
285         g_object_set (cell,
286                       "xpad", (guint) 4,
287                       "ypad", (guint) 1,
288                       "ellipsize", PANGO_ELLIPSIZE_END,
289                       NULL);
290
291         column = gtk_tree_view_column_new_with_attributes (_("Chat Rooms"),
292                                                            cell,
293                                                            "text", COL_NAME,
294                                                            NULL);
295
296         gtk_tree_view_column_set_expand (column, TRUE);
297         gtk_tree_view_append_column (view, column);
298 }
299
300 static void
301 new_chatroom_dialog_update_widgets (EmpathyNewChatroomDialog *dialog)
302 {
303         EmpathyAccountChooser *account_chooser;
304         McAccount             *account;
305         McProfile             *profile;
306         const gchar           *protocol;
307         const gchar           *room;
308         
309         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
310         account = empathy_account_chooser_get_account (account_chooser);
311         profile = mc_account_get_profile (account);
312         protocol = mc_profile_get_protocol_name (profile);
313
314         gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), "");
315
316         /* hardcode here known protocols */
317         if (strcmp (protocol, "jabber") == 0) {
318                 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
319                 gtk_widget_show (dialog->vbox_browse);
320
321         }
322         else if (strcmp (protocol, "local-xmpp") == 0) {
323                 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
324                 gtk_widget_show (dialog->vbox_browse);          
325         }
326         else if (strcmp (protocol, "irc") == 0) {
327                 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
328                 gtk_widget_show (dialog->vbox_browse);          
329         } else {
330                 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
331                 gtk_widget_show (dialog->vbox_browse);
332         }
333
334         room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
335         gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
336
337         /* Final set up of the dialog */
338         gtk_widget_grab_focus (dialog->entry_room);
339
340         g_object_unref (account);
341         g_object_unref (profile);
342 }
343
344 static void
345 new_chatroom_dialog_account_changed_cb (GtkComboBox             *combobox,
346                                         EmpathyNewChatroomDialog *dialog)
347 {
348         EmpathyAccountChooser *account_chooser;
349         McAccount             *account;
350         gboolean               listing = FALSE;
351
352         if (dialog->room_list) {
353                 g_object_unref (dialog->room_list);
354         }
355
356         ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
357         new_chatroom_dialog_model_clear (dialog);
358
359         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
360         account = empathy_account_chooser_get_account (account_chooser);
361         dialog->room_list = empathy_tp_roomlist_new (account);
362
363         if (dialog->room_list)  {
364                 g_signal_connect (dialog->room_list, "destroy",
365                                   G_CALLBACK (new_chatroom_dialog_roomlist_destroy_cb),
366                                   dialog);
367                 g_signal_connect (dialog->room_list, "new-room",
368                                   G_CALLBACK (new_chatroom_dialog_new_room_cb),
369                                   dialog);
370                 g_signal_connect (dialog->room_list, "notify::is-listing",
371                                   G_CALLBACK (new_chatroom_dialog_listing_cb),
372                                   dialog);
373
374                 listing = empathy_tp_roomlist_is_listing (dialog->room_list);
375                 if (listing) {
376                         ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
377                 }
378         }
379
380         new_chatroom_dialog_update_widgets (dialog);
381 }
382
383 static void
384 new_chatroom_dialog_roomlist_destroy_cb (EmpathyTpRoomlist        *room_list,
385                                          EmpathyNewChatroomDialog *dialog)
386 {
387         g_object_unref (dialog->room_list);
388         dialog->room_list = NULL;
389 }
390
391 static void
392 new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist        *room_list,
393                                  EmpathyChatroom          *chatroom,
394                                  EmpathyNewChatroomDialog *dialog)
395 {
396         GtkTreeView      *view;
397         GtkTreeSelection *selection;
398         GtkListStore     *store;
399         GtkTreeIter       iter;
400
401         DEBUG ("New chatroom listed: %s (%s)",
402                 empathy_chatroom_get_name (chatroom),
403                 empathy_chatroom_get_room (chatroom));
404
405         /* Add to model */
406         view = GTK_TREE_VIEW (dialog->treeview);
407         selection = gtk_tree_view_get_selection (view);
408         store = GTK_LIST_STORE (dialog->model);
409
410         gtk_list_store_append (store, &iter);
411         gtk_list_store_set (store, &iter,
412                             COL_NAME, empathy_chatroom_get_name (chatroom),
413                             COL_ROOM, empathy_chatroom_get_room (chatroom),
414                             -1);
415 }
416
417 static void
418 new_chatroom_dialog_listing_cb (EmpathyTpRoomlist        *room_list,
419                                 gpointer                  unused,
420                                 EmpathyNewChatroomDialog *dialog)
421 {
422         gboolean listing;
423
424         listing = empathy_tp_roomlist_is_listing (room_list);
425
426         /* Update the throbber */
427         if (listing) {
428                 ephy_spinner_start (EPHY_SPINNER (dialog->throbber));           
429         } else {
430                 ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
431         }
432
433         /* Update the refresh toggle button */
434         g_signal_handlers_block_by_func (dialog->togglebutton_refresh,
435                                          new_chatroom_dialog_togglebutton_refresh_toggled_cb,
436                                          dialog);
437         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->togglebutton_refresh),
438                                       listing);
439         g_signal_handlers_unblock_by_func (dialog->togglebutton_refresh,
440                                            new_chatroom_dialog_togglebutton_refresh_toggled_cb,
441                                            dialog);
442 }
443
444 static void
445 new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog)
446 {
447         GtkListStore *store;
448
449         store = GTK_LIST_STORE (dialog->model);
450         gtk_list_store_clear (store);
451 }
452
453 static void
454 new_chatroom_dialog_model_row_activated_cb (GtkTreeView             *tree_view,
455                                             GtkTreePath             *path,
456                                             GtkTreeViewColumn       *column,
457                                             EmpathyNewChatroomDialog *dialog)
458 {
459         gtk_widget_activate (dialog->button_join);
460 }
461
462 static void
463 new_chatroom_dialog_model_selection_changed (GtkTreeSelection         *selection,
464                                              EmpathyNewChatroomDialog *dialog)
465 {       
466         GtkTreeModel *model;
467         GtkTreeIter   iter;
468         gchar        *room = NULL;
469         gchar        *server = NULL;
470
471         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
472                 return;
473         }
474
475         gtk_tree_model_get (model, &iter, COL_ROOM, &room, -1);
476         server = strstr (room, "@");
477         if (server) {
478                 *server = '\0';
479                 server++;
480         }
481
482         gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), server ? server : "");
483         gtk_entry_set_text (GTK_ENTRY (dialog->entry_room), room ? room : "");
484
485         g_free (room);
486 }
487
488 static void
489 new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog)
490 {
491         EmpathyAccountChooser *account_chooser;
492         McAccount             *account;
493         const gchar           *room;
494         const gchar           *server = NULL;
495         gchar                 *room_name = NULL;
496
497         room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
498         server = gtk_entry_get_text (GTK_ENTRY (dialog->entry_server));
499
500         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
501         account = empathy_account_chooser_get_account (account_chooser);
502
503         if (!EMP_STR_EMPTY (server)) {
504                 room_name = g_strconcat (room, "@", server, NULL);
505         } else {
506                 room_name = g_strdup (room);
507         }
508
509         DEBUG ("Requesting channel for '%s'", room_name);
510         empathy_dispatcher_join_muc (account, room_name, NULL, NULL);
511
512         g_free (room_name);
513 }
514
515 static void
516 new_chatroom_dialog_entry_changed_cb (GtkWidget                *entry,
517                                       EmpathyNewChatroomDialog *dialog)
518 {
519         if (entry == dialog->entry_room) {
520                 const gchar *room;
521
522                 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
523                 gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
524                 /* FIXME: Select the room in the list */
525         }
526 }
527
528 static void
529 new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog)
530 {
531         new_chatroom_dialog_model_clear (dialog);
532         if (dialog->room_list) {
533                 empathy_tp_roomlist_start (dialog->room_list);
534         }
535 }
536
537 static void
538 new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog)
539 {
540         if (dialog->room_list) {
541                 empathy_tp_roomlist_stop (dialog->room_list);
542         }
543 }
544
545 static void
546 new_chatroom_dialog_entry_server_activate_cb (GtkWidget                *widget,
547                                               EmpathyNewChatroomDialog  *dialog)
548 {
549         new_chatroom_dialog_togglebutton_refresh_toggled_cb (dialog->togglebutton_refresh, 
550                                                              dialog);
551 }
552
553 static void
554 new_chatroom_dialog_togglebutton_refresh_toggled_cb (GtkWidget               *widget,
555                                                      EmpathyNewChatroomDialog *dialog)
556 {
557         gboolean toggled;
558
559         toggled = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
560         
561         if (toggled) {
562                 new_chatroom_dialog_browse_start (dialog);
563         } else {
564                 new_chatroom_dialog_browse_stop (dialog);
565         }
566 }
567