From aecce87bc3bf771ef9e920c07c42b8a3e57e26e9 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Wed, 23 Dec 2009 01:50:43 +0100 Subject: Added som directory adding stuff. --- db.c | 27 +++++++++++++++++++++++++++ db.h | 1 + wallpapers.c | 47 ++++++++++++++++++++++++++++------------------- wallpapers.h | 8 ++++++++ walls.ui | 20 ++++++++++++++++++++ window_main.c | 17 +++++++++++++++-- 6 files changed, 99 insertions(+), 21 deletions(-) create mode 100644 wallpapers.h diff --git a/db.c b/db.c index 2fbaed8..931767d 100644 --- a/db.c +++ b/db.c @@ -271,6 +271,33 @@ sqlite_uint64 db_add_wallpaper(const char *filepath, sqlite_uint64 dirid, int si } } +sqlite_uint64 db_get_wallpaper(const char *path) { + sqlite3_stmt *stmt; + int rc; + sqlite_uint64 dirid; + + rc = sqlite3_prepare_v2(db, "SELECT id FROM wallpaper WHERE filepath = ? LIMIT 1", -1, &stmt, NULL); + if(rc != SQLITE_OK) { + return 0; + } + + rc = sqlite3_bind_text(stmt, 1, path, -1, SQLITE_STATIC); + if(rc != SQLITE_OK) { + sqlite3_finalize(stmt); + return 0; + } + + rc = sqlite3_step(stmt); + if(rc == SQLITE_ROW) { + dirid = sqlite3_column_int64(stmt, 0); + sqlite3_finalize(stmt); + return dirid; + } + + sqlite3_finalize(stmt); + return 0; +} + int db_get_wallpapers(sqlite_uint64 dirid, GArray **array) { struct wallpaper_t temp, *temp2; sqlite3_stmt *stmt; diff --git a/db.h b/db.h index 3e17d9c..18d6cf2 100644 --- a/db.h +++ b/db.h @@ -24,6 +24,7 @@ sqlite_uint64 db_get_directory(const char*); 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_get_wallpapers(sqlite_uint64, GArray**); #endif diff --git a/wallpapers.c b/wallpapers.c index 525a6c4..53ea102 100644 --- a/wallpapers.c +++ b/wallpapers.c @@ -10,17 +10,20 @@ #include #include #include +#include -#include "db.h" +#include "wallpapers.h" -void listdir(const char *path, sqlite_uint64 parent) { +void add_dir_recursive(const char *path, sqlite_uint64 parent) { int pathlen = strlen(path); - //DIR *dir = opendir(path); - //struct dirent *de; GDir *dir; const char *filename; sqlite_uint64 dir_temp; sqlite_uint64 dirid; + char *filepath; + struct stat st; + GdkPixbuf *pixbuf; + GError *error; dirid = db_get_directory(path); if(dirid == 0) { @@ -34,18 +37,24 @@ void listdir(const char *path, sqlite_uint64 parent) { return; while((filename = g_dir_read_name(dir)) != NULL) { - char *filepath = g_strdup_printf("%s/%s", path, filename); + filepath = g_strdup_printf("%s/%s", path, filename); + + if(db_get_wallpaper(filepath) != 0) { + continue; + } + if(g_access(filepath, R_OK) == -1) { fprintf(stderr, "Can't read %s: \n", filepath, strerror(errno)); g_free(filepath); continue; } - struct stat st; + if(g_stat(filepath, &st) == -1) { fprintf(stderr, "Failed to stat file %s\n", filepath); g_free(filepath); continue; } + switch(st.st_mode & S_IFMT) { case S_IFDIR: if(strcmp(filepath, ".") == 0 || strcmp(filepath, "..") == 0) { @@ -53,7 +62,8 @@ void listdir(const char *path, sqlite_uint64 parent) { continue; } printf("Directory: %s\n", filepath); - listdir(filepath, dirid); + add_dir_recursive(filepath, dirid); + g_free(filepath); continue; case S_IFLNK: case S_IFREG: @@ -63,19 +73,18 @@ void listdir(const char *path, sqlite_uint64 parent) { g_free(filepath); continue; } - Imlib_Image image; - image = imlib_load_image(filepath); - if(image) { - imlib_context_set_image(image); - printf("%s loaded: %dx%d\n", filepath, imlib_image_get_width(), imlib_image_get_height()); - if(db_add_wallpaper(filepath, dirid, st.st_size, imlib_image_get_width(), imlib_image_get_height())) - printf("added\n"); - else - printf("failed to add\n"); - imlib_free_image(); - } else { - fprintf(stderr, "%s failed\n", filepath); + + pixbuf = gdk_pixbuf_new_from_file(filepath, &error); + if(!pixbuf) { + g_warning("%s", error->message); + continue; } + printf("%s loaded: %dx%d\n", filepath, gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf)); + if(db_add_wallpaper(filepath, dirid, st.st_size, gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf))) + printf("added\n"); + else + printf("failed to add\n"); + g_object_unref(pixbuf); g_free(filepath); } g_dir_close(dir); diff --git a/wallpapers.h b/wallpapers.h new file mode 100644 index 0000000..174b34f --- /dev/null +++ b/wallpapers.h @@ -0,0 +1,8 @@ +#ifndef _WALLPAPERS_H_ +#define _WALLPAPERS_H_ + +#include "db.h" + +void add_dir_recursive(const char*, sqlite_uint64); + +#endif diff --git a/walls.ui b/walls.ui index bd57bbd..f7350f8 100644 --- a/walls.ui +++ b/walls.ui @@ -20,6 +20,16 @@ True + + + Add _Directory + True + add_dir_action + True + image1 + False + + True @@ -143,4 +153,14 @@ + + True + gtk-directory + 1 + + + Add _Directory + gtk-directory + + diff --git a/window_main.c b/window_main.c index 47c70ec..96a596b 100644 --- a/window_main.c +++ b/window_main.c @@ -6,6 +6,7 @@ #include "walls_ui.h" #include "db.h" #include "walls_model.h" +#include "wallpapers.h" GdkPixbuf *orig_pixbuf = NULL; gint last_width = 0, @@ -106,7 +107,7 @@ static void resize_pixbuf() { gtk_widget_set_size_request(GTK_WIDGET(image), width, height); gtk_widget_set_uposition(GTK_WIDGET(image), (width < win_width ? win_width / 2 - width / 2 : 0), (height < win_height ? win_height / 2 - height / 2 : 0)); gtk_image_set_from_pixbuf(image, pb); - gdk_pixbuf_unref(pb); + g_object_unref(pb); } void on_window_size_allocate(GtkWidget *widget, GtkAllocation *allocation, gpointer user_data) { @@ -135,7 +136,7 @@ static void load_pixbuf(struct wallpaper_t *wall) { gdouble scalex, scaley, width_ratio, height_ratio, max_ratio; if(orig_pixbuf) - gdk_pixbuf_unref(orig_pixbuf); + g_object_unref(orig_pixbuf); orig_pixbuf = gdk_pixbuf_new_from_file(wall->filepath, &error); if(!orig_pixbuf) { g_warning("%s", error->message); @@ -159,6 +160,18 @@ void on_filetree_selection_changed(GtkTreeSelection *treeselection, gpointer use } } +void on_add_dir_action_activate(GtkAction *action, gpointer user_data) { + GtkWidget *dialog; + + dialog = gtk_file_chooser_dialog_new("Choose Directory", NULL, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, "_Select"); + if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { + char *directory; + directory = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); + add_dir_recursive(directory, 0); + } + gtk_widget_destroy(dialog); +} + int gui_main(int argc, char **argv) { GtkWidget *window; GtkWidget *filetree; -- cgit v1.2.3