]> git.0d.be Git - empathy.git/blob - src/empathy-new-chatroom-dialog.c
Merge remote-tracking branch 'pochu/upgrade-software'
[empathy.git] / src / empathy-new-chatroom-dialog.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2006-2007 Imendio AB
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA  02110-1301  USA
20  *
21  * Authors: Martyn Russell <martyn@imendio.com>
22  *          Xavier Claessens <xclaesse@gmail.com>
23  */
24
25 #include <config.h>
26
27 #include <string.h>
28
29 #include <gtk/gtk.h>
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <glib/gprintf.h>
33
34 #include <telepathy-glib/interfaces.h>
35
36 #include <libempathy/empathy-tp-roomlist.h>
37 #include <libempathy/empathy-chatroom.h>
38 #include <libempathy/empathy-utils.h>
39 #include <libempathy/empathy-request-util.h>
40 #include <libempathy/empathy-gsettings.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_grid;
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
76         GSettings         *gsettings;
77 } EmpathyNewChatroomDialog;
78
79 enum {
80         COL_NEED_PASSWORD,
81         COL_INVITE_ONLY,
82         COL_NAME,
83         COL_ROOM,
84         COL_MEMBERS,
85         COL_MEMBERS_INT,
86         COL_TOOLTIP,
87         COL_COUNT
88 };
89
90 static void     new_chatroom_dialog_response_cb                     (GtkWidget               *widget,
91                                                                      gint                     response,
92                                                                      EmpathyNewChatroomDialog *dialog);
93 static void     new_chatroom_dialog_destroy_cb                      (GtkWidget               *widget,
94                                                                      EmpathyNewChatroomDialog *dialog);
95 static void     new_chatroom_dialog_model_setup                     (EmpathyNewChatroomDialog *dialog);
96 static void     new_chatroom_dialog_model_add_columns               (EmpathyNewChatroomDialog *dialog);
97 static void     new_chatroom_dialog_update_widgets                  (EmpathyNewChatroomDialog *dialog);
98 static void     new_chatroom_dialog_account_changed_cb              (GtkComboBox              *combobox,
99                                                                      EmpathyNewChatroomDialog *dialog);
100 static void     new_chatroom_dialog_account_ready_cb                (EmpathyAccountChooser   *combobox,
101                                                                      EmpathyNewChatroomDialog  *dialog);
102 static void     new_chatroom_dialog_roomlist_destroy_cb             (EmpathyTpRoomlist        *room_list,
103                                                                      EmpathyNewChatroomDialog *dialog);
104 static void     new_chatroom_dialog_new_room_cb                     (EmpathyTpRoomlist        *room_list,
105                                                                      EmpathyChatroom          *chatroom,
106                                                                      EmpathyNewChatroomDialog *dialog);
107 static void     new_chatroom_dialog_listing_cb                      (EmpathyTpRoomlist        *room_list,
108                                                                      gpointer                  unused,
109                                                                      EmpathyNewChatroomDialog *dialog);
110 static void     start_listing_error_cb                              (EmpathyTpRoomlist        *room_list,
111                                                                      GError                   *error,
112                                                                      EmpathyNewChatroomDialog *dialog);
113 static void     stop_listing_error_cb                               (EmpathyTpRoomlist        *room_list,
114                                                                      GError                   *error,
115                                                                      EmpathyNewChatroomDialog *dialog);
116 static void     new_chatroom_dialog_model_clear                     (EmpathyNewChatroomDialog *dialog);
117 static void     new_chatroom_dialog_model_row_activated_cb          (GtkTreeView             *tree_view,
118                                                                      GtkTreePath             *path,
119                                                                      GtkTreeViewColumn       *column,
120                                                                      EmpathyNewChatroomDialog *dialog);
121 static void     new_chatroom_dialog_model_selection_changed         (GtkTreeSelection        *selection,
122                                                                      EmpathyNewChatroomDialog *dialog);
123 static void     new_chatroom_dialog_join                            (EmpathyNewChatroomDialog *dialog);
124 static void     new_chatroom_dialog_entry_changed_cb                (GtkWidget               *entry,
125                                                                      EmpathyNewChatroomDialog *dialog);
126 static void     new_chatroom_dialog_browse_start                    (EmpathyNewChatroomDialog *dialog);
127 static void     new_chatroom_dialog_browse_stop                     (EmpathyNewChatroomDialog *dialog);
128 static void     new_chatroom_dialog_entry_server_activate_cb        (GtkWidget               *widget,
129                                                                      EmpathyNewChatroomDialog *dialog);
130 static void     new_chatroom_dialog_expander_browse_activate_cb     (GtkWidget               *widget,
131                                                                      EmpathyNewChatroomDialog *dialog);
132 static gboolean new_chatroom_dialog_entry_server_focus_out_cb       (GtkWidget               *widget,
133                                                                      GdkEventFocus           *event,
134                                                                      EmpathyNewChatroomDialog *dialog);
135 static void     new_chatroom_dialog_button_close_error_clicked_cb   (GtkButton                *button,
136                                                                      EmpathyNewChatroomDialog *dialog);
137
138 static EmpathyNewChatroomDialog *dialog_p = NULL;
139
140
141 void
142 empathy_new_chatroom_dialog_show (GtkWindow *parent)
143 {
144         EmpathyNewChatroomDialog *dialog;
145         GtkBuilder               *gui;
146         GtkSizeGroup             *size_group;
147         gchar                    *filename;
148
149         if (dialog_p) {
150                 gtk_window_present (GTK_WINDOW (dialog_p->window));
151                 return;
152         }
153
154         dialog_p = dialog = g_new0 (EmpathyNewChatroomDialog, 1);
155
156         filename = empathy_file_lookup ("empathy-new-chatroom-dialog.ui", "src");
157         gui = empathy_builder_get_file (filename,
158                                        "new_chatroom_dialog", &dialog->window,
159                                        "table_grid", &dialog->table_grid,
160                                        "label_account", &dialog->label_account,
161                                        "label_server", &dialog->label_server,
162                                        "label_room", &dialog->label_room,
163                                        "entry_server", &dialog->entry_server,
164                                        "entry_room", &dialog->entry_room,
165                                        "treeview", &dialog->treeview,
166                                        "button_join", &dialog->button_join,
167                                        "expander_browse", &dialog->expander_browse,
168                                        "hbox_expander", &dialog->hbox_expander,
169                                        "label_error_message", &dialog->label_error_message,
170                                        "viewport_error", &dialog->viewport_error,
171                                        NULL);
172         g_free (filename);
173
174         empathy_builder_connect (gui, dialog,
175                               "new_chatroom_dialog", "response", new_chatroom_dialog_response_cb,
176                               "new_chatroom_dialog", "destroy", new_chatroom_dialog_destroy_cb,
177                               "entry_server", "changed", new_chatroom_dialog_entry_changed_cb,
178                               "entry_server", "activate", new_chatroom_dialog_entry_server_activate_cb,
179                               "entry_server", "focus-out-event", new_chatroom_dialog_entry_server_focus_out_cb,
180                               "entry_room", "changed", new_chatroom_dialog_entry_changed_cb,
181                               "expander_browse", "activate", new_chatroom_dialog_expander_browse_activate_cb,
182                               "button_close_error", "clicked", new_chatroom_dialog_button_close_error_clicked_cb,
183                               NULL);
184
185         g_object_unref (gui);
186
187         g_object_add_weak_pointer (G_OBJECT (dialog->window), (gpointer) &dialog_p);
188
189         /* Label alignment */
190         size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
191
192         gtk_size_group_add_widget (size_group, dialog->label_account);
193         gtk_size_group_add_widget (size_group, dialog->label_server);
194         gtk_size_group_add_widget (size_group, dialog->label_room);
195
196         g_object_unref (size_group);
197
198         /* Set up chatrooms treeview */
199         new_chatroom_dialog_model_setup (dialog);
200
201         /* Add throbber */
202         dialog->throbber = gtk_spinner_new ();
203         gtk_box_pack_start (GTK_BOX (dialog->hbox_expander), dialog->throbber,
204                 TRUE, TRUE, 0);
205
206         dialog->gsettings = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
207
208         /* Account chooser for custom */
209         dialog->account_chooser = empathy_account_chooser_new ();
210         empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser),
211                                             empathy_account_chooser_filter_supports_chatrooms,
212                                             NULL);
213         gtk_grid_attach (GTK_GRID (dialog->table_grid),
214                                    dialog->account_chooser,
215                                    1, 0, 1, 1);
216         gtk_widget_show (dialog->account_chooser);
217
218         g_signal_connect (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser), "ready",
219                           G_CALLBACK (new_chatroom_dialog_account_ready_cb),
220                           dialog);
221         g_signal_connect (GTK_COMBO_BOX (dialog->account_chooser), "changed",
222                           G_CALLBACK (new_chatroom_dialog_account_changed_cb),
223                           dialog);
224         new_chatroom_dialog_account_changed_cb (GTK_COMBO_BOX (dialog->account_chooser),
225                                                 dialog);
226
227         if (parent) {
228                 gtk_window_set_transient_for (GTK_WINDOW (dialog->window),
229                                               GTK_WINDOW (parent));
230         }
231
232         gtk_widget_show (dialog->window);
233 }
234
235 static void
236 new_chatroom_dialog_store_last_account (GSettings             *gsettings,
237                                         EmpathyAccountChooser *account_chooser)
238 {
239         TpAccount   *account;
240         const char *account_path;
241
242         account = empathy_account_chooser_get_account (account_chooser);
243         if (account == NULL)
244                 return;
245
246         account_path = tp_proxy_get_object_path (account);
247         DEBUG ("Storing account path '%s'", account_path);
248
249         g_settings_set (gsettings, EMPATHY_PREFS_CHAT_ROOM_LAST_ACCOUNT,
250                         "o", account_path);
251 }
252
253 static void
254 new_chatroom_dialog_response_cb (GtkWidget               *widget,
255                                  gint                     response,
256                                  EmpathyNewChatroomDialog *dialog)
257 {
258         if (response == GTK_RESPONSE_OK) {
259                 new_chatroom_dialog_join (dialog);
260                 new_chatroom_dialog_store_last_account (dialog->gsettings,
261                                                         EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser));
262         }
263
264         gtk_widget_destroy (widget);
265 }
266
267 static void
268 new_chatroom_dialog_destroy_cb (GtkWidget               *widget,
269                                 EmpathyNewChatroomDialog *dialog)
270 {
271         if (dialog->room_list) {
272                 g_object_unref (dialog->room_list);
273         }
274         g_object_unref (dialog->model);
275
276         if (dialog->account != NULL) {
277                 g_signal_handler_disconnect (dialog->account, dialog->status_changed_id);
278                 g_object_unref (dialog->account);
279         }
280
281         g_clear_object (&dialog->gsettings);
282
283         g_free (dialog);
284 }
285
286 static void
287 new_chatroom_dialog_model_setup (EmpathyNewChatroomDialog *dialog)
288 {
289         GtkTreeView      *view;
290         GtkListStore     *store;
291         GtkTreeSelection *selection;
292
293         /* View */
294         view = GTK_TREE_VIEW (dialog->treeview);
295
296         g_signal_connect (view, "row-activated",
297                           G_CALLBACK (new_chatroom_dialog_model_row_activated_cb),
298                           dialog);
299
300         /* Store/Model */
301         store = gtk_list_store_new (COL_COUNT,
302                                     G_TYPE_STRING,       /* Invite */
303                                     G_TYPE_STRING,       /* Password */
304                                     G_TYPE_STRING,       /* Name */
305                                     G_TYPE_STRING,       /* Room */
306                                     G_TYPE_STRING,       /* Member count */
307                                     G_TYPE_INT,          /* Member count int */
308                                     G_TYPE_STRING);      /* Tool tip */
309
310         dialog->model = GTK_TREE_MODEL (store);
311         gtk_tree_view_set_model (view, dialog->model);
312         gtk_tree_view_set_tooltip_column (view, COL_TOOLTIP);
313         gtk_tree_view_set_search_column (view, COL_NAME);
314
315         /* Selection */
316         selection = gtk_tree_view_get_selection (view);
317         gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store),
318                                               COL_NAME, GTK_SORT_ASCENDING);
319
320         g_signal_connect (selection, "changed",
321                           G_CALLBACK (new_chatroom_dialog_model_selection_changed),
322                           dialog);
323
324         /* Columns */
325         new_chatroom_dialog_model_add_columns (dialog);
326 }
327
328 static void
329 new_chatroom_dialog_model_add_columns (EmpathyNewChatroomDialog *dialog)
330 {
331         GtkTreeView       *view;
332         GtkTreeViewColumn *column;
333         GtkCellRenderer   *cell;
334         gint               width, height;
335
336         gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &width, &height);
337
338         view = GTK_TREE_VIEW (dialog->treeview);
339
340         cell = gtk_cell_renderer_pixbuf_new ();
341         g_object_set (cell,
342                       "width", width,
343                       "height", height,
344                       "stock-size", GTK_ICON_SIZE_MENU,
345                       NULL);
346         column = gtk_tree_view_column_new_with_attributes (NULL,
347                                                            cell,
348                                                            "stock-id", COL_INVITE_ONLY,
349                                                            NULL);
350
351         gtk_tree_view_column_set_sort_column_id (column, COL_INVITE_ONLY);
352         gtk_tree_view_append_column (view, column);
353
354         column = gtk_tree_view_column_new_with_attributes (NULL,
355                                                            cell,
356                                                            "stock-id", COL_NEED_PASSWORD,
357                                                            NULL);
358
359         gtk_tree_view_column_set_sort_column_id (column, COL_NEED_PASSWORD);
360         gtk_tree_view_append_column (view, column);
361
362         cell = gtk_cell_renderer_text_new ();
363         g_object_set (cell,
364                       "xpad", (guint) 4,
365                       "ypad", (guint) 2,
366                       "ellipsize", PANGO_ELLIPSIZE_END,
367                       NULL);
368
369         column = gtk_tree_view_column_new_with_attributes (_("Chat Room"),
370                                                            cell,
371                                                            "text", COL_NAME,
372                                                            NULL);
373
374         gtk_tree_view_column_set_sort_column_id (column, COL_NAME);
375         gtk_tree_view_column_set_expand (column, TRUE);
376         gtk_tree_view_append_column (view, column);
377
378         cell = gtk_cell_renderer_text_new ();
379         g_object_set (cell,
380                       "xpad", (guint) 4,
381                       "ypad", (guint) 2,
382                       "ellipsize", PANGO_ELLIPSIZE_END,
383                       "alignment", PANGO_ALIGN_RIGHT,
384                       NULL);
385         column = gtk_tree_view_column_new_with_attributes (_("Members"),
386                                                            cell,
387                                                            "text", COL_MEMBERS,
388                                                            NULL);
389
390         gtk_tree_view_column_set_sort_column_id (column, COL_MEMBERS_INT);
391         gtk_tree_view_append_column (view, column);
392 }
393
394 static void
395 update_join_button_sensitivity (EmpathyNewChatroomDialog *dialog)
396 {
397         const gchar           *room;
398         gboolean               sensitive = FALSE;
399
400
401         room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
402         if (EMP_STR_EMPTY (room))
403                 goto out;
404
405         if (dialog->account == NULL)
406                 goto out;
407
408         if (tp_account_get_connection_status (dialog->account, NULL) !=
409                       TP_CONNECTION_STATUS_CONNECTED)
410                 goto out;
411
412         sensitive = TRUE;
413
414 out:
415         gtk_widget_set_sensitive (dialog->button_join, sensitive);
416 }
417
418 static void
419 new_chatroom_dialog_update_widgets (EmpathyNewChatroomDialog *dialog)
420 {
421         const gchar           *protocol;
422
423         if (dialog->account == NULL)
424                 return;
425
426         protocol = tp_account_get_protocol (dialog->account);
427
428         gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), "");
429
430         /* hardcode here known protocols */
431         if (strcmp (protocol, "jabber") == 0) {
432                 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
433         }
434         else if (strcmp (protocol, "local-xmpp") == 0) {
435                 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
436         }
437         else if (strcmp (protocol, "irc") == 0) {
438                 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
439         }
440         else {
441                 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
442         }
443
444         update_join_button_sensitivity (dialog);
445
446         /* Final set up of the dialog */
447         gtk_widget_grab_focus (dialog->entry_room);
448 }
449
450 static void
451 account_status_changed_cb (TpAccount *account,
452                             guint old_status,
453                             guint new_status,
454                             guint reason,
455                             gchar *dbus_error_name,
456                             GHashTable *details,
457                             EmpathyNewChatroomDialog *self)
458 {
459         update_join_button_sensitivity (self);
460 }
461
462 static void
463 new_chatroom_dialog_select_last_account (GSettings             *gsettings,
464                                          EmpathyAccountChooser *account_chooser)
465 {
466         const gchar          *account_path;
467         TpAccountManager      *manager;
468         TpSimpleClientFactory *factory;
469         TpAccount             *account;
470         TpConnectionStatus    status;
471
472         account_path = g_settings_get_string (gsettings, EMPATHY_PREFS_CHAT_ROOM_LAST_ACCOUNT);
473         DEBUG ("Selecting account path '%s'", account_path);
474
475         manager =  tp_account_manager_dup ();
476         factory = tp_proxy_get_factory (manager);
477         account = tp_simple_client_factory_ensure_account (factory,
478                                                            account_path,
479                                                            NULL,
480                                                            NULL);
481
482         if (account != NULL) {
483                 status = tp_account_get_connection_status (account, NULL);
484                 if (status == TP_CONNECTION_STATUS_CONNECTED) {
485                         empathy_account_chooser_set_account (account_chooser,
486                                                              account);
487                 }
488                 g_object_unref (account);
489         }
490         g_object_unref (manager);
491 }
492
493 static void
494 new_chatroom_dialog_account_ready_cb (EmpathyAccountChooser    *combobox,
495                                       EmpathyNewChatroomDialog *dialog)
496 {
497         new_chatroom_dialog_select_last_account (dialog->gsettings, combobox);
498 }
499
500 static void
501 new_chatroom_dialog_account_changed_cb (GtkComboBox             *combobox,
502                                         EmpathyNewChatroomDialog *dialog)
503 {
504         EmpathyAccountChooser *account_chooser;
505         gboolean               listing = FALSE;
506         gboolean               expanded = FALSE;
507         TpConnection          *connection;
508         TpCapabilities *caps;
509
510         if (dialog->room_list) {
511                 g_object_unref (dialog->room_list);
512                 dialog->room_list = NULL;
513         }
514
515         gtk_spinner_stop (GTK_SPINNER (dialog->throbber));
516         gtk_widget_hide (dialog->throbber);
517         new_chatroom_dialog_model_clear (dialog);
518
519         if (dialog->account != NULL) {
520                 g_signal_handler_disconnect (dialog->account, dialog->status_changed_id);
521                 g_object_unref (dialog->account);
522         }
523
524         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
525         dialog->account = empathy_account_chooser_dup_account (account_chooser);
526         connection = empathy_account_chooser_get_connection (account_chooser);
527         if (dialog->account == NULL)
528                 goto out;
529
530         dialog->status_changed_id = g_signal_connect (dialog->account,
531                       "status-changed", G_CALLBACK (account_status_changed_cb), dialog);
532
533         /* empathy_account_chooser_filter_supports_chatrooms ensures that the
534         * account has a connection and CAPABILITIES has been prepared. */
535         g_assert (connection != NULL);
536         g_assert (tp_proxy_is_prepared (connection,
537                 TP_CONNECTION_FEATURE_CAPABILITIES));
538         caps = tp_connection_get_capabilities (connection);
539
540         if (tp_capabilities_supports_room_list (caps, NULL)) {
541                 /* Roomlist channels are supported */
542                 dialog->room_list = empathy_tp_roomlist_new (dialog->account);
543         }
544         else {
545                 dialog->room_list = NULL;
546         }
547
548         if (dialog->room_list) {
549                 g_signal_connect (dialog->room_list, "destroy",
550                                   G_CALLBACK (new_chatroom_dialog_roomlist_destroy_cb),
551                                   dialog);
552                 g_signal_connect (dialog->room_list, "new-room",
553                                   G_CALLBACK (new_chatroom_dialog_new_room_cb),
554                                   dialog);
555                 g_signal_connect (dialog->room_list, "notify::is-listing",
556                                   G_CALLBACK (new_chatroom_dialog_listing_cb),
557                                   dialog);
558                 g_signal_connect (dialog->room_list, "error::start",
559                                   G_CALLBACK (start_listing_error_cb),
560                                   dialog);
561                 g_signal_connect (dialog->room_list, "error::stop",
562                                   G_CALLBACK (stop_listing_error_cb),
563                                   dialog);
564
565                 expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
566                 if (expanded) {
567                         gtk_widget_hide (dialog->viewport_error);
568                         gtk_widget_set_sensitive (dialog->treeview, TRUE);
569                         new_chatroom_dialog_browse_start (dialog);
570                 }
571
572                 listing = empathy_tp_roomlist_is_listing (dialog->room_list);
573                 if (listing) {
574                         gtk_spinner_start (GTK_SPINNER (dialog->throbber));
575                         gtk_widget_show (dialog->throbber);
576                 }
577         }
578
579         gtk_widget_set_sensitive (dialog->expander_browse, dialog->room_list != NULL);
580
581 out:
582         new_chatroom_dialog_update_widgets (dialog);
583 }
584
585 static void
586 new_chatroom_dialog_button_close_error_clicked_cb   (GtkButton                *button,
587                                                      EmpathyNewChatroomDialog *dialog)
588 {
589         gtk_widget_hide (dialog->viewport_error);
590 }
591
592 static void
593 new_chatroom_dialog_roomlist_destroy_cb (EmpathyTpRoomlist        *room_list,
594                                          EmpathyNewChatroomDialog *dialog)
595 {
596         g_object_unref (dialog->room_list);
597         dialog->room_list = NULL;
598 }
599
600 static void
601 new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist        *room_list,
602                                  EmpathyChatroom          *chatroom,
603                                  EmpathyNewChatroomDialog *dialog)
604 {
605         GtkListStore     *store;
606         GtkTreeIter       iter;
607         gchar            *members;
608         gchar            *tooltip;
609         const gchar      *need_password;
610         const gchar      *invite_only;
611         gchar            *tmp;
612
613         DEBUG ("New chatroom listed: %s (%s)",
614                 empathy_chatroom_get_name (chatroom),
615                 empathy_chatroom_get_room (chatroom));
616
617         /* Add to model */
618         store = GTK_LIST_STORE (dialog->model);
619         members = g_strdup_printf ("%d", empathy_chatroom_get_members_count (chatroom));
620         tmp = g_strdup_printf ("<b>%s</b>", empathy_chatroom_get_name (chatroom));
621         /* Translators: Room/Join's roomlist tooltip. Parameters are a channel name,
622         yes/no, yes/no and a number. */
623         tooltip = g_strdup_printf (_("%s\nInvite required: %s\nPassword required: %s\nMembers: %s"),
624                 tmp,
625                 empathy_chatroom_get_invite_only (chatroom) ? _("Yes") : _("No"),
626                 empathy_chatroom_get_need_password (chatroom) ? _("Yes") : _("No"),
627                 members);
628         g_free (tmp);
629         invite_only = (empathy_chatroom_get_invite_only (chatroom) ?
630                 GTK_STOCK_INDEX : NULL);
631         need_password = (empathy_chatroom_get_need_password (chatroom) ?
632                 GTK_STOCK_DIALOG_AUTHENTICATION : NULL);
633
634         gtk_list_store_append (store, &iter);
635         gtk_list_store_set (store, &iter,
636                             COL_NEED_PASSWORD, need_password,
637                             COL_INVITE_ONLY, invite_only,
638                             COL_NAME, empathy_chatroom_get_name (chatroom),
639                             COL_ROOM, empathy_chatroom_get_room (chatroom),
640                             COL_MEMBERS, members,
641                             COL_MEMBERS_INT, empathy_chatroom_get_members_count (chatroom),
642                             COL_TOOLTIP, tooltip,
643                             -1);
644
645         g_free (members);
646         g_free (tooltip);
647 }
648
649 static void
650 start_listing_error_cb (EmpathyTpRoomlist        *room_list,
651                         GError                   *error,
652                         EmpathyNewChatroomDialog *dialog)
653 {
654         gtk_label_set_text (GTK_LABEL (dialog->label_error_message), _("Could not start room listing"));
655         gtk_widget_show_all (dialog->viewport_error);
656         gtk_widget_set_sensitive (dialog->treeview, FALSE);
657 }
658
659 static void
660 stop_listing_error_cb (EmpathyTpRoomlist        *room_list,
661                        GError                   *error,
662                        EmpathyNewChatroomDialog *dialog)
663 {
664         gtk_label_set_text (GTK_LABEL (dialog->label_error_message), _("Could not stop room listing"));
665         gtk_widget_show_all (dialog->viewport_error);
666         gtk_widget_set_sensitive (dialog->treeview, FALSE);
667 }
668
669 static void
670 new_chatroom_dialog_listing_cb (EmpathyTpRoomlist        *room_list,
671                                 gpointer                  unused,
672                                 EmpathyNewChatroomDialog *dialog)
673 {
674         gboolean listing;
675
676         listing = empathy_tp_roomlist_is_listing (room_list);
677
678         /* Update the throbber */
679         if (listing) {
680                 gtk_spinner_start (GTK_SPINNER (dialog->throbber));
681                 gtk_widget_show (dialog->throbber);
682         } else {
683                 gtk_spinner_stop (GTK_SPINNER (dialog->throbber));
684                 gtk_widget_hide (dialog->throbber);
685         }
686 }
687
688 static void
689 new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog)
690 {
691         GtkListStore *store;
692
693         store = GTK_LIST_STORE (dialog->model);
694         gtk_list_store_clear (store);
695 }
696
697 static void
698 new_chatroom_dialog_model_row_activated_cb (GtkTreeView             *tree_view,
699                                             GtkTreePath             *path,
700                                             GtkTreeViewColumn       *column,
701                                             EmpathyNewChatroomDialog *dialog)
702 {
703         gtk_widget_activate (dialog->button_join);
704 }
705
706 static void
707 new_chatroom_dialog_model_selection_changed (GtkTreeSelection         *selection,
708                                              EmpathyNewChatroomDialog *dialog)
709 {
710         GtkTreeModel *model;
711         GtkTreeIter   iter;
712         gchar        *room = NULL;
713         gchar        *server = NULL;
714
715         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
716                 return;
717         }
718
719         gtk_tree_model_get (model, &iter, COL_ROOM, &room, -1);
720         server = strstr (room, "@");
721         if (server) {
722                 *server = '\0';
723                 server++;
724         }
725
726         gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), server ? server : "");
727         gtk_entry_set_text (GTK_ENTRY (dialog->entry_room), room ? room : "");
728
729         g_free (room);
730 }
731
732 static void
733 new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog)
734 {
735         EmpathyAccountChooser *account_chooser;
736         TpAccount *account;
737         const gchar           *room;
738         const gchar           *server = NULL;
739         gchar                 *room_name = NULL;
740
741         room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
742         server = gtk_entry_get_text (GTK_ENTRY (dialog->entry_server));
743
744         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
745         account = empathy_account_chooser_get_account (account_chooser);
746
747         if (!EMP_STR_EMPTY (server)) {
748                 room_name = g_strconcat (room, "@", server, NULL);
749         } else {
750                 room_name = g_strdup (room);
751         }
752
753         g_strstrip (room_name);
754
755         DEBUG ("Requesting channel for '%s'", room_name);
756
757         empathy_join_muc (account, room_name, empathy_get_current_action_time ());
758
759         g_free (room_name);
760 }
761
762 static void
763 new_chatroom_dialog_entry_changed_cb (GtkWidget                *entry,
764                                       EmpathyNewChatroomDialog *dialog)
765 {
766         if (entry == dialog->entry_room) {
767                 update_join_button_sensitivity (dialog);
768
769                 /* FIXME: Select the room in the list */
770         }
771 }
772
773 static void
774 new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog)
775 {
776         new_chatroom_dialog_model_clear (dialog);
777         if (dialog->room_list) {
778                 empathy_tp_roomlist_start (dialog->room_list);
779         }
780 }
781
782 static void
783 new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog)
784 {
785         if (dialog->room_list) {
786                 empathy_tp_roomlist_stop (dialog->room_list);
787         }
788 }
789
790 static void
791 new_chatroom_dialog_entry_server_activate_cb (GtkWidget                *widget,
792                                               EmpathyNewChatroomDialog  *dialog)
793 {
794         new_chatroom_dialog_browse_start (dialog);
795 }
796
797 static void
798 new_chatroom_dialog_expander_browse_activate_cb (GtkWidget               *widget,
799                                                  EmpathyNewChatroomDialog *dialog)
800 {
801         gboolean expanded;
802
803         expanded = gtk_expander_get_expanded (GTK_EXPANDER (widget));
804         if (expanded) {
805                 new_chatroom_dialog_browse_stop (dialog);
806                 gtk_window_set_resizable (GTK_WINDOW (dialog->window), FALSE);
807         } else {
808                 gtk_widget_hide (dialog->viewport_error);
809                 gtk_widget_set_sensitive (dialog->treeview, TRUE);
810                 new_chatroom_dialog_browse_start (dialog);
811                 gtk_window_set_resizable (GTK_WINDOW (dialog->window), TRUE);
812         }
813 }
814
815 static gboolean
816 new_chatroom_dialog_entry_server_focus_out_cb (GtkWidget               *widget,
817                                                GdkEventFocus           *event,
818                                                EmpathyNewChatroomDialog *dialog)
819 {
820         gboolean expanded;
821
822         expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
823         if (expanded) {
824                 new_chatroom_dialog_browse_start (dialog);
825         }
826         return FALSE;
827 }