Revert "Ticket #1605: Incorrect parsing FTP-string"
[midnight-commander.git] / src / skin / common.c
blob6d3496f81f54d1748afe7d7510f0662b8604d91d
1 /*
2 Skins engine.
3 Interface functions
5 Copyright (C) 2009 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2009.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software; you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be
18 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
19 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 MA 02110-1301, USA.
28 #include <config.h>
29 #include <stdlib.h>
31 #include "../src/global.h"
32 #include "../src/args.h"
33 #include "skin.h"
34 #include "internal.h"
36 /*** global variables ****************************************************************************/
38 mc_skin_t mc_skin__default;
40 /*** file scope macro definitions ****************************************************************/
42 /*** file scope type declarations ****************************************************************/
44 /*** file scope variables ************************************************************************/
46 static gboolean mc_skin_is_init = FALSE;
48 /* --------------------------------------------------------------------------------------------- */
49 /*** file scope functions ************************************************************************/
50 /* --------------------------------------------------------------------------------------------- */
52 static inline void
53 mc_skin_hash_destroy_key (gpointer data)
55 g_free (data);
58 /* --------------------------------------------------------------------------------------------- */
60 static void
61 mc_skin_hash_destroy_value (gpointer data)
63 mc_skin_color_t *mc_skin_color = (mc_skin_color_t *) data;
64 g_free (mc_skin_color->fgcolor);
65 g_free (mc_skin_color->bgcolor);
66 g_free (mc_skin_color);
69 /* --------------------------------------------------------------------------------------------- */
71 static char *
72 mc_skin_get_default_name (void)
74 char *tmp_str;
76 /* from command line */
77 if (mc_args__skin != NULL)
78 return g_strdup (mc_args__skin);
80 /* from envirovement variable */
81 tmp_str = getenv ("MC_SKIN");
82 if (tmp_str != NULL)
83 return g_strdup (tmp_str);
85 /* from config. Or 'default' if no present in config */
86 return mc_config_get_string (mc_main_config, CONFIG_APP_SECTION, "skin", "default");
89 /* --------------------------------------------------------------------------------------------- */
91 static void
92 mc_skin_reinit (void)
94 mc_skin_deinit ();
95 mc_skin__default.name = mc_skin_get_default_name ();
96 mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal,
97 mc_skin_hash_destroy_key,
98 mc_skin_hash_destroy_value);
101 /* --------------------------------------------------------------------------------------------- */
103 static void
104 mc_skin_try_to_load_default (void)
106 mc_skin_reinit ();
107 g_free (mc_skin__default.name);
108 mc_skin__default.name = g_strdup ("default");
109 if (!mc_skin_ini_file_load (&mc_skin__default)) {
110 mc_skin_reinit ();
111 mc_skin_set_hardcoded_skin (&mc_skin__default);
116 /* --------------------------------------------------------------------------------------------- */
117 /*** public functions ****************************************************************************/
118 /* --------------------------------------------------------------------------------------------- */
120 gboolean
121 mc_skin_init (GError ** error)
123 gboolean is_good_init = TRUE;
125 mc_skin__default.name = mc_skin_get_default_name ();
127 mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal,
128 mc_skin_hash_destroy_key,
129 mc_skin_hash_destroy_value);
131 if (!mc_skin_ini_file_load (&mc_skin__default)) {
132 *error = g_error_new (MC_ERROR, 0,
133 _("Unable to load '%s' skin.\nDefault skin has been loaded"),
134 mc_skin__default.name);
136 mc_skin_try_to_load_default ();
137 is_good_init = FALSE;
139 mc_skin_colors_old_configure (&mc_skin__default);
141 if (!mc_skin_ini_file_parse (&mc_skin__default)) {
142 if (*error == NULL)
143 *error = g_error_new (MC_ERROR, 0,
144 _("Unable to parse '%s' skin.\nDefault skin has been loaded"),
145 mc_skin__default.name);
147 mc_skin_try_to_load_default ();
148 mc_skin_colors_old_configure (&mc_skin__default);
149 (void) mc_skin_ini_file_parse (&mc_skin__default);
150 is_good_init = FALSE;
152 mc_skin_is_init = TRUE;
153 return is_good_init;
156 /* --------------------------------------------------------------------------------------------- */
158 void
159 mc_skin_deinit (void)
162 g_free (mc_skin__default.name);
163 mc_skin__default.name = NULL;
164 g_hash_table_destroy (mc_skin__default.colors);
165 mc_skin__default.colors = NULL;
167 g_free (mc_skin__default.description);
168 mc_skin__default.description = NULL;
170 if (mc_skin__default.config) {
171 mc_config_deinit (mc_skin__default.config);
172 mc_skin__default.config = NULL;
175 mc_skin_is_init = FALSE;
178 /* --------------------------------------------------------------------------------------------- */
180 gchar *
181 mc_skin_get(const gchar *group, const gchar *key, const gchar *default_value)
183 if (mc_args__ugly_line_drawing) {
184 return g_strdup(default_value);
186 return mc_config_get_string(mc_skin__default.config, group, key, default_value);
189 /* --------------------------------------------------------------------------------------------- */