history.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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
  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
  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 = g_build_filename (mc_config_get_cache_path (), MC_HISTORY_FILE, NULL);
  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. #if 0
  225. /**
  226. * Write the history to the ${XDG_CACHE_HOME}/mc/history file.
  227. */
  228. void
  229. history_put (const char *input_name, GList * h)
  230. {
  231. char *profile;
  232. int i;
  233. if (num_history_items_recorded == 0) /* this is how to disable */
  234. return;
  235. if ((input_name == NULL) || (*input_name == '\0'))
  236. return;
  237. if (h == NULL)
  238. return;
  239. profile = g_build_filename (mc_config_get_cache_path (), MC_HISTORY_FILE, (char *) NULL);
  240. i = open (profile, O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
  241. if (i != -1)
  242. close (i);
  243. /* Make sure the history is only readable by the user */
  244. if (chmod (profile, S_IRUSR | S_IWUSR) != -1 || errno == ENOENT)
  245. {
  246. mc_config_t *cfg;
  247. cfg = mc_config_init (profile);
  248. history_save (cfg, input_name, h);
  249. mc_config_save_file (cfg, NULL);
  250. mc_config_deinit (cfg);
  251. }
  252. g_free (profile);
  253. }
  254. #endif
  255. /* --------------------------------------------------------------------------------------------- */
  256. char *
  257. history_show (GList ** history, Widget * widget)
  258. {
  259. GList *z, *hlist = NULL, *hi;
  260. size_t maxlen, i, count = 0;
  261. char *r = NULL;
  262. Dlg_head *query_dlg;
  263. WListbox *query_list;
  264. history_dlg_data hist_data;
  265. if (*history == NULL)
  266. return NULL;
  267. maxlen = str_term_width1 (_("History")) + 2;
  268. for (z = *history; z != NULL; z = g_list_previous (z))
  269. {
  270. WLEntry *entry;
  271. i = str_term_width1 ((char *) z->data);
  272. maxlen = max (maxlen, i);
  273. count++;
  274. entry = g_new0 (WLEntry, 1);
  275. /* history is being reverted here */
  276. entry->text = g_strdup ((char *) z->data);
  277. hlist = g_list_prepend (hlist, entry);
  278. }
  279. hist_data.widget = widget;
  280. hist_data.count = count;
  281. hist_data.maxlen = maxlen;
  282. query_dlg =
  283. create_dlg (TRUE, 0, 0, 4, 4, dialog_colors, history_dlg_callback,
  284. "[History-query]", _("History"), DLG_COMPACT);
  285. query_dlg->data = &hist_data;
  286. query_list = listbox_new (1, 1, 2, 2, TRUE, NULL);
  287. /* this call makes list stick to all sides of dialog, effectively make
  288. it be resized with dialog */
  289. add_widget_autopos (query_dlg, query_list, WPOS_KEEP_ALL);
  290. /* to avoid diplicating of (calculating sizes in two places)
  291. code, call dlg_hist_callback function here, to set dialog and
  292. controls positions.
  293. The main idea - create 4x4 dialog and add 2x2 list in
  294. center of it, and let dialog function resize it to needed
  295. size. */
  296. history_dlg_callback (query_dlg, NULL, DLG_RESIZE, 0, NULL);
  297. if (query_dlg->y < widget->y)
  298. {
  299. /* draw list entries from bottom upto top */
  300. listbox_set_list (query_list, hlist);
  301. listbox_select_last (query_list);
  302. }
  303. else
  304. {
  305. /* draw list entries from top downto bottom */
  306. /* revert history direction */
  307. hlist = g_list_reverse (hlist);
  308. listbox_set_list (query_list, hlist);
  309. }
  310. if (run_dlg (query_dlg) != B_CANCEL)
  311. {
  312. char *q;
  313. listbox_get_current (query_list, &q, NULL);
  314. r = g_strdup (q);
  315. }
  316. /* get modified history from dialog */
  317. z = NULL;
  318. for (hi = query_list->list; hi != NULL; hi = g_list_next (hi))
  319. {
  320. WLEntry *entry;
  321. entry = (WLEntry *) hi->data;
  322. /* history is being reverted here again */
  323. z = g_list_prepend (z, entry->text);
  324. entry->text = NULL;
  325. }
  326. /* restore history direction */
  327. if (query_dlg->y < widget->y)
  328. z = g_list_reverse (z);
  329. destroy_dlg (query_dlg);
  330. g_list_foreach (*history, (GFunc) g_free, NULL);
  331. g_list_free (*history);
  332. *history = g_list_last (z);
  333. return r;
  334. }
  335. /* --------------------------------------------------------------------------------------------- */