ini-file.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 (file_name == NULL)
  63. return FALSE;
  64. if (strcmp (file_name, mc_skin->name) != 0) {
  65. g_free (file_name);
  66. if (!g_path_is_absolute (mc_skin->name))
  67. return FALSE;
  68. mc_skin->config = mc_config_init (mc_skin->name);
  69. return (mc_skin->config != NULL);
  70. }
  71. g_free (file_name);
  72. /* ~/.mc/skins/ */
  73. user_home_dir = concat_dir_and_file (home_dir, MC_USERCONF_DIR);
  74. if (mc_skin_ini_file_load_search_in_dir (mc_skin, user_home_dir)) {
  75. g_free (user_home_dir);
  76. return TRUE;
  77. }
  78. g_free (user_home_dir);
  79. /* /etc/mc/skins/ */
  80. if (mc_skin_ini_file_load_search_in_dir (mc_skin, mc_home))
  81. return TRUE;
  82. /* /usr/share/mc/skins/ */
  83. return mc_skin_ini_file_load_search_in_dir (mc_skin, mc_home_alt);
  84. }
  85. /* --------------------------------------------------------------------------------------------- */
  86. gboolean
  87. mc_skin_ini_file_parse (mc_skin_t * mc_skin)
  88. {
  89. mc_skin->description =
  90. mc_config_get_string (mc_skin->config, "skin", "description", "- no description -");
  91. if (!mc_skin_color_parse_ini_file (mc_skin))
  92. return FALSE;
  93. mc_skin_lines_parse_ini_file (mc_skin);
  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. /* --------------------------------------------------------------------------------------------- */