ccf65d1acd1010cd1dcf68c47226713541be1f78
[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->to = NULL;
52         tmpl->cc = NULL;        
53         tmpl->bcc = 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_ascii_strncasecmp(buf, "Name:", 5))
60                         tmpl->name = g_strdup(g_strstrip(buf + 5));
61                 else if (!g_ascii_strncasecmp(buf, "To:", 3))
62                         tmpl->to = g_strdup(g_strstrip(buf + 3));
63                 else if (!g_ascii_strncasecmp(buf, "Cc:", 3))
64                         tmpl->cc = g_strdup(g_strstrip(buf + 3));
65                 else if (!g_ascii_strncasecmp(buf, "Bcc:", 4))
66                         tmpl->bcc = g_strdup(g_strstrip(buf + 4));                                              
67                 else if (!g_ascii_strncasecmp(buf, "Subject:", 8))
68                         tmpl->subject = g_strdup(g_strstrip(buf + 8));
69         }
70
71         if (!tmpl->name) {
72                 g_warning("wrong template format\n");
73                 template_free(tmpl);
74                 return NULL;
75         }
76
77         if ((bytes_read = fread(buf, 1, sizeof(buf), fp)) == 0) {
78                 if (ferror(fp)) {
79                         FILE_OP_ERROR(filename, "fread");
80                         template_free(tmpl);
81                         return NULL;
82                 }
83         }
84         fclose(fp);
85         tmpl->value = g_strndup(buf, bytes_read);
86
87         return tmpl;
88 }
89
90 void template_free(Template *tmpl)
91 {
92         g_free(tmpl->load_filename);
93         g_free(tmpl->name);
94         g_free(tmpl->subject);
95         g_free(tmpl->to);
96         g_free(tmpl->cc);
97         g_free(tmpl->bcc);              
98         g_free(tmpl->value);
99         g_free(tmpl);
100 }
101
102 void template_clear_config(GSList *tmpl_list)
103 {
104         GSList *cur;
105         Template *tmpl;
106
107         for (cur = tmpl_list; cur != NULL; cur = cur->next) {
108                 tmpl = (Template *)cur->data;
109                 template_free(tmpl);
110         }
111         g_slist_free(tmpl_list);
112 }
113
114 static gint tmpl_compare(gconstpointer tmpl1, gconstpointer tmpl2)
115 {
116         gchar *basename1, *basename2;
117         long filenum1, filenum2;
118         gint ret = 0;
119
120         if ((Template *)tmpl1 == NULL || (Template *)tmpl2 == NULL)
121                 return 0;
122
123         if (((Template *)tmpl1)->load_filename == NULL || ((Template *)tmpl2)->load_filename == NULL)
124                 return 0;
125
126         basename1 = g_path_get_basename(((Template *)tmpl1)->load_filename);
127         basename2 = g_path_get_basename(((Template *)tmpl2)->load_filename);
128         filenum1 = atol(basename1);
129         filenum2 = atol(basename2);
130         g_free(basename1);
131         g_free(basename2);
132
133         if (filenum1 == 0 || filenum2 == 0)
134                 return 0;
135
136         if (filenum1 < filenum2)
137                 ret = -1;
138         else
139                 if (filenum1 > filenum2)
140                         ret = 1;
141                         
142         return ret;
143 }
144
145 GSList *template_read_config(void)
146 {
147         const gchar *path;
148         gchar *filename;
149         GDir *dir;
150         const gchar *dir_name;
151         struct stat s;
152         Template *tmpl;
153         GSList *tmpl_list = NULL;
154
155         path = get_template_dir();
156         debug_print("%s:%d reading templates dir %s\n",
157                     __FILE__, __LINE__, path);
158
159         if (!is_dir_exist(path)) {
160                 if (make_dir(path) < 0)
161                         return NULL;
162         }
163
164         if ((dir = g_dir_open(path, 0, NULL)) == NULL) {
165                 g_warning("failed to open directory: %s\n", path);
166                 return NULL;
167         }
168
169         while ((dir_name = g_dir_read_name(dir)) != NULL) {
170                 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
171                                        dir_name, NULL);
172
173                 if (g_stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
174                         debug_print("%s:%d %s is not an ordinary file\n",
175                                     __FILE__, __LINE__, filename);
176                         continue;
177                 }
178
179                 tmpl = template_load(filename);
180                 if (tmpl)
181                         tmpl_list = g_slist_insert_sorted(tmpl_list, tmpl, tmpl_compare);
182
183                 g_free(filename);
184         }
185
186         g_dir_close(dir);
187
188         return tmpl_list;
189 }
190
191 #define TRY(func) { \
192 if (!(func)) \
193 { \
194         g_warning("Failed to write template to file\n"); \
195         if (fp) fclose(fp); \
196         if (new) g_unlink(new); \
197         g_free(new); \
198         g_free(filename); \
199         return; \
200 } \
201 }
202
203 void template_write_config(GSList *tmpl_list)
204 {
205         const gchar *path;
206         GSList *cur;
207         Template *tmpl;
208         FILE *fp;
209         gint tmpl_num;
210
211         debug_print("%s:%d writing templates\n", __FILE__, __LINE__);
212
213         path = get_template_dir();
214
215         if (!is_dir_exist(path)) {
216                 if (is_file_exist(path)) {
217                         g_warning("file %s already exists\n", path);
218                         return;
219                 }
220                 if (make_dir(path) < 0)
221                         return;
222         }
223
224         for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
225              cur = cur->next, tmpl_num++) {
226                 gchar *filename, *new = NULL;
227
228                 tmpl = cur->data;
229
230                 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
231                                        itos(tmpl_num), NULL);
232
233                 if (is_file_exist(filename)) {
234                         new = g_strconcat(filename, ".new", NULL);
235                 }
236
237                 if ((fp = g_fopen(new?new:filename, "wb")) == NULL) {
238                         FILE_OP_ERROR(new?new:filename, "fopen");
239                         g_free(new);
240                         g_free(filename);
241                         return;
242                 }
243
244                 TRY(fprintf(fp, "Name: %s\n", tmpl->name) > 0);
245                 if (tmpl->subject && *tmpl->subject != '\0')
246                         TRY(fprintf(fp, "Subject: %s\n", tmpl->subject) > 0);
247                 if (tmpl->to && *tmpl->to != '\0')
248                         TRY(fprintf(fp, "To: %s\n", tmpl->to) > 0);
249                 if (tmpl->cc && *tmpl->cc != '\0')
250                         TRY(fprintf(fp, "Cc: %s\n", tmpl->cc) > 0);
251                 if (tmpl->bcc && *tmpl->bcc != '\0')
252                         TRY(fprintf(fp, "Bcc: %s\n", tmpl->bcc) > 0);
253
254                 TRY(fputs("\n", fp) != EOF);
255
256                 if (tmpl->value && *tmpl->value != '\0') {
257                         TRY(fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp) == strlen(tmpl->value));
258                 } else {
259                         TRY(fwrite("", sizeof(gchar), 1, fp) == 1);
260                 }
261                 TRY(fclose(fp) != EOF);
262
263                 if (new) {
264                         g_unlink(filename);
265                         rename_force(new, filename);
266                 }
267                 g_free(new);
268                 g_free(filename);
269         }
270 }
271
272 GSList *template_get_config(void)
273 {
274         if (!template_list)
275                 template_list = template_read_config();
276
277         return template_list;
278 }
279
280 void template_set_config(GSList *tmpl_list)
281 {
282         template_clear_config(template_list);
283         template_write_config(tmpl_list);
284         template_list = tmpl_list;
285 }