]> git.0d.be Git - empathy.git/blobdiff - src/empathy.c
Merge back from master
[empathy.git] / src / empathy.c
index 660e47cea42d11a7251935e94fe228636cf40338..718fe70bfc7a5324deb5cb53365411d54413b072 100644 (file)
@@ -30,6 +30,7 @@
 #include <glib/gi18n.h>
 #include <gtk/gtk.h>
 #include <gdk/gdkx.h>
+#include <unique/unique.h>
 
 #if HAVE_LIBCHAMPLAIN
 #include <clutter-gtk/clutter-gtk.h>
 
 #include <telepathy-glib/dbus.h>
 #include <telepathy-glib/util.h>
-#include <libmissioncontrol/mission-control.h>
+#include <telepathy-glib/connection-manager.h>
+#include <telepathy-glib/interfaces.h>
 
 #include <libempathy/empathy-idle.h>
 #include <libempathy/empathy-utils.h>
 #include <libempathy/empathy-call-factory.h>
 #include <libempathy/empathy-chatroom-manager.h>
+#include <libempathy/empathy-account-settings.h>
 #include <libempathy/empathy-connectivity.h>
 #include <libempathy/empathy-account-manager.h>
+#include <libempathy/empathy-connection-managers.h>
 #include <libempathy/empathy-debugger.h>
 #include <libempathy/empathy-dispatcher.h>
 #include <libempathy/empathy-dispatch-operation.h>
@@ -66,7 +70,6 @@
 #include "empathy-call-window.h"
 #include "empathy-chat-window.h"
 #include "empathy-ft-manager.h"
-#include "bacon-message-connection.h"
 
 #include "extensions/extensions.h"
 
@@ -75,7 +78,7 @@
 
 #include <gst/gst.h>
 
-static BaconMessageConnection *connection = NULL;
+#define COMMAND_ACCOUNTS_DIALOG 1
 
 static void
 dispatch_cb (EmpathyDispatcher *dispatcher,
@@ -111,7 +114,7 @@ dispatch_cb (EmpathyDispatcher *dispatcher,
 
                        manager = empathy_account_manager_dup_singleton ();
                        connection = empathy_tp_chat_get_connection (tp_chat);
-                       account = empathy_account_manager_get_account (manager,
+                       account = empathy_account_manager_get_account_for_connection (manager,
                                                                       connection);
                        chat = empathy_chat_window_find_chat (account, id);
                        g_object_unref (manager);
@@ -146,66 +149,71 @@ dispatch_cb (EmpathyDispatcher *dispatcher,
        }
 }
 
-static void
-service_ended_cb (MissionControl *mc,
-                 gpointer        user_data)
+/* Salut account creation */
+static gboolean
+should_create_salut_account (void)
 {
-       DEBUG ("Mission Control stopped");
+       EmpathyAccountManager *manager;
+       gboolean salut_created = FALSE;
+       GList *accounts, *l;
+
+       /* Check if we already created a salut account */
+       empathy_conf_get_bool (empathy_conf_get (),
+                              EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
+                              &salut_created);
+
+       if (salut_created)
+               {
+                       DEBUG ("Gconf says we already created a salut account once");
+                       return FALSE;
+               }
+
+       manager = empathy_account_manager_dup_singleton ();
+       accounts = empathy_account_manager_dup_accounts (manager);
+
+       for (l = accounts; l != NULL;  l = g_list_next (l)) {
+               EmpathyAccount *account = EMPATHY_ACCOUNT (l->data);
+
+               if (!tp_strdiff (empathy_account_get_protocol (account), "local-xmpp"))
+                       salut_created = TRUE;
+
+               g_object_unref (account);
+       }
+
+       g_object_unref (manager);
+
+       if (salut_created)
+               {
+                       DEBUG ("Existing salut account already exists, flagging so in gconf");
+                       empathy_conf_set_bool (empathy_conf_get (),
+                               EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
+                               TRUE);
+               }
+
+       return !salut_created;
 }
 
 static void
-operation_error_cb (MissionControl *mc,
-                   guint           operation_id,
-                   guint           error_code,
-                   gpointer        user_data)
+salut_account_created (GObject *source,
+       GAsyncResult *result, gpointer user_data)
 {
-       const gchar *message;
-
-       switch (error_code) {
-       case MC_DISCONNECTED_ERROR:
-               message = "Disconnected";
-               break;
-       case MC_INVALID_HANDLE_ERROR:
-               message = "Invalid handle";
-               break;
-       case MC_NO_MATCHING_CONNECTION_ERROR:
-               message = "No matching connection";
-               break;
-       case MC_INVALID_ACCOUNT_ERROR:
-               message = "Invalid account";
-               break;
-       case MC_PRESENCE_FAILURE_ERROR:
-               message = "Presence failure";
-               break;
-       case MC_NO_ACCOUNTS_ERROR:
-               message = "No accounts";
-               break;
-       case MC_NETWORK_ERROR:
-               message = "Network error";
-               break;
-       case MC_CONTACT_DOES_NOT_SUPPORT_VOICE_ERROR:
-               message = "Contact does not support voice";
-               break;
-       case MC_LOWMEM_ERROR:
-               message = "Lowmem";
-               break;
-       case MC_CHANNEL_REQUEST_GENERIC_ERROR:
-               message = "Channel request generic error";
-               break;
-       case MC_CHANNEL_BANNED_ERROR:
-               message = "Channel banned";
-               break;
-       case MC_CHANNEL_FULL_ERROR:
-               message = "Channel full";
-               break;
-       case MC_CHANNEL_INVITE_ONLY_ERROR:
-               message = "Channel invite only";
-               break;
-       default:
-               message = "Unknown error code";
-       }
+       EmpathyAccountSettings *settings = EMPATHY_ACCOUNT_SETTINGS (source);
+       EmpathyAccount *account;
+       GError *error = NULL;
+
+       if (!empathy_account_settings_apply_finish (settings, result, &error))
+               {
+                       DEBUG ("Failed to create salut account: %s", error->message);
+                       g_error_free (error);
+                       return;
+               }
+
+       account = empathy_account_settings_get_account (settings);
 
-       DEBUG ("Error during operation %d: %s", operation_id, message);
+       empathy_account_set_enabled (account, TRUE);
+       empathy_conf_set_bool (empathy_conf_get (),
+                              EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
+                              TRUE);
 }
 
 static void
@@ -222,14 +230,11 @@ use_conn_notify_cb (EmpathyConf *conf,
 }
 
 static void
-create_salut_account (void)
+create_salut_account_if_needed (EmpathyConnectionManagers *managers)
 {
-       McProfile  *profile;
-       McProtocol *protocol;
-       gboolean    salut_created = FALSE;
-       EmpathyAccount  *account;
-       EmpathyAccountManager *account_manager;
-       GList      *accounts;
+       EmpathyAccountSettings  *settings;
+       TpConnectionManager *manager;
+       const TpConnectionManagerProtocol *protocol;
        EBook      *book;
        EContact   *contact;
        gchar      *nickname = NULL;
@@ -239,56 +244,36 @@ create_salut_account (void)
        gchar      *jid = NULL;
        GError     *error = NULL;
 
-       /* Check if we already created a salut account */
-       empathy_conf_get_bool (empathy_conf_get (),
-                              EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
-                              &salut_created);
-       if (salut_created) {
+
+       if (!should_create_salut_account ())
                return;
-       }
 
-       DEBUG ("Try to add a salut account...");
+       manager = empathy_connection_managers_get_cm (managers, "salut");
+       if (manager == NULL)
+               {
+                       DEBUG ("Salut not installed, not making a salut account");
+                       return;
+               }
 
-       /* Check if the salut CM is installed */
-       profile = mc_profile_lookup ("salut");
-       if (!profile) {
-               DEBUG ("No salut profile");
-               return;
-       }
-       protocol = mc_profile_get_protocol (profile);
-       if (!protocol) {
-               DEBUG ("Salut not installed");
-               g_object_unref (profile);
-               return;
-       }
-       g_object_unref (protocol);
+       protocol = tp_connection_manager_get_protocol (manager, "local-xmpp");
+       if (protocol == NULL)
+               {
+                       DEBUG ("Salut doesn't support local-xmpp!!");
+                       return;
+               }
+
+       DEBUG ("Trying to add a salut account...");
 
        /* Get self EContact from EDS */
        if (!e_book_get_self (&contact, &book, &error)) {
                DEBUG ("Failed to get self econtact: %s",
                        error ? error->message : "No error given");
                g_clear_error (&error);
-               g_object_unref (profile);
-               return;
-       }
-
-       empathy_conf_set_bool (empathy_conf_get (),
-                              EMPATHY_PREFS_SALUT_ACCOUNT_CREATED,
-                              TRUE);
-
-       /* Check if there is already a salut account */
-       accounts = mc_accounts_list_by_profile (profile);
-       if (accounts) {
-               DEBUG ("There is already a salut account");
-               mc_accounts_list_free (accounts);
-               g_object_unref (profile);
                return;
        }
 
-       account_manager = empathy_account_manager_dup_singleton ();
-       account = empathy_account_manager_create (account_manager, profile);
-       empathy_account_set_display_name (account, _("People nearby"));
-       g_object_unref (account_manager);
+       settings = empathy_account_settings_new ("salut", "local-xmpp",
+               _("People nearby"));
 
        nickname = e_contact_get (contact, E_CONTACT_NICKNAME);
        first_name = e_contact_get (contact, E_CONTACT_GIVEN_NAME);
@@ -305,23 +290,59 @@ create_salut_account (void)
                "last-name=%s\nemail=%s\njid=%s\n",
                nickname, first_name, last_name, email, jid);
 
-       empathy_account_set_param_string (account, "nickname", nickname ? nickname : "");
-       empathy_account_set_param_string (account, "first-name", first_name ? first_name : "");
-       empathy_account_set_param_string (account, "last-name", last_name ? last_name : "");
-       empathy_account_set_param_string (account, "email", email ? email : "");
-       empathy_account_set_param_string (account, "jid", jid ? jid : "");
+       empathy_account_settings_set_string (settings, "nickname", nickname ? nickname : "");
+       empathy_account_settings_set_string (settings, "first-name", first_name ? first_name : "");
+       empathy_account_settings_set_string (settings, "last-name", last_name ? last_name : "");
+       empathy_account_settings_set_string (settings, "email", email ? email : "");
+       empathy_account_settings_set_string (settings, "jid", jid ? jid : "");
+
+       empathy_account_settings_apply_async (settings,
+               salut_account_created, NULL);
 
        g_free (nickname);
        g_free (first_name);
        g_free (last_name);
        g_free (email);
        g_free (jid);
-       g_object_unref (account);
-       g_object_unref (profile);
+       g_object_unref (settings);
        g_object_unref (contact);
        g_object_unref (book);
 }
 
+static void
+connection_managers_ready_cb (EmpathyConnectionManagers *managers,
+       GParamSpec *spec, gpointer user_data)
+{
+       if (empathy_connection_managers_is_ready (managers))
+               {
+                       create_salut_account_if_needed (managers);
+                       g_object_unref (managers);
+                       managers = NULL;
+               }
+}
+
+static void
+create_salut_account (void)
+{
+       EmpathyConnectionManagers *managers;
+
+       if (!should_create_salut_account ())
+               return;
+
+       managers = empathy_connection_managers_dup_singleton ();
+
+       if (empathy_connection_managers_is_ready (managers))
+               {
+                       create_salut_account_if_needed (managers);
+                       g_object_unref (managers);
+               }
+       else
+               {
+                       g_signal_connect (managers, "notify::ready",
+                               G_CALLBACK (connection_managers_ready_cb), NULL);
+               }
+}
+
 static void
 migrate_config_to_xdg_dir (void)
 {
@@ -383,85 +404,29 @@ migrate_config_to_xdg_dir (void)
        g_free (old_dir);
 }
 
-/* The code that handles single-instance and startup notification is
- * copied from gedit.
- *
- * Copyright (C) 2005 - Paolo Maggi
- */
-static void
-on_bacon_message_received (const char *message,
-                          gpointer    data)
+static UniqueResponse
+unique_app_message_cb (UniqueApp *unique_app,
+                       gint command,
+                       UniqueMessageData *data,
+                       guint timestamp,
+                       gpointer user_data)
 {
-       GtkWidget *window = data;
-       guint32    startup_timestamp;
-
-       g_return_if_fail (message != NULL);
+       GtkWidget *window = user_data;
 
-       DEBUG ("Other instance launched, presenting the main window. message='%s'",
-               message);
+       DEBUG ("Other instance launched, presenting the main window. "
+              "Command=%d, timestamp %u", command, timestamp);
 
-       if (strcmp (message, "accounts") == 0) {
-               /* accounts dialog requested */
+       if (command == COMMAND_ACCOUNTS_DIALOG) {
                empathy_accounts_dialog_show (GTK_WINDOW (window), NULL);
        } else {
-               startup_timestamp = atoi (message);
-
-               /* Set the proper interaction time on the window.
-                * Fall back to roundtripping to the X server when we
-                * don't have the timestamp, e.g. when launched from
-                * terminal. We also need to make sure that the window
-                * has been realized otherwise it will not work. lame. */
-               if (startup_timestamp == 0) {
-                       /* Work if launched from the terminal */
-                       DEBUG ("Using X server timestamp as a fallback");
-
-                       if (!GTK_WIDGET_REALIZED (window)) {
-                               gtk_widget_realize (GTK_WIDGET (window));
-                       }
-
-                       startup_timestamp = gdk_x11_get_server_time (gtk_widget_get_window (window));
-               }
-
-               gtk_window_present_with_time (GTK_WINDOW (window), startup_timestamp);
-       }
-}
-
-static guint32
-get_startup_timestamp ()
-{
-       const gchar *startup_id_env;
-       gchar       *startup_id = NULL;
-       gchar       *time_str;
-       gchar       *end;
-       gulong       retval = 0;
-
-       /* we don't unset the env, since startup-notification
-        * may still need it */
-       startup_id_env = g_getenv ("DESKTOP_STARTUP_ID");
-       if (startup_id_env == NULL) {
-               goto out;
+               gtk_window_set_screen (GTK_WINDOW (window),
+                                      unique_message_data_get_screen (data));
+               gtk_window_set_startup_id (GTK_WINDOW (window),
+                                          unique_message_data_get_startup_id (data));
+               gtk_window_present_with_time (GTK_WINDOW (window), timestamp);
        }
 
-       startup_id = g_strdup (startup_id_env);
-
-       time_str = g_strrstr (startup_id, "_TIME");
-       if (time_str == NULL) {
-               goto out;
-       }
-
-       errno = 0;
-
-       /* Skip past the "_TIME" part */
-       time_str += 5;
-
-       retval = strtoul (time_str, &end, 0);
-       if (end == time_str || errno != 0)
-               retval = 0;
-
- out:
-       g_free (startup_id);
-
-       return (retval > 0) ? retval : 0;
+       return UNIQUE_RESPONSE_OK;
 }
 
 static gboolean
@@ -540,21 +505,36 @@ default_log_handler (const gchar *log_domain,
 }
 #endif /* ENABLE_DEBUG */
 
+static void
+account_manager_ready_cb (EmpathyAccountManager *manager,
+       GParamSpec *spec,
+       gpointer user_data)
+{
+       if (!empathy_account_manager_is_ready (manager))
+               return;
+
+       if (empathy_account_manager_get_count (manager) == 0)
+               {
+                       empathy_accounts_dialog_show (GTK_WINDOW (empathy_main_window_get ()),
+                               NULL);
+               }
+       create_salut_account ();
+}
+
 int
 main (int argc, char *argv[])
 {
-       guint32            startup_timestamp;
 #if HAVE_GEOCLUE
        EmpathyLocationManager *location_manager = NULL;
 #endif
        EmpathyStatusIcon *icon;
        EmpathyDispatcher *dispatcher;
+       EmpathyAccountManager *account_manager;
        EmpathyLogManager *log_manager;
        EmpathyChatroomManager *chatroom_manager;
        EmpathyCallFactory *call_factory;
        EmpathyFTFactory  *ft_factory;
        GtkWidget         *window;
-       MissionControl    *mc;
        EmpathyIdle       *idle;
        EmpathyConnectivity *connectivity;
        gboolean           autoconnect = TRUE;
@@ -563,6 +543,7 @@ main (int argc, char *argv[])
        gboolean           accounts_dialog = FALSE;
        GError            *error = NULL;
        TpDBusDaemon      *dbus_daemon;
+       UniqueApp         *unique_app;
        GOptionEntry       options[] = {
                { "no-connect", 'n',
                  0, G_OPTION_ARG_NONE, &no_connect,
@@ -610,39 +591,20 @@ main (int argc, char *argv[])
        g_log_set_default_handler (default_log_handler, NULL);
 #endif
 
-        /* Setting up the bacon connection */
-       startup_timestamp = get_startup_timestamp ();
-       connection = bacon_message_connection_new ("empathy");
-       if (connection != NULL) {
-               if (!bacon_message_connection_get_is_server (connection)) {
-                       gchar *message;
-
-                       if (accounts_dialog) {
-                               DEBUG ("Showing accounts dialog from existing Empathy instance");
-
-                               message = g_strdup ("accounts");
-
-                       } else {
-
-                               DEBUG ("Activating existing instance");
-
-                               message = g_strdup_printf ("%" G_GUINT32_FORMAT,
-                                                          startup_timestamp);
-                       }
-
-                       bacon_message_connection_send (connection, message);
-
-                       /* We never popup a window, so tell startup-notification
-                        * that we are done. */
-                       gdk_notify_startup_complete ();
-
-                       g_free (message);
-                       bacon_message_connection_free (connection);
-
-                       return EXIT_SUCCESS;
-               }
-       } else {
-               g_warning ("Cannot create the 'empathy' bacon connection.");
+       unique_app = unique_app_new_with_commands ("org.gnome.Empathy",
+                                                  NULL,
+                                                  "accounts_dialog",
+                                                  COMMAND_ACCOUNTS_DIALOG,
+                                                  NULL);
+
+       if (unique_app_is_running (unique_app)) {
+               unique_app_send_message (unique_app,
+                                        accounts_dialog ?
+                                        COMMAND_ACCOUNTS_DIALOG :
+                                                UNIQUE_ACTIVATE,
+                                        NULL);
+               g_object_unref (unique_app);
+               return EXIT_SUCCESS;
        }
 
        /* Take well-known name */
@@ -662,15 +624,6 @@ main (int argc, char *argv[])
                g_clear_error (&error);
        }
 
-       /* Setting up MC */
-       mc = empathy_mission_control_dup_singleton ();
-       g_signal_connect (mc, "ServiceEnded",
-                         G_CALLBACK (service_ended_cb),
-                         NULL);
-       g_signal_connect (mc, "Error",
-                         G_CALLBACK (operation_error_cb),
-                         NULL);
-
        if (accounts_dialog) {
                GtkWidget *dialog;
 
@@ -701,23 +654,23 @@ main (int argc, char *argv[])
        if (autoconnect && ! no_connect &&
                tp_connection_presence_type_cmp_availability (empathy_idle_get_state
                        (idle), TP_CONNECTION_PRESENCE_TYPE_OFFLINE) <= 0) {
-               empathy_idle_set_state (idle, MC_PRESENCE_AVAILABLE);
+               empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_AVAILABLE);
        }
 
+       /* account management */
+       account_manager = empathy_account_manager_dup_singleton ();
+       g_signal_connect (account_manager, "notify::ready",
+               G_CALLBACK (account_manager_ready_cb), NULL);
 
        migrate_config_to_xdg_dir ();
-       create_salut_account ();
 
        /* Setting up UI */
        window = empathy_main_window_show ();
        icon = empathy_status_icon_new (GTK_WINDOW (window), hide_contact_list);
 
-       if (connection) {
-               /* We se the callback here because we need window */
-               bacon_message_connection_set_callback (connection,
-                                                      on_bacon_message_received,
-                                                      window);
-       }
+       g_signal_connect (unique_app, "message-received",
+                         G_CALLBACK (unique_app_message_cb),
+                         window);
 
        /* Handle channels */
        dispatcher = empathy_dispatcher_dup_singleton ();
@@ -751,10 +704,10 @@ main (int argc, char *argv[])
 
        empathy_idle_set_state (idle, TP_CONNECTION_PRESENCE_TYPE_OFFLINE);
 
-       g_object_unref (mc);
        g_object_unref (idle);
        g_object_unref (connectivity);
        g_object_unref (icon);
+       g_object_unref (account_manager);
        g_object_unref (log_manager);
        g_object_unref (dispatcher);
        g_object_unref (chatroom_manager);
@@ -762,9 +715,9 @@ main (int argc, char *argv[])
        g_object_unref (location_manager);
 #endif
        g_object_unref (ft_factory);
+       g_object_unref (unique_app);
 
        notify_uninit ();
 
        return EXIT_SUCCESS;
 }
-