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