2 * Sylpheed templates subsystem
3 * Copyright (C) 2001 Alexander Barinov
4 * Copyright (C) 2001-2009 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 3 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, see <http://www.gnu.org/licenses/>.
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);;
57 while (fgets(buf, sizeof(buf), fp) != NULL) {
60 else if (!g_ascii_strncasecmp(buf, "Name:", 5))
61 tmpl->name = g_strdup(g_strstrip(buf + 5));
62 else if (!g_ascii_strncasecmp(buf, "From:", 5))
63 tmpl->from = g_strdup(g_strstrip(buf + 5));
64 else if (!g_ascii_strncasecmp(buf, "To:", 3))
65 tmpl->to = g_strdup(g_strstrip(buf + 3));
66 else if (!g_ascii_strncasecmp(buf, "Cc:", 3))
67 tmpl->cc = g_strdup(g_strstrip(buf + 3));
68 else if (!g_ascii_strncasecmp(buf, "Bcc:", 4))
69 tmpl->bcc = g_strdup(g_strstrip(buf + 4));
70 else if (!g_ascii_strncasecmp(buf, "Subject:", 8))
71 tmpl->subject = g_strdup(g_strstrip(buf + 8));
75 g_warning("wrong template format\n");
81 if ((bytes_read = fread(buf, 1, sizeof(buf), fp)) == 0) {
83 FILE_OP_ERROR(filename, "fread");
90 tmpl->value = g_strndup(buf, bytes_read);
95 void template_free(Template *tmpl)
97 g_free(tmpl->load_filename);
99 g_free(tmpl->subject);
108 static void template_clear_config(GSList *tmpl_list)
113 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
114 tmpl = (Template *)cur->data;
117 g_slist_free(tmpl_list);
120 static gint tmpl_compare(gconstpointer tmpl1, gconstpointer tmpl2)
122 gchar *basename1, *basename2;
123 long filenum1, filenum2;
126 if ((Template *)tmpl1 == NULL || (Template *)tmpl2 == NULL)
129 if (((Template *)tmpl1)->load_filename == NULL || ((Template *)tmpl2)->load_filename == NULL)
132 basename1 = g_path_get_basename(((Template *)tmpl1)->load_filename);
133 basename2 = g_path_get_basename(((Template *)tmpl2)->load_filename);
134 filenum1 = atol(basename1);
135 filenum2 = atol(basename2);
139 if (filenum1 == 0 || filenum2 == 0)
142 if (filenum1 < filenum2)
145 if (filenum1 > filenum2)
151 GSList *template_read_config(void)
156 const gchar *dir_name;
159 GSList *tmpl_list = NULL;
161 path = get_template_dir();
162 debug_print("%s:%d reading templates dir %s\n",
163 __FILE__, __LINE__, path);
165 if (!is_dir_exist(path)) {
166 if (make_dir(path) < 0)
170 if ((dir = g_dir_open(path, 0, NULL)) == NULL) {
171 g_warning("failed to open directory: %s\n", path);
175 while ((dir_name = g_dir_read_name(dir)) != NULL) {
176 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
179 if (g_stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
180 debug_print("%s:%d %s is not an ordinary file\n",
181 __FILE__, __LINE__, filename);
185 tmpl = template_load(filename);
187 tmpl_list = g_slist_insert_sorted(tmpl_list, tmpl, tmpl_compare);
197 #define TRY(func) { \
200 g_warning("Failed to write template to file\n"); \
201 if (fp) fclose(fp); \
202 if (new) claws_unlink(new); \
209 #define TRY_NO_CLOSE(func) { \
212 g_warning("Failed to write template to file\n"); \
213 if (new) claws_unlink(new); \
220 static void template_write_config(GSList *tmpl_list)
228 debug_print("%s:%d writing templates\n", __FILE__, __LINE__);
230 path = get_template_dir();
232 if (!is_dir_exist(path)) {
233 if (is_file_exist(path)) {
234 g_warning("file %s already exists\n", path);
237 if (make_dir(path) < 0)
241 for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
242 cur = cur->next, tmpl_num++) {
243 gchar *filename, *new = NULL;
247 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
248 itos(tmpl_num), NULL);
250 if (is_file_exist(filename)) {
251 new = g_strconcat(filename, ".new", NULL);
254 if ((fp = g_fopen(new?new:filename, "wb")) == NULL) {
255 FILE_OP_ERROR(new?new:filename, "fopen");
261 TRY(fprintf(fp, "Name: %s\n", tmpl->name) > 0);
262 if (tmpl->subject && *tmpl->subject != '\0')
263 TRY(fprintf(fp, "Subject: %s\n", tmpl->subject) > 0);
264 if (tmpl->from && *tmpl->from != '\0')
265 TRY(fprintf(fp, "From: %s\n", tmpl->from) > 0);
266 if (tmpl->to && *tmpl->to != '\0')
267 TRY(fprintf(fp, "To: %s\n", tmpl->to) > 0);
268 if (tmpl->cc && *tmpl->cc != '\0')
269 TRY(fprintf(fp, "Cc: %s\n", tmpl->cc) > 0);
270 if (tmpl->bcc && *tmpl->bcc != '\0')
271 TRY(fprintf(fp, "Bcc: %s\n", tmpl->bcc) > 0);
273 TRY(fputs("\n", fp) != EOF);
275 if (tmpl->value && *tmpl->value != '\0') {
276 TRY(fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp) == strlen(tmpl->value));
278 TRY(fwrite("", sizeof(gchar), 1, fp) == 1);
280 TRY_NO_CLOSE(fclose(fp) != EOF);
283 claws_unlink(filename);
284 rename_force(new, filename);
290 /* remove other templates */
292 gchar *filename = g_strconcat(path, G_DIR_SEPARATOR_S,
293 itos(tmpl_num), NULL);
294 if (is_file_exist(filename)) {
295 debug_print("removing old template %d\n", tmpl_num);
296 claws_unlink(filename);
306 GSList *template_get_config(void)
309 template_list = template_read_config();
311 return template_list;
314 void template_set_config(GSList *tmpl_list)
316 template_clear_config(template_list);
317 template_write_config(tmpl_list);
318 template_list = tmpl_list;