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