1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/*
Skins engine.
Reading and parse ini-files
Copyright (C) 2009-2022
Free Software Foundation, Inc.
Written by:
Slava Zanko <slavazanko@gmail.com>, 2009.
This file is part of the Midnight Commander.
The Midnight Commander is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
The Midnight Commander is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <string.h>
#include "lib/global.h" /* <glib.h> */
#include "internal.h"
#include "lib/fileloc.h"
#include "lib/util.h" /* exist_file() */
/*** global variables ****************************************************************************/
/*** file scope macro definitions ****************************************************************/
/*** file scope type declarations ****************************************************************/
/*** file scope variables ************************************************************************/
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
static void
mc_skin_get_list_from_dir (const gchar * base_dir, GPtrArray * list)
{
gchar *name;
GDir *dir;
name = g_build_filename (base_dir, MC_SKINS_DIR, (char *) NULL);
dir = g_dir_open (name, 0, NULL);
g_free (name);
if (dir != NULL)
{
const gchar *cname;
while ((cname = g_dir_read_name (dir)) != NULL)
{
gchar *sname;
size_t slen;
unsigned int i;
slen = strlen (cname);
sname = g_strndup (cname, slen);
if (slen > 4 && strcmp (sname + slen - 4, ".ini") == 0)
sname[slen - 4] = '\0';
for (i = 0; i < list->len; i++)
if (strcmp (sname, g_ptr_array_index (list, i)) == 0)
break;
if (i < list->len)
g_free (sname);
else
g_ptr_array_add (list, sname);
}
g_dir_close (dir);
}
}
/* --------------------------------------------------------------------------------------------- */
static int
string_array_comparator (gconstpointer a, gconstpointer b)
{
return strcmp (*(char *const *) a, *(char *const *) b);
}
/* --------------------------------------------------------------------------------------------- */
static gboolean
mc_skin_ini_file_load_search_in_dir (mc_skin_t * mc_skin, const gchar * base_dir)
{
char *file_name, *file_name2;
file_name = g_build_filename (base_dir, MC_SKINS_DIR, mc_skin->name, (char *) NULL);
if (exist_file (file_name))
{
mc_skin->config = mc_config_init (file_name, TRUE);
g_free (file_name);
return (mc_skin->config != NULL);
}
g_free (file_name);
file_name2 = g_strdup_printf ("%s.ini", mc_skin->name);
file_name = g_build_filename (base_dir, MC_SKINS_DIR, file_name2, (char *) NULL);
g_free (file_name2);
if (exist_file (file_name))
{
mc_skin->config = mc_config_init (file_name, TRUE);
g_free (file_name);
return (mc_skin->config != NULL);
}
g_free (file_name);
return FALSE;
}
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
GPtrArray *
mc_skin_list (void)
{
GPtrArray *list;
list = g_ptr_array_new ();
mc_skin_get_list_from_dir (mc_config_get_data_path (), list);
mc_skin_get_list_from_dir (mc_global.sysconfig_dir, list);
mc_skin_get_list_from_dir (mc_global.share_data_dir, list);
g_ptr_array_sort (list, (GCompareFunc) string_array_comparator);
return list;
}
/* --------------------------------------------------------------------------------------------- */
gboolean
mc_skin_ini_file_load (mc_skin_t * mc_skin)
{
char *file_name;
file_name = g_path_get_basename (mc_skin->name);
if (file_name == NULL)
return FALSE;
if (strcmp (file_name, mc_skin->name) != 0)
{
g_free (file_name);
if (!g_path_is_absolute (mc_skin->name))
return FALSE;
mc_skin->config = mc_config_init (mc_skin->name, TRUE);
return (mc_skin->config != NULL);
}
g_free (file_name);
/* ${XDG_DATA_HOME}/mc/skins/ */
if (mc_skin_ini_file_load_search_in_dir (mc_skin, mc_config_get_data_path ()))
return TRUE;
/* /etc/mc/skins/ */
if (mc_skin_ini_file_load_search_in_dir (mc_skin, mc_global.sysconfig_dir))
return TRUE;
/* /usr/share/mc/skins/ */
return mc_skin_ini_file_load_search_in_dir (mc_skin, mc_global.share_data_dir);
}
/* --------------------------------------------------------------------------------------------- */
gboolean
mc_skin_ini_file_parse (mc_skin_t * mc_skin)
{
mc_skin->description =
mc_config_get_string (mc_skin->config, "skin", "description", "- no description -");
if (!mc_skin_color_parse_ini_file (mc_skin))
return FALSE;
mc_skin_lines_parse_ini_file (mc_skin);
mc_skin->have_256_colors = mc_config_get_bool (mc_skin->config, "skin", "256colors", FALSE);
mc_skin->have_true_colors = mc_config_get_bool (mc_skin->config, "skin", "truecolors", FALSE);
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
void
mc_skin_set_hardcoded_skin (mc_skin_t * mc_skin)
{
mc_skin->config = mc_config_init (NULL, TRUE);
mc_config_set_string (mc_skin->config, "skin", "description", "hardcoded skin");
mc_skin_hardcoded_ugly_lines (mc_skin);
mc_skin_hardcoded_blackwhite_colors (mc_skin);
}
/* --------------------------------------------------------------------------------------------- */
|