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"
29 #include "secure-save.h"
30 #include "ui-fileops.h"
32 static void update_recent_viewed_folder_image_list(const gchar *path);
36 *-----------------------------------------------------------------------------
37 * Implements a history chain. Used by the Back and Forward toolbar buttons.
38 * Selecting any folder appends the path to the end of the chain.
39 * Pressing the Back and Forward buttons moves along the chain, but does
40 * not make additions to the chain.
41 * The chain always increases and is deleted at the end of the session
43 *-----------------------------------------------------------------------------
46 static GList *history_chain = nullptr;
47 static guint chain_index = G_MAXUINT;
48 static gboolean nav_button = FALSE; /** Used to prevent the nav buttons making entries to the chain **/
50 const gchar *history_chain_back()
54 chain_index = chain_index > 0 ? chain_index - 1 : 0;
56 return static_cast<const gchar *>(g_list_nth_data(history_chain, chain_index));
59 const gchar *history_chain_forward()
62 guint last = g_list_length(history_chain) - 1;
64 chain_index = chain_index < last ? chain_index + 1 : last;
66 return static_cast<const gchar *>(g_list_nth_data(history_chain, chain_index));
70 * @brief Appends a path to the history chain
71 * @param path Path selected
73 * Each time the user selects a new path it is appended to the chain
74 * except when it is identical to the current last entry
75 * The pointer is always moved to the end of the chain
77 void history_chain_append_end(const gchar *path)
83 if(chain_index == G_MAXUINT)
85 history_chain = g_list_append (history_chain, g_strdup(path));
90 work = g_list_last(history_chain);
91 if (g_strcmp0(static_cast<const gchar *>(work->data), path) != 0)
93 history_chain = g_list_append (history_chain, g_strdup(path));
94 chain_index = g_list_length(history_chain) - 1;
95 DEBUG_3("%d %s", chain_index, path);
99 chain_index = g_list_length(history_chain) - 1;
111 *-----------------------------------------------------------------------------
112 * Implements an image history chain. Whenever an image is displayed it is
113 * appended to a chain.
114 * Pressing the Image Back and Image Forward buttons moves along the chain,
115 * but does not make additions to the chain.
116 * The chain always increases and is deleted at the end of the session
118 *-----------------------------------------------------------------------------
120 static GList *image_chain = nullptr;
121 static guint image_chain_index = G_MAXUINT;
122 static gboolean image_nav_button = FALSE; /** Used to prevent the nav buttons making entries to the chain **/
123 const gchar *image_chain_back()
125 image_nav_button = TRUE;
127 image_chain_index = image_chain_index > 0 ? image_chain_index - 1 : 0;
129 return static_cast<const gchar *>(g_list_nth_data(image_chain, image_chain_index));
132 const gchar *image_chain_forward()
134 image_nav_button= TRUE;
135 guint last = g_list_length(image_chain) - 1;
137 image_chain_index = image_chain_index < last ? image_chain_index + 1 : last;
139 return static_cast<const gchar *>(g_list_nth_data(image_chain, image_chain_index));
143 * @brief Appends a path to the image history chain
144 * @param path Image path selected
146 * Each time the user selects a new image it is appended to the chain
147 * except when it is identical to the current last entry
148 * The pointer is always moved to the end of the chain
150 * Updates the recent viewed image_list
152 void image_chain_append_end(const gchar *path)
156 if (!image_nav_button)
158 if(image_chain_index == G_MAXUINT)
160 image_chain = g_list_append(image_chain, g_strdup(path));
161 image_chain_index = 0;
165 work = g_list_last(image_chain);
166 if (g_strcmp0(static_cast<const gchar *>(work->data) , path) != 0)
168 image_chain = g_list_append(image_chain, g_strdup(path));
169 image_chain_index = g_list_length(image_chain) - 1;
170 DEBUG_3("%d %s", image_chain_index, path);
174 image_chain_index = g_list_length(image_chain) - 1;
178 update_recent_viewed_folder_image_list(path);
182 image_nav_button = FALSE;
187 *-----------------------------------------------------------------------------
189 *-----------------------------------------------------------------------------
198 static GList *history_list = nullptr;
201 static gchar *quoted_from_text(const gchar *text)
205 gint l = strlen(text);
207 if (l == 0) return nullptr;
209 while (c < l && text[c] !='"') c++;
216 while (e < l && text[e] !='"') e++;
221 return g_strndup(ptr, e - c);
228 gboolean history_list_load(const gchar *path)
231 gchar *key = nullptr;
235 pathl = path_from_utf8(path);
236 f = fopen(pathl, "r");
238 if (!f) return FALSE;
240 /* first line must start with History comment */
241 if (!fgets(s_buf, sizeof(s_buf), f) ||
242 strncmp(s_buf, "#History", 8) != 0)
248 while (fgets(s_buf, sizeof(s_buf), f))
250 if (s_buf[0]=='#') continue;
258 while (ptr[c] != ']' && ptr[c] != '\n' && ptr[c] != '\0') c++;
261 key = g_strndup(ptr, c);
267 value = quoted_from_text(s_buf);
270 history_list_add_to_key(key, value, 0);
283 gboolean history_list_save(const gchar *path)
290 pathl = path_from_utf8(path);
291 ssi = secure_open(pathl);
295 log_printf(_("Unable to write history lists to: %s\n"), path);
299 secure_fprintf(ssi, "#History lists\n\n");
301 list = g_list_last(history_list);
302 while (list && secsave_errno == SS_ERR_NONE)
307 hd = static_cast<HistoryData *>(list->data);
310 secure_fprintf(ssi, "[%s]\n", hd->key);
312 /* save them inverted (oldest to newest)
313 * so that when reading they are added correctly
315 work = g_list_last(hd->list);
316 list_count = g_list_position(hd->list, g_list_last(hd->list)) + 1;
317 while (work && secsave_errno == SS_ERR_NONE)
319 if ((strcmp(hd->key, "path_list") != 0 || list_count <= options->open_recent_list_maxsize)
321 (strcmp(hd->key, "recent") != 0 || !(!isfile(static_cast<const gchar *>(work->data))))
323 (strcmp(hd->key, "image_list") != 0 || list_count <= options->recent_folder_image_list_maxsize))
325 secure_fprintf(ssi, "\"%s\"\n", static_cast<gchar *>(work->data));
330 secure_fputc(ssi, '\n');
333 secure_fprintf(ssi, "#end\n");
335 return (secure_close(ssi) == 0);
338 static void history_list_free(HistoryData *hd)
355 static HistoryData *history_list_find_by_key(const gchar *key)
357 GList *work = history_list;
359 if (!key) return nullptr;
363 auto hd = static_cast<HistoryData *>(work->data);
364 if (strcmp(hd->key, key) == 0) return hd;
370 const gchar *history_list_find_last_path_by_key(const gchar *key)
374 hd = history_list_find_by_key(key);
375 if (!hd || !hd->list) return nullptr;
377 return static_cast<const gchar *>(hd->list->data);
380 void history_list_free_key(const gchar *key)
383 hd = history_list_find_by_key(key);
386 history_list = g_list_remove(history_list, hd);
387 history_list_free(hd);
390 void history_list_add_to_key(const gchar *key, const gchar *path, gint max)
395 if (!key || !path) return;
397 hd = history_list_find_by_key(key);
400 hd = g_new(HistoryData, 1);
401 hd->key = g_strdup(key);
403 history_list = g_list_prepend(history_list, hd);
406 /* if already in the list, simply move it to the top */
410 auto buf = static_cast<gchar *>(work->data);
412 if (strcmp(buf, path) == 0)
414 /* if not first, move it */
415 if (work != hd->list)
417 hd->list = g_list_remove(hd->list, buf);
418 hd->list = g_list_prepend(hd->list, buf);
425 hd->list = g_list_prepend(hd->list, g_strdup(path));
427 if (max == -1) max = options->open_recent_list_maxsize;
431 GList *work = hd->list;
432 GList *last = nullptr;
442 while (work && len > max)
448 hd->list = g_list_delete_link(hd->list, node);
454 void history_list_item_change(const gchar *key, const gchar *oldpath, const gchar *newpath)
459 if (!oldpath) return;
460 hd = history_list_find_by_key(key);
466 auto buf = static_cast<gchar *>(work->data);
468 if (!(g_str_has_prefix(buf, ".") && !newpath))
470 if (strcmp(buf, oldpath) == 0)
474 work->data = g_strdup(newpath);
478 hd->list = g_list_remove(hd->list, buf);
486 hd->list = g_list_remove(hd->list, buf);
494 void history_list_item_move(const gchar *key, const gchar *path, gint direction)
501 hd = history_list_find_by_key(key);
507 auto buf = static_cast<gchar *>(work->data);
508 if (strcmp(buf, path) == 0)
512 hd->list = g_list_remove(hd->list, buf);
513 hd->list = g_list_insert(hd->list, buf, p);
521 void history_list_item_remove(const gchar *key, const gchar *path)
523 history_list_item_change(key, path, nullptr);
526 GList *history_list_get_by_key(const gchar *key)
530 hd = history_list_find_by_key(key);
531 if (!hd) return nullptr;
537 * @brief Get image last viewed in a folder
538 * @param path Must be a folder
539 * @returns Last image viewed in folder or NULL
541 * Returned string should be freed
543 gchar *get_recent_viewed_folder_image(gchar *path)
548 gchar *ret = nullptr;
550 if (options->recent_folder_image_list_maxsize == 0)
555 hd = history_list_find_by_key("image_list");
559 hd = g_new(HistoryData, 1);
560 hd->key = g_strdup("image_list");
562 history_list = g_list_prepend(history_list, hd);
569 dirname = g_path_get_dirname(static_cast<const gchar *>(work->data));
571 if (g_strcmp0(dirname, path) == 0)
573 if (isfile(static_cast<const gchar *>(work->data)))
575 ret = g_strdup(static_cast<const gchar *>(work->data));
588 static void update_recent_viewed_folder_image_list(const gchar *path)
592 gchar *image_dir = nullptr;
593 gchar *list_dir = nullptr;
594 gboolean found = FALSE;
596 if (options->recent_folder_image_list_maxsize == 0)
601 image_dir = g_path_get_dirname(path);
602 hd = history_list_find_by_key("image_list");
605 hd = g_new(HistoryData, 1);
606 hd->key = g_strdup("image_list");
608 history_list = g_list_prepend(history_list, hd);
615 list_dir = g_path_get_dirname(static_cast<const gchar *>(work->data));
617 /* If folder already in list, update and move to start of list */
618 if (g_strcmp0(list_dir, image_dir) == 0)
621 work->data = g_strdup(path);
622 hd->list = g_list_remove_link(hd->list, work);
623 hd->list = g_list_concat(work, hd->list);
636 hd->list = g_list_prepend(hd->list, g_strdup(path));
639 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */