common.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* Configure module for the Midnight Commander
  2. Copyright (C) 1994, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
  3. 2007, 2009 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. */
  16. #include <config.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <errno.h> /* extern int errno */
  22. #include "lib/global.h"
  23. #include "lib/vfs/vfs.h" /* mc_stat */
  24. #include "lib/util.h"
  25. #include "lib/mcconfig.h"
  26. /*** global variables **************************************************/
  27. mc_config_t *mc_main_config;
  28. mc_config_t *mc_panels_config;
  29. /*** file scope macro definitions **************************************/
  30. /*** file scope type declarations **************************************/
  31. /*** file scope variables **********************************************/
  32. /*** file scope functions **********************************************/
  33. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  34. static gboolean
  35. mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path, GError ** error)
  36. {
  37. gchar *data, *written_data;
  38. gsize len, total_written;
  39. gboolean ret;
  40. int fd;
  41. ssize_t cur_written;
  42. data = g_key_file_to_data (mc_config->handle, &len, NULL);
  43. if (!exist_file (ini_path))
  44. {
  45. ret = g_file_set_contents (ini_path, data, len, error);
  46. g_free (data);
  47. return ret;
  48. }
  49. mc_util_make_backup_if_possible (ini_path, "~");
  50. fd = mc_open (ini_path, O_WRONLY | O_TRUNC, 0);
  51. if (fd == -1)
  52. {
  53. g_propagate_error (error, g_error_new (MC_ERROR, 0, "%s", unix_error_string (errno)));
  54. g_free (data);
  55. return FALSE;
  56. }
  57. for (written_data = data, total_written = len;
  58. (cur_written = mc_write (fd, (const void *) written_data, total_written)) > 0;
  59. written_data += cur_written, total_written -= cur_written);
  60. mc_close (fd);
  61. g_free (data);
  62. if (cur_written == -1)
  63. {
  64. mc_util_restore_from_backup_if_possible (ini_path, "~");
  65. g_propagate_error (error, g_error_new (MC_ERROR, 0, "%s", unix_error_string (errno)));
  66. return FALSE;
  67. }
  68. mc_util_unlink_backup_if_possible (ini_path, "~");
  69. return TRUE;
  70. }
  71. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  72. /*** public functions **************************************************/
  73. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  74. mc_config_t *
  75. mc_config_init (const gchar * ini_path)
  76. {
  77. mc_config_t *mc_config;
  78. struct stat st;
  79. mc_config = g_try_malloc0 (sizeof (mc_config_t));
  80. if (mc_config == NULL)
  81. return NULL;
  82. mc_config->handle = g_key_file_new ();
  83. if (mc_config->handle == NULL)
  84. {
  85. g_free (mc_config);
  86. return NULL;
  87. }
  88. if (ini_path == NULL)
  89. return mc_config;
  90. if (exist_file (ini_path) && mc_stat (ini_path, &st) == 0 && st.st_size != 0)
  91. {
  92. /* file exists and not empty */
  93. g_key_file_load_from_file (mc_config->handle, ini_path, G_KEY_FILE_KEEP_COMMENTS, NULL);
  94. }
  95. mc_config->ini_path = g_strdup (ini_path);
  96. return mc_config;
  97. }
  98. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  99. void
  100. mc_config_deinit (mc_config_t * mc_config)
  101. {
  102. if (mc_config != NULL)
  103. {
  104. g_free (mc_config->ini_path);
  105. g_key_file_free (mc_config->handle);
  106. g_free (mc_config);
  107. }
  108. }
  109. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  110. gboolean
  111. mc_config_has_param (const mc_config_t * mc_config, const char *group, const gchar * param)
  112. {
  113. if (!mc_config || !group || !param)
  114. return FALSE;
  115. return g_key_file_has_key (mc_config->handle, group, param, NULL);
  116. }
  117. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  118. gboolean
  119. mc_config_has_group (mc_config_t * mc_config, const char *group)
  120. {
  121. if (!mc_config || !group)
  122. return FALSE;
  123. return g_key_file_has_group (mc_config->handle, group);
  124. }
  125. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  126. gboolean
  127. mc_config_del_key (mc_config_t * mc_config, const char *group, const gchar * param)
  128. {
  129. if (!mc_config || !group || !param)
  130. return FALSE;
  131. #if GLIB_CHECK_VERSION (2, 15, 0)
  132. return g_key_file_remove_key (mc_config->handle, group, param, NULL);
  133. #else
  134. g_key_file_remove_key (mc_config->handle, group, param, NULL);
  135. return TRUE;
  136. #endif
  137. }
  138. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  139. gboolean
  140. mc_config_del_group (mc_config_t * mc_config, const char *group)
  141. {
  142. if (!mc_config || !group)
  143. return FALSE;
  144. #if GLIB_CHECK_VERSION (2, 15, 0)
  145. return g_key_file_remove_group (mc_config->handle, group, NULL);
  146. #else
  147. g_key_file_remove_group (mc_config->handle, group, NULL);
  148. return TRUE;
  149. #endif
  150. }
  151. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  152. gboolean
  153. mc_config_read_file (mc_config_t * mc_config, const gchar * ini_path, gboolean remove_empty)
  154. {
  155. mc_config_t *tmp_config;
  156. gchar **groups, **curr_grp;
  157. gchar **keys, **curr_key;
  158. gchar *value;
  159. if (mc_config == NULL)
  160. return FALSE;
  161. tmp_config = mc_config_init (ini_path);
  162. if (tmp_config == NULL)
  163. return FALSE;
  164. groups = mc_config_get_groups (tmp_config, NULL);
  165. if (groups == NULL)
  166. {
  167. mc_config_deinit (tmp_config);
  168. return FALSE;
  169. }
  170. for (curr_grp = groups; *curr_grp != NULL; curr_grp++)
  171. {
  172. keys = mc_config_get_keys (tmp_config, *curr_grp, NULL);
  173. for (curr_key = keys; *curr_key != NULL; curr_key++)
  174. {
  175. value = g_key_file_get_value (tmp_config->handle, *curr_grp, *curr_key, NULL);
  176. if (value != NULL)
  177. {
  178. if (*value == '\0' && remove_empty)
  179. g_key_file_remove_key (mc_config->handle, *curr_grp, *curr_key, NULL);
  180. else
  181. g_key_file_set_value (mc_config->handle, *curr_grp, *curr_key, value);
  182. g_free (value);
  183. }
  184. else if (remove_empty)
  185. g_key_file_remove_key (mc_config->handle, *curr_grp, *curr_key, NULL);
  186. }
  187. g_strfreev (keys);
  188. }
  189. g_strfreev (groups);
  190. mc_config_deinit (tmp_config);
  191. return TRUE;
  192. }
  193. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  194. gboolean
  195. mc_config_save_file (mc_config_t * mc_config, GError ** error)
  196. {
  197. if (mc_config == NULL || mc_config->ini_path == NULL)
  198. {
  199. return FALSE;
  200. }
  201. return mc_config_new_or_override_file (mc_config, mc_config->ini_path, error);
  202. }
  203. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  204. gboolean
  205. mc_config_save_to_file (mc_config_t * mc_config, const gchar * ini_path, GError ** error)
  206. {
  207. if (mc_config == NULL)
  208. {
  209. return FALSE;
  210. }
  211. return mc_config_new_or_override_file (mc_config, ini_path, error);
  212. }
  213. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */