fix bug 4773, 'remove obsolescent AC_C_CONST'
[claws.git] / src / common / template.c
blob933649e597029fb2c0a04815bcf71524690087d2
1 /*
2 * Claws Mail templates subsystem
3 * Copyright (C) 2001 Alexander Barinov
4 * Copyright (C) 2001-2021 The Claws Mail team
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.
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.
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/>.
21 #include "config.h"
22 #include "defs.h"
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <stdio.h>
27 #include <sys/stat.h>
28 #include <ctype.h>
30 #include "utils.h"
31 #include "template.h"
32 #include "codeconv.h"
33 #include "file-utils.h"
35 static GSList *template_list;
37 static Template *template_load(gchar *filename)
39 Template *tmpl;
40 FILE *fp;
41 gchar buf[BUFFSIZE];
42 gint bytes_read;
44 if ((fp = claws_fopen(filename, "rb")) == NULL) {
45 FILE_OP_ERROR(filename, "claws_fopen");
46 return NULL;
49 tmpl = g_new(Template, 1);
50 tmpl->load_filename = g_strdup(filename);
51 tmpl->name = NULL;
52 tmpl->subject = NULL;
53 tmpl->from = NULL;
54 tmpl->to = NULL;
55 tmpl->cc = NULL;
56 tmpl->bcc = NULL;
57 tmpl->replyto = NULL;
58 tmpl->value = NULL;
60 while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
61 if (buf[0] == '\n')
62 break;
63 else if (!g_ascii_strncasecmp(buf, "Name:", 5))
64 tmpl->name = g_strdup(g_strstrip(buf + 5));
65 else if (!g_ascii_strncasecmp(buf, "From:", 5))
66 tmpl->from = g_strdup(g_strstrip(buf + 5));
67 else if (!g_ascii_strncasecmp(buf, "To:", 3))
68 tmpl->to = g_strdup(g_strstrip(buf + 3));
69 else if (!g_ascii_strncasecmp(buf, "Cc:", 3))
70 tmpl->cc = g_strdup(g_strstrip(buf + 3));
71 else if (!g_ascii_strncasecmp(buf, "Bcc:", 4))
72 tmpl->bcc = g_strdup(g_strstrip(buf + 4));
73 else if (!g_ascii_strncasecmp(buf, "Reply-To:", 9))
74 tmpl->replyto = g_strdup(g_strstrip(buf + 9));
75 else if (!g_ascii_strncasecmp(buf, "Subject:", 8))
76 tmpl->subject = g_strdup(g_strstrip(buf + 8));
79 if (!tmpl->name) {
80 g_warning("wrong template format");
81 template_free(tmpl);
82 claws_fclose(fp);
83 return NULL;
86 if ((bytes_read = claws_fread(buf, 1, sizeof(buf), fp)) == 0) {
87 if (claws_ferror(fp)) {
88 FILE_OP_ERROR(filename, "claws_fread");
89 template_free(tmpl);
90 claws_fclose(fp);
91 return NULL;
94 claws_fclose(fp);
95 tmpl->value = g_strndup(buf, bytes_read);
97 return tmpl;
100 void template_free(Template *tmpl)
102 g_free(tmpl->load_filename);
103 g_free(tmpl->name);
104 g_free(tmpl->subject);
105 g_free(tmpl->from);
106 g_free(tmpl->to);
107 g_free(tmpl->cc);
108 g_free(tmpl->bcc);
109 g_free(tmpl->replyto);
110 g_free(tmpl->value);
111 g_free(tmpl);
114 static void template_clear_config(GSList *tmpl_list)
116 GSList *cur;
117 Template *tmpl;
119 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
120 tmpl = (Template *)cur->data;
121 template_free(tmpl);
123 g_slist_free(tmpl_list);
126 static gint tmpl_compare(gconstpointer tmpl1, gconstpointer tmpl2)
128 gchar *basename1, *basename2;
129 long filenum1, filenum2;
130 gint ret = 0;
132 if ((Template *)tmpl1 == NULL || (Template *)tmpl2 == NULL)
133 return 0;
135 if (((Template *)tmpl1)->load_filename == NULL || ((Template *)tmpl2)->load_filename == NULL)
136 return 0;
138 basename1 = g_path_get_basename(((Template *)tmpl1)->load_filename);
139 basename2 = g_path_get_basename(((Template *)tmpl2)->load_filename);
140 filenum1 = atol(basename1);
141 filenum2 = atol(basename2);
142 g_free(basename1);
143 g_free(basename2);
145 if (filenum1 == 0 || filenum2 == 0)
146 return 0;
148 if (filenum1 < filenum2)
149 ret = -1;
150 else
151 if (filenum1 > filenum2)
152 ret = 1;
154 return ret;
157 GSList *template_read_config(void)
159 const gchar *path;
160 GDir *dir;
161 const gchar *dir_name;
162 GSList *tmpl_list = NULL;
164 path = get_template_dir();
165 debug_print("%s:%d reading templates dir %s\n",
166 __FILE__, __LINE__, path);
168 if (!is_dir_exist(path)) {
169 if (make_dir(path) < 0)
170 return NULL;
173 if ((dir = g_dir_open(path, 0, NULL)) == NULL) {
174 g_warning("failed to open directory: '%s'", path);
175 return NULL;
178 while ((dir_name = g_dir_read_name(dir)) != NULL) {
179 Template *tmpl;
180 GStatBuf s;
181 gchar *filename = g_strconcat(path, G_DIR_SEPARATOR_S,
182 dir_name, NULL);
184 if (g_stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
185 debug_print("%s:%d %s is not an ordinary file\n",
186 __FILE__, __LINE__, filename);
187 g_free(filename);
188 continue;
191 tmpl = template_load(filename);
192 if (tmpl)
193 tmpl_list = g_slist_insert_sorted(tmpl_list, tmpl, tmpl_compare);
195 g_free(filename);
198 g_dir_close(dir);
200 return tmpl_list;
203 #define TRY(func) { \
204 if (!(func)) \
206 g_warning("failed to write template to file"); \
207 if (fp) claws_fclose(fp); \
208 if (new) claws_unlink(new); \
209 g_free(new); \
210 g_free(filename); \
211 return; \
215 #define TRY_NO_CLOSE(func) { \
216 if (!(func)) \
218 g_warning("failed to write template to file"); \
219 if (new) claws_unlink(new); \
220 g_free(new); \
221 g_free(filename); \
222 return; \
226 static void template_write_config(GSList *tmpl_list)
228 const gchar *path;
229 GSList *cur;
230 Template *tmpl;
231 FILE *fp;
232 gint tmpl_num;
234 debug_print("%s:%d writing templates\n", __FILE__, __LINE__);
236 path = get_template_dir();
238 if (!is_dir_exist(path)) {
239 if (is_file_exist(path)) {
240 g_warning("file '%s' already exists", path);
241 return;
243 if (make_dir(path) < 0)
244 return;
247 for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
248 cur = cur->next, tmpl_num++) {
249 gchar *filename, *new = NULL;
251 tmpl = cur->data;
253 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
254 itos(tmpl_num), NULL);
256 if (is_file_exist(filename)) {
257 new = g_strconcat(filename, ".new", NULL);
260 if ((fp = claws_fopen(new?new:filename, "wb")) == NULL) {
261 FILE_OP_ERROR(new?new:filename, "claws_fopen");
262 g_free(new);
263 g_free(filename);
264 return;
267 TRY(fprintf(fp, "Name: %s\n", tmpl->name) > 0);
268 if (tmpl->subject && *tmpl->subject != '\0')
269 TRY(fprintf(fp, "Subject: %s\n", tmpl->subject) > 0);
270 if (tmpl->from && *tmpl->from != '\0')
271 TRY(fprintf(fp, "From: %s\n", tmpl->from) > 0);
272 if (tmpl->to && *tmpl->to != '\0')
273 TRY(fprintf(fp, "To: %s\n", tmpl->to) > 0);
274 if (tmpl->cc && *tmpl->cc != '\0')
275 TRY(fprintf(fp, "Cc: %s\n", tmpl->cc) > 0);
276 if (tmpl->bcc && *tmpl->bcc != '\0')
277 TRY(fprintf(fp, "Bcc: %s\n", tmpl->bcc) > 0);
278 if (tmpl->replyto && *tmpl->replyto != '\0')
279 TRY(fprintf(fp, "Reply-To: %s\n", tmpl->replyto) > 0);
281 TRY(claws_fputs("\n", fp) != EOF);
283 if (tmpl->value && *tmpl->value != '\0') {
284 TRY(claws_fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp) == strlen(tmpl->value));
285 } else {
286 TRY(claws_fwrite("", sizeof(gchar), 1, fp) == 1);
288 TRY_NO_CLOSE(claws_safe_fclose(fp) != EOF);
290 if (new) {
291 if (rename_force(new, filename) < 0) {
292 FILE_OP_ERROR(new, "rename");
295 g_free(new);
296 g_free(filename);
299 /* remove other templates */
300 while (TRUE) {
301 gchar *filename = g_strconcat(path, G_DIR_SEPARATOR_S,
302 itos(tmpl_num), NULL);
303 if (is_file_exist(filename)) {
304 debug_print("removing old template %d\n", tmpl_num);
305 claws_unlink(filename);
306 g_free(filename);
307 } else {
308 g_free(filename);
309 break;
311 tmpl_num++;
315 GSList *template_get_config(void)
317 if (!template_list)
318 template_list = template_read_config();
320 return template_list;
323 void template_set_config(GSList *tmpl_list)
325 template_clear_config(template_list);
326 template_write_config(tmpl_list);
327 template_list = tmpl_list;