ini-file.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #include "src/main.h"
  27. /*** global variables ****************************************************************************/
  28. /*** file scope macro definitions ****************************************************************/
  29. /*** file scope type declarations ****************************************************************/
  30. /*** file scope variables ************************************************************************/
  31. /*** file scope functions ************************************************************************/
  32. /* --------------------------------------------------------------------------------------------- */
  33. static gboolean
  34. mc_skin_ini_file_load_search_in_dir (mc_skin_t * mc_skin, const gchar * base_dir)
  35. {
  36. char *file_name, *file_name2;
  37. file_name = g_build_filename (base_dir, MC_SKINS_SUBDIR, mc_skin->name, NULL);
  38. if (exist_file (file_name))
  39. {
  40. mc_skin->config = mc_config_init (file_name);
  41. g_free (file_name);
  42. return (mc_skin->config != NULL);
  43. }
  44. g_free (file_name);
  45. file_name2 = g_strdup_printf ("%s.ini", mc_skin->name);
  46. file_name = g_build_filename (base_dir, MC_SKINS_SUBDIR, file_name2, NULL);
  47. g_free (file_name2);
  48. if (exist_file (file_name))
  49. {
  50. mc_skin->config = mc_config_init (file_name);
  51. g_free (file_name);
  52. return (mc_skin->config != NULL);
  53. }
  54. g_free (file_name);
  55. return FALSE;
  56. }
  57. /* --------------------------------------------------------------------------------------------- */
  58. /*** public functions ****************************************************************************/
  59. /* --------------------------------------------------------------------------------------------- */
  60. gboolean
  61. mc_skin_ini_file_load (mc_skin_t * mc_skin)
  62. {
  63. char *file_name, *user_home_dir;
  64. gboolean ok;
  65. file_name = g_path_get_basename (mc_skin->name);
  66. if (file_name == NULL)
  67. return FALSE;
  68. if (strcmp (file_name, mc_skin->name) != 0)
  69. {
  70. g_free (file_name);
  71. if (!g_path_is_absolute (mc_skin->name))
  72. return FALSE;
  73. mc_skin->config = mc_config_init (mc_skin->name);
  74. return (mc_skin->config != NULL);
  75. }
  76. g_free (file_name);
  77. /* ~/.mc/skins/ */
  78. user_home_dir = g_build_filename (home_dir, MC_USERCONF_DIR, (char *) NULL);
  79. ok = mc_skin_ini_file_load_search_in_dir (mc_skin, user_home_dir);
  80. g_free (user_home_dir);
  81. if (ok)
  82. return TRUE;
  83. /* /etc/mc/skins/ */
  84. if (mc_skin_ini_file_load_search_in_dir (mc_skin, mc_home))
  85. return TRUE;
  86. /* /usr/share/mc/skins/ */
  87. return mc_skin_ini_file_load_search_in_dir (mc_skin, mc_home_alt);
  88. }
  89. /* --------------------------------------------------------------------------------------------- */
  90. gboolean
  91. mc_skin_ini_file_parse (mc_skin_t * mc_skin)
  92. {
  93. mc_skin->description =
  94. mc_config_get_string (mc_skin->config, "skin", "description", "- no description -");
  95. if (!mc_skin_color_parse_ini_file (mc_skin))
  96. return FALSE;
  97. mc_skin_lines_parse_ini_file (mc_skin);
  98. return TRUE;
  99. }
  100. /* --------------------------------------------------------------------------------------------- */
  101. void
  102. mc_skin_set_hardcoded_skin (mc_skin_t * mc_skin)
  103. {
  104. mc_skin->config = mc_config_init (NULL);
  105. mc_config_set_string (mc_skin->config, "skin", "description", "hardcoded skin");
  106. mc_skin_hardcoded_ugly_lines (mc_skin);
  107. mc_skin_hardcoded_blackwhite_colors (mc_skin);
  108. }
  109. /* --------------------------------------------------------------------------------------------- */