]> git.0d.be Git - empathy.git/blob - src/empathy-new-chatroom-dialog.c
Remove useless mission-control includes
[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         protocol = empathy_account_get_protocol (account);
364
365         gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), "");
366
367         /* hardcode here known protocols */
368         if (strcmp (protocol, "jabber") == 0) {
369                 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
370         }
371         else if (strcmp (protocol, "local-xmpp") == 0) {
372                 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
373         }
374         else if (strcmp (protocol, "irc") == 0) {
375                 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
376         }
377         else {
378                 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
379         }
380
381         room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
382         gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
383
384         /* Final set up of the dialog */
385         gtk_widget_grab_focus (dialog->entry_room);
386
387         g_object_unref (account);
388 }
389
390 static void
391 new_chatroom_dialog_account_changed_cb (GtkComboBox             *combobox,
392                                         EmpathyNewChatroomDialog *dialog)
393 {
394         EmpathyAccountChooser *account_chooser;
395         EmpathyAccount        *account;
396         gboolean               listing = FALSE;
397         gboolean               expanded = FALSE;
398
399         if (dialog->room_list) {
400                 g_object_unref (dialog->room_list);
401                 dialog->room_list = NULL;
402         }
403
404         ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
405         new_chatroom_dialog_model_clear (dialog);
406
407         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
408         account = empathy_account_chooser_dup_account (account_chooser);
409         if (account == NULL)
410                 goto out;
411
412         dialog->room_list = empathy_tp_roomlist_new (account);
413
414         if (dialog->room_list) {
415                 g_signal_connect (dialog->room_list, "destroy",
416                                   G_CALLBACK (new_chatroom_dialog_roomlist_destroy_cb),
417                                   dialog);
418                 g_signal_connect (dialog->room_list, "new-room",
419                                   G_CALLBACK (new_chatroom_dialog_new_room_cb),
420                                   dialog);
421                 g_signal_connect (dialog->room_list, "notify::is-listing",
422                                   G_CALLBACK (new_chatroom_dialog_listing_cb),
423                                   dialog);
424                 g_signal_connect (dialog->room_list, "error::start",
425                                   G_CALLBACK (start_listing_error_cb),
426                                   dialog);
427                 g_signal_connect (dialog->room_list, "error::stop",
428                                   G_CALLBACK (stop_listing_error_cb),
429                                   dialog);
430
431                 expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
432                 if (expanded) {
433                         gtk_widget_hide (dialog->viewport_error);
434                         gtk_widget_set_sensitive (dialog->treeview, TRUE);
435                         new_chatroom_dialog_browse_start (dialog);
436                 }
437
438                 listing = empathy_tp_roomlist_is_listing (dialog->room_list);
439                 if (listing) {
440                         ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
441                 }
442         }
443
444         g_object_unref (account);
445
446 out:
447         new_chatroom_dialog_update_widgets (dialog);
448 }
449
450 static void
451 new_chatroom_dialog_button_close_error_clicked_cb   (GtkButton                *button,
452                                                      EmpathyNewChatroomDialog *dialog)
453 {
454         gtk_widget_hide (dialog->viewport_error);
455 }
456
457 static void
458 new_chatroom_dialog_roomlist_destroy_cb (EmpathyTpRoomlist        *room_list,
459                                          EmpathyNewChatroomDialog *dialog)
460 {
461         g_object_unref (dialog->room_list);
462         dialog->room_list = NULL;
463 }
464
465 static void
466 new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist        *room_list,
467                                  EmpathyChatroom          *chatroom,
468                                  EmpathyNewChatroomDialog *dialog)
469 {
470         GtkTreeView      *view;
471         GtkTreeSelection *selection;
472         GtkListStore     *store;
473         GtkTreeIter       iter;
474         gchar            *members;
475         gchar            *tooltip;
476         const gchar      *need_password;
477         const gchar      *invite_only;
478
479         DEBUG ("New chatroom listed: %s (%s)",
480                 empathy_chatroom_get_name (chatroom),
481                 empathy_chatroom_get_room (chatroom));
482
483         /* Add to model */
484         view = GTK_TREE_VIEW (dialog->treeview);
485         selection = gtk_tree_view_get_selection (view);
486         store = GTK_LIST_STORE (dialog->model);
487         members = g_strdup_printf ("%d", empathy_chatroom_get_members_count (chatroom));
488         tooltip = g_strdup_printf (C_("Room/Join's roomlist tooltip. Parameters"
489                 "are a channel name, yes/no, yes/no and a number.",
490                 "<b>%s</b>\nInvite required: %s\nPassword required: %s\nMembers: %s"),
491                 empathy_chatroom_get_name (chatroom),
492                 empathy_chatroom_get_invite_only (chatroom) ? _("Yes") : _("No"),
493                 empathy_chatroom_get_need_password (chatroom) ? _("Yes") : _("No"),
494                 members);
495         invite_only = (empathy_chatroom_get_invite_only (chatroom) ?
496                 GTK_STOCK_INDEX : NULL);
497         need_password = (empathy_chatroom_get_need_password (chatroom) ?
498                 GTK_STOCK_DIALOG_AUTHENTICATION : NULL);
499
500         gtk_list_store_append (store, &iter);
501         gtk_list_store_set (store, &iter,
502                             COL_NEED_PASSWORD, need_password,
503                             COL_INVITE_ONLY, invite_only,
504                             COL_NAME, empathy_chatroom_get_name (chatroom),
505                             COL_ROOM, empathy_chatroom_get_room (chatroom),
506                             COL_MEMBERS, members,
507                             COL_MEMBERS_INT, empathy_chatroom_get_members_count (chatroom),
508                             COL_TOOLTIP, tooltip,
509                             -1);
510
511         g_free (members);
512         g_free (tooltip);
513 }
514
515 static void
516 start_listing_error_cb (EmpathyTpRoomlist        *room_list,
517                         GError                   *error,
518                         EmpathyNewChatroomDialog *dialog)
519 {
520         gtk_label_set_text (GTK_LABEL (dialog->label_error_message), _("Could not start room listing"));
521         gtk_widget_show_all (dialog->viewport_error);
522         gtk_widget_set_sensitive (dialog->treeview, FALSE);
523 }
524
525 static void
526 stop_listing_error_cb (EmpathyTpRoomlist        *room_list,
527                        GError                   *error,
528                        EmpathyNewChatroomDialog *dialog)
529 {
530         gtk_label_set_text (GTK_LABEL (dialog->label_error_message), _("Could not stop room listing"));
531         gtk_widget_show_all (dialog->viewport_error);
532         gtk_widget_set_sensitive (dialog->treeview, FALSE);
533 }
534
535 static void
536 new_chatroom_dialog_listing_cb (EmpathyTpRoomlist        *room_list,
537                                 gpointer                  unused,
538                                 EmpathyNewChatroomDialog *dialog)
539 {
540         gboolean listing;
541
542         listing = empathy_tp_roomlist_is_listing (room_list);
543
544         /* Update the throbber */
545         if (listing) {
546                 ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
547         } else {
548                 ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
549         }
550 }
551
552 static void
553 new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog)
554 {
555         GtkListStore *store;
556
557         store = GTK_LIST_STORE (dialog->model);
558         gtk_list_store_clear (store);
559 }
560
561 static void
562 new_chatroom_dialog_model_row_activated_cb (GtkTreeView             *tree_view,
563                                             GtkTreePath             *path,
564                                             GtkTreeViewColumn       *column,
565                                             EmpathyNewChatroomDialog *dialog)
566 {
567         gtk_widget_activate (dialog->button_join);
568 }
569
570 static void
571 new_chatroom_dialog_model_selection_changed (GtkTreeSelection         *selection,
572                                              EmpathyNewChatroomDialog *dialog)
573 {
574         GtkTreeModel *model;
575         GtkTreeIter   iter;
576         gchar        *room = NULL;
577         gchar        *server = NULL;
578
579         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
580                 return;
581         }
582
583         gtk_tree_model_get (model, &iter, COL_ROOM, &room, -1);
584         server = strstr (room, "@");
585         if (server) {
586                 *server = '\0';
587                 server++;
588         }
589
590         gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), server ? server : "");
591         gtk_entry_set_text (GTK_ENTRY (dialog->entry_room), room ? room : "");
592
593         g_free (room);
594 }
595
596 static void
597 new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog)
598 {
599         EmpathyAccountChooser *account_chooser;
600         TpConnection          *connection;
601         const gchar           *room;
602         const gchar           *server = NULL;
603         gchar                 *room_name = NULL;
604
605         room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
606         server = gtk_entry_get_text (GTK_ENTRY (dialog->entry_server));
607
608         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
609         connection = empathy_account_chooser_get_connection (account_chooser);
610
611         if (!EMP_STR_EMPTY (server)) {
612                 room_name = g_strconcat (room, "@", server, NULL);
613         } else {
614                 room_name = g_strdup (room);
615         }
616
617         DEBUG ("Requesting channel for '%s'", room_name);
618         empathy_dispatcher_join_muc (connection, room_name, NULL, NULL);
619
620         g_free (room_name);
621 }
622
623 static void
624 new_chatroom_dialog_entry_changed_cb (GtkWidget                *entry,
625                                       EmpathyNewChatroomDialog *dialog)
626 {
627         if (entry == dialog->entry_room) {
628                 const gchar *room;
629
630                 room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
631                 gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
632                 /* FIXME: Select the room in the list */
633         }
634 }
635
636 static void
637 new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog)
638 {
639         new_chatroom_dialog_model_clear (dialog);
640         if (dialog->room_list) {
641                 empathy_tp_roomlist_start (dialog->room_list);
642         }
643 }
644
645 static void
646 new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog)
647 {
648         if (dialog->room_list) {
649                 empathy_tp_roomlist_stop (dialog->room_list);
650         }
651 }
652
653 static void
654 new_chatroom_dialog_entry_server_activate_cb (GtkWidget                *widget,
655                                               EmpathyNewChatroomDialog  *dialog)
656 {
657         new_chatroom_dialog_browse_start (dialog);
658 }
659
660 static void
661 new_chatroom_dialog_expander_browse_activate_cb (GtkWidget               *widget,
662                                                  EmpathyNewChatroomDialog *dialog)
663 {
664         gboolean expanded;
665
666         expanded = gtk_expander_get_expanded (GTK_EXPANDER (widget));
667         if (expanded) {
668                 new_chatroom_dialog_browse_stop (dialog);
669                 gtk_window_set_resizable (GTK_WINDOW (dialog->window), FALSE);
670         } else {
671                 gtk_widget_hide (dialog->viewport_error);
672                 gtk_widget_set_sensitive (dialog->treeview, TRUE);
673                 new_chatroom_dialog_browse_start (dialog);
674                 gtk_window_set_resizable (GTK_WINDOW (dialog->window), TRUE);
675         }
676 }
677
678 static gboolean
679 new_chatroom_dialog_entry_server_focus_out_cb (GtkWidget               *widget,
680                                                GdkEventFocus           *event,
681                                                EmpathyNewChatroomDialog *dialog)
682 {
683         gboolean expanded;
684
685         expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
686         if (expanded) {
687                 new_chatroom_dialog_browse_start (dialog);
688         }
689         return FALSE;
690 }