summaryrefslogtreecommitdiff
path: root/wallpapers.c
blob: fd893f3a403854c33366c316d51049c56c35522a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include <glib.h>
#include <glib/gstdio.h>
#include <glib/gprintf.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#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;
}