initial message templates support
[claws.git] / src / template.c
blob26b01bdd15fbe66047e3fcbb75d5aebeeb4a3120
1 /*
2 * Sylpheed templates subsystem
3 * Copyright (C) 2001 Alexander Barinov
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 #include "defs.h"
22 #include <gdk/gdk.h>
23 #include <stdio.h>
24 #include <dirent.h>
25 #include <sys/stat.h>
27 #include "utils.h"
28 #include "main.h"
29 #include "template.h"
30 #include "intl.h"
32 static GSList* template_load(GSList *tmpl_list, gchar *filename)
34 Template *tmpl;
35 FILE *fp;
36 char buf[32000];
37 gint bytes_read;
39 LOG_MESSAGE(_("%s:%d loading template from %s\n"), __FILE__, __LINE__, filename);
41 if ((fp = fopen(filename, "r")) == NULL) {
42 FILE_OP_ERROR(filename, "fopen");
43 return tmpl_list;
46 tmpl = g_new(Template, 1);
48 if (fgets(buf, sizeof(buf), fp) == NULL) {
49 FILE_OP_ERROR(filename, "fgets");
50 g_free(tmpl);
51 LOG_MESSAGE(_("%s:%d exiting\n"), __FILE__, __LINE__);
52 return tmpl_list;
54 tmpl->name = g_strdup(g_strstrip(buf));
56 memset(buf, 0, sizeof(buf));
57 if ((bytes_read = fread(buf, 1, sizeof(buf)-1, fp)) == 0) {
58 FILE_OP_ERROR(filename, "fread");
59 g_free(tmpl->name);
60 g_free(tmpl);
61 return tmpl_list;
63 tmpl->value = g_strdup(buf);
65 tmpl_list = g_slist_append(tmpl_list, tmpl);
66 fclose(fp);
67 return tmpl_list;
70 void template_free(Template *tmpl)
72 g_free(tmpl->name);
73 g_free(tmpl->value);
74 g_free(tmpl);
77 void template_clear_config(GSList *tmpl_list)
79 Template *tmpl;
81 while (tmpl_list != NULL) {
82 tmpl = tmpl_list->data;
83 template_free(tmpl);
84 tmpl_list = g_slist_remove(tmpl_list, tmpl);
88 GSList* template_read_config(void)
90 gchar *path;
91 gchar *filename;
92 DIR *dp;
93 struct dirent *de;
94 struct stat s;
95 GSList *tmpl_list = NULL;
97 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, TEMPLATES_DIR, NULL);
98 LOG_MESSAGE(_("%s:%d reading templates dir %s\n"), __FILE__, __LINE__, path);
100 if ((dp = opendir(path)) == NULL) {
101 FILE_OP_ERROR(path, "opendir");
102 return tmpl_list;
105 while ((de = readdir(dp)) != NULL) {
106 filename = g_strconcat(path, G_DIR_SEPARATOR_S, de->d_name, NULL);
107 LOG_MESSAGE(_("%s:%d found file %s\n"), __FILE__, __LINE__, filename);
109 if (stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
110 LOG_MESSAGE(_("%s:%d %s is not an ordinary file\n"),
111 __FILE__, __LINE__, filename);
112 continue;
115 tmpl_list = template_load(tmpl_list, filename);
116 g_free(filename);
119 closedir(dp);
120 g_free(path);
121 return tmpl_list;
124 void template_write_config(GSList *tmpl_list)
126 gchar *path;
127 gchar *filename;
128 GSList *cur;
129 Template *tmpl;
130 FILE *fp;
131 gint tmpl_num = 1;
133 path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, TEMPLATES_DIR, NULL);
135 if (!is_dir_exist(path)) {
136 if (is_file_exist(path)) {
137 LOG_MESSAGE(_("%s:%d file %s allready exists\n"),
138 __FILE__, __LINE__, filename);
139 g_free(path);
140 return;
142 if (mkdir(path, S_IRWXU) < 0) {
143 FILE_OP_ERROR(path, "mkdir");
144 g_free(path);
145 return;
149 remove_all_files(path);
151 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
152 tmpl = cur->data;
154 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(tmpl_num), NULL);
156 if ((fp = fopen(filename, "w")) == NULL) {
157 FILE_OP_ERROR(filename, "fopen");
158 g_free(filename);
159 g_free(path);
160 return;
163 LOG_MESSAGE(_("%s:%d writing template \"%s\" to %s\n"),
164 __FILE__, __LINE__, tmpl->name, filename);
165 fputs(tmpl->name, fp);
166 fputs("\n", fp);
167 fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp);
168 fclose(fp);
170 tmpl_num ++;
173 g_free(path);