2 * Claws Mail templates subsystem
3 * Copyright (C) 2001 Alexander Barinov
4 * Copyright (C) 2001-2018 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>
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);;
58 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, "From:", 5))
64 tmpl->from = g_strdup(g_strstrip(buf + 5));
65 else if (!g_ascii_strncasecmp(buf, "To:", 3))
66 tmpl->to = g_strdup(g_strstrip(buf + 3));
67 else if (!g_ascii_strncasecmp(buf, "Cc:", 3))
68 tmpl->cc = g_strdup(g_strstrip(buf + 3));
69 else if (!g_ascii_strncasecmp(buf, "Bcc:", 4))
70 tmpl->bcc = g_strdup(g_strstrip(buf + 4));
71 else if (!g_ascii_strncasecmp(buf, "Reply-To:", 9))
72 tmpl->replyto = g_strdup(g_strstrip(buf + 9));
73 else if (!g_ascii_strncasecmp(buf, "Subject:", 8))
74 tmpl->subject = g_strdup(g_strstrip(buf + 8));
78 g_warning("wrong template format");
84 if ((bytes_read = fread(buf, 1, sizeof(buf), fp)) == 0) {
86 FILE_OP_ERROR(filename, "fread");
93 tmpl->value = g_strndup(buf, bytes_read);
98 void template_free(Template *tmpl)
100 g_free(tmpl->load_filename);
102 g_free(tmpl->subject);
107 g_free(tmpl->replyto);
112 static void template_clear_config(GSList *tmpl_list)
117 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
118 tmpl = (Template *)cur->data;
121 g_slist_free(tmpl_list);
124 static gint tmpl_compare(gconstpointer tmpl1, gconstpointer tmpl2)
126 gchar *basename1, *basename2;
127 long filenum1, filenum2;
130 if ((Template *)tmpl1 == NULL || (Template *)tmpl2 == NULL)
133 if (((Template *)tmpl1)->load_filename == NULL || ((Template *)tmpl2)->load_filename == NULL)
136 basename1 = g_path_get_basename(((Template *)tmpl1)->load_filename);
137 basename2 = g_path_get_basename(((Template *)tmpl2)->load_filename);
138 filenum1 = atol(basename1);
139 filenum2 = atol(basename2);
143 if (filenum1 == 0 || filenum2 == 0)
146 if (filenum1 < filenum2)
149 if (filenum1 > filenum2)
155 GSList *template_read_config(void)
160 const gchar *dir_name;
163 GSList *tmpl_list = NULL;
165 path = get_template_dir();
166 debug_print("%s:%d reading templates dir %s\n",
167 __FILE__, __LINE__, path);
169 if (!is_dir_exist(path)) {
170 if (make_dir(path) < 0)
174 if ((dir = g_dir_open(path, 0, NULL)) == NULL) {
175 g_warning("failed to open directory: '%s'", path);
179 while ((dir_name = g_dir_read_name(dir)) != NULL) {
180 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
183 if (g_stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
184 debug_print("%s:%d %s is not an ordinary file\n",
185 __FILE__, __LINE__, filename);
189 tmpl = template_load(filename);
191 tmpl_list = g_slist_insert_sorted(tmpl_list, tmpl, tmpl_compare);
201 #define TRY(func) { \
204 g_warning("Failed to write template to file"); \
205 if (fp) fclose(fp); \
206 if (new) claws_unlink(new); \
213 #define TRY_NO_CLOSE(func) { \
216 g_warning("Failed to write template to file"); \
217 if (new) claws_unlink(new); \
224 static void template_write_config(GSList *tmpl_list)
232 debug_print("%s:%d writing templates\n", __FILE__, __LINE__);
234 path = get_template_dir();
236 if (!is_dir_exist(path)) {
237 if (is_file_exist(path)) {
238 g_warning("file '%s' already exists", path);
241 if (make_dir(path) < 0)
245 for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
246 cur = cur->next, tmpl_num++) {
247 gchar *filename, *new = NULL;
251 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
252 itos(tmpl_num), NULL);
254 if (is_file_exist(filename)) {
255 new = g_strconcat(filename, ".new", NULL);
258 if ((fp = g_fopen(new?new:filename, "wb")) == NULL) {
259 FILE_OP_ERROR(new?new:filename, "fopen");
265 TRY(fprintf(fp, "Name: %s\n", tmpl->name) > 0);
266 if (tmpl->subject && *tmpl->subject != '\0')
267 TRY(fprintf(fp, "Subject: %s\n", tmpl->subject) > 0);
268 if (tmpl->from && *tmpl->from != '\0')
269 TRY(fprintf(fp, "From: %s\n", tmpl->from) > 0);
270 if (tmpl->to && *tmpl->to != '\0')
271 TRY(fprintf(fp, "To: %s\n", tmpl->to) > 0);
272 if (tmpl->cc && *tmpl->cc != '\0')
273 TRY(fprintf(fp, "Cc: %s\n", tmpl->cc) > 0);
274 if (tmpl->bcc && *tmpl->bcc != '\0')
275 TRY(fprintf(fp, "Bcc: %s\n", tmpl->bcc) > 0);
276 if (tmpl->replyto && *tmpl->replyto != '\0')
277 TRY(fprintf(fp, "Reply-To: %s\n", tmpl->replyto) > 0);
279 TRY(fputs("\n", fp) != EOF);
281 if (tmpl->value && *tmpl->value != '\0') {
282 TRY(fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp) == strlen(tmpl->value));
284 TRY(fwrite("", sizeof(gchar), 1, fp) == 1);
286 TRY_NO_CLOSE(fclose(fp) != EOF);
289 if (rename_force(new, filename) < 0) {
290 FILE_OP_ERROR(new, "rename");
297 /* remove other templates */
299 gchar *filename = g_strconcat(path, G_DIR_SEPARATOR_S,
300 itos(tmpl_num), NULL);
301 if (is_file_exist(filename)) {
302 debug_print("removing old template %d\n", tmpl_num);
303 claws_unlink(filename);
313 GSList *template_get_config(void)
316 template_list = template_read_config();
318 return template_list;
321 void template_set_config(GSList *tmpl_list)
323 template_clear_config(template_list);
324 template_write_config(tmpl_list);
325 template_list = tmpl_list;