history.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
  4. 2004, 2005, 2006, 2007, 2009, 2010, 2011, 2012
  5. The Free Software Foundation, Inc.
  6. Authors:
  7. Radek Doulik, 1994, 1995
  8. Miguel de Icaza, 1994, 1995
  9. Jakub Jelinek, 1995
  10. Andrej Borsenkow, 1996
  11. Norbert Warmuth, 1997
  12. Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2011, 2012
  13. This file is part of the Midnight Commander.
  14. The Midnight Commander is free software: you can redistribute it
  15. and/or modify it under the terms of the GNU General Public License as
  16. published by the Free Software Foundation, either version 3 of the License,
  17. or (at your option) any later version.
  18. The Midnight Commander is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. /** \file history.c
  26. * \brief Source: save, load and show history
  27. */
  28. #include <config.h>
  29. #include <errno.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <fcntl.h>
  36. #include "lib/global.h"
  37. #include "lib/tty/tty.h" /* LINES, COLS */
  38. #include "lib/mcconfig.h" /* for history loading and saving */
  39. #include "lib/fileloc.h"
  40. #include "lib/strutil.h"
  41. #include "lib/util.h" /* list_append_unique */
  42. #include "lib/widget.h"
  43. /*** global variables ****************************************************************************/
  44. /* how much history items are used */
  45. int num_history_items_recorded = 60;
  46. /*** file scope macro definitions ****************************************************************/
  47. /*** file scope type declarations ****************************************************************/
  48. typedef struct
  49. {
  50. Widget *widget;
  51. size_t count;
  52. size_t maxlen;
  53. } history_dlg_data;
  54. /*** file scope variables ************************************************************************/
  55. /*** file scope functions ************************************************************************/
  56. static cb_ret_t
  57. history_dlg_reposition (Dlg_head * dlg_head)
  58. {
  59. history_dlg_data *data;
  60. int x = 0, y, he, wi;
  61. /* guard checks */
  62. if ((dlg_head == NULL) || (dlg_head->data == NULL))
  63. return MSG_NOT_HANDLED;
  64. data = (history_dlg_data *) dlg_head->data;
  65. y = data->widget->y;
  66. he = data->count + 2;
  67. if (he <= y || y > (LINES - 6))
  68. {
  69. he = min (he, y - 1);
  70. y -= he;
  71. }
  72. else
  73. {
  74. y++;
  75. he = min (he, LINES - y);
  76. }
  77. if (data->widget->x > 2)
  78. x = data->widget->x - 2;
  79. wi = data->maxlen + 4;
  80. if ((wi + x) > COLS)
  81. {
  82. wi = min (wi, COLS);
  83. x = COLS - wi;
  84. }
  85. dlg_set_position (dlg_head, y, x, y + he, x + wi);
  86. return MSG_HANDLED;
  87. }
  88. /* --------------------------------------------------------------------------------------------- */
  89. static cb_ret_t
  90. history_dlg_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
  91. {
  92. switch (msg)
  93. {
  94. case DLG_RESIZE:
  95. return history_dlg_reposition (h);
  96. default:
  97. return default_dlg_callback (h, sender, msg, parm, data);
  98. }
  99. }
  100. /* --------------------------------------------------------------------------------------------- */
  101. /*** public functions ****************************************************************************/
  102. /* --------------------------------------------------------------------------------------------- */
  103. /**
  104. * Load the history from the ${XDG_CACHE_HOME}/mc/history file.
  105. * It is called with the widgets history name and returns the GList list.
  106. */
  107. GList *
  108. history_get (const char *input_name)
  109. {
  110. GList *hist = NULL;
  111. char *profile;
  112. mc_config_t *cfg;
  113. if (num_history_items_recorded == 0) /* this is how to disable */
  114. return NULL;
  115. if ((input_name == NULL) || (*input_name == '\0'))
  116. return NULL;
  117. profile = mc_config_get_full_path (MC_HISTORY_FILE);
  118. cfg = mc_config_init (profile);
  119. hist = history_load (cfg, input_name);
  120. mc_config_deinit (cfg);
  121. g_free (profile);
  122. return hist;
  123. }
  124. /* --------------------------------------------------------------------------------------------- */
  125. /**
  126. * Load history form the mc_config
  127. */
  128. GList *
  129. history_load (struct mc_config_t * cfg, const char *name)
  130. {
  131. size_t i;
  132. GList *hist = NULL;
  133. char **keys;
  134. size_t keys_num = 0;
  135. GIConv conv = INVALID_CONV;
  136. GString *buffer;
  137. if (name == NULL || *name == '\0')
  138. return NULL;
  139. /* get number of keys */
  140. keys = mc_config_get_keys (cfg, name, &keys_num);
  141. g_strfreev (keys);
  142. /* create charset conversion handler to convert strings
  143. from utf-8 to system codepage */
  144. if (!mc_global.utf8_display)
  145. conv = str_crt_conv_from ("UTF-8");
  146. buffer = g_string_sized_new (64);
  147. for (i = 0; i < keys_num; i++)
  148. {
  149. char key[BUF_TINY];
  150. char *this_entry;
  151. g_snprintf (key, sizeof (key), "%lu", (unsigned long) i);
  152. this_entry = mc_config_get_string_raw (cfg, name, key, "");
  153. if (this_entry == NULL)
  154. continue;
  155. if (conv == INVALID_CONV)
  156. hist = list_append_unique (hist, this_entry);
  157. else
  158. {
  159. g_string_set_size (buffer, 0);
  160. if (str_convert (conv, this_entry, buffer) == ESTR_FAILURE)
  161. hist = list_append_unique (hist, this_entry);
  162. else
  163. {
  164. hist = list_append_unique (hist, g_strdup (buffer->str));
  165. g_free (this_entry);
  166. }
  167. }
  168. }
  169. g_string_free (buffer, TRUE);
  170. if (conv != INVALID_CONV)
  171. str_close_conv (conv);
  172. /* return pointer to the last entry in the list */
  173. return g_list_last (hist);
  174. }
  175. /* --------------------------------------------------------------------------------------------- */
  176. /**
  177. * Save history to the mc_config, but don't save config to file
  178. */
  179. void
  180. history_save (struct mc_config_t *cfg, const char *name, GList * h)
  181. {
  182. GIConv conv = INVALID_CONV;
  183. GString *buffer;
  184. int i;
  185. if (name == NULL || *name == '\0' || h == NULL)
  186. return;
  187. /* go to end of list */
  188. h = g_list_last (h);
  189. /* go back 60 places */
  190. for (i = 0; (i < num_history_items_recorded - 1) && (h->prev != NULL); i++)
  191. h = g_list_previous (h);
  192. if (name != NULL)
  193. mc_config_del_group (cfg, name);
  194. /* create charset conversion handler to convert strings
  195. from system codepage to UTF-8 */
  196. if (!mc_global.utf8_display)
  197. conv = str_crt_conv_to ("UTF-8");
  198. buffer = g_string_sized_new (64);
  199. /* dump history into profile */
  200. for (i = 0; h != NULL; h = g_list_next (h))
  201. {
  202. char key[BUF_TINY];
  203. char *text = (char *) h->data;
  204. /* We shouldn't have null entries, but let's be sure */
  205. if (text == NULL)
  206. continue;
  207. g_snprintf (key, sizeof (key), "%d", i++);
  208. if (conv == INVALID_CONV)
  209. mc_config_set_string_raw (cfg, name, key, text);
  210. else
  211. {
  212. g_string_set_size (buffer, 0);
  213. if (str_convert (conv, text, buffer) == ESTR_FAILURE)
  214. mc_config_set_string_raw (cfg, name, key, text);
  215. else
  216. mc_config_set_string_raw (cfg, name, key, buffer->str);
  217. }
  218. }
  219. g_string_free (buffer, TRUE);
  220. if (conv != INVALID_CONV)
  221. str_close_conv (conv);
  222. }
  223. /* --------------------------------------------------------------------------------------------- */
  224. char *
  225. history_show (GList ** history, Widget * widget, int current)
  226. {
  227. GList *z, *hlist = NULL, *hi;
  228. size_t maxlen, i, count = 0;
  229. char *r = NULL;
  230. Dlg_head *query_dlg;
  231. WListbox *query_list;
  232. history_dlg_data hist_data;
  233. if (*history == NULL)
  234. return NULL;
  235. maxlen = str_term_width1 (_("History")) + 2;
  236. for (z = *history; z != NULL; z = g_list_previous (z))
  237. {
  238. WLEntry *entry;
  239. i = str_term_width1 ((char *) z->data);
  240. maxlen = max (maxlen, i);
  241. count++;
  242. entry = g_new0 (WLEntry, 1);
  243. /* history is being reverted here */
  244. entry->text = g_strdup ((char *) z->data);
  245. hlist = g_list_prepend (hlist, entry);
  246. }
  247. hist_data.widget = widget;
  248. hist_data.count = count;
  249. hist_data.maxlen = maxlen;
  250. query_dlg =
  251. create_dlg (TRUE, 0, 0, 4, 4, dialog_colors, history_dlg_callback, NULL,
  252. "[History-query]", _("History"), DLG_COMPACT);
  253. query_dlg->data = &hist_data;
  254. query_list = listbox_new (1, 1, 2, 2, TRUE, NULL);
  255. /* this call makes list stick to all sides of dialog, effectively make
  256. it be resized with dialog */
  257. add_widget_autopos (query_dlg, query_list, WPOS_KEEP_ALL, NULL);
  258. /* to avoid diplicating of (calculating sizes in two places)
  259. code, call dlg_hist_callback function here, to set dialog and
  260. controls positions.
  261. The main idea - create 4x4 dialog and add 2x2 list in
  262. center of it, and let dialog function resize it to needed
  263. size. */
  264. history_dlg_callback (query_dlg, NULL, DLG_RESIZE, 0, NULL);
  265. if (query_dlg->y < widget->y)
  266. {
  267. /* draw list entries from bottom upto top */
  268. listbox_set_list (query_list, hlist);
  269. if (current < 0 || (size_t) current >= count)
  270. listbox_select_last (query_list);
  271. else
  272. listbox_select_entry (query_list, count - 1 - (size_t) current);
  273. }
  274. else
  275. {
  276. /* draw list entries from top downto bottom */
  277. /* revert history direction */
  278. hlist = g_list_reverse (hlist);
  279. listbox_set_list (query_list, hlist);
  280. if (current > 0)
  281. listbox_select_entry (query_list, current);
  282. }
  283. if (run_dlg (query_dlg) != B_CANCEL)
  284. {
  285. char *q;
  286. listbox_get_current (query_list, &q, NULL);
  287. r = g_strdup (q);
  288. }
  289. /* get modified history from dialog */
  290. z = NULL;
  291. for (hi = query_list->list; hi != NULL; hi = g_list_next (hi))
  292. {
  293. WLEntry *entry;
  294. entry = (WLEntry *) hi->data;
  295. /* history is being reverted here again */
  296. z = g_list_prepend (z, entry->text);
  297. entry->text = NULL;
  298. }
  299. /* restore history direction */
  300. if (query_dlg->y < widget->y)
  301. z = g_list_reverse (z);
  302. destroy_dlg (query_dlg);
  303. g_list_foreach (*history, (GFunc) g_free, NULL);
  304. g_list_free (*history);
  305. *history = g_list_last (z);
  306. return r;
  307. }
  308. /* --------------------------------------------------------------------------------------------- */