2007-05-03 [wwp] 2.9.1cvs41
[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 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 void template_write_config(GSList *tmpl_list)
192 {
193         const gchar *path;
194         GSList *cur;
195         Template *tmpl;
196         FILE *fp;
197         gint tmpl_num;
198
199         debug_print("%s:%d writing templates\n", __FILE__, __LINE__);
200
201         path = get_template_dir();
202
203         if (!is_dir_exist(path)) {
204                 if (is_file_exist(path)) {
205                         g_warning("file %s already exists\n", path);
206                         return;
207                 }
208                 if (make_dir(path) < 0)
209                         return;
210         }
211
212         remove_all_files(path);
213
214         for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
215              cur = cur->next, tmpl_num++) {
216                 gchar *filename;
217
218                 tmpl = cur->data;
219
220                 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
221                                        itos(tmpl_num), NULL);
222
223                 if ((fp = g_fopen(filename, "wb")) == NULL) {
224                         FILE_OP_ERROR(filename, "fopen");
225                         g_free(filename);
226                         return;
227                 }
228
229                 fprintf(fp, "Name: %s\n", tmpl->name);
230                 if (tmpl->subject && *tmpl->subject != '\0')
231                         fprintf(fp, "Subject: %s\n", tmpl->subject);
232                 if (tmpl->to && *tmpl->to != '\0')
233                         fprintf(fp, "To: %s\n", tmpl->to);
234                 if (tmpl->cc && *tmpl->cc != '\0')
235                         fprintf(fp, "Cc: %s\n", tmpl->cc);
236                 if (tmpl->bcc && *tmpl->bcc != '\0')
237                         fprintf(fp, "Bcc: %s\n", tmpl->bcc);                                            
238                 fputs("\n", fp);
239                 if (tmpl->value && *tmpl->value != '\0')
240                         fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp);
241                 else
242                         fwrite("", sizeof(gchar), 1, fp);
243
244                 fclose(fp);
245                 g_free(filename);
246         }
247 }
248
249 GSList *template_get_config(void)
250 {
251         if (!template_list)
252                 template_list = template_read_config();
253
254         return template_list;
255 }
256
257 void template_set_config(GSList *tmpl_list)
258 {
259         template_clear_config(template_list);
260         template_write_config(tmpl_list);
261         template_list = tmpl_list;
262 }