2011-10-11 15:59:36 +02:00
|
|
|
/* See LICENSE file for license and copyright information */
|
|
|
|
|
2011-11-21 16:40:26 +01:00
|
|
|
#define _POSIX_SOURCE
|
|
|
|
|
2011-10-11 22:39:55 +02:00
|
|
|
#include <glib.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-10-14 01:04:06 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2011-10-11 22:39:55 +02:00
|
|
|
#include <unistd.h>
|
2011-10-23 17:01:15 +02:00
|
|
|
#include <girara/utils.h>
|
|
|
|
#include <girara/datastructures.h>
|
2011-10-11 15:59:36 +02:00
|
|
|
|
|
|
|
#include "database.h"
|
|
|
|
|
2011-10-11 23:18:21 +02:00
|
|
|
#define BOOKMARKS "bookmarks"
|
|
|
|
#define HISTORY "history"
|
|
|
|
|
2011-10-12 12:43:36 +02:00
|
|
|
#define KEY_PAGE "page"
|
|
|
|
#define KEY_OFFSET "offset"
|
|
|
|
#define KEY_SCALE "scale"
|
2012-02-08 22:23:45 +01:00
|
|
|
#define KEY_ROTATE "rotate"
|
2011-10-11 22:39:55 +02:00
|
|
|
|
2011-10-14 01:04:06 +02:00
|
|
|
#define file_lock_set(fd, cmd) \
|
|
|
|
{ \
|
|
|
|
struct flock lock = { .l_type = cmd, .l_start = 0, .l_whence = SEEK_SET, .l_len = 0}; \
|
|
|
|
fcntl(fd, F_SETLK, lock); \
|
|
|
|
}
|
|
|
|
|
2011-10-11 22:39:55 +02:00
|
|
|
/* forward declaration */
|
|
|
|
static bool zathura_db_check_file(const char* path);
|
2011-10-16 20:28:58 +02:00
|
|
|
static GKeyFile* zathura_db_read_key_file_from_file(const char* path);
|
2011-10-12 12:43:36 +02:00
|
|
|
static void zathura_db_write_key_file_to_file(const char* file, GKeyFile* key_file);
|
2011-10-11 23:43:30 +02:00
|
|
|
static void cb_zathura_db_watch_file(GFileMonitor* monitor, GFile* file, GFile*
|
|
|
|
other_file, GFileMonitorEvent event, zathura_database_t* database);
|
2011-10-11 22:39:55 +02:00
|
|
|
|
2011-10-11 15:59:36 +02:00
|
|
|
struct zathura_database_s
|
|
|
|
{
|
2011-10-11 22:39:55 +02:00
|
|
|
char* bookmark_path;
|
2011-10-12 12:32:31 +02:00
|
|
|
GKeyFile* bookmarks;
|
2011-10-11 22:39:55 +02:00
|
|
|
GFileMonitor* bookmark_monitor;
|
|
|
|
|
|
|
|
char* history_path;
|
2011-10-12 12:43:36 +02:00
|
|
|
GKeyFile* history;
|
2011-10-11 22:39:55 +02:00
|
|
|
GFileMonitor* history_monitor;
|
2011-10-11 15:59:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
zathura_database_t*
|
|
|
|
zathura_db_init(const char* dir)
|
|
|
|
{
|
2011-10-11 22:39:55 +02:00
|
|
|
if (dir == NULL) {
|
|
|
|
goto error_ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
zathura_database_t* db = calloc(1, sizeof(zathura_database_t));
|
|
|
|
if (db == NULL) {
|
|
|
|
goto error_ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* bookmarks */
|
|
|
|
db->bookmark_path = g_build_filename(dir, BOOKMARKS, NULL);
|
|
|
|
if (db->bookmark_path == NULL ||
|
|
|
|
zathura_db_check_file(db->bookmark_path) == false) {
|
|
|
|
goto error_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
GFile* bookmark_file = g_file_new_for_path(db->bookmark_path);
|
|
|
|
if (bookmark_file != NULL) {
|
|
|
|
db->bookmark_monitor = g_file_monitor(bookmark_file, G_FILE_MONITOR_NONE, NULL, NULL);
|
|
|
|
} else {
|
|
|
|
goto error_free;
|
|
|
|
}
|
|
|
|
g_object_unref(bookmark_file);
|
|
|
|
|
2011-10-11 23:43:30 +02:00
|
|
|
g_signal_connect(
|
|
|
|
G_OBJECT(db->bookmark_monitor),
|
|
|
|
"changed",
|
|
|
|
G_CALLBACK(cb_zathura_db_watch_file),
|
|
|
|
db
|
|
|
|
);
|
|
|
|
|
2011-10-12 12:43:36 +02:00
|
|
|
db->bookmarks = zathura_db_read_key_file_from_file(db->bookmark_path);
|
2011-10-12 12:32:31 +02:00
|
|
|
if (db->bookmarks == NULL) {
|
|
|
|
goto error_free;
|
|
|
|
}
|
|
|
|
|
2011-10-11 22:39:55 +02:00
|
|
|
/* history */
|
|
|
|
db->history_path = g_build_filename(dir, HISTORY, NULL);
|
|
|
|
if (db->history_path == NULL ||
|
|
|
|
zathura_db_check_file(db->history_path) == false) {
|
|
|
|
goto error_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
GFile* history_file = g_file_new_for_path(db->history_path);
|
|
|
|
if (history_file != NULL) {
|
|
|
|
db->history_monitor = g_file_monitor(history_file, G_FILE_MONITOR_NONE, NULL, NULL);
|
|
|
|
} else {
|
|
|
|
goto error_free;
|
|
|
|
}
|
|
|
|
g_object_unref(history_file);
|
|
|
|
|
2011-10-11 23:43:30 +02:00
|
|
|
g_signal_connect(
|
|
|
|
G_OBJECT(db->history_monitor),
|
|
|
|
"changed",
|
|
|
|
G_CALLBACK(cb_zathura_db_watch_file),
|
|
|
|
db
|
|
|
|
);
|
|
|
|
|
2011-10-12 12:43:36 +02:00
|
|
|
db->history = zathura_db_read_key_file_from_file(db->history_path);
|
2011-10-11 22:39:55 +02:00
|
|
|
if (db->history == NULL) {
|
|
|
|
goto error_free;
|
|
|
|
}
|
|
|
|
|
|
|
|
return db;
|
|
|
|
|
|
|
|
error_free:
|
|
|
|
|
|
|
|
zathura_db_free(db);
|
|
|
|
|
|
|
|
error_ret:
|
|
|
|
|
2011-10-11 15:59:36 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
zathura_db_free(zathura_database_t* db)
|
|
|
|
{
|
2011-10-11 22:39:55 +02:00
|
|
|
if (db == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* bookmarks */
|
|
|
|
g_free(db->bookmark_path);
|
|
|
|
|
|
|
|
if (db->bookmark_monitor != NULL) {
|
|
|
|
g_object_unref(db->bookmark_monitor);
|
|
|
|
}
|
|
|
|
|
2011-10-12 12:32:31 +02:00
|
|
|
if (db->bookmarks != NULL) {
|
|
|
|
g_key_file_free(db->bookmarks);
|
|
|
|
}
|
2011-10-11 22:39:55 +02:00
|
|
|
|
|
|
|
/* history */
|
|
|
|
g_free(db->history_path);
|
|
|
|
|
|
|
|
if (db->history_monitor != NULL) {
|
|
|
|
g_object_unref(db->history_monitor);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* database */
|
|
|
|
free(db);
|
2011-10-11 15:59:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
zathura_db_add_bookmark(zathura_database_t* db, const char* file,
|
|
|
|
zathura_bookmark_t* bookmark)
|
|
|
|
{
|
2011-10-12 12:32:31 +02:00
|
|
|
if (db == NULL || db->bookmarks == NULL || db->bookmark_path == NULL || file
|
|
|
|
== NULL || bookmark == NULL || bookmark->id == NULL) {
|
2011-10-11 22:39:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-10-12 12:32:31 +02:00
|
|
|
g_key_file_set_integer(db->bookmarks, file, bookmark->id, bookmark->page);
|
|
|
|
|
2011-10-12 12:43:36 +02:00
|
|
|
zathura_db_write_key_file_to_file(db->bookmark_path, db->bookmarks);
|
2011-10-12 12:32:31 +02:00
|
|
|
|
|
|
|
return true;
|
2011-10-11 15:59:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
zathura_db_remove_bookmark(zathura_database_t* db, const char* file, const char*
|
|
|
|
id)
|
|
|
|
{
|
2011-10-12 12:32:31 +02:00
|
|
|
if (db == NULL || db->bookmarks == NULL || db->bookmark_path == NULL || file
|
|
|
|
== NULL || id == NULL) {
|
2011-10-11 22:39:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-10-12 12:32:31 +02:00
|
|
|
if (g_key_file_has_group(db->bookmarks, file) == TRUE) {
|
|
|
|
g_key_file_remove_group(db->bookmarks, file, NULL);
|
|
|
|
|
2011-10-12 12:43:36 +02:00
|
|
|
zathura_db_write_key_file_to_file(db->bookmark_path, db->bookmarks);
|
2011-10-12 12:32:31 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-11 15:59:36 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
girara_list_t*
|
|
|
|
zathura_db_load_bookmarks(zathura_database_t* db, const char* file)
|
|
|
|
{
|
2011-10-12 12:32:31 +02:00
|
|
|
if (db == NULL || db->bookmarks == NULL || file == NULL) {
|
2011-10-11 22:39:55 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-10-12 12:32:31 +02:00
|
|
|
if (g_key_file_has_group(db->bookmarks, file) == FALSE) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-10-15 18:42:30 +02:00
|
|
|
girara_list_t* result = girara_sorted_list_new2((girara_compare_function_t) zathura_bookmarks_compare,
|
|
|
|
(girara_free_function_t) zathura_bookmark_free);
|
2011-10-12 12:32:31 +02:00
|
|
|
if (result == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
girara_list_set_free_function(result, (girara_free_function_t) zathura_bookmark_free);
|
|
|
|
|
|
|
|
gsize length;
|
|
|
|
char** keys = g_key_file_get_keys(db->bookmarks, file, &length, NULL);
|
|
|
|
if (keys == NULL) {
|
|
|
|
girara_list_free(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (gsize i = 0; i < length; i++) {
|
|
|
|
zathura_bookmark_t* bookmark = g_malloc0(sizeof(zathura_bookmark_t));
|
|
|
|
|
|
|
|
bookmark->id = g_strdup(keys[i]);
|
|
|
|
bookmark->page = g_key_file_get_integer(db->bookmarks, file, keys[i], NULL);
|
|
|
|
|
|
|
|
girara_list_append(result, bookmark);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2011-10-11 15:59:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned int
|
2012-02-08 22:23:45 +01:00
|
|
|
page, int offset, double scale, int rotation)
|
2011-10-11 15:59:36 +02:00
|
|
|
{
|
2011-10-11 23:18:21 +02:00
|
|
|
if (db == NULL || db->history == NULL || file == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-10-12 12:43:36 +02:00
|
|
|
char* tmp = g_strdup_printf("%f", scale);
|
2011-10-11 23:18:21 +02:00
|
|
|
|
2011-10-12 12:43:36 +02:00
|
|
|
g_key_file_set_integer(db->history, file, KEY_PAGE, page);
|
|
|
|
g_key_file_set_integer(db->history, file, KEY_OFFSET, offset);
|
|
|
|
g_key_file_set_string (db->history, file, KEY_SCALE, tmp);
|
2012-02-08 22:23:45 +01:00
|
|
|
g_key_file_set_integer(db->history, file, KEY_ROTATE, rotation);
|
2011-10-11 23:18:21 +02:00
|
|
|
|
2011-10-12 12:43:36 +02:00
|
|
|
g_free(tmp);
|
2011-10-11 23:18:21 +02:00
|
|
|
|
2011-10-12 12:43:36 +02:00
|
|
|
zathura_db_write_key_file_to_file(db->history_path, db->history);
|
2011-10-11 23:18:21 +02:00
|
|
|
|
2011-10-12 11:45:00 +02:00
|
|
|
return true;
|
2011-10-11 15:59:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
zathura_db_get_fileinfo(zathura_database_t* db, const char* file, unsigned int*
|
2012-02-08 22:23:45 +01:00
|
|
|
page, int* offset, double* scale, int* rotation)
|
2011-10-11 15:59:36 +02:00
|
|
|
{
|
2011-10-11 23:18:21 +02:00
|
|
|
if (db == NULL || db->history == NULL || file == NULL || page == NULL ||
|
2012-02-08 22:23:45 +01:00
|
|
|
offset == NULL || scale == NULL || rotation == NULL) {
|
2011-10-11 22:39:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-10-12 12:43:36 +02:00
|
|
|
if (g_key_file_has_group(db->history, file) == FALSE) {
|
|
|
|
return false;
|
2011-10-11 23:18:21 +02:00
|
|
|
}
|
|
|
|
|
2011-10-12 12:43:36 +02:00
|
|
|
*page = g_key_file_get_integer(db->history, file, KEY_PAGE, NULL);
|
|
|
|
*offset = g_key_file_get_integer(db->history, file, KEY_OFFSET, NULL);
|
2011-10-20 15:55:10 +02:00
|
|
|
*scale = strtod(g_key_file_get_string(db->history, file, KEY_SCALE, NULL), NULL);
|
2012-02-08 22:23:45 +01:00
|
|
|
*rotation = g_key_file_get_integer(db->history, file, KEY_ROTATE, NULL);
|
2011-10-12 12:43:36 +02:00
|
|
|
|
|
|
|
return true;
|
2011-10-11 15:59:36 +02:00
|
|
|
}
|
2011-10-11 22:39:55 +02:00
|
|
|
|
|
|
|
static bool
|
|
|
|
zathura_db_check_file(const char* path)
|
|
|
|
{
|
|
|
|
if (path == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_file_test(path, G_FILE_TEST_EXISTS) == false) {
|
|
|
|
FILE* file = fopen(path, "w");
|
|
|
|
if (file != NULL) {
|
|
|
|
fclose(file);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-10-12 12:32:31 +02:00
|
|
|
static GKeyFile*
|
2011-10-16 20:28:58 +02:00
|
|
|
zathura_db_read_key_file_from_file(const char* path)
|
2011-10-11 22:39:55 +02:00
|
|
|
{
|
|
|
|
if (path == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-10-14 01:24:42 +02:00
|
|
|
/* open file */
|
2011-11-21 16:40:26 +01:00
|
|
|
FILE* file = fopen(path, "r");
|
|
|
|
if (file == NULL) {
|
2011-10-14 01:24:42 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-10-12 12:32:31 +02:00
|
|
|
GKeyFile* key_file = g_key_file_new();
|
|
|
|
if (key_file == NULL) {
|
2011-11-21 16:40:26 +01:00
|
|
|
fclose(file);
|
2011-10-14 01:24:42 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read config file */
|
2011-11-21 16:40:26 +01:00
|
|
|
file_lock_set(fileno(file), F_WRLCK);
|
|
|
|
char* content = girara_file_read2(file);
|
|
|
|
file_lock_set(fileno(file), F_UNLCK);
|
|
|
|
fclose(file);
|
2011-10-14 01:24:42 +02:00
|
|
|
if (content == NULL) {
|
2011-11-21 16:40:26 +01:00
|
|
|
g_key_file_free(key_file);
|
2011-10-11 23:18:21 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-10-14 01:24:42 +02:00
|
|
|
|
|
|
|
/* parse config file */
|
2011-10-16 20:28:58 +02:00
|
|
|
size_t contentlen = strlen(content);
|
|
|
|
if (contentlen == 0) {
|
|
|
|
static const char dummy_content[] = "# nothing";
|
|
|
|
static const size_t dummy_len = sizeof(dummy_content) - 1;
|
|
|
|
|
|
|
|
free(content);
|
|
|
|
content = malloc(sizeof(char) * (dummy_len + 1));
|
2011-10-17 09:37:22 +02:00
|
|
|
content = memcpy(content, dummy_content, dummy_len + 1);
|
2011-10-16 20:28:58 +02:00
|
|
|
contentlen = dummy_len;
|
|
|
|
}
|
|
|
|
|
2011-10-12 12:32:31 +02:00
|
|
|
GError* error = NULL;
|
2011-10-16 20:28:58 +02:00
|
|
|
if (g_key_file_load_from_data(key_file, content, contentlen,
|
2011-10-12 12:32:31 +02:00
|
|
|
G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error) ==
|
|
|
|
FALSE) {
|
|
|
|
if (error->code != 1) /* ignore empty file */ {
|
2011-10-14 01:24:42 +02:00
|
|
|
free(content);
|
2011-10-12 12:32:31 +02:00
|
|
|
g_key_file_free(key_file);
|
|
|
|
g_error_free(error);
|
2011-10-14 01:24:42 +02:00
|
|
|
return NULL;
|
2011-10-12 12:32:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
g_error_free(error);
|
|
|
|
}
|
|
|
|
|
2011-10-14 01:24:42 +02:00
|
|
|
free(content);
|
|
|
|
|
2011-10-12 12:32:31 +02:00
|
|
|
return key_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-10-12 12:43:36 +02:00
|
|
|
zathura_db_write_key_file_to_file(const char* file, GKeyFile* key_file)
|
2011-10-12 12:32:31 +02:00
|
|
|
{
|
2011-10-12 12:43:36 +02:00
|
|
|
if (file == NULL || key_file == NULL) {
|
2011-10-12 12:32:31 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-12 12:43:36 +02:00
|
|
|
gchar* content = g_key_file_to_data(key_file, NULL, NULL);
|
2011-10-12 12:32:31 +02:00
|
|
|
if (content == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-14 01:04:06 +02:00
|
|
|
/* open file */
|
|
|
|
int fd = open(file, O_RDWR);
|
|
|
|
if (fd == -1) {
|
2011-10-12 12:32:31 +02:00
|
|
|
g_free(content);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-14 01:04:06 +02:00
|
|
|
file_lock_set(fd, F_WRLCK);
|
2011-11-21 16:40:26 +01:00
|
|
|
if (write(fd, content, strlen(content)) == 0) {
|
|
|
|
girara_error("Failed to write to %s", file);
|
|
|
|
}
|
2011-10-14 01:04:06 +02:00
|
|
|
file_lock_set(fd, F_UNLCK);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
2011-10-12 12:32:31 +02:00
|
|
|
g_free(content);
|
2011-10-11 22:39:55 +02:00
|
|
|
}
|
|
|
|
|
2011-10-11 23:43:30 +02:00
|
|
|
static void
|
|
|
|
cb_zathura_db_watch_file(GFileMonitor* UNUSED(monitor), GFile* file, GFile* UNUSED(other_file),
|
|
|
|
GFileMonitorEvent event, zathura_database_t* database)
|
|
|
|
{
|
|
|
|
if (event != G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT || database == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* path = g_file_get_path(file);
|
|
|
|
if (path == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (database->bookmark_path && strcmp(database->bookmark_path, path) == 0) {
|
2011-10-16 20:28:58 +02:00
|
|
|
database->bookmarks = zathura_db_read_key_file_from_file(database->bookmark_path);
|
2011-10-11 23:43:30 +02:00
|
|
|
} else if (database->history_path && strcmp(database->history_path, path) == 0) {
|
2011-10-12 12:43:36 +02:00
|
|
|
database->history = zathura_db_read_key_file_from_file(database->history_path);
|
2011-10-11 23:43:30 +02:00
|
|
|
}
|
|
|
|
}
|