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.
24 #include <glib/gi18n.h>
32 #include "../codeconv.h"
34 static GSList *template_list;
36 static Template *template_load(gchar *filename)
43 if ((fp = fopen(filename, "rb")) == NULL) {
44 FILE_OP_ERROR(filename, "fopen");
48 tmpl = g_new(Template, 1);
56 while (fgets(buf, sizeof(buf), fp) != NULL) {
59 else if (!g_ascii_strncasecmp(buf, "Name:", 5))
60 tmpl->name = g_strdup(g_strstrip(buf + 5));
61 else if (!g_ascii_strncasecmp(buf, "To:", 3))
62 tmpl->to = g_strdup(g_strstrip(buf + 3));
63 else if (!g_ascii_strncasecmp(buf, "Cc:", 3))
64 tmpl->cc = g_strdup(g_strstrip(buf + 3));
65 else if (!g_ascii_strncasecmp(buf, "Bcc:", 4))
66 tmpl->bcc = g_strdup(g_strstrip(buf + 4));
67 else if (!g_ascii_strncasecmp(buf, "Subject:", 8))
68 tmpl->subject = g_strdup(g_strstrip(buf + 8));
72 g_warning("wrong template format\n");
77 if ((bytes_read = fread(buf, 1, sizeof(buf), fp)) == 0) {
79 FILE_OP_ERROR(filename, "fread");
85 tmpl->value = g_strndup(buf, bytes_read);
90 void template_free(Template *tmpl)
93 g_free(tmpl->subject);
101 void template_clear_config(GSList *tmpl_list)
106 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
107 tmpl = (Template *)cur->data;
110 g_slist_free(tmpl_list);
113 GSList *template_read_config(void)
121 GSList *tmpl_list = NULL;
123 path = get_template_dir();
124 debug_print("%s:%d reading templates dir %s\n",
125 __FILE__, __LINE__, path);
127 if (!is_dir_exist(path)) {
128 if (make_dir(path) < 0)
132 if ((dp = opendir(path)) == NULL) {
133 FILE_OP_ERROR(path, "opendir");
137 while ((de = readdir(dp)) != NULL) {
138 if (*de->d_name == '.') continue;
140 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
143 if (stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
144 debug_print("%s:%d %s is not an ordinary file\n",
145 __FILE__, __LINE__, filename);
149 tmpl = template_load(filename);
151 tmpl_list = g_slist_append(tmpl_list, tmpl);
161 void template_write_config(GSList *tmpl_list)
169 debug_print("%s:%d writing templates\n", __FILE__, __LINE__);
171 path = get_template_dir();
173 if (!is_dir_exist(path)) {
174 if (is_file_exist(path)) {
175 g_warning("file %s already exists\n", path);
178 if (make_dir(path) < 0)
182 remove_all_files(path);
184 for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
185 cur = cur->next, tmpl_num++) {
190 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
191 itos(tmpl_num), NULL);
193 if ((fp = fopen(filename, "wb")) == NULL) {
194 FILE_OP_ERROR(filename, "fopen");
199 fprintf(fp, "Name: %s\n", tmpl->name);
200 if (tmpl->subject && *tmpl->subject != '\0')
201 fprintf(fp, "Subject: %s\n", tmpl->subject);
202 if (tmpl->to && *tmpl->to != '\0')
203 fprintf(fp, "To: %s\n", tmpl->to);
204 if (tmpl->cc && *tmpl->cc != '\0')
205 fprintf(fp, "Cc: %s\n", tmpl->cc);
206 if (tmpl->bcc && *tmpl->bcc != '\0')
207 fprintf(fp, "Bcc: %s\n", tmpl->bcc);
209 if (tmpl->value && *tmpl->value != '\0')
210 fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp);
212 fwrite("", sizeof(gchar), 1, fp);
219 GSList *template_get_config(void)
222 template_list = template_read_config();
224 return template_list;
227 void template_set_config(GSList *tmpl_list)
229 template_clear_config(template_list);
230 template_write_config(tmpl_list);
231 template_list = tmpl_list;