]> git.0d.be Git - empathy.git/blob - src/empathy-new-chatroom-dialog.c
Merge remote-tracking branch 'glassrose/add-All-service-selection-in-debug-window'
[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         const gchar           *protocol;
399         gboolean               sensitive = FALSE;
400
401
402         room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
403         protocol = tp_account_get_protocol (dialog->account);
404         if (EMP_STR_EMPTY (room))
405                 goto out;
406
407         if (!tp_strdiff (protocol, "irc") && (!tp_strdiff (room, "#") ||
408                                               !tp_strdiff (room, "&")))
409         {
410                 goto out;
411         }
412
413         if (dialog->account == NULL)
414                 goto out;
415
416         if (tp_account_get_connection_status (dialog->account, NULL) !=
417                       TP_CONNECTION_STATUS_CONNECTED)
418                 goto out;
419
420         sensitive = TRUE;
421
422 out:
423         gtk_widget_set_sensitive (dialog->button_join, sensitive);
424 }
425
426 static void
427 new_chatroom_dialog_update_widgets (EmpathyNewChatroomDialog *dialog)
428 {
429         const gchar           *protocol;
430
431         if (dialog->account == NULL)
432                 return;
433
434         protocol = tp_account_get_protocol (dialog->account);
435
436         gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), "");
437
438         /* hardcode here known protocols */
439         if (strcmp (protocol, "jabber") == 0) {
440                 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
441         }
442         else if (strcmp (protocol, "local-xmpp") == 0) {
443                 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
444         }
445         else if (strcmp (protocol, "irc") == 0) {
446                 gtk_widget_set_sensitive (dialog->entry_server, FALSE);
447         }
448         else {
449                 gtk_widget_set_sensitive (dialog->entry_server, TRUE);
450         }
451
452         if (!tp_strdiff (protocol, "irc"))
453                 gtk_entry_set_text (GTK_ENTRY (dialog->entry_room), "#");
454         else
455                 gtk_entry_set_text (GTK_ENTRY (dialog->entry_room), "");
456
457         update_join_button_sensitivity (dialog);
458
459         /* Final set up of the dialog */
460         gtk_widget_grab_focus (dialog->entry_room);
461         gtk_editable_set_position (GTK_EDITABLE (dialog->entry_room), -1);
462 }
463
464 static void
465 account_status_changed_cb (TpAccount *account,
466                             guint old_status,
467                             guint new_status,
468                             guint reason,
469                             gchar *dbus_error_name,
470                             GHashTable *details,
471                             EmpathyNewChatroomDialog *self)
472 {
473         update_join_button_sensitivity (self);
474 }
475
476 static void
477 new_chatroom_dialog_select_last_account (GSettings             *gsettings,
478                                          EmpathyAccountChooser *account_chooser)
479 {
480         const gchar          *account_path;
481         TpAccountManager      *manager;
482         TpSimpleClientFactory *factory;
483         TpAccount             *account;
484         TpConnectionStatus    status;
485
486         account_path = g_settings_get_string (gsettings, EMPATHY_PREFS_CHAT_ROOM_LAST_ACCOUNT);
487         DEBUG ("Selecting account path '%s'", account_path);
488
489         manager =  tp_account_manager_dup ();
490         factory = tp_proxy_get_factory (manager);
491         account = tp_simple_client_factory_ensure_account (factory,
492                                                            account_path,
493                                                            NULL,
494                                                            NULL);
495
496         if (account != NULL) {
497                 status = tp_account_get_connection_status (account, NULL);
498                 if (status == TP_CONNECTION_STATUS_CONNECTED) {
499                         empathy_account_chooser_set_account (account_chooser,
500                                                              account);
501                 }
502                 g_object_unref (account);
503         }
504         g_object_unref (manager);
505 }
506
507 static void
508 new_chatroom_dialog_account_ready_cb (EmpathyAccountChooser    *combobox,
509                                       EmpathyNewChatroomDialog *dialog)
510 {
511         new_chatroom_dialog_select_last_account (dialog->gsettings, combobox);
512 }
513
514 static void
515 new_chatroom_dialog_account_changed_cb (GtkComboBox             *combobox,
516                                         EmpathyNewChatroomDialog *dialog)
517 {
518         EmpathyAccountChooser *account_chooser;
519         gboolean               listing = FALSE;
520         gboolean               expanded = FALSE;
521         TpConnection          *connection;
522         TpCapabilities *caps;
523
524         if (dialog->room_list) {
525                 g_object_unref (dialog->room_list);
526                 dialog->room_list = NULL;
527         }
528
529         gtk_spinner_stop (GTK_SPINNER (dialog->throbber));
530         gtk_widget_hide (dialog->throbber);
531         new_chatroom_dialog_model_clear (dialog);
532
533         if (dialog->account != NULL) {
534                 g_signal_handler_disconnect (dialog->account, dialog->status_changed_id);
535                 g_object_unref (dialog->account);
536         }
537
538         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
539         dialog->account = empathy_account_chooser_dup_account (account_chooser);
540         connection = empathy_account_chooser_get_connection (account_chooser);
541         if (dialog->account == NULL)
542                 goto out;
543
544         dialog->status_changed_id = g_signal_connect (dialog->account,
545                       "status-changed", G_CALLBACK (account_status_changed_cb), dialog);
546
547         /* empathy_account_chooser_filter_supports_chatrooms ensures that the
548         * account has a connection and CAPABILITIES has been prepared. */
549         g_assert (connection != NULL);
550         g_assert (tp_proxy_is_prepared (connection,
551                 TP_CONNECTION_FEATURE_CAPABILITIES));
552         caps = tp_connection_get_capabilities (connection);
553
554         if (tp_capabilities_supports_room_list (caps, NULL)) {
555                 /* Roomlist channels are supported */
556                 dialog->room_list = empathy_tp_roomlist_new (dialog->account);
557         }
558         else {
559                 dialog->room_list = NULL;
560         }
561
562         if (dialog->room_list) {
563                 g_signal_connect (dialog->room_list, "destroy",
564                                   G_CALLBACK (new_chatroom_dialog_roomlist_destroy_cb),
565                                   dialog);
566                 g_signal_connect (dialog->room_list, "new-room",
567                                   G_CALLBACK (new_chatroom_dialog_new_room_cb),
568                                   dialog);
569                 g_signal_connect (dialog->room_list, "notify::is-listing",
570                                   G_CALLBACK (new_chatroom_dialog_listing_cb),
571                                   dialog);
572                 g_signal_connect (dialog->room_list, "error::start",
573                                   G_CALLBACK (start_listing_error_cb),
574                                   dialog);
575                 g_signal_connect (dialog->room_list, "error::stop",
576                                   G_CALLBACK (stop_listing_error_cb),
577                                   dialog);
578
579                 expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
580                 if (expanded) {
581                         gtk_widget_hide (dialog->viewport_error);
582                         gtk_widget_set_sensitive (dialog->treeview, TRUE);
583                         new_chatroom_dialog_browse_start (dialog);
584                 }
585
586                 listing = empathy_tp_roomlist_is_listing (dialog->room_list);
587                 if (listing) {
588                         gtk_spinner_start (GTK_SPINNER (dialog->throbber));
589                         gtk_widget_show (dialog->throbber);
590                 }
591         }
592
593         gtk_widget_set_sensitive (dialog->expander_browse, dialog->room_list != NULL);
594
595 out:
596         new_chatroom_dialog_update_widgets (dialog);
597 }
598
599 static void
600 new_chatroom_dialog_button_close_error_clicked_cb   (GtkButton                *button,
601                                                      EmpathyNewChatroomDialog *dialog)
602 {
603         gtk_widget_hide (dialog->viewport_error);
604 }
605
606 static void
607 new_chatroom_dialog_roomlist_destroy_cb (EmpathyTpRoomlist        *room_list,
608                                          EmpathyNewChatroomDialog *dialog)
609 {
610         g_object_unref (dialog->room_list);
611         dialog->room_list = NULL;
612 }
613
614 static void
615 new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist        *room_list,
616                                  EmpathyChatroom          *chatroom,
617                                  EmpathyNewChatroomDialog *dialog)
618 {
619         GtkListStore     *store;
620         GtkTreeIter       iter;
621         gchar            *members;
622         gchar            *tooltip;
623         const gchar      *need_password;
624         const gchar      *invite_only;
625         gchar            *tmp;
626
627         DEBUG ("New chatroom listed: %s (%s)",
628                 empathy_chatroom_get_name (chatroom),
629                 empathy_chatroom_get_room (chatroom));
630
631         /* Add to model */
632         store = GTK_LIST_STORE (dialog->model);
633         members = g_strdup_printf ("%d", empathy_chatroom_get_members_count (chatroom));
634         tmp = g_strdup_printf ("<b>%s</b>", empathy_chatroom_get_name (chatroom));
635         /* Translators: Room/Join's roomlist tooltip. Parameters are a channel name,
636         yes/no, yes/no and a number. */
637         tooltip = g_strdup_printf (_("%s\nInvite required: %s\nPassword required: %s\nMembers: %s"),
638                 tmp,
639                 empathy_chatroom_get_invite_only (chatroom) ? _("Yes") : _("No"),
640                 empathy_chatroom_get_need_password (chatroom) ? _("Yes") : _("No"),
641                 members);
642         g_free (tmp);
643         invite_only = (empathy_chatroom_get_invite_only (chatroom) ?
644                 GTK_STOCK_INDEX : NULL);
645         need_password = (empathy_chatroom_get_need_password (chatroom) ?
646                 GTK_STOCK_DIALOG_AUTHENTICATION : NULL);
647
648         gtk_list_store_append (store, &iter);
649         gtk_list_store_set (store, &iter,
650                             COL_NEED_PASSWORD, need_password,
651                             COL_INVITE_ONLY, invite_only,
652                             COL_NAME, empathy_chatroom_get_name (chatroom),
653                             COL_ROOM, empathy_chatroom_get_room (chatroom),
654                             COL_MEMBERS, members,
655                             COL_MEMBERS_INT, empathy_chatroom_get_members_count (chatroom),
656                             COL_TOOLTIP, tooltip,
657                             -1);
658
659         g_free (members);
660         g_free (tooltip);
661 }
662
663 static void
664 start_listing_error_cb (EmpathyTpRoomlist        *room_list,
665                         GError                   *error,
666                         EmpathyNewChatroomDialog *dialog)
667 {
668         gtk_label_set_text (GTK_LABEL (dialog->label_error_message), _("Could not start room listing"));
669         gtk_widget_show_all (dialog->viewport_error);
670         gtk_widget_set_sensitive (dialog->treeview, FALSE);
671 }
672
673 static void
674 stop_listing_error_cb (EmpathyTpRoomlist        *room_list,
675                        GError                   *error,
676                        EmpathyNewChatroomDialog *dialog)
677 {
678         gtk_label_set_text (GTK_LABEL (dialog->label_error_message), _("Could not stop room listing"));
679         gtk_widget_show_all (dialog->viewport_error);
680         gtk_widget_set_sensitive (dialog->treeview, FALSE);
681 }
682
683 static void
684 new_chatroom_dialog_listing_cb (EmpathyTpRoomlist        *room_list,
685                                 gpointer                  unused,
686                                 EmpathyNewChatroomDialog *dialog)
687 {
688         gboolean listing;
689
690         listing = empathy_tp_roomlist_is_listing (room_list);
691
692         /* Update the throbber */
693         if (listing) {
694                 gtk_spinner_start (GTK_SPINNER (dialog->throbber));
695                 gtk_widget_show (dialog->throbber);
696         } else {
697                 gtk_spinner_stop (GTK_SPINNER (dialog->throbber));
698                 gtk_widget_hide (dialog->throbber);
699         }
700 }
701
702 static void
703 new_chatroom_dialog_model_clear (EmpathyNewChatroomDialog *dialog)
704 {
705         GtkListStore *store;
706
707         store = GTK_LIST_STORE (dialog->model);
708         gtk_list_store_clear (store);
709 }
710
711 static void
712 new_chatroom_dialog_model_row_activated_cb (GtkTreeView             *tree_view,
713                                             GtkTreePath             *path,
714                                             GtkTreeViewColumn       *column,
715                                             EmpathyNewChatroomDialog *dialog)
716 {
717         gtk_widget_activate (dialog->button_join);
718 }
719
720 static void
721 new_chatroom_dialog_model_selection_changed (GtkTreeSelection         *selection,
722                                              EmpathyNewChatroomDialog *dialog)
723 {
724         GtkTreeModel *model;
725         GtkTreeIter   iter;
726         gchar        *room = NULL;
727         gchar        *server = NULL;
728
729         if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
730                 return;
731         }
732
733         gtk_tree_model_get (model, &iter, COL_ROOM, &room, -1);
734         server = strstr (room, "@");
735         if (server) {
736                 *server = '\0';
737                 server++;
738         }
739
740         gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), server ? server : "");
741         gtk_entry_set_text (GTK_ENTRY (dialog->entry_room), room ? room : "");
742
743         g_free (room);
744 }
745
746 static void
747 new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog)
748 {
749         EmpathyAccountChooser *account_chooser;
750         TpAccount *account;
751         const gchar           *room;
752         const gchar           *server = NULL;
753         gchar                 *room_name = NULL;
754
755         room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
756         server = gtk_entry_get_text (GTK_ENTRY (dialog->entry_server));
757
758         account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
759         account = empathy_account_chooser_get_account (account_chooser);
760
761         if (!EMP_STR_EMPTY (server)) {
762                 room_name = g_strconcat (room, "@", server, NULL);
763         } else {
764                 room_name = g_strdup (room);
765         }
766
767         g_strstrip (room_name);
768
769         DEBUG ("Requesting channel for '%s'", room_name);
770
771         empathy_join_muc (account, room_name, empathy_get_current_action_time ());
772
773         g_free (room_name);
774 }
775
776 static void
777 new_chatroom_dialog_entry_changed_cb (GtkWidget                *entry,
778                                       EmpathyNewChatroomDialog *dialog)
779 {
780         if (entry == dialog->entry_room) {
781                 update_join_button_sensitivity (dialog);
782
783                 /* FIXME: Select the room in the list */
784         }
785 }
786
787 static void
788 new_chatroom_dialog_browse_start (EmpathyNewChatroomDialog *dialog)
789 {
790         new_chatroom_dialog_model_clear (dialog);
791         if (dialog->room_list) {
792                 empathy_tp_roomlist_start (dialog->room_list);
793         }
794 }
795
796 static void
797 new_chatroom_dialog_browse_stop (EmpathyNewChatroomDialog *dialog)
798 {
799         if (dialog->room_list) {
800                 empathy_tp_roomlist_stop (dialog->room_list);
801         }
802 }
803
804 static void
805 new_chatroom_dialog_entry_server_activate_cb (GtkWidget                *widget,
806                                               EmpathyNewChatroomDialog  *dialog)
807 {
808         new_chatroom_dialog_browse_start (dialog);
809 }
810
811 static void
812 new_chatroom_dialog_expander_browse_activate_cb (GtkWidget               *widget,
813                                                  EmpathyNewChatroomDialog *dialog)
814 {
815         gboolean expanded;
816
817         expanded = gtk_expander_get_expanded (GTK_EXPANDER (widget));
818         if (expanded) {
819                 new_chatroom_dialog_browse_stop (dialog);
820                 gtk_window_set_resizable (GTK_WINDOW (dialog->window), FALSE);
821         } else {
822                 gtk_widget_hide (dialog->viewport_error);
823                 gtk_widget_set_sensitive (dialog->treeview, TRUE);
824                 new_chatroom_dialog_browse_start (dialog);
825                 gtk_window_set_resizable (GTK_WINDOW (dialog->window), TRUE);
826         }
827 }
828
829 static gboolean
830 new_chatroom_dialog_entry_server_focus_out_cb (GtkWidget               *widget,
831                                                GdkEventFocus           *event,
832                                                EmpathyNewChatroomDialog *dialog)
833 {
834         gboolean expanded;
835
836         expanded = gtk_expander_get_expanded (GTK_EXPANDER (dialog->expander_browse));
837         if (expanded) {
838                 new_chatroom_dialog_browse_start (dialog);
839         }
840         return FALSE;
841 }