Statistics
| Revision:

root / src / template.c @ 2411

History | View | Annotate | Download (5.2 kB)

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