From 29110e56811a29ccf09feac7e7c39a9836627be0 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Sun, 28 Jul 2019 15:24:00 +0300 Subject: [PATCH] Move history operation routines from lib/widget/history.c to lib/mcconfig/history.c. Signed-off-by: Andrew Borodin --- lib/mcconfig.h | 12 +++ lib/mcconfig/Makefile.am | 1 + lib/mcconfig/history.c | 219 +++++++++++++++++++++++++++++++++++++++ lib/widget/dialog.c | 1 + lib/widget/history.c | 167 +---------------------------- lib/widget/history.h | 15 +-- lib/widget/input.c | 5 +- src/editor/editcmd.c | 2 +- src/filemanager/panel.c | 6 +- src/setup.c | 2 +- src/viewer/actions_cmd.c | 3 +- 11 files changed, 246 insertions(+), 187 deletions(-) create mode 100644 lib/mcconfig/history.c diff --git a/lib/mcconfig.h b/lib/mcconfig.h index 81928de51..e34ca6570 100644 --- a/lib/mcconfig.h +++ b/lib/mcconfig.h @@ -23,6 +23,8 @@ typedef struct mc_config_t /*** global variables defined in .c file *********************************************************/ +extern int num_history_items_recorded; + /*** declarations of public functions ************************************************************/ /* mcconfig/common.c: */ @@ -101,6 +103,16 @@ vfs_path_t *mc_config_get_full_vpath (const char *config_name); gboolean mc_config_migrate_from_old_place (GError ** mcerror, char **msg); +/* mcconfig/history.h */ + +/* read history to the mc_config, but don't save config to file */ +GList *mc_config_history_get (const char *name); +/* load history from the mc_config */ +GList *mc_config_history_load (mc_config_t * cfg, const char *name); +/* save history to the mc_config, but don't save config to file */ +void mc_config_history_save (mc_config_t * cfg, const char *name, GList * h); + + /*** inline functions ****************************************************************************/ #endif /* MC__CONFIG_H */ diff --git a/lib/mcconfig/Makefile.am b/lib/mcconfig/Makefile.am index 0cabd2f8a..ad62b9132 100644 --- a/lib/mcconfig/Makefile.am +++ b/lib/mcconfig/Makefile.am @@ -4,6 +4,7 @@ noinst_LTLIBRARIES = libmcconfig.la libmcconfig_la_SOURCES = \ common.c \ get.c \ + history.c \ set.c \ paths.c diff --git a/lib/mcconfig/history.c b/lib/mcconfig/history.c new file mode 100644 index 000000000..3174fd2ab --- /dev/null +++ b/lib/mcconfig/history.c @@ -0,0 +1,219 @@ +/* + Widgets for the Midnight Commander + + Copyright (C) 1994-2019 + Free Software Foundation, Inc. + + Authors: + Radek Doulik, 1994, 1995 + Miguel de Icaza, 1994, 1995 + Jakub Jelinek, 1995 + Andrej Borsenkow, 1996 + Norbert Warmuth, 1997 + Andrew Borodin , 2009-2019 + + This file is part of the Midnight Commander. + + The Midnight Commander is free software: you can redistribute it + and/or modify it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + The Midnight Commander is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +/** \file history.c + * \brief Source: save and load history + */ + +#include + +#include +#include + +#include "lib/global.h" + +#include "lib/fileloc.h" /* MC_HISTORY_FILE */ +#include "lib/strutil.h" +#include "lib/util.h" /* list_append_unique */ + +#include "lib/mcconfig.h" + +/*** global variables ****************************************************************************/ + +/* how much history items are used */ +int num_history_items_recorded = 60; + +/*** file scope macro definitions ****************************************************************/ + +/*** file scope type declarations ****************************************************************/ + +/*** file scope variables ************************************************************************/ + +/* --------------------------------------------------------------------------------------------- */ +/*** file scope functions ************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + +/** + * Load the history from the ${XDG_CACHE_HOME}/mc/history file. + * It is called with the widgets history name and returns the GList list. + */ + +GList * +mc_config_history_get (const char *name) +{ + GList *hist = NULL; + char *profile; + mc_config_t *cfg; + + if (num_history_items_recorded == 0) /* this is how to disable */ + return NULL; + if (name == NULL || *name == '\0') + return NULL; + + profile = mc_config_get_full_path (MC_HISTORY_FILE); + cfg = mc_config_init (profile, TRUE); + + hist = mc_config_history_load (cfg, name); + + mc_config_deinit (cfg); + g_free (profile); + + return hist; +} + +/* --------------------------------------------------------------------------------------------- */ + +/** + * Load history from the mc_config + */ +GList * +mc_config_history_load (mc_config_t * cfg, const char *name) +{ + size_t i; + GList *hist = NULL; + char **keys; + size_t keys_num = 0; + GIConv conv = INVALID_CONV; + GString *buffer; + + if (name == NULL || *name == '\0') + return NULL; + + /* get number of keys */ + keys = mc_config_get_keys (cfg, name, &keys_num); + g_strfreev (keys); + + /* create charset conversion handler to convert strings + from utf-8 to system codepage */ + if (!mc_global.utf8_display) + conv = str_crt_conv_from ("UTF-8"); + + buffer = g_string_sized_new (64); + + for (i = 0; i < keys_num; i++) + { + char key[BUF_TINY]; + char *this_entry; + + g_snprintf (key, sizeof (key), "%lu", (unsigned long) i); + this_entry = mc_config_get_string_raw (cfg, name, key, ""); + + if (this_entry == NULL) + continue; + + if (conv == INVALID_CONV) + hist = list_append_unique (hist, this_entry); + else + { + g_string_set_size (buffer, 0); + if (str_convert (conv, this_entry, buffer) == ESTR_FAILURE) + hist = list_append_unique (hist, this_entry); + else + { + hist = list_append_unique (hist, g_strndup (buffer->str, buffer->len)); + g_free (this_entry); + } + } + } + + g_string_free (buffer, TRUE); + if (conv != INVALID_CONV) + str_close_conv (conv); + + /* return pointer to the last entry in the list */ + return g_list_last (hist); +} + +/* --------------------------------------------------------------------------------------------- */ + +/** + * Save history to the mc_config, but don't save config to file + */ +void +mc_config_history_save (mc_config_t * cfg, const char *name, GList * h) +{ + GIConv conv = INVALID_CONV; + GString *buffer; + int i; + + if (name == NULL || *name == '\0' || h == NULL) + return; + + /* go to end of list */ + h = g_list_last (h); + + /* go back 60 places */ + for (i = 0; (i < num_history_items_recorded - 1) && (h->prev != NULL); i++) + h = g_list_previous (h); + + if (name != NULL) + mc_config_del_group (cfg, name); + + /* create charset conversion handler to convert strings + from system codepage to UTF-8 */ + if (!mc_global.utf8_display) + conv = str_crt_conv_to ("UTF-8"); + + buffer = g_string_sized_new (64); + + /* dump history into profile */ + for (i = 0; h != NULL; h = g_list_next (h)) + { + char key[BUF_TINY]; + char *text = (char *) h->data; + + /* We shouldn't have null entries, but let's be sure */ + if (text == NULL) + continue; + + g_snprintf (key, sizeof (key), "%d", i++); + + if (conv == INVALID_CONV) + mc_config_set_string_raw (cfg, name, key, text); + else + { + g_string_set_size (buffer, 0); + if (str_convert (conv, text, buffer) == ESTR_FAILURE) + mc_config_set_string_raw (cfg, name, key, text); + else + mc_config_set_string_raw (cfg, name, key, buffer->str); + } + } + + g_string_free (buffer, TRUE); + if (conv != INVALID_CONV) + str_close_conv (conv); +} + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/widget/dialog.c b/lib/widget/dialog.c index 23359f6a9..1273b67b4 100644 --- a/lib/widget/dialog.c +++ b/lib/widget/dialog.c @@ -43,6 +43,7 @@ #include "lib/fileloc.h" /* MC_HISTORY_FILE */ #include "lib/event.h" /* mc_event_raise() */ #include "lib/util.h" /* MC_PTR_FREE */ +#include "lib/mcconfig.h" /* num_history_items_recorded */ #include "lib/widget.h" #include "lib/widget/mouse.h" diff --git a/lib/widget/history.c b/lib/widget/history.c index ae5ceb73f..e1bddf681 100644 --- a/lib/widget/history.c +++ b/lib/widget/history.c @@ -10,7 +10,7 @@ Jakub Jelinek, 1995 Andrej Borsenkow, 1996 Norbert Warmuth, 1997 - Andrew Borodin , 2009, 2010, 2011, 2012, 2013 + Andrew Borodin , 2009-2019 This file is part of the Midnight Commander. @@ -29,33 +29,23 @@ */ /** \file history.c - * \brief Source: save, load and show history + * \brief Source: show history */ #include -#include #include -#include -#include #include -#include #include "lib/global.h" #include "lib/tty/tty.h" /* LINES, COLS */ -#include "lib/mcconfig.h" /* for history loading and saving */ -#include "lib/fileloc.h" #include "lib/strutil.h" -#include "lib/util.h" /* list_append_unique */ #include "lib/widget.h" #include "lib/keybind.h" /* CK_* */ /*** global variables ****************************************************************************/ -/* how much history items are used */ -int num_history_items_recorded = 60; - /*** file scope macro definitions ****************************************************************/ #define B_VIEW (B_USER + 1) @@ -189,159 +179,6 @@ history_release_item (history_descriptor_t * hd, WLEntry * le) /*** public functions ****************************************************************************/ /* --------------------------------------------------------------------------------------------- */ -/** - * Load the history from the ${XDG_CACHE_HOME}/mc/history file. - * It is called with the widgets history name and returns the GList list. - */ - -GList * -history_get (const char *input_name) -{ - GList *hist = NULL; - char *profile; - mc_config_t *cfg; - - if (num_history_items_recorded == 0) /* this is how to disable */ - return NULL; - if ((input_name == NULL) || (*input_name == '\0')) - return NULL; - - profile = mc_config_get_full_path (MC_HISTORY_FILE); - cfg = mc_config_init (profile, TRUE); - - hist = history_load (cfg, input_name); - - mc_config_deinit (cfg); - g_free (profile); - - return hist; -} - -/* --------------------------------------------------------------------------------------------- */ - -/** - * Load history from the mc_config - */ -GList * -history_load (mc_config_t * cfg, const char *name) -{ - size_t i; - GList *hist = NULL; - char **keys; - size_t keys_num = 0; - GIConv conv = INVALID_CONV; - GString *buffer; - - if (name == NULL || *name == '\0') - return NULL; - - /* get number of keys */ - keys = mc_config_get_keys (cfg, name, &keys_num); - g_strfreev (keys); - - /* create charset conversion handler to convert strings - from utf-8 to system codepage */ - if (!mc_global.utf8_display) - conv = str_crt_conv_from ("UTF-8"); - - buffer = g_string_sized_new (64); - - for (i = 0; i < keys_num; i++) - { - char key[BUF_TINY]; - char *this_entry; - - g_snprintf (key, sizeof (key), "%lu", (unsigned long) i); - this_entry = mc_config_get_string_raw (cfg, name, key, ""); - - if (this_entry == NULL) - continue; - - if (conv == INVALID_CONV) - hist = list_append_unique (hist, this_entry); - else - { - g_string_set_size (buffer, 0); - if (str_convert (conv, this_entry, buffer) == ESTR_FAILURE) - hist = list_append_unique (hist, this_entry); - else - { - hist = list_append_unique (hist, g_strndup (buffer->str, buffer->len)); - g_free (this_entry); - } - } - } - - g_string_free (buffer, TRUE); - if (conv != INVALID_CONV) - str_close_conv (conv); - - /* return pointer to the last entry in the list */ - return g_list_last (hist); -} - -/* --------------------------------------------------------------------------------------------- */ - -/** - * Save history to the mc_config, but don't save config to file - */ -void -history_save (mc_config_t * cfg, const char *name, GList * h) -{ - GIConv conv = INVALID_CONV; - GString *buffer; - int i; - - if (name == NULL || *name == '\0' || h == NULL) - return; - - /* go to end of list */ - h = g_list_last (h); - - /* go back 60 places */ - for (i = 0; (i < num_history_items_recorded - 1) && (h->prev != NULL); i++) - h = g_list_previous (h); - - if (name != NULL) - mc_config_del_group (cfg, name); - - /* create charset conversion handler to convert strings - from system codepage to UTF-8 */ - if (!mc_global.utf8_display) - conv = str_crt_conv_to ("UTF-8"); - - buffer = g_string_sized_new (64); - /* dump history into profile */ - for (i = 0; h != NULL; h = g_list_next (h)) - { - char key[BUF_TINY]; - char *text = (char *) h->data; - - /* We shouldn't have null entries, but let's be sure */ - if (text == NULL) - continue; - - g_snprintf (key, sizeof (key), "%d", i++); - - if (conv == INVALID_CONV) - mc_config_set_string_raw (cfg, name, key, text); - else - { - g_string_set_size (buffer, 0); - if (str_convert (conv, text, buffer) == ESTR_FAILURE) - mc_config_set_string_raw (cfg, name, key, text); - else - mc_config_set_string_raw (cfg, name, key, buffer->str); - } - } - - g_string_free (buffer, TRUE); - if (conv != INVALID_CONV) - str_close_conv (conv); -} - -/* --------------------------------------------------------------------------------------------- */ - void history_descriptor_init (history_descriptor_t * hd, int y, int x, GList * history, int current) { diff --git a/lib/widget/history.h b/lib/widget/history.h index 4a8c5d668..9c4b403d1 100644 --- a/lib/widget/history.h +++ b/lib/widget/history.h @@ -1,13 +1,11 @@ /** \file lib/widget/history.h - * \brief Header: save, load and show history + * \brief Header: show history */ #ifndef MC__WIDGET_HISTORY_H #define MC__WIDGET_HISTORY_H -#include "lib/mcconfig.h" /* mc_config_t */ - /*** typedefs(not structures) and defined constants **********************************************/ /* forward declarations */ @@ -41,22 +39,11 @@ typedef struct history_descriptor_t /*** global variables defined in .c file *********************************************************/ -extern int num_history_items_recorded; - /*** declarations of public functions ************************************************************/ -/* read history to the mc_config, but don't save config to file */ -GList *history_get (const char *input_name); -/* load history from the mc_config */ -GList *history_load (mc_config_t * cfg, const char *name); -/* save history to the mc_config, but don't save config to file */ -void history_save (mc_config_t * cfg, const char *name, GList * h); - void history_descriptor_init (history_descriptor_t * hd, int y, int x, GList * history, int current); -/* for repositioning of history dialog we should pass widget to this - * function, as position of history dialog depends on widget's position */ void history_show (history_descriptor_t * hd); /*** inline functions ****************************************************************************/ diff --git a/lib/widget/input.c b/lib/widget/input.c index ce3905dd1..143e09b95 100644 --- a/lib/widget/input.c +++ b/lib/widget/input.c @@ -49,6 +49,7 @@ #include "lib/keybind.h" /* global_keymap_t */ #include "lib/widget.h" #include "lib/event.h" /* mc_event_raise() */ +#include "lib/mcconfig.h" /* mc_config_history_*() */ #include "input_complete.h" @@ -842,7 +843,7 @@ input_load_history (const gchar * event_group_name, const gchar * event_name, (void) event_group_name; (void) event_name; - in->history.list = history_load (ev->cfg, in->history.name); + in->history.list = mc_config_history_load (ev->cfg, in->history.name); in->history.current = in->history.list; if (in->init_from_history) @@ -876,7 +877,7 @@ input_save_history (const gchar * event_group_name, const gchar * event_name, push_history (in, in->buffer); if (in->history.changed) - history_save (ev->cfg, in->history.name, in->history.list); + mc_config_history_save (ev->cfg, in->history.name, in->history.list); in->history.changed = FALSE; } diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c index 99e8de5a9..24bafab0f 100644 --- a/src/editor/editcmd.c +++ b/src/editor/editcmd.c @@ -2806,7 +2806,7 @@ edit_search_cmd (WEdit * edit, gboolean again) /* find last search string in history */ GList *history; - history = history_get (MC_HISTORY_SHARED_SEARCH); + history = mc_config_history_get (MC_HISTORY_SHARED_SEARCH); if (history != NULL && history->data != NULL) { edit->last_search_string = (char *) history->data; diff --git a/src/filemanager/panel.c b/src/filemanager/panel.c index 24a17f373..d8d84d1cf 100644 --- a/src/filemanager/panel.c +++ b/src/filemanager/panel.c @@ -1433,9 +1433,9 @@ panel_load_history (const gchar * event_group_name, const gchar * event_name, if (ev->receiver == NULL || ev->receiver == WIDGET (p)) { if (ev->cfg != NULL) - p->dir_history = history_load (ev->cfg, p->hist_name); + p->dir_history = mc_config_history_load (ev->cfg, p->hist_name); else - p->dir_history = history_get (p->hist_name); + p->dir_history = mc_config_history_get (p->hist_name); directory_history_add (p, p->cwd_vpath); } @@ -1459,7 +1459,7 @@ panel_save_history (const gchar * event_group_name, const gchar * event_name, { ev_history_load_save_t *ev = (ev_history_load_save_t *) data; - history_save (ev->cfg, p->hist_name, p->dir_history); + mc_config_history_save (ev->cfg, p->hist_name, p->dir_history); } return TRUE; diff --git a/src/setup.c b/src/setup.c index 0b6f2f699..277b8363a 100644 --- a/src/setup.c +++ b/src/setup.c @@ -36,7 +36,7 @@ #include "lib/tty/tty.h" #include "lib/tty/key.h" -#include "lib/mcconfig.h" +#include "lib/mcconfig.h" /* num_history_items_recorded */ #include "lib/fileloc.h" #include "lib/timefmt.h" #include "lib/util.h" diff --git a/src/viewer/actions_cmd.c b/src/viewer/actions_cmd.c index e3bd3abeb..1ecce4e72 100644 --- a/src/viewer/actions_cmd.c +++ b/src/viewer/actions_cmd.c @@ -58,6 +58,7 @@ #include "lib/charsets.h" #endif #include "lib/event.h" /* mc_event_raise() */ +#include "lib/mcconfig.h" /* mc_config_history_get() */ #include "src/filemanager/layout.h" #include "src/filemanager/cmd.h" @@ -140,7 +141,7 @@ mcview_continue_search_cmd (WView * view) /* find last search string in history */ GList *history; - history = history_get (MC_HISTORY_SHARED_SEARCH); + history = mc_config_history_get (MC_HISTORY_SHARED_SEARCH); if (history != NULL && history->data != NULL) { view->last_search_string = (gchar *) g_strdup (history->data);