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