2 * Sylpheed templates subsystem
3 * Copyright (C) 2001 Alexander Barinov
4 * Copyright (C) 2001 Hiroyuki Yamamoto
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.
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.
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.
32 #include "../codeconv.h"
34 static GSList *template_list;
36 static Template *template_load(gchar *filename)
43 const gchar *src_codeset = conv_get_current_charset_str();
44 const gchar *dest_codeset = CS_UTF_8;
46 if ((fp = fopen(filename, "rb")) == NULL) {
47 FILE_OP_ERROR(filename, "fopen");
51 tmpl = g_new(Template, 1);
59 while (fgets(buf, sizeof(buf), fp) != NULL) {
64 else if (!g_ascii_strncasecmp(buf, "Name:", 5)) {
65 tmp = conv_codeset_strdup(buf + 5,
68 tmpl->name = tmp ? g_strstrip(tmp) : g_strdup(buf + 5);
69 } else if (!g_ascii_strncasecmp(buf, "Subject:", 8)) {
70 tmp = conv_codeset_strdup(buf + 8,
73 tmpl->subject = tmp ? g_strstrip(tmp) : g_strdup(buf + 8);
74 } else if (!g_ascii_strncasecmp(buf, "To:", 3)) {
75 tmp = conv_codeset_strdup(buf + 3,
78 tmpl->to = tmp ? g_strstrip(tmp) : g_strdup(buf + 3);
79 } else if (!g_ascii_strncasecmp(buf, "Cc:", 3)) {
80 tmp = conv_codeset_strdup(buf + 3,
83 tmpl->cc = tmp ? g_strstrip(tmp) : g_strdup(buf + 3);
84 } else if (!g_ascii_strncasecmp(buf, "Bcc:", 4)) {
85 tmp = conv_codeset_strdup(buf + 4,
88 tmpl->bcc = tmp ? g_strstrip(tmp) : g_strdup(buf + 4);
93 g_warning("wrong template format\n");
98 if ((bytes_read = fread(buf, 1, sizeof(buf), fp)) == 0) {
100 FILE_OP_ERROR(filename, "fread");
106 buf[bytes_read] = '\0';
107 tmpl->value = conv_codeset_strdup(buf, src_codeset, dest_codeset);
109 tmpl->value = g_strdup(buf);
114 void template_free(Template *tmpl)
117 g_free(tmpl->subject);
125 void template_clear_config(GSList *tmpl_list)
130 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
131 tmpl = (Template *)cur->data;
134 g_slist_free(tmpl_list);
137 GSList *template_read_config(void)
145 GSList *tmpl_list = NULL;
147 path = get_template_dir();
148 debug_print("%s:%d reading templates dir %s\n",
149 __FILE__, __LINE__, path);
151 if (!is_dir_exist(path)) {
152 if (make_dir(path) < 0)
156 if ((dp = opendir(path)) == NULL) {
157 FILE_OP_ERROR(path, "opendir");
161 while ((de = readdir(dp)) != NULL) {
162 if (*de->d_name == '.') continue;
164 filename = g_strconcat(path, G_DIR_SEPARATOR_S, de->d_name, NULL);
166 if (stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
167 debug_print("%s:%d %s is not an ordinary file\n",
168 __FILE__, __LINE__, filename);
172 tmpl = template_load(filename);
174 tmpl_list = g_slist_append(tmpl_list, tmpl);
183 void template_write_config(GSList *tmpl_list)
191 debug_print("%s:%d writing templates\n", __FILE__, __LINE__);
193 path = get_template_dir();
195 if (!is_dir_exist(path)) {
196 if (is_file_exist(path)) {
197 g_warning("file %s already exists\n", path);
200 if (make_dir(path) < 0)
204 remove_all_files(path);
206 for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
207 cur = cur->next, tmpl_num++) {
209 const gchar *src_codeset = CS_UTF_8;
210 const gchar *dest_codeset = conv_get_current_charset_str();
216 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
217 itos(tmpl_num), NULL);
219 if ((fp = fopen(filename, "wb")) == NULL) {
220 FILE_OP_ERROR(filename, "fopen");
225 tmp = conv_codeset_strdup(tmpl->name, src_codeset, dest_codeset);
227 tmp = g_strdup(tmpl->name);
228 fprintf(fp, "Name: %s\n", tmp ? tmp : "");
231 if (tmpl->subject && *tmpl->subject != '\0') {
232 tmp = conv_codeset_strdup(tmpl->subject,
233 src_codeset, dest_codeset);
235 tmp = g_strdup(tmpl->subject);
236 fprintf(fp, "Subject: %s\n", tmp);
240 if (tmpl->to && *tmpl->to != '\0') {
241 tmp = conv_codeset_strdup(tmpl->to,
242 src_codeset, dest_codeset);
244 tmp = g_strdup(tmpl->to);
245 fprintf(fp, "To: %s\n", tmp);
249 if (tmpl->cc && *tmpl->cc != '\0') {
250 tmp = conv_codeset_strdup(tmpl->cc,
251 src_codeset, dest_codeset);
253 tmp = g_strdup(tmpl->cc);
254 fprintf(fp, "Cc: %s\n", tmp);
258 if (tmpl->bcc && *tmpl->bcc != '\0') {
259 tmp = conv_codeset_strdup(tmpl->bcc,
260 src_codeset, dest_codeset);
262 tmp = g_strdup(tmpl->bcc);
263 fprintf(fp, "Bcc: %s\n", tmp);
268 tmp = conv_codeset_strdup(tmpl->value,
269 src_codeset, dest_codeset);
271 tmp = g_strdup(tmpl->value);
272 fwrite(tmp, sizeof(gchar) * strlen(tmp), 1, fp);
278 GSList *template_get_config(void)
281 template_list = template_read_config();
283 return template_list;
286 void template_set_config(GSList *tmpl_list)
288 template_clear_config(template_list);
289 template_write_config(tmpl_list);
290 template_list = tmpl_list;