Fix clang-tidy: readability-non-const-parameter (4)
[geeqie.git] / src / collect-io.cc
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "main.h"
23 #include "collect-io.h"
24
25 #include "collect.h"
26 #include "filedata.h"
27 #include "layout-util.h"
28 #include "secure-save.h"
29 #include "thumb.h"
30 #include "ui-fileops.h"
31
32 #define GQ_COLLECTION_MARKER "#" GQ_APPNAME
33
34 #define GQ_COLLECTION_FAIL_MIN     300
35 #define GQ_COLLECTION_FAIL_PERCENT 98
36 #define GQ_COLLECTION_READ_BUFSIZE 4096
37
38 struct CollectManagerEntry;
39
40 static void collection_load_thumb_step(CollectionData *cd);
41 static gboolean collection_save_private(CollectionData *cd, const gchar *path);
42
43 static CollectManagerEntry *collect_manager_get_entry(const gchar *path);
44 static void collect_manager_entry_reset(CollectManagerEntry *entry);
45 static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr);
46
47 namespace
48 {
49
50 gboolean scan_geometry(gchar *buffer, GdkRectangle &window)
51 {
52         gint nx, ny, nw, nh;
53
54         if (sscanf(buffer, "%d %d %d %d", &nx, &ny, &nw, &nh) != 4) return FALSE;
55
56         window.x = nx;
57         window.y = ny;
58         window.width = nw;
59         window.height = nh;
60
61         return TRUE;
62 }
63
64 }
65
66 static gboolean collection_load_private(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
67 {
68         gchar s_buf[GQ_COLLECTION_READ_BUFSIZE];
69         FILE *f;
70         gchar *pathl;
71         gboolean limit_failures = TRUE;
72         gboolean success = TRUE;
73         gboolean has_official_header = FALSE;
74         gboolean has_geometry_header = FALSE;
75         gboolean has_gqview_header   = FALSE;
76         gboolean need_header     = TRUE;
77         guint total = 0;
78         guint fail = 0;
79         gboolean changed = FALSE;
80         CollectManagerEntry *entry = nullptr;
81         guint flush = !!(flags & COLLECTION_LOAD_FLUSH);
82         guint append = !!(flags & COLLECTION_LOAD_APPEND);
83         guint only_geometry = !!(flags & COLLECTION_LOAD_GEOMETRY);
84         gboolean reading_extended_filename = FALSE;
85         gchar *buffer2;
86
87         if (!only_geometry)
88                 {
89                 collection_load_stop(cd);
90
91                 if (flush)
92                         collect_manager_flush();
93                 else
94                         entry = collect_manager_get_entry(path);
95
96                 if (!append)
97                         {
98                         g_list_free_full(cd->list, reinterpret_cast<GDestroyNotify>(collection_info_free));
99                         cd->list = nullptr;
100                         }
101                 }
102
103         if (!path && !cd->path) return FALSE;
104
105         if (!path) path = cd->path;
106
107         pathl = path_from_utf8(path);
108
109         DEBUG_1("collection load: append=%d flush=%d only_geometry=%d path=%s",
110                           append, flush, only_geometry, pathl);
111
112         /* load it */
113         f = fopen(pathl, "r");
114         g_free(pathl);
115         if (!f)
116                 {
117                 log_printf("Failed to open collection file: \"%s\"\n", path);
118                 return FALSE;
119                 }
120
121         GString *extended_filename_buffer = g_string_new(nullptr);
122         while (fgets(s_buf, sizeof(s_buf), f))
123                 {
124                 gchar *buf;
125                 gchar *p = s_buf;
126
127                 if (!reading_extended_filename)
128                         {
129                         /* Skip whitespaces and empty lines */
130                         while (*p && g_ascii_isspace(*p)) p++;
131                         if (*p == '\n' || *p == '\r') continue;
132
133                         /* Parse comments */
134                         if (*p == '#')
135                                 {
136                                 if (!need_header) continue;
137                                 if (g_ascii_strncasecmp(p, GQ_COLLECTION_MARKER, strlen(GQ_COLLECTION_MARKER)) == 0)
138                                         {
139                                         /* Looks like an official collection, allow unchecked input.
140                                          * All this does is allow adding files that may not exist,
141                                          * which is needed for the collection manager to work.
142                                          * Also unofficial files abort after too many invalid entries.
143                                          */
144                                         has_official_header = TRUE;
145                                         limit_failures = FALSE;
146                                         }
147                                 else if (strncmp(p, "#geometry:", 10 ) == 0 &&
148                                          scan_geometry(p + 10, cd->window))
149                                         {
150                                         has_geometry_header = TRUE;
151                                         cd->window_read = TRUE;
152                                         if (only_geometry) break;
153                                         }
154                                 else if (g_ascii_strncasecmp(p, "#GQview collection", strlen("#GQview collection")) == 0)
155                                         {
156                                         /* As 2008/04/15 there is no difference between our collection file format
157                                          * and GQview 2.1.5 collection file format so ignore failures as well. */
158                                         has_gqview_header = TRUE;
159                                         limit_failures = FALSE;
160                                         }
161                                 need_header = (!has_official_header && !has_gqview_header) || !has_geometry_header;
162                                 continue;
163                                 }
164
165                         if (only_geometry) continue;
166                         }
167
168                 /* Read filenames */
169                 /** @todo This is not safe! */
170                 /* Updated: anything within double quotes is considered a filename */
171                 if (!reading_extended_filename)
172                         {
173                         /* not yet reading filename */
174                         while (*p && *p != '"') p++;
175                         if (*p) p++;
176                         /* start of filename read */
177                         buf = p;
178                         while (*p && *p != '"') p++;
179                         if (p[0] != '"')
180                                 {
181                                 /* first part of extended filename */
182                                 g_string_append(extended_filename_buffer, buf);
183                                 reading_extended_filename = TRUE;
184                                 continue;
185                                 }
186                         }
187                 else
188                         {
189                         buf = p;
190                         while (*p && *p != '"') p++;
191                         if (p[0] != '"')
192                                 {
193                                 /* end of extended filename still not found */
194                                 g_string_append(extended_filename_buffer, buf);
195                                 continue;
196                                 }
197
198                         /* end of extended filename found */
199                         g_string_append_len(extended_filename_buffer, buf, p - buf);
200                         reading_extended_filename = FALSE;
201                         }
202
203                 if (extended_filename_buffer->len > 0)
204                         {
205                         buffer2 = g_strdup(extended_filename_buffer->str);
206                         g_string_erase(extended_filename_buffer, 0, -1);
207                         }
208                 else
209                         {
210                         *p = 0;
211                         buffer2 = g_strdup(buf);
212                         }
213
214                 if (*buffer2)
215                         {
216                         gboolean valid;
217
218                         if (!flush)
219                                 changed |= collect_manager_process_action(entry, &buffer2);
220
221                         valid = (buffer2[0] == G_DIR_SEPARATOR && collection_add_check(cd, file_data_new_simple(buffer2), FALSE, TRUE));
222                         if (!valid) DEBUG_1("collection invalid file: %s", buffer2);
223
224                         total++;
225                         if (!valid)
226                                 {
227                                 fail++;
228                                 if (limit_failures &&
229                                     fail > GQ_COLLECTION_FAIL_MIN &&
230                                     fail * 100 / total > GQ_COLLECTION_FAIL_PERCENT)
231                                         {
232                                         log_printf("%d invalid filenames in unofficial collection file, closing: %s\n", fail, path);
233                                         success = FALSE;
234                                         break;
235                                         }
236                                 }
237                         }
238                 g_free(buffer2);
239                 }
240
241         g_string_free(extended_filename_buffer, TRUE);
242
243         DEBUG_1("collection files: total = %d fail = %d official=%d gqview=%d geometry=%d",
244                           total, fail, has_official_header, has_gqview_header, has_geometry_header);
245
246         fclose(f);
247         if (only_geometry) return has_geometry_header;
248
249         if (!flush)
250                 {
251                 gchar *buf = nullptr;
252                 while (collect_manager_process_action(entry, &buf))
253                         {
254                         collection_add_check(cd, file_data_new_group(buf), FALSE, TRUE);
255                         changed = TRUE;
256                         g_free(buf);
257                         buf = nullptr;
258                         }
259                 }
260
261         cd->list = collection_list_sort(cd->list, cd->sort_method);
262
263         if (!flush && changed && success)
264                 collection_save_private(cd, path);
265
266         if (!flush)
267                 collect_manager_entry_reset(entry);
268
269         if (!append) cd->changed = FALSE;
270
271         return success;
272 }
273
274 gboolean collection_load(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
275 {
276         if (collection_load_private(cd, path, static_cast<CollectionLoadFlags>(flags | COLLECTION_LOAD_FLUSH)))
277                 {
278                 layout_recent_add_path(cd->path);
279                 return TRUE;
280                 }
281
282         return FALSE;
283 }
284
285 static void collection_load_thumb_do(CollectionData *cd)
286 {
287         GdkPixbuf *pixbuf;
288
289         if (!cd->thumb_loader || !g_list_find(cd->list, cd->thumb_info)) return;
290
291         pixbuf = thumb_loader_get_pixbuf(cd->thumb_loader);
292         collection_info_set_thumb(cd->thumb_info, pixbuf);
293         g_object_unref(pixbuf);
294
295         if (cd->info_updated_func) cd->info_updated_func(cd, cd->thumb_info, cd->info_updated_data);
296 }
297
298 static void collection_load_thumb_error_cb(ThumbLoader *, gpointer data)
299 {
300         auto cd = static_cast<CollectionData *>(data);
301
302         collection_load_thumb_do(cd);
303         collection_load_thumb_step(cd);
304 }
305
306 static void collection_load_thumb_done_cb(ThumbLoader *, gpointer data)
307 {
308         auto cd = static_cast<CollectionData *>(data);
309
310         collection_load_thumb_do(cd);
311         collection_load_thumb_step(cd);
312 }
313
314 static void collection_load_thumb_step(CollectionData *cd)
315 {
316         GList *work;
317         CollectInfo *ci;
318
319         if (!cd->list)
320                 {
321                 collection_load_stop(cd);
322                 return;
323                 }
324
325         work = cd->list;
326         ci = static_cast<CollectInfo *>(work->data);
327         work = work->next;
328         /* find first unloaded thumb */
329         while (work && ci->pixbuf)
330                 {
331                 ci = static_cast<CollectInfo *>(work->data);
332                 work = work->next;
333                 }
334
335         if (!ci || ci->pixbuf)
336                 {
337                 /* done */
338                 collection_load_stop(cd);
339
340                 /* send a NULL CollectInfo to notify end */
341                 if (cd->info_updated_func) cd->info_updated_func(cd, nullptr, cd->info_updated_data);
342
343                 return;
344                 }
345
346         /* setup loader and call it */
347         cd->thumb_info = ci;
348         thumb_loader_free(cd->thumb_loader);
349         cd->thumb_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height);
350         thumb_loader_set_callbacks(cd->thumb_loader,
351                                    collection_load_thumb_done_cb,
352                                    collection_load_thumb_error_cb,
353                                    nullptr,
354                                    cd);
355
356         /* start it */
357         if (!thumb_loader_start(cd->thumb_loader, ci->fd))
358                 {
359                 /* error, handle it, do next */
360                 DEBUG_1("error loading thumb for %s", ci->fd->path);
361                 collection_load_thumb_do(cd);
362                 collection_load_thumb_step(cd);
363                 }
364 }
365
366 void collection_load_thumb_idle(CollectionData *cd)
367 {
368         if (!cd->thumb_loader) collection_load_thumb_step(cd);
369 }
370
371 gboolean collection_load_begin(CollectionData *cd, const gchar *path, CollectionLoadFlags flags)
372 {
373         if (!collection_load(cd, path, flags)) return FALSE;
374
375         collection_load_thumb_idle(cd);
376
377         return TRUE;
378 }
379
380 void collection_load_stop(CollectionData *cd)
381 {
382         if (!cd->thumb_loader) return;
383
384         thumb_loader_free(cd->thumb_loader);
385         cd->thumb_loader = nullptr;
386 }
387
388 static gboolean collection_save_private(CollectionData *cd, const gchar *path)
389 {
390         SecureSaveInfo *ssi;
391         GList *work;
392         gchar *pathl;
393
394         if (!path && !cd->path) return FALSE;
395
396         if (!path)
397                 {
398                 path = cd->path;
399                 }
400
401
402         pathl = path_from_utf8(path);
403         ssi = secure_open(pathl);
404         g_free(pathl);
405         if (!ssi)
406                 {
407                 log_printf(_("failed to open collection (write) \"%s\"\n"), path);
408                 return FALSE;
409                 }
410
411         secure_fprintf(ssi, "%s collection\n", GQ_COLLECTION_MARKER);
412         secure_fprintf(ssi, "#created with %s version %s\n", GQ_APPNAME, VERSION);
413
414         collection_update_geometry(cd);
415         if (cd->window_read)
416                 {
417                 secure_fprintf(ssi, "#geometry: %d %d %d %d\n", cd->window.x, cd->window.y, cd->window.width, cd->window.height);
418                 }
419
420         work = cd->list;
421         while (work && secsave_errno == SS_ERR_NONE)
422                 {
423                 auto ci = static_cast<CollectInfo *>(work->data);
424                 secure_fprintf(ssi, "\"%s\"\n", ci->fd->path);
425                 work = work->next;
426                 }
427
428         secure_fprintf(ssi, "#end\n");
429
430         if (secure_close(ssi))
431                 {
432                 log_printf(_("error saving collection file: %s\nerror: %s\n"), path,
433                             secsave_strerror(secsave_errno));
434                 return FALSE;
435                 }
436
437         if (!cd->path || strcmp(path, cd->path) != 0)
438                 {
439                 gchar *buf = cd->path;
440                 cd->path = g_strdup(path);
441                 path = cd->path;
442                 g_free(buf);
443
444                 g_free(cd->name);
445                 cd->name = g_strdup(filename_from_path(cd->path));
446
447                 collection_path_changed(cd);
448                 }
449
450         cd->changed = FALSE;
451
452         return TRUE;
453 }
454
455 gboolean collection_save(CollectionData *cd, const gchar *path)
456 {
457         if (collection_save_private(cd, path))
458                 {
459                 layout_recent_add_path(cd->path);
460                 return TRUE;
461                 }
462
463         return FALSE;
464 }
465
466 gboolean collection_load_only_geometry(CollectionData *cd, const gchar *path)
467 {
468         return collection_load(cd, path, COLLECTION_LOAD_GEOMETRY);
469 }
470
471
472 /*
473  *-------------------------------------------------------------------
474  * collection manager
475  *-------------------------------------------------------------------
476  */
477
478 #define COLLECT_MANAGER_ACTIONS_PER_IDLE 1000
479 #define COLLECT_MANAGER_FLUSH_DELAY      10000
480
481 struct CollectManagerEntry
482 {
483         gchar *path;
484         GList *add_list;
485         GHashTable *oldpath_hash;
486         GHashTable *newpath_hash;
487         gboolean empty;
488 };
489
490 enum CollectManagerType {
491         COLLECTION_MANAGER_UPDATE,
492         COLLECTION_MANAGER_ADD,
493         COLLECTION_MANAGER_REMOVE
494 };
495
496 struct CollectManagerAction
497 {
498         gchar *oldpath;
499         gchar *newpath;
500
501         CollectManagerType type;
502
503         gint ref;
504 };
505
506
507 static GList *collection_manager_entry_list = nullptr;
508 static GList *collection_manager_action_list = nullptr;
509 static GList *collection_manager_action_tail = nullptr;
510 static guint collection_manager_timer_id = 0; /* event source id */
511
512
513 static CollectManagerAction *collect_manager_action_new(const gchar *oldpath, const gchar *newpath,
514                                                         CollectManagerType type)
515 {
516         CollectManagerAction *action;
517
518         action = g_new0(CollectManagerAction, 1);
519         action->ref = 1;
520
521         action->oldpath = g_strdup(oldpath);
522         action->newpath = g_strdup(newpath);
523
524         action->type = type;
525
526         return action;
527 }
528
529 static void collect_manager_action_ref(CollectManagerAction *action)
530 {
531         action->ref++;
532 }
533
534 static void collect_manager_action_unref(CollectManagerAction *action)
535 {
536         action->ref--;
537
538         if (action->ref > 0) return;
539
540         g_free(action->oldpath);
541         g_free(action->newpath);
542         g_free(action);
543 }
544
545 static void collect_manager_entry_free_data(CollectManagerEntry *entry)
546 {
547         g_list_free_full(entry->add_list, reinterpret_cast<GDestroyNotify>(collect_manager_action_unref));
548         if (g_hash_table_size(entry->oldpath_hash) > 0)
549                 g_hash_table_destroy(entry->oldpath_hash);
550         else
551                 g_hash_table_unref(entry->oldpath_hash);
552         if (g_hash_table_size(entry->newpath_hash) > 0)
553                 g_hash_table_destroy(entry->newpath_hash);
554         else
555                 g_hash_table_unref(entry->newpath_hash);
556 }
557
558 static void collect_manager_entry_init_data(CollectManagerEntry *entry)
559 {
560         entry->add_list = nullptr;
561         entry->oldpath_hash = g_hash_table_new_full(g_str_hash, g_str_equal, nullptr, reinterpret_cast<GDestroyNotify>(collect_manager_action_unref));
562         entry->newpath_hash = g_hash_table_new(g_str_hash, g_str_equal);
563         entry->empty = TRUE;
564
565 }
566
567 static CollectManagerEntry *collect_manager_entry_new(const gchar *path)
568 {
569         CollectManagerEntry *entry;
570
571         entry = g_new0(CollectManagerEntry, 1);
572         entry->path = g_strdup(path);
573         collect_manager_entry_init_data(entry);
574
575         collection_manager_entry_list = g_list_append(collection_manager_entry_list, entry);
576
577         return entry;
578 }
579
580
581 static void collect_manager_entry_free(CollectManagerEntry *entry)
582 {
583         collection_manager_entry_list = g_list_remove(collection_manager_entry_list, entry);
584
585         collect_manager_entry_free_data(entry);
586
587         g_free(entry->path);
588         g_free(entry);
589 }
590
591 static void collect_manager_entry_reset(CollectManagerEntry *entry)
592 {
593         collect_manager_entry_free_data(entry);
594         collect_manager_entry_init_data(entry);
595 }
596
597 static CollectManagerEntry *collect_manager_get_entry(const gchar *path)
598 {
599         GList *work;
600
601         work = collection_manager_entry_list;
602         while (work)
603                 {
604                 CollectManagerEntry *entry;
605
606                 entry = static_cast<CollectManagerEntry *>(work->data);
607                 work = work->next;
608                 if (strcmp(entry->path, path) == 0)
609                         {
610                         return entry;
611                         }
612                 }
613         return nullptr;
614
615 }
616
617 static void collect_manager_entry_add_action(CollectManagerEntry *entry, CollectManagerAction *action)
618 {
619
620         CollectManagerAction *orig_action;
621
622         entry->empty = FALSE;
623
624         if (action->oldpath == nullptr)
625                 {
626                 /* add file */
627                 if (action->newpath == nullptr)
628                         {
629                         return;
630                         }
631
632                 orig_action = static_cast<CollectManagerAction *>(g_hash_table_lookup(entry->newpath_hash, action->newpath));
633                 if (orig_action)
634                         {
635                         /* target already exists */
636                         log_printf("collection manager failed to add another action for target %s in collection %s\n",
637                                 action->newpath, entry->path);
638                         return;
639                         }
640                 entry->add_list = g_list_append(entry->add_list, action);
641                 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
642                 collect_manager_action_ref(action);
643                 return;
644                 }
645
646         orig_action = static_cast<CollectManagerAction *>(g_hash_table_lookup(entry->newpath_hash, action->oldpath));
647         if (orig_action)
648                 {
649                 /* new action with the same file */
650                 CollectManagerAction *new_action = collect_manager_action_new(orig_action->oldpath, action->newpath, action->type);
651
652                 if (new_action->oldpath)
653                         {
654                         g_hash_table_steal(entry->oldpath_hash, orig_action->oldpath);
655                         g_hash_table_insert(entry->oldpath_hash, new_action->oldpath, new_action);
656                         }
657                 else
658                         {
659                         GList *work = g_list_find(entry->add_list, orig_action);
660                         work->data = new_action;
661                         }
662
663                 g_hash_table_steal(entry->newpath_hash, orig_action->newpath);
664                 if (new_action->newpath)
665                         {
666                         g_hash_table_insert(entry->newpath_hash, new_action->newpath, new_action);
667                         }
668                 collect_manager_action_unref(orig_action);
669                 return;
670                 }
671
672
673         orig_action = static_cast<CollectManagerAction *>(g_hash_table_lookup(entry->oldpath_hash, action->oldpath));
674         if (orig_action)
675                 {
676                 /* another action for the same source, ignore */
677                 log_printf("collection manager failed to add another action for source %s in collection %s\n",
678                         action->oldpath, entry->path);
679                 return;
680                 }
681
682         g_hash_table_insert(entry->oldpath_hash, action->oldpath, action);
683         if (action->newpath)
684                 {
685                 g_hash_table_insert(entry->newpath_hash, action->newpath, action);
686                 }
687         collect_manager_action_ref(action);
688 }
689
690 static gboolean collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr)
691 {
692         gchar *path = *path_ptr;
693         CollectManagerAction *action;
694
695         if (path == nullptr)
696                 {
697                 /* get new files */
698                 if (entry->add_list)
699                         {
700                         action = static_cast<CollectManagerAction *>(entry->add_list->data);
701                         g_assert(action->oldpath == nullptr);
702                         entry->add_list = g_list_remove(entry->add_list, action);
703                         path = g_strdup(action->newpath);
704                         g_hash_table_remove(entry->newpath_hash, path);
705                         collect_manager_action_unref(action);
706                         }
707                 *path_ptr = path;
708                 return (path != nullptr);
709                 }
710
711         action = static_cast<CollectManagerAction *>(g_hash_table_lookup(entry->oldpath_hash, path));
712
713         if (action)
714                 {
715                 strcpy(*path_ptr, action->newpath);
716                 return TRUE;
717                 }
718
719         return FALSE; /* no change */
720 }
721
722 static void collect_manager_refresh()
723 {
724         GList *list;
725         GList *work;
726         FileData *dir_fd;
727
728         dir_fd = file_data_new_dir(get_collections_dir());
729         filelist_read(dir_fd, &list, nullptr);
730         file_data_unref(dir_fd);
731
732         work = collection_manager_entry_list;
733         while (work && list)
734                 {
735                 CollectManagerEntry *entry;
736                 GList *list_step;
737
738                 entry = static_cast<CollectManagerEntry *>(work->data);
739                 work = work->next;
740
741                 list_step = list;
742                 while (list_step && entry)
743                         {
744                         FileData *fd;
745
746                         fd = static_cast<FileData *>(list_step->data);
747                         list_step = list_step->next;
748
749                         if (strcmp(fd->path, entry->path) == 0)
750                                 {
751                                 list = g_list_remove(list, fd);
752                                 file_data_unref(fd);
753
754                                 entry = nullptr;
755                                 }
756                         else
757                                 {
758                                 collect_manager_entry_free(entry);
759
760                                 entry = nullptr;
761                                 }
762                         }
763                 }
764
765         work = list;
766         while (work)
767                 {
768                 FileData *fd;
769
770                 fd = static_cast<FileData *>(work->data);
771                 work = work->next;
772
773                 collect_manager_entry_new(fd->path);
774                 }
775
776         filelist_free(list);
777 }
778
779 static void collect_manager_process_actions(gint max)
780 {
781         if (collection_manager_action_list) DEBUG_1("collection manager processing actions");
782
783         while (collection_manager_action_list != nullptr && max > 0)
784                 {
785                 CollectManagerAction *action;
786                 GList *work;
787
788                 action = static_cast<CollectManagerAction *>(collection_manager_action_list->data);
789                 work = collection_manager_entry_list;
790                 while (work)
791                         {
792                         CollectManagerEntry *entry;
793
794                         entry = static_cast<CollectManagerEntry *>(work->data);
795                         work = work->next;
796
797                         if (action->type == COLLECTION_MANAGER_UPDATE)
798                                 {
799                                 collect_manager_entry_add_action(entry, action);
800                                 }
801                         else if (action->oldpath && action->newpath &&
802                                  strcmp(action->newpath, entry->path) == 0)
803                                 {
804                                 /* convert action to standard add format */
805                                 g_free(action->newpath);
806                                 if (action->type == COLLECTION_MANAGER_ADD)
807                                         {
808                                         action->newpath = action->oldpath;
809                                         action->oldpath = nullptr;
810                                         }
811                                 else if (action->type == COLLECTION_MANAGER_REMOVE)
812                                         {
813                                         action->newpath = nullptr;
814                                         }
815                                 collect_manager_entry_add_action(entry, action);
816                                 }
817
818                         max--;
819                         }
820
821                 if (action->type != COLLECTION_MANAGER_UPDATE &&
822                     action->oldpath && action->newpath)
823                         {
824                         log_printf("collection manager failed to %s %s for collection %s\n",
825                                 (action->type == COLLECTION_MANAGER_ADD) ? "add" : "remove",
826                                 action->oldpath, action->newpath);
827                         }
828
829                 if (collection_manager_action_tail == collection_manager_action_list)
830                         {
831                         collection_manager_action_tail = nullptr;
832                         }
833                 collection_manager_action_list = g_list_remove(collection_manager_action_list, action);
834                 collect_manager_action_unref(action);
835                 }
836 }
837
838 static gboolean collect_manager_process_entry(CollectManagerEntry *entry)
839 {
840         CollectionData *cd;
841
842         if (entry->empty) return FALSE;
843
844         cd = collection_new(entry->path);
845         (void) collection_load_private(cd, entry->path, COLLECTION_LOAD_NONE);
846
847         collection_unref(cd);
848
849         return TRUE;
850 }
851
852 static gboolean collect_manager_process_entry_list()
853 {
854         GList *work;
855
856         work = collection_manager_entry_list;
857         while (work)
858                 {
859                 CollectManagerEntry *entry;
860
861                 entry = static_cast<CollectManagerEntry *>(work->data);
862                 work = work->next;
863                 if (collect_manager_process_entry(entry)) return TRUE;
864                 }
865
866         return FALSE;
867 }
868
869
870
871 static gboolean collect_manager_process_cb(gpointer)
872 {
873         if (collection_manager_action_list) collect_manager_refresh();
874         collect_manager_process_actions(COLLECT_MANAGER_ACTIONS_PER_IDLE);
875         if (collection_manager_action_list) return G_SOURCE_CONTINUE;
876
877         if (collect_manager_process_entry_list()) return G_SOURCE_CONTINUE;
878
879         DEBUG_1("collection manager is up to date");
880         return G_SOURCE_REMOVE;
881 }
882
883 static gboolean collect_manager_timer_cb(gpointer)
884 {
885         DEBUG_1("collection manager timer expired");
886
887         g_idle_add_full(G_PRIORITY_LOW, collect_manager_process_cb, nullptr, nullptr);
888
889         collection_manager_timer_id = 0;
890         return FALSE;
891 }
892
893 static void collect_manager_timer_push(gint stop)
894 {
895         if (collection_manager_timer_id)
896                 {
897                 if (!stop) return;
898
899                 g_source_remove(collection_manager_timer_id);
900                 collection_manager_timer_id = 0;
901                 }
902
903         if (!stop)
904                 {
905                 collection_manager_timer_id = g_timeout_add(COLLECT_MANAGER_FLUSH_DELAY,
906                                                             collect_manager_timer_cb, nullptr);
907                 DEBUG_1("collection manager timer started");
908                 }
909 }
910
911 static void collect_manager_add_action(CollectManagerAction *action)
912 {
913         if (!action) return;
914
915         /* we keep track of the list's tail to keep this a n(1) operation */
916
917         if (collection_manager_action_tail)
918                 {
919                 collection_manager_action_tail = g_list_append(collection_manager_action_tail, action);
920                 collection_manager_action_tail = collection_manager_action_tail->next;
921                 }
922         else
923                 {
924                 collection_manager_action_list = g_list_append(collection_manager_action_list, action);
925                 collection_manager_action_tail = collection_manager_action_list;
926                 }
927
928         collect_manager_timer_push(FALSE);
929 }
930
931 void collect_manager_moved(FileData *fd)
932 {
933         CollectManagerAction *action;
934         const gchar *oldpath = fd->change->source;
935         const gchar *newpath = fd->change->dest;
936
937         action = collect_manager_action_new(oldpath, newpath, COLLECTION_MANAGER_UPDATE);
938         collect_manager_add_action(action);
939 }
940
941 void collect_manager_add(FileData *fd, const gchar *collection)
942 {
943         CollectManagerAction *action;
944         CollectWindow *cw;
945
946         if (!fd || !collection) return;
947
948         cw = collection_window_find_by_path(collection);
949         if (cw)
950                 {
951                 if (collection_list_find_fd(cw->cd->list, fd) == nullptr)
952                         {
953                         collection_add(cw->cd, fd, FALSE);
954                         }
955                 return;
956                 }
957
958         action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_ADD);
959         collect_manager_add_action(action);
960 }
961
962 void collect_manager_remove(FileData *fd, const gchar *collection)
963 {
964         CollectManagerAction *action;
965         CollectWindow *cw;
966
967         if (!fd || !collection) return;
968
969         cw = collection_window_find_by_path(collection);
970         if (cw)
971                 {
972                 while (collection_remove(cw->cd, fd));
973                 return;
974                 }
975
976         action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_REMOVE);
977         collect_manager_add_action(action);
978 }
979
980 void collect_manager_flush()
981 {
982         collect_manager_timer_push(TRUE);
983
984         DEBUG_1("collection manager flushing");
985         while (collect_manager_process_cb(nullptr));
986 }
987
988 void collect_manager_notify_cb(FileData *fd, NotifyType type, gpointer)
989 {
990         if (!(type & NOTIFY_CHANGE) || !fd->change) return;
991
992         DEBUG_1("Notify collect_manager: %s %04x", fd->path, type);
993         switch (fd->change->type)
994                 {
995                 case FILEDATA_CHANGE_MOVE:
996                         collect_manager_moved(fd);
997                         break;
998                 case FILEDATA_CHANGE_COPY:
999                         break;
1000                 case FILEDATA_CHANGE_RENAME:
1001                         collect_manager_moved(fd);
1002                         break;
1003                 case FILEDATA_CHANGE_DELETE:
1004                 case FILEDATA_CHANGE_UNSPECIFIED:
1005                 case FILEDATA_CHANGE_WRITE_METADATA:
1006                         break;
1007                 }
1008 }
1009
1010 static gint collection_manager_sort_cb(gconstpointer a, gconstpointer b)
1011 {
1012         auto char_a = static_cast<const gchar *>(a);
1013         auto char_b = static_cast<const gchar *>(b);
1014
1015         return g_strcmp0(char_a, char_b);
1016 }
1017
1018 /**
1019  * @brief Creates sorted list of collections
1020  * @param[out] names_exc sorted list of collections names excluding extension
1021  * @param[out] names_inc sorted list of collections names including extension
1022  * @param[out] paths sorted list of collection paths
1023  *
1024  * Lists of type gchar.
1025  * Used lists must be freed with string_list_free()
1026  */
1027 void collect_manager_list(GList **names_exc, GList **names_inc, GList **paths)
1028 {
1029         FileData *dir_fd;
1030         GList *list = nullptr;
1031         gchar *name;
1032         FileData *fd;
1033         gchar *filename;
1034
1035         if (names_exc == nullptr && names_inc == nullptr && paths == nullptr)
1036                 {
1037                 return;
1038                 }
1039
1040         dir_fd = file_data_new_dir((get_collections_dir()));
1041
1042         filelist_read(dir_fd, &list, nullptr);
1043
1044         while (list)
1045                 {
1046                 fd = static_cast<FileData *>(list->data);
1047                 filename = g_strdup(filename_from_path(fd->path));
1048
1049                 if (file_extension_match(filename, GQ_COLLECTION_EXT))
1050                         {
1051                         name = remove_extension_from_path(filename);
1052
1053                         if (names_exc != nullptr)
1054                                 {
1055                                 *names_exc = g_list_insert_sorted(*names_exc, g_strdup(name),
1056                                                                                         collection_manager_sort_cb);
1057                                 *names_exc = g_list_first(*names_exc);
1058                                 }
1059                         if (names_inc != nullptr)
1060                                 {
1061                                 *names_inc = g_list_insert_sorted(*names_inc,filename,
1062                                                                                         collection_manager_sort_cb);
1063                                 *names_inc = g_list_first(*names_inc);
1064                                 }
1065                         if (paths != nullptr)
1066                                 {
1067                                 *paths = g_list_insert_sorted(*paths, g_strdup(fd->path),
1068                                                                                         collection_manager_sort_cb);
1069                                 *paths = g_list_first(*paths);
1070                                 }
1071                         g_free(name);
1072                         }
1073                 list = list->next;
1074                 g_free(filename);
1075                 }
1076
1077         filelist_free(list);
1078 }
1079 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */