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>
34 static GSList *template_list;
36 static Template *template_load(gchar *filename)
43 if ((fp = g_fopen(filename, "rb")) == NULL) {
44 FILE_OP_ERROR(filename, "fopen");
48 tmpl = g_new(Template, 1);
49 tmpl->load_filename = g_strdup(filename);;
59 while (fgets(buf, sizeof(buf), fp) != NULL) {
62 else if (!g_ascii_strncasecmp(buf, "Name:", 5))
63 tmpl->name = g_strdup(g_strstrip(buf + 5));
64 else if (!g_ascii_strncasecmp(buf, "From:", 5))
65 tmpl->from = g_strdup(g_strstrip(buf + 5));
66 else if (!g_ascii_strncasecmp(buf, "To:", 3))
67 tmpl->to = g_strdup(g_strstrip(buf + 3));
68 else if (!g_ascii_strncasecmp(buf, "Cc:", 3))
69 tmpl->cc = g_strdup(g_strstrip(buf + 3));
70 else if (!g_ascii_strncasecmp(buf, "Bcc:", 4))
71 tmpl->bcc = g_strdup(g_strstrip(buf + 4));
72 else if (!g_ascii_strncasecmp(buf, "Reply-To:", 9))
73 tmpl->replyto = g_strdup(g_strstrip(buf + 9));
74 else if (!g_ascii_strncasecmp(buf, "Subject:", 8))
75 tmpl->subject = g_strdup(g_strstrip(buf + 8));
79 g_warning("wrong template format");
85 if ((bytes_read = fread(buf, 1, sizeof(buf), fp)) == 0) {
87 FILE_OP_ERROR(filename, "fread");
94 tmpl->value = g_strndup(buf, bytes_read);
99 void template_free(Template *tmpl)
101 g_free(tmpl->load_filename);
103 g_free(tmpl->subject);
108 g_free(tmpl->replyto);
113 static void template_clear_config(GSList *tmpl_list)
118 for (cur = tmpl_list; cur != NULL; cur = cur->next) {
119 tmpl = (Template *)cur->data;
122 g_slist_free(tmpl_list);
125 static gint tmpl_compare(gconstpointer tmpl1, gconstpointer tmpl2)
127 gchar *basename1, *basename2;
128 long filenum1, filenum2;
131 if ((Template *)tmpl1 == NULL || (Template *)tmpl2 == NULL)
134 if (((Template *)tmpl1)->load_filename == NULL || ((Template *)tmpl2)->load_filename == NULL)
137 basename1 = g_path_get_basename(((Template *)tmpl1)->load_filename);
138 basename2 = g_path_get_basename(((Template *)tmpl2)->load_filename);
139 filenum1 = atol(basename1);
140 filenum2 = atol(basename2);
144 if (filenum1 == 0 || filenum2 == 0)
147 if (filenum1 < filenum2)
150 if (filenum1 > filenum2)
156 GSList *template_read_config(void)
161 const gchar *dir_name;
164 GSList *tmpl_list = NULL;
166 path = get_template_dir();
167 debug_print("%s:%d reading templates dir %s\n",
168 __FILE__, __LINE__, path);
170 if (!is_dir_exist(path)) {
171 if (make_dir(path) < 0)
175 if ((dir = g_dir_open(path, 0, NULL)) == NULL) {
176 g_warning("failed to open directory: '%s'", path);
180 while ((dir_name = g_dir_read_name(dir)) != NULL) {
181 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
184 if (g_stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
185 debug_print("%s:%d %s is not an ordinary file\n",
186 __FILE__, __LINE__, filename);
190 tmpl = template_load(filename);
192 tmpl_list = g_slist_insert_sorted(tmpl_list, tmpl, tmpl_compare);
202 #define TRY(func) { \
205 g_warning("Failed to write template to file"); \
206 if (fp) fclose(fp); \
207 if (new) claws_unlink(new); \
214 #define TRY_NO_CLOSE(func) { \
217 g_warning("Failed to write template to file"); \
218 if (new) claws_unlink(new); \
225 static void template_write_config(GSList *tmpl_list)
233 debug_print("%s:%d writing templates\n", __FILE__, __LINE__);
235 path = get_template_dir();
237 if (!is_dir_exist(path)) {
238 if (is_file_exist(path)) {
239 g_warning("file '%s' already exists", path);
242 if (make_dir(path) < 0)
246 for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
247 cur = cur->next, tmpl_num++) {
248 gchar *filename, *new = NULL;
252 filename = g_strconcat(path, G_DIR_SEPARATOR_S,
253 itos(tmpl_num), NULL);
255 if (is_file_exist(filename)) {
256 new = g_strconcat(filename, ".new", NULL);
259 if ((fp = g_fopen(new?new:filename, "wb")) == NULL) {
260 FILE_OP_ERROR(new?new:filename, "fopen");
266 TRY(fprintf(fp, "Name: %s\n", tmpl->name) > 0);
267 if (tmpl->subject && *tmpl->subject != '\0')
268 TRY(fprintf(fp, "Subject: %s\n", tmpl->subject) > 0);
269 if (tmpl->from && *tmpl->from != '\0')
270 TRY(fprintf(fp, "From: %s\n", tmpl->from) > 0);
271 if (tmpl->to && *tmpl->to != '\0')
272 TRY(fprintf(fp, "To: %s\n", tmpl->to) > 0);
273 if (tmpl->cc && *tmpl->cc != '\0')
274 TRY(fprintf(fp, "Cc: %s\n", tmpl->cc) > 0);
275 if (tmpl->bcc && *tmpl->bcc != '\0')
276 TRY(fprintf(fp, "Bcc: %s\n", tmpl->bcc) > 0);
277 if (tmpl->replyto && *tmpl->replyto != '\0')
278 TRY(fprintf(fp, "Reply-To: %s\n", tmpl->replyto) > 0);
280 TRY(fputs("\n", fp) != EOF);
282 if (tmpl->value && *tmpl->value != '\0') {
283 TRY(fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp) == strlen(tmpl->value));
285 TRY(fwrite("", sizeof(gchar), 1, fp) == 1);
287 TRY_NO_CLOSE(safe_fclose(fp) != EOF);
290 if (rename_force(new, filename) < 0) {
291 FILE_OP_ERROR(new, "rename");
298 /* remove other templates */
300 gchar *filename = g_strconcat(path, G_DIR_SEPARATOR_S,
301 itos(tmpl_num), NULL);
302 if (is_file_exist(filename)) {
303 debug_print("removing old template %d\n", tmpl_num);
304 claws_unlink(filename);
314 GSList *template_get_config(void)
317 template_list = template_read_config();
319 return template_list;
322 void template_set_config(GSList *tmpl_list)
324 template_clear_config(template_list);
325 template_write_config(tmpl_list);
326 template_list = tmpl_list;