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