summaryrefslogtreecommitdiff
path: root/db.c
diff options
context:
space:
mode:
Diffstat (limited to 'db.c')
-rw-r--r--db.c312
1 files changed, 312 insertions, 0 deletions
diff --git a/db.c b/db.c
new file mode 100644
index 0000000..2fbaed8
--- /dev/null
+++ b/db.c
@@ -0,0 +1,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;
+}