diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2009-12-22 19:26:54 +0100 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2009-12-22 19:26:54 +0100 |
commit | 4e9cdd282beb1c6dcbcca148745f5665b561c403 (patch) | |
tree | a6ff0bd1d8db019b73f61bfa2c7d3271c1116ed9 /wallpapers.c |
Initial commit.
Diffstat (limited to 'wallpapers.c')
-rw-r--r-- | wallpapers.c | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/wallpapers.c b/wallpapers.c new file mode 100644 index 0000000..525a6c4 --- /dev/null +++ b/wallpapers.c @@ -0,0 +1,82 @@ +#include <stdio.h> +#include <stdlib.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> + +#include <Imlib2.h> +#include <glib.h> +#include <glib/gstdio.h> +#include <glib/gprintf.h> + +#include "db.h" + +void listdir(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; + + dirid = db_get_directory(path); + if(dirid == 0) { + dirid = db_add_directory(path, parent); + if(dirid == 0) + return; + } + + dir = g_dir_open(path, 0, NULL); + if(!dir) + return; + + while((filename = g_dir_read_name(dir)) != NULL) { + char *filepath = g_strdup_printf("%s/%s", path, filename); + 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) { + g_free(filepath); + continue; + } + printf("Directory: %s\n", filepath); + listdir(filepath, dirid); + continue; + case S_IFLNK: + case S_IFREG: + break; + default: + printf("Skipping %s\n", filepath); + 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); + } + g_free(filepath); + } + g_dir_close(dir); +} |