/** * Find the folder name given the folder's id. * * @param folders the folders list for the device * @param id the folder_id to look up, returns the folder's parent folder_id * @param name returns the name of the folder or NULL on failure * @see discover_filepath_from_id() */ static void find_folder_name(LIBMTP_folder_t* folders, uint32_t* id, char** name) { // FIXME this function is exactly LIBMTP_Find_Folder LIBMTP_folder_t* f = LIBMTP_Find_Folder(folders, *id); if(f == NULL) { *name = NULL; } else { // found it! *name = strdup(f->name); *id = f->parent_id; } }
static void delete_done_cb (LIBMTP_mtpdevice_t *device, TracksDeletedCallbackData *data) { LIBMTP_folder_t *folders; LIBMTP_file_t *files; data->actually_free = FALSE; update_free_space_cb (device, RB_MTP_SOURCE (data->source)); /* if any of the folders we just deleted from are now empty, delete them */ folders = LIBMTP_Get_Folder_List (device); files = LIBMTP_Get_Filelisting_With_Callback (device, NULL, NULL); if (folders != NULL) { GHashTableIter iter; gpointer key; g_hash_table_iter_init (&iter, data->check_folders); while (g_hash_table_iter_next (&iter, &key, NULL)) { LIBMTP_folder_t *f; LIBMTP_folder_t *c; LIBMTP_file_t *file; uint32_t folder_id = GPOINTER_TO_UINT(key); while (folder_id != device->default_music_folder && folder_id != 0) { f = LIBMTP_Find_Folder (folders, folder_id); if (f == NULL) { rb_debug ("unable to find folder %u", folder_id); break; } /* don't delete folders with children that we didn't just delete */ for (c = f->child; c != NULL; c = c->sibling) { if (g_hash_table_lookup (data->check_folders, GUINT_TO_POINTER (c->folder_id)) == NULL) { break; } } if (c != NULL) { rb_debug ("folder %s has children", f->name); break; } /* don't delete folders that contain files */ for (file = files; file != NULL; file = file->next) { if (file->parent_id == folder_id) { break; } } if (file != NULL) { rb_debug ("folder %s contains at least one file: %s", f->name, file->filename); break; } /* ok, the folder is empty */ rb_debug ("deleting empty folder %s", f->name); LIBMTP_Delete_Object (device, f->folder_id); /* if the folder we just deleted has siblings, the parent * can't be empty. */ if (f->sibling != NULL) { rb_debug ("folder %s has siblings, can't delete parent", f->name); break; } folder_id = f->parent_id; } } LIBMTP_destroy_folder_t (folders); } else { rb_debug ("unable to get device folder list"); } /* clean up the file list */ while (files != NULL) { LIBMTP_file_t *n; n = files->next; LIBMTP_destroy_file_t (files); files = n; } g_idle_add ((GSourceFunc) delete_done_idle_cb, data); }