common.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. Skins engine.
  3. Interface functions
  4. Copyright (C) 2009, 2010 The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2009
  7. Egmont Koblinger <egmont@gmail.com>, 2010
  8. This file is part of the Midnight Commander.
  9. The Midnight Commander is free software; you can redistribute it
  10. and/or modify it under the terms of the GNU General Public License as
  11. published by the Free Software Foundation; either version 2 of the
  12. License, or (at your option) any later version.
  13. The Midnight Commander is distributed in the hope that it will be
  14. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  15. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20. MA 02110-1301, USA.
  21. */
  22. #include <config.h>
  23. #include <stdlib.h>
  24. #include "internal.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.skin != NULL)
  51. return g_strdup (mc_global.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_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 (GError ** error)
  86. {
  87. gboolean is_good_init = TRUE;
  88. mc_skin__default.have_256_colors = FALSE;
  89. mc_skin__default.name = mc_skin_get_default_name ();
  90. mc_skin__default.colors = g_hash_table_new_full (g_str_hash, g_str_equal,
  91. g_free, mc_skin_hash_destroy_value);
  92. if (!mc_skin_ini_file_load (&mc_skin__default))
  93. {
  94. *error = g_error_new (MC_ERROR, 0,
  95. _("Unable to load '%s' skin.\nDefault skin has been loaded"),
  96. mc_skin__default.name);
  97. mc_skin_try_to_load_default ();
  98. is_good_init = FALSE;
  99. }
  100. mc_skin_colors_old_configure (&mc_skin__default);
  101. if (!mc_skin_ini_file_parse (&mc_skin__default))
  102. {
  103. if (*error == NULL)
  104. *error = g_error_new (MC_ERROR, 0,
  105. _("Unable to parse '%s' skin.\nDefault skin has been loaded"),
  106. mc_skin__default.name);
  107. mc_skin_try_to_load_default ();
  108. mc_skin_colors_old_configure (&mc_skin__default);
  109. (void) mc_skin_ini_file_parse (&mc_skin__default);
  110. is_good_init = FALSE;
  111. }
  112. if (is_good_init && !tty_use_256colors () && mc_skin__default.have_256_colors)
  113. {
  114. if (*error == NULL)
  115. *error = g_error_new (MC_ERROR, 0,
  116. _
  117. ("Unable to use '%s' skin with 256 colors support\non non-256 colors terminal.\nDefault skin has been loaded"),
  118. mc_skin__default.name);
  119. mc_skin_try_to_load_default ();
  120. mc_skin_colors_old_configure (&mc_skin__default);
  121. (void) mc_skin_ini_file_parse (&mc_skin__default);
  122. is_good_init = FALSE;
  123. }
  124. mc_skin_is_init = TRUE;
  125. return is_good_init;
  126. }
  127. /* --------------------------------------------------------------------------------------------- */
  128. void
  129. mc_skin_deinit (void)
  130. {
  131. g_free (mc_skin__default.name);
  132. mc_skin__default.name = NULL;
  133. g_hash_table_destroy (mc_skin__default.colors);
  134. mc_skin__default.colors = NULL;
  135. g_free (mc_skin__default.description);
  136. mc_skin__default.description = NULL;
  137. mc_config_deinit (mc_skin__default.config);
  138. mc_skin__default.config = NULL;
  139. mc_skin_is_init = FALSE;
  140. }
  141. /* --------------------------------------------------------------------------------------------- */
  142. gchar *
  143. mc_skin_get (const gchar * group, const gchar * key, const gchar * default_value)
  144. {
  145. if (mc_global.tty.ugly_line_drawing)
  146. {
  147. return g_strdup (default_value);
  148. }
  149. return mc_config_get_string (mc_skin__default.config, group, key, default_value);
  150. }
  151. /* --------------------------------------------------------------------------------------------- */