f7a54946fd75fa71b7b300f034e2e5216713e74f
[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->name = NULL;
49         tmpl->subject = NULL;
50         tmpl->to = NULL;
51         tmpl->cc = NULL;        
52         tmpl->bcc = NULL;       
53         tmpl->value = NULL;
54
55         while (fgets(buf, sizeof(buf), fp) != NULL) {
56                 if (buf[0] == '\n')
57                         break;
58                 else if (!g_ascii_strncasecmp(buf, "Name:", 5))
59                         tmpl->name = g_strdup(g_strstrip(buf + 5));
60                 else if (!g_ascii_strncasecmp(buf, "To:", 3))
61                         tmpl->to = g_strdup(g_strstrip(buf + 3));
62                 else if (!g_ascii_strncasecmp(buf, "Cc:", 3))
63                         tmpl->cc = g_strdup(g_strstrip(buf + 3));
64                 else if (!g_ascii_strncasecmp(buf, "Bcc:", 4))
65                         tmpl->bcc = g_strdup(g_strstrip(buf + 4));                                              
66                 else if (!g_ascii_strncasecmp(buf, "Subject:", 8))
67                         tmpl->subject = g_strdup(g_strstrip(buf + 8));
68         }
69
70         if (!tmpl->name) {
71                 g_warning("wrong template format\n");
72                 template_free(tmpl);
73                 return NULL;
74         }
75
76         if ((bytes_read = fread(buf, 1, sizeof(buf), fp)) == 0) {
77                 if (ferror(fp)) {
78                         FILE_OP_ERROR(filename, "fread");
79                         template_free(tmpl);
80                         return NULL;
81                 }
82         }
83         fclose(fp);
84         tmpl->value = g_strndup(buf, bytes_read);
85
86         return tmpl;
87 }
88
89 void template_free(Template *tmpl)
90 {
91         g_free(tmpl->name);
92         g_free(tmpl->subject);
93         g_free(tmpl->to);
94         g_free(tmpl->cc);
95         g_free(tmpl->bcc);              
96         g_free(tmpl->value);
97         g_free(tmpl);
98 }
99
100 void template_clear_config(GSList *tmpl_list)
101 {
102         GSList *cur;
103         Template *tmpl;
104
105         for (cur = tmpl_list; cur != NULL; cur = cur->next) {
106                 tmpl = (Template *)cur->data;
107                 template_free(tmpl);
108         }
109         g_slist_free(tmpl_list);
110 }
111
112 gint tmpl_compare(gconstpointer tmpl1, gconstpointer tmpl2)
113 {
114         if ((Template *)tmpl1 == NULL || (Template *)tmpl2 == NULL)
115                 return 0;
116
117         if (((Template *)tmpl1)->name == NULL || ((Template *)tmpl2)->name == NULL)
118                 return 0;
119
120         return (gint) strcmp((char *)((Template *)tmpl1)->name, 
121                              (char *)((Template *)tmpl2)->name);
122 }
123
124 GSList *template_read_config(void)
125 {
126         const gchar *path;
127         gchar *filename;
128         GDir *dir;
129         const gchar *dir_name;
130         struct stat s;
131         Template *tmpl;
132         GSList *tmpl_list = NULL;
133
134         path = get_template_dir();
135         debug_print("%s:%d reading templates dir %s\n",
136                     __FILE__, __LINE__, path);
137
138         if (!is_dir_exist(path)) {
139                 if (make_dir(path) < 0)
140                         return NULL;
141         }
142
143         if ((dir = g_dir_open(path, 0, NULL)) == NULL) {
144                 g_warning("failed to open directory: %s\n", path);
145                 return NULL;
146         }
147
148         while ((dir_name = g_dir_read_name(dir)) != NULL) {
149                 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
150                                        dir_name, NULL);
151
152                 if (g_stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
153                         debug_print("%s:%d %s is not an ordinary file\n",
154                                     __FILE__, __LINE__, filename);
155                         continue;
156                 }
157
158                 tmpl = template_load(filename);
159                 if (tmpl)
160                         tmpl_list = g_slist_insert_sorted(tmpl_list, tmpl, tmpl_compare);
161
162                 g_free(filename);
163         }
164
165         g_dir_close(dir);
166
167         return tmpl_list;
168 }
169
170 void template_write_config(GSList *tmpl_list)
171 {
172         const gchar *path;
173         GSList *cur;
174         Template *tmpl;
175         FILE *fp;
176         gint tmpl_num;
177
178         debug_print("%s:%d writing templates\n", __FILE__, __LINE__);
179
180         path = get_template_dir();
181
182         if (!is_dir_exist(path)) {
183                 if (is_file_exist(path)) {
184                         g_warning("file %s already exists\n", path);
185                         return;
186                 }
187                 if (make_dir(path) < 0)
188                         return;
189         }
190
191         remove_all_files(path);
192
193         for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
194              cur = cur->next, tmpl_num++) {
195                 gchar *filename;
196
197                 tmpl = cur->data;
198
199                 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
200                                        itos(tmpl_num), NULL);
201
202                 if ((fp = g_fopen(filename, "wb")) == NULL) {
203                         FILE_OP_ERROR(filename, "fopen");
204                         g_free(filename);
205                         return;
206                 }
207
208                 fprintf(fp, "Name: %s\n", tmpl->name);
209                 if (tmpl->subject && *tmpl->subject != '\0')
210                         fprintf(fp, "Subject: %s\n", tmpl->subject);
211                 if (tmpl->to && *tmpl->to != '\0')
212                         fprintf(fp, "To: %s\n", tmpl->to);
213                 if (tmpl->cc && *tmpl->cc != '\0')
214                         fprintf(fp, "Cc: %s\n", tmpl->cc);
215                 if (tmpl->bcc && *tmpl->bcc != '\0')
216                         fprintf(fp, "Bcc: %s\n", tmpl->bcc);                                            
217                 fputs("\n", fp);
218                 if (tmpl->value && *tmpl->value != '\0')
219                         fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp);
220                 else
221                         fwrite("", sizeof(gchar), 1, fp);
222
223                 fclose(fp);
224                 g_free(filename);
225         }
226 }
227
228 GSList *template_get_config(void)
229 {
230         if (!template_list)
231                 template_list = template_read_config();
232
233         return template_list;
234 }
235
236 void template_set_config(GSList *tmpl_list)
237 {
238         template_clear_config(template_list);
239         template_write_config(tmpl_list);
240         template_list = tmpl_list;
241 }