3 * Copyright (C) 2008 - 2012 The Geeqie Team
5 * Authors: John Ellis, Vladimir Nadvornik, Laurent Monin
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
14 #include "history_list.h"
16 #include "secure_save.h"
17 #include "ui_fileops.h"
20 *-----------------------------------------------------------------------------
22 *-----------------------------------------------------------------------------
25 #define HISTORY_DEFAULT_KEY_COUNT 16
28 typedef struct _HistoryData HistoryData;
35 static GList *history_list = NULL;
38 static gchar *quoted_from_text(const gchar *text)
42 gint l = strlen(text);
44 if (l == 0) return NULL;
46 while (c < l && text[c] !='"') c++;
53 while (e < l && text[e] !='"') e++;
58 return g_strndup(ptr, e - c);
65 gboolean history_list_load(const gchar *path)
72 pathl = path_from_utf8(path);
73 f = fopen(pathl, "r");
77 /* first line must start with History comment */
78 if (!fgets(s_buf, sizeof(s_buf), f) ||
79 strncmp(s_buf, "#History", 8) != 0)
85 while (fgets(s_buf, sizeof(s_buf), f))
87 if (s_buf[0]=='#') continue;
95 while (ptr[c] != ']' && ptr[c] != '\n' && ptr[c] != '\0') c++;
98 key = g_strndup(ptr, c);
104 value = quoted_from_text(s_buf);
107 history_list_add_to_key(key, value, 0);
120 gboolean history_list_save(const gchar *path)
126 pathl = path_from_utf8(path);
127 ssi = secure_open(pathl);
131 log_printf(_("Unable to write history lists to: %s\n"), path);
135 secure_fprintf(ssi, "#History lists\n\n");
137 list = g_list_last(history_list);
138 while (list && secsave_errno == SS_ERR_NONE)
146 secure_fprintf(ssi, "[%s]\n", hd->key);
148 /* save them inverted (oldest to newest)
149 * so that when reading they are added correctly
151 work = g_list_last(hd->list);
152 while (work && secsave_errno == SS_ERR_NONE)
154 secure_fprintf(ssi, "\"%s\"\n", (gchar *)work->data);
157 secure_fputc(ssi, '\n');
160 secure_fprintf(ssi, "#end\n");
162 return (secure_close(ssi) == 0);
165 static void history_list_free(HistoryData *hd)
182 static HistoryData *history_list_find_by_key(const gchar *key)
184 GList *work = history_list;
186 if (!key) return NULL;
190 HistoryData *hd = work->data;
191 if (strcmp(hd->key, key) == 0) return hd;
197 const gchar *history_list_find_last_path_by_key(const gchar *key)
201 hd = history_list_find_by_key(key);
202 if (!hd || !hd->list) return NULL;
204 return hd->list->data;
207 void history_list_free_key(const gchar *key)
210 hd = history_list_find_by_key(key);
213 history_list = g_list_remove(history_list, hd);
214 history_list_free(hd);
217 void history_list_add_to_key(const gchar *key, const gchar *path, gint max)
222 if (!key || !path) return;
224 hd = history_list_find_by_key(key);
227 hd = g_new(HistoryData, 1);
228 hd->key = g_strdup(key);
230 history_list = g_list_prepend(history_list, hd);
233 /* if already in the list, simply move it to the top */
237 gchar *buf = work->data;
239 if (strcmp(buf, path) == 0)
241 /* if not first, move it */
242 if (work != hd->list)
244 hd->list = g_list_remove(hd->list, buf);
245 hd->list = g_list_prepend(hd->list, buf);
252 hd->list = g_list_prepend(hd->list, g_strdup(path));
254 if (max == -1) max = HISTORY_DEFAULT_KEY_COUNT;
258 GList *work = hd->list;
269 while (work && len > max)
275 hd->list = g_list_delete_link(hd->list, node);
281 void history_list_item_change(const gchar *key, const gchar *oldpath, const gchar *newpath)
286 if (!oldpath) return;
287 hd = history_list_find_by_key(key);
293 gchar *buf = work->data;
294 if (strcmp(buf, oldpath) == 0)
298 work->data = g_strdup(newpath);
302 hd->list = g_list_remove(hd->list, buf);
311 void history_list_item_move(const gchar *key, const gchar *path, gint direction)
318 hd = history_list_find_by_key(key);
324 gchar *buf = work->data;
325 if (strcmp(buf, path) == 0)
329 hd->list = g_list_remove(hd->list, buf);
330 hd->list = g_list_insert(hd->list, buf, p);
338 void history_list_item_remove(const gchar *key, const gchar *path)
340 history_list_item_change(key, path, NULL);
343 GList *history_list_get_by_key(const gchar *key)
347 hd = history_list_find_by_key(key);
348 if (!hd) return NULL;
352 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */