Applied MC indentation policy.
[midnight-commander.git] / lib / skin / common.c
blobc16966bbb473bcf79b51fcbfccab971535fc8c30
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 "internal.h"
33 #include "src/args.h"
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 inline void
52 mc_skin_hash_destroy_key (gpointer data)
54 g_free (data);
57 /* --------------------------------------------------------------------------------------------- */
59 static void
60 mc_skin_hash_destroy_value (gpointer data)
62 mc_skin_color_t *mc_skin_color = (mc_skin_color_t *) data;
63 g_free (mc_skin_color->fgcolor);
64 g_free (mc_skin_color->bgcolor);
65 g_free (mc_skin_color);
68 /* --------------------------------------------------------------------------------------------- */
70 static char *
71 mc_skin_get_default_name (void)
73 char *tmp_str;
75 /* from command line */
76 if (mc_args__skin != NULL)
77 return g_strdup (mc_args__skin);
79 /* from envirovement variable */
80 tmp_str = getenv ("MC_SKIN");
81 if (tmp_str != NULL)
82 return g_strdup (tmp_str);
84 /* from config. Or 'default' if no present in config */
85 return mc_config_get_string (mc_main_config, CONFIG_APP_SECTION, "skin", "default");
88 /* --------------------------------------------------------------------------------------------- */
90 static void
91 mc_skin_reinit (void)
93 mc_skin_deinit ();
94 mc_skin__default.name = mc_skin_get_default_name ();
95 mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal,
96 mc_skin_hash_destroy_key,
97 mc_skin_hash_destroy_value);
100 /* --------------------------------------------------------------------------------------------- */
102 static void
103 mc_skin_try_to_load_default (void)
105 mc_skin_reinit ();
106 g_free (mc_skin__default.name);
107 mc_skin__default.name = g_strdup ("default");
108 if (!mc_skin_ini_file_load (&mc_skin__default)) {
109 mc_skin_reinit ();
110 mc_skin_set_hardcoded_skin (&mc_skin__default);
115 /* --------------------------------------------------------------------------------------------- */
116 /*** public functions ****************************************************************************/
117 /* --------------------------------------------------------------------------------------------- */
119 gboolean
120 mc_skin_init (GError ** error)
122 gboolean is_good_init = TRUE;
124 mc_skin__default.name = mc_skin_get_default_name ();
126 mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal,
127 mc_skin_hash_destroy_key,
128 mc_skin_hash_destroy_value);
130 if (!mc_skin_ini_file_load (&mc_skin__default)) {
131 *error = g_error_new (MC_ERROR, 0,
132 _("Unable to load '%s' skin.\nDefault skin has been loaded"),
133 mc_skin__default.name);
135 mc_skin_try_to_load_default ();
136 is_good_init = FALSE;
138 mc_skin_colors_old_configure (&mc_skin__default);
140 if (!mc_skin_ini_file_parse (&mc_skin__default)) {
141 if (*error == NULL)
142 *error = g_error_new (MC_ERROR, 0,
143 _("Unable to parse '%s' skin.\nDefault skin has been loaded"),
144 mc_skin__default.name);
146 mc_skin_try_to_load_default ();
147 mc_skin_colors_old_configure (&mc_skin__default);
148 (void) mc_skin_ini_file_parse (&mc_skin__default);
149 is_good_init = FALSE;
151 mc_skin_is_init = TRUE;
152 return is_good_init;
155 /* --------------------------------------------------------------------------------------------- */
157 void
158 mc_skin_deinit (void)
161 g_free (mc_skin__default.name);
162 mc_skin__default.name = NULL;
163 g_hash_table_destroy (mc_skin__default.colors);
164 mc_skin__default.colors = NULL;
166 g_free (mc_skin__default.description);
167 mc_skin__default.description = NULL;
169 if (mc_skin__default.config) {
170 mc_config_deinit (mc_skin__default.config);
171 mc_skin__default.config = NULL;
174 mc_skin_is_init = FALSE;
177 /* --------------------------------------------------------------------------------------------- */
179 gchar *
180 mc_skin_get(const gchar *group, const gchar *key, const gchar *default_value)
182 if (mc_args__ugly_line_drawing) {
183 return g_strdup(default_value);
185 return mc_config_get_string(mc_skin__default.config, group, key, default_value);
188 /* --------------------------------------------------------------------------------------------- */