common.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. Skins engine.
  3. Interface functions
  4. Copyright (C) 2009, 2010, 2011
  5. The Free Software Foundation, Inc.
  6. Written by:
  7. Slava Zanko <slavazanko@gmail.com>, 2009
  8. Egmont Koblinger <egmont@gmail.com>, 2010
  9. This file is part of the Midnight Commander.
  10. The Midnight Commander is free software: you can redistribute it
  11. and/or modify it under the terms of the GNU General Public License as
  12. published by the Free Software Foundation, either version 3 of the License,
  13. or (at your option) any later version.
  14. The Midnight Commander is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <stdlib.h>
  23. #include "internal.h"
  24. #include "lib/tty/color.h" /* tty_use_256colors(); */
  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->attrs);
  41. g_free (mc_skin_color);
  42. }
  43. /* --------------------------------------------------------------------------------------------- */
  44. static char *
  45. mc_skin_get_default_name (void)
  46. {
  47. char *tmp_str;
  48. /* from command line */
  49. if (mc_global.tty.skin != NULL)
  50. return g_strdup (mc_global.tty.skin);
  51. /* from envirovement variable */
  52. tmp_str = getenv ("MC_SKIN");
  53. if (tmp_str != NULL)
  54. return g_strdup (tmp_str);
  55. /* from config. Or 'default' if no present in config */
  56. return mc_config_get_string (mc_main_config, CONFIG_APP_SECTION, "skin", "default");
  57. }
  58. /* --------------------------------------------------------------------------------------------- */
  59. static void
  60. mc_skin_reinit (void)
  61. {
  62. mc_skin_deinit ();
  63. mc_skin__default.name = mc_skin_get_default_name ();
  64. mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal,
  65. g_free, mc_skin_hash_destroy_value);
  66. }
  67. /* --------------------------------------------------------------------------------------------- */
  68. static void
  69. mc_skin_try_to_load_default (void)
  70. {
  71. mc_skin_reinit ();
  72. g_free (mc_skin__default.name);
  73. mc_skin__default.name = g_strdup ("default");
  74. if (!mc_skin_ini_file_load (&mc_skin__default))
  75. {
  76. mc_skin_reinit ();
  77. mc_skin_set_hardcoded_skin (&mc_skin__default);
  78. }
  79. }
  80. /* --------------------------------------------------------------------------------------------- */
  81. /*** public functions ****************************************************************************/
  82. /* --------------------------------------------------------------------------------------------- */
  83. gboolean
  84. mc_skin_init (GError ** error)
  85. {
  86. gboolean is_good_init = TRUE;
  87. mc_skin__default.have_256_colors = FALSE;
  88. mc_skin__default.name = mc_skin_get_default_name ();
  89. mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal,
  90. g_free, mc_skin_hash_destroy_value);
  91. if (!mc_skin_ini_file_load (&mc_skin__default))
  92. {
  93. *error = g_error_new (MC_ERROR, 0,
  94. _("Unable to load '%s' skin.\nDefault skin has been loaded"),
  95. mc_skin__default.name);
  96. mc_skin_try_to_load_default ();
  97. is_good_init = FALSE;
  98. }
  99. mc_skin_colors_old_configure (&mc_skin__default);
  100. if (!mc_skin_ini_file_parse (&mc_skin__default))
  101. {
  102. if (*error == NULL)
  103. *error = g_error_new (MC_ERROR, 0,
  104. _("Unable to parse '%s' skin.\nDefault skin has been loaded"),
  105. mc_skin__default.name);
  106. mc_skin_try_to_load_default ();
  107. mc_skin_colors_old_configure (&mc_skin__default);
  108. (void) mc_skin_ini_file_parse (&mc_skin__default);
  109. is_good_init = FALSE;
  110. }
  111. if (is_good_init && !tty_use_256colors () && mc_skin__default.have_256_colors)
  112. {
  113. if (*error == NULL)
  114. *error = g_error_new (MC_ERROR, 0,
  115. _
  116. ("Unable to use '%s' skin with 256 colors support\non non-256 colors terminal.\nDefault skin has been loaded"),
  117. mc_skin__default.name);
  118. mc_skin_try_to_load_default ();
  119. mc_skin_colors_old_configure (&mc_skin__default);
  120. (void) mc_skin_ini_file_parse (&mc_skin__default);
  121. is_good_init = FALSE;
  122. }
  123. mc_skin_is_init = TRUE;
  124. return is_good_init;
  125. }
  126. /* --------------------------------------------------------------------------------------------- */
  127. void
  128. mc_skin_deinit (void)
  129. {
  130. g_free (mc_skin__default.name);
  131. mc_skin__default.name = NULL;
  132. g_hash_table_destroy (mc_skin__default.colors);
  133. mc_skin__default.colors = NULL;
  134. g_free (mc_skin__default.description);
  135. mc_skin__default.description = NULL;
  136. mc_config_deinit (mc_skin__default.config);
  137. mc_skin__default.config = NULL;
  138. mc_skin_is_init = FALSE;
  139. }
  140. /* --------------------------------------------------------------------------------------------- */
  141. gchar *
  142. mc_skin_get (const gchar * group, const gchar * key, const gchar * default_value)
  143. {
  144. if (mc_global.tty.ugly_line_drawing)
  145. {
  146. return g_strdup (default_value);
  147. }
  148. return mc_config_get_string (mc_skin__default.config, group, key, default_value);
  149. }
  150. /* --------------------------------------------------------------------------------------------- */