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.
21 #include "history-list.h"
28 #include "secure-save.h"
29 #include "ui-fileops.h"
31 static void update_recent_viewed_folder_image_list(const gchar *path);
35 *-----------------------------------------------------------------------------
36 * Implements a history chain. Used by the Back and Forward toolbar buttons.
37 * Selecting any folder appends the path to the end of the chain.
38 * Pressing the Back and Forward buttons moves along the chain, but does
39 * not make additions to the chain.
40 * The chain always increases and is deleted at the end of the session
42 *-----------------------------------------------------------------------------
45 static GList *history_chain = nullptr;
46 static guint chain_index = G_MAXUINT;
47 static gboolean nav_button = FALSE; /** Used to prevent the nav buttons making entries to the chain **/
49 const gchar *history_chain_back()
53 chain_index = chain_index > 0 ? chain_index - 1 : 0;
55 return static_cast<const gchar *>(g_list_nth_data(history_chain, chain_index));
58 const gchar *history_chain_forward()
61 guint last = g_list_length(history_chain) - 1;
63 chain_index = chain_index < last ? chain_index + 1 : last;
65 return static_cast<const gchar *>(g_list_nth_data(history_chain, chain_index));
69 * @brief Appends a path to the history chain
70 * @param path Path selected
72 * Each time the user selects a new path it is appended to the chain
73 * except when it is identical to the current last entry
74 * The pointer is always moved to the end of the chain
76 void history_chain_append_end(const gchar *path)
82 if(chain_index == G_MAXUINT)
84 history_chain = g_list_append (history_chain, g_strdup(path));
89 work = g_list_last(history_chain);
90 if (g_strcmp0(static_cast<const gchar *>(work->data), path) != 0)
92 history_chain = g_list_append (history_chain, g_strdup(path));
93 chain_index = g_list_length(history_chain) - 1;
94 DEBUG_3("%d %s", chain_index, path);
98 chain_index = g_list_length(history_chain) - 1;
110 *-----------------------------------------------------------------------------
111 * Implements an image history chain. Whenever an image is displayed it is
112 * appended to a chain.
113 * Pressing the Image Back and Image Forward buttons moves along the chain,
114 * but does not make additions to the chain.
115 * The chain always increases and is deleted at the end of the session
117 *-----------------------------------------------------------------------------
119 static GList *image_chain = nullptr;
120 static guint image_chain_index = G_MAXUINT;
121 static gboolean image_nav_button = FALSE; /** Used to prevent the nav buttons making entries to the chain **/
122 const gchar *image_chain_back()
124 image_nav_button = TRUE;
126 image_chain_index = image_chain_index > 0 ? image_chain_index - 1 : 0;
128 return static_cast<const gchar *>(g_list_nth_data(image_chain, image_chain_index));
131 const gchar *image_chain_forward()
133 image_nav_button= TRUE;
134 guint last = g_list_length(image_chain) - 1;
136 image_chain_index = image_chain_index < last ? image_chain_index + 1 : last;
138 return static_cast<const gchar *>(g_list_nth_data(image_chain, image_chain_index));
142 * @brief Appends a path to the image history chain
143 * @param path Image path selected
145 * Each time the user selects a new image it is appended to the chain
146 * except when it is identical to the current last entry
147 * The pointer is always moved to the end of the chain
149 * Updates the recent viewed image_list
151 void image_chain_append_end(const gchar *path)
155 if (!image_nav_button)
157 if(image_chain_index == G_MAXUINT)
159 image_chain = g_list_append(image_chain, g_strdup(path));
160 image_chain_index = 0;
164 work = g_list_last(image_chain);
165 if (g_strcmp0(static_cast<const gchar *>(work->data) , path) != 0)
167 image_chain = g_list_append(image_chain, g_strdup(path));
168 image_chain_index = g_list_length(image_chain) - 1;
169 DEBUG_3("%d %s", image_chain_index, path);
173 image_chain_index = g_list_length(image_chain) - 1;
177 update_recent_viewed_folder_image_list(path);
181 image_nav_button = FALSE;
186 *-----------------------------------------------------------------------------
188 *-----------------------------------------------------------------------------
197 static GList *history_list = nullptr;
200 static gchar *quoted_from_text(const gchar *text)
204 gint l = strlen(text);
206 if (l == 0) return nullptr;
208 while (c < l && text[c] !='"') c++;
215 while (e < l && text[e] !='"') e++;
220 return g_strndup(ptr, e - c);
227 gboolean history_list_load(const gchar *path)
230 gchar *key = nullptr;
234 pathl = path_from_utf8(path);
235 f = fopen(pathl, "r");
237 if (!f) return FALSE;
239 /* first line must start with History comment */
240 if (!fgets(s_buf, sizeof(s_buf), f) ||
241 strncmp(s_buf, "#History", 8) != 0)
247 while (fgets(s_buf, sizeof(s_buf), f))
249 if (s_buf[0]=='#') continue;
257 while (ptr[c] != ']' && ptr[c] != '\n' && ptr[c] != '\0') c++;
260 key = g_strndup(ptr, c);
266 value = quoted_from_text(s_buf);
269 history_list_add_to_key(key, value, 0);
282 gboolean history_list_save(const gchar *path)
289 pathl = path_from_utf8(path);
290 ssi = secure_open(pathl);
294 log_printf(_("Unable to write history lists to: %s\n"), path);
298 secure_fprintf(ssi, "#History lists\n\n");
300 list = g_list_last(history_list);
301 while (list && secsave_errno == SS_ERR_NONE)
306 hd = static_cast<HistoryData *>(list->data);
309 secure_fprintf(ssi, "[%s]\n", hd->key);
311 /* save them inverted (oldest to newest)
312 * so that when reading they are added correctly
314 work = g_list_last(hd->list);
315 list_count = g_list_position(hd->list, g_list_last(hd->list)) + 1;
316 while (work && secsave_errno == SS_ERR_NONE)
318 if ((strcmp(hd->key, "path_list") != 0 || list_count <= options->open_recent_list_maxsize)
320 (strcmp(hd->key, "recent") != 0 || !(!isfile(static_cast<const gchar *>(work->data))))
322 (strcmp(hd->key, "image_list") != 0 || list_count <= options->recent_folder_image_list_maxsize))
324 secure_fprintf(ssi, "\"%s\"\n", static_cast<gchar *>(work->data));
329 secure_fputc(ssi, '\n');
332 secure_fprintf(ssi, "#end\n");
334 return (secure_close(ssi) == 0);
337 static void history_list_free(HistoryData *hd)
354 static HistoryData *history_list_find_by_key(const gchar *key)
356 GList *work = history_list;
358 if (!key) return nullptr;
362 auto hd = static_cast<HistoryData *>(work->data);
363 if (strcmp(hd->key, key) == 0) return hd;
369 const gchar *history_list_find_last_path_by_key(const gchar *key)
373 hd = history_list_find_by_key(key);
374 if (!hd || !hd->list) return nullptr;
376 return static_cast<const gchar *>(hd->list->data);
379 void history_list_free_key(const gchar *key)
382 hd = history_list_find_by_key(key);
385 history_list = g_list_remove(history_list, hd);
386 history_list_free(hd);
389 void history_list_add_to_key(const gchar *key, const gchar *path, gint max)
394 if (!key || !path) return;
396 hd = history_list_find_by_key(key);
399 hd = g_new(HistoryData, 1);
400 hd->key = g_strdup(key);
402 history_list = g_list_prepend(history_list, hd);
405 /* if already in the list, simply move it to the top */
409 auto buf = static_cast<gchar *>(work->data);
411 if (strcmp(buf, path) == 0)
413 /* if not first, move it */
414 if (work != hd->list)
416 hd->list = g_list_remove(hd->list, buf);
417 hd->list = g_list_prepend(hd->list, buf);
424 hd->list = g_list_prepend(hd->list, g_strdup(path));
426 if (max == -1) max = options->open_recent_list_maxsize;
430 GList *work = hd->list;
431 GList *last = nullptr;
441 while (work && len > max)
447 hd->list = g_list_delete_link(hd->list, node);
453 void history_list_item_change(const gchar *key, const gchar *oldpath, const gchar *newpath)
458 if (!oldpath) return;
459 hd = history_list_find_by_key(key);
465 auto buf = static_cast<gchar *>(work->data);
467 if (!(g_str_has_prefix(buf, ".") && !newpath))
469 if (strcmp(buf, oldpath) == 0)
473 work->data = g_strdup(newpath);
477 hd->list = g_list_remove(hd->list, buf);
485 hd->list = g_list_remove(hd->list, buf);
493 void history_list_item_move(const gchar *key, const gchar *path, gint direction)
500 hd = history_list_find_by_key(key);
506 auto buf = static_cast<gchar *>(work->data);
507 if (strcmp(buf, path) == 0)
511 hd->list = g_list_remove(hd->list, buf);
512 hd->list = g_list_insert(hd->list, buf, p);
520 void history_list_item_remove(const gchar *key, const gchar *path)
522 history_list_item_change(key, path, nullptr);
525 GList *history_list_get_by_key(const gchar *key)
529 hd = history_list_find_by_key(key);
530 if (!hd) return nullptr;
536 * @brief Get image last viewed in a folder
537 * @param path Must be a folder
538 * @returns Last image viewed in folder or NULL
540 * Returned string should be freed
542 gchar *get_recent_viewed_folder_image(gchar *path)
547 gchar *ret = nullptr;
549 if (options->recent_folder_image_list_maxsize == 0)
554 hd = history_list_find_by_key("image_list");
558 hd = g_new(HistoryData, 1);
559 hd->key = g_strdup("image_list");
561 history_list = g_list_prepend(history_list, hd);
568 dirname = g_path_get_dirname(static_cast<const gchar *>(work->data));
570 if (g_strcmp0(dirname, path) == 0)
572 if (isfile(static_cast<const gchar *>(work->data)))
574 ret = g_strdup(static_cast<const gchar *>(work->data));
587 static void update_recent_viewed_folder_image_list(const gchar *path)
591 gchar *image_dir = nullptr;
592 gchar *list_dir = nullptr;
593 gboolean found = FALSE;
595 if (options->recent_folder_image_list_maxsize == 0)
600 image_dir = g_path_get_dirname(path);
601 hd = history_list_find_by_key("image_list");
604 hd = g_new(HistoryData, 1);
605 hd->key = g_strdup("image_list");
607 history_list = g_list_prepend(history_list, hd);
614 list_dir = g_path_get_dirname(static_cast<const gchar *>(work->data));
616 /* If folder already in list, update and move to start of list */
617 if (g_strcmp0(list_dir, image_dir) == 0)
620 work->data = g_strdup(path);
621 hd->list = g_list_remove_link(hd->list, work);
622 hd->list = g_list_concat(work, hd->list);
635 hd->list = g_list_prepend(hd->list, g_strdup(path));
638 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */