]> git.0d.be Git - empathy.git/blob - src/empathy-new-chatroom-dialog.c
Merge remote branch 'glassrose/Display-typing-icon-in-muc-contacts-list-609419'
[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-dispatcher.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         EmpathyAccountChooser *account_chooser;
390         const gchar           *protocol;
391
392         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
393
394         if (dialog->account == NULL)
395                 return;
396
397         protocol = tp_account_get_protocol (dialog->account);
398
399         gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), "");
400
401         /* hardcode here known protocols */
402         if (strcmp (protocol, "jabber") == 0) {
403                 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
404         }
405         else if (strcmp (protocol, "local-xmpp") == 0) {
406                 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
407         }
408         else if (strcmp (protocol, "irc") == 0) {
409                 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
410         }
411         else {
412                 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
413         }
414
415         update_join_button_sensitivity (dialog);
416
417         /* Final set up of the dialog */
418         gtk_widget_grab_focus (dialog->entry_room);
419 }
420
421 static void
422 account_status_changed_cb (TpAccount *account,
423                             guint old_status,
424                             guint new_status,
425                             guint reason,
426                             gchar *dbus_error_name,
427                             GHashTable *details,
428                             EmpathyNewChatroomDialog *self)
429 {
430         update_join_button_sensitivity (self);
431 }
432
433 static void
434 new_chatroom_dialog_account_changed_cb (GtkComboBox             *combobox,
435                                         EmpathyNewChatroomDialog *dialog)
436 {
437         EmpathyAccountChooser *account_chooser;
438         gboolean               listing = FALSE;
439         gboolean               expanded = FALSE;
440         TpConnection          *connection;
441         EmpathyDispatcher     *dispatcher;
442         GList                 *classes = NULL;
443
444         if (dialog->room_list) {
445                 g_object_unref (dialog->room_list);
446                 dialog->room_list = NULL;
447         }
448
449         gtk_spinner_stop (GTK_SPINNER (dialog->throbber));
450         gtk_widget_hide (dialog->throbber);
451         new_chatroom_dialog_model_clear (dialog);
452
453         if (dialog->account != NULL) {
454                 g_signal_handler_disconnect (dialog->account, dialog->status_changed_id);
455                 g_object_unref (dialog->account);
456         }
457
458         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
459         dialog->account = empathy_account_chooser_dup_account (account_chooser);
460         connection = empathy_account_chooser_get_connection (account_chooser);
461         if (dialog->account == NULL)
462                 goto out;
463
464         dialog->status_changed_id = g_signal_connect (dialog->account,
465                       "status-changed", G_CALLBACK (account_status_changed_cb), dialog);
466
467         dispatcher = empathy_dispatcher_dup_singleton ();
468
469         if (connection != NULL) {
470                 classes = empathy_dispatcher_find_requestable_channel_classes (dispatcher,
471                         connection, TP_IFACE_CHANNEL_TYPE_ROOM_LIST,
472                         TP_HANDLE_TYPE_NONE, NULL);
473         }
474
475         if (classes != NULL) {
476                 /* Roomlist channels are supported */
477                 dialog->room_list = empathy_tp_roomlist_new (dialog->account);
478                 g_list_free (classes);
479         }
480         else {
481                 dialog->room_list = NULL;
482         }
483
484         g_object_unref (dispatcher);
485
486         if (dialog->room_list) {
487                 g_signal_connect (dialog->room_list, "destroy",
488                                   G_CALLBACK (new_chatroom_dialog_roomlist_destroy_cb),
489                                   dialog);
490                 g_signal_connect (dialog->room_list, "new-room",
491                                   G_CALLBACK (new_chatroom_dialog_new_room_cb),
492                                   dialog);
493                 g_signal_connect (dialog->room_list, "notify::is-listing",
494                                   G_CALLBACK (new_chatroom_dialog_listing_cb),
495                                   dialog);
496                 g_signal_connect (dialog->room_list, "error::start",
497                                   G_CALLBACK (start_listing_error_cb),
498                                   dialog);
499                 g_signal_connect (dialog->room_list, "error::stop",
500                                   G_CALLBACK (stop_listing_error_cb),
501                                   dialog);
502
503                 expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
504                 if (expanded) {
505                         gtk_widget_hide (dialog->viewport_error);
506                         gtk_widget_set_sensitive (dialog->treeview, TRUE);
507                         new_chatroom_dialog_browse_start (dialog);
508                 }
509
510                 listing = empathy_tp_roomlist_is_listing (dialog->room_list);
511                 if (listing) {
512                         gtk_spinner_start (GTK_SPINNER (dialog->throbber));
513                         gtk_widget_show (dialog->throbber);
514                 }
515         }
516
517         gtk_widget_set_sensitive (dialog->expander_browse, dialog->room_list != NULL);
518
519 out:
520         new_chatroom_dialog_update_widgets (dialog);
521 }
522
523 static void
524 new_chatroom_dialog_button_close_error_clicked_cb   (GtkButton                *button,
525                                                      EmpathyNewChatroomDialog *dialog)
526 {
527         gtk_widget_hide (dialog->viewport_error);
528 }
529
530 static void
531 new_chatroom_dialog_roomlist_destroy_cb (EmpathyTpRoomlist        *room_list,
532                                          EmpathyNewChatroomDialog *dialog)
533 {
534         g_object_unref (dialog->room_list);
535         dialog->room_list = NULL;
536 }
537
538 static void
539 new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist        *room_list,
540                                  EmpathyChatroom          *chatroom,
541                                  EmpathyNewChatroomDialog *dialog)
542 {
543         GtkTreeView      *view;
544         GtkTreeSelection *selection;
545         GtkListStore     *store;
546         GtkTreeIter       iter;
547         gchar            *members;
548         gchar            *tooltip;
549         const gchar      *need_password;
550         const gchar      *invite_only;
551         gchar            *tmp;
552
553         DEBUG ("New chatroom listed: %s (%s)",
554                 empathy_chatroom_get_name (chatroom),
555                 empathy_chatroom_get_room (chatroom));
556
557         /* Add to model */
558         view = GTK_TREE_VIEW (dialog->treeview);
559         selection = gtk_tree_view_get_selection (view);
560         store = GTK_LIST_STORE (dialog->model);
561         members = g_strdup_printf ("%d", empathy_chatroom_get_members_count (chatroom));
562         tmp = g_strdup_printf ("<b>%s</b>", empathy_chatroom_get_name (chatroom));
563         /* Translators: Room/Join's roomlist tooltip. Parameters are a channel name,
564         yes/no, yes/no and a number. */
565         tooltip = g_strdup_printf (_("%s\nInvite required: %s\nPassword required: %s\nMembers: %s"),
566                 tmp,
567                 empathy_chatroom_get_invite_only (chatroom) ? _("Yes") : _("No"),
568                 empathy_chatroom_get_need_password (chatroom) ? _("Yes") : _("No"),
569                 members);
570         g_free (tmp);
571         invite_only = (empathy_chatroom_get_invite_only (chatroom) ?
572                 GTK_STOCK_INDEX : NULL);
573         need_password = (empathy_chatroom_get_need_password (chatroom) ?
574                 GTK_STOCK_DIALOG_AUTHENTICATION : NULL);
575
576         gtk_list_store_append (store, &iter);
577         gtk_list_store_set (store, &iter,
578                             COL_NEED_PASSWORD, need_password,
579                             COL_INVITE_ONLY, invite_only,
580                             COL_NAME, empathy_chatroom_get_name (chatroom),
581                             COL_ROOM, empathy_chatroom_get_room (chatroom),
582                             COL_MEMBERS, members,
583                             COL_MEMBERS_INT, empathy_chatroom_get_members_count (chatroom),
584                             COL_TOOLTIP, tooltip,
585                             -1);
586
587         g_free (members);
588         g_free (tooltip);
589 }
590
591 static void
592 start_listing_error_cb (EmpathyTpRoomlist        *room_list,
593                         GError                   *error,
594                         EmpathyNewChatroomDialog *dialog)
595 {
596         gtk_label_set_text (GTK_LABEL (dialog->label_error_message), _("Could not start room listing"));
597         gtk_widget_show_all (dialog->viewport_error);
598         gtk_widget_set_sensitive (dialog->treeview, FALSE);
599 }
600
601 static void
602 stop_listing_error_cb (EmpathyTpRoomlist        *room_list,
603                        GError                   *error,
604                        EmpathyNewChatroomDialog *dialog)
605 {
606         gtk_label_set_text (GTK_LABEL (dialog->label_error_message), _("Could not stop room listing"));
607         gtk_widget_show_all (dialog->viewport_error);
608         gtk_widget_set_sensitive (dialog->treeview, FALSE);
609 }
610
611 static void
612 new_chatroom_dialog_listing_cb (EmpathyTpRoomlist        *room_list,
613                                 gpointer                  unused,
614                                 EmpathyNewChatroomDialog *dialog)
615 {
616         gboolean listing;
617
618         listing = empathy_tp_roomlist_is_listing (room_list);
619
620         /* Update the throbber */
621         if (listing) {
622                 gtk_spinner_start (GTK_SPINNER (dialog->throbber));
623                 gtk_widget_show (dialog->throbber);
624         } else {
625                 gtk_spinner_stop (GTK_SPINNER (dialog->throbber));
626                 gtk_widget_hide (dialog->throbber);
627         }
628 }
629
630 static void
631 new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog)
632 {
633         GtkListStore *store;
634
635         store = GTK_LIST_STORE (dialog->model);
636         gtk_list_store_clear (store);
637 }
638
639 static void
640 new_chatroom_dialog_model_row_activated_cb (GtkTreeView             *tree_view,
641                                             GtkTreePath             *path,
642                                             GtkTreeViewColumn       *column,
643                                             EmpathyNewChatroomDialog *dialog)
644 {
645         gtk_widget_activate (dialog->button_join);
646 }
647
648 static void
649 new_chatroom_dialog_model_selection_changed (GtkTreeSelection         *selection,
650                                              EmpathyNewChatroomDialog *dialog)
651 {
652         GtkTreeModel *model;
653         GtkTreeIter   iter;
654         gchar        *room = NULL;
655         gchar        *server = NULL;
656
657         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
658                 return;
659         }
660
661         gtk_tree_model_get (model, &iter, COL_ROOM, &room, -1);
662         server = strstr (room, "@");
663         if (server) {
664                 *server = '\0';
665                 server++;
666         }
667
668         gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), server ? server : "");
669         gtk_entry_set_text (GTK_ENTRY (dialog->entry_room), room ? room : "");
670
671         g_free (room);
672 }
673
674 static void
675 new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog)
676 {
677         EmpathyAccountChooser *account_chooser;
678         TpAccount *account;
679         const gchar           *room;
680         const gchar           *server = NULL;
681         gchar                 *room_name = NULL;
682
683         room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
684         server = gtk_entry_get_text (GTK_ENTRY (dialog->entry_server));
685
686         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
687         account = empathy_account_chooser_get_account (account_chooser);
688
689         if (!EMP_STR_EMPTY (server)) {
690                 room_name = g_strconcat (room, "@", server, NULL);
691         } else {
692                 room_name = g_strdup (room);
693         }
694
695         g_strstrip (room_name);
696
697         DEBUG ("Requesting channel for '%s'", room_name);
698         empathy_dispatcher_join_muc (account, room_name,
699                 gtk_get_current_event_time ());
700
701         g_free (room_name);
702 }
703
704 static void
705 new_chatroom_dialog_entry_changed_cb (GtkWidget                *entry,
706                                       EmpathyNewChatroomDialog *dialog)
707 {
708         if (entry == dialog->entry_room) {
709                 update_join_button_sensitivity (dialog);
710
711                 /* FIXME: Select the room in the list */
712         }
713 }
714
715 static void
716 new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog)
717 {
718         new_chatroom_dialog_model_clear (dialog);
719         if (dialog->room_list) {
720                 empathy_tp_roomlist_start (dialog->room_list);
721         }
722 }
723
724 static void
725 new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog)
726 {
727         if (dialog->room_list) {
728                 empathy_tp_roomlist_stop (dialog->room_list);
729         }
730 }
731
732 static void
733 new_chatroom_dialog_entry_server_activate_cb (GtkWidget                *widget,
734                                               EmpathyNewChatroomDialog  *dialog)
735 {
736         new_chatroom_dialog_browse_start (dialog);
737 }
738
739 static void
740 new_chatroom_dialog_expander_browse_activate_cb (GtkWidget               *widget,
741                                                  EmpathyNewChatroomDialog *dialog)
742 {
743         gboolean expanded;
744
745         expanded = gtk_expander_get_expanded (GTK_EXPANDER (widget));
746         if (expanded) {
747                 new_chatroom_dialog_browse_stop (dialog);
748                 gtk_window_set_resizable (GTK_WINDOW (dialog->window), FALSE);
749         } else {
750                 gtk_widget_hide (dialog->viewport_error);
751                 gtk_widget_set_sensitive (dialog->treeview, TRUE);
752                 new_chatroom_dialog_browse_start (dialog);
753                 gtk_window_set_resizable (GTK_WINDOW (dialog->window), TRUE);
754         }
755 }
756
757 static gboolean
758 new_chatroom_dialog_entry_server_focus_out_cb (GtkWidget               *widget,
759                                                GdkEventFocus           *event,
760                                                EmpathyNewChatroomDialog *dialog)
761 {
762         gboolean expanded;
763
764         expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
765         if (expanded) {
766                 new_chatroom_dialog_browse_start (dialog);
767         }
768         return FALSE;
769 }