fix bug 2979, 'claws fails to load (empty) folderlist.xml and shows account wizard'
[claws.git] / src / file_checker.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2013 Paul Mangan <paul@claws-mail.org> 
4  * and the Claws Mail team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  * 
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #include "claws-features.h"
24 #endif
25
26 #include "defs.h"
27
28 #include <glib.h>
29 #include <glib/gi18n.h>
30 #include <gdk/gdkkeysyms.h>
31 #include <gtk/gtk.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37
38 #include "utils.h"
39 #include "alertpanel.h"
40 #include "folder.h"
41
42 static gboolean verify_folderlist_xml();
43
44 gboolean check_file_integrity() {
45         if (verify_folderlist_xml() != TRUE)
46                 return FALSE;
47
48         return TRUE;
49 }
50
51 static gboolean verify_folderlist_xml() {
52         GNode *node;
53         static gchar *filename = NULL;
54         static gchar *bak = NULL;
55         time_t date;
56         struct tm *ts;
57         gchar buf[BUFFSIZE];
58
59         filename = folder_get_list_path();
60         bak = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
61                           FOLDER_LIST, ".bak", NULL);
62         
63         if (is_file_exist(bak)) {
64                 date = get_file_mtime(bak);
65                 ts = localtime(&date);
66                 strftime(buf, sizeof(buf), "%a %d-%b-%Y %H:%M %Z", ts);
67         }
68         
69         if (!is_file_exist(filename) && is_file_exist(bak)) {
70                 AlertValue aval;
71                 gchar *msg;
72
73                 msg = g_strdup_printf
74                         (_("The file %s is missing! "
75                            "Do you want to use the backup file from %s?"), FOLDER_LIST,buf);
76                 aval = alertpanel(_("Warning"), msg, GTK_STOCK_NO, GTK_STOCK_YES, NULL);
77                 g_free(msg);
78                 if (aval != G_ALERTALTERNATE)
79                         return FALSE;
80                 else {
81                         if (copy_file(bak,filename,FALSE) < 0) {
82                                 alertpanel_warning(_("Could not copy %s to %s"),bak,filename);
83                                 return FALSE;
84                         }
85                         g_free(bak);
86                         return TRUE;
87                 }
88
89         }
90         node = xml_parse_file(filename);
91         if (!node && is_file_exist(bak)) {
92                 AlertValue aval;
93                 gchar *msg;
94
95                 msg = g_strdup_printf
96                         (_("The file %s is empty or corrupted! "
97                            "Do you want to use the backup file from %s?"), FOLDER_LIST,buf);
98                 aval = alertpanel(_("Warning"), msg, GTK_STOCK_NO, GTK_STOCK_YES, NULL);
99                 g_free(msg);
100                 if (aval != G_ALERTALTERNATE)
101                         return FALSE;
102                 else {
103                         if (copy_file(bak,filename,FALSE) < 0) {
104                                 alertpanel_warning(_("Could not copy %s to %s"),bak,filename);
105                                 return FALSE;
106                         }
107                         g_free(bak);    
108                 }
109         }
110
111         return TRUE;    
112 }