]> git.0d.be Git - empathy.git/blob - src/empathy-new-chatroom-dialog.c
Use autotools magic to take care of disabling deprecated symbols
[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 <glib.h>
32 #include <glib/gi18n.h>
33
34 #include <libmissioncontrol/mission-control.h>
35 #include <libmissioncontrol/mc-account.h>
36 #include <libmissioncontrol/mc-profile.h>
37
38 #include <libempathy/empathy-tp-roomlist.h>
39 #include <libempathy/empathy-chatroom.h>
40 #include <libempathy/empathy-utils.h>
41 #include <libempathy/empathy-dispatcher.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         GtkBuilder               *gui;
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.ui", "src");
135         gui = empathy_builder_get_file (filename,
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         g_free (filename);
152
153         empathy_builder_connect (gui, 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 (gui);
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         empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser),
189                                             empathy_account_chooser_filter_is_connected,
190                                             NULL);
191         gtk_table_attach_defaults (GTK_TABLE (dialog->table_info),
192                                    dialog->account_chooser,
193                                    1, 3, 0, 1);
194         gtk_widget_show (dialog->account_chooser);
195
196         g_signal_connect (GTK_COMBO_BOX (dialog->account_chooser), "changed",
197                           G_CALLBACK (new_chatroom_dialog_account_changed_cb),
198                           dialog);
199         new_chatroom_dialog_account_changed_cb (GTK_COMBO_BOX (dialog->account_chooser),
200                                                 dialog);
201
202         if (parent) {
203                 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
204                                               GTK_WINDOW (parent));
205         }
206
207         gtk_widget_show (dialog->window);
208 }
209
210 static void
211 new_chatroom_dialog_response_cb (GtkWidget               *widget,
212                                  gint                     response,
213                                  EmpathyNewChatroomDialog *dialog)
214 {
215         if (response == GTK_RESPONSE_OK) {
216                 new_chatroom_dialog_join (dialog);
217         }
218
219         gtk_widget_destroy (widget);
220 }
221
222 static void
223 new_chatroom_dialog_destroy_cb (GtkWidget               *widget,
224                                 EmpathyNewChatroomDialog *dialog)
225 {
226         if (dialog->room_list) {
227                 g_object_unref (dialog->room_list);
228         }
229         g_object_unref (dialog->model);  
230
231         g_free (dialog);
232 }
233
234 static void
235 new_chatroom_dialog_model_setup (EmpathyNewChatroomDialog *dialog)
236 {
237         GtkTreeView      *view;
238         GtkListStore     *store;
239         GtkTreeSelection *selection;
240
241         /* View */
242         view = GTK_TREE_VIEW (dialog->treeview);
243
244         g_signal_connect (view, "row-activated",
245                           G_CALLBACK (new_chatroom_dialog_model_row_activated_cb),
246                           dialog);
247
248         /* Store/Model */
249         store = gtk_list_store_new (COL_COUNT,
250                                     G_TYPE_STRING,       /* Image */
251                                     G_TYPE_STRING,       /* Text */
252                                     G_TYPE_STRING);      /* Room */
253
254         dialog->model = GTK_TREE_MODEL (store);
255         gtk_tree_view_set_model (view, dialog->model);
256
257         /* Selection */
258         selection = gtk_tree_view_get_selection (view);
259         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
260                                               COL_NAME, GTK_SORT_ASCENDING);
261
262         g_signal_connect (selection, "changed",
263                           G_CALLBACK (new_chatroom_dialog_model_selection_changed),
264                           dialog);
265
266         /* Columns */
267         new_chatroom_dialog_model_add_columns (dialog);
268 }
269
270 static void
271 new_chatroom_dialog_model_add_columns (EmpathyNewChatroomDialog *dialog)
272 {
273         GtkTreeView       *view;
274         GtkTreeViewColumn *column;
275         GtkCellRenderer   *cell;
276
277         view = GTK_TREE_VIEW (dialog->treeview);
278         gtk_tree_view_set_headers_visible (view, FALSE);
279
280         cell = gtk_cell_renderer_text_new ();
281         g_object_set (cell,
282                       "xpad", (guint) 4,
283                       "ypad", (guint) 1,
284                       "ellipsize", PANGO_ELLIPSIZE_END,
285                       NULL);
286
287         column = gtk_tree_view_column_new_with_attributes (_("Chat Rooms"),
288                                                            cell,
289                                                            "text", COL_NAME,
290                                                            NULL);
291
292         gtk_tree_view_column_set_expand (column, TRUE);
293         gtk_tree_view_append_column (view, column);
294 }
295
296 static void
297 new_chatroom_dialog_update_widgets (EmpathyNewChatroomDialog *dialog)
298 {
299         EmpathyAccountChooser *account_chooser;
300         McAccount             *account;
301         McProfile             *profile;
302         const gchar           *protocol;
303         const gchar           *room;
304         
305         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
306         account = empathy_account_chooser_get_account (account_chooser);
307         profile = mc_account_get_profile (account);
308         protocol = mc_profile_get_protocol_name (profile);
309
310         gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), "");
311
312         /* hardcode here known protocols */
313         if (strcmp (protocol, "jabber") == 0) {
314                 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
315                 gtk_widget_show (dialog->vbox_browse);
316
317         }
318         else if (strcmp (protocol, "local-xmpp") == 0) {
319                 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
320                 gtk_widget_show (dialog->vbox_browse);          
321         }
322         else if (strcmp (protocol, "irc") == 0) {
323                 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
324                 gtk_widget_show (dialog->vbox_browse);          
325         } else {
326                 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
327                 gtk_widget_show (dialog->vbox_browse);
328         }
329
330         room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
331         gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
332
333         /* Final set up of the dialog */
334         gtk_widget_grab_focus (dialog->entry_room);
335
336         g_object_unref (account);
337         g_object_unref (profile);
338 }
339
340 static void
341 new_chatroom_dialog_account_changed_cb (GtkComboBox             *combobox,
342                                         EmpathyNewChatroomDialog *dialog)
343 {
344         EmpathyAccountChooser *account_chooser;
345         McAccount             *account;
346         gboolean               listing = FALSE;
347
348         if (dialog->room_list) {
349                 g_object_unref (dialog->room_list);
350         }
351
352         ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
353         new_chatroom_dialog_model_clear (dialog);
354
355         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
356         account = empathy_account_chooser_get_account (account_chooser);
357         dialog->room_list = empathy_tp_roomlist_new (account);
358
359         if (dialog->room_list)  {
360                 g_signal_connect (dialog->room_list, "destroy",
361                                   G_CALLBACK (new_chatroom_dialog_roomlist_destroy_cb),
362                                   dialog);
363                 g_signal_connect (dialog->room_list, "new-room",
364                                   G_CALLBACK (new_chatroom_dialog_new_room_cb),
365                                   dialog);
366                 g_signal_connect (dialog->room_list, "notify::is-listing",
367                                   G_CALLBACK (new_chatroom_dialog_listing_cb),
368                                   dialog);
369
370                 listing = empathy_tp_roomlist_is_listing (dialog->room_list);
371                 if (listing) {
372                         ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
373                 }
374         }
375
376         new_chatroom_dialog_update_widgets (dialog);
377 }
378
379 static void
380 new_chatroom_dialog_roomlist_destroy_cb (EmpathyTpRoomlist        *room_list,
381                                          EmpathyNewChatroomDialog *dialog)
382 {
383         g_object_unref (dialog->room_list);
384         dialog->room_list = NULL;
385 }
386
387 static void
388 new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist        *room_list,
389                                  EmpathyChatroom          *chatroom,
390                                  EmpathyNewChatroomDialog *dialog)
391 {
392         GtkTreeView      *view;
393         GtkTreeSelection *selection;
394         GtkListStore     *store;
395         GtkTreeIter       iter;
396
397         DEBUG ("New chatroom listed: %s (%s)",
398                 empathy_chatroom_get_name (chatroom),
399                 empathy_chatroom_get_room (chatroom));
400
401         /* Add to model */
402         view = GTK_TREE_VIEW (dialog->treeview);
403         selection = gtk_tree_view_get_selection (view);
404         store = GTK_LIST_STORE (dialog->model);
405
406         gtk_list_store_append (store, &iter);
407         gtk_list_store_set (store, &iter,
408                             COL_NAME, empathy_chatroom_get_name (chatroom),
409                             COL_ROOM, empathy_chatroom_get_room (chatroom),
410                             -1);
411 }
412
413 static void
414 new_chatroom_dialog_listing_cb (EmpathyTpRoomlist        *room_list,
415                                 gpointer                  unused,
416                                 EmpathyNewChatroomDialog *dialog)
417 {
418         gboolean listing;
419
420         listing = empathy_tp_roomlist_is_listing (room_list);
421
422         /* Update the throbber */
423         if (listing) {
424                 ephy_spinner_start (EPHY_SPINNER (dialog->throbber));           
425         } else {
426                 ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
427         }
428
429         /* Update the refresh toggle button */
430         g_signal_handlers_block_by_func (dialog->togglebutton_refresh,
431                                          new_chatroom_dialog_togglebutton_refresh_toggled_cb,
432                                          dialog);
433         gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->togglebutton_refresh),
434                                       listing);
435         g_signal_handlers_unblock_by_func (dialog->togglebutton_refresh,
436                                            new_chatroom_dialog_togglebutton_refresh_toggled_cb,
437                                            dialog);
438 }
439
440 static void
441 new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog)
442 {
443         GtkListStore *store;
444
445         store = GTK_LIST_STORE (dialog->model);
446         gtk_list_store_clear (store);
447 }
448
449 static void
450 new_chatroom_dialog_model_row_activated_cb (GtkTreeView             *tree_view,
451                                             GtkTreePath             *path,
452                                             GtkTreeViewColumn       *column,
453                                             EmpathyNewChatroomDialog *dialog)
454 {
455         gtk_widget_activate (dialog->button_join);
456 }
457
458 static void
459 new_chatroom_dialog_model_selection_changed (GtkTreeSelection         *selection,
460                                              EmpathyNewChatroomDialog *dialog)
461 {       
462         GtkTreeModel *model;
463         GtkTreeIter   iter;
464         gchar        *room = NULL;
465         gchar        *server = NULL;
466
467         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
468                 return;
469         }
470
471         gtk_tree_model_get (model, &iter, COL_ROOM, &room, -1);
472         server = strstr (room, "@");
473         if (server) {
474                 *server = '\0';
475                 server++;
476         }
477
478         gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), server ? server : "");
479         gtk_entry_set_text (GTK_ENTRY (dialog->entry_room), room ? room : "");
480
481         g_free (room);
482 }
483
484 static void
485 new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog)
486 {
487         EmpathyAccountChooser *account_chooser;
488         McAccount             *account;
489         const gchar           *room;
490         const gchar           *server = NULL;
491         gchar                 *room_name = NULL;
492
493         room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
494         server = gtk_entry_get_text (GTK_ENTRY (dialog->entry_server));
495
496         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
497         account = empathy_account_chooser_get_account (account_chooser);
498
499         if (!EMP_STR_EMPTY (server)) {
500                 room_name = g_strconcat (room, "@", server, NULL);
501         } else {
502                 room_name = g_strdup (room);
503         }
504
505         DEBUG ("Requesting channel for '%s'", room_name);
506         empathy_dispatcher_join_muc (account, room_name, NULL, NULL);
507
508         g_free (room_name);
509 }
510
511 static void
512 new_chatroom_dialog_entry_changed_cb (GtkWidget                *entry,
513                                       EmpathyNewChatroomDialog *dialog)
514 {
515         if (entry == dialog->entry_room) {
516                 const gchar *room;
517
518                 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
519                 gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
520                 /* FIXME: Select the room in the list */
521         }
522 }
523
524 static void
525 new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog)
526 {
527         new_chatroom_dialog_model_clear (dialog);
528         if (dialog->room_list) {
529                 empathy_tp_roomlist_start (dialog->room_list);
530         }
531 }
532
533 static void
534 new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog)
535 {
536         if (dialog->room_list) {
537                 empathy_tp_roomlist_stop (dialog->room_list);
538         }
539 }
540
541 static void
542 new_chatroom_dialog_entry_server_activate_cb (GtkWidget                *widget,
543                                               EmpathyNewChatroomDialog  *dialog)
544 {
545         new_chatroom_dialog_togglebutton_refresh_toggled_cb (dialog->togglebutton_refresh, 
546                                                              dialog);
547 }
548
549 static void
550 new_chatroom_dialog_togglebutton_refresh_toggled_cb (GtkWidget               *widget,
551                                                      EmpathyNewChatroomDialog *dialog)
552 {
553         gboolean toggled;
554
555         toggled = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
556         
557         if (toggled) {
558                 new_chatroom_dialog_browse_start (dialog);
559         } else {
560                 new_chatroom_dialog_browse_stop (dialog);
561         }
562 }
563