]> git.0d.be Git - empathy.git/blob - src/empathy-new-chatroom-dialog.c
Merge branch 'debugger'
[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., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  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 #include <glib/gprintf.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         *label_room;
64         GtkWidget         *entry_room;
65         GtkWidget         *expander_browse;
66         GtkWidget         *throbber;
67         GtkWidget         *treeview;
68         GtkTreeModel      *model;
69         GtkWidget         *button_join;
70         GtkWidget         *label_error_message;
71         GtkWidget         *viewport_error;
72 } EmpathyNewChatroomDialog;
73
74 enum {
75         COL_NEED_PASSWORD,
76         COL_INVITE_ONLY,
77         COL_NAME,
78         COL_ROOM,
79         COL_MEMBERS,
80         COL_MEMBERS_INT,
81         COL_TOOLTIP,
82         COL_COUNT
83 };
84
85 static void     new_chatroom_dialog_response_cb                     (GtkWidget               *widget,
86                                                                      gint                     response,
87                                                                      EmpathyNewChatroomDialog *dialog);
88 static void     new_chatroom_dialog_destroy_cb                      (GtkWidget               *widget,
89                                                                      EmpathyNewChatroomDialog *dialog);
90 static void     new_chatroom_dialog_model_setup                     (EmpathyNewChatroomDialog *dialog);
91 static void     new_chatroom_dialog_model_add_columns               (EmpathyNewChatroomDialog *dialog);
92 static void     new_chatroom_dialog_update_widgets                  (EmpathyNewChatroomDialog *dialog);
93 static void     new_chatroom_dialog_account_changed_cb              (GtkComboBox              *combobox,
94                                                                      EmpathyNewChatroomDialog *dialog);
95 static void     new_chatroom_dialog_roomlist_destroy_cb             (EmpathyTpRoomlist        *room_list,
96                                                                      EmpathyNewChatroomDialog *dialog);
97 static void     new_chatroom_dialog_new_room_cb                     (EmpathyTpRoomlist        *room_list,
98                                                                      EmpathyChatroom          *chatroom,
99                                                                      EmpathyNewChatroomDialog *dialog);
100 static void     new_chatroom_dialog_listing_cb                      (EmpathyTpRoomlist        *room_list,
101                                                                      gpointer                  unused,
102                                                                      EmpathyNewChatroomDialog *dialog);
103 static void     start_listing_error_cb                              (EmpathyTpRoomlist        *room_list,
104                                                                      GError                   *error,
105                                                                      EmpathyNewChatroomDialog *dialog);
106 static void     stop_listing_error_cb                               (EmpathyTpRoomlist        *room_list,
107                                                                      GError                   *error,
108                                                                      EmpathyNewChatroomDialog *dialog);
109 static void     new_chatroom_dialog_model_clear                     (EmpathyNewChatroomDialog *dialog);
110 static void     new_chatroom_dialog_model_row_activated_cb          (GtkTreeView             *tree_view,
111                                                                      GtkTreePath             *path,
112                                                                      GtkTreeViewColumn       *column,
113                                                                      EmpathyNewChatroomDialog *dialog);
114 static void     new_chatroom_dialog_model_selection_changed         (GtkTreeSelection        *selection,
115                                                                      EmpathyNewChatroomDialog *dialog);
116 static void     new_chatroom_dialog_join                            (EmpathyNewChatroomDialog *dialog);
117 static void     new_chatroom_dialog_entry_changed_cb                (GtkWidget               *entry,
118                                                                      EmpathyNewChatroomDialog *dialog);
119 static void     new_chatroom_dialog_browse_start                    (EmpathyNewChatroomDialog *dialog);
120 static void     new_chatroom_dialog_browse_stop                     (EmpathyNewChatroomDialog *dialog);
121 static void     new_chatroom_dialog_entry_server_activate_cb        (GtkWidget               *widget,
122                                                                      EmpathyNewChatroomDialog *dialog);
123 static void     new_chatroom_dialog_expander_browse_activate_cb     (GtkWidget               *widget,
124                                                                      EmpathyNewChatroomDialog *dialog);
125 static gboolean new_chatroom_dialog_entry_server_focus_out_cb       (GtkWidget               *widget,
126                                                                      GdkEventFocus           *event,
127                                                                      EmpathyNewChatroomDialog *dialog);
128 static void     new_chatroom_dialog_button_close_error_clicked_cb   (GtkButton                *button,
129                                                                      EmpathyNewChatroomDialog *dialog);
130
131 static EmpathyNewChatroomDialog *dialog_p = NULL;
132
133 void
134 empathy_new_chatroom_dialog_show (GtkWindow *parent)
135 {
136         EmpathyNewChatroomDialog *dialog;
137         GtkBuilder               *gui;
138         GtkSizeGroup             *size_group;
139         gchar                    *filename;
140
141         if (dialog_p) {
142                 gtk_window_present (GTK_WINDOW (dialog_p->window));
143                 return;
144         }
145
146         dialog_p = dialog = g_new0 (EmpathyNewChatroomDialog, 1);
147
148         filename = empathy_file_lookup ("empathy-new-chatroom-dialog.ui", "src");
149         gui = empathy_builder_get_file (filename,
150                                        "new_chatroom_dialog", &dialog->window,
151                                        "table_info", &dialog->table_info,
152                                        "label_account", &dialog->label_account,
153                                        "label_server", &dialog->label_server,
154                                        "label_room", &dialog->label_room,
155                                        "entry_server", &dialog->entry_server,
156                                        "entry_room", &dialog->entry_room,
157                                        "treeview", &dialog->treeview,
158                                        "button_join", &dialog->button_join,
159                                        "expander_browse", &dialog->expander_browse,
160                                        "label_error_message", &dialog->label_error_message,
161                                        "viewport_error", &dialog->viewport_error,
162                                        NULL);
163         g_free (filename);
164
165         empathy_builder_connect (gui, dialog,
166                               "new_chatroom_dialog", "response", new_chatroom_dialog_response_cb,
167                               "new_chatroom_dialog", "destroy", new_chatroom_dialog_destroy_cb,
168                               "entry_server", "changed", new_chatroom_dialog_entry_changed_cb,
169                               "entry_server", "activate", new_chatroom_dialog_entry_server_activate_cb,
170                               "entry_server", "focus-out-event", new_chatroom_dialog_entry_server_focus_out_cb,
171                               "entry_room", "changed", new_chatroom_dialog_entry_changed_cb,
172                               "expander_browse", "activate", new_chatroom_dialog_expander_browse_activate_cb,
173                               "button_close_error", "clicked", new_chatroom_dialog_button_close_error_clicked_cb,
174                               NULL);
175
176         g_object_unref (gui);
177
178         g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog_p);
179
180         /* Label alignment */
181         size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
182
183         gtk_size_group_add_widget (size_group, dialog->label_account);
184         gtk_size_group_add_widget (size_group, dialog->label_server);
185         gtk_size_group_add_widget (size_group, dialog->label_room);
186
187         g_object_unref (size_group);
188
189         /* Set up chatrooms treeview */
190         new_chatroom_dialog_model_setup (dialog);
191
192         /* Add throbber */
193         dialog->throbber = ephy_spinner_new ();
194         ephy_spinner_set_size (EPHY_SPINNER (dialog->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
195         gtk_widget_show (dialog->throbber);
196         gtk_table_attach (GTK_TABLE (dialog->table_info),
197                           dialog->throbber,
198                           2, 3, 0, 1,
199                           0, 0, 0, 0);
200
201         /* Account chooser for custom */
202         dialog->account_chooser = empathy_account_chooser_new ();
203         empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser),
204                                             empathy_account_chooser_filter_is_connected,
205                                             NULL);
206         gtk_table_attach_defaults (GTK_TABLE (dialog->table_info),
207                                    dialog->account_chooser,
208                                    1, 2, 0, 1);
209         gtk_widget_show (dialog->account_chooser);
210
211         g_signal_connect (GTK_COMBO_BOX (dialog->account_chooser), "changed",
212                           G_CALLBACK (new_chatroom_dialog_account_changed_cb),
213                           dialog);
214         new_chatroom_dialog_account_changed_cb (GTK_COMBO_BOX (dialog->account_chooser),
215                                                 dialog);
216
217         if (parent) {
218                 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
219                                               GTK_WINDOW (parent));
220         }
221
222         gtk_widget_show (dialog->window);
223 }
224
225 static void
226 new_chatroom_dialog_response_cb (GtkWidget               *widget,
227                                  gint                     response,
228                                  EmpathyNewChatroomDialog *dialog)
229 {
230         if (response == GTK_RESPONSE_OK) {
231                 new_chatroom_dialog_join (dialog);
232         }
233
234         gtk_widget_destroy (widget);
235 }
236
237 static void
238 new_chatroom_dialog_destroy_cb (GtkWidget               *widget,
239                                 EmpathyNewChatroomDialog *dialog)
240 {
241         if (dialog->room_list) {
242                 g_object_unref (dialog->room_list);
243         }
244         g_object_unref (dialog->model);
245
246         g_free (dialog);
247 }
248
249 static void
250 new_chatroom_dialog_model_setup (EmpathyNewChatroomDialog *dialog)
251 {
252         GtkTreeView      *view;
253         GtkListStore     *store;
254         GtkTreeSelection *selection;
255
256         /* View */
257         view = GTK_TREE_VIEW (dialog->treeview);
258
259         g_signal_connect (view, "row-activated",
260                           G_CALLBACK (new_chatroom_dialog_model_row_activated_cb),
261                           dialog);
262
263         /* Store/Model */
264         store = gtk_list_store_new (COL_COUNT,
265                                     G_TYPE_STRING,       /* Invite */
266                                     G_TYPE_STRING,       /* Password */
267                                     G_TYPE_STRING,       /* Name */
268                                     G_TYPE_STRING,       /* Room */
269                                     G_TYPE_STRING,       /* Member count */
270                                     G_TYPE_INT,          /* Member count int */
271                                     G_TYPE_STRING);      /* Tool tip */
272
273         dialog->model = GTK_TREE_MODEL (store);
274         gtk_tree_view_set_model (view, dialog->model);
275         gtk_tree_view_set_tooltip_column (view, COL_TOOLTIP);
276         gtk_tree_view_set_search_column (view, COL_NAME);
277
278         /* Selection */
279         selection = gtk_tree_view_get_selection (view);
280         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
281                                               COL_NAME, GTK_SORT_ASCENDING);
282
283         g_signal_connect (selection, "changed",
284                           G_CALLBACK (new_chatroom_dialog_model_selection_changed),
285                           dialog);
286
287         /* Columns */
288         new_chatroom_dialog_model_add_columns (dialog);
289 }
290
291 static void
292 new_chatroom_dialog_model_add_columns (EmpathyNewChatroomDialog *dialog)
293 {
294         GtkTreeView       *view;
295         GtkTreeViewColumn *column;
296         GtkCellRenderer   *cell;
297         gint               width, height;
298
299         gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &width, &height);
300
301         view = GTK_TREE_VIEW (dialog->treeview);
302
303         cell = gtk_cell_renderer_pixbuf_new ();
304         g_object_set (cell,
305                       "width", width,
306                       "height", height,
307                       "stock-size", GTK_ICON_SIZE_MENU,
308                       NULL);
309         column = gtk_tree_view_column_new_with_attributes (NULL,
310                                                            cell,
311                                                            "stock-id", COL_INVITE_ONLY,
312                                                            NULL);
313
314         gtk_tree_view_column_set_sort_column_id (column, COL_INVITE_ONLY);
315         gtk_tree_view_append_column (view, column);
316
317         column = gtk_tree_view_column_new_with_attributes (NULL,
318                                                            cell,
319                                                            "stock-id", COL_NEED_PASSWORD,
320                                                            NULL);
321
322         gtk_tree_view_column_set_sort_column_id (column, COL_NEED_PASSWORD);
323         gtk_tree_view_append_column (view, column);
324
325         cell = gtk_cell_renderer_text_new ();
326         g_object_set (cell,
327                       "xpad", (guint) 4,
328                       "ypad", (guint) 2,
329                       "ellipsize", PANGO_ELLIPSIZE_END,
330                       NULL);
331
332         column = gtk_tree_view_column_new_with_attributes (_("Chat Room"),
333                                                            cell,
334                                                            "text", COL_NAME,
335                                                            NULL);
336
337         gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
338         gtk_tree_view_column_set_expand (column, TRUE);
339         gtk_tree_view_append_column (view, column);
340
341         cell = gtk_cell_renderer_text_new ();
342         g_object_set (cell,
343                       "xpad", (guint) 4,
344                       "ypad", (guint) 2,
345                       "ellipsize", PANGO_ELLIPSIZE_END,
346                       "alignment", PANGO_ALIGN_RIGHT,
347                       NULL);
348         column = gtk_tree_view_column_new_with_attributes (_("Members"),
349                                                            cell,
350                                                            "text", COL_MEMBERS,
351                                                            NULL);
352
353         gtk_tree_view_column_set_sort_column_id (column, COL_MEMBERS_INT);
354         gtk_tree_view_append_column (view, column);
355 }
356
357 static void
358 new_chatroom_dialog_update_widgets (EmpathyNewChatroomDialog *dialog)
359 {
360         EmpathyAccountChooser *account_chooser;
361         McAccount             *account;
362         McProfile             *profile;
363         const gchar           *protocol;
364         const gchar           *room;
365
366         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
367         account = empathy_account_chooser_dup_account (account_chooser);
368         profile = mc_account_get_profile (account);
369         protocol = mc_profile_get_protocol_name (profile);
370
371         gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), "");
372
373         /* hardcode here known protocols */
374         if (strcmp (protocol, "jabber") == 0) {
375                 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
376         }
377         else if (strcmp (protocol, "local-xmpp") == 0) {
378                 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
379         }
380         else if (strcmp (protocol, "irc") == 0) {
381                 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
382         }
383         else {
384                 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
385         }
386
387         room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
388         gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
389
390         /* Final set up of the dialog */
391         gtk_widget_grab_focus (dialog->entry_room);
392
393         g_object_unref (account);
394         g_object_unref (profile);
395 }
396
397 static void
398 new_chatroom_dialog_account_changed_cb (GtkComboBox             *combobox,
399                                         EmpathyNewChatroomDialog *dialog)
400 {
401         EmpathyAccountChooser *account_chooser;
402         McAccount             *account;
403         gboolean               listing = FALSE;
404         gboolean               expanded = FALSE;
405
406         if (dialog->room_list) {
407                 g_object_unref (dialog->room_list);
408         }
409
410         ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
411         new_chatroom_dialog_model_clear (dialog);
412
413         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
414         account = empathy_account_chooser_dup_account (account_chooser);
415         dialog->room_list = empathy_tp_roomlist_new (account);
416
417         if (dialog->room_list) {
418                 g_signal_connect (dialog->room_list, "destroy",
419                                   G_CALLBACK (new_chatroom_dialog_roomlist_destroy_cb),
420                                   dialog);
421                 g_signal_connect (dialog->room_list, "new-room",
422                                   G_CALLBACK (new_chatroom_dialog_new_room_cb),
423                                   dialog);
424                 g_signal_connect (dialog->room_list, "notify::is-listing",
425                                   G_CALLBACK (new_chatroom_dialog_listing_cb),
426                                   dialog);
427                 g_signal_connect (dialog->room_list, "error::start",
428                                   G_CALLBACK (start_listing_error_cb),
429                                   dialog);
430                 g_signal_connect (dialog->room_list, "error::stop",
431                                   G_CALLBACK (stop_listing_error_cb),
432                                   dialog);
433
434                 expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
435                 if (expanded) {
436                         gtk_widget_hide (dialog->viewport_error);
437                         gtk_widget_set_sensitive (dialog->treeview, TRUE);
438                         new_chatroom_dialog_browse_start (dialog);
439                 }
440
441                 listing = empathy_tp_roomlist_is_listing (dialog->room_list);
442                 if (listing) {
443                         ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
444                 }
445         }
446
447         new_chatroom_dialog_update_widgets (dialog);
448
449         g_object_unref (account);
450 }
451
452 static void
453 new_chatroom_dialog_button_close_error_clicked_cb   (GtkButton                *button,
454                                                      EmpathyNewChatroomDialog *dialog)
455 {
456         gtk_widget_hide (dialog->viewport_error);
457 }
458
459 static void
460 new_chatroom_dialog_roomlist_destroy_cb (EmpathyTpRoomlist        *room_list,
461                                          EmpathyNewChatroomDialog *dialog)
462 {
463         g_object_unref (dialog->room_list);
464         dialog->room_list = NULL;
465 }
466
467 static void
468 new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist        *room_list,
469                                  EmpathyChatroom          *chatroom,
470                                  EmpathyNewChatroomDialog *dialog)
471 {
472         GtkTreeView      *view;
473         GtkTreeSelection *selection;
474         GtkListStore     *store;
475         GtkTreeIter       iter;
476         gchar            *members;
477         gchar            *tooltip;
478         const gchar      *need_password;
479         const gchar      *invite_only;
480
481         DEBUG ("New chatroom listed: %s (%s)",
482                 empathy_chatroom_get_name (chatroom),
483                 empathy_chatroom_get_room (chatroom));
484
485         /* Add to model */
486         view = GTK_TREE_VIEW (dialog->treeview);
487         selection = gtk_tree_view_get_selection (view);
488         store = GTK_LIST_STORE (dialog->model);
489         members = g_strdup_printf ("%d", empathy_chatroom_get_members_count (chatroom));
490         tooltip = g_strdup_printf (C_("Room/Join's roomlist tooltip. Parameters"
491                 "are a channel name, yes/no, yes/no and a number.",
492                 "<b>%s</b>\nInvite required: %s\nPassword required: %s\nMembers: %s"),
493                 empathy_chatroom_get_name (chatroom),
494                 empathy_chatroom_get_invite_only (chatroom) ? _("Yes") : _("No"),
495                 empathy_chatroom_get_need_password (chatroom) ? _("Yes") : _("No"),
496                 members);
497         invite_only = (empathy_chatroom_get_invite_only (chatroom) ?
498                 GTK_STOCK_INDEX : NULL);
499         need_password = (empathy_chatroom_get_need_password (chatroom) ?
500                 GTK_STOCK_DIALOG_AUTHENTICATION : NULL);
501
502         gtk_list_store_append (store, &iter);
503         gtk_list_store_set (store, &iter,
504                             COL_NEED_PASSWORD, need_password,
505                             COL_INVITE_ONLY, invite_only,
506                             COL_NAME, empathy_chatroom_get_name (chatroom),
507                             COL_ROOM, empathy_chatroom_get_room (chatroom),
508                             COL_MEMBERS, members,
509                             COL_MEMBERS_INT, empathy_chatroom_get_members_count (chatroom),
510                             COL_TOOLTIP, tooltip,
511                             -1);
512
513         g_free (members);
514         g_free (tooltip);
515 }
516
517 static void
518 start_listing_error_cb (EmpathyTpRoomlist        *room_list,
519                         GError                   *error,
520                         EmpathyNewChatroomDialog *dialog)
521 {
522         gtk_label_set_text (GTK_LABEL (dialog->label_error_message), _("Could not start room listing"));
523         gtk_widget_show_all (dialog->viewport_error);
524         gtk_widget_set_sensitive (dialog->treeview, FALSE);
525 }
526
527 static void
528 stop_listing_error_cb (EmpathyTpRoomlist        *room_list,
529                        GError                   *error,
530                        EmpathyNewChatroomDialog *dialog)
531 {
532         gtk_label_set_text (GTK_LABEL (dialog->label_error_message), _("Could not stop room listing"));
533         gtk_widget_show_all (dialog->viewport_error);
534         gtk_widget_set_sensitive (dialog->treeview, FALSE);
535 }
536
537 static void
538 new_chatroom_dialog_listing_cb (EmpathyTpRoomlist        *room_list,
539                                 gpointer                  unused,
540                                 EmpathyNewChatroomDialog *dialog)
541 {
542         gboolean listing;
543
544         listing = empathy_tp_roomlist_is_listing (room_list);
545
546         /* Update the throbber */
547         if (listing) {
548                 ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
549         } else {
550                 ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
551         }
552 }
553
554 static void
555 new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog)
556 {
557         GtkListStore *store;
558
559         store = GTK_LIST_STORE (dialog->model);
560         gtk_list_store_clear (store);
561 }
562
563 static void
564 new_chatroom_dialog_model_row_activated_cb (GtkTreeView             *tree_view,
565                                             GtkTreePath             *path,
566                                             GtkTreeViewColumn       *column,
567                                             EmpathyNewChatroomDialog *dialog)
568 {
569         gtk_widget_activate (dialog->button_join);
570 }
571
572 static void
573 new_chatroom_dialog_model_selection_changed (GtkTreeSelection         *selection,
574                                              EmpathyNewChatroomDialog *dialog)
575 {
576         GtkTreeModel *model;
577         GtkTreeIter   iter;
578         gchar        *room = NULL;
579         gchar        *server = NULL;
580
581         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
582                 return;
583         }
584
585         gtk_tree_model_get (model, &iter, COL_ROOM, &room, -1);
586         server = strstr (room, "@");
587         if (server) {
588                 *server = '\0';
589                 server++;
590         }
591
592         gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), server ? server : "");
593         gtk_entry_set_text (GTK_ENTRY (dialog->entry_room), room ? room : "");
594
595         g_free (room);
596 }
597
598 static void
599 new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog)
600 {
601         EmpathyAccountChooser *account_chooser;
602         TpConnection          *connection;
603         const gchar           *room;
604         const gchar           *server = NULL;
605         gchar                 *room_name = NULL;
606
607         room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
608         server = gtk_entry_get_text (GTK_ENTRY (dialog->entry_server));
609
610         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
611         connection = empathy_account_chooser_get_connection (account_chooser);
612
613         if (!EMP_STR_EMPTY (server)) {
614                 room_name = g_strconcat (room, "@", server, NULL);
615         } else {
616                 room_name = g_strdup (room);
617         }
618
619         DEBUG ("Requesting channel for '%s'", room_name);
620         empathy_dispatcher_join_muc (connection, room_name, NULL, NULL);
621
622         g_free (room_name);
623 }
624
625 static void
626 new_chatroom_dialog_entry_changed_cb (GtkWidget                *entry,
627                                       EmpathyNewChatroomDialog *dialog)
628 {
629         if (entry == dialog->entry_room) {
630                 const gchar *room;
631
632                 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
633                 gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
634                 /* FIXME: Select the room in the list */
635         }
636 }
637
638 static void
639 new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog)
640 {
641         new_chatroom_dialog_model_clear (dialog);
642         if (dialog->room_list) {
643                 empathy_tp_roomlist_start (dialog->room_list);
644         }
645 }
646
647 static void
648 new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog)
649 {
650         if (dialog->room_list) {
651                 empathy_tp_roomlist_stop (dialog->room_list);
652         }
653 }
654
655 static void
656 new_chatroom_dialog_entry_server_activate_cb (GtkWidget                *widget,
657                                               EmpathyNewChatroomDialog  *dialog)
658 {
659         new_chatroom_dialog_browse_start (dialog);
660 }
661
662 static void
663 new_chatroom_dialog_expander_browse_activate_cb (GtkWidget               *widget,
664                                                  EmpathyNewChatroomDialog *dialog)
665 {
666         gboolean expanded;
667
668         expanded = gtk_expander_get_expanded (GTK_EXPANDER (widget));
669         if (expanded) {
670                 new_chatroom_dialog_browse_stop (dialog);
671                 gtk_window_set_resizable (GTK_WINDOW (dialog->window), FALSE);
672         } else {
673                 gtk_widget_hide (dialog->viewport_error);
674                 gtk_widget_set_sensitive (dialog->treeview, TRUE);
675                 new_chatroom_dialog_browse_start (dialog);
676                 gtk_window_set_resizable (GTK_WINDOW (dialog->window), TRUE);
677         }
678 }
679
680 static gboolean
681 new_chatroom_dialog_entry_server_focus_out_cb (GtkWidget               *widget,
682                                                GdkEventFocus           *event,
683                                                EmpathyNewChatroomDialog *dialog)
684 {
685         gboolean expanded;
686
687         expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
688         if (expanded) {
689                 new_chatroom_dialog_browse_start (dialog);
690         }
691         return FALSE;
692 }