ini-file.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "src/main.h"
  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. mc_skin->config = mc_config_init (file_name);
  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. mc_skin->config = mc_config_init (file_name);
  48. g_free (file_name);
  49. return (mc_skin->config != NULL);
  50. }
  51. g_free (file_name);
  52. return FALSE;
  53. }
  54. /* --------------------------------------------------------------------------------------------- */
  55. /*** public functions ****************************************************************************/
  56. /* --------------------------------------------------------------------------------------------- */
  57. gboolean
  58. mc_skin_ini_file_load (mc_skin_t * mc_skin)
  59. {
  60. char *file_name, *user_home_dir;
  61. file_name = g_path_get_basename (mc_skin->name);
  62. if (strcmp (file_name, mc_skin->name) != 0) {
  63. g_free (file_name);
  64. if (!g_path_is_absolute (mc_skin->name))
  65. return FALSE;
  66. mc_skin->config = mc_config_init (mc_skin->name);
  67. return (mc_skin->config != NULL);
  68. }
  69. g_free (file_name);
  70. /* ~/.mc/skins/ */
  71. user_home_dir = concat_dir_and_file (home_dir, MC_USERCONF_DIR);
  72. if (mc_skin_ini_file_load_search_in_dir (mc_skin, user_home_dir)) {
  73. g_free (user_home_dir);
  74. return TRUE;
  75. }
  76. g_free (user_home_dir);
  77. /* /etc/mc/skins/ */
  78. if (mc_skin_ini_file_load_search_in_dir (mc_skin, mc_home))
  79. return TRUE;
  80. /* /usr/share/mc/skins/ */
  81. return mc_skin_ini_file_load_search_in_dir (mc_skin, mc_home_alt);
  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. return TRUE;
  93. }
  94. /* --------------------------------------------------------------------------------------------- */
  95. void
  96. mc_skin_set_hardcoded_skin (mc_skin_t * mc_skin)
  97. {
  98. mc_skin->config = mc_config_init (NULL);
  99. mc_config_set_string (mc_skin->config, "skin", "description", "hardcoded skin");
  100. mc_skin_hardcoded_ugly_lines (mc_skin);
  101. mc_skin_hardcoded_blackwhite_colors (mc_skin);
  102. }
  103. /* --------------------------------------------------------------------------------------------- */