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) {
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));
74 g_warning("wrong template format\n");
79 if ((bytes_read = fread(buf, 1, sizeof(buf), fp)) == 0) {
81 FILE_OP_ERROR(filename, "fread");
87 tmpl->value = g_strndup(buf, bytes_read);
92 void template_free(Template *tmpl)
95 g_free(tmpl->subject);
103 void template_clear_config(GSList *tmpl_list)
108 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
109 tmpl = (Template *)cur->data;
112 g_slist_free(tmpl_list);
115 GSList *template_read_config(void)
123 GSList *tmpl_list = NULL;
125 path = get_template_dir();
126 debug_print("%s:%d reading templates dir %s\n",
127 __FILE__, __LINE__, path);
129 if (!is_dir_exist(path)) {
130 if (make_dir(path) < 0)
134 if ((dp = opendir(path)) == NULL) {
135 FILE_OP_ERROR(path, "opendir");
139 while ((de = readdir(dp)) != NULL) {
140 if (*de->d_name == '.') continue;
142 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
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);
151 tmpl = template_load(filename);
153 tmpl_list = g_slist_append(tmpl_list, tmpl);
163 void template_write_config(GSList *tmpl_list)
171 debug_print("%s:%d writing templates\n", __FILE__, __LINE__);
173 path = get_template_dir();
175 if (!is_dir_exist(path)) {
176 if (is_file_exist(path)) {
177 g_warning("file %s already exists\n", path);
180 if (make_dir(path) < 0)
184 remove_all_files(path);
186 for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
187 cur = cur->next, tmpl_num++) {
192 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
193 itos(tmpl_num), NULL);
195 if ((fp = fopen(filename, "wb")) == NULL) {
196 FILE_OP_ERROR(filename, "fopen");
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);
211 if (tmpl->value && *tmpl->value != '\0')
212 fwrite(tmpl->value, sizeof(gchar) * strlen(tmpl->value), 1, fp);
214 fwrite("", sizeof(gchar), 1, fp);
221 GSList *template_get_config(void)
224 template_list = template_read_config();
226 return template_list;
229 void template_set_config(GSList *tmpl_list)
231 template_clear_config(template_list);
232 template_write_config(tmpl_list);
233 template_list = tmpl_list;