251ba19fa9fb49a2ca3660b4e578d593e12c5bfd
[claws.git] / src / prefs_migration.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2016 the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #include "claws-features.h"
22 #endif
23
24 #ifdef ENABLE_NLS
25 #include <glib/gi18n.h>
26 #else
27 #define _(a) (a)
28 #define N_(a) (a)
29 #endif
30
31 #include "defs.h"
32 #include "account.h"
33 #include "prefs_account.h"
34 #include "prefs_common.h"
35 #include "alertpanel.h"
36
37 extern gint _password_store_config_version;
38
39 static gint starting_config_version = 0;
40
41 gboolean _version_check(gint ver)
42 {
43         if (ver > CLAWS_CONFIG_VERSION) {
44                 gchar *msg;
45                 gchar *markup;
46                 AlertValue av;
47
48                 markup = g_strdup_printf(
49                         "<a href=\"%s\"><span underline=\"none\">",
50                         CONFIG_VERSIONS_URI);
51                 msg = g_strdup_printf(
52                         _("Your Claws Mail configuration is from a newer "
53                           "version than the version which you are currently "
54                           "using.\n\n"
55                           "This is not recommended.\n\n"
56                           "For further information see the %sClaws Mail "
57                           "website%s.\n\n"
58                           "Do you want to exit now?"),
59                           markup, "</span></a>");
60                 g_free(markup);
61                 av = alertpanel_full(_("Configuration warning"), msg,
62                                         GTK_STOCK_NO, GTK_STOCK_YES, NULL,
63                                         FALSE, NULL,
64                                         ALERT_ERROR, G_ALERTALTERNATE);
65                 g_free(msg);
66
67                 if (av != G_ALERTDEFAULT)
68                         return FALSE; /* abort startup */
69
70                 return TRUE; /* hic sunt dracones */
71         }
72
73         return TRUE;
74 }
75
76 static void _update_config_common(gint version)
77 {
78         debug_print("Updating config version %d to %d.\n", version, version + 1);
79
80         switch (version) {
81                 case 1:
82
83                         /* The autochk_interval preference is now
84                          * interpreted as seconds instead of minutes */
85                         prefs_common.autochk_itv *= 60;
86
87                         break;
88
89                 default:
90
91                         /* NOOP */
92
93                         break;
94         }
95 }
96
97 static void _update_config_account(PrefsAccount *ac_prefs, gint version)
98 {
99         debug_print("Account '%s': Updating config version from %d to %d.\n",
100                         ac_prefs->account_name, version, version + 1);
101
102         switch (version) {
103                 case 0:
104
105                         /* Removing A_APOP and A_RPOP from RecvProtocol enum,
106                          * protocol numbers above A_POP3 need to be adjusted.
107                          *
108                          * In config_version=0:
109                          * A_POP3 is 0,
110                          * A_APOP is 1,
111                          * A_RPOP is 2,
112                          * A_IMAP and the rest are from 3 up.
113                          * We can't use the macros, since they may change in the
114                          * future. Numbers do not change. :) */
115                         if (ac_prefs->protocol == 1) {
116                                 ac_prefs->protocol = 0;
117                         } else if (ac_prefs->protocol > 2) {
118                                 /* A_IMAP and above gets bumped down by 2. */
119                                 ac_prefs->protocol -= 2;
120                         }
121
122                         break;
123
124                 default:
125
126                         /* NOOP */
127
128                         break;
129         }
130
131         ac_prefs->config_version = version + 1;
132 }
133
134 static void _update_config_password_store(gint version)
135 {
136         debug_print("Password store: Updating config version from %d to %d.\n",
137                         version, version + 1);
138
139         switch (version) {
140                 /* nothing here yet */
141
142                 default:
143
144                         /* NOOP */
145
146                         break;
147         }
148 }
149
150 int prefs_update_config_version_common()
151 {
152         gint ver = prefs_common_get_prefs()->config_version;
153
154         /* Store the starting version number for other components'
155          * migration functions. */
156         starting_config_version = ver;
157
158         if (!_version_check(ver))
159                 return -1;
160
161         debug_print("Starting config update at config_version %d.\n", ver);
162         if (ver == CLAWS_CONFIG_VERSION) {
163                 debug_print("No update necessary, already at latest config_version.\n");
164                 return 0; /* nothing to do */
165         }
166
167         while (ver < CLAWS_CONFIG_VERSION) {
168                 _update_config_common(ver++);
169                 prefs_common_get_prefs()->config_version = ver;
170         }
171
172         debug_print("Config update done.\n");
173         return 1; /* update done */
174 }
175
176 int prefs_update_config_version_accounts()
177 {
178         GList *cur;
179         PrefsAccount *ac_prefs;
180
181         for (cur = account_get_list(); cur != NULL; cur = cur->next) {
182                 ac_prefs = (PrefsAccount *)cur->data;
183
184                 if (ac_prefs->config_version == -1) {
185                         /* There was no config_version stored in accountrc, let's assume
186                          * config_version same as clawsrc started at, to avoid breaking
187                          * this account by "upgrading" it unnecessarily. */
188                         debug_print("Account '%s': config_version not saved, using one from clawsrc: %d\n", ac_prefs->account_name, starting_config_version);
189                         ac_prefs->config_version = starting_config_version;
190                 }
191
192                 gint ver = ac_prefs->config_version;
193
194                 debug_print("Account '%s': Starting config update at config_version %d.\n", ac_prefs->account_name, ver);
195
196                 if (!_version_check(ver))
197                         return -1;
198
199                 if (ver == CLAWS_CONFIG_VERSION) {
200                         debug_print("Account '%s': No update necessary, already at latest config_version.\n", ac_prefs->account_name);
201                         continue;
202                 }
203
204                 while (ver < CLAWS_CONFIG_VERSION) {
205                         _update_config_account(ac_prefs, ver++);
206                 }
207         }
208
209         return 1;
210 }
211
212 int prefs_update_config_version_password_store()
213 {
214         gint ver;
215
216         if (_password_store_config_version == -1) {
217                 /* There was no config_version stored in the config, let's assume
218                  * config_version same as clawsrc started at, to avoid breaking
219                  * the configuration by "upgrading" it unnecessarily. */
220                 debug_print("Password store: config_version not saved, using one from clawsrc: %d\n", starting_config_version);
221                 _password_store_config_version = starting_config_version;
222         }
223
224         ver = _password_store_config_version;
225
226         debug_print("Starting config update at config_version %d.\n", ver);
227
228         if (!_version_check(ver))
229                 return -1;
230
231         if (ver == CLAWS_CONFIG_VERSION) {
232                 debug_print("No update necessary, already at latest config_version.\n");
233                 return 0; /* nothing to do */
234         }
235
236         while (ver < CLAWS_CONFIG_VERSION) {
237                 _update_config_password_store(ver++);
238                 _password_store_config_version = ver;
239         }
240
241         debug_print("Config update done.\n");
242         return 1;
243 }