2 * Copyright (C) 2008 - 2016 The Geeqie Team
4 * Authors: John Ellis, Vladimir Nadvornik, Laurent Monin
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "history_list.h"
24 #include "secure_save.h"
25 #include "ui_fileops.h"
28 *-----------------------------------------------------------------------------
30 *-----------------------------------------------------------------------------
33 #define HISTORY_DEFAULT_KEY_COUNT 16
36 typedef struct _HistoryData HistoryData;
43 static GList *history_list = NULL;
46 static gchar *quoted_from_text(const gchar *text)
50 gint l = strlen(text);
52 if (l == 0) return NULL;
54 while (c < l && text[c] !='"') c++;
61 while (e < l && text[e] !='"') e++;
66 return g_strndup(ptr, e - c);
73 gboolean history_list_load(const gchar *path)
80 pathl = path_from_utf8(path);
81 f = fopen(pathl, "r");
85 /* first line must start with History comment */
86 if (!fgets(s_buf, sizeof(s_buf), f) ||
87 strncmp(s_buf, "#History", 8) != 0)
93 while (fgets(s_buf, sizeof(s_buf), f))
95 if (s_buf[0]=='#') continue;
103 while (ptr[c] != ']' && ptr[c] != '\n' && ptr[c] != '\0') c++;
106 key = g_strndup(ptr, c);
112 value = quoted_from_text(s_buf);
115 history_list_add_to_key(key, value, 0);
128 gboolean history_list_save(const gchar *path)
134 pathl = path_from_utf8(path);
135 ssi = secure_open(pathl);
139 log_printf(_("Unable to write history lists to: %s\n"), path);
143 secure_fprintf(ssi, "#History lists\n\n");
145 list = g_list_last(history_list);
146 while (list && secsave_errno == SS_ERR_NONE)
154 secure_fprintf(ssi, "[%s]\n", hd->key);
156 /* save them inverted (oldest to newest)
157 * so that when reading they are added correctly
159 work = g_list_last(hd->list);
160 while (work && secsave_errno == SS_ERR_NONE)
162 secure_fprintf(ssi, "\"%s\"\n", (gchar *)work->data);
165 secure_fputc(ssi, '\n');
168 secure_fprintf(ssi, "#end\n");
170 return (secure_close(ssi) == 0);
173 static void history_list_free(HistoryData *hd)
190 static HistoryData *history_list_find_by_key(const gchar *key)
192 GList *work = history_list;
194 if (!key) return NULL;
198 HistoryData *hd = work->data;
199 if (strcmp(hd->key, key) == 0) return hd;
205 const gchar *history_list_find_last_path_by_key(const gchar *key)
209 hd = history_list_find_by_key(key);
210 if (!hd || !hd->list) return NULL;
212 return hd->list->data;
215 void history_list_free_key(const gchar *key)
218 hd = history_list_find_by_key(key);
221 history_list = g_list_remove(history_list, hd);
222 history_list_free(hd);
225 void history_list_add_to_key(const gchar *key, const gchar *path, gint max)
230 if (!key || !path) return;
232 hd = history_list_find_by_key(key);
235 hd = g_new(HistoryData, 1);
236 hd->key = g_strdup(key);
238 history_list = g_list_prepend(history_list, hd);
241 /* if already in the list, simply move it to the top */
245 gchar *buf = work->data;
247 if (strcmp(buf, path) == 0)
249 /* if not first, move it */
250 if (work != hd->list)
252 hd->list = g_list_remove(hd->list, buf);
253 hd->list = g_list_prepend(hd->list, buf);
260 hd->list = g_list_prepend(hd->list, g_strdup(path));
262 if (max == -1) max = HISTORY_DEFAULT_KEY_COUNT;
266 GList *work = hd->list;
277 while (work && len > max)
283 hd->list = g_list_delete_link(hd->list, node);
289 void history_list_item_change(const gchar *key, const gchar *oldpath, const gchar *newpath)
294 if (!oldpath) return;
295 hd = history_list_find_by_key(key);
301 gchar *buf = work->data;
302 if (strcmp(buf, oldpath) == 0)
306 work->data = g_strdup(newpath);
310 hd->list = g_list_remove(hd->list, buf);
319 void history_list_item_move(const gchar *key, const gchar *path, gint direction)
326 hd = history_list_find_by_key(key);
332 gchar *buf = work->data;
333 if (strcmp(buf, path) == 0)
337 hd->list = g_list_remove(hd->list, buf);
338 hd->list = g_list_insert(hd->list, buf, p);
346 void history_list_item_remove(const gchar *key, const gchar *path)
348 history_list_item_change(key, path, NULL);
351 GList *history_list_get_by_key(const gchar *key)
355 hd = history_list_find_by_key(key);
356 if (!hd) return NULL;
360 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */