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"
27 static void update_recent_viewed_folder_image_list(const gchar *path);
31 *-----------------------------------------------------------------------------
32 * Implements a history chain. Used by the Back and Forward toolbar buttons.
33 * Selecting any folder appends the path to the end of the chain.
34 * Pressing the Back and Forward buttons moves along the chain, but does
35 * not make additions to the chain.
36 * The chain always increases and is deleted at the end of the session
38 *-----------------------------------------------------------------------------
41 static GList *history_chain = NULL;
42 static guint chain_index = G_MAXUINT;
43 static gboolean nav_button = FALSE; /** Used to prevent the nav buttons making entries to the chain **/
45 const gchar *history_chain_back()
49 chain_index = chain_index > 0 ? chain_index - 1 : 0;
51 return g_list_nth_data(history_chain, chain_index);
54 const gchar *history_chain_forward()
57 guint last = g_list_length(history_chain) - 1;
59 chain_index = chain_index < last ? chain_index + 1 : last;
61 return g_list_nth_data(history_chain, chain_index);
65 * @brief Appends a path to the history chain
66 * @param path Path selected
68 * Each time the user selects a new path it is appended to the chain
69 * except when it is identical to the current last entry
70 * The pointer is always moved to the end of the chain
72 void history_chain_append_end(const gchar *path)
78 if(chain_index == G_MAXUINT)
80 history_chain = g_list_append (history_chain, g_strdup(path));
85 work = g_list_last(history_chain);
86 if (g_strcmp0(work->data , path) != 0)
88 history_chain = g_list_append (history_chain, g_strdup(path));
89 chain_index = g_list_length(history_chain) - 1;
90 DEBUG_3("%d %s", chain_index, path);
94 chain_index = g_list_length(history_chain) - 1;
106 *-----------------------------------------------------------------------------
107 * Implements an image history chain. Whenever an image is displayed it is
108 * appended to a chain.
109 * Pressing the Image Back and Image Forward buttons moves along the chain,
110 * but does not make additions to the chain.
111 * The chain always increases and is deleted at the end of the session
113 *-----------------------------------------------------------------------------
115 static GList *image_chain = NULL;
116 static guint image_chain_index = G_MAXUINT;
117 static gboolean image_nav_button = FALSE; /** Used to prevent the nav buttons making entries to the chain **/
118 const gchar *image_chain_back()
120 image_nav_button = TRUE;
122 image_chain_index = image_chain_index > 0 ? image_chain_index - 1 : 0;
124 return g_list_nth_data(image_chain, image_chain_index);
127 const gchar *image_chain_forward()
129 image_nav_button= TRUE;
130 guint last = g_list_length(image_chain) - 1;
132 image_chain_index = image_chain_index < last ? image_chain_index + 1 : last;
134 return g_list_nth_data(image_chain, image_chain_index);
138 * @brief Appends a path to the image history chain
139 * @param path Image path selected
141 * Each time the user selects a new image it is appended to the chain
142 * except when it is identical to the current last entry
143 * The pointer is always moved to the end of the chain
145 * Updates the recent viewed image_list
147 void image_chain_append_end(const gchar *path)
151 if (!image_nav_button)
153 if(image_chain_index == G_MAXUINT)
155 image_chain = g_list_append(image_chain, g_strdup(path));
156 image_chain_index = 0;
160 work = g_list_last(image_chain);
161 if (g_strcmp0(work->data , path) != 0)
163 image_chain = g_list_append(image_chain, g_strdup(path));
164 image_chain_index = g_list_length(image_chain) - 1;
165 DEBUG_3("%d %s", image_chain_index, path);
169 image_chain_index = g_list_length(image_chain) - 1;
173 update_recent_viewed_folder_image_list(path);
177 image_nav_button = FALSE;
182 *-----------------------------------------------------------------------------
184 *-----------------------------------------------------------------------------
187 typedef struct _HistoryData HistoryData;
194 static GList *history_list = NULL;
197 static gchar *quoted_from_text(const gchar *text)
201 gint l = strlen(text);
203 if (l == 0) return NULL;
205 while (c < l && text[c] !='"') c++;
212 while (e < l && text[e] !='"') e++;
217 return g_strndup(ptr, e - c);
224 gboolean history_list_load(const gchar *path)
231 pathl = path_from_utf8(path);
232 f = fopen(pathl, "r");
234 if (!f) return FALSE;
236 /* first line must start with History comment */
237 if (!fgets(s_buf, sizeof(s_buf), f) ||
238 strncmp(s_buf, "#History", 8) != 0)
244 while (fgets(s_buf, sizeof(s_buf), f))
246 if (s_buf[0]=='#') continue;
254 while (ptr[c] != ']' && ptr[c] != '\n' && ptr[c] != '\0') c++;
257 key = g_strndup(ptr, c);
263 value = quoted_from_text(s_buf);
266 history_list_add_to_key(key, value, 0);
279 gboolean history_list_save(const gchar *path)
286 pathl = path_from_utf8(path);
287 ssi = secure_open(pathl);
291 log_printf(_("Unable to write history lists to: %s\n"), path);
295 secure_fprintf(ssi, "#History lists\n\n");
297 list = g_list_last(history_list);
298 while (list && secsave_errno == SS_ERR_NONE)
306 secure_fprintf(ssi, "[%s]\n", hd->key);
308 /* save them inverted (oldest to newest)
309 * so that when reading they are added correctly
311 work = g_list_last(hd->list);
312 list_count = g_list_position(hd->list, g_list_last(hd->list)) + 1;
313 while (work && secsave_errno == SS_ERR_NONE)
315 if ((!(strcmp(hd->key, "path_list") == 0 && list_count > options->open_recent_list_maxsize))
317 (!(strcmp(hd->key, "recent") == 0 && (!isfile(work->data))))
319 (!(strcmp(hd->key, "image_list") == 0 && list_count > options->recent_folder_image_list_maxsize)))
321 secure_fprintf(ssi, "\"%s\"\n", (gchar *)work->data);
326 secure_fputc(ssi, '\n');
329 secure_fprintf(ssi, "#end\n");
331 return (secure_close(ssi) == 0);
334 static void history_list_free(HistoryData *hd)
351 static HistoryData *history_list_find_by_key(const gchar *key)
353 GList *work = history_list;
355 if (!key) return NULL;
359 HistoryData *hd = work->data;
360 if (strcmp(hd->key, key) == 0) return hd;
366 const gchar *history_list_find_last_path_by_key(const gchar *key)
370 hd = history_list_find_by_key(key);
371 if (!hd || !hd->list) return NULL;
373 return hd->list->data;
376 void history_list_free_key(const gchar *key)
379 hd = history_list_find_by_key(key);
382 history_list = g_list_remove(history_list, hd);
383 history_list_free(hd);
386 void history_list_add_to_key(const gchar *key, const gchar *path, gint max)
391 if (!key || !path) return;
393 hd = history_list_find_by_key(key);
396 hd = g_new(HistoryData, 1);
397 hd->key = g_strdup(key);
399 history_list = g_list_prepend(history_list, hd);
402 /* if already in the list, simply move it to the top */
406 gchar *buf = work->data;
408 if (strcmp(buf, path) == 0)
410 /* if not first, move it */
411 if (work != hd->list)
413 hd->list = g_list_remove(hd->list, buf);
414 hd->list = g_list_prepend(hd->list, buf);
421 hd->list = g_list_prepend(hd->list, g_strdup(path));
423 if (max == -1) max = options->open_recent_list_maxsize;
427 GList *work = hd->list;
438 while (work && len > max)
444 hd->list = g_list_delete_link(hd->list, node);
450 void history_list_item_change(const gchar *key, const gchar *oldpath, const gchar *newpath)
455 if (!oldpath) return;
456 hd = history_list_find_by_key(key);
462 gchar *buf = work->data;
463 if (strcmp(buf, oldpath) == 0)
467 work->data = g_strdup(newpath);
471 hd->list = g_list_remove(hd->list, buf);
480 void history_list_item_move(const gchar *key, const gchar *path, gint direction)
487 hd = history_list_find_by_key(key);
493 gchar *buf = work->data;
494 if (strcmp(buf, path) == 0)
498 hd->list = g_list_remove(hd->list, buf);
499 hd->list = g_list_insert(hd->list, buf, p);
507 void history_list_item_remove(const gchar *key, const gchar *path)
509 history_list_item_change(key, path, NULL);
512 GList *history_list_get_by_key(const gchar *key)
516 hd = history_list_find_by_key(key);
517 if (!hd) return NULL;
523 * @brief Get image last viewed in a folder
524 * @param path Must be a folder
525 * @returns Last image viewed in folder or NULL
527 * Returned string should be freed
529 gchar *get_recent_viewed_folder_image(gchar *path)
536 if (options->recent_folder_image_list_maxsize == 0)
541 hd = history_list_find_by_key("image_list");
545 hd = g_new(HistoryData, 1);
546 hd->key = g_strdup("image_list");
548 history_list = g_list_prepend(history_list, hd);
555 dirname = g_path_get_dirname(work->data);
557 if (g_strcmp0(dirname, path) == 0)
559 if (isfile(work->data))
561 ret = g_strdup(work->data);
574 static void update_recent_viewed_folder_image_list(const gchar *path)
578 gchar *image_dir = NULL;
579 gchar *list_dir = NULL;
580 gboolean found = FALSE;
582 if (options->recent_folder_image_list_maxsize == 0)
587 image_dir = g_path_get_dirname(path);
588 hd = history_list_find_by_key("image_list");
591 hd = g_new(HistoryData, 1);
592 hd->key = g_strdup("image_list");
594 history_list = g_list_prepend(history_list, hd);
601 list_dir = g_path_get_dirname(work->data);
603 /* If folder already in list, update and move to start of list */
604 if (g_strcmp0(list_dir, image_dir) == 0)
607 work->data = g_strdup(path);
608 hd->list = g_list_remove_link(hd->list, work);
609 hd->list = g_list_concat(work, hd->list);
622 hd->list = g_list_prepend(hd->list, g_strdup(path));
625 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */