set.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. Configure module for the Midnight Commander
  3. Copyright (C) 1994-2017
  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. /*** file scope functions **********************************************/
  26. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  27. static gchar *
  28. mc_config_normalize_before_save (const gchar * value)
  29. {
  30. GIConv conv;
  31. GString *buffer;
  32. gboolean ok;
  33. if (mc_global.utf8_display)
  34. return g_strdup (value);
  35. conv = str_crt_conv_to ("UTF-8");
  36. if (conv == INVALID_CONV)
  37. return g_strdup (value);
  38. buffer = g_string_new ("");
  39. ok = (str_convert (conv, value, buffer) != ESTR_FAILURE);
  40. str_close_conv (conv);
  41. if (!ok)
  42. {
  43. g_string_free (buffer, TRUE);
  44. return g_strdup (value);
  45. }
  46. return g_string_free (buffer, FALSE);
  47. }
  48. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  49. /*** public functions **************************************************/
  50. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  51. void
  52. mc_config_set_string_raw (mc_config_t * mc_config, const gchar * group,
  53. const gchar * param, const gchar * value)
  54. {
  55. if (!mc_config || !group || !param || !value)
  56. return;
  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 || !group || !param || !value)
  65. return;
  66. g_key_file_set_value (mc_config->handle, group, param, value);
  67. }
  68. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  69. void
  70. mc_config_set_string (const mc_config_t * mc_config, const gchar * group,
  71. const gchar * param, const gchar * value)
  72. {
  73. gchar *buffer;
  74. if (!mc_config || !group || !param || !value)
  75. return;
  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. 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 || !group || !param)
  86. return;
  87. g_key_file_set_boolean (mc_config->handle, group, param, value);
  88. }
  89. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  90. void
  91. mc_config_set_int (mc_config_t * mc_config, const gchar * group, const gchar * param, int value)
  92. {
  93. if (!mc_config || !group || !param)
  94. return;
  95. g_key_file_set_integer (mc_config->handle, group, param, value);
  96. }
  97. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  98. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  99. void
  100. mc_config_set_string_list (mc_config_t * mc_config, const gchar * group,
  101. const gchar * param, const gchar * const value[], gsize length)
  102. {
  103. if (!mc_config || !group || !param || !value || length == 0)
  104. return;
  105. g_key_file_set_string_list (mc_config->handle, group, param, value, length);
  106. }
  107. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  108. void
  109. mc_config_set_bool_list (mc_config_t * mc_config, const gchar * group,
  110. const gchar * param, gboolean value[], gsize length)
  111. {
  112. if (!mc_config || !group || !param || !value || length == 0)
  113. return;
  114. g_key_file_set_boolean_list (mc_config->handle, group, param, value, length);
  115. }
  116. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  117. void
  118. mc_config_set_int_list (mc_config_t * mc_config, const gchar * group,
  119. const gchar * param, int value[], gsize length)
  120. {
  121. if (!mc_config || !group || !param || !value || length == 0)
  122. return;
  123. g_key_file_set_integer_list (mc_config->handle, group, param, value, length);
  124. }
  125. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */