Statistics
| Revision:

root / src / template.c @ 478

History | View | Annotate | Download (4.9 kB)

1
/*
2
 * Sylpheed templates subsystem 
3
 * Copyright (C) 2001 Alexander Barinov
4
 * Copyright (C) 2001-2004 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 <dirent.h>
27
#include <sys/stat.h>
28
#include <ctype.h>
29
30
#include "main.h"
31
#include "template.h"
32
#include "utils.h"
33
34
static GSList *template_list;
35
36
static Template *template_load(gchar *filename)
37
{
38
        Template *tmpl;
39
        FILE *fp;
40
        gchar buf[BUFFSIZE];
41
        gint bytes_read;
42
43
        if ((fp = g_fopen(filename, "rb")) == NULL) {
44
                FILE_OP_ERROR(filename, "fopen");
45
                return NULL;
46
        }
47
48
        tmpl = g_new(Template, 1);
49
        tmpl->name = NULL;
50
        tmpl->to = NULL;
51
        tmpl->cc = 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, "Subject:", 8))
65
                        tmpl->subject = g_strdup(g_strstrip(buf + 8));
66
        }
67
68
        if (!tmpl->name) {
69
                g_warning("wrong template format\n");
70
                template_free(tmpl);
71
                return NULL;
72
        }
73
74
        if ((bytes_read = fread(buf, 1, sizeof(buf), fp)) == 0) {
75
                if (ferror(fp)) {
76
                        FILE_OP_ERROR(filename, "fread");
77
                        template_free(tmpl);
78
                        return NULL;
79
                }
80
        }
81
        fclose(fp);
82
        tmpl->value = g_strndup(buf, bytes_read);
83
84
        return tmpl;
85
}
86
87
void template_free(Template *tmpl)
88
{
89
        g_free(tmpl->name);
90
        g_free(tmpl->to);
91
        g_free(tmpl->cc);
92
        g_free(tmpl->subject);
93
        g_free(tmpl->value);
94
        g_free(tmpl);
95
}
96
97
void template_clear_config(GSList *tmpl_list)
98
{
99
        GSList *cur;
100
        Template *tmpl;
101
102
        for (cur = tmpl_list; cur != NULL; cur = cur->next) {
103
                tmpl = (Template *)cur->data;
104
                template_free(tmpl);
105
        }
106
        g_slist_free(tmpl_list);
107
}
108
109
GSList *template_read_config(void)
110
{
111
        const gchar *path;
112
        gchar *filename;
113
        DIR *dp;
114
        struct dirent *de;
115
        struct stat s;
116
        Template *tmpl;
117
        GSList *tmpl_list = NULL;
118
119
        path = get_template_dir();
120
        debug_print("%s:%d reading templates dir %s\n",
121
                    __FILE__, __LINE__, path);
122
123
        if (!is_dir_exist(path)) {
124
                if (make_dir(path) < 0)
125
                        return NULL;
126
        }
127
128
        if ((dp = opendir(path)) == NULL) {
129
                FILE_OP_ERROR(path, "opendir");
130
                return NULL;
131
        }
132
133
        while ((de = readdir(dp)) != NULL) {
134
                if (*de->d_name == '.') continue;
135
136
                filename = g_strconcat(path, G_DIR_SEPARATOR_S,
137
                                       de->d_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
        closedir(dp);
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->subject && *tmpl->subject != '\0')
201
                        fprintf(fp, "Subject: %s\n", tmpl->subject);
202
                fputs("\n", fp);
203
                fwrite(tmpl->value, sizeof(gchar) * strlen(tmpl->value), 1, fp);
204
205
                fclose(fp);
206
                g_free(filename);
207
        }
208
}
209
210
GSList *template_get_config(void)
211
{
212
        if (!template_list)
213
                template_list = template_read_config();
214
215
        return template_list;
216
}
217
218
void template_set_config(GSList *tmpl_list)
219
{
220
        template_clear_config(template_list);
221
        template_write_config(tmpl_list);
222
        template_list = tmpl_list;
223
}