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