Move history operation routines from lib/widget/history.c to lib/mcconfig/history.c.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2019-07-28 15:24:00 +03:00
parent 0efb799db3
commit 29110e5681
11 changed files with 246 additions and 187 deletions

View File

@ -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 */

View File

@ -4,6 +4,7 @@ noinst_LTLIBRARIES = libmcconfig.la
libmcconfig_la_SOURCES = \
common.c \
get.c \
history.c \
set.c \
paths.c

219
lib/mcconfig/history.c Normal file
View File

@ -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 <aborodin@vmail.ru>, 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 <http://www.gnu.org/licenses/>.
*/
/** \file history.c
* \brief Source: save and load history
*/
#include <config.h>
#include <stdlib.h>
#include <sys/types.h>
#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);
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -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"

View File

@ -10,7 +10,7 @@
Jakub Jelinek, 1995
Andrej Borsenkow, 1996
Norbert Warmuth, 1997
Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2011, 2012, 2013
Andrew Borodin <aborodin@vmail.ru>, 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 <config.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#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)
{

View File

@ -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 ****************************************************************************/

View File

@ -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;
}

View File

@ -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;

View File

@ -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;

View File

@ -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"

View File

@ -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);