set.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. Configure module for the Midnight Commander
  3. Copyright (C) 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
  4. 2007, 2009, 2011
  5. The Free Software Foundation, Inc.
  6. This file is part of the Midnight Commander.
  7. The Midnight Commander is free software: you can redistribute it
  8. and/or modify it under the terms of the GNU General Public License as
  9. published by the Free Software Foundation, either version 3 of the License,
  10. or (at your option) any later version.
  11. The Midnight Commander is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <config.h>
  19. #include "lib/global.h"
  20. #include "lib/strutil.h"
  21. #include "lib/mcconfig.h"
  22. /*** global variables **************************************************/
  23. /*** file scope macro definitions **************************************/
  24. /*** file scope type declarations **************************************/
  25. /*** file scope variables **********************************************/
  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 || !group || !param || !value)
  57. return;
  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 || !group || !param || !value)
  66. return;
  67. g_key_file_set_value (mc_config->handle, group, param, value);
  68. }
  69. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  70. void
  71. mc_config_set_string (const mc_config_t * mc_config, const gchar * group,
  72. const gchar * param, const gchar * value)
  73. {
  74. gchar *buffer;
  75. if (!mc_config || !group || !param || !value)
  76. return;
  77. buffer = mc_config_normalize_before_save (value);
  78. g_key_file_set_string (mc_config->handle, group, param, buffer);
  79. g_free (buffer);
  80. }
  81. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  82. void
  83. mc_config_set_bool (mc_config_t * mc_config, const gchar * group,
  84. const gchar * param, gboolean value)
  85. {
  86. if (!mc_config || !group || !param)
  87. return;
  88. g_key_file_set_boolean (mc_config->handle, group, param, value);
  89. }
  90. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  91. void
  92. mc_config_set_int (mc_config_t * mc_config, const gchar * group, const gchar * param, int value)
  93. {
  94. if (!mc_config || !group || !param)
  95. return;
  96. g_key_file_set_integer (mc_config->handle, group, param, value);
  97. }
  98. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  99. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  100. void
  101. mc_config_set_string_list (mc_config_t * mc_config, const gchar * group,
  102. const gchar * param, const gchar * const value[], gsize length)
  103. {
  104. if (!mc_config || !group || !param || !value || length == 0)
  105. return;
  106. g_key_file_set_string_list (mc_config->handle, group, param, value, length);
  107. }
  108. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  109. void
  110. mc_config_set_bool_list (mc_config_t * mc_config, const gchar * group,
  111. const gchar * param, gboolean value[], gsize length)
  112. {
  113. if (!mc_config || !group || !param || !value || length == 0)
  114. return;
  115. g_key_file_set_boolean_list (mc_config->handle, group, param, value, length);
  116. }
  117. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  118. void
  119. mc_config_set_int_list (mc_config_t * mc_config, const gchar * group,
  120. const gchar * param, int value[], gsize length)
  121. {
  122. if (!mc_config || !group || !param || !value || length == 0)
  123. return;
  124. g_key_file_set_integer_list (mc_config->handle, group, param, value, length);
  125. }
  126. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */