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