history.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. Configure module for the Midnight Commander
  3. Copyright (C) 1994-2024
  4. Free Software Foundation, Inc.
  5. Authors:
  6. Radek Doulik, 1994, 1995
  7. Miguel de Icaza, 1994, 1995
  8. Jakub Jelinek, 1995
  9. Andrej Borsenkow, 1996
  10. Norbert Warmuth, 1997
  11. Andrew Borodin <aborodin@vmail.ru>, 2009-2023
  12. This file is part of the Midnight Commander.
  13. The Midnight Commander is free software: you can redistribute it
  14. and/or modify it under the terms of the GNU General Public License as
  15. published by the Free Software Foundation, either version 3 of the License,
  16. or (at your option) any later version.
  17. The Midnight Commander is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. /** \file history.c
  25. * \brief Source: save and load history
  26. */
  27. #include <config.h>
  28. #include <stdlib.h>
  29. #include <sys/types.h>
  30. #include "lib/global.h"
  31. #include "lib/fileloc.h" /* MC_HISTORY_FILE */
  32. #include "lib/strutil.h"
  33. #include "lib/util.h" /* list_append_unique */
  34. #include "lib/mcconfig.h"
  35. /*** global variables ****************************************************************************/
  36. /* how much history items are used */
  37. int num_history_items_recorded = 60;
  38. /*** file scope macro definitions ****************************************************************/
  39. /*** file scope type declarations ****************************************************************/
  40. /*** file scope variables ************************************************************************/
  41. /* --------------------------------------------------------------------------------------------- */
  42. /*** file scope functions ************************************************************************/
  43. /* --------------------------------------------------------------------------------------------- */
  44. /* --------------------------------------------------------------------------------------------- */
  45. /*** public functions ****************************************************************************/
  46. /* --------------------------------------------------------------------------------------------- */
  47. /**
  48. * Load the history from the ${XDG_DATA_HOME}/mc/history file.
  49. * It is called with the widgets history name and returns the GList list.
  50. */
  51. GList *
  52. mc_config_history_get (const char *name)
  53. {
  54. GList *hist = NULL;
  55. char *profile;
  56. mc_config_t *cfg;
  57. if (num_history_items_recorded == 0) /* this is how to disable */
  58. return NULL;
  59. if (name == NULL || *name == '\0')
  60. return NULL;
  61. profile = mc_config_get_full_path (MC_HISTORY_FILE);
  62. cfg = mc_config_init (profile, TRUE);
  63. hist = mc_config_history_load (cfg, name);
  64. mc_config_deinit (cfg);
  65. g_free (profile);
  66. return hist;
  67. }
  68. /* --------------------------------------------------------------------------------------------- */
  69. /**
  70. * Get the recent item of a history from the ${XDG_DATA_HOME}/mc/history file.
  71. *
  72. * TODO: get rid of load the entire history to get the only top item.
  73. */
  74. char *
  75. mc_config_history_get_recent_item (const char *name)
  76. {
  77. GList *history;
  78. char *item = NULL;
  79. history = mc_config_history_get (name);
  80. if (history != NULL)
  81. {
  82. /* FIXME: can history->data be NULL? */
  83. item = (char *) history->data;
  84. history->data = NULL;
  85. history = g_list_first (history);
  86. g_list_free_full (history, g_free);
  87. }
  88. return item;
  89. }
  90. /* --------------------------------------------------------------------------------------------- */
  91. /**
  92. * Load history from the mc_config
  93. */
  94. GList *
  95. mc_config_history_load (mc_config_t *cfg, const char *name)
  96. {
  97. size_t i;
  98. GList *hist = NULL;
  99. char **keys;
  100. size_t keys_num = 0;
  101. GIConv conv = INVALID_CONV;
  102. GString *buffer;
  103. if (name == NULL || *name == '\0')
  104. return NULL;
  105. /* get number of keys */
  106. keys = mc_config_get_keys (cfg, name, &keys_num);
  107. g_strfreev (keys);
  108. /* create charset conversion handler to convert strings
  109. from utf-8 to system codepage */
  110. if (!mc_global.utf8_display)
  111. conv = str_crt_conv_from ("UTF-8");
  112. buffer = g_string_sized_new (64);
  113. for (i = 0; i < keys_num; i++)
  114. {
  115. char key[BUF_TINY];
  116. char *this_entry;
  117. g_snprintf (key, sizeof (key), "%lu", (unsigned long) i);
  118. this_entry = mc_config_get_string_raw (cfg, name, key, "");
  119. if (this_entry == NULL)
  120. continue;
  121. if (conv == INVALID_CONV)
  122. hist = list_append_unique (hist, this_entry);
  123. else
  124. {
  125. g_string_set_size (buffer, 0);
  126. if (str_convert (conv, this_entry, buffer) == ESTR_FAILURE)
  127. hist = list_append_unique (hist, this_entry);
  128. else
  129. {
  130. hist = list_append_unique (hist, g_strndup (buffer->str, buffer->len));
  131. g_free (this_entry);
  132. }
  133. }
  134. }
  135. g_string_free (buffer, TRUE);
  136. if (conv != INVALID_CONV)
  137. str_close_conv (conv);
  138. /* return pointer to the last entry in the list */
  139. return g_list_last (hist);
  140. }
  141. /* --------------------------------------------------------------------------------------------- */
  142. /**
  143. * Save history to the mc_config, but don't save config to file
  144. */
  145. void
  146. mc_config_history_save (mc_config_t *cfg, const char *name, GList *h)
  147. {
  148. GIConv conv = INVALID_CONV;
  149. GString *buffer;
  150. int i;
  151. if (name == NULL || *name == '\0' || h == NULL)
  152. return;
  153. /* go to end of list */
  154. h = g_list_last (h);
  155. /* go back 60 places */
  156. for (i = 0; (i < num_history_items_recorded - 1) && (h->prev != NULL); i++)
  157. h = g_list_previous (h);
  158. mc_config_del_group (cfg, name);
  159. /* create charset conversion handler to convert strings
  160. from system codepage to UTF-8 */
  161. if (!mc_global.utf8_display)
  162. conv = str_crt_conv_to ("UTF-8");
  163. buffer = g_string_sized_new (64);
  164. /* dump history into profile */
  165. for (i = 0; h != NULL; h = g_list_next (h))
  166. {
  167. char key[BUF_TINY];
  168. char *text = (char *) h->data;
  169. /* We shouldn't have null entries, but let's be sure */
  170. if (text == NULL)
  171. continue;
  172. g_snprintf (key, sizeof (key), "%d", i++);
  173. if (conv == INVALID_CONV)
  174. mc_config_set_string_raw (cfg, name, key, text);
  175. else
  176. {
  177. g_string_set_size (buffer, 0);
  178. if (str_convert (conv, text, buffer) == ESTR_FAILURE)
  179. mc_config_set_string_raw (cfg, name, key, text);
  180. else
  181. mc_config_set_string_raw (cfg, name, key, buffer->str);
  182. }
  183. }
  184. g_string_free (buffer, TRUE);
  185. if (conv != INVALID_CONV)
  186. str_close_conv (conv);
  187. }
  188. /* --------------------------------------------------------------------------------------------- */