summaryrefslogtreecommitdiff
path: root/wallpapers.c
blob: 56a72c68f8ce12aaafb7a885d1fadfb8c2939c41 (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
#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();
}