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 #include "claws-features.h"
24 #endif
25
26 #include "defs.h"
27
28 #include "utils.h"
29 #include "autofaces.h"
30 #include "file-utils.h"
31
get_content_for_any_face(gchar * buf,gint len,gchar * anyname,gint maxlen)32 static gint get_content_for_any_face(gchar *buf, gint len, gchar *anyname, gint maxlen)
33 {
34 FILE *xfp;
35 gchar *xfile;
36 gint lastc;
37
38 xfile = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, AUTOFACES_DIR,
39 G_DIR_SEPARATOR_S, anyname, NULL);
40 buf[0] = '\0';
41 if ((xfp = claws_fopen(xfile, "rb")) == NULL) {
42 g_free(xfile);
43 debug_print("header content file '%s' not found\n", anyname);
44 return -1;
45 }
46 if (claws_fgets(buf, (len < maxlen)? len: maxlen, xfp) == NULL) {
47 claws_fclose(xfp);
48 g_free(xfile);
49 g_warning("header content file '%s' read failure", anyname);
50 return -2;
51 }
52 lastc = strlen(buf) - 1; /* remove trailing \n */
53 buf[lastc] = (buf[lastc] == '\n')? '\0': buf[lastc];
54 claws_fclose(xfp);
55 g_free(xfile);
56
57 return 0;
58 }
59
get_any_face_filename_for_account(gchar * facetype,gchar * accountname)60 static gchar * get_any_face_filename_for_account(gchar *facetype, gchar *accountname)
61 {
62 gchar *name = NULL;
63 gchar *what = NULL;
64 if (facetype == NULL || accountname == NULL)
65 return NULL;
66 if (*facetype == '\0' || *accountname == '\0')
67 return NULL;
68 what = name = g_strdup_printf("%s.%s", facetype, accountname);
69 while (*what) {
70 switch (*what) {
71 case '/':
72 case '\\':
73 case '<':
74 case '>':
75 case ':':
76 case '?':
77 case '*':
78 *what = '_';
79 break;
80 default:
81 if (*what <= ' ') {
82 *what = '_';
83 }
84 break;
85 }
86 ++what;
87 }
88 return name;
89 }
90
get_default_xface(gchar * buf,gint len)91 gint get_default_xface(gchar *buf, gint len) {
92 return get_content_for_any_face(buf, len, "xface", MAX_XFACE_LEN);
93 }
94
get_account_xface(gchar * buf,gint len,gchar * name)95 gint get_account_xface(gchar *buf, gint len, gchar *name) {
96 gchar *filename = get_any_face_filename_for_account("xface", name);
97 if (filename) {
98 gint result = get_content_for_any_face(buf, len, filename, MAX_XFACE_LEN);
99 g_free(filename);
100 return result;
101 }
102 g_warning("header xface filename invalid");
103 return -1;
104 }
105
get_default_face(gchar * buf,gint len)106 gint get_default_face(gchar *buf, gint len) {
107 return get_content_for_any_face(buf, len, "face", MAX_FACE_LEN);
108 }
109
get_account_face(gchar * buf,gint len,gchar * name)110 gint get_account_face(gchar *buf, gint len, gchar *name) {
111 gchar *filename = get_any_face_filename_for_account("face", name);
112 if (filename) {
113 gint result = get_content_for_any_face(buf, len, filename, MAX_FACE_LEN);
114 g_free(filename);
115 return result;
116 }
117 g_warning("header face filename invalid");
118 return -1;
119 }
120
121