ini-file.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. Skins engine.
  3. Reading and parse ini-files
  4. Copyright (C) 2009, 2011
  5. The 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 "internal.h"
  23. #include "lib/fileloc.h"
  24. #include "lib/util.h" /* exist_file() */
  25. /*** global variables ****************************************************************************/
  26. /*** file scope macro definitions ****************************************************************/
  27. /*** file scope type declarations ****************************************************************/
  28. /*** file scope variables ************************************************************************/
  29. /*** file scope functions ************************************************************************/
  30. /* --------------------------------------------------------------------------------------------- */
  31. static gboolean
  32. mc_skin_ini_file_load_search_in_dir (mc_skin_t * mc_skin, const gchar * base_dir)
  33. {
  34. char *file_name, *file_name2;
  35. file_name = g_build_filename (base_dir, MC_SKINS_SUBDIR, mc_skin->name, NULL);
  36. if (exist_file (file_name))
  37. {
  38. mc_skin->config = mc_config_init (file_name, TRUE);
  39. g_free (file_name);
  40. return (mc_skin->config != NULL);
  41. }
  42. g_free (file_name);
  43. file_name2 = g_strdup_printf ("%s.ini", mc_skin->name);
  44. file_name = g_build_filename (base_dir, MC_SKINS_SUBDIR, file_name2, NULL);
  45. g_free (file_name2);
  46. if (exist_file (file_name))
  47. {
  48. mc_skin->config = mc_config_init (file_name, TRUE);
  49. g_free (file_name);
  50. return (mc_skin->config != NULL);
  51. }
  52. g_free (file_name);
  53. return FALSE;
  54. }
  55. /* --------------------------------------------------------------------------------------------- */
  56. /*** public functions ****************************************************************************/
  57. /* --------------------------------------------------------------------------------------------- */
  58. gboolean
  59. mc_skin_ini_file_load (mc_skin_t * mc_skin)
  60. {
  61. char *file_name;
  62. file_name = g_path_get_basename (mc_skin->name);
  63. if (file_name == NULL)
  64. return FALSE;
  65. if (strcmp (file_name, mc_skin->name) != 0)
  66. {
  67. g_free (file_name);
  68. if (!g_path_is_absolute (mc_skin->name))
  69. return FALSE;
  70. mc_skin->config = mc_config_init (mc_skin->name, TRUE);
  71. return (mc_skin->config != NULL);
  72. }
  73. g_free (file_name);
  74. /* ${XDG_DATA_HOME}/mc/skins/ */
  75. if (mc_skin_ini_file_load_search_in_dir (mc_skin, mc_config_get_data_path ()))
  76. return TRUE;
  77. /* /etc/mc/skins/ */
  78. if (mc_skin_ini_file_load_search_in_dir (mc_skin, mc_global.sysconfig_dir))
  79. return TRUE;
  80. /* /usr/share/mc/skins/ */
  81. return mc_skin_ini_file_load_search_in_dir (mc_skin, mc_global.share_data_dir);
  82. }
  83. /* --------------------------------------------------------------------------------------------- */
  84. gboolean
  85. mc_skin_ini_file_parse (mc_skin_t * mc_skin)
  86. {
  87. mc_skin->description =
  88. mc_config_get_string (mc_skin->config, "skin", "description", "- no description -");
  89. if (!mc_skin_color_parse_ini_file (mc_skin))
  90. return FALSE;
  91. mc_skin_lines_parse_ini_file (mc_skin);
  92. mc_skin->have_256_colors = mc_config_get_bool (mc_skin->config, "skin", "256colors", FALSE);
  93. return TRUE;
  94. }
  95. /* --------------------------------------------------------------------------------------------- */
  96. void
  97. mc_skin_set_hardcoded_skin (mc_skin_t * mc_skin)
  98. {
  99. mc_skin->config = mc_config_init (NULL, TRUE);
  100. mc_config_set_string (mc_skin->config, "skin", "description", "hardcoded skin");
  101. mc_skin_hardcoded_ugly_lines (mc_skin);
  102. mc_skin_hardcoded_blackwhite_colors (mc_skin);
  103. }
  104. /* --------------------------------------------------------------------------------------------- */