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