static gboolean collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr) { gchar *path = *path_ptr; CollectManagerAction *action; if (path == NULL) { /* get new files */ if (entry->add_list) { action = entry->add_list->data; g_assert(action->oldpath == NULL); entry->add_list = g_list_remove(entry->add_list, action); path = g_strdup(action->newpath); g_hash_table_remove(entry->newpath_hash, path); collect_manager_action_unref(action); } *path_ptr = path; return (path != NULL); } action = g_hash_table_lookup(entry->oldpath_hash, path); if (action) { g_free(path); path = g_strdup(action->newpath); *path_ptr = path; return TRUE; } return FALSE; /* no change */ }
static void collect_manager_process_actions(gint max) { if (collection_manager_action_list) DEBUG_1("collection manager processing actions"); while (collection_manager_action_list != NULL && max > 0) { CollectManagerAction *action; GList *work; action = collection_manager_action_list->data; work = collection_manager_entry_list; while (work) { CollectManagerEntry *entry; entry = work->data; work = work->next; if (action->type == COLLECTION_MANAGER_UPDATE) { collect_manager_entry_add_action(entry, action); } else if (action->oldpath && action->newpath && strcmp(action->newpath, entry->path) == 0) { /* convert action to standard add format */ g_free(action->newpath); if (action->type == COLLECTION_MANAGER_ADD) { action->newpath = action->oldpath; action->oldpath = NULL; } else if (action->type == COLLECTION_MANAGER_REMOVE) { action->newpath = NULL; } collect_manager_entry_add_action(entry, action); } max--; } if (action->type != COLLECTION_MANAGER_UPDATE && action->oldpath && action->newpath) { log_printf("collection manager failed to %s %s for collection %s\n", (action->type == COLLECTION_MANAGER_ADD) ? "add" : "remove", action->oldpath, action->newpath); } if (collection_manager_action_tail == collection_manager_action_list) { collection_manager_action_tail = NULL; } collection_manager_action_list = g_list_remove(collection_manager_action_list, action); collect_manager_action_unref(action); } }
static gint collect_manager_process_entry(CollectManagerEntry *entry) { CollectionData *cd; gint success; GList *work; if (!entry->action_list) return FALSE; cd = collection_new(entry->path); success = collection_load_private(cd, entry->path, FALSE, FALSE); work = g_list_last(entry->action_list); while (work) { CollectManagerAction *action; action = work->data; work = work->prev; if (!action->oldpath) { /* add image */ if (collection_list_find(cd->list, action->newpath) == NULL) { collection_add_check(cd, action->newpath, FALSE, FALSE); } } else if (action->newpath) { /* rename image */ while (collection_rename(cd, action->oldpath, action->newpath)); } else { /* remove image */ while (collection_remove(cd, action->oldpath)); } collect_manager_action_unref(action); } if (success && cd->changed) { collection_save_private(cd, entry->path); if (debug) printf("collection manager updated: %s\n", entry->path); } collection_unref(cd); g_list_free(entry->action_list); entry->action_list = NULL; return TRUE; }
static void collect_manager_entry_free_data(CollectManagerEntry *entry) { GList *work; work = entry->add_list; while (work) { CollectManagerAction *action; action = work->data; work = work->next; collect_manager_action_unref(action); } g_list_free(entry->add_list); g_hash_table_destroy(entry->oldpath_hash); g_hash_table_destroy(entry->newpath_hash); }
static void collect_manager_entry_free(CollectManagerEntry *entry) { GList *work; collection_manager_entry_list = g_list_remove(collection_manager_entry_list, entry); work = entry->action_list; while (work) { CollectManagerAction *action; action = work->data; work = work->next; collect_manager_action_unref(action); } g_list_free(entry->action_list); g_free(entry->path); g_free(entry); }
static void collect_manager_entry_add_action(CollectManagerEntry *entry, CollectManagerAction *action) { CollectManagerAction *orig_action; entry->empty = FALSE; if (action->oldpath == NULL) { /* add file */ if (action->newpath == NULL) { return; } orig_action = g_hash_table_lookup(entry->newpath_hash, action->newpath); if (orig_action) { /* target already exists */ log_printf("collection manager failed to add another action for target %s in collection %s\n", action->newpath, entry->path); return; } entry->add_list = g_list_append(entry->add_list, action); g_hash_table_insert(entry->newpath_hash, action->newpath, action); collect_manager_action_ref(action); return; } orig_action = g_hash_table_lookup(entry->newpath_hash, action->oldpath); if (orig_action) { /* new action with the same file */ CollectManagerAction *new_action = collect_manager_action_new(orig_action->oldpath, action->newpath, action->type); if (new_action->oldpath) { g_hash_table_steal(entry->oldpath_hash, orig_action->oldpath); g_hash_table_insert(entry->oldpath_hash, new_action->oldpath, new_action); } else { GList *work = g_list_find(entry->add_list, orig_action); work->data = new_action; } g_hash_table_steal(entry->newpath_hash, orig_action->newpath); if (new_action->newpath) { g_hash_table_insert(entry->newpath_hash, new_action->newpath, new_action); } collect_manager_action_unref(orig_action); return; } orig_action = g_hash_table_lookup(entry->oldpath_hash, action->oldpath); if (orig_action) { /* another action for the same source, ignore */ log_printf("collection manager failed to add another action for source %s in collection %s\n", action->oldpath, entry->path); return; } g_hash_table_insert(entry->oldpath_hash, action->oldpath, action); if (action->newpath) { g_hash_table_insert(entry->newpath_hash, action->newpath, action); } collect_manager_action_ref(action); }