2012-07-03 [mir] 3.8.1cvs5
[claws.git] / src / autofaces.c
1
2 /*
3  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
4  * Copyright (C) 2008-2012 Ricardo Mones and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
18  * 
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include "defs.h"
26
27 #include "utils.h"
28 #include "autofaces.h"
29
30 static gint get_content_for_any_face(gchar *buf, gint len, gchar *anyname, gint maxlen)
31 {
32         FILE  *xfp;
33         gchar *xfile;
34         gint  lastc;
35
36         xfile = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, AUTOFACES_DIR,
37                             G_DIR_SEPARATOR_S, anyname, NULL);
38         buf[0] = '\0';
39         if ((xfp = g_fopen(xfile, "rb")) == NULL) {
40                 g_free(xfile);
41                 debug_print("header content file '%s' not found\n", anyname);
42                 return -1;
43         }
44         if (fgets(buf, (len < maxlen)? len: maxlen, xfp) == NULL) {
45                 fclose(xfp);
46                 g_free(xfile);
47                 g_warning("header content file '%s' read failure\n", anyname);
48                 return -2;
49         }
50         lastc = strlen(buf) - 1;        /* remove trailing \n */
51         buf[lastc] = (buf[lastc] == '\n')? '\0': buf[lastc];
52         fclose(xfp);
53         g_free(xfile);
54
55         return 0;
56 }
57
58 static gchar * get_any_face_filename_for_account(gchar *facetype, gchar *accountname)
59 {
60         gchar *name = NULL;
61         gchar *what = NULL;
62         if (facetype == NULL || accountname == NULL) 
63                 return NULL;
64         if (*facetype == '\0' || *accountname == '\0') 
65                 return NULL;
66         what = name = g_strdup_printf("%s.%s", facetype, accountname);
67         while (*what) {
68                 switch (*what) {
69                 case '/':
70                 case '\\':
71                 case '<':
72                 case '>':
73                 case ':':
74                 case '?':
75                 case '*':
76                         *what = '_';
77                         break;
78                 default:
79                         if (*what <= ' ') {
80                                 *what = '_';
81                         }
82                         break;
83                 }
84                 ++what;
85         }
86         return name;
87 }
88
89 gint get_default_xface(gchar *buf, gint len) {
90         return get_content_for_any_face(buf, len, "xface", MAX_XFACE_LEN);
91 }
92
93 gint get_account_xface(gchar *buf, gint len, gchar *name) {
94         gchar *filename = get_any_face_filename_for_account("xface", name);
95         if (filename) {
96                 gint result = get_content_for_any_face(buf, len, filename, MAX_XFACE_LEN);
97                 g_free(filename);
98                 return result;
99         }
100         g_warning("header xface filename invalid\n");
101         return -1;
102 }
103
104 gint get_default_face(gchar *buf, gint len) {
105         return get_content_for_any_face(buf, len, "face", MAX_FACE_LEN);
106 }
107
108 gint get_account_face(gchar *buf, gint len, gchar *name) {
109         gchar *filename = get_any_face_filename_for_account("face", name);
110         if (filename) {
111                 gint result = get_content_for_any_face(buf, len, filename, MAX_FACE_LEN);
112                 g_free(filename);
113                 return result;
114         }
115         g_warning("header face filename invalid\n");
116         return -1;
117 }
118