history.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2019
  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-2019
  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_CACHE_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. * Load history from the mc_config
  71. */
  72. GList *
  73. mc_config_history_load (mc_config_t * cfg, const char *name)
  74. {
  75. size_t i;
  76. GList *hist = NULL;
  77. char **keys;
  78. size_t keys_num = 0;
  79. GIConv conv = INVALID_CONV;
  80. GString *buffer;
  81. if (name == NULL || *name == '\0')
  82. return NULL;
  83. /* get number of keys */
  84. keys = mc_config_get_keys (cfg, name, &keys_num);
  85. g_strfreev (keys);
  86. /* create charset conversion handler to convert strings
  87. from utf-8 to system codepage */
  88. if (!mc_global.utf8_display)
  89. conv = str_crt_conv_from ("UTF-8");
  90. buffer = g_string_sized_new (64);
  91. for (i = 0; i < keys_num; i++)
  92. {
  93. char key[BUF_TINY];
  94. char *this_entry;
  95. g_snprintf (key, sizeof (key), "%lu", (unsigned long) i);
  96. this_entry = mc_config_get_string_raw (cfg, name, key, "");
  97. if (this_entry == NULL)
  98. continue;
  99. if (conv == INVALID_CONV)
  100. hist = list_append_unique (hist, this_entry);
  101. else
  102. {
  103. g_string_set_size (buffer, 0);
  104. if (str_convert (conv, this_entry, buffer) == ESTR_FAILURE)
  105. hist = list_append_unique (hist, this_entry);
  106. else
  107. {
  108. hist = list_append_unique (hist, g_strndup (buffer->str, buffer->len));
  109. g_free (this_entry);
  110. }
  111. }
  112. }
  113. g_string_free (buffer, TRUE);
  114. if (conv != INVALID_CONV)
  115. str_close_conv (conv);
  116. /* return pointer to the last entry in the list */
  117. return g_list_last (hist);
  118. }
  119. /* --------------------------------------------------------------------------------------------- */
  120. /**
  121. * Save history to the mc_config, but don't save config to file
  122. */
  123. void
  124. mc_config_history_save (mc_config_t * cfg, const char *name, GList * h)
  125. {
  126. GIConv conv = INVALID_CONV;
  127. GString *buffer;
  128. int i;
  129. if (name == NULL || *name == '\0' || h == NULL)
  130. return;
  131. /* go to end of list */
  132. h = g_list_last (h);
  133. /* go back 60 places */
  134. for (i = 0; (i < num_history_items_recorded - 1) && (h->prev != NULL); i++)
  135. h = g_list_previous (h);
  136. if (name != NULL)
  137. mc_config_del_group (cfg, name);
  138. /* create charset conversion handler to convert strings
  139. from system codepage to UTF-8 */
  140. if (!mc_global.utf8_display)
  141. conv = str_crt_conv_to ("UTF-8");
  142. buffer = g_string_sized_new (64);
  143. /* dump history into profile */
  144. for (i = 0; h != NULL; h = g_list_next (h))
  145. {
  146. char key[BUF_TINY];
  147. char *text = (char *) h->data;
  148. /* We shouldn't have null entries, but let's be sure */
  149. if (text == NULL)
  150. continue;
  151. g_snprintf (key, sizeof (key), "%d", i++);
  152. if (conv == INVALID_CONV)
  153. mc_config_set_string_raw (cfg, name, key, text);
  154. else
  155. {
  156. g_string_set_size (buffer, 0);
  157. if (str_convert (conv, text, buffer) == ESTR_FAILURE)
  158. mc_config_set_string_raw (cfg, name, key, text);
  159. else
  160. mc_config_set_string_raw (cfg, name, key, buffer->str);
  161. }
  162. }
  163. g_string_free (buffer, TRUE);
  164. if (conv != INVALID_CONV)
  165. str_close_conv (conv);
  166. }
  167. /* --------------------------------------------------------------------------------------------- */