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