summaryrefslogtreecommitdiff
path: root/db.c
blob: 2fbaed8ca3999fcae388b5845afc1b0f22ff2c92 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <glib/gprintf.h>

#include "db.h"

sqlite3 *db = NULL;

void db_close();

static int db_create_tables() {
	char *errmsg;
	int rc;
	rc = sqlite3_exec(db,
				"create table directory ("
				"	id integer not null,"
				"	parent integer null,"
				"	name varchar(255),"
				"	primary key (id)"
				");"
				"create table wallpaper ("
				"	id integer not null,"
				"	dirid integer not null,"
				"	filepath text not null,"
				"	size integer not null,"
				"	width integer not null,"
				"	height integer not null,"
				"	primary key (id)"
				");"
				"create table tag ("
				"	id integer not null,"
				"	name varchar(100) not null,"
				"	primary key (id)"
				");"
				"create table walltags ("
				"	wallid integer not null,"
				"	tagid integer not null,"
				"	primary key (wallid, tagid)"
				");",
				NULL, NULL, &errmsg);
	if(rc != SQLITE_OK) {
		fprintf(stderr, "db_create_tables failed: %s\n", errmsg);
		sqlite3_free(errmsg);
		return 0;
	}
	return 1;
}

int db_open() {
	int rc;
	int configfound;
	char *dbfile;
	char *configdir;

	configdir = g_strdup_printf("%s/walls", g_get_user_config_dir());
	if(access(configdir, F_OK) == -1 && g_mkdir_with_parents(configdir, 0700) == -1) {
		g_free(configdir);
		return 0;
	}
	g_free(configdir);

	dbfile = g_strdup_printf("%s/walls/db", g_get_user_config_dir());
	printf("db file: %s\n", dbfile);
	configfound = access(dbfile, F_OK) == 0;

	rc = sqlite3_open(dbfile, &db);
	g_free(dbfile);

	if(rc != SQLITE_OK) {
		return 0;
	}

	if(!configfound && !db_create_tables()) {
		db_close();
		return 0;
	}

	return 1;
}

void db_close() {
	if(db) {
		sqlite3_close(db);
		db = NULL;
	}
}

sqlite_uint64 db_add_directory(const char *path, sqlite_uint64 parent) {
	sqlite3_stmt *stmt;
	int rc;

	if(parent) {
		rc = sqlite3_prepare_v2(db, "INSERT INTO directory (name, parent) VALUES (?, ?)", -1, &stmt, NULL);
	} else {
		rc = sqlite3_prepare_v2(db, "INSERT INTO directory (name) VALUES (?)", -1, &stmt, NULL);
	}

	if(rc != SQLITE_OK) {
		return 0;
	}

	rc = sqlite3_bind_text(stmt, 1, path, -1, SQLITE_STATIC);
	if(rc != SQLITE_OK) {
		return 0;
	}

	if(parent) {
		rc = sqlite3_bind_int(stmt, 2, parent);
		if(rc != SQLITE_OK) {
			return 0;
		}
	}

	rc = sqlite3_step(stmt);

	sqlite3_finalize(stmt);
	if(rc == SQLITE_DONE) {
		return sqlite3_last_insert_rowid(db);
	} else {
		return 0;
	}
}

sqlite_uint64 db_get_directory(const char *path) {
	sqlite3_stmt *stmt;
	int rc;
	sqlite_uint64 dirid;

	rc = sqlite3_prepare_v2(db, "SELECT id FROM directory WHERE name = ? LIMIT 1", -1, &stmt, NULL);
	if(rc != SQLITE_OK) {
		return 0;
	}

	rc = sqlite3_bind_text(stmt, 1, path, -1, SQLITE_STATIC);
	if(rc != SQLITE_OK) {
		sqlite3_finalize(stmt);
		return 0;
	}

	rc = sqlite3_step(stmt);
	if(rc == SQLITE_ROW) {
		dirid = sqlite3_column_int64(stmt, 0);
		sqlite3_finalize(stmt);
		return dirid;
	}

	sqlite3_finalize(stmt);
	return 0;
}

int db_get_top_level_directories(GArray **array) {
	struct directory_t temp, *temp2;
	sqlite3_stmt *stmt;
	int rc;

	rc = sqlite3_prepare_v2(db, "SELECT id, name FROM directory WHERE parent ISNULL ORDER BY name", -1, &stmt, NULL);
	if(rc != SQLITE_OK) {
		return 0;
	}

	*array = g_array_new(TRUE, FALSE, sizeof(struct directory_t));
	while((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
		temp.name = g_strdup(sqlite3_column_text(stmt, 1));
		temp.dirid = sqlite3_column_int64(stmt, 0);
		g_array_append_val(*array, temp);
	}

	sqlite3_finalize(stmt);

	if(rc != SQLITE_DONE) {
		for(int i = 0; (*array)->len; i++) {
			temp2 = &g_array_index(*array, struct directory_t, i);
			g_free(temp2->name);
		}
		g_array_free(*array, TRUE);
		return 0;
	}

	return 1;
}

int db_get_directories(sqlite_uint64 parent, GArray **array) {
	struct directory_t temp, *temp2;
	sqlite3_stmt *stmt;
	int rc;

	rc = sqlite3_prepare_v2(db, "SELECT id, name FROM directory WHERE parent = ? ORDER BY name", -1, &stmt, NULL);
	if(rc != SQLITE_OK) {
		return 0;
	}

	rc = sqlite3_bind_int64(stmt, 1, parent);
	if(rc != SQLITE_OK) {
		sqlite3_finalize(stmt);
		return 0;
	}

	*array = g_array_new(TRUE, FALSE, sizeof(struct directory_t));
	while((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
		temp.name = g_strdup(sqlite3_column_text(stmt, 1));
		temp.dirid = sqlite3_column_int64(stmt, 0);
		g_array_append_val(*array, temp);
	}

	sqlite3_finalize(stmt);

	if(rc != SQLITE_DONE) {
		for(int i = 0; i < (*array)->len; i++) {
			temp2 = &g_array_index(*array, struct directory_t, i);
			g_free(temp2->name);
		}
		g_array_free(*array, TRUE);
		return 0;
	}

	return 1;
}

sqlite_uint64 db_add_wallpaper(const char *filepath, sqlite_uint64 dirid, int size, int width, int height) {
	sqlite3_stmt *stmt;
	int rc;

	rc = sqlite3_prepare_v2(db, "INSERT INTO wallpaper (dirid, filepath, size, width, height) VALUES (?, ?, ?, ?, ?)", -1, &stmt, NULL);

	if(rc != SQLITE_OK) {
		return 0;
	}

	rc = sqlite3_bind_int64(stmt, 1, dirid);
	if(rc != SQLITE_OK) {
		sqlite3_finalize(stmt);
		return 0;
	}

	rc = sqlite3_bind_text(stmt, 2, filepath, -1, SQLITE_STATIC);
	if(rc != SQLITE_OK) {
		sqlite3_finalize(stmt);
		return 0;
	}

	rc = sqlite3_bind_int(stmt, 3, size);
	if(rc != SQLITE_OK) {
		sqlite3_finalize(stmt);
		return 0;
	}

	rc = sqlite3_bind_int(stmt, 4, width);
	if(rc != SQLITE_OK) {
		sqlite3_finalize(stmt);
		return 0;
	}

	rc = sqlite3_bind_int(stmt, 5, height);
	if(rc != SQLITE_OK) {
		sqlite3_finalize(stmt);
		return 0;
	}

	rc = sqlite3_step(stmt);

	sqlite3_finalize(stmt);
	if(rc == SQLITE_DONE) {
		return sqlite3_last_insert_rowid(db);
	} else {
		return 0;
	}
}

int db_get_wallpapers(sqlite_uint64 dirid, GArray **array) {
	struct wallpaper_t temp, *temp2;
	sqlite3_stmt *stmt;
	int rc;

	rc = sqlite3_prepare_v2(db, "SELECT id, filepath, size, width, height FROM wallpaper WHERE dirid = ? ORDER BY filepath", -1, &stmt, NULL);

	if(rc != SQLITE_OK) {
		return 0;
	}

	rc = sqlite3_bind_int64(stmt, 1, dirid);
	if(rc != SQLITE_OK) {
		sqlite3_finalize(stmt);
		return 0;
	}

	*array = g_array_new(TRUE, FALSE, sizeof(struct wallpaper_t));
	while((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
		temp.filepath = g_strdup(sqlite3_column_text(stmt, 1));
		temp.id = sqlite3_column_int64(stmt, 1);
		temp.size = sqlite3_column_int(stmt, 2);
		temp.width = sqlite3_column_int(stmt, 3);
		temp.height = sqlite3_column_int(stmt, 4);
		g_array_append_val(*array, temp);
	}

	sqlite3_finalize(stmt);

	if(rc != SQLITE_DONE) {
		for(int i = 0; i < (*array)->len; i++) {
			temp2 = &g_array_index(*array, struct wallpaper_t, i);
			g_free(temp2->filepath);
		}
		return 0;
	}

	return 1;
}