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