history.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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, 2013
  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, 2013
  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 (WDialog * 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 (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  91. {
  92. switch (msg)
  93. {
  94. case MSG_RESIZE:
  95. return history_dlg_reposition (DIALOG (w));
  96. default:
  97. return dlg_default_callback (w, 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, TRUE);
  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_strndup (buffer->str, buffer->len));
  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, count = 0;
  229. char *r = NULL;
  230. WDialog *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. size_t i;
  240. i = str_term_width1 ((char *) z->data);
  241. maxlen = max (maxlen, i);
  242. count++;
  243. entry = g_new0 (WLEntry, 1);
  244. /* history is being reverted here */
  245. entry->text = g_strdup ((char *) z->data);
  246. hlist = g_list_prepend (hlist, entry);
  247. }
  248. hist_data.widget = widget;
  249. hist_data.count = count;
  250. hist_data.maxlen = maxlen;
  251. query_dlg =
  252. dlg_create (TRUE, 0, 0, 4, 4, dialog_colors, history_dlg_callback, NULL,
  253. "[History-query]", _("History"), DLG_COMPACT);
  254. query_dlg->data = &hist_data;
  255. query_list = listbox_new (1, 1, 2, 2, TRUE, NULL);
  256. /* this call makes list stick to all sides of dialog, effectively make
  257. it be resized with dialog */
  258. add_widget_autopos (query_dlg, query_list, WPOS_KEEP_ALL, NULL);
  259. /* to avoid diplicating of (calculating sizes in two places)
  260. code, call dlg_hist_callback function here, to set dialog and
  261. controls positions.
  262. The main idea - create 4x4 dialog and add 2x2 list in
  263. center of it, and let dialog function resize it to needed
  264. size. */
  265. send_message (query_dlg, NULL, MSG_RESIZE, 0, NULL);
  266. if (WIDGET (query_dlg)->y < widget->y)
  267. {
  268. /* draw list entries from bottom upto top */
  269. listbox_set_list (query_list, hlist);
  270. if (current < 0 || (size_t) current >= count)
  271. listbox_select_last (query_list);
  272. else
  273. listbox_select_entry (query_list, count - 1 - (size_t) current);
  274. }
  275. else
  276. {
  277. /* draw list entries from top downto bottom */
  278. /* revert history direction */
  279. hlist = g_list_reverse (hlist);
  280. listbox_set_list (query_list, hlist);
  281. if (current > 0)
  282. listbox_select_entry (query_list, current);
  283. }
  284. if (dlg_run (query_dlg) != B_CANCEL)
  285. {
  286. char *q;
  287. listbox_get_current (query_list, &q, NULL);
  288. r = g_strdup (q);
  289. }
  290. /* get modified history from dialog */
  291. z = NULL;
  292. for (hi = query_list->list; hi != NULL; hi = g_list_next (hi))
  293. {
  294. WLEntry *entry = LENTRY (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 (WIDGET (query_dlg)->y < widget->y)
  301. z = g_list_reverse (z);
  302. dlg_destroy (query_dlg);
  303. g_list_free_full (*history, g_free);
  304. *history = g_list_last (z);
  305. return r;
  306. }
  307. /* --------------------------------------------------------------------------------------------- */