From d1c5102c04a4f2745eb808682e575a4f12faa6e1 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sat, 30 Jan 2010 18:34:40 +0100 Subject: Added option to remove missing files from a directory. --- db.c | 20 ++++++++++++++++++++ db.h | 1 + wallpapers.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++-- wallpapers.h | 1 + window_main.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++--------- 5 files changed, 127 insertions(+), 11 deletions(-) diff --git a/db.c b/db.c index 29ce639..fe8c6a2 100644 --- a/db.c +++ b/db.c @@ -294,6 +294,26 @@ sqlite_uint64 db_get_wallpaper(const char *path) { return 0; } +int db_remove_wallpaper(sqlite_uint64 id) { + sqlite3_stmt *stmt; + int rc; + + rc = sqlite3_prepare_v2(db, "DELETE FROM wallpaper WHERE id = ?", -1, &stmt, NULL); + if(rc != SQLITE_OK) { + return 0; + } + + rc = sqlite3_bind_int64(stmt, 1, id); + if(rc != SQLITE_OK) { + sqlite3_finalize(stmt); + return 0; + } + + rc = sqlite3_step(stmt); + sqlite3_finalize(stmt); + return rc == SQLITE_DONE; +} + int db_get_wallpaper_data(sqlite_uint64 id, struct wallpaper_t *wall) { sqlite3_stmt *stmt; int rc; diff --git a/db.h b/db.h index 0819c69..5992c76 100644 --- a/db.h +++ b/db.h @@ -30,6 +30,7 @@ int db_get_top_level_directories(GArray**); int db_get_directories(sqlite_uint64, GArray**); sqlite_uint64 db_add_wallpaper(const char*, sqlite_uint64, int, int, int); sqlite_uint64 db_get_wallpaper(const char*); +int db_remove_wallpaper(sqlite_uint64); int db_get_wallpaper_data(sqlite_uint64, struct wallpaper_t*); int db_get_wall_tags(sqlite_uint64, GArray**); int db_get_wallpapers(sqlite_uint64, GArray**); diff --git a/wallpapers.c b/wallpapers.c index fd893f3..25a6578 100644 --- a/wallpapers.c +++ b/wallpapers.c @@ -13,9 +13,8 @@ #include "wallpapers.h" -static guint context_id = 0; - void add_dir_recursive(const gchar *path, sqlite_uint64 parent, GtkStatusbar *statusbar) { + static guint context_id = 0; GDir *dir; const gchar *filename; sqlite_uint64 dirid; @@ -119,6 +118,58 @@ void add_dir_recursive(const gchar *path, sqlite_uint64 parent, GtkStatusbar *st gdk_threads_leave(); } +void wallpapers_remove_missing(struct directory_t *dir, GtkStatusbar *statusbar, gboolean recursive) { + static guint context_id = 0; + gchar *msg; + GArray *array; + struct wallpaper_t *wall; + + gdk_threads_enter(); + + if(context_id == 0) + context_id = gtk_statusbar_get_context_id(statusbar, "Removal of missing files"); + + msg = g_strdup_printf("Removing missing files from %s", dir->name); + gtk_statusbar_push(statusbar, context_id, msg); + g_free(msg); + + gdk_threads_leave(); + + if(!db_get_wallpapers(dir->dirid, &array)) { + gdk_threads_enter(); + gtk_statusbar_pop(statusbar, context_id); + gtk_statusbar_push(statusbar, context_id, "Failed to get directory wallpapers"); + gdk_threads_leave(); + g_free(dir->name); + return; + } + + for(int i = 0; i < array->len; i++) { + wall = &g_array_index(array, struct wallpaper_t, i); + + if(g_access(wall->filepath, F_OK) == -1) { + msg = g_strdup_printf("Removing %s", wall->filepath); + gdk_threads_enter(); + gtk_statusbar_pop(statusbar, context_id); + gtk_statusbar_push(statusbar, context_id, msg); + gdk_threads_leave(); + g_printf("%s\n", msg); + g_free(msg); + + db_remove_wallpaper(wall->id); + } + + g_free(wall->filepath); + } + g_array_free(array, TRUE); + + gdk_threads_enter(); + gtk_statusbar_pop(statusbar, context_id); + gdk_threads_leave(); + + g_free(dir->name); +} + GdkPixbuf *resize_pixbuf(GdkPixbuf *orig, gint win_width, gint win_height, gint *_width, gint *_height, gdouble *ratio) { GdkPixbuf *pb; gint img_width, img_height, width, height; diff --git a/wallpapers.h b/wallpapers.h index 049cba0..d97d6c8 100644 --- a/wallpapers.h +++ b/wallpapers.h @@ -7,6 +7,7 @@ #include "db.h" void add_dir_recursive(const gchar*, sqlite_uint64, GtkStatusbar*); +void wallpapers_remove_missing(struct directory_t*, GtkStatusbar*, gboolean); GdkPixbuf *resize_pixbuf(GdkPixbuf*, gint, gint, gint*, gint*, gdouble*); #endif diff --git a/window_main.c b/window_main.c index a4e84f1..955c380 100644 --- a/window_main.c +++ b/window_main.c @@ -25,7 +25,7 @@ GtkStatusbar *statusbar; GtkIconView *thumbview; GtkWidget *window_hpane, *window_vpane, *foldtree, *tagview; -GThread *add_thread = NULL; +GThread *wallpaper_thread = NULL; gchar *cur_wall_msg = NULL; @@ -437,7 +437,22 @@ gpointer add_dir_thread(gpointer data) { g_free(directory); - add_thread = NULL; + wallpaper_thread = NULL; + + return NULL; +} + +gpointer remove_missing_thread(gpointer data) { + struct directory_t *dir, temp; + + dir = data; + + temp.dirid = dir->dirid; + temp.name = g_strdup(dir->name); + + wallpapers_remove_missing(&temp, statusbar, FALSE); + + wallpaper_thread = NULL; return NULL; } @@ -446,15 +461,15 @@ void on_add_dir_action_activate(GtkAction *action, gpointer user_data) { GtkWidget *dialog; GError *error; - if(add_thread) return; + if(wallpaper_thread) return; dialog = gtk_file_chooser_dialog_new("Choose Directory", NULL, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL); if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { char *directory; directory = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); - add_thread = g_thread_create(add_dir_thread, directory, FALSE, &error); - if(!add_thread) { + wallpaper_thread = g_thread_create(add_dir_thread, directory, FALSE, &error); + if(!wallpaper_thread) { g_warning("%s", error->message); g_error_free(error); } @@ -469,16 +484,39 @@ void on_foldtree_update_activate(GtkMenuItem *menuitem, gpointer user_data) { struct directory_t *dir; GError *error; - if(add_thread) return; + if(wallpaper_thread) return; + + treeselection = gtk_tree_view_get_selection(GTK_TREE_VIEW(foldtree)); + + if(gtk_tree_selection_get_selected(treeselection, &model, &iter)) { + browse_model_get_dir_record(model, &iter, &dir); + + wallpaper_thread = g_thread_create(add_dir_thread, g_strdup(dir->name), FALSE, &error); + + if(!wallpaper_thread) { + g_warning("%s", error->message); + g_error_free(error); + } + } +} + +void on_foldtree_remove_missing_activate(GtkMenuItem *menuitem, gpointer user_data) { + GtkTreeSelection *treeselection; + GtkTreeModel *model; + GtkTreeIter iter; + struct directory_t *dir; + GError *error; + + if(wallpaper_thread) return; treeselection = gtk_tree_view_get_selection(GTK_TREE_VIEW(foldtree)); if(gtk_tree_selection_get_selected(treeselection, &model, &iter)) { browse_model_get_dir_record(model, &iter, &dir); - add_thread = g_thread_create(add_dir_thread, g_strdup(dir->name), FALSE, &error); + wallpaper_thread = g_thread_create(remove_missing_thread, dir, FALSE, &error); - if(!add_thread) { + if(!wallpaper_thread) { g_warning("%s", error->message); g_error_free(error); } @@ -486,19 +524,24 @@ void on_foldtree_update_activate(GtkMenuItem *menuitem, gpointer user_data) { } inline static void foldtree_popup_add_items(GtkWidget *menu) { - GtkWidget *update; + GtkWidget *update, *remove_missing; GtkTreeSelection *selection; update = gtk_menu_item_new_with_label("Update"); + remove_missing = gtk_menu_item_new_with_label("Remove missing files"); selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(foldtree)); gtk_widget_set_sensitive(update, (gtk_tree_selection_count_selected_rows(selection) == 1 ? TRUE : FALSE)); + gtk_widget_set_sensitive(remove_missing, (gtk_tree_selection_count_selected_rows(selection) == 1 ? TRUE : FALSE)); gtk_menu_shell_append(GTK_MENU_SHELL(menu), update); + gtk_menu_shell_append(GTK_MENU_SHELL(menu), remove_missing); g_signal_connect(G_OBJECT(update), "activate", G_CALLBACK(on_foldtree_update_activate), NULL); + g_signal_connect(G_OBJECT(remove_missing), "activate", G_CALLBACK(on_foldtree_remove_missing_activate), NULL); gtk_widget_show(update); + gtk_widget_show(remove_missing); } static void do_foldtree_popup(GtkWidget *widget, GdkEventButton *event) { -- cgit v1.2.3