2 * Sylpheed templates subsystem
3 * Copyright (C) 2001 Alexander Barinov
4 * Copyright (C) 2001-2007 Hiroyuki Yamamoto and the Claws Mail team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <glib/gi18n.h>
31 #include "../codeconv.h"
33 static GSList *template_list;
35 static Template *template_load(gchar *filename)
42 if ((fp = g_fopen(filename, "rb")) == NULL) {
43 FILE_OP_ERROR(filename, "fopen");
47 tmpl = g_new(Template, 1);
48 tmpl->load_filename = g_strdup(filename);;
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)
92 g_free(tmpl->load_filename);
94 g_free(tmpl->subject);
102 void template_clear_config(GSList *tmpl_list)
107 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
108 tmpl = (Template *)cur->data;
111 g_slist_free(tmpl_list);
114 static gint tmpl_compare(gconstpointer tmpl1, gconstpointer tmpl2)
116 gchar *basename1, *basename2;
117 long filenum1, filenum2;
120 if ((Template *)tmpl1 == NULL || (Template *)tmpl2 == NULL)
123 if (((Template *)tmpl1)->load_filename == NULL || ((Template *)tmpl2)->load_filename == NULL)
126 basename1 = g_path_get_basename(((Template *)tmpl1)->load_filename);
127 basename2 = g_path_get_basename(((Template *)tmpl2)->load_filename);
128 filenum1 = atol(basename1);
129 filenum2 = atol(basename2);
133 if (filenum1 == 0 || filenum2 == 0)
136 if (filenum1 < filenum2)
139 if (filenum1 > filenum2)
145 GSList *template_read_config(void)
150 const gchar *dir_name;
153 GSList *tmpl_list = NULL;
155 path = get_template_dir();
156 debug_print("%s:%d reading templates dir %s\n",
157 __FILE__, __LINE__, path);
159 if (!is_dir_exist(path)) {
160 if (make_dir(path) < 0)
164 if ((dir = g_dir_open(path, 0, NULL)) == NULL) {
165 g_warning("failed to open directory: %s\n", path);
169 while ((dir_name = g_dir_read_name(dir)) != NULL) {
170 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
173 if (g_stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
174 debug_print("%s:%d %s is not an ordinary file\n",
175 __FILE__, __LINE__, filename);
179 tmpl = template_load(filename);
181 tmpl_list = g_slist_insert_sorted(tmpl_list, tmpl, tmpl_compare);
191 void template_write_config(GSList *tmpl_list)
199 debug_print("%s:%d writing templates\n", __FILE__, __LINE__);
201 path = get_template_dir();
203 if (!is_dir_exist(path)) {
204 if (is_file_exist(path)) {
205 g_warning("file %s already exists\n", path);
208 if (make_dir(path) < 0)
212 remove_all_files(path);
214 for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
215 cur = cur->next, tmpl_num++) {
220 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
221 itos(tmpl_num), NULL);
223 if ((fp = g_fopen(filename, "wb")) == NULL) {
224 FILE_OP_ERROR(filename, "fopen");
229 fprintf(fp, "Name: %s\n", tmpl->name);
230 if (tmpl->subject && *tmpl->subject != '\0')
231 fprintf(fp, "Subject: %s\n", tmpl->subject);
232 if (tmpl->to && *tmpl->to != '\0')
233 fprintf(fp, "To: %s\n", tmpl->to);
234 if (tmpl->cc && *tmpl->cc != '\0')
235 fprintf(fp, "Cc: %s\n", tmpl->cc);
236 if (tmpl->bcc && *tmpl->bcc != '\0')
237 fprintf(fp, "Bcc: %s\n", tmpl->bcc);
239 if (tmpl->value && *tmpl->value != '\0')
240 fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp);
242 fwrite("", sizeof(gchar), 1, fp);
249 GSList *template_get_config(void)
252 template_list = template_read_config();
254 return template_list;
257 void template_set_config(GSList *tmpl_list)
259 template_clear_config(template_list);
260 template_write_config(tmpl_list);
261 template_list = tmpl_list;