#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) { GDir *dir; const gchar *filename; 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: %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(); } 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; gdouble scalex, scaley, width_ratio, height_ratio; img_width = gdk_pixbuf_get_width(orig); img_height = gdk_pixbuf_get_height(orig); /* do we need to resize? */ if(img_width > win_width || img_height > win_height) { /* resize by width */ width_ratio = (gdouble)img_width / (gdouble)win_width; height_ratio = (gdouble)img_height / (gdouble)win_height; if(width_ratio > height_ratio) { width = win_width; height = (gint)(1.0 / width_ratio * img_height); scalex = (gdouble)width / (gdouble)img_width; scaley = (gdouble)height / (gdouble)img_height; } else { /* resize by height */ height = win_height; width = (gint)(1.0 / height_ratio * img_width); scalex = (gdouble)width / (gdouble)img_width; scaley = (gdouble)height / (gdouble)img_height; } if(width == 0 || height == 0) return NULL; pb = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height); gdk_pixbuf_scale(orig, pb, 0, 0, width, height, 0, 0, scalex, scaley, GDK_INTERP_BILINEAR); } else { width = img_width; height = img_height; pb = gdk_pixbuf_copy(orig); } *ratio = (gdouble)width / (gdouble)img_width; *_width = width; *_height = height; return pb; }