common.c 6.5 KB

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