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