common.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. Skins engine.
  3. Interface functions
  4. Copyright (C) 2009-2017
  5. 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/util.h"
  25. #include "lib/tty/color.h" /* tty_use_256colors(); */
  26. /*** global variables ****************************************************************************/
  27. mc_skin_t mc_skin__default;
  28. /*** file scope macro definitions ****************************************************************/
  29. /*** file scope type declarations ****************************************************************/
  30. /*** file scope variables ************************************************************************/
  31. static gboolean mc_skin_is_init = FALSE;
  32. /* --------------------------------------------------------------------------------------------- */
  33. /*** file scope functions ************************************************************************/
  34. /* --------------------------------------------------------------------------------------------- */
  35. static void
  36. mc_skin_hash_destroy_value (gpointer data)
  37. {
  38. mc_skin_color_t *mc_skin_color = (mc_skin_color_t *) data;
  39. g_free (mc_skin_color->fgcolor);
  40. g_free (mc_skin_color->bgcolor);
  41. g_free (mc_skin_color->attrs);
  42. g_free (mc_skin_color);
  43. }
  44. /* --------------------------------------------------------------------------------------------- */
  45. static char *
  46. mc_skin_get_default_name (void)
  47. {
  48. char *tmp_str;
  49. /* from command line */
  50. if (mc_global.tty.skin != NULL)
  51. return g_strdup (mc_global.tty.skin);
  52. /* from envirovement variable */
  53. tmp_str = getenv ("MC_SKIN");
  54. if (tmp_str != NULL)
  55. return g_strdup (tmp_str);
  56. /* from config. Or 'default' if no present in config */
  57. return mc_config_get_string (mc_global.main_config, CONFIG_APP_SECTION, "skin", "default");
  58. }
  59. /* --------------------------------------------------------------------------------------------- */
  60. static void
  61. mc_skin_reinit (void)
  62. {
  63. mc_skin_deinit ();
  64. mc_skin__default.name = mc_skin_get_default_name ();
  65. mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal,
  66. g_free, mc_skin_hash_destroy_value);
  67. }
  68. /* --------------------------------------------------------------------------------------------- */
  69. static void
  70. mc_skin_try_to_load_default (void)
  71. {
  72. mc_skin_reinit ();
  73. g_free (mc_skin__default.name);
  74. mc_skin__default.name = g_strdup ("default");
  75. if (!mc_skin_ini_file_load (&mc_skin__default))
  76. {
  77. mc_skin_reinit ();
  78. mc_skin_set_hardcoded_skin (&mc_skin__default);
  79. }
  80. }
  81. /* --------------------------------------------------------------------------------------------- */
  82. /*** public functions ****************************************************************************/
  83. /* --------------------------------------------------------------------------------------------- */
  84. gboolean
  85. mc_skin_init (const gchar * skin_override, GError ** mcerror)
  86. {
  87. gboolean is_good_init = TRUE;
  88. GError *error = NULL;
  89. mc_return_val_if_error (mcerror, FALSE);
  90. mc_skin__default.have_256_colors = FALSE;
  91. mc_skin__default.have_true_colors = FALSE;
  92. mc_skin__default.name =
  93. skin_override != NULL ? g_strdup (skin_override) : mc_skin_get_default_name ();
  94. mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal,
  95. g_free, mc_skin_hash_destroy_value);
  96. if (!mc_skin_ini_file_load (&mc_skin__default))
  97. {
  98. mc_propagate_error (mcerror, 0,
  99. _("Unable to load '%s' skin.\nDefault skin has been loaded"),
  100. mc_skin__default.name);
  101. mc_skin_try_to_load_default ();
  102. is_good_init = FALSE;
  103. }
  104. mc_skin_colors_old_configure (&mc_skin__default);
  105. if (!mc_skin_ini_file_parse (&mc_skin__default))
  106. {
  107. mc_propagate_error (mcerror, 0,
  108. _("Unable to parse '%s' skin.\nDefault skin has been loaded"),
  109. mc_skin__default.name);
  110. mc_skin_try_to_load_default ();
  111. mc_skin_colors_old_configure (&mc_skin__default);
  112. (void) mc_skin_ini_file_parse (&mc_skin__default);
  113. is_good_init = FALSE;
  114. }
  115. if (is_good_init && !tty_use_truecolors (&error) && mc_skin__default.have_true_colors)
  116. {
  117. mc_propagate_error (mcerror, 0,
  118. _
  119. ("Unable to use '%s' skin with true colors support:\n%s\nDefault skin has been loaded"),
  120. mc_skin__default.name, error->message);
  121. g_error_free (error);
  122. mc_skin_try_to_load_default ();
  123. mc_skin_colors_old_configure (&mc_skin__default);
  124. (void) mc_skin_ini_file_parse (&mc_skin__default);
  125. is_good_init = FALSE;
  126. }
  127. if (is_good_init && !tty_use_256colors () && mc_skin__default.have_256_colors)
  128. {
  129. mc_propagate_error (mcerror, 0,
  130. _
  131. ("Unable to use '%s' skin with 256 colors support\non non-256 colors terminal.\nDefault skin has been loaded"),
  132. mc_skin__default.name);
  133. mc_skin_try_to_load_default ();
  134. mc_skin_colors_old_configure (&mc_skin__default);
  135. (void) mc_skin_ini_file_parse (&mc_skin__default);
  136. is_good_init = FALSE;
  137. }
  138. mc_skin_is_init = TRUE;
  139. return is_good_init;
  140. }
  141. /* --------------------------------------------------------------------------------------------- */
  142. void
  143. mc_skin_deinit (void)
  144. {
  145. tty_color_free_all_tmp ();
  146. tty_color_free_all_non_tmp ();
  147. MC_PTR_FREE (mc_skin__default.name);
  148. g_hash_table_destroy (mc_skin__default.colors);
  149. mc_skin__default.colors = NULL;
  150. MC_PTR_FREE (mc_skin__default.description);
  151. mc_config_deinit (mc_skin__default.config);
  152. mc_skin__default.config = NULL;
  153. mc_skin_is_init = FALSE;
  154. }
  155. /* --------------------------------------------------------------------------------------------- */
  156. gchar *
  157. mc_skin_get (const gchar * group, const gchar * key, const gchar * default_value)
  158. {
  159. if (mc_global.tty.ugly_line_drawing)
  160. {
  161. return g_strdup (default_value);
  162. }
  163. return mc_config_get_string (mc_skin__default.config, group, key, default_value);
  164. }
  165. /* --------------------------------------------------------------------------------------------- */