set.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. Configure module for the Midnight Commander
  3. Copyright (C) 1994-2024
  4. Free Software Foundation, Inc.
  5. This file is part of the Midnight Commander.
  6. The Midnight Commander is free software: you can redistribute it
  7. and/or modify it under the terms of the GNU General Public License as
  8. published by the Free Software Foundation, either version 3 of the License,
  9. or (at your option) any later version.
  10. The Midnight Commander is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <config.h>
  18. #include "lib/global.h"
  19. #include "lib/strutil.h"
  20. #include "lib/mcconfig.h"
  21. /*** global variables ****************************************************************************/
  22. /*** file scope macro definitions ****************************************************************/
  23. /*** file scope type declarations ****************************************************************/
  24. /*** forward declarations (file scope functions) *************************************************/
  25. /*** file scope variables ************************************************************************/
  26. /* --------------------------------------------------------------------------------------------- */
  27. /*** file scope functions ************************************************************************/
  28. /* --------------------------------------------------------------------------------------------- */
  29. static gchar *
  30. mc_config_normalize_before_save (const gchar *value)
  31. {
  32. GIConv conv;
  33. GString *buffer;
  34. gboolean ok;
  35. if (mc_global.utf8_display)
  36. return g_strdup (value);
  37. conv = str_crt_conv_to ("UTF-8");
  38. if (conv == INVALID_CONV)
  39. return g_strdup (value);
  40. buffer = g_string_new ("");
  41. ok = (str_convert (conv, value, buffer) != ESTR_FAILURE);
  42. str_close_conv (conv);
  43. if (!ok)
  44. {
  45. g_string_free (buffer, TRUE);
  46. return g_strdup (value);
  47. }
  48. return g_string_free (buffer, FALSE);
  49. }
  50. /* --------------------------------------------------------------------------------------------- */
  51. /*** public functions ****************************************************************************/
  52. /* --------------------------------------------------------------------------------------------- */
  53. void
  54. mc_config_set_string_raw (mc_config_t *mc_config, const gchar *group,
  55. const gchar *param, const gchar *value)
  56. {
  57. if (mc_config != NULL && group != NULL && param != NULL && value != NULL)
  58. g_key_file_set_string (mc_config->handle, group, param, value);
  59. }
  60. /* --------------------------------------------------------------------------------------------- */
  61. void
  62. mc_config_set_string_raw_value (mc_config_t *mc_config, const gchar *group,
  63. const gchar *param, const gchar *value)
  64. {
  65. if (mc_config != NULL && group != NULL && param != NULL && value != NULL)
  66. g_key_file_set_value (mc_config->handle, group, param, value);
  67. }
  68. /* --------------------------------------------------------------------------------------------- */
  69. void
  70. mc_config_set_string (mc_config_t *mc_config, const gchar *group,
  71. const gchar *param, const gchar *value)
  72. {
  73. if (mc_config != NULL && group != NULL && param != NULL && value != NULL)
  74. {
  75. gchar *buffer;
  76. buffer = mc_config_normalize_before_save (value);
  77. g_key_file_set_string (mc_config->handle, group, param, buffer);
  78. g_free (buffer);
  79. }
  80. }
  81. /* --------------------------------------------------------------------------------------------- */
  82. void
  83. mc_config_set_bool (mc_config_t *mc_config, const gchar *group, const gchar *param, gboolean value)
  84. {
  85. if (mc_config != NULL && group != NULL && param != NULL)
  86. g_key_file_set_boolean (mc_config->handle, group, param, value);
  87. }
  88. /* --------------------------------------------------------------------------------------------- */
  89. void
  90. mc_config_set_int (mc_config_t *mc_config, const gchar *group, const gchar *param, int value)
  91. {
  92. if (mc_config != NULL && group != NULL && param != NULL)
  93. g_key_file_set_integer (mc_config->handle, group, param, value);
  94. }
  95. /* --------------------------------------------------------------------------------------------- */
  96. void
  97. mc_config_set_string_list (mc_config_t *mc_config, const gchar *group,
  98. const gchar *param, const gchar *const value[], gsize length)
  99. {
  100. if (mc_config != NULL && group != NULL && param != NULL && value != NULL && length != 0)
  101. g_key_file_set_string_list (mc_config->handle, group, param, value, length);
  102. }
  103. /* --------------------------------------------------------------------------------------------- */
  104. void
  105. mc_config_set_bool_list (mc_config_t *mc_config, const gchar *group,
  106. const gchar *param, gboolean value[], gsize length)
  107. {
  108. if (mc_config != NULL && group != NULL && param != NULL && value != NULL && length != 0)
  109. g_key_file_set_boolean_list (mc_config->handle, group, param, value, length);
  110. }
  111. /* --------------------------------------------------------------------------------------------- */
  112. void
  113. mc_config_set_int_list (mc_config_t *mc_config, const gchar *group,
  114. const gchar *param, int value[], gsize length)
  115. {
  116. if (mc_config != NULL && group != NULL && param != NULL && value != NULL && length != 0)
  117. g_key_file_set_integer_list (mc_config->handle, group, param, value, length);
  118. }
  119. /* --------------------------------------------------------------------------------------------- */