2 * Sylpheed templates subsystem
3 * Copyright (C) 2001 Alexander Barinov
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 static GSList
* template_load(GSList
*tmpl_list
, gchar
*filename
)
39 LOG_MESSAGE(_("%s:%d loading template from %s\n"), __FILE__
, __LINE__
, filename
);
41 if ((fp
= fopen(filename
, "r")) == NULL
) {
42 FILE_OP_ERROR(filename
, "fopen");
46 tmpl
= g_new(Template
, 1);
48 if (fgets(buf
, sizeof(buf
), fp
) == NULL
) {
49 FILE_OP_ERROR(filename
, "fgets");
51 LOG_MESSAGE(_("%s:%d exiting\n"), __FILE__
, __LINE__
);
54 tmpl
->name
= g_strdup(g_strstrip(buf
));
56 memset(buf
, 0, sizeof(buf
));
57 if ((bytes_read
= fread(buf
, 1, sizeof(buf
)-1, fp
)) == 0) {
58 FILE_OP_ERROR(filename
, "fread");
63 tmpl
->value
= g_strdup(buf
);
65 tmpl_list
= g_slist_append(tmpl_list
, tmpl
);
70 void template_free(Template
*tmpl
)
77 void template_clear_config(GSList
*tmpl_list
)
81 while (tmpl_list
!= NULL
) {
82 tmpl
= tmpl_list
->data
;
84 tmpl_list
= g_slist_remove(tmpl_list
, tmpl
);
88 GSList
* template_read_config(void)
95 GSList
*tmpl_list
= NULL
;
97 path
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, TEMPLATES_DIR
, NULL
);
98 LOG_MESSAGE(_("%s:%d reading templates dir %s\n"), __FILE__
, __LINE__
, path
);
100 if ((dp
= opendir(path
)) == NULL
) {
101 FILE_OP_ERROR(path
, "opendir");
105 while ((de
= readdir(dp
)) != NULL
) {
106 filename
= g_strconcat(path
, G_DIR_SEPARATOR_S
, de
->d_name
, NULL
);
107 LOG_MESSAGE(_("%s:%d found file %s\n"), __FILE__
, __LINE__
, filename
);
109 if (stat(filename
, &s
) != 0 || !S_ISREG(s
.st_mode
) ) {
110 LOG_MESSAGE(_("%s:%d %s is not an ordinary file\n"),
111 __FILE__
, __LINE__
, filename
);
115 tmpl_list
= template_load(tmpl_list
, filename
);
124 void template_write_config(GSList
*tmpl_list
)
133 path
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, TEMPLATES_DIR
, NULL
);
135 if (!is_dir_exist(path
)) {
136 if (is_file_exist(path
)) {
137 LOG_MESSAGE(_("%s:%d file %s allready exists\n"),
138 __FILE__
, __LINE__
, filename
);
142 if (mkdir(path
, S_IRWXU
) < 0) {
143 FILE_OP_ERROR(path
, "mkdir");
149 remove_all_files(path
);
151 for (cur
= tmpl_list
; cur
!= NULL
; cur
= cur
->next
) {
154 filename
= g_strconcat(path
, G_DIR_SEPARATOR_S
, itos(tmpl_num
), NULL
);
156 if ((fp
= fopen(filename
, "w")) == NULL
) {
157 FILE_OP_ERROR(filename
, "fopen");
163 LOG_MESSAGE(_("%s:%d writing template \"%s\" to %s\n"),
164 __FILE__
, __LINE__
, tmpl
->name
, filename
);
165 fputs(tmpl
->name
, fp
);
167 fwrite(tmpl
->value
, sizeof(gchar
), strlen(tmpl
->value
), fp
);