update dutch translation
[claws.git] / src / template.c
1 /*
2  * Sylpheed templates subsystem 
3  * Copyright (C) 2001 Alexander Barinov
4  * Copyright (C) 2001 Hiroyuki Yamamoto
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 2 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, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "defs.h"
22
23 #include <glib.h>
24 #include <stdio.h>
25 #include <dirent.h>
26 #include <sys/stat.h>
27 #include <ctype.h>
28
29 #include "intl.h"
30 #include "main.h"
31 #include "template.h"
32 #include "utils.h"
33
34 static GSList *template_list;
35
36 static Template *template_load(gchar *filename)
37 {
38         Template *tmpl;
39         FILE *fp;
40         gchar buf[BUFFSIZE];
41         gint bytes_read;
42
43         debug_print(_("%s:%d loading template from %s\n"), __FILE__, __LINE__, filename);
44
45         if ((fp = fopen(filename, "r")) == NULL) {
46                 FILE_OP_ERROR(filename, "fopen");
47                 return NULL;
48         }
49
50         tmpl = g_new(Template, 1);
51         tmpl->name = NULL;
52         tmpl->subject = NULL;
53         tmpl->to = NULL;
54         tmpl->value = NULL;
55
56         while (fgets(buf, sizeof(buf), fp) != NULL) {
57                 if (buf[0] == '\n')
58                         break;
59                 else if (!g_strncasecmp(buf, "Name:", 5))
60                         tmpl->name = g_strdup(g_strstrip(buf + 5));
61                 else if (!g_strncasecmp(buf, "Subject:", 8))
62                         tmpl->subject = g_strdup(g_strstrip(buf + 8));
63                 else if (!g_strncasecmp(buf, "To:", 3))
64                         tmpl->to = g_strdup(g_strstrip(buf + 3));
65         }
66
67         if (!tmpl->name) {
68                 g_warning("wrong template format\n");
69                 template_free(tmpl);
70                 return NULL;
71         }
72
73         if ((bytes_read = fread(buf, 1, sizeof(buf), fp)) == 0) {
74                 if (ferror(fp)) {
75                         FILE_OP_ERROR(filename, "fread");
76                         template_free(tmpl);
77                         return NULL;
78                 }
79         }
80         fclose(fp);
81         tmpl->value = g_strndup(buf, bytes_read);
82
83         return tmpl;
84 }
85
86 void template_free(Template *tmpl)
87 {
88         g_free(tmpl->name);
89         g_free(tmpl->subject);
90         g_free(tmpl->to);
91         g_free(tmpl->value);
92         g_free(tmpl);
93 }
94
95 void template_clear_config(GSList *tmpl_list)
96 {
97         GSList *cur;
98         Template *tmpl;
99
100         for (cur = tmpl_list; cur != NULL; cur = cur->next) {
101                 tmpl = (Template *)cur->data;
102                 template_free(tmpl);
103         }
104         g_slist_free(tmpl_list);
105 }
106
107 GSList *template_read_config(void)
108 {
109         gchar *path;
110         gchar *filename;
111         DIR *dp;
112         struct dirent *de;
113         struct stat s;
114         Template *tmpl;
115         GSList *tmpl_list = NULL;
116
117         path = get_template_dir();
118         debug_print(_("%s:%d reading templates dir %s\n"), __FILE__, __LINE__, path);
119
120         if (!is_dir_exist(path)) {
121                 if (mkdir(path, S_IRWXU) < 0) {
122                         FILE_OP_ERROR(path, "mkdir");
123                         return NULL;
124                 }
125         }
126
127         if ((dp = opendir(path)) == NULL) {
128                 FILE_OP_ERROR(path, "opendir");
129                 return NULL;
130         }
131
132         while ((de = readdir(dp)) != NULL) {
133                 if (*de->d_name == '.') continue;
134
135                 filename = g_strconcat(path, G_DIR_SEPARATOR_S, de->d_name, NULL);
136                 debug_print(_("%s:%d found file %s\n"), __FILE__, __LINE__, filename);
137
138                 if (stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
139                         debug_print(_("%s:%d %s is not an ordinary file\n"), 
140                                     __FILE__, __LINE__, filename);
141                         continue;
142                 }
143
144                 tmpl = template_load(filename);
145                 if (tmpl)
146                         tmpl_list = g_slist_append(tmpl_list, tmpl);
147                 g_free(filename);
148         }
149
150         closedir(dp);
151
152         return tmpl_list;
153 }
154
155 void template_write_config(GSList *tmpl_list)
156 {
157         gchar *path;
158         GSList *cur;
159         Template *tmpl;
160         FILE *fp;
161         gint tmpl_num;
162
163         path = get_template_dir();
164
165         if (!is_dir_exist(path)) {
166                 if (is_file_exist(path)) {
167                         g_warning(_("file %s already exists\n"), path);
168                         return;
169                 }
170                 if (mkdir(path, S_IRWXU) < 0) {
171                         FILE_OP_ERROR(path, "mkdir");
172                         return;
173                 }
174         }
175
176         remove_all_files(path);
177
178         for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
179              cur = cur->next, tmpl_num++) {
180                 gchar *filename;
181
182                 tmpl = cur->data;
183
184                 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
185                                        itos(tmpl_num), NULL);
186
187                 if ((fp = fopen(filename, "w")) == NULL) {
188                         FILE_OP_ERROR(filename, "fopen");
189                         g_free(filename);
190                         g_free(path);
191                         return;
192                 }
193
194                 debug_print(_("%s:%d writing template \"%s\" to %s\n"),
195                             __FILE__, __LINE__, tmpl->name, filename);
196                 fprintf(fp, "Name: %s\n", tmpl->name);
197                 if (tmpl->subject && *tmpl->subject != '\0')
198                         fprintf(fp, "Subject: %s\n", tmpl->subject);
199                 if (tmpl->to && *tmpl->to != '\0')
200                         fprintf(fp, "To: %s\n", tmpl->to);
201                 fputs("\n", fp);
202                 fwrite(tmpl->value, sizeof(gchar) * strlen(tmpl->value), 1,
203                        fp);
204                 fclose(fp);
205         }
206 }
207
208 GSList *template_get_config(void)
209 {
210         if (!template_list)
211                 template_list = template_read_config();
212
213         return template_list;
214 }
215
216 void template_set_config(GSList *tmpl_list)
217 {
218         template_clear_config(template_list);
219         template_write_config(tmpl_list);
220         template_list = tmpl_list;
221 }