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