zathura/bookmarks.c

118 lines
3.0 KiB
C
Raw Normal View History

2011-09-01 15:43:34 +02:00
/* See LICENSE file for license and copyright information */
#include <string.h>
#include "bookmarks.h"
#include "database.h"
2011-09-03 13:40:28 +02:00
#include "document.h"
2011-09-01 15:43:34 +02:00
#include <girara/datastructures.h>
#include <girara/utils.h>
2011-09-01 15:43:34 +02:00
zathura_bookmark_t*
zathura_bookmark_add(zathura_t* zathura, const gchar* id, unsigned int page)
{
2011-09-03 13:40:28 +02:00
g_return_val_if_fail(zathura && zathura->document && zathura->bookmarks.bookmarks, NULL);
2011-09-01 15:43:34 +02:00
g_return_val_if_fail(id, NULL);
GIRARA_LIST_FOREACH(zathura->bookmarks.bookmarks, zathura_bookmark_t*, iter, bookmark)
if (strcmp(bookmark->id, id) == 0) {
2011-09-01 15:43:34 +02:00
girara_list_iterator_free(iter);
return NULL;
}
2011-10-17 11:16:14 +02:00
GIRARA_LIST_FOREACH_END(zathura->bookmarks.bookmarks, zathura_bookmark_t*, iter, bookmark);
2011-09-01 15:43:34 +02:00
zathura_bookmark_t* bookmark = g_malloc0(sizeof(zathura_bookmark_t));
bookmark->id = g_strdup(id);
bookmark->page = page;
girara_list_append(zathura->bookmarks.bookmarks, bookmark);
2011-09-03 13:40:28 +02:00
if (zathura->database) {
if (!zathura_db_add_bookmark(zathura->database, zathura->document->file_path, bookmark)) {
girara_warning("Failed to add bookmark to database.");
}
}
2011-09-29 15:23:13 +02:00
2011-09-01 15:43:34 +02:00
return bookmark;
}
2011-09-03 13:40:28 +02:00
bool
zathura_bookmark_remove(zathura_t* zathura, const gchar* id)
2011-09-01 15:43:34 +02:00
{
2011-09-03 13:40:28 +02:00
g_return_val_if_fail(zathura && zathura->document && zathura->bookmarks.bookmarks, false);
g_return_val_if_fail(id, false);
2011-09-01 15:43:34 +02:00
zathura_bookmark_t* bookmark = zathura_bookmark_get(zathura, id);
2011-09-03 13:40:28 +02:00
if (!bookmark) {
return false;
2011-09-01 15:43:34 +02:00
}
2011-09-03 13:40:28 +02:00
if (zathura->database) {
if (!zathura_db_remove_bookmark(zathura->database, zathura->document->file_path, bookmark->id)) {
girara_warning("Failed to remove bookmark from database.");
}
}
girara_list_remove(zathura->bookmarks.bookmarks, bookmark);
return true;
2011-09-01 15:43:34 +02:00
}
2011-09-03 13:40:28 +02:00
zathura_bookmark_t*
zathura_bookmark_get(zathura_t* zathura, const gchar* id)
2011-09-01 15:43:34 +02:00
{
g_return_val_if_fail(zathura && zathura->bookmarks.bookmarks, NULL);
g_return_val_if_fail(id, NULL);
GIRARA_LIST_FOREACH(zathura->bookmarks.bookmarks, zathura_bookmark_t*, iter, bookmark)
if (strcmp(bookmark->id, id) == 0) {
2011-09-01 15:43:34 +02:00
girara_list_iterator_free(iter);
return bookmark;
}
2011-10-17 11:16:14 +02:00
GIRARA_LIST_FOREACH_END(zathura->bookmarks.bookmarks, zathura_bookmark_t*, iter, bookmark);
2011-09-01 15:43:34 +02:00
return NULL;
}
2011-09-03 13:40:28 +02:00
void
zathura_bookmark_free(zathura_bookmark_t* bookmark)
2011-09-01 15:43:34 +02:00
{
if (!bookmark) {
2011-09-01 15:43:34 +02:00
return;
}
g_free(bookmark->id);
g_free(bookmark);
}
bool
zathura_bookmarks_load(zathura_t* zathura, const gchar* file) {
2011-10-03 17:28:14 +02:00
g_return_val_if_fail(zathura, false);
g_return_val_if_fail(file, false);
2011-10-03 17:28:14 +02:00
if (zathura->database == NULL) {
return false;
}
girara_list_t* bookmarks = zathura_db_load_bookmarks(zathura->database, file);
if (!bookmarks) {
return false;
}
girara_list_free(zathura->bookmarks.bookmarks);
zathura->bookmarks.bookmarks = bookmarks;
return true;
}
2011-10-15 18:42:30 +02:00
int
zathura_bookmarks_compare(zathura_bookmark_t* lhs, zathura_bookmark_t* rhs)
{
if (lhs == NULL && rhs == NULL) {
return 0;
}
if (lhs == NULL) {
return -1;
}
if (rhs == NULL) {
return 1;
}
return g_strcmp0(lhs->id, rhs->id);
}