Do not consider missing passwordstorerc file as error.
[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 static gint starting_config_version = 0;
38
39 gboolean _version_check(gint ver)
40 {
41         if (ver > CLAWS_CONFIG_VERSION) {
42                 gchar *msg;
43                 gchar *markup;
44                 AlertValue av;
45
46                 markup = g_strdup_printf(
47                         "<a href=\"%s\"><span underline=\"none\">",
48                         CONFIG_VERSIONS_URI);
49                 msg = g_strdup_printf(
50                         _("Your Claws Mail configuration is from a newer "
51                           "version than the version which you are currently "
52                           "using.\n\n"
53                           "This is not recommended.\n\n"
54                           "For further information see the %sClaws Mail "
55                           "website%s.\n\n"
56                           "Do you want to exit now?"),
57                           markup, "</span></a>");
58                 g_free(markup);
59                 av = alertpanel_full(_("Configuration warning"), msg,
60                                         GTK_STOCK_NO, GTK_STOCK_YES, NULL,
61                                         FALSE, NULL,
62                                         ALERT_ERROR, G_ALERTALTERNATE);
63                 g_free(msg);
64
65                 if (av != G_ALERTDEFAULT)
66                         return FALSE; /* abort startup */
67
68                 return TRUE; /* hic sunt dracones */
69         }
70
71         return TRUE;
72 }
73
74 static void _update_config_common(gint version)
75 {
76         debug_print("Updating config version %d to %d.\n", version, version + 1);
77
78         switch (version) {
79                 case 1:
80
81                         /* The autochk_interval preference is now
82                          * interpreted as seconds instead of minutes */
83                         prefs_common.autochk_itv *= 60;
84
85                         break;
86
87                 default:
88
89                         /* NOOP */
90
91                         break;
92         }
93 }
94
95 static void _update_config_account(PrefsAccount *ac_prefs, gint version)
96 {
97         debug_print("Account '%s': Updating config version from %d to %d.\n",
98                         ac_prefs->account_name, version, version + 1);
99
100         switch (version) {
101                 case 0:
102
103                         /* Removing A_APOP and A_RPOP from RecvProtocol enum,
104                          * protocol numbers above A_POP3 need to be adjusted.
105                          *
106                          * In config_version=0:
107                          * A_POP3 is 0,
108                          * A_APOP is 1,
109                          * A_RPOP is 2,
110                          * A_IMAP and the rest are from 3 up.
111                          * We can't use the macros, since they may change in the
112                          * future. Numbers do not change. :) */
113                         if (ac_prefs->protocol == 1) {
114                                 ac_prefs->protocol = 0;
115                         } else if (ac_prefs->protocol > 2) {
116                                 /* A_IMAP and above gets bumped down by 2. */
117                                 ac_prefs->protocol -= 2;
118                         }
119
120                         break;
121
122                 default:
123
124                         /* NOOP */
125
126                         break;
127         }
128
129         ac_prefs->config_version = version + 1;
130 }
131
132 static void _update_config_password_store(gint version)
133 {
134         debug_print("Password store: Updating config version from %d to %d.\n",
135                         version, version + 1);
136
137         switch (version) {
138                 /* nothing here yet */
139
140                 default:
141
142                         /* NOOP */
143
144                         break;
145         }
146 }
147
148 static void _update_config_folderlist(gint version)
149 {
150         debug_print("Folderlist: Updating config version from %d to %d.\n",
151                         version, version + 1);
152
153         switch (version) {
154                 /* nothing here yet */
155
156                 default:
157
158                         /* NOOP */
159
160                         break;
161         }
162 }
163
164 int prefs_update_config_version_common()
165 {
166         gint ver = prefs_common_get_prefs()->config_version;
167
168         /* Store the starting version number for other components'
169          * migration functions. */
170         starting_config_version = ver;
171
172         if (!_version_check(ver))
173                 return -1;
174
175         debug_print("Starting config update at config_version %d.\n", ver);
176         if (ver == CLAWS_CONFIG_VERSION) {
177                 debug_print("No update necessary, already at latest config_version.\n");
178                 return 0; /* nothing to do */
179         }
180
181         while (ver < CLAWS_CONFIG_VERSION) {
182                 _update_config_common(ver++);
183                 prefs_common_get_prefs()->config_version = ver;
184         }
185
186         debug_print("Config update done.\n");
187         return 1; /* update done */
188 }
189
190 int prefs_update_config_version_accounts()
191 {
192         GList *cur;
193         PrefsAccount *ac_prefs;
194
195         for (cur = account_get_list(); cur != NULL; cur = cur->next) {
196                 ac_prefs = (PrefsAccount *)cur->data;
197
198                 if (ac_prefs->config_version == -1) {
199                         /* There was no config_version stored in accountrc, let's assume
200                          * config_version same as clawsrc started at, to avoid breaking
201                          * this account by "upgrading" it unnecessarily. */
202                         debug_print("Account '%s': config_version not saved, using one from clawsrc: %d\n", ac_prefs->account_name, starting_config_version);
203                         ac_prefs->config_version = starting_config_version;
204                 }
205
206                 gint ver = ac_prefs->config_version;
207
208                 debug_print("Account '%s': Starting config update at config_version %d.\n", ac_prefs->account_name, ver);
209
210                 if (!_version_check(ver))
211                         return -1;
212
213                 if (ver == CLAWS_CONFIG_VERSION) {
214                         debug_print("Account '%s': No update necessary, already at latest config_version.\n", ac_prefs->account_name);
215                         continue;
216                 }
217
218                 while (ver < CLAWS_CONFIG_VERSION) {
219                         _update_config_account(ac_prefs, ver++);
220                 }
221         }
222
223         return 1;
224 }
225
226 int prefs_update_config_version_password_store(gint from_version)
227 {
228         gint ver = from_version;
229
230         if (ver == -1) {
231                 /* There was no config_version stored in the config, let's assume
232                  * config_version same as clawsrc started at, to avoid breaking
233                  * the configuration by "upgrading" it unnecessarily. */
234                 debug_print("Password store: config_version not saved, using one from clawsrc: %d\n", starting_config_version);
235                 ver = starting_config_version;
236         }
237
238         debug_print("Starting config update at config_version %d.\n", ver);
239
240         if (!_version_check(ver))
241                 return -1;
242
243         if (ver == CLAWS_CONFIG_VERSION) {
244                 debug_print("No update necessary, already at latest config_version.\n");
245                 return 0; /* nothing to do */
246         }
247
248         while (ver < CLAWS_CONFIG_VERSION) {
249                 _update_config_password_store(ver++);
250         }
251
252         debug_print("Config update done.\n");
253         return 1;
254 }
255
256 int prefs_update_config_version_folderlist(gint from_version)
257 {
258         gint ver = from_version;
259
260         if (ver == -1) {
261                 /* There was no config_version stored in the config, let's assume
262                  * config_version same as clawsrc started at, to avoid breaking
263                  * the configuration by "upgrading" it unnecessarily. */
264                 debug_print("Folderlist: config_version not saved, using one from clawsrc: %d\n", starting_config_version);
265                 ver = starting_config_version;
266         }
267
268         debug_print("Starting config_update at config_version %d,\n", ver);
269
270         if (!_version_check(ver))
271                 return -1;
272
273         if (ver == CLAWS_CONFIG_VERSION) {
274                 debug_print("No update necessary, already at latest config_version.\n");
275                 return 0; /* nothing to do */
276         }
277
278         while (ver < CLAWS_CONFIG_VERSION) {
279                 _update_config_folderlist(ver++);
280         }
281
282         debug_print("Config update done.\n");
283         return 1;
284 }