Merge branch '2873_diff_escape_dollar'
[midnight-commander.git] / lib / skin / common.c
blob1737a4b4a03e7bbf2c8de1dcab641b69223c1a4c
1 /*
2 Skins engine.
3 Interface functions
5 Copyright (C) 2009, 2010, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Slava Zanko <slavazanko@gmail.com>, 2009
10 Egmont Koblinger <egmont@gmail.com>, 2010
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include <config.h>
29 #include <stdlib.h>
31 #include "internal.h"
33 #include "lib/tty/color.h" /* tty_use_256colors(); */
35 /*** global variables ****************************************************************************/
37 mc_skin_t mc_skin__default;
39 /*** file scope macro definitions ****************************************************************/
41 /*** file scope type declarations ****************************************************************/
43 /*** file scope variables ************************************************************************/
45 static gboolean mc_skin_is_init = FALSE;
47 /* --------------------------------------------------------------------------------------------- */
48 /*** file scope functions ************************************************************************/
49 /* --------------------------------------------------------------------------------------------- */
51 static void
52 mc_skin_hash_destroy_value (gpointer data)
54 mc_skin_color_t *mc_skin_color = (mc_skin_color_t *) data;
55 g_free (mc_skin_color->fgcolor);
56 g_free (mc_skin_color->bgcolor);
57 g_free (mc_skin_color->attrs);
58 g_free (mc_skin_color);
61 /* --------------------------------------------------------------------------------------------- */
63 static char *
64 mc_skin_get_default_name (void)
66 char *tmp_str;
68 /* from command line */
69 if (mc_global.tty.skin != NULL)
70 return g_strdup (mc_global.tty.skin);
72 /* from envirovement variable */
73 tmp_str = getenv ("MC_SKIN");
74 if (tmp_str != NULL)
75 return g_strdup (tmp_str);
77 /* from config. Or 'default' if no present in config */
78 return mc_config_get_string (mc_main_config, CONFIG_APP_SECTION, "skin", "default");
81 /* --------------------------------------------------------------------------------------------- */
83 static void
84 mc_skin_reinit (void)
86 mc_skin_deinit ();
87 mc_skin__default.name = mc_skin_get_default_name ();
88 mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal,
89 g_free, mc_skin_hash_destroy_value);
92 /* --------------------------------------------------------------------------------------------- */
94 static void
95 mc_skin_try_to_load_default (void)
97 mc_skin_reinit ();
98 g_free (mc_skin__default.name);
99 mc_skin__default.name = g_strdup ("default");
100 if (!mc_skin_ini_file_load (&mc_skin__default))
102 mc_skin_reinit ();
103 mc_skin_set_hardcoded_skin (&mc_skin__default);
108 /* --------------------------------------------------------------------------------------------- */
109 /*** public functions ****************************************************************************/
110 /* --------------------------------------------------------------------------------------------- */
112 gboolean
113 mc_skin_init (GError ** error)
115 gboolean is_good_init = TRUE;
117 mc_skin__default.have_256_colors = FALSE;
119 mc_skin__default.name = mc_skin_get_default_name ();
121 mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal,
122 g_free, mc_skin_hash_destroy_value);
123 if (!mc_skin_ini_file_load (&mc_skin__default))
125 *error = g_error_new (MC_ERROR, 0,
126 _("Unable to load '%s' skin.\nDefault skin has been loaded"),
127 mc_skin__default.name);
129 mc_skin_try_to_load_default ();
130 is_good_init = FALSE;
132 mc_skin_colors_old_configure (&mc_skin__default);
134 if (!mc_skin_ini_file_parse (&mc_skin__default))
136 if (*error == NULL)
137 *error = g_error_new (MC_ERROR, 0,
138 _("Unable to parse '%s' skin.\nDefault skin has been loaded"),
139 mc_skin__default.name);
141 mc_skin_try_to_load_default ();
142 mc_skin_colors_old_configure (&mc_skin__default);
143 (void) mc_skin_ini_file_parse (&mc_skin__default);
144 is_good_init = FALSE;
146 if (is_good_init && !tty_use_256colors () && mc_skin__default.have_256_colors)
148 if (*error == NULL)
149 *error = g_error_new (MC_ERROR, 0,
151 ("Unable to use '%s' skin with 256 colors support\non non-256 colors terminal.\nDefault skin has been loaded"),
152 mc_skin__default.name);
154 mc_skin_try_to_load_default ();
155 mc_skin_colors_old_configure (&mc_skin__default);
156 (void) mc_skin_ini_file_parse (&mc_skin__default);
157 is_good_init = FALSE;
159 mc_skin_is_init = TRUE;
160 return is_good_init;
163 /* --------------------------------------------------------------------------------------------- */
165 void
166 mc_skin_deinit (void)
168 g_free (mc_skin__default.name);
169 mc_skin__default.name = NULL;
170 g_hash_table_destroy (mc_skin__default.colors);
171 mc_skin__default.colors = NULL;
173 g_free (mc_skin__default.description);
174 mc_skin__default.description = NULL;
176 mc_config_deinit (mc_skin__default.config);
177 mc_skin__default.config = NULL;
179 mc_skin_is_init = FALSE;
182 /* --------------------------------------------------------------------------------------------- */
184 gchar *
185 mc_skin_get (const gchar * group, const gchar * key, const gchar * default_value)
187 if (mc_global.tty.ugly_line_drawing)
189 return g_strdup (default_value);
191 return mc_config_get_string (mc_skin__default.config, group, key, default_value);
194 /* --------------------------------------------------------------------------------------------- */