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