Statistics
| Revision:

root / src / template.c @ 2380

History | View | Annotate | Download (5.2 kB)

1
/*
2
 * Sylpheed templates subsystem 
3
 * Copyright (C) 2001 Alexander Barinov
4
 * Copyright (C) 2001-2006 Hiroyuki Yamamoto
5
 *
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.
10
 *
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.
15
 *
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.
19
 */
20
21
#include "defs.h"
22
23
#include <glib.h>
24
#include <glib/gi18n.h>
25
#include <stdio.h>
26
#include <sys/stat.h>
27
#include <ctype.h>
28
29
#include "main.h"
30
#include "template.h"
31
#include "utils.h"
32
33
static GSList *template_list;
34
35
static Template *template_load(gchar *filename)
36
{
37
        Template *tmpl;
38
        FILE *fp;
39
        gchar buf[BUFFSIZE];
40
41
        if ((fp = g_fopen(filename, "rb")) == NULL) {
42
                FILE_OP_ERROR(filename, "fopen");
43
                return NULL;
44
        }
45
46
        tmpl = g_new(Template, 1);
47
        tmpl->name = NULL;
48
        tmpl->to = NULL;
49
        tmpl->cc = NULL;
50
        tmpl->bcc = NULL;
51
        tmpl->replyto = NULL;
52
        tmpl->subject = NULL;
53
        tmpl->value = NULL;
54
55
        while (fgets(buf, sizeof(buf), fp) != NULL) {
56
                if (buf[0] == '\n')
57
                        break;
58
                else if (!g_ascii_strncasecmp(buf, "Name:", 5))
59
                        tmpl->name = g_strdup(g_strstrip(buf + 5));
60
                else if (!g_ascii_strncasecmp(buf, "To:", 3))
61
                        tmpl->to = g_strdup(g_strstrip(buf + 3));
62
                else if (!g_ascii_strncasecmp(buf, "Cc:", 3))
63
                        tmpl->cc = g_strdup(g_strstrip(buf + 3));
64
                else if (!g_ascii_strncasecmp(buf, "Bcc:", 3))
65
                        tmpl->bcc = g_strdup(g_strstrip(buf + 4));
66
                else if (!g_ascii_strncasecmp(buf, "Reply-To:", 9))
67
                        tmpl->replyto = g_strdup(g_strstrip(buf + 9));
68
                else if (!g_ascii_strncasecmp(buf, "Subject:", 8))
69
                        tmpl->subject = g_strdup(g_strstrip(buf + 8));
70
        }
71
72
        if (!tmpl->name) {
73
                g_warning("wrong template format\n");
74
                template_free(tmpl);
75
                return NULL;
76
        }
77
78
        tmpl->value = file_read_stream_to_str(fp);
79
        if (!tmpl->value) {
80
                g_warning("cannot read template body\n");
81
                template_free(tmpl);
82
                return NULL;
83
        }
84
        fclose(fp);
85
86
        return tmpl;
87
}
88
89
void template_free(Template *tmpl)
90
{
91
        g_free(tmpl->name);
92
        g_free(tmpl->to);
93
        g_free(tmpl->cc);
94
        g_free(tmpl->subject);
95
        g_free(tmpl->value);
96
        g_free(tmpl);
97
}
98
99
void template_clear_config(GSList *tmpl_list)
100
{
101
        GSList *cur;
102
        Template *tmpl;
103
104
        for (cur = tmpl_list; cur != NULL; cur = cur->next) {
105
                tmpl = (Template *)cur->data;
106
                template_free(tmpl);
107
        }
108
        g_slist_free(tmpl_list);
109
}
110
111
GSList *template_read_config(void)
112
{
113
        const gchar *path;
114
        gchar *filename;
115
        GDir *dir;
116
        const gchar *dir_name;
117
        struct stat s;
118
        Template *tmpl;
119
        GSList *tmpl_list = NULL;
120
121
        path = get_template_dir();
122
        debug_print("%s:%d reading templates dir %s\n",
123
                    __FILE__, __LINE__, path);
124
125
        if (!is_dir_exist(path)) {
126
                if (make_dir(path) < 0)
127
                        return NULL;
128
        }
129
130
        if ((dir = g_dir_open(path, 0, NULL)) == NULL) {
131
                g_warning("failed to open directory: %s\n", path);
132
                return NULL;
133
        }
134
135
        while ((dir_name = g_dir_read_name(dir)) != NULL) {
136
                filename = g_strconcat(path, G_DIR_SEPARATOR_S,
137
                                       dir_name, NULL);
138
139
                if (g_stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
140
                        debug_print("%s:%d %s is not an ordinary file\n",
141
                                    __FILE__, __LINE__, filename);
142
                        continue;
143
                }
144
145
                tmpl = template_load(filename);
146
                if (tmpl)
147
                        tmpl_list = g_slist_append(tmpl_list, tmpl);
148
149
                g_free(filename);
150
        }
151
152
        g_dir_close(dir);
153
154
        return tmpl_list;
155
}
156
157
void template_write_config(GSList *tmpl_list)
158
{
159
        const gchar *path;
160
        GSList *cur;
161
        Template *tmpl;
162
        FILE *fp;
163
        gint tmpl_num;
164
165
        debug_print("%s:%d writing templates\n", __FILE__, __LINE__);
166
167
        path = get_template_dir();
168
169
        if (!is_dir_exist(path)) {
170
                if (is_file_exist(path)) {
171
                        g_warning(_("file %s already exists\n"), path);
172
                        return;
173
                }
174
                if (make_dir(path) < 0)
175
                        return;
176
        }
177
178
        remove_all_files(path);
179
180
        for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
181
             cur = cur->next, tmpl_num++) {
182
                gchar *filename;
183
184
                tmpl = cur->data;
185
186
                filename = g_strconcat(path, G_DIR_SEPARATOR_S,
187
                                       itos(tmpl_num), NULL);
188
189
                if ((fp = g_fopen(filename, "wb")) == NULL) {
190
                        FILE_OP_ERROR(filename, "fopen");
191
                        g_free(filename);
192
                        return;
193
                }
194
195
                fprintf(fp, "Name: %s\n", tmpl->name);
196
                if (tmpl->to && *tmpl->to != '\0')
197
                        fprintf(fp, "To: %s\n", tmpl->to);
198
                if (tmpl->cc && *tmpl->cc != '\0')
199
                        fprintf(fp, "Cc: %s\n", tmpl->cc);
200
                if (tmpl->bcc && *tmpl->bcc != '\0')
201
                        fprintf(fp, "Bcc: %s\n", tmpl->bcc);
202
                if (tmpl->replyto && *tmpl->replyto != '\0')
203
                        fprintf(fp, "Reply-To: %s\n", tmpl->replyto);
204
                if (tmpl->subject && *tmpl->subject != '\0')
205
                        fprintf(fp, "Subject: %s\n", tmpl->subject);
206
                fputs("\n", fp);
207
                fwrite(tmpl->value, sizeof(gchar) * strlen(tmpl->value), 1, fp);
208
209
                fclose(fp);
210
                g_free(filename);
211
        }
212
}
213
214
GSList *template_get_config(void)
215
{
216
        if (!template_list)
217
                template_list = template_read_config();
218
219
        return template_list;
220
}
221
222
void template_set_config(GSList *tmpl_list)
223
{
224
        template_clear_config(template_list);
225
        template_write_config(tmpl_list);
226
        template_list = tmpl_list;
227
}