ini-file.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. Skins engine.
  3. Reading and parse ini-files
  4. Copyright (C) 2009-2017
  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. /*** file scope variables ************************************************************************/
  30. /*** file scope functions ************************************************************************/
  31. /* --------------------------------------------------------------------------------------------- */
  32. static void
  33. mc_skin_get_list_from_dir (const gchar * base_dir, GPtrArray * list)
  34. {
  35. gchar *name;
  36. GDir *dir;
  37. name = g_build_filename (base_dir, MC_SKINS_SUBDIR, (char *) NULL);
  38. dir = g_dir_open (name, 0, NULL);
  39. g_free (name);
  40. if (dir != NULL)
  41. {
  42. const gchar *cname;
  43. while ((cname = g_dir_read_name (dir)) != NULL)
  44. {
  45. gchar *sname;
  46. size_t slen;
  47. unsigned int i;
  48. slen = strlen (cname);
  49. sname = g_strndup (cname, slen);
  50. if (slen > 4 && strcmp (sname + slen - 4, ".ini") == 0)
  51. sname[slen - 4] = '\0';
  52. for (i = 0; i < list->len; i++)
  53. if (strcmp (sname, g_ptr_array_index (list, i)) == 0)
  54. break;
  55. if (i < list->len)
  56. g_free (sname);
  57. else
  58. g_ptr_array_add (list, sname);
  59. }
  60. g_dir_close (dir);
  61. }
  62. }
  63. /* --------------------------------------------------------------------------------------------- */
  64. static int
  65. string_array_comparator (gconstpointer a, gconstpointer b)
  66. {
  67. return strcmp (*(char *const *) a, *(char *const *) b);
  68. }
  69. /* --------------------------------------------------------------------------------------------- */
  70. static gboolean
  71. mc_skin_ini_file_load_search_in_dir (mc_skin_t * mc_skin, const gchar * base_dir)
  72. {
  73. char *file_name, *file_name2;
  74. file_name = g_build_filename (base_dir, MC_SKINS_SUBDIR, mc_skin->name, (char *) NULL);
  75. if (exist_file (file_name))
  76. {
  77. mc_skin->config = mc_config_init (file_name, TRUE);
  78. g_free (file_name);
  79. return (mc_skin->config != NULL);
  80. }
  81. g_free (file_name);
  82. file_name2 = g_strdup_printf ("%s.ini", mc_skin->name);
  83. file_name = g_build_filename (base_dir, MC_SKINS_SUBDIR, file_name2, (char *) NULL);
  84. g_free (file_name2);
  85. if (exist_file (file_name))
  86. {
  87. mc_skin->config = mc_config_init (file_name, TRUE);
  88. g_free (file_name);
  89. return (mc_skin->config != NULL);
  90. }
  91. g_free (file_name);
  92. return FALSE;
  93. }
  94. /* --------------------------------------------------------------------------------------------- */
  95. /*** public functions ****************************************************************************/
  96. /* --------------------------------------------------------------------------------------------- */
  97. GPtrArray *
  98. mc_skin_list (void)
  99. {
  100. GPtrArray *list;
  101. list = g_ptr_array_new ();
  102. mc_skin_get_list_from_dir (mc_config_get_data_path (), list);
  103. mc_skin_get_list_from_dir (mc_global.sysconfig_dir, list);
  104. mc_skin_get_list_from_dir (mc_global.share_data_dir, list);
  105. g_ptr_array_sort (list, (GCompareFunc) string_array_comparator);
  106. return list;
  107. }
  108. /* --------------------------------------------------------------------------------------------- */
  109. gboolean
  110. mc_skin_ini_file_load (mc_skin_t * mc_skin)
  111. {
  112. char *file_name;
  113. file_name = g_path_get_basename (mc_skin->name);
  114. if (file_name == NULL)
  115. return FALSE;
  116. if (strcmp (file_name, mc_skin->name) != 0)
  117. {
  118. g_free (file_name);
  119. if (!g_path_is_absolute (mc_skin->name))
  120. return FALSE;
  121. mc_skin->config = mc_config_init (mc_skin->name, TRUE);
  122. return (mc_skin->config != NULL);
  123. }
  124. g_free (file_name);
  125. /* ${XDG_DATA_HOME}/mc/skins/ */
  126. if (mc_skin_ini_file_load_search_in_dir (mc_skin, mc_config_get_data_path ()))
  127. return TRUE;
  128. /* /etc/mc/skins/ */
  129. if (mc_skin_ini_file_load_search_in_dir (mc_skin, mc_global.sysconfig_dir))
  130. return TRUE;
  131. /* /usr/share/mc/skins/ */
  132. return mc_skin_ini_file_load_search_in_dir (mc_skin, mc_global.share_data_dir);
  133. }
  134. /* --------------------------------------------------------------------------------------------- */
  135. gboolean
  136. mc_skin_ini_file_parse (mc_skin_t * mc_skin)
  137. {
  138. mc_skin->description =
  139. mc_config_get_string (mc_skin->config, "skin", "description", "- no description -");
  140. if (!mc_skin_color_parse_ini_file (mc_skin))
  141. return FALSE;
  142. mc_skin_lines_parse_ini_file (mc_skin);
  143. mc_skin->have_256_colors = mc_config_get_bool (mc_skin->config, "skin", "256colors", FALSE);
  144. mc_skin->have_true_colors = mc_config_get_bool (mc_skin->config, "skin", "truecolors", FALSE);
  145. return TRUE;
  146. }
  147. /* --------------------------------------------------------------------------------------------- */
  148. void
  149. mc_skin_set_hardcoded_skin (mc_skin_t * mc_skin)
  150. {
  151. mc_skin->config = mc_config_init (NULL, TRUE);
  152. mc_config_set_string (mc_skin->config, "skin", "description", "hardcoded skin");
  153. mc_skin_hardcoded_ugly_lines (mc_skin);
  154. mc_skin_hardcoded_blackwhite_colors (mc_skin);
  155. }
  156. /* --------------------------------------------------------------------------------------------- */