#include #include #include #include #include #include #include #include #include #include #include #include "wallpapers.h" static guint context_id = 0; void add_dir_recursive(const gchar *path, sqlite_uint64 parent, GtkStatusbar *statusbar) { int pathlen = strlen(path); GDir *dir; const gchar *filename; sqlite_uint64 dir_temp; sqlite_uint64 dirid; gchar *filepath; gchar *msg; struct stat st; GdkPixbuf *pixbuf; GError *error; gdk_threads_enter(); if(context_id == 0) context_id = gtk_statusbar_get_context_id(statusbar, "Recursive directory adding"); gtk_statusbar_pop(statusbar, context_id); msg = g_strdup_printf("Adding directory: %s", path); gtk_statusbar_push(statusbar, context_id, msg); g_free(msg); gdk_threads_leave(); 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) { filepath = g_strdup_printf("%s/%s", path, filename); if(db_get_wallpaper(filepath) != 0) { continue; } if(g_access(filepath, R_OK) == -1) { g_warning("Can't read %s: \n", filepath, strerror(errno)); g_free(filepath); continue; } if(g_stat(filepath, &st) == -1) { g_warning("Failed to stat file %s\n", filepath); g_free(filepath); continue; } switch(st.st_mode & S_IFMT) { case S_IFDIR: if(strcmp(filename, ".") == 0 || strcmp(filename, "..") == 0) { g_free(filepath); continue; } add_dir_recursive(filepath, dirid, statusbar); g_free(filepath); gdk_threads_enter(); gtk_statusbar_pop(statusbar, context_id); gdk_threads_leave(); continue; case S_IFLNK: case S_IFREG: break; default: g_free(filepath); continue; } error = NULL; pixbuf = gdk_pixbuf_new_from_file(filepath, &error); if(!pixbuf) { g_warning("%s", error->message); g_error_free(error); continue; } if(db_add_wallpaper(filepath, dirid, st.st_size, gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf))) { msg = g_strdup_printf("%s loaded: %dx%d", filepath, gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf)); } else { msg = g_strdup_printf("Failed to load %s", filepath); } gdk_threads_enter(); gtk_statusbar_pop(statusbar, context_id); gtk_statusbar_push(statusbar, context_id, msg); gdk_threads_leave(); g_free(msg); g_object_unref(pixbuf); g_free(filepath); } g_dir_close(dir); gdk_threads_enter(); gtk_statusbar_pop(statusbar, context_id); msg = g_strdup_printf("Done adding %s", path); gtk_statusbar_push(statusbar, context_id, msg); g_free(msg); gdk_threads_leave(); }