Statistics
| Revision:

root / src / template.c @ 3270

History | View | Annotate | Download (5.47 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
                return NULL;
77
        }
78

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

    
87
        return tmpl;
88
}
89

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

    
100
void template_clear_config(GSList *tmpl_list)
101
{
102
        GSList *cur;
103
        Template *tmpl;
104

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

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

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

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

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

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

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

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

    
152
                filename = g_strconcat(path, G_DIR_SEPARATOR_S,
153
                                       dir_name, NULL);
154

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

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

    
167
                g_free(filename);
168
        }
169

    
170
        g_dir_close(dir);
171

    
172
        return tmpl_list;
173
}
174

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

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

    
184
        path = get_template_dir();
185

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

    
195
        remove_all_files(path);
196

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

    
200
                tmpl = cur->data;
201

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

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

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

    
225
                fclose(fp);
226
                g_free(filename);
227
        }
228
}
229

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

    
235
        return template_list;
236
}
237

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