summaryrefslogtreecommitdiff
path: root/services
diff options
context:
space:
mode:
authorMatthew Barnes <mbarnes@redhat.com>2012-12-24 08:43:53 -0500
committerMatthew Barnes <mbarnes@redhat.com>2012-12-24 08:45:02 -0500
commitcda199f86d740c4db6e9dd532ebfe0da867e8f65 (patch)
treebba9a53161e97ad2122c9d56d1424d4899fa8b86 /services
parentf274e9d1f617017d88670f18d0e7ec59e889a79b (diff)
downloadevolution-data-server-cda199f86d740c4db6e9dd532ebfe0da867e8f65.tar.gz
Add automatic IMAP to IMAPX account migration.
Uses the new "tweak-key-file" ESourceRegistryServer signal.
Diffstat (limited to 'services')
-rw-r--r--services/evolution-source-registry/Makefile.am1
-rw-r--r--services/evolution-source-registry/evolution-source-registry-migrate-imap-to-imapx.c144
-rw-r--r--services/evolution-source-registry/evolution-source-registry.c11
3 files changed, 156 insertions, 0 deletions
diff --git a/services/evolution-source-registry/Makefile.am b/services/evolution-source-registry/Makefile.am
index 86a533979..e276680de 100644
--- a/services/evolution-source-registry/Makefile.am
+++ b/services/evolution-source-registry/Makefile.am
@@ -27,6 +27,7 @@ evolution_source_registry_SOURCES = \
evolution-source-registry.c \
evolution-source-registry-migrate-basedir.c \
evolution-source-registry-migrate-sources.c \
+ evolution-source-registry-migrate-imap-to-imapx.c \
$(NULL)
evolution_source_registry_LDADD = \
diff --git a/services/evolution-source-registry/evolution-source-registry-migrate-imap-to-imapx.c b/services/evolution-source-registry/evolution-source-registry-migrate-imap-to-imapx.c
new file mode 100644
index 000000000..4bfb2375a
--- /dev/null
+++ b/services/evolution-source-registry/evolution-source-registry-migrate-imap-to-imapx.c
@@ -0,0 +1,144 @@
+/*
+ * evolution-source-registry-migrate-imap-to-imapx.c
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) version 3.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with the program; if not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+#include <errno.h>
+#include <glib/gstdio.h>
+
+#include <libebackend/libebackend.h>
+
+/* Forward Declarations */
+gboolean evolution_source_registry_migrate_imap_to_imapx
+ (ESourceRegistryServer *server,
+ GKeyFile *key_file,
+ const gchar *uid);
+
+gboolean
+evolution_source_registry_migrate_imap_to_imapx (ESourceRegistryServer *server,
+ GKeyFile *key_file,
+ const gchar *uid)
+{
+ GHashTable *settings;
+ const gchar *group_name;
+ gboolean backend_is_imap;
+ gchar *cache_dir;
+ gchar *trash_dir;
+ gchar *value;
+ gint ii;
+
+ const gchar *imap_keys[] = {
+ "CheckAll",
+ "CheckSubscribed",
+ "FilterAll",
+ "FilterJunk",
+ "FilterJunkInbox",
+ "Namespace",
+ "RealJunkPath",
+ "RealTrashPath",
+ "ShellCommand",
+ "UseNamespace",
+ "UseRealJunkPath",
+ "UseRealTrashPath",
+ "UseShellCommand"
+ };
+
+ /* Convert mail accounts with BackendName=imap to imapx. */
+
+ group_name = E_SOURCE_EXTENSION_MAIL_ACCOUNT;
+ if (!g_key_file_has_group (key_file, group_name))
+ return FALSE;
+
+ value = g_key_file_get_string (
+ key_file, group_name, "BackendName", NULL);
+ backend_is_imap = (g_strcmp0 (value, "imap") == 0);
+ g_free (value);
+
+ if (!backend_is_imap)
+ return FALSE;
+
+ g_print ("Converting %s from IMAP to IMAPX\n", uid);
+
+ g_key_file_set_string (key_file, group_name, "BackendName", "imapx");
+
+ /* Gather IMAP backend settings into a hash table. */
+
+ settings = g_hash_table_new_full (
+ (GHashFunc) g_str_hash,
+ (GEqualFunc) g_str_equal,
+ (GDestroyNotify) g_free,
+ (GDestroyNotify) g_free);
+
+ group_name = e_source_camel_get_extension_name ("imap");
+
+ if (g_key_file_has_group (key_file, group_name)) {
+ gchar **keys;
+ gint ii;
+
+ keys = g_key_file_get_keys (
+ key_file, group_name, NULL, NULL);
+
+ for (ii = 0; keys != NULL && keys[ii] != NULL; ii++) {
+ /* Hash table takes ownership of key and value. */
+ value = g_key_file_get_value (
+ key_file, group_name, keys[ii], NULL);
+ g_hash_table_insert (settings, keys[ii], value);
+ keys[ii] = NULL;
+ }
+
+ /* Use g_free() instead of g_strfreev() since we
+ * stripped the array of its strings. It's just
+ * an array of NULL pointers now. */
+ g_free (keys);
+
+ g_key_file_remove_group (key_file, group_name, NULL);
+ }
+
+ /* Translate IMAP settings into IMAPX settings. Fortunately
+ * all matching settings are identical in name and data type. */
+
+ group_name = e_source_camel_get_extension_name ("imapx");
+
+ for (ii = 0; ii < G_N_ELEMENTS (imap_keys); ii++) {
+ value = g_hash_table_lookup (settings, imap_keys[ii]);
+ if (value != NULL)
+ g_key_file_set_value (
+ key_file, group_name,
+ imap_keys[ii], value);
+ }
+
+ g_hash_table_destroy (settings);
+
+ /* Move the cache directory aside. IMAPX has a different
+ * cache format, so the cache will need to be regenerated. */
+
+ cache_dir = g_build_filename (
+ e_get_user_cache_dir (), "mail", uid, NULL);
+ trash_dir = g_build_filename (
+ e_get_user_cache_dir (), "mail", "trash", uid, NULL);
+
+ if (g_rename (cache_dir, trash_dir) == -1) {
+ g_warning (
+ "Failed to move '%s' to trash: %s",
+ cache_dir, g_strerror (errno));
+ }
+
+ g_free (cache_dir);
+ g_free (trash_dir);
+
+ return TRUE;
+}
+
diff --git a/services/evolution-source-registry/evolution-source-registry.c b/services/evolution-source-registry/evolution-source-registry.c
index ab2bb1084..9c60ad878 100644
--- a/services/evolution-source-registry/evolution-source-registry.c
+++ b/services/evolution-source-registry/evolution-source-registry.c
@@ -31,6 +31,11 @@
void evolution_source_registry_migrate_basedir (void);
void evolution_source_registry_migrate_sources (void);
+gboolean evolution_source_registry_migrate_imap_to_imapx
+ (ESourceRegistryServer *server,
+ GKeyFile *key_file,
+ const gchar *uid);
+
gint
main (gint argc,
gchar **argv)
@@ -64,6 +69,12 @@ reload:
server = e_source_registry_server_new ();
+ /* Convert "imap" mail accounts to "imapx". */
+ g_signal_connect (
+ server, "tweak-key-file", G_CALLBACK (
+ evolution_source_registry_migrate_imap_to_imapx),
+ NULL);
+
/* Failure here is fatal. Don't even try to keep going. */
e_source_registry_server_load_all (
E_SOURCE_REGISTRY_SERVER (server), &error);