ini-file.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. Skins engine.
  3. Reading and parse ini-files
  4. Copyright (C) 2009 The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2009.
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software; you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation; either version 2 of the
  11. License, or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be
  13. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  14. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. MA 02110-1301, USA.
  20. */
  21. #include <config.h>
  22. #include <string.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 gboolean
  33. mc_skin_ini_file_load_search_in_dir (mc_skin_t * mc_skin, const gchar * base_dir)
  34. {
  35. char *file_name, *file_name2;
  36. file_name = g_build_filename (base_dir, MC_SKINS_SUBDIR, mc_skin->name, NULL);
  37. if (exist_file (file_name))
  38. {
  39. mc_skin->config = mc_config_init (file_name);
  40. g_free (file_name);
  41. return (mc_skin->config != NULL);
  42. }
  43. g_free (file_name);
  44. file_name2 = g_strdup_printf ("%s.ini", mc_skin->name);
  45. file_name = g_build_filename (base_dir, MC_SKINS_SUBDIR, file_name2, NULL);
  46. g_free (file_name2);
  47. if (exist_file (file_name))
  48. {
  49. mc_skin->config = mc_config_init (file_name);
  50. g_free (file_name);
  51. return (mc_skin->config != NULL);
  52. }
  53. g_free (file_name);
  54. return FALSE;
  55. }
  56. /* --------------------------------------------------------------------------------------------- */
  57. /*** public functions ****************************************************************************/
  58. /* --------------------------------------------------------------------------------------------- */
  59. gboolean
  60. mc_skin_ini_file_load (mc_skin_t * mc_skin)
  61. {
  62. char *file_name;
  63. file_name = g_path_get_basename (mc_skin->name);
  64. if (file_name == NULL)
  65. return FALSE;
  66. if (strcmp (file_name, mc_skin->name) != 0)
  67. {
  68. g_free (file_name);
  69. if (!g_path_is_absolute (mc_skin->name))
  70. return FALSE;
  71. mc_skin->config = mc_config_init (mc_skin->name);
  72. return (mc_skin->config != NULL);
  73. }
  74. g_free (file_name);
  75. /* ${XDG_DATA_HOME}/mc/skins/ */
  76. if (mc_skin_ini_file_load_search_in_dir (mc_skin, mc_config_get_data_path ()))
  77. return TRUE;
  78. /* /etc/mc/skins/ */
  79. if (mc_skin_ini_file_load_search_in_dir (mc_skin, mc_global.sysconfig_dir))
  80. return TRUE;
  81. /* /usr/share/mc/skins/ */
  82. return mc_skin_ini_file_load_search_in_dir (mc_skin, mc_global.share_data_dir);
  83. }
  84. /* --------------------------------------------------------------------------------------------- */
  85. gboolean
  86. mc_skin_ini_file_parse (mc_skin_t * mc_skin)
  87. {
  88. mc_skin->description =
  89. mc_config_get_string (mc_skin->config, "skin", "description", "- no description -");
  90. if (!mc_skin_color_parse_ini_file (mc_skin))
  91. return FALSE;
  92. mc_skin_lines_parse_ini_file (mc_skin);
  93. mc_skin->have_256_colors = mc_config_get_bool (mc_skin->config, "skin", "256colors", FALSE);
  94. return TRUE;
  95. }
  96. /* --------------------------------------------------------------------------------------------- */
  97. void
  98. mc_skin_set_hardcoded_skin (mc_skin_t * mc_skin)
  99. {
  100. mc_skin->config = mc_config_init (NULL);
  101. mc_config_set_string (mc_skin->config, "skin", "description", "hardcoded skin");
  102. mc_skin_hardcoded_ugly_lines (mc_skin);
  103. mc_skin_hardcoded_blackwhite_colors (mc_skin);
  104. }
  105. /* --------------------------------------------------------------------------------------------- */