-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathhistory-list.cc
553 lines (460 loc) · 12 KB
/
history-list.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
* Copyright (C) 2008 - 2016 The Geeqie Team
*
* Authors: John Ellis, Vladimir Nadvornik, Laurent Monin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "history-list.h"
#include <cstdio>
#include <cstring>
#include <vector>
#include "intl.h"
#include "options.h"
#include "secure-save.h"
#include "ui-fileops.h"
namespace
{
struct HistoryChain
{
const gchar *prev();
const gchar *next();
bool push_back(const gchar *path);
private:
std::vector<gchar *> chain;
guint index = 0;
bool is_nav_button = false; /** Used to prevent the nav buttons making entries to the chain **/
};
const gchar *HistoryChain::prev()
{
is_nav_button = true;
index = index > 0 ? index - 1 : 0;
return chain[index];
}
const gchar *HistoryChain::next()
{
is_nav_button = true;
guint last = chain.size() - 1;
index = index < last ? index + 1 : last;
return chain[index];
}
bool HistoryChain::push_back(const gchar *path)
{
if (is_nav_button)
{
is_nav_button = false;
return false;
}
if (chain.empty())
{
chain.push_back(g_strdup(path));
index = 0;
}
else
{
if (g_strcmp0(chain.back(), path) != 0)
{
chain.push_back(g_strdup(path));
DEBUG_3("%d %s", chain.size() - 1, path);
}
index = chain.size() - 1;
}
return true;
}
HistoryChain history_chain{};
HistoryChain image_chain{};
gint dirname_compare(gconstpointer data, gconstpointer user_data)
{
g_autofree gchar *dirname = g_path_get_dirname(static_cast<const gchar *>(data));
return g_strcmp0(dirname, static_cast<const gchar *>(user_data));
}
} // namespace
static void update_recent_viewed_folder_image_list(const gchar *path);
/**
* @file
*-----------------------------------------------------------------------------
* Implements a history chain. Used by the Back and Forward toolbar buttons.
* Selecting any folder appends the path to the end of the chain.
* Pressing the Back and Forward buttons moves along the chain, but does
* not make additions to the chain.
* The chain always increases and is deleted at the end of the session
*
*-----------------------------------------------------------------------------
*/
const gchar *history_chain_back()
{
return history_chain.prev();
}
const gchar *history_chain_forward()
{
return history_chain.next();
}
/**
* @brief Appends a path to the history chain
* @param path Path selected
*
* Each time the user selects a new path it is appended to the chain
* except when it is identical to the current last entry
* The pointer is always moved to the end of the chain
*/
void history_chain_append_end(const gchar *path)
{
history_chain.push_back(path);
}
/**
* @file
*-----------------------------------------------------------------------------
* Implements an image history chain. Whenever an image is displayed it is
* appended to a chain.
* Pressing the Image Back and Image Forward buttons moves along the chain,
* but does not make additions to the chain.
* The chain always increases and is deleted at the end of the session
*
*-----------------------------------------------------------------------------
*/
const gchar *image_chain_back()
{
return image_chain.prev();
}
const gchar *image_chain_forward()
{
return image_chain.next();
}
/**
* @brief Appends a path to the image history chain
* @param path Image path selected
*
* Each time the user selects a new image it is appended to the chain
* except when it is identical to the current last entry
* The pointer is always moved to the end of the chain
*
* Updates the recent viewed image_list
*/
void image_chain_append_end(const gchar *path)
{
if (!image_chain.push_back(path)) return;
update_recent_viewed_folder_image_list(path);
}
/*
*-----------------------------------------------------------------------------
* history lists
*-----------------------------------------------------------------------------
*/
struct HistoryData
{
gchar *key;
GList *list;
};
static GList *history_list = nullptr;
static gchar *quoted_from_text(const gchar *text)
{
const gchar *ptr;
gint c = 0;
gint l = strlen(text);
if (l == 0) return nullptr;
while (c < l && text[c] !='"') c++;
if (text[c] == '"')
{
gint e;
c++;
ptr = text + c;
e = c;
while (e < l && text[e] !='"') e++;
if (text[e] == '"')
{
if (e - c > 0)
{
return g_strndup(ptr, e - c);
}
}
}
return nullptr;
}
gboolean history_list_load(const gchar *path)
{
g_autofree gchar *key = nullptr;
gchar s_buf[1024];
g_autofree gchar *pathl = path_from_utf8(path);
g_autoptr(FILE) f = fopen(pathl, "r");
if (!f) return FALSE;
/* first line must start with History comment */
if (!fgets(s_buf, sizeof(s_buf), f) ||
strncmp(s_buf, "#History", 8) != 0)
{
return FALSE;
}
while (fgets(s_buf, sizeof(s_buf), f))
{
if (s_buf[0]=='#') continue;
if (s_buf[0]=='[')
{
gint c;
gchar *ptr;
ptr = s_buf + 1;
c = 0;
while (ptr[c] != ']' && ptr[c] != '\n' && ptr[c] != '\0') c++;
g_free(key);
key = g_strndup(ptr, c);
}
else
{
g_autofree gchar *value = quoted_from_text(s_buf);
if (value && key)
{
history_list_add_to_key(key, value, 0);
}
}
}
return TRUE;
}
gboolean history_list_save(const gchar *path)
{
SecureSaveInfo *ssi;
g_autofree gchar *pathl = path_from_utf8(path);
ssi = secure_open(pathl);
if (!ssi)
{
log_printf(_("Unable to write history lists to: %s\n"), path);
return FALSE;
}
secure_fprintf(ssi, "#History lists\n\n");
for (GList *list = g_list_last(history_list); list && secsave_succeed(); list = list->prev)
{
const auto *hd = static_cast<HistoryData *>(list->data);
secure_fprintf(ssi, "[%s]\n", hd->key);
/* save them inverted (oldest to newest)
* so that when reading they are added correctly
*/
gint list_count = g_list_length(hd->list);
for (GList *work = g_list_last(hd->list); work && secsave_succeed(); work = work->prev)
{
const auto *item = static_cast<gchar *>(work->data);
if ((strcmp(hd->key, "path_list") != 0 || list_count <= options->open_recent_list_maxsize)
&&
(strcmp(hd->key, "recent") != 0 || isfile(item))
&&
(strcmp(hd->key, "image_list") != 0 || list_count <= options->recent_folder_image_list_maxsize))
{
secure_fprintf(ssi, "\"%s\"\n", item);
}
list_count--;
}
secure_fputc(ssi, '\n');
}
secure_fprintf(ssi, "#end\n");
return (secure_close(ssi) == 0);
}
static void history_list_free(HistoryData *hd)
{
if (!hd) return;
g_free(hd->key);
g_list_free_full(hd->list, g_free);
g_free(hd);
}
static HistoryData *history_list_find_by_key(const gchar *key)
{
if (!key) return nullptr;
static const auto history_data_compare_key = [](gconstpointer data, gconstpointer user_data)
{
auto *hd = static_cast<const HistoryData *>(data);
return strcmp(hd->key, static_cast<const gchar *>(user_data));
};
GList *work = g_list_find_custom(history_list, key, history_data_compare_key);
return work ? static_cast<HistoryData *>(work->data) : nullptr;
}
const gchar *history_list_find_last_path_by_key(const gchar *key)
{
HistoryData *hd;
hd = history_list_find_by_key(key);
if (!hd || !hd->list) return nullptr;
return static_cast<const gchar *>(hd->list->data);
}
void history_list_free_key(const gchar *key)
{
HistoryData *hd;
hd = history_list_find_by_key(key);
if (!hd) return;
history_list = g_list_remove(history_list, hd);
history_list_free(hd);
}
void history_list_add_to_key(const gchar *key, const gchar *path, gint max)
{
HistoryData *hd;
GList *work;
if (!key || !path) return;
hd = history_list_find_by_key(key);
if (!hd)
{
hd = g_new(HistoryData, 1);
hd->key = g_strdup(key);
hd->list = nullptr;
history_list = g_list_prepend(history_list, hd);
}
/* if already in the list, simply move it to the top */
work = g_list_find_custom(hd->list, path, reinterpret_cast<GCompareFunc>(strcmp));
if (work)
{
/* if not first, move it */
if (work != hd->list)
{
auto buf = static_cast<gchar *>(work->data);
hd->list = g_list_remove(hd->list, buf);
hd->list = g_list_prepend(hd->list, buf);
}
return;
}
hd->list = g_list_prepend(hd->list, g_strdup(path));
if (max == -1) max = options->open_recent_list_maxsize;
if (max > 0)
{
work = g_list_nth(hd->list, max);
if (work)
{
GList *last = work->prev;
if (last)
{
last->next = nullptr;
}
work->prev = nullptr;
g_list_free_full(work, g_free);
}
}
}
void history_list_item_change(const gchar *key, const gchar *oldpath, const gchar *newpath)
{
HistoryData *hd;
GList *work;
if (!oldpath) return;
hd = history_list_find_by_key(key);
if (!hd) return;
work = hd->list;
while (work)
{
auto buf = static_cast<gchar *>(work->data);
if (!g_str_has_prefix(buf, ".") || newpath)
{
if (strcmp(buf, oldpath) == 0)
{
if (newpath)
{
work->data = g_strdup(newpath);
}
else
{
hd->list = g_list_remove(hd->list, buf);
}
g_free(buf);
return;
}
}
else
{
hd->list = g_list_remove(hd->list, buf);
g_free(buf);
return;
}
work = work->next;
}
}
void history_list_item_move(const gchar *key, const gchar *path, gint direction)
{
HistoryData *hd;
GList *work;
if (!path) return;
hd = history_list_find_by_key(key);
if (!hd) return;
work = g_list_find_custom(hd->list, path, reinterpret_cast<GCompareFunc>(strcmp));
if (!work) return;
gint p = g_list_position(hd->list, work) + direction;
if (p < 0) return;
auto buf = static_cast<gchar *>(work->data);
hd->list = g_list_remove(hd->list, buf);
hd->list = g_list_insert(hd->list, buf, p);
}
void history_list_item_remove(const gchar *key, const gchar *path)
{
history_list_item_change(key, path, nullptr);
}
/**
* @brief The returned GList is internal, don't free it
*/
GList *history_list_get_by_key(const gchar *key)
{
HistoryData *hd;
hd = history_list_find_by_key(key);
if (!hd) return nullptr;
return hd->list;
}
/**
* @brief Get image last viewed in a folder
* @param path Must be a folder
* @returns Last image viewed in folder or NULL
*
* Returned string should be freed
*/
gchar *get_recent_viewed_folder_image(gchar *path)
{
HistoryData *hd;
GList *work;
if (options->recent_folder_image_list_maxsize == 0)
{
return nullptr;
}
hd = history_list_find_by_key("image_list");
if (!hd)
{
hd = g_new(HistoryData, 1);
hd->key = g_strdup("image_list");
hd->list = nullptr;
history_list = g_list_prepend(history_list, hd);
}
work = g_list_find_custom(hd->list, path, dirname_compare);
if (!work || !isfile(static_cast<const gchar *>(work->data)))
{
return nullptr;
}
return g_strdup(static_cast<const gchar *>(work->data));
}
static void update_recent_viewed_folder_image_list(const gchar *path)
{
HistoryData *hd;
GList *work;
if (options->recent_folder_image_list_maxsize == 0)
{
return;
}
hd = history_list_find_by_key("image_list");
if (!hd)
{
hd = g_new(HistoryData, 1);
hd->key = g_strdup("image_list");
hd->list = nullptr;
history_list = g_list_prepend(history_list, hd);
}
g_autofree gchar *image_dir = g_path_get_dirname(path);
work = g_list_find_custom(hd->list, image_dir, dirname_compare);
if (work)
{
g_free(work->data);
work->data = g_strdup(path);
hd->list = g_list_remove_link(hd->list, work);
hd->list = g_list_concat(work, hd->list);
}
else
{
hd->list = g_list_prepend(hd->list, g_strdup(path));
}
}
/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */