659769e187862228a02526fdf5ef6a93ce573ed3
[claws.git] / src / template.c
1 /*
2  * Sylpheed templates subsystem 
3  * Copyright (C) 2001 Alexander Barinov
4  * Copyright (C) 2001 Hiroyuki Yamamoto
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "defs.h"
22
23 #include <glib.h>
24 #include <stdio.h>
25 #include <dirent.h>
26 #include <sys/stat.h>
27 #include <ctype.h>
28
29 #include "intl.h"
30 #include "main.h"
31 #include "template.h"
32 #include "utils.h"
33
34 static GSList *template_list;
35
36 static Template *template_load(gchar *filename)
37 {
38         Template *tmpl;
39         FILE *fp;
40         gchar buf[BUFFSIZE];
41         gint bytes_read;
42
43         if ((fp = fopen(filename, "rb")) == NULL) {
44                 FILE_OP_ERROR(filename, "fopen");
45                 return NULL;
46         }
47
48         tmpl = g_new(Template, 1);
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_strncasecmp(buf, "Name:", 5))
60                         tmpl->name = g_strdup(g_strstrip(buf + 5));
61                 else if (!g_strncasecmp(buf, "Subject:", 8))
62                         tmpl->subject = g_strdup(g_strstrip(buf + 8));
63                 else if (!g_strncasecmp(buf, "To:", 3))
64                         tmpl->to = g_strdup(g_strstrip(buf + 3));
65                 else if (!g_strncasecmp(buf, "Cc:", 3))
66                         tmpl->cc = g_strdup(g_strstrip(buf + 3));
67                 else if (!g_strncasecmp(buf, "Bcc:", 4))
68                         tmpl->bcc = g_strdup(g_strstrip(buf + 4));                                              
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->name);
93         g_free(tmpl->subject);
94         g_free(tmpl->to);
95         g_free(tmpl->cc);
96         g_free(tmpl->bcc);              
97         g_free(tmpl->value);
98         g_free(tmpl);
99 }
100
101 void template_clear_config(GSList *tmpl_list)
102 {
103         GSList *cur;
104         Template *tmpl;
105
106         for (cur = tmpl_list; cur != NULL; cur = cur->next) {
107                 tmpl = (Template *)cur->data;
108                 template_free(tmpl);
109         }
110         g_slist_free(tmpl_list);
111 }
112
113 GSList *template_read_config(void)
114 {
115         gchar *path;
116         gchar *filename;
117         DIR *dp;
118         struct dirent *de;
119         struct stat s;
120         Template *tmpl;
121         GSList *tmpl_list = NULL;
122
123         path = get_template_dir();
124         debug_print("%s:%d reading templates dir %s\n",
125                     __FILE__, __LINE__, path);
126
127         if (!is_dir_exist(path)) {
128                 if (make_dir(path) < 0)
129                         return NULL;
130         }
131
132         if ((dp = opendir(path)) == NULL) {
133                 FILE_OP_ERROR(path, "opendir");
134                 return NULL;
135         }
136
137         while ((de = readdir(dp)) != NULL) {
138                 if (*de->d_name == '.') continue;
139
140                 filename = g_strconcat(path, G_DIR_SEPARATOR_S, de->d_name, NULL);
141
142                 if (stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
143                         debug_print("%s:%d %s is not an ordinary file\n",
144                                     __FILE__, __LINE__, filename);
145                         continue;
146                 }
147
148                 tmpl = template_load(filename);
149                 if (tmpl)
150                         tmpl_list = g_slist_append(tmpl_list, tmpl);
151                 g_free(filename);
152         }
153
154         closedir(dp);
155
156         return tmpl_list;
157 }
158
159 void template_write_config(GSList *tmpl_list)
160 {
161         gchar *path;
162         GSList *cur;
163         Template *tmpl;
164         FILE *fp;
165         gint tmpl_num;
166
167         debug_print("%s:%d writing templates\n", __FILE__, __LINE__);
168
169         path = get_template_dir();
170
171         if (!is_dir_exist(path)) {
172                 if (is_file_exist(path)) {
173                         g_warning(_("file %s already exists\n"), path);
174                         return;
175                 }
176                 if (make_dir(path) < 0)
177                         return;
178         }
179
180         remove_all_files(path);
181
182         for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
183              cur = cur->next, tmpl_num++) {
184                 gchar *filename;
185
186                 tmpl = cur->data;
187
188                 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
189                                        itos(tmpl_num), NULL);
190
191                 if ((fp = fopen(filename, "wb")) == NULL) {
192                         FILE_OP_ERROR(filename, "fopen");
193                         g_free(filename);
194                         g_free(path);
195                         return;
196                 }
197
198                 fprintf(fp, "Name: %s\n", tmpl->name);
199                 if (tmpl->subject && *tmpl->subject != '\0')
200                         fprintf(fp, "Subject: %s\n", tmpl->subject);
201                 if (tmpl->to && *tmpl->to != '\0')
202                         fprintf(fp, "To: %s\n", tmpl->to);
203                 if (tmpl->cc && *tmpl->cc != '\0')
204                         fprintf(fp, "Cc: %s\n", tmpl->cc);
205                 if (tmpl->bcc && *tmpl->bcc != '\0')
206                         fprintf(fp, "Bcc: %s\n", tmpl->bcc);                                            
207                 fputs("\n", fp);
208                 fwrite(tmpl->value, sizeof(gchar) * strlen(tmpl->value), 1, fp);
209                 fclose(fp);
210         }
211 }
212
213 GSList *template_get_config(void)
214 {
215         if (!template_list)
216                 template_list = template_read_config();
217
218         return template_list;
219 }
220
221 void template_set_config(GSList *tmpl_list)
222 {
223         template_clear_config(template_list);
224         template_write_config(tmpl_list);
225         template_list = tmpl_list;
226 }