set.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. Configure module for the Midnight Commander
  3. Copyright (C) 1994-2019
  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. /*** file scope variables ************************************************************************/
  25. /* --------------------------------------------------------------------------------------------- */
  26. /*** file scope functions ************************************************************************/
  27. /* --------------------------------------------------------------------------------------------- */
  28. static gchar *
  29. mc_config_normalize_before_save (const gchar * value)
  30. {
  31. GIConv conv;
  32. GString *buffer;
  33. gboolean ok;
  34. if (mc_global.utf8_display)
  35. return g_strdup (value);
  36. conv = str_crt_conv_to ("UTF-8");
  37. if (conv == INVALID_CONV)
  38. return g_strdup (value);
  39. buffer = g_string_new ("");
  40. ok = (str_convert (conv, value, buffer) != ESTR_FAILURE);
  41. str_close_conv (conv);
  42. if (!ok)
  43. {
  44. g_string_free (buffer, TRUE);
  45. return g_strdup (value);
  46. }
  47. return g_string_free (buffer, FALSE);
  48. }
  49. /* --------------------------------------------------------------------------------------------- */
  50. /*** public functions ****************************************************************************/
  51. /* --------------------------------------------------------------------------------------------- */
  52. void
  53. mc_config_set_string_raw (mc_config_t * mc_config, const gchar * group,
  54. const gchar * param, const gchar * value)
  55. {
  56. if (mc_config != NULL && group != NULL && param != NULL && value != NULL)
  57. g_key_file_set_string (mc_config->handle, group, param, value);
  58. }
  59. /* --------------------------------------------------------------------------------------------- */
  60. void
  61. mc_config_set_string_raw_value (mc_config_t * mc_config, const gchar * group,
  62. const gchar * param, const gchar * value)
  63. {
  64. if (mc_config != NULL && group != NULL && param != NULL && value != NULL)
  65. g_key_file_set_value (mc_config->handle, group, param, value);
  66. }
  67. /* --------------------------------------------------------------------------------------------- */
  68. void
  69. mc_config_set_string (mc_config_t * mc_config, const gchar * group,
  70. const gchar * param, const gchar * value)
  71. {
  72. if (mc_config != NULL && group != NULL && param != NULL && value != NULL)
  73. {
  74. gchar *buffer;
  75. buffer = mc_config_normalize_before_save (value);
  76. g_key_file_set_string (mc_config->handle, group, param, buffer);
  77. g_free (buffer);
  78. }
  79. }
  80. /* --------------------------------------------------------------------------------------------- */
  81. void
  82. mc_config_set_bool (mc_config_t * mc_config, const gchar * group,
  83. 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. /* --------------------------------------------------------------------------------------------- */