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)
135 pathl = path_from_utf8(path);
136 ssi = secure_open(pathl);
140 log_printf(_("Unable to write history lists to: %s\n"), path);
144 secure_fprintf(ssi, "#History lists\n\n");
146 list = g_list_last(history_list);
147 while (list && secsave_errno == SS_ERR_NONE)
155 secure_fprintf(ssi, "[%s]\n", hd->key);
157 /* save them inverted (oldest to newest)
158 * so that when reading they are added correctly
160 work = g_list_last(hd->list);
161 list_count = g_list_position(hd->list, g_list_last(hd->list)) + 1;
162 while (work && secsave_errno == SS_ERR_NONE)
164 if (!(strcmp(hd->key, "path_list") == 0 && list_count > options->open_recent_list_maxsize))
166 secure_fprintf(ssi, "\"%s\"\n", (gchar *)work->data);
171 secure_fputc(ssi, '\n');
174 secure_fprintf(ssi, "#end\n");
176 return (secure_close(ssi) == 0);
179 static void history_list_free(HistoryData *hd)
196 static HistoryData *history_list_find_by_key(const gchar *key)
198 GList *work = history_list;
200 if (!key) return NULL;
204 HistoryData *hd = work->data;
205 if (strcmp(hd->key, key) == 0) return hd;
211 const gchar *history_list_find_last_path_by_key(const gchar *key)
215 hd = history_list_find_by_key(key);
216 if (!hd || !hd->list) return NULL;
218 return hd->list->data;
221 void history_list_free_key(const gchar *key)
224 hd = history_list_find_by_key(key);
227 history_list = g_list_remove(history_list, hd);
228 history_list_free(hd);
231 void history_list_add_to_key(const gchar *key, const gchar *path, gint max)
236 if (!key || !path) return;
238 hd = history_list_find_by_key(key);
241 hd = g_new(HistoryData, 1);
242 hd->key = g_strdup(key);
244 history_list = g_list_prepend(history_list, hd);
247 /* if already in the list, simply move it to the top */
251 gchar *buf = work->data;
253 if (strcmp(buf, path) == 0)
255 /* if not first, move it */
256 if (work != hd->list)
258 hd->list = g_list_remove(hd->list, buf);
259 hd->list = g_list_prepend(hd->list, buf);
266 hd->list = g_list_prepend(hd->list, g_strdup(path));
268 if (max == -1) max = HISTORY_DEFAULT_KEY_COUNT;
272 GList *work = hd->list;
283 while (work && len > max)
289 hd->list = g_list_delete_link(hd->list, node);
295 void history_list_item_change(const gchar *key, const gchar *oldpath, const gchar *newpath)
300 if (!oldpath) return;
301 hd = history_list_find_by_key(key);
307 gchar *buf = work->data;
308 if (strcmp(buf, oldpath) == 0)
312 work->data = g_strdup(newpath);
316 hd->list = g_list_remove(hd->list, buf);
325 void history_list_item_move(const gchar *key, const gchar *path, gint direction)
332 hd = history_list_find_by_key(key);
338 gchar *buf = work->data;
339 if (strcmp(buf, path) == 0)
343 hd->list = g_list_remove(hd->list, buf);
344 hd->list = g_list_insert(hd->list, buf, p);
352 void history_list_item_remove(const gchar *key, const gchar *path)
354 history_list_item_change(key, path, NULL);
357 GList *history_list_get_by_key(const gchar *key)
361 hd = history_list_find_by_key(key);
362 if (!hd) return NULL;
366 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */