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