sync with sylpheed 0.6.5cvs14
[claws.git] / src / template.c
blob80721819b0ec96a6004af78704a2aa5cac77cc5e
1 /*
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.
21 #include "defs.h"
23 #include <glib.h>
24 #include <stdio.h>
25 #include <dirent.h>
26 #include <sys/stat.h>
27 #include <ctype.h>
29 #include "intl.h"
30 #include "main.h"
31 #include "template.h"
32 #include "utils.h"
34 static GSList *template_list;
36 static Template *template_load(gchar *filename)
38 Template *tmpl;
39 FILE *fp;
40 gchar buf[BUFFSIZE];
41 gint bytes_read;
43 debug_print(_("%s:%d loading template from %s\n"), __FILE__, __LINE__, filename);
45 if ((fp = fopen(filename, "r")) == NULL) {
46 FILE_OP_ERROR(filename, "fopen");
47 return NULL;
50 tmpl = g_new(Template, 1);
51 tmpl->name = NULL;
52 tmpl->subject = NULL;
53 tmpl->value = NULL;
55 while (fgets(buf, sizeof(buf), fp) != NULL) {
56 if (buf[0] == '\n')
57 break;
58 else if (!g_strncasecmp(buf, "Name:", 5))
59 tmpl->name = g_strdup(g_strstrip(buf + 5));
60 else if (!g_strncasecmp(buf, "Subject:", 8))
61 tmpl->subject = g_strdup(g_strstrip(buf + 8));
64 if (!tmpl->name) {
65 g_warning("wrong template format\n");
66 template_free(tmpl);
67 return NULL;
70 if ((bytes_read = fread(buf, 1, sizeof(buf), fp)) == 0) {
71 if (ferror(fp)) {
72 FILE_OP_ERROR(filename, "fread");
73 template_free(tmpl);
74 return NULL;
77 fclose(fp);
78 tmpl->value = g_strndup(buf, bytes_read);
80 return tmpl;
83 void template_free(Template *tmpl)
85 g_free(tmpl->name);
86 g_free(tmpl->subject);
87 g_free(tmpl->value);
88 g_free(tmpl);
91 void template_clear_config(GSList *tmpl_list)
93 GSList *cur;
94 Template *tmpl;
96 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
97 tmpl = (Template *)cur->data;
98 template_free(tmpl);
100 g_slist_free(tmpl_list);
103 GSList *template_read_config(void)
105 gchar *path;
106 gchar *filename;
107 DIR *dp;
108 struct dirent *de;
109 struct stat s;
110 Template *tmpl;
111 GSList *tmpl_list = NULL;
113 path = get_template_dir();
114 debug_print(_("%s:%d reading templates dir %s\n"), __FILE__, __LINE__, path);
116 if (!is_dir_exist(path)) {
117 if (mkdir(path, S_IRWXU) < 0) {
118 FILE_OP_ERROR(path, "mkdir");
119 return NULL;
123 if ((dp = opendir(path)) == NULL) {
124 FILE_OP_ERROR(path, "opendir");
125 return NULL;
128 while ((de = readdir(dp)) != NULL) {
129 if (*de->d_name == '.') continue;
131 filename = g_strconcat(path, G_DIR_SEPARATOR_S, de->d_name, NULL);
132 debug_print(_("%s:%d found file %s\n"), __FILE__, __LINE__, filename);
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;
140 tmpl = template_load(filename);
141 if (tmpl)
142 tmpl_list = g_slist_append(tmpl_list, tmpl);
143 g_free(filename);
146 closedir(dp);
148 return tmpl_list;
151 void template_write_config(GSList *tmpl_list)
153 gchar *path;
154 GSList *cur;
155 Template *tmpl;
156 FILE *fp;
157 gint tmpl_num;
159 path = get_template_dir();
161 if (!is_dir_exist(path)) {
162 if (is_file_exist(path)) {
163 g_warning(_("file %s allready exists\n"), path);
164 return;
166 if (mkdir(path, S_IRWXU) < 0) {
167 FILE_OP_ERROR(path, "mkdir");
168 return;
172 remove_all_files(path);
174 for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
175 cur = cur->next, tmpl_num++) {
176 gchar *filename;
178 tmpl = cur->data;
180 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
181 itos(tmpl_num), NULL);
183 if ((fp = fopen(filename, "w")) == NULL) {
184 FILE_OP_ERROR(filename, "fopen");
185 g_free(filename);
186 g_free(path);
187 return;
190 debug_print(_("%s:%d writing template \"%s\" to %s\n"),
191 __FILE__, __LINE__, tmpl->name, filename);
192 fprintf(fp, "Name: %s\n", tmpl->name);
193 if (tmpl->subject)
194 fprintf(fp, "Subject: %s\n", tmpl->subject);
195 fputs("\n", fp);
196 fwrite(tmpl->value, sizeof(gchar) * strlen(tmpl->value), 1,
197 fp);
198 fclose(fp);
202 GSList *template_get_config(void)
204 if (!template_list)
205 template_list = template_read_config();
207 return template_list;
210 void template_set_config(GSList *tmpl_list)
212 template_clear_config(template_list);
213 template_write_config(tmpl_list);
214 template_list = tmpl_list;