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