Implement config_version in folderlist.xml.
[claws.git] / src / prefs_migration.c
index cf8a103..04f20a6 100644 (file)
 #include "prefs_common.h"
 #include "alertpanel.h"
 
-static void _update_config(gint version)
+extern gint _password_store_config_version;
+
+static gint starting_config_version = 0;
+
+gboolean _version_check(gint ver)
 {
-       GList *cur;
-       PrefsAccount *ac_prefs;
+       if (ver > CLAWS_CONFIG_VERSION) {
+               gchar *msg;
+               gchar *markup;
+               AlertValue av;
+
+               markup = g_strdup_printf(
+                       "<a href=\"%s\"><span underline=\"none\">",
+                       CONFIG_VERSIONS_URI);
+               msg = g_strdup_printf(
+                       _("Your Claws Mail configuration is from a newer "
+                         "version than the version which you are currently "
+                         "using.\n\n"
+                         "This is not recommended.\n\n"
+                         "For further information see the %sClaws Mail "
+                         "website%s.\n\n"
+                         "Do you want to exit now?"),
+                         markup, "</span></a>");
+               g_free(markup);
+               av = alertpanel_full(_("Configuration warning"), msg,
+                                       GTK_STOCK_NO, GTK_STOCK_YES, NULL,
+                                       FALSE, NULL,
+                                       ALERT_ERROR, G_ALERTALTERNATE);
+               g_free(msg);
+
+               if (av != G_ALERTDEFAULT)
+                       return FALSE; /* abort startup */
 
+               return TRUE; /* hic sunt dracones */
+       }
+
+       return TRUE;
+}
+
+static void _update_config_common(gint version)
+{
        debug_print("Updating config version %d to %d.\n", version, version + 1);
 
+       switch (version) {
+               case 1:
+
+                       /* The autochk_interval preference is now
+                        * interpreted as seconds instead of minutes */
+                       prefs_common.autochk_itv *= 60;
+
+                       break;
+
+               default:
+
+                       /* NOOP */
+
+                       break;
+       }
+}
+
+static void _update_config_account(PrefsAccount *ac_prefs, gint version)
+{
+       debug_print("Account '%s': Updating config version from %d to %d.\n",
+                       ac_prefs->account_name, version, version + 1);
+
        switch (version) {
                case 0:
 
@@ -54,64 +112,67 @@ static void _update_config(gint version)
                         * A_IMAP and the rest are from 3 up.
                         * We can't use the macros, since they may change in the
                         * future. Numbers do not change. :) */
-                       for (cur = account_get_list(); cur != NULL; cur = cur->next) {
-                               ac_prefs = (PrefsAccount *)cur->data;
-                               if (ac_prefs->protocol == 1) {
-                                       ac_prefs->protocol = 0;
-                               } else if (ac_prefs->protocol > 2) {
-                                       /* A_IMAP and above gets bumped down by 2. */
-                                       ac_prefs->protocol -= 2;
-                               }
+                       if (ac_prefs->protocol == 1) {
+                               ac_prefs->protocol = 0;
+                       } else if (ac_prefs->protocol > 2) {
+                               /* A_IMAP and above gets bumped down by 2. */
+                               ac_prefs->protocol -= 2;
                        }
 
                        break;
 
-               case 1:
+               default:
 
-                       /* The autochk_interval preference is now
-                        * interpreted as seconds instead of minutes */
-                       prefs_common.autochk_itv *= 60;
+                       /* NOOP */
 
                        break;
+       }
+
+       ac_prefs->config_version = version + 1;
+}
+
+static void _update_config_password_store(gint version)
+{
+       debug_print("Password store: Updating config version from %d to %d.\n",
+                       version, version + 1);
+
+       switch (version) {
+               /* nothing here yet */
 
                default:
+
+                       /* NOOP */
+
                        break;
        }
 }
 
-int prefs_update_config_version()
+static void _update_config_folderlist(gint version)
 {
-       gint ver = prefs_common_get_prefs()->config_version;
+       debug_print("Folderlist: Updating config version from %d to %d.\n",
+                       version, version + 1);
 
-       if (ver > CLAWS_CONFIG_VERSION) {
-               gchar *msg;
-               gchar *markup;
-               AlertValue av;
+       switch (version) {
+               /* nothing here yet */
 
-               markup = g_strdup_printf(
-                       "<a href=\"%s\"><span underline=\"none\">",
-                       CONFIG_VERSIONS_URI);
-               msg = g_strdup_printf(
-                       _("Your Claws Mail configuration is from a newer "
-                         "version than the version which you are currently "
-                         "using.\n\n"
-                         "This is not recommended.\n\n"
-                         "For further information see the %sClaws Mail "
-                         "website%s.\n\n"
-                         "Do you want to exit now?"),
-                         markup, "</span></a>");
-               g_free(markup);
-               av = alertpanel_full(_("Configuration warning"), msg,
-                                       GTK_STOCK_NO, GTK_STOCK_YES, NULL,
-                                       FALSE, NULL,
-                                       ALERT_ERROR, G_ALERTALTERNATE);
-               g_free(msg);
+               default:
 
-               if (av != G_ALERTDEFAULT)
-                       return -1; /* abort startup */
+                       /* NOOP */
 
-               return 0; /* hic sunt dracones */
+                       break;
        }
+}
+
+int prefs_update_config_version_common()
+{
+       gint ver = prefs_common_get_prefs()->config_version;
+
+       /* Store the starting version number for other components'
+        * migration functions. */
+       starting_config_version = ver;
+
+       if (!_version_check(ver))
+               return -1;
 
        debug_print("Starting config update at config_version %d.\n", ver);
        if (ver == CLAWS_CONFIG_VERSION) {
@@ -120,10 +181,109 @@ int prefs_update_config_version()
        }
 
        while (ver < CLAWS_CONFIG_VERSION) {
-               _update_config(ver++);
+               _update_config_common(ver++);
                prefs_common_get_prefs()->config_version = ver;
        }
 
        debug_print("Config update done.\n");
        return 1; /* update done */
 }
+
+int prefs_update_config_version_accounts()
+{
+       GList *cur;
+       PrefsAccount *ac_prefs;
+
+       for (cur = account_get_list(); cur != NULL; cur = cur->next) {
+               ac_prefs = (PrefsAccount *)cur->data;
+
+               if (ac_prefs->config_version == -1) {
+                       /* There was no config_version stored in accountrc, let's assume
+                        * config_version same as clawsrc started at, to avoid breaking
+                        * this account by "upgrading" it unnecessarily. */
+                       debug_print("Account '%s': config_version not saved, using one from clawsrc: %d\n", ac_prefs->account_name, starting_config_version);
+                       ac_prefs->config_version = starting_config_version;
+               }
+
+               gint ver = ac_prefs->config_version;
+
+               debug_print("Account '%s': Starting config update at config_version %d.\n", ac_prefs->account_name, ver);
+
+               if (!_version_check(ver))
+                       return -1;
+
+               if (ver == CLAWS_CONFIG_VERSION) {
+                       debug_print("Account '%s': No update necessary, already at latest config_version.\n", ac_prefs->account_name);
+                       continue;
+               }
+
+               while (ver < CLAWS_CONFIG_VERSION) {
+                       _update_config_account(ac_prefs, ver++);
+               }
+       }
+
+       return 1;
+}
+
+int prefs_update_config_version_password_store()
+{
+       gint ver;
+
+       if (_password_store_config_version == -1) {
+               /* There was no config_version stored in the config, let's assume
+                * config_version same as clawsrc started at, to avoid breaking
+                * the configuration by "upgrading" it unnecessarily. */
+               debug_print("Password store: config_version not saved, using one from clawsrc: %d\n", starting_config_version);
+               _password_store_config_version = starting_config_version;
+       }
+
+       ver = _password_store_config_version;
+
+       debug_print("Starting config update at config_version %d.\n", ver);
+
+       if (!_version_check(ver))
+               return -1;
+
+       if (ver == CLAWS_CONFIG_VERSION) {
+               debug_print("No update necessary, already at latest config_version.\n");
+               return 0; /* nothing to do */
+       }
+
+       while (ver < CLAWS_CONFIG_VERSION) {
+               _update_config_password_store(ver++);
+               _password_store_config_version = ver;
+       }
+
+       debug_print("Config update done.\n");
+       return 1;
+}
+
+int prefs_update_config_version_folderlist(gint from_version)
+{
+       gint ver = from_version;
+
+       if (ver == -1) {
+               /* There was no config_version stored in the config, let's assume
+                * config_version same as clawsrc started at, to avoid breaking
+                * the configuration by "upgrading" it unnecessarily. */
+               debug_print("Folderlist: config_version not saved, using one from clawsrc: %d\n", starting_config_version);
+               ver = starting_config_version;
+       }
+
+       debug_print("Starting config_update at config_version %d,\n", ver);
+
+       if (!_version_check(ver))
+               return -1;
+
+       if (ver == CLAWS_CONFIG_VERSION) {
+               debug_print("No update necessary, already at latest config_version.\n");
+               return 0; /* nothing to do */
+       }
+
+       while (ver < CLAWS_CONFIG_VERSION) {
+               _update_config_folderlist(ver++);
+       }
+
+       debug_print("Config update done.\n");
+       return 1;
+}