Statistics
| Revision:

root / src / template.c @ 3300

History | View | Annotate | Download (5.5 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, guint tmplid)
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->tmplid = tmplid;
48
        tmpl->name = NULL;
49
        tmpl->to = NULL;
50
        tmpl->cc = NULL;
51
        tmpl->bcc = NULL;
52
        tmpl->replyto = NULL;
53
        tmpl->subject = NULL;
54
        tmpl->value = NULL;
55

    
56
        while (fgets(buf, sizeof(buf), fp) != NULL) {
57
                if (buf[0] == '\n')
58
                        break;
59
                else if (!g_ascii_strncasecmp(buf, "Name:", 5))
60
                        tmpl->name = g_strdup(g_strstrip(buf + 5));
61
                else if (!g_ascii_strncasecmp(buf, "To:", 3))
62
                        tmpl->to = g_strdup(g_strstrip(buf + 3));
63
                else if (!g_ascii_strncasecmp(buf, "Cc:", 3))
64
                        tmpl->cc = g_strdup(g_strstrip(buf + 3));
65
                else if (!g_ascii_strncasecmp(buf, "Bcc:", 3))
66
                        tmpl->bcc = g_strdup(g_strstrip(buf + 4));
67
                else if (!g_ascii_strncasecmp(buf, "Reply-To:", 9))
68
                        tmpl->replyto = g_strdup(g_strstrip(buf + 9));
69
                else if (!g_ascii_strncasecmp(buf, "Subject:", 8))
70
                        tmpl->subject = g_strdup(g_strstrip(buf + 8));
71
        }
72

    
73
        if (!tmpl->name) {
74
                g_warning("wrong template format\n");
75
                template_free(tmpl);
76
                fclose(fp);
77
                return NULL;
78
        }
79

    
80
        tmpl->value = file_read_stream_to_str(fp);
81
        if (!tmpl->value) {
82
                g_warning("cannot read template body\n");
83
                template_free(tmpl);
84
                fclose(fp);
85
                return NULL;
86
        }
87
        fclose(fp);
88

    
89
        return tmpl;
90
}
91

    
92
void template_free(Template *tmpl)
93
{
94
        g_free(tmpl->name);
95
        g_free(tmpl->to);
96
        g_free(tmpl->cc);
97
        g_free(tmpl->subject);
98
        g_free(tmpl->value);
99
        g_free(tmpl);
100
}
101

    
102
void template_clear_config(GSList *tmpl_list)
103
{
104
        GSList *cur;
105
        Template *tmpl;
106

    
107
        for (cur = tmpl_list; cur != NULL; cur = cur->next) {
108
                tmpl = (Template *)cur->data;
109
                template_free(tmpl);
110
        }
111
        g_slist_free(tmpl_list);
112
}
113

    
114
static gint template_compare_id(gconstpointer a, gconstpointer b)
115
{
116
        const Template *ta, *tb;
117

    
118
        ta = a;
119
        tb = b;
120
        return (ta->tmplid - tb->tmplid);
121
}
122

    
123
GSList *template_read_config(void)
124
{
125
        const gchar *path;
126
        gchar *filename;
127
        GDir *dir;
128
        const gchar *dir_name;
129
        struct stat s;
130
        Template *tmpl;
131
        guint tmplid;
132
        GSList *tmpl_list = NULL;
133

    
134
        path = get_template_dir();
135
        debug_print("%s:%d reading templates dir %s\n",
136
                    __FILE__, __LINE__, path);
137

    
138
        if (!is_dir_exist(path)) {
139
                if (make_dir(path) < 0)
140
                        return NULL;
141
        }
142

    
143
        if ((dir = g_dir_open(path, 0, NULL)) == NULL) {
144
                g_warning("failed to open directory: %s\n", path);
145
                return NULL;
146
        }
147

    
148
        while ((dir_name = g_dir_read_name(dir)) != NULL) {
149
                tmplid = atoi(dir_name);
150
                if (tmplid <= 0) {
151
                        continue;
152
                }
153

    
154
                filename = g_strconcat(path, G_DIR_SEPARATOR_S,
155
                                       dir_name, NULL);
156

    
157
                if (g_stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
158
                        debug_print("%s:%d %s is not an ordinary file\n",
159
                                    __FILE__, __LINE__, filename);
160
                        g_free(filename);
161
                        continue;
162
                }
163

    
164
                tmpl = template_load(filename, tmplid);
165
                if (tmpl)
166
                        tmpl_list = g_slist_insert_sorted(tmpl_list, tmpl,
167
                                                          template_compare_id);
168

    
169
                g_free(filename);
170
        }
171

    
172
        g_dir_close(dir);
173

    
174
        return tmpl_list;
175
}
176

    
177
void template_write_config(GSList *tmpl_list)
178
{
179
        const gchar *path;
180
        GSList *cur;
181
        Template *tmpl;
182
        FILE *fp;
183

    
184
        debug_print("%s:%d writing templates\n", __FILE__, __LINE__);
185

    
186
        path = get_template_dir();
187

    
188
        if (!is_dir_exist(path)) {
189
                if (is_file_exist(path)) {
190
                        g_warning(_("file %s already exists\n"), path);
191
                        return;
192
                }
193
                if (make_dir(path) < 0)
194
                        return;
195
        }
196

    
197
        remove_all_files(path);
198

    
199
        for (cur = tmpl_list; cur != NULL; cur = cur->next) {
200
                gchar *filename;
201

    
202
                tmpl = cur->data;
203

    
204
                filename = g_strconcat(path, G_DIR_SEPARATOR_S,
205
                                       itos(tmpl->tmplid), NULL);
206

    
207
                if ((fp = g_fopen(filename, "wb")) == NULL) {
208
                        FILE_OP_ERROR(filename, "fopen");
209
                        g_free(filename);
210
                        return;
211
                }
212

    
213
                fprintf(fp, "Name: %s\n", tmpl->name);
214
                if (tmpl->to && *tmpl->to != '\0')
215
                        fprintf(fp, "To: %s\n", tmpl->to);
216
                if (tmpl->cc && *tmpl->cc != '\0')
217
                        fprintf(fp, "Cc: %s\n", tmpl->cc);
218
                if (tmpl->bcc && *tmpl->bcc != '\0')
219
                        fprintf(fp, "Bcc: %s\n", tmpl->bcc);
220
                if (tmpl->replyto && *tmpl->replyto != '\0')
221
                        fprintf(fp, "Reply-To: %s\n", tmpl->replyto);
222
                if (tmpl->subject && *tmpl->subject != '\0')
223
                        fprintf(fp, "Subject: %s\n", tmpl->subject);
224
                fputs("\n", fp);
225
                fwrite(tmpl->value, sizeof(gchar) * strlen(tmpl->value), 1, fp);
226

    
227
                fclose(fp);
228
                g_free(filename);
229
        }
230
}
231

    
232
GSList *template_get_config(void)
233
{
234
        if (!template_list)
235
                template_list = template_read_config();
236

    
237
        return template_list;
238
}
239

    
240
void template_set_config(GSList *tmpl_list)
241
{
242
        template_clear_config(template_list);
243
        template_write_config(tmpl_list);
244
        template_list = tmpl_list;
245
}