common.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. Skins engine.
  3. Interface functions
  4. Copyright (C) 2009 The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2009.
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software; you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation; either version 2 of the
  11. License, or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be
  13. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  14. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. MA 02110-1301, USA.
  20. */
  21. #include <config.h>
  22. #include <stdlib.h>
  23. #include "internal.h"
  24. #include "src/args.h"
  25. /*** global variables ****************************************************************************/
  26. mc_skin_t mc_skin__default;
  27. /*** file scope macro definitions ****************************************************************/
  28. /*** file scope type declarations ****************************************************************/
  29. /*** file scope variables ************************************************************************/
  30. static gboolean mc_skin_is_init = FALSE;
  31. /* --------------------------------------------------------------------------------------------- */
  32. /*** file scope functions ************************************************************************/
  33. /* --------------------------------------------------------------------------------------------- */
  34. static inline void
  35. mc_skin_hash_destroy_key (gpointer data)
  36. {
  37. g_free (data);
  38. }
  39. /* --------------------------------------------------------------------------------------------- */
  40. static void
  41. mc_skin_hash_destroy_value (gpointer data)
  42. {
  43. mc_skin_color_t *mc_skin_color = (mc_skin_color_t *) data;
  44. g_free (mc_skin_color->fgcolor);
  45. g_free (mc_skin_color->bgcolor);
  46. g_free (mc_skin_color);
  47. }
  48. /* --------------------------------------------------------------------------------------------- */
  49. static char *
  50. mc_skin_get_default_name (void)
  51. {
  52. char *tmp_str;
  53. /* from command line */
  54. if (mc_args__skin != NULL)
  55. return g_strdup (mc_args__skin);
  56. /* from envirovement variable */
  57. tmp_str = getenv ("MC_SKIN");
  58. if (tmp_str != NULL)
  59. return g_strdup (tmp_str);
  60. /* from config. Or 'default' if no present in config */
  61. return mc_config_get_string (mc_main_config, CONFIG_APP_SECTION, "skin", "default");
  62. }
  63. /* --------------------------------------------------------------------------------------------- */
  64. static void
  65. mc_skin_reinit (void)
  66. {
  67. mc_skin_deinit ();
  68. mc_skin__default.name = mc_skin_get_default_name ();
  69. mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal,
  70. mc_skin_hash_destroy_key,
  71. mc_skin_hash_destroy_value);
  72. }
  73. /* --------------------------------------------------------------------------------------------- */
  74. static void
  75. mc_skin_try_to_load_default (void)
  76. {
  77. mc_skin_reinit ();
  78. g_free (mc_skin__default.name);
  79. mc_skin__default.name = g_strdup ("default");
  80. if (!mc_skin_ini_file_load (&mc_skin__default)) {
  81. mc_skin_reinit ();
  82. mc_skin_set_hardcoded_skin (&mc_skin__default);
  83. }
  84. }
  85. /* --------------------------------------------------------------------------------------------- */
  86. /*** public functions ****************************************************************************/
  87. /* --------------------------------------------------------------------------------------------- */
  88. gboolean
  89. mc_skin_init (GError ** error)
  90. {
  91. gboolean is_good_init = TRUE;
  92. mc_skin__default.name = mc_skin_get_default_name ();
  93. mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal,
  94. mc_skin_hash_destroy_key,
  95. mc_skin_hash_destroy_value);
  96. if (!mc_skin_ini_file_load (&mc_skin__default)) {
  97. *error = g_error_new (MC_ERROR, 0,
  98. _("Unable to load '%s' skin.\nDefault skin has been loaded"),
  99. mc_skin__default.name);
  100. mc_skin_try_to_load_default ();
  101. is_good_init = FALSE;
  102. }
  103. mc_skin_colors_old_configure (&mc_skin__default);
  104. if (!mc_skin_ini_file_parse (&mc_skin__default)) {
  105. if (*error == NULL)
  106. *error = g_error_new (MC_ERROR, 0,
  107. _("Unable to parse '%s' skin.\nDefault skin has been loaded"),
  108. mc_skin__default.name);
  109. mc_skin_try_to_load_default ();
  110. mc_skin_colors_old_configure (&mc_skin__default);
  111. (void) mc_skin_ini_file_parse (&mc_skin__default);
  112. is_good_init = FALSE;
  113. }
  114. mc_skin_is_init = TRUE;
  115. return is_good_init;
  116. }
  117. /* --------------------------------------------------------------------------------------------- */
  118. void
  119. mc_skin_deinit (void)
  120. {
  121. g_free (mc_skin__default.name);
  122. mc_skin__default.name = NULL;
  123. g_hash_table_destroy (mc_skin__default.colors);
  124. mc_skin__default.colors = NULL;
  125. g_free (mc_skin__default.description);
  126. mc_skin__default.description = NULL;
  127. if (mc_skin__default.config) {
  128. mc_config_deinit (mc_skin__default.config);
  129. mc_skin__default.config = NULL;
  130. }
  131. mc_skin_is_init = FALSE;
  132. }
  133. /* --------------------------------------------------------------------------------------------- */
  134. gchar *
  135. mc_skin_get(const gchar *group, const gchar *key, const gchar *default_value)
  136. {
  137. if (mc_args__ugly_line_drawing) {
  138. return g_strdup(default_value);
  139. }
  140. return mc_config_get_string(mc_skin__default.config, group, key, default_value);
  141. }
  142. /* --------------------------------------------------------------------------------------------- */