]> git.0d.be Git - empathy.git/blobdiff - src/empathy-new-chatroom-dialog.c
Move should_create_salut_account to local-xmpp-assistant-widget
[empathy.git] / src / empathy-new-chatroom-dialog.c
index e16da91a7b2076d2fc7f9aeca5a7a5ac6520f989..df30c2a85394060cbe629af9f402d0ea9ef28211 100644 (file)
 #include <config.h>
 
 #include <string.h>
-#include <stdio.h>
 
 #include <gtk/gtk.h>
 #include <glib.h>
 #include <glib/gi18n.h>
 #include <glib/gprintf.h>
 
-#include <libmissioncontrol/mission-control.h>
-#include <libmissioncontrol/mc-profile.h>
+#include <telepathy-glib/interfaces.h>
 
 #include <libempathy/empathy-tp-roomlist.h>
 #include <libempathy/empathy-chatroom.h>
 #include <libempathy/empathy-utils.h>
-#include <libempathy/empathy-dispatcher.h>
+#include <libempathy/empathy-request-util.h>
+#include <libempathy/empathy-gsettings.h>
 
 #include <libempathy-gtk/empathy-account-chooser.h>
 #include <libempathy-gtk/empathy-ui-utils.h>
 
 #include "empathy-new-chatroom-dialog.h"
-#include "ephy-spinner.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
 #include <libempathy/empathy-debug.h>
 
 typedef struct {
        EmpathyTpRoomlist *room_list;
+       /* Currently selected account */
+       TpAccount         *account;
+       /* Signal id of the "status-changed" signal connected on the currently
+        * selected account */
+       gulong             status_changed_id;
 
        GtkWidget         *window;
        GtkWidget         *vbox_widgets;
-       GtkWidget         *table_info;
+       GtkWidget         *table_grid;
        GtkWidget         *label_account;
        GtkWidget         *account_chooser;
        GtkWidget         *label_server;
@@ -62,12 +65,15 @@ typedef struct {
        GtkWidget         *label_room;
        GtkWidget         *entry_room;
        GtkWidget         *expander_browse;
+       GtkWidget         *hbox_expander;
        GtkWidget         *throbber;
        GtkWidget         *treeview;
        GtkTreeModel      *model;
        GtkWidget         *button_join;
        GtkWidget         *label_error_message;
        GtkWidget         *viewport_error;
+
+       GSettings         *gsettings;
 } EmpathyNewChatroomDialog;
 
 enum {
@@ -91,6 +97,8 @@ static void     new_chatroom_dialog_model_add_columns               (EmpathyNewC
 static void     new_chatroom_dialog_update_widgets                  (EmpathyNewChatroomDialog *dialog);
 static void     new_chatroom_dialog_account_changed_cb              (GtkComboBox              *combobox,
                                                                     EmpathyNewChatroomDialog *dialog);
+static void     new_chatroom_dialog_account_ready_cb                (EmpathyAccountChooser   *combobox,
+                                                                     EmpathyNewChatroomDialog  *dialog);
 static void     new_chatroom_dialog_roomlist_destroy_cb             (EmpathyTpRoomlist        *room_list,
                                                                     EmpathyNewChatroomDialog *dialog);
 static void     new_chatroom_dialog_new_room_cb                     (EmpathyTpRoomlist        *room_list,
@@ -129,6 +137,7 @@ static void new_chatroom_dialog_button_close_error_clicked_cb   (GtkButton
 
 static EmpathyNewChatroomDialog *dialog_p = NULL;
 
+
 void
 empathy_new_chatroom_dialog_show (GtkWindow *parent)
 {
@@ -147,7 +156,7 @@ empathy_new_chatroom_dialog_show (GtkWindow *parent)
        filename = empathy_file_lookup ("empathy-new-chatroom-dialog.ui", "src");
        gui = empathy_builder_get_file (filename,
                                       "new_chatroom_dialog", &dialog->window,
-                                      "table_info", &dialog->table_info,
+                                      "table_grid", &dialog->table_grid,
                                       "label_account", &dialog->label_account,
                                       "label_server", &dialog->label_server,
                                       "label_room", &dialog->label_room,
@@ -156,6 +165,7 @@ empathy_new_chatroom_dialog_show (GtkWindow *parent)
                                       "treeview", &dialog->treeview,
                                       "button_join", &dialog->button_join,
                                       "expander_browse", &dialog->expander_browse,
+                                      "hbox_expander", &dialog->hbox_expander,
                                       "label_error_message", &dialog->label_error_message,
                                       "viewport_error", &dialog->viewport_error,
                                       NULL);
@@ -189,24 +199,25 @@ empathy_new_chatroom_dialog_show (GtkWindow *parent)
        new_chatroom_dialog_model_setup (dialog);
 
        /* Add throbber */
-       dialog->throbber = ephy_spinner_new ();
-       ephy_spinner_set_size (EPHY_SPINNER (dialog->throbber), GTK_ICON_SIZE_LARGE_TOOLBAR);
-       gtk_widget_show (dialog->throbber);
-       gtk_table_attach (GTK_TABLE (dialog->table_info),
-                         dialog->throbber,
-                         2, 3, 0, 1,
-                         0, 0, 0, 0);
+       dialog->throbber = gtk_spinner_new ();
+       gtk_box_pack_start (GTK_BOX (dialog->hbox_expander), dialog->throbber,
+               TRUE, TRUE, 0);
+
+       dialog->gsettings = g_settings_new (EMPATHY_PREFS_CHAT_SCHEMA);
 
        /* Account chooser for custom */
        dialog->account_chooser = empathy_account_chooser_new ();
        empathy_account_chooser_set_filter (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser),
-                                           empathy_account_chooser_filter_is_connected,
+                                           empathy_account_chooser_filter_supports_chatrooms,
                                            NULL);
-       gtk_table_attach_defaults (GTK_TABLE (dialog->table_info),
+       gtk_grid_attach (GTK_GRID (dialog->table_grid),
                                   dialog->account_chooser,
-                                  1, 2, 0, 1);
+                                  1, 0, 1, 1);
        gtk_widget_show (dialog->account_chooser);
 
+       g_signal_connect (EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser), "ready",
+                         G_CALLBACK (new_chatroom_dialog_account_ready_cb),
+                         dialog);
        g_signal_connect (GTK_COMBO_BOX (dialog->account_chooser), "changed",
                          G_CALLBACK (new_chatroom_dialog_account_changed_cb),
                          dialog);
@@ -221,6 +232,24 @@ empathy_new_chatroom_dialog_show (GtkWindow *parent)
        gtk_widget_show (dialog->window);
 }
 
+static void
+new_chatroom_dialog_store_last_account (GSettings             *gsettings,
+                                        EmpathyAccountChooser *account_chooser)
+{
+       TpAccount   *account;
+       const char *account_path;
+
+       account = empathy_account_chooser_get_account (account_chooser);
+       if (account == NULL)
+               return;
+
+       account_path = tp_proxy_get_object_path (account);
+       DEBUG ("Storing account path '%s'", account_path);
+
+       g_settings_set (gsettings, EMPATHY_PREFS_CHAT_ROOM_LAST_ACCOUNT,
+                       "o", account_path);
+}
+
 static void
 new_chatroom_dialog_response_cb (GtkWidget               *widget,
                                 gint                     response,
@@ -228,6 +257,8 @@ new_chatroom_dialog_response_cb (GtkWidget               *widget,
 {
        if (response == GTK_RESPONSE_OK) {
                new_chatroom_dialog_join (dialog);
+               new_chatroom_dialog_store_last_account (dialog->gsettings,
+                                                       EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser));
        }
 
        gtk_widget_destroy (widget);
@@ -242,6 +273,13 @@ new_chatroom_dialog_destroy_cb (GtkWidget               *widget,
        }
        g_object_unref (dialog->model);
 
+       if (dialog->account != NULL) {
+               g_signal_handler_disconnect (dialog->account, dialog->status_changed_id);
+               g_object_unref (dialog->account);
+       }
+
+       g_clear_object (&dialog->gsettings);
+
        g_free (dialog);
 }
 
@@ -353,19 +391,47 @@ new_chatroom_dialog_model_add_columns (EmpathyNewChatroomDialog *dialog)
        gtk_tree_view_append_column (view, column);
 }
 
+static void
+update_join_button_sensitivity (EmpathyNewChatroomDialog *dialog)
+{
+       const gchar           *room;
+       const gchar           *protocol;
+       gboolean               sensitive = FALSE;
+
+
+       room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
+       protocol = tp_account_get_protocol (dialog->account);
+       if (EMP_STR_EMPTY (room))
+               goto out;
+
+       if (!tp_strdiff (protocol, "irc") && (!tp_strdiff (room, "#") ||
+                                             !tp_strdiff (room, "&")))
+       {
+               goto out;
+       }
+
+       if (dialog->account == NULL)
+               goto out;
+
+       if (tp_account_get_connection_status (dialog->account, NULL) !=
+                     TP_CONNECTION_STATUS_CONNECTED)
+               goto out;
+
+       sensitive = TRUE;
+
+out:
+       gtk_widget_set_sensitive (dialog->button_join, sensitive);
+}
+
 static void
 new_chatroom_dialog_update_widgets (EmpathyNewChatroomDialog *dialog)
 {
-       EmpathyAccountChooser *account_chooser;
-       EmpathyAccount        *account;
-       McProfile             *profile;
        const gchar           *protocol;
-       const gchar           *room;
 
-       account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
-       account = empathy_account_chooser_dup_account (account_chooser);
-       profile = empathy_account_get_profile (account);
-       protocol = mc_profile_get_protocol_name (profile);
+       if (dialog->account == NULL)
+               return;
+
+       protocol = tp_account_get_protocol (dialog->account);
 
        gtk_entry_set_text (GTK_ENTRY (dialog->entry_server), "");
 
@@ -383,14 +449,66 @@ new_chatroom_dialog_update_widgets (EmpathyNewChatroomDialog *dialog)
                gtk_widget_set_sensitive (dialog->entry_server, TRUE);
        }
 
-       room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
-       gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
+       if (!tp_strdiff (protocol, "irc"))
+               gtk_entry_set_text (GTK_ENTRY (dialog->entry_room), "#");
+       else
+               gtk_entry_set_text (GTK_ENTRY (dialog->entry_room), "");
+
+       update_join_button_sensitivity (dialog);
 
        /* Final set up of the dialog */
        gtk_widget_grab_focus (dialog->entry_room);
+       gtk_editable_set_position (GTK_EDITABLE (dialog->entry_room), -1);
+}
 
-       g_object_unref (account);
-       g_object_unref (profile);
+static void
+account_status_changed_cb (TpAccount *account,
+                           guint old_status,
+                           guint new_status,
+                           guint reason,
+                           gchar *dbus_error_name,
+                           GHashTable *details,
+                           EmpathyNewChatroomDialog *self)
+{
+       update_join_button_sensitivity (self);
+}
+
+static void
+new_chatroom_dialog_select_last_account (GSettings             *gsettings,
+                                         EmpathyAccountChooser *account_chooser)
+{
+       const gchar          *account_path;
+       TpAccountManager      *manager;
+       TpSimpleClientFactory *factory;
+       TpAccount             *account;
+       TpConnectionStatus    status;
+
+       account_path = g_settings_get_string (gsettings, EMPATHY_PREFS_CHAT_ROOM_LAST_ACCOUNT);
+       DEBUG ("Selecting account path '%s'", account_path);
+
+       manager =  tp_account_manager_dup ();
+       factory = tp_proxy_get_factory (manager);
+       account = tp_simple_client_factory_ensure_account (factory,
+                                                          account_path,
+                                                          NULL,
+                                                          NULL);
+
+       if (account != NULL) {
+               status = tp_account_get_connection_status (account, NULL);
+               if (status == TP_CONNECTION_STATUS_CONNECTED) {
+                       empathy_account_chooser_set_account (account_chooser,
+                                                            account);
+               }
+               g_object_unref (account);
+       }
+       g_object_unref (manager);
+}
+
+static void
+new_chatroom_dialog_account_ready_cb (EmpathyAccountChooser    *combobox,
+                                      EmpathyNewChatroomDialog *dialog)
+{
+       new_chatroom_dialog_select_last_account (dialog->gsettings, combobox);
 }
 
 static void
@@ -398,24 +516,48 @@ new_chatroom_dialog_account_changed_cb (GtkComboBox             *combobox,
                                        EmpathyNewChatroomDialog *dialog)
 {
        EmpathyAccountChooser *account_chooser;
-       EmpathyAccount        *account;
        gboolean               listing = FALSE;
        gboolean               expanded = FALSE;
+       TpConnection          *connection;
+       TpCapabilities *caps;
 
        if (dialog->room_list) {
                g_object_unref (dialog->room_list);
                dialog->room_list = NULL;
        }
 
-       ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
+       gtk_spinner_stop (GTK_SPINNER (dialog->throbber));
+       gtk_widget_hide (dialog->throbber);
        new_chatroom_dialog_model_clear (dialog);
 
+       if (dialog->account != NULL) {
+               g_signal_handler_disconnect (dialog->account, dialog->status_changed_id);
+               g_object_unref (dialog->account);
+       }
+
        account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
-       account = empathy_account_chooser_dup_account (account_chooser);
-       if (account == NULL)
+       dialog->account = empathy_account_chooser_dup_account (account_chooser);
+       connection = empathy_account_chooser_get_connection (account_chooser);
+       if (dialog->account == NULL)
                goto out;
 
-       dialog->room_list = empathy_tp_roomlist_new (account);
+       dialog->status_changed_id = g_signal_connect (dialog->account,
+                     "status-changed", G_CALLBACK (account_status_changed_cb), dialog);
+
+       /* empathy_account_chooser_filter_supports_chatrooms ensures that the
+       * account has a connection and CAPABILITIES has been prepared. */
+       g_assert (connection != NULL);
+       g_assert (tp_proxy_is_prepared (connection,
+               TP_CONNECTION_FEATURE_CAPABILITIES));
+       caps = tp_connection_get_capabilities (connection);
+
+       if (tp_capabilities_supports_room_list (caps, NULL)) {
+               /* Roomlist channels are supported */
+               dialog->room_list = empathy_tp_roomlist_new (dialog->account);
+       }
+       else {
+               dialog->room_list = NULL;
+       }
 
        if (dialog->room_list) {
                g_signal_connect (dialog->room_list, "destroy",
@@ -443,11 +585,12 @@ new_chatroom_dialog_account_changed_cb (GtkComboBox             *combobox,
 
                listing = empathy_tp_roomlist_is_listing (dialog->room_list);
                if (listing) {
-                       ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
+                       gtk_spinner_start (GTK_SPINNER (dialog->throbber));
+                       gtk_widget_show (dialog->throbber);
                }
        }
 
-       g_object_unref (account);
+       gtk_widget_set_sensitive (dialog->expander_browse, dialog->room_list != NULL);
 
 out:
        new_chatroom_dialog_update_widgets (dialog);
@@ -473,31 +616,30 @@ new_chatroom_dialog_new_room_cb (EmpathyTpRoomlist        *room_list,
                                 EmpathyChatroom          *chatroom,
                                 EmpathyNewChatroomDialog *dialog)
 {
-       GtkTreeView      *view;
-       GtkTreeSelection *selection;
        GtkListStore     *store;
        GtkTreeIter       iter;
        gchar            *members;
        gchar            *tooltip;
        const gchar      *need_password;
        const gchar      *invite_only;
+       gchar            *tmp;
 
        DEBUG ("New chatroom listed: %s (%s)",
                empathy_chatroom_get_name (chatroom),
                empathy_chatroom_get_room (chatroom));
 
        /* Add to model */
-       view = GTK_TREE_VIEW (dialog->treeview);
-       selection = gtk_tree_view_get_selection (view);
        store = GTK_LIST_STORE (dialog->model);
        members = g_strdup_printf ("%d", empathy_chatroom_get_members_count (chatroom));
-       tooltip = g_strdup_printf (C_("Room/Join's roomlist tooltip. Parameters"
-               "are a channel name, yes/no, yes/no and a number.",
-               "<b>%s</b>\nInvite required: %s\nPassword required: %s\nMembers: %s"),
-               empathy_chatroom_get_name (chatroom),
+       tmp = g_strdup_printf ("<b>%s</b>", empathy_chatroom_get_name (chatroom));
+       /* Translators: Room/Join's roomlist tooltip. Parameters are a channel name,
+       yes/no, yes/no and a number. */
+       tooltip = g_strdup_printf (_("%s\nInvite required: %s\nPassword required: %s\nMembers: %s"),
+               tmp,
                empathy_chatroom_get_invite_only (chatroom) ? _("Yes") : _("No"),
                empathy_chatroom_get_need_password (chatroom) ? _("Yes") : _("No"),
                members);
+       g_free (tmp);
        invite_only = (empathy_chatroom_get_invite_only (chatroom) ?
                GTK_STOCK_INDEX : NULL);
        need_password = (empathy_chatroom_get_need_password (chatroom) ?
@@ -549,9 +691,11 @@ new_chatroom_dialog_listing_cb (EmpathyTpRoomlist        *room_list,
 
        /* Update the throbber */
        if (listing) {
-               ephy_spinner_start (EPHY_SPINNER (dialog->throbber));
+               gtk_spinner_start (GTK_SPINNER (dialog->throbber));
+               gtk_widget_show (dialog->throbber);
        } else {
-               ephy_spinner_stop (EPHY_SPINNER (dialog->throbber));
+               gtk_spinner_stop (GTK_SPINNER (dialog->throbber));
+               gtk_widget_hide (dialog->throbber);
        }
 }
 
@@ -603,7 +747,7 @@ static void
 new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog)
 {
        EmpathyAccountChooser *account_chooser;
-       TpConnection          *connection;
+       TpAccount *account;
        const gchar           *room;
        const gchar           *server = NULL;
        gchar                 *room_name = NULL;
@@ -612,7 +756,7 @@ new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog)
        server = gtk_entry_get_text (GTK_ENTRY (dialog->entry_server));
 
        account_chooser = EMPATHY_ACCOUNT_CHOOSER (dialog->account_chooser);
-       connection = empathy_account_chooser_get_connection (account_chooser);
+       account = empathy_account_chooser_get_account (account_chooser);
 
        if (!EMP_STR_EMPTY (server)) {
                room_name = g_strconcat (room, "@", server, NULL);
@@ -620,8 +764,11 @@ new_chatroom_dialog_join (EmpathyNewChatroomDialog *dialog)
                room_name = g_strdup (room);
        }
 
+       g_strstrip (room_name);
+
        DEBUG ("Requesting channel for '%s'", room_name);
-       empathy_dispatcher_join_muc (connection, room_name, NULL, NULL);
+
+       empathy_join_muc (account, room_name, empathy_get_current_action_time ());
 
        g_free (room_name);
 }
@@ -631,10 +778,8 @@ new_chatroom_dialog_entry_changed_cb (GtkWidget                *entry,
                                      EmpathyNewChatroomDialog *dialog)
 {
        if (entry == dialog->entry_room) {
-               const gchar *room;
+               update_join_button_sensitivity (dialog);
 
-               room = gtk_entry_get_text (GTK_ENTRY (dialog->entry_room));
-               gtk_widget_set_sensitive (dialog->button_join, !EMP_STR_EMPTY (room));
                /* FIXME: Select the room in the list */
        }
 }