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