history.c 11 KB

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