root/lib/mcconfig/history.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. mc_config_history_get
  2. mc_config_history_load
  3. mc_config_history_save

   1 /*
   2    Widgets for the Midnight Commander
   3 
   4    Copyright (C) 1994-2019
   5    Free Software Foundation, Inc.
   6 
   7    Authors:
   8    Radek Doulik, 1994, 1995
   9    Miguel de Icaza, 1994, 1995
  10    Jakub Jelinek, 1995
  11    Andrej Borsenkow, 1996
  12    Norbert Warmuth, 1997
  13    Andrew Borodin <aborodin@vmail.ru>, 2009-2019
  14 
  15    This file is part of the Midnight Commander.
  16 
  17    The Midnight Commander is free software: you can redistribute it
  18    and/or modify it under the terms of the GNU General Public License as
  19    published by the Free Software Foundation, either version 3 of the License,
  20    or (at your option) any later version.
  21 
  22    The Midnight Commander is distributed in the hope that it will be useful,
  23    but WITHOUT ANY WARRANTY; without even the implied warranty of
  24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  25    GNU General Public License for more details.
  26 
  27    You should have received a copy of the GNU General Public License
  28    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  29  */
  30 
  31 /** \file history.c
  32  *  \brief Source: save and load history
  33  */
  34 
  35 #include <config.h>
  36 
  37 #include <stdlib.h>
  38 #include <sys/types.h>
  39 
  40 #include "lib/global.h"
  41 
  42 #include "lib/fileloc.h"        /* MC_HISTORY_FILE */
  43 #include "lib/strutil.h"
  44 #include "lib/util.h"           /* list_append_unique */
  45 
  46 #include "lib/mcconfig.h"
  47 
  48 /*** global variables ****************************************************************************/
  49 
  50 /* how much history items are used */
  51 int num_history_items_recorded = 60;
  52 
  53 /*** file scope macro definitions ****************************************************************/
  54 
  55 /*** file scope type declarations ****************************************************************/
  56 
  57 /*** file scope variables ************************************************************************/
  58 
  59 /* --------------------------------------------------------------------------------------------- */
  60 /*** file scope functions ************************************************************************/
  61 /* --------------------------------------------------------------------------------------------- */
  62 
  63 /* --------------------------------------------------------------------------------------------- */
  64 /*** public functions ****************************************************************************/
  65 /* --------------------------------------------------------------------------------------------- */
  66 
  67 /**
  68  * Load the history from the ${XDG_CACHE_HOME}/mc/history file.
  69  * It is called with the widgets history name and returns the GList list.
  70  */
  71 
  72 GList *
  73 mc_config_history_get (const char *name)
     /* [previous][next][first][last][top][bottom][index][help]  */
  74 {
  75     GList *hist = NULL;
  76     char *profile;
  77     mc_config_t *cfg;
  78 
  79     if (num_history_items_recorded == 0)        /* this is how to disable */
  80         return NULL;
  81     if (name == NULL || *name == '\0')
  82         return NULL;
  83 
  84     profile = mc_config_get_full_path (MC_HISTORY_FILE);
  85     cfg = mc_config_init (profile, TRUE);
  86 
  87     hist = mc_config_history_load (cfg, name);
  88 
  89     mc_config_deinit (cfg);
  90     g_free (profile);
  91 
  92     return hist;
  93 }
  94 
  95 /* --------------------------------------------------------------------------------------------- */
  96 
  97 /**
  98  * Load history from the mc_config
  99  */
 100 GList *
 101 mc_config_history_load (mc_config_t * cfg, const char *name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 102 {
 103     size_t i;
 104     GList *hist = NULL;
 105     char **keys;
 106     size_t keys_num = 0;
 107     GIConv conv = INVALID_CONV;
 108     GString *buffer;
 109 
 110     if (name == NULL || *name == '\0')
 111         return NULL;
 112 
 113     /* get number of keys */
 114     keys = mc_config_get_keys (cfg, name, &keys_num);
 115     g_strfreev (keys);
 116 
 117     /* create charset conversion handler to convert strings
 118        from utf-8 to system codepage */
 119     if (!mc_global.utf8_display)
 120         conv = str_crt_conv_from ("UTF-8");
 121 
 122     buffer = g_string_sized_new (64);
 123 
 124     for (i = 0; i < keys_num; i++)
 125     {
 126         char key[BUF_TINY];
 127         char *this_entry;
 128 
 129         g_snprintf (key, sizeof (key), "%lu", (unsigned long) i);
 130         this_entry = mc_config_get_string_raw (cfg, name, key, "");
 131 
 132         if (this_entry == NULL)
 133             continue;
 134 
 135         if (conv == INVALID_CONV)
 136             hist = list_append_unique (hist, this_entry);
 137         else
 138         {
 139             g_string_set_size (buffer, 0);
 140             if (str_convert (conv, this_entry, buffer) == ESTR_FAILURE)
 141                 hist = list_append_unique (hist, this_entry);
 142             else
 143             {
 144                 hist = list_append_unique (hist, g_strndup (buffer->str, buffer->len));
 145                 g_free (this_entry);
 146             }
 147         }
 148     }
 149 
 150     g_string_free (buffer, TRUE);
 151     if (conv != INVALID_CONV)
 152         str_close_conv (conv);
 153 
 154     /* return pointer to the last entry in the list */
 155     return g_list_last (hist);
 156 }
 157 
 158 /* --------------------------------------------------------------------------------------------- */
 159 
 160 /**
 161   * Save history to the mc_config, but don't save config to file
 162   */
 163 void
 164 mc_config_history_save (mc_config_t * cfg, const char *name, GList * h)
     /* [previous][next][first][last][top][bottom][index][help]  */
 165 {
 166     GIConv conv = INVALID_CONV;
 167     GString *buffer;
 168     int i;
 169 
 170     if (name == NULL || *name == '\0' || h == NULL)
 171         return;
 172 
 173     /* go to end of list */
 174     h = g_list_last (h);
 175 
 176     /* go back 60 places */
 177     for (i = 0; (i < num_history_items_recorded - 1) && (h->prev != NULL); i++)
 178         h = g_list_previous (h);
 179 
 180     if (name != NULL)
 181         mc_config_del_group (cfg, name);
 182 
 183     /* create charset conversion handler to convert strings
 184        from system codepage to UTF-8 */
 185     if (!mc_global.utf8_display)
 186         conv = str_crt_conv_to ("UTF-8");
 187 
 188     buffer = g_string_sized_new (64);
 189 
 190     /* dump history into profile */
 191     for (i = 0; h != NULL; h = g_list_next (h))
 192     {
 193         char key[BUF_TINY];
 194         char *text = (char *) h->data;
 195 
 196         /* We shouldn't have null entries, but let's be sure */
 197         if (text == NULL)
 198             continue;
 199 
 200         g_snprintf (key, sizeof (key), "%d", i++);
 201 
 202         if (conv == INVALID_CONV)
 203             mc_config_set_string_raw (cfg, name, key, text);
 204         else
 205         {
 206             g_string_set_size (buffer, 0);
 207             if (str_convert (conv, text, buffer) == ESTR_FAILURE)
 208                 mc_config_set_string_raw (cfg, name, key, text);
 209             else
 210                 mc_config_set_string_raw (cfg, name, key, buffer->str);
 211         }
 212     }
 213 
 214     g_string_free (buffer, TRUE);
 215     if (conv != INVALID_CONV)
 216         str_close_conv (conv);
 217 }
 218 
 219 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */