common.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 void
  35. mc_skin_hash_destroy_value (gpointer data)
  36. {
  37. mc_skin_color_t *mc_skin_color = (mc_skin_color_t *) data;
  38. g_free (mc_skin_color->fgcolor);
  39. g_free (mc_skin_color->bgcolor);
  40. g_free (mc_skin_color);
  41. }
  42. /* --------------------------------------------------------------------------------------------- */
  43. static char *
  44. mc_skin_get_default_name (void)
  45. {
  46. char *tmp_str;
  47. /* from command line */
  48. if (mc_args__skin != NULL)
  49. return g_strdup (mc_args__skin);
  50. /* from envirovement variable */
  51. tmp_str = getenv ("MC_SKIN");
  52. if (tmp_str != NULL)
  53. return g_strdup (tmp_str);
  54. /* from config. Or 'default' if no present in config */
  55. return mc_config_get_string (mc_main_config, CONFIG_APP_SECTION, "skin", "default");
  56. }
  57. /* --------------------------------------------------------------------------------------------- */
  58. static void
  59. mc_skin_reinit (void)
  60. {
  61. mc_skin_deinit ();
  62. mc_skin__default.name = mc_skin_get_default_name ();
  63. mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal,
  64. g_free, mc_skin_hash_destroy_value);
  65. }
  66. /* --------------------------------------------------------------------------------------------- */
  67. static void
  68. mc_skin_try_to_load_default (void)
  69. {
  70. mc_skin_reinit ();
  71. g_free (mc_skin__default.name);
  72. mc_skin__default.name = g_strdup ("default");
  73. if (!mc_skin_ini_file_load (&mc_skin__default))
  74. {
  75. mc_skin_reinit ();
  76. mc_skin_set_hardcoded_skin (&mc_skin__default);
  77. }
  78. }
  79. /* --------------------------------------------------------------------------------------------- */
  80. /*** public functions ****************************************************************************/
  81. /* --------------------------------------------------------------------------------------------- */
  82. gboolean
  83. mc_skin_init (GError ** error)
  84. {
  85. gboolean is_good_init = TRUE;
  86. mc_skin__default.name = mc_skin_get_default_name ();
  87. mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal,
  88. g_free, mc_skin_hash_destroy_value);
  89. if (!mc_skin_ini_file_load (&mc_skin__default))
  90. {
  91. *error = g_error_new (MC_ERROR, 0,
  92. _("Unable to load '%s' skin.\nDefault skin has been loaded"),
  93. mc_skin__default.name);
  94. mc_skin_try_to_load_default ();
  95. is_good_init = FALSE;
  96. }
  97. mc_skin_colors_old_configure (&mc_skin__default);
  98. if (!mc_skin_ini_file_parse (&mc_skin__default))
  99. {
  100. if (*error == NULL)
  101. *error = g_error_new (MC_ERROR, 0,
  102. _("Unable to parse '%s' skin.\nDefault skin has been loaded"),
  103. mc_skin__default.name);
  104. mc_skin_try_to_load_default ();
  105. mc_skin_colors_old_configure (&mc_skin__default);
  106. (void) mc_skin_ini_file_parse (&mc_skin__default);
  107. is_good_init = FALSE;
  108. }
  109. mc_skin_is_init = TRUE;
  110. return is_good_init;
  111. }
  112. /* --------------------------------------------------------------------------------------------- */
  113. void
  114. mc_skin_deinit (void)
  115. {
  116. g_free (mc_skin__default.name);
  117. mc_skin__default.name = NULL;
  118. g_hash_table_destroy (mc_skin__default.colors);
  119. mc_skin__default.colors = NULL;
  120. g_free (mc_skin__default.description);
  121. mc_skin__default.description = NULL;
  122. mc_config_deinit (mc_skin__default.config);
  123. mc_skin__default.config = NULL;
  124. mc_skin_is_init = FALSE;
  125. }
  126. /* --------------------------------------------------------------------------------------------- */
  127. gchar *
  128. mc_skin_get (const gchar * group, const gchar * key, const gchar * default_value)
  129. {
  130. if (mc_args__ugly_line_drawing)
  131. {
  132. return g_strdup (default_value);
  133. }
  134. return mc_config_get_string (mc_skin__default.config, group, key, default_value);
  135. }
  136. /* --------------------------------------------------------------------------------------------- */