#include #include #include #include #include #include #include #include #include #include #include #include "wallpapers.h" #include "walls_conf.h" gboolean wallpapers_add_dir(const gchar *path, sqlite_uint64 parent, GtkStatusbar *statusbar, gboolean recursive ) { static guint context_id = 0; GDir *dir; const gchar *filename; sqlite_uint64 dirid; gchar *filepath; gchar *msg; struct stat st; GdkPixbuf *pixbuf; GError *error; gboolean added = FALSE; 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 FALSE; added = TRUE; } dir = g_dir_open(path, 0, NULL); if(!dir) return added; 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; } if(recursive) added |= wallpapers_add_dir(filepath, dirid, statusbar, recursive); 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(); return added; } void wallpapers_remove_missing(struct directory_t *dir, GtkStatusbar *statusbar, gboolean recursive) { static guint context_id = 0; gchar *msg; GArray *array; struct wallpaper_t *wall; gdk_threads_enter(); if(context_id == 0) context_id = gtk_statusbar_get_context_id(statusbar, "Removal of missing files"); msg = g_strdup_printf("Removing missing files from %s", dir->name); gtk_statusbar_push(statusbar, context_id, msg); g_free(msg); gdk_threads_leave(); if(!db_get_wallpapers(dir->dirid, &array)) { gdk_threads_enter(); gtk_statusbar_pop(statusbar, context_id); gtk_statusbar_push(statusbar, context_id, "Failed to get directory wallpapers"); gdk_threads_leave(); g_free(dir->name); return; } for(int i = 0; i < array->len; i++) { wall = &g_array_index(array, struct wallpaper_t, i); if(g_access(wall->filepath, F_OK) == -1) { msg = g_strdup_printf("Removing %s", wall->filepath); gdk_threads_enter(); gtk_statusbar_pop(statusbar, context_id); gtk_statusbar_push(statusbar, context_id, msg); gdk_threads_leave(); g_printf("%s\n", msg); g_free(msg); db_remove_wallpaper(wall->id); } g_free(wall->filepath); } g_array_free(array, TRUE); if(recursive && db_get_directories(dir->dirid, &array)) { for(int i = 0; i < array->len; i++) { struct directory_t *subdir; subdir = &g_array_index(array, struct directory_t, i); wallpapers_remove_missing(subdir, statusbar, recursive); } g_array_free(array, TRUE); } gdk_threads_enter(); gtk_statusbar_pop(statusbar, context_id); gdk_threads_leave(); g_free(dir->name); } 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, config.interp_type); } else { width = img_width; height = img_height; pb = gdk_pixbuf_copy(orig); } *ratio = (gdouble)width / (gdouble)img_width; *_width = width; *_height = height; return pb; } GdkPixbuf *zoom_pixbuf(GdkPixbuf *orig, gdouble zoom) { GdkPixbuf *pb; gint img_width, img_height, width, height; img_width = gdk_pixbuf_get_width(orig); img_height = gdk_pixbuf_get_height(orig); width = img_width * zoom; height = img_height * zoom; pb = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height); gdk_pixbuf_scale(orig, pb, 0, 0, width, height, 0, 0, (gdouble)width / (gdouble)img_width, (gdouble)height / (gdouble)img_height, config.interp_type); return pb; }