New address book.
[claws.git] / src / template.c
1 /*
2  * Sylpheed templates subsystem 
3  * Copyright (C) 2001 Alexander Barinov
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include "defs.h"
21
22 #include <gdk/gdk.h>
23 #include <stdio.h>
24 #include <dirent.h>
25 #include <sys/stat.h>
26
27 #include "utils.h"
28 #include "main.h"
29 #include "template.h"
30 #include "intl.h"
31
32 static GSList* template_load(GSList *tmpl_list, gchar *filename)
33 {
34         Template *tmpl;
35         FILE *fp;
36         char buf[32000];
37         gint bytes_read;
38
39         LOG_MESSAGE(_("%s:%d loading template from %s\n"), __FILE__, __LINE__, filename);
40
41         if ((fp = fopen(filename, "r")) == NULL) {
42                 FILE_OP_ERROR(filename, "fopen");
43                 return tmpl_list;
44         }
45
46         tmpl = g_new(Template, 1);
47         
48         if (fgets(buf, sizeof(buf), fp) == NULL) {
49                 FILE_OP_ERROR(filename, "fgets");
50                 g_free(tmpl);
51                 LOG_MESSAGE(_("%s:%d exiting\n"), __FILE__, __LINE__);
52                 return tmpl_list;
53         }
54         tmpl->name = g_strdup(g_strstrip(buf));
55
56         memset(buf, 0, sizeof(buf));
57         if ((bytes_read = fread(buf, 1, sizeof(buf)-1, fp)) == 0) {
58                 FILE_OP_ERROR(filename, "fread");
59                 g_free(tmpl->name);
60                 g_free(tmpl);
61                 return tmpl_list;
62         }
63         tmpl->value = g_strdup(buf);
64
65         tmpl_list = g_slist_append(tmpl_list, tmpl);
66         fclose(fp);
67         return tmpl_list;
68 }
69
70 void template_free(Template *tmpl)
71 {
72         g_free(tmpl->name);
73         g_free(tmpl->value);
74         g_free(tmpl);
75 }
76
77 void template_clear_config(GSList *tmpl_list)
78 {
79         Template *tmpl;
80
81         while (tmpl_list != NULL) {
82                 tmpl = tmpl_list->data;
83                 template_free(tmpl);
84                 tmpl_list = g_slist_remove(tmpl_list, tmpl);
85         }
86 }
87
88 GSList* template_read_config(void)
89 {
90         gchar *path;
91         gchar *filename;
92         DIR *dp;
93         struct dirent *de;
94         struct stat s;
95         GSList *tmpl_list = NULL;
96
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);
99
100         if ((dp = opendir(path)) == NULL) {
101                 FILE_OP_ERROR(path, "opendir");
102                 return tmpl_list;
103         }
104
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);
108
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);
112                         continue;
113                 }
114
115                 tmpl_list = template_load(tmpl_list, filename);
116                 g_free(filename);
117         }
118
119         closedir(dp);
120         g_free(path);
121         return tmpl_list;
122 }
123
124 void template_write_config(GSList *tmpl_list)
125 {
126         gchar *path;
127         gchar *filename;
128         GSList *cur;
129         Template *tmpl;
130         FILE *fp;
131         gint tmpl_num = 1;
132
133         path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, TEMPLATES_DIR, NULL);
134
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);
139                         g_free(path);
140                         return;
141                 }
142                 if (mkdir(path, S_IRWXU) < 0) {
143                         FILE_OP_ERROR(path, "mkdir");
144                         g_free(path);
145                         return;
146                 }
147         }
148
149         remove_all_files(path);
150
151         for (cur = tmpl_list; cur != NULL; cur = cur->next) {
152                 tmpl = cur->data;
153
154                 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(tmpl_num), NULL);
155
156                 if ((fp = fopen(filename, "w")) == NULL) {
157                         FILE_OP_ERROR(filename, "fopen");
158                         g_free(filename);
159                         g_free(path);
160                         return;
161                 }
162
163                 LOG_MESSAGE(_("%s:%d writing template \"%s\" to %s\n"), 
164                             __FILE__, __LINE__, tmpl->name, filename);
165                 fputs(tmpl->name, fp);
166                 fputs("\n", fp);
167                 fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp);
168                 fclose(fp);
169
170                 tmpl_num ++;
171         }
172
173         g_free(path);
174 }