ini-file.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. Skins engine.
  3. Reading and parse ini-files
  4. Copyright (C) 2009-2024
  5. Free Software Foundation, Inc.
  6. Written by:
  7. Slava Zanko <slavazanko@gmail.com>, 2009.
  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 3 of the License,
  12. or (at your option) any later version.
  13. The Midnight Commander is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU 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, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <string.h>
  22. #include "lib/global.h" /* <glib.h> */
  23. #include "internal.h"
  24. #include "lib/fileloc.h"
  25. #include "lib/util.h" /* exist_file() */
  26. /*** global variables ****************************************************************************/
  27. /*** file scope macro definitions ****************************************************************/
  28. /*** file scope type declarations ****************************************************************/
  29. /*** forward declarations (file scope functions) *************************************************/
  30. /*** file scope variables ************************************************************************/
  31. /* --------------------------------------------------------------------------------------------- */
  32. /*** file scope functions ************************************************************************/
  33. /* --------------------------------------------------------------------------------------------- */
  34. static void
  35. mc_skin_get_list_from_dir (const gchar *base_dir, GPtrArray *list)
  36. {
  37. gchar *name;
  38. GDir *dir;
  39. name = g_build_filename (base_dir, MC_SKINS_DIR, (char *) NULL);
  40. dir = g_dir_open (name, 0, NULL);
  41. g_free (name);
  42. if (dir != NULL)
  43. {
  44. const gchar *cname;
  45. while ((cname = g_dir_read_name (dir)) != NULL)
  46. {
  47. gchar *sname;
  48. size_t slen;
  49. unsigned int i;
  50. slen = strlen (cname);
  51. sname = g_strndup (cname, slen);
  52. if (slen > 4 && strcmp (sname + slen - 4, ".ini") == 0)
  53. sname[slen - 4] = '\0';
  54. for (i = 0; i < list->len; i++)
  55. if (strcmp (sname, g_ptr_array_index (list, i)) == 0)
  56. break;
  57. if (i < list->len)
  58. g_free (sname);
  59. else
  60. g_ptr_array_add (list, sname);
  61. }
  62. g_dir_close (dir);
  63. }
  64. }
  65. /* --------------------------------------------------------------------------------------------- */
  66. static int
  67. string_array_comparator (gconstpointer a, gconstpointer b)
  68. {
  69. return strcmp (*(char *const *) a, *(char *const *) b);
  70. }
  71. /* --------------------------------------------------------------------------------------------- */
  72. static gboolean
  73. mc_skin_ini_file_load_search_in_dir (mc_skin_t *mc_skin, const gchar *base_dir)
  74. {
  75. char *file_name, *file_name2;
  76. file_name = g_build_filename (base_dir, MC_SKINS_DIR, mc_skin->name, (char *) NULL);
  77. if (exist_file (file_name))
  78. {
  79. mc_skin->config = mc_config_init (file_name, TRUE);
  80. g_free (file_name);
  81. return (mc_skin->config != NULL);
  82. }
  83. g_free (file_name);
  84. file_name2 = g_strdup_printf ("%s.ini", mc_skin->name);
  85. file_name = g_build_filename (base_dir, MC_SKINS_DIR, file_name2, (char *) NULL);
  86. g_free (file_name2);
  87. if (exist_file (file_name))
  88. {
  89. mc_skin->config = mc_config_init (file_name, TRUE);
  90. g_free (file_name);
  91. return (mc_skin->config != NULL);
  92. }
  93. g_free (file_name);
  94. return FALSE;
  95. }
  96. /* --------------------------------------------------------------------------------------------- */
  97. /*** public functions ****************************************************************************/
  98. /* --------------------------------------------------------------------------------------------- */
  99. GPtrArray *
  100. mc_skin_list (void)
  101. {
  102. GPtrArray *list;
  103. list = g_ptr_array_new_with_free_func (g_free);
  104. mc_skin_get_list_from_dir (mc_config_get_data_path (), list);
  105. mc_skin_get_list_from_dir (mc_global.sysconfig_dir, list);
  106. mc_skin_get_list_from_dir (mc_global.share_data_dir, list);
  107. g_ptr_array_sort (list, (GCompareFunc) string_array_comparator);
  108. return list;
  109. }
  110. /* --------------------------------------------------------------------------------------------- */
  111. gboolean
  112. mc_skin_ini_file_load (mc_skin_t *mc_skin)
  113. {
  114. char *file_name;
  115. file_name = g_path_get_basename (mc_skin->name);
  116. if (file_name == NULL)
  117. return FALSE;
  118. if (strcmp (file_name, mc_skin->name) != 0)
  119. {
  120. g_free (file_name);
  121. if (!g_path_is_absolute (mc_skin->name))
  122. return FALSE;
  123. mc_skin->config = mc_config_init (mc_skin->name, TRUE);
  124. return (mc_skin->config != NULL);
  125. }
  126. g_free (file_name);
  127. /* ${XDG_DATA_HOME}/mc/skins/ */
  128. if (mc_skin_ini_file_load_search_in_dir (mc_skin, mc_config_get_data_path ()))
  129. return TRUE;
  130. /* /etc/mc/skins/ */
  131. if (mc_skin_ini_file_load_search_in_dir (mc_skin, mc_global.sysconfig_dir))
  132. return TRUE;
  133. /* /usr/share/mc/skins/ */
  134. return mc_skin_ini_file_load_search_in_dir (mc_skin, mc_global.share_data_dir);
  135. }
  136. /* --------------------------------------------------------------------------------------------- */
  137. gboolean
  138. mc_skin_ini_file_parse (mc_skin_t *mc_skin)
  139. {
  140. mc_skin->description = mc_config_get_string (mc_skin->config, "skin", "description",
  141. "- no description -");
  142. if (!mc_skin_color_parse_ini_file (mc_skin))
  143. return FALSE;
  144. mc_skin_lines_parse_ini_file (mc_skin);
  145. mc_skin->have_256_colors = mc_config_get_bool (mc_skin->config, "skin", "256colors", FALSE);
  146. mc_skin->have_true_colors = mc_config_get_bool (mc_skin->config, "skin", "truecolors", FALSE);
  147. return TRUE;
  148. }
  149. /* --------------------------------------------------------------------------------------------- */
  150. void
  151. mc_skin_set_hardcoded_skin (mc_skin_t *mc_skin)
  152. {
  153. mc_skin->config = mc_config_init (NULL, TRUE);
  154. mc_config_set_string (mc_skin->config, "skin", "description", "hardcoded skin");
  155. mc_skin_hardcoded_ugly_lines (mc_skin);
  156. mc_skin_hardcoded_blackwhite_colors (mc_skin);
  157. }
  158. /* --------------------------------------------------------------------------------------------- */