]> git.0d.be Git - empathy.git/blobdiff - src/empathy-import-mc4-accounts.c
Updated Polish translation
[empathy.git] / src / empathy-import-mc4-accounts.c
index 6f2a02c831c16cacf95485be929cc6ef4c3c145d..b4bca6fdbff803787591b641360bff0e55db8131 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 Collabora Ltd.
+ * Copyright (C) 2009 Collabora Ltd.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
 #include <glib.h>
 #include <glib/gi18n.h>
 #include <gconf/gconf-client.h>
+#include <telepathy-glib/account-manager.h>
 #include <telepathy-glib/util.h>
+#include <telepathy-glib/defs.h>
 #include <dbus/dbus-protocol.h>
 #include <gnome-keyring.h>
-#include <libempathy/empathy-account-manager.h>
 #include <libempathy/empathy-account-settings.h>
 #include <libempathy/empathy-connection-managers.h>
 
@@ -56,6 +57,11 @@ static ProfileProtocolMapItem profile_protocol_map[] =
   { "sofiasip", "sip" },
 };
 
+typedef struct {
+  gchar *account_name;
+  gboolean enable;
+} Misc;
+
 static gchar *
 _account_name_from_key (const gchar *key)
 {
@@ -79,13 +85,16 @@ _param_name_from_key (const gchar *key)
 {
  const gchar *base, *slash;
  gchar *account_name;
+ gchar *ret;
 
  account_name = _account_name_from_key (key);
  base = strstr (key, account_name);
  slash = strchr (base, '/');
+
+ ret = g_strdup (slash+1);
  g_free (account_name);
 
- return g_strdup (slash+1);
+ return ret;
 }
 
 static gchar *
@@ -94,7 +103,7 @@ _create_default_display_name (const gchar *protocol)
   if (!tp_strdiff (protocol, "local-xmpp"))
     return g_strdup (_("People Nearby"));
 
-  return g_strdup_printf ("%s account", protocol);
+  return g_strdup_printf (_("%s account"), protocol);
 }
 
 static const gchar *
@@ -110,7 +119,7 @@ _get_manager_for_protocol (EmpathyConnectionManagers *managers,
     {
       TpConnectionManager *tp_cm = l->data;
 
-      /* Only use haze if no other cm provides this account */
+      /* Only use haze if no other cm provides this protocol */
       if (!tp_strdiff (tp_connection_manager_get_name (tp_cm), "haze"))
         {
           haze = tp_cm;
@@ -131,28 +140,104 @@ out:
   return cm != NULL ? tp_connection_manager_get_name (cm) : NULL;
 }
 
+static void
+_move_contents (const gchar *old, const gchar *new)
+{
+  GDir *source;
+  const gchar *f;
+  int ret;
+
+  ret = g_mkdir_with_parents (new, 0777);
+  if (ret == -1)
+    return;
+
+  source = g_dir_open (old, 0, NULL);
+  if (source == NULL)
+    return;
+
+  while ((f = g_dir_read_name (source)) != NULL)
+    {
+      gchar *old_path;
+      gchar *new_path;
+
+      old_path = g_build_path (G_DIR_SEPARATOR_S, old, f, NULL);
+      new_path = g_build_path (G_DIR_SEPARATOR_S, new, f, NULL);
+
+      if (g_file_test (old_path, G_FILE_TEST_IS_DIR))
+        {
+          _move_contents (old_path, new_path);
+        }
+      else
+        {
+          GFile *f_old, *f_new;
+
+          f_old = g_file_new_for_path (old_path);
+          f_new = g_file_new_for_path (new_path);
+
+          g_file_move (f_old, f_new, G_FILE_COPY_NONE,
+            NULL, NULL, NULL, NULL);
+
+          g_object_unref (f_old);
+          g_object_unref (f_new);
+        }
+
+      g_free (old_path);
+      g_free (new_path);
+    }
+
+  g_dir_close (source);
+}
+
+static void
+_move_logs (TpAccount *account, const gchar *account_name)
+{
+  gchar *old_path, *new_path, *escaped;
+  const gchar *name;
+
+  name = tp_proxy_get_object_path (account);
+  if (g_str_has_prefix (name, TP_ACCOUNT_OBJECT_PATH_BASE))
+    name += strlen (TP_ACCOUNT_OBJECT_PATH_BASE);
+
+  escaped = g_strdelimit (g_strdup (name), "/", '_');
+  new_path = g_build_path (G_DIR_SEPARATOR_S, g_get_user_data_dir (),
+    PACKAGE_NAME, "logs", escaped, NULL);
+  g_free (escaped);
+
+  old_path = g_build_path (G_DIR_SEPARATOR_S,
+    g_get_home_dir (),
+    ".gnome2", PACKAGE_NAME, "logs", account_name, NULL);
+
+  _move_contents (old_path, new_path);
+}
+
 static void
 _create_account_cb (GObject *source,
   GAsyncResult *result,
   gpointer user_data)
 {
-  EmpathyAccount *account;
+  TpAccount *account;
   GError *error = NULL;
+  Misc *misc = (Misc *) user_data;
 
   if (!empathy_account_settings_apply_finish (
       EMPATHY_ACCOUNT_SETTINGS (source), result, &error))
     {
-      DEBUG ("Failed to create account: %s",
-          error ? error->message : "No error given");
+      DEBUG ("Failed to create account: %s", error->message);
       g_error_free (error);
       goto out;
     }
 
-  DEBUG ("account created\n");
+  DEBUG ("account created");
   account = empathy_account_settings_get_account (
     EMPATHY_ACCOUNT_SETTINGS (source));
-  empathy_account_set_enabled_async (account,
-      GPOINTER_TO_INT (user_data), NULL, NULL);
+
+  _move_logs (account, misc->account_name);
+
+  tp_account_set_enabled_async (account,
+      misc->enable, NULL, NULL);
+
+  g_free (misc->account_name);
+  g_slice_free (Misc, misc);
 
 out:
   g_object_unref (source);
@@ -161,9 +246,9 @@ out:
 static gchar *
 _get_protocol_from_profile (const gchar *profile)
 {
-  gint i;
+  guint i;
 
-  DEBUG ("profile: %s\n", profile);
+  DEBUG ("profile: %s", profile);
 
   for (i = 0; i < G_N_ELEMENTS (profile_protocol_map); i++)
     if (!tp_strdiff (profile, profile_protocol_map[i].profile))
@@ -269,7 +354,7 @@ _recurse_account (GSList *entries, EmpathyAccountSettings *settings,
       GConfEntry *entry;
       gchar *param;
 
-      entry = (GConfEntry*) tmp->data;
+      entry = (GConfEntry *) tmp->data;
       param = _param_name_from_key (gconf_entry_get_key (entry));
 
       if (g_str_has_prefix (param, "param-"))
@@ -289,7 +374,7 @@ import_one_account (const char *path,
   GConfClient *client)
 {
   gchar *account_name = _account_name_from_key (path);
-  EmpathyAccountSettings *settings;
+  EmpathyAccountSettings *settings = NULL;
   GError *error = NULL;
   GSList *entries = NULL;
   gchar *profile = NULL;
@@ -299,6 +384,7 @@ import_one_account (const char *path,
   gchar *key;
   gboolean enabled = FALSE;
   gboolean ret = FALSE;
+  Misc *misc;
 
   DEBUG ("Starting import of %s (%s)", path, account_name);
 
@@ -308,7 +394,7 @@ import_one_account (const char *path,
 
   if (profile == NULL)
     {
-      DEBUG ("Account is missing a profile entry\n");
+      DEBUG ("Account is missing a profile entry");
       goto failed;
     }
 
@@ -338,8 +424,7 @@ import_one_account (const char *path,
 
   if (entries == NULL)
     {
-
-      DEBUG ("Failed to get all entries: %s\n", error->message);
+      DEBUG ("Failed to get all entries: %s", error->message);
       g_error_free (error);
       goto failed;
     }
@@ -349,20 +434,25 @@ import_one_account (const char *path,
   key = g_strdup_printf ("%s/enabled", path);
   enabled = gconf_client_get_bool (client, key, NULL);
   g_free (key);
+
+  misc = g_slice_new (Misc);
+  misc->account_name = account_name;
+  misc->enable = enabled;
+
   empathy_account_settings_apply_async (settings,
-          _create_account_cb, GINT_TO_POINTER (enabled));
+          _create_account_cb, misc);
   ret = TRUE;
 
 out:
   g_free (protocol);
   g_free (profile);
   g_slist_free (entries);
-  g_free (account_name);
 
   return ret;
 
 failed:
   DEBUG ("Failed to import %s", path);
+  g_free (account_name);
   if (settings != NULL)
     g_object_unref (settings);
   goto out;
@@ -401,7 +491,7 @@ empathy_import_mc4_accounts (EmpathyConnectionManagers *managers)
 
   if (error != NULL)
     {
-      DEBUG ("Failed to get import_mc4_accounts key: %s\n", error->message);
+      DEBUG ("Failed to get import_mc4_accounts key: %s", error->message);
       g_error_free (error);
       goto out;
     }
@@ -412,13 +502,13 @@ empathy_import_mc4_accounts (EmpathyConnectionManagers *managers)
       goto out;
     }
 
-  DEBUG ("MC 4 accounts are going to be imported\n");
+  DEBUG ("MC 4 accounts are going to be imported");
 
   dirs = gconf_client_all_dirs (client, MC_ACCOUNTS_GCONF_BASE, &error);
 
   if (error != NULL)
     {
-      DEBUG ("Failed to get MC4 account dirs: %s\n",
+      DEBUG ("Failed to get MC4 account dirs: %s",
           error->message);
       g_clear_error (&error);
       g_object_unref (client);
@@ -427,7 +517,7 @@ empathy_import_mc4_accounts (EmpathyConnectionManagers *managers)
 
   for (dir = dirs; NULL != dir; dir = dir->next)
     {
-      if (import_one_account ((gchar *)dir->data, managers, client))
+      if (import_one_account ((gchar *) dir->data, managers, client))
         imported = TRUE;
       g_free (dir->data);
     }