029f45537989b9242602c0945f7be1e635b96197
[claws.git] / src / common / template.c
1 /*
2  * Sylpheed templates subsystem 
3  * Copyright (C) 2001 Alexander Barinov
4  * Copyright (C) 2001-2007 Hiroyuki Yamamoto 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 #include "defs.h"
22
23 #include <glib.h>
24 #include <glib/gi18n.h>
25 #include <stdio.h>
26 #include <sys/stat.h>
27 #include <ctype.h>
28
29 #include "utils.h"
30 #include "template.h"
31 #include "../codeconv.h"
32
33 static GSList *template_list;
34
35 static Template *template_load(gchar *filename)
36 {
37         Template *tmpl;
38         FILE *fp;
39         gchar buf[BUFFSIZE];
40         gint bytes_read;
41
42         if ((fp = g_fopen(filename, "rb")) == NULL) {
43                 FILE_OP_ERROR(filename, "fopen");
44                 return NULL;
45         }
46
47         tmpl = g_new(Template, 1);
48         tmpl->load_filename = g_strdup(filename);;
49         tmpl->name = NULL;
50         tmpl->subject = NULL;
51         tmpl->from = NULL;
52         tmpl->to = NULL;
53         tmpl->cc = NULL;        
54         tmpl->bcc = NULL;       
55         tmpl->value = NULL;
56
57         while (fgets(buf, sizeof(buf), fp) != NULL) {
58                 if (buf[0] == '\n')
59                         break;
60                 else if (!g_ascii_strncasecmp(buf, "Name:", 5))
61                         tmpl->name = g_strdup(g_strstrip(buf + 5));
62                 else if (!g_ascii_strncasecmp(buf, "From:", 5))
63                         tmpl->from = g_strdup(g_strstrip(buf + 5));
64                 else if (!g_ascii_strncasecmp(buf, "To:", 3))
65                         tmpl->to = g_strdup(g_strstrip(buf + 3));
66                 else if (!g_ascii_strncasecmp(buf, "Cc:", 3))
67                         tmpl->cc = g_strdup(g_strstrip(buf + 3));
68                 else if (!g_ascii_strncasecmp(buf, "Bcc:", 4))
69                         tmpl->bcc = g_strdup(g_strstrip(buf + 4));                                              
70                 else if (!g_ascii_strncasecmp(buf, "Subject:", 8))
71                         tmpl->subject = g_strdup(g_strstrip(buf + 8));
72         }
73
74         if (!tmpl->name) {
75                 g_warning("wrong template format\n");
76                 template_free(tmpl);
77                 return NULL;
78         }
79
80         if ((bytes_read = fread(buf, 1, sizeof(buf), fp)) == 0) {
81                 if (ferror(fp)) {
82                         FILE_OP_ERROR(filename, "fread");
83                         template_free(tmpl);
84                         return NULL;
85                 }
86         }
87         fclose(fp);
88         tmpl->value = g_strndup(buf, bytes_read);
89
90         return tmpl;
91 }
92
93 void template_free(Template *tmpl)
94 {
95         g_free(tmpl->load_filename);
96         g_free(tmpl->name);
97         g_free(tmpl->subject);
98         g_free(tmpl->to);
99         g_free(tmpl->cc);
100         g_free(tmpl->bcc);              
101         g_free(tmpl->value);
102         g_free(tmpl);
103 }
104
105 static void template_clear_config(GSList *tmpl_list)
106 {
107         GSList *cur;
108         Template *tmpl;
109
110         for (cur = tmpl_list; cur != NULL; cur = cur->next) {
111                 tmpl = (Template *)cur->data;
112                 template_free(tmpl);
113         }
114         g_slist_free(tmpl_list);
115 }
116
117 static gint tmpl_compare(gconstpointer tmpl1, gconstpointer tmpl2)
118 {
119         gchar *basename1, *basename2;
120         long filenum1, filenum2;
121         gint ret = 0;
122
123         if ((Template *)tmpl1 == NULL || (Template *)tmpl2 == NULL)
124                 return 0;
125
126         if (((Template *)tmpl1)->load_filename == NULL || ((Template *)tmpl2)->load_filename == NULL)
127                 return 0;
128
129         basename1 = g_path_get_basename(((Template *)tmpl1)->load_filename);
130         basename2 = g_path_get_basename(((Template *)tmpl2)->load_filename);
131         filenum1 = atol(basename1);
132         filenum2 = atol(basename2);
133         g_free(basename1);
134         g_free(basename2);
135
136         if (filenum1 == 0 || filenum2 == 0)
137                 return 0;
138
139         if (filenum1 < filenum2)
140                 ret = -1;
141         else
142                 if (filenum1 > filenum2)
143                         ret = 1;
144                         
145         return ret;
146 }
147
148 GSList *template_read_config(void)
149 {
150         const gchar *path;
151         gchar *filename;
152         GDir *dir;
153         const gchar *dir_name;
154         struct stat s;
155         Template *tmpl;
156         GSList *tmpl_list = NULL;
157
158         path = get_template_dir();
159         debug_print("%s:%d reading templates dir %s\n",
160                     __FILE__, __LINE__, path);
161
162         if (!is_dir_exist(path)) {
163                 if (make_dir(path) < 0)
164                         return NULL;
165         }
166
167         if ((dir = g_dir_open(path, 0, NULL)) == NULL) {
168                 g_warning("failed to open directory: %s\n", path);
169                 return NULL;
170         }
171
172         while ((dir_name = g_dir_read_name(dir)) != NULL) {
173                 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
174                                        dir_name, NULL);
175
176                 if (g_stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
177                         debug_print("%s:%d %s is not an ordinary file\n",
178                                     __FILE__, __LINE__, filename);
179                         continue;
180                 }
181
182                 tmpl = template_load(filename);
183                 if (tmpl)
184                         tmpl_list = g_slist_insert_sorted(tmpl_list, tmpl, tmpl_compare);
185
186                 g_free(filename);
187         }
188
189         g_dir_close(dir);
190
191         return tmpl_list;
192 }
193
194 #define TRY(func) { \
195 if (!(func)) \
196 { \
197         g_warning("Failed to write template to file\n"); \
198         if (fp) fclose(fp); \
199         if (new) g_unlink(new); \
200         g_free(new); \
201         g_free(filename); \
202         return; \
203 } \
204 }
205
206 static void template_write_config(GSList *tmpl_list)
207 {
208         const gchar *path;
209         GSList *cur;
210         Template *tmpl;
211         FILE *fp;
212         gint tmpl_num;
213
214         debug_print("%s:%d writing templates\n", __FILE__, __LINE__);
215
216         path = get_template_dir();
217
218         if (!is_dir_exist(path)) {
219                 if (is_file_exist(path)) {
220                         g_warning("file %s already exists\n", path);
221                         return;
222                 }
223                 if (make_dir(path) < 0)
224                         return;
225         }
226
227         for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
228              cur = cur->next, tmpl_num++) {
229                 gchar *filename, *new = NULL;
230
231                 tmpl = cur->data;
232
233                 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
234                                        itos(tmpl_num), NULL);
235
236                 if (is_file_exist(filename)) {
237                         new = g_strconcat(filename, ".new", NULL);
238                 }
239
240                 if ((fp = g_fopen(new?new:filename, "wb")) == NULL) {
241                         FILE_OP_ERROR(new?new:filename, "fopen");
242                         g_free(new);
243                         g_free(filename);
244                         return;
245                 }
246
247                 TRY(fprintf(fp, "Name: %s\n", tmpl->name) > 0);
248                 if (tmpl->subject && *tmpl->subject != '\0')
249                         TRY(fprintf(fp, "Subject: %s\n", tmpl->subject) > 0);
250                 if (tmpl->from && *tmpl->from != '\0')
251                         TRY(fprintf(fp, "From: %s\n", tmpl->from) > 0);
252                 if (tmpl->to && *tmpl->to != '\0')
253                         TRY(fprintf(fp, "To: %s\n", tmpl->to) > 0);
254                 if (tmpl->cc && *tmpl->cc != '\0')
255                         TRY(fprintf(fp, "Cc: %s\n", tmpl->cc) > 0);
256                 if (tmpl->bcc && *tmpl->bcc != '\0')
257                         TRY(fprintf(fp, "Bcc: %s\n", tmpl->bcc) > 0);
258
259                 TRY(fputs("\n", fp) != EOF);
260
261                 if (tmpl->value && *tmpl->value != '\0') {
262                         TRY(fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp) == strlen(tmpl->value));
263                 } else {
264                         TRY(fwrite("", sizeof(gchar), 1, fp) == 1);
265                 }
266                 TRY(fclose(fp) != EOF);
267
268                 if (new) {
269                         g_unlink(filename);
270                         rename_force(new, filename);
271                 }
272                 g_free(new);
273                 g_free(filename);
274         }
275         
276         /* remove other templates */
277         while (TRUE) {
278                 gchar *filename = g_strconcat(path, G_DIR_SEPARATOR_S,
279                                        itos(tmpl_num), NULL);
280                 if (is_file_exist(filename)) {
281                         debug_print("removing old template %d\n", tmpl_num);
282                         g_unlink(filename);
283                         g_free(filename);
284                 } else {
285                         g_free(filename);
286                         break;
287                 }
288                 tmpl_num++;
289         }
290 }
291
292 GSList *template_get_config(void)
293 {
294         if (!template_list)
295                 template_list = template_read_config();
296
297         return template_list;
298 }
299
300 void template_set_config(GSList *tmpl_list)
301 {
302         template_clear_config(template_list);
303         template_write_config(tmpl_list);
304         template_list = tmpl_list;
305 }