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. --- wallpapers.c | 47 ++++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 19 deletions(-) (limited to 'wallpapers.c') 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); -- cgit v1.2.3