Statistics
| Revision:

root / src / template.c @ 1

History | View | Annotate | Download (4.79 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 <stdio.h>
25
#include <dirent.h>
26
#include <sys/stat.h>
27
#include <ctype.h>
28

    
29
#include "intl.h"
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 = 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_strncasecmp(buf, "Name:", 5))
59
                        tmpl->name = g_strdup(g_strstrip(buf + 5));
60
                else if (!g_strncasecmp(buf, "To:", 3))
61
                        tmpl->to = g_strdup(g_strstrip(buf + 3));
62
                else if (!g_strncasecmp(buf, "Cc:", 3))
63
                        tmpl->cc = g_strdup(g_strstrip(buf + 3));
64
                else if (!g_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, de->d_name, NULL);
137

    
138
                if (stat(filename, &s) != 0 || !S_ISREG(s.st_mode) ) {
139
                        debug_print("%s:%d %s is not an ordinary file\n",
140
                                    __FILE__, __LINE__, filename);
141
                        continue;
142
                }
143

    
144
                tmpl = template_load(filename);
145
                if (tmpl)
146
                        tmpl_list = g_slist_append(tmpl_list, tmpl);
147
                g_free(filename);
148
        }
149

    
150
        closedir(dp);
151

    
152
        return tmpl_list;
153
}
154

    
155
void template_write_config(GSList *tmpl_list)
156
{
157
        const gchar *path;
158
        GSList *cur;
159
        Template *tmpl;
160
        FILE *fp;
161
        gint tmpl_num;
162

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

    
165
        path = get_template_dir();
166

    
167
        if (!is_dir_exist(path)) {
168
                if (is_file_exist(path)) {
169
                        g_warning(_("file %s already exists\n"), path);
170
                        return;
171
                }
172
                if (make_dir(path) < 0)
173
                        return;
174
        }
175

    
176
        remove_all_files(path);
177

    
178
        for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
179
             cur = cur->next, tmpl_num++) {
180
                gchar *filename;
181

    
182
                tmpl = cur->data;
183

    
184
                filename = g_strconcat(path, G_DIR_SEPARATOR_S,
185
                                       itos(tmpl_num), NULL);
186

    
187
                if ((fp = fopen(filename, "wb")) == NULL) {
188
                        FILE_OP_ERROR(filename, "fopen");
189
                        g_free(filename);
190
                        return;
191
                }
192

    
193
                fprintf(fp, "Name: %s\n", tmpl->name);
194
                if (tmpl->to && *tmpl->to != '\0')
195
                        fprintf(fp, "To: %s\n", tmpl->to);
196
                if (tmpl->cc && *tmpl->cc != '\0')
197
                        fprintf(fp, "Cc: %s\n", tmpl->cc);
198
                if (tmpl->subject && *tmpl->subject != '\0')
199
                        fprintf(fp, "Subject: %s\n", tmpl->subject);
200
                fputs("\n", fp);
201
                fwrite(tmpl->value, sizeof(gchar) * strlen(tmpl->value), 1, fp);
202
                fclose(fp);
203
        }
204
}
205

    
206
GSList *template_get_config(void)
207
{
208
        if (!template_list)
209
                template_list = template_read_config();
210

    
211
        return template_list;
212
}
213

    
214
void template_set_config(GSList *tmpl_list)
215
{
216
        template_clear_config(template_list);
217
        template_write_config(tmpl_list);
218
        template_list = tmpl_list;
219
}