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