zathura/bookmarks.c

150 lines
3.7 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"
Bookmark the exact position, not just the page number (along with a number of fixes). This patch adds some enhancements/fixes to the bookmarking feature of Zathura: - Bookmark the exact vertical and horizontal adjustments values, along with the page number. This is done in a backward-compatible way for both the plain and sqlite database backends, so that bookmarks that was taken previously (bookmarking only the page number) will still work as expected and won't be lost. - Fix the issue of not being able to remove bookmarks from the plain database; removing a bookmark in plain_remove_bookmark using g_key_file_remove_key corrupts the bookmarks file. This is due to not truncating the entire bookmarks file in zathura_db_write_key_file_to_file prior to overriding it with a new one not containing the removed line. This is why, I guess, plain_remove_bookmark hadn't been implemented as expected using g_key_file_remove_key, because apparently, someone thought that the problem is caused by this API. - Fix not being able to update existing bookmarks persistently; updating a bookmark works only during the current session, but after the file is closed and reopened, the updated bookmark still has it's old value. This is due to changing only the bookmark structure in memory; the proper way to do it, is to call zathura_db_remove_bookmark on the old bookmark, then follow that by a call to zathura_db_add_bookmark on a bookmark structure containing the new position and the same old ID. - Make zathura_bookmark_add updates the bookmark if it already exists, rather than doing this externally in cmd_bookmark_create. This allows us to have all the relevant girara_notify messages in cmd_bookmark_create. Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
2013-06-21 06:12:12 +02:00
#include "adjustment.h"
2011-09-01 15:43:34 +02:00
#include <girara/datastructures.h>
#include <girara/utils.h>
Bookmark the exact position, not just the page number (along with a number of fixes). This patch adds some enhancements/fixes to the bookmarking feature of Zathura: - Bookmark the exact vertical and horizontal adjustments values, along with the page number. This is done in a backward-compatible way for both the plain and sqlite database backends, so that bookmarks that was taken previously (bookmarking only the page number) will still work as expected and won't be lost. - Fix the issue of not being able to remove bookmarks from the plain database; removing a bookmark in plain_remove_bookmark using g_key_file_remove_key corrupts the bookmarks file. This is due to not truncating the entire bookmarks file in zathura_db_write_key_file_to_file prior to overriding it with a new one not containing the removed line. This is why, I guess, plain_remove_bookmark hadn't been implemented as expected using g_key_file_remove_key, because apparently, someone thought that the problem is caused by this API. - Fix not being able to update existing bookmarks persistently; updating a bookmark works only during the current session, but after the file is closed and reopened, the updated bookmark still has it's old value. This is due to changing only the bookmark structure in memory; the proper way to do it, is to call zathura_db_remove_bookmark on the old bookmark, then follow that by a call to zathura_db_add_bookmark on a bookmark structure containing the new position and the same old ID. - Make zathura_bookmark_add updates the bookmark if it already exists, rather than doing this externally in cmd_bookmark_create. This allows us to have all the relevant girara_notify messages in cmd_bookmark_create. Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
2013-06-21 06:12:12 +02:00
#include <girara/session.h>
2011-10-23 20:18:44 +02:00
static int
bookmark_compare_find(const void* item, const void* data)
{
const zathura_bookmark_t* bookmark = item;
const char* id = data;
return g_strcmp0(bookmark->id, id);
}
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);
Bookmark the exact position, not just the page number (along with a number of fixes). This patch adds some enhancements/fixes to the bookmarking feature of Zathura: - Bookmark the exact vertical and horizontal adjustments values, along with the page number. This is done in a backward-compatible way for both the plain and sqlite database backends, so that bookmarks that was taken previously (bookmarking only the page number) will still work as expected and won't be lost. - Fix the issue of not being able to remove bookmarks from the plain database; removing a bookmark in plain_remove_bookmark using g_key_file_remove_key corrupts the bookmarks file. This is due to not truncating the entire bookmarks file in zathura_db_write_key_file_to_file prior to overriding it with a new one not containing the removed line. This is why, I guess, plain_remove_bookmark hadn't been implemented as expected using g_key_file_remove_key, because apparently, someone thought that the problem is caused by this API. - Fix not being able to update existing bookmarks persistently; updating a bookmark works only during the current session, but after the file is closed and reopened, the updated bookmark still has it's old value. This is due to changing only the bookmark structure in memory; the proper way to do it, is to call zathura_db_remove_bookmark on the old bookmark, then follow that by a call to zathura_db_add_bookmark on a bookmark structure containing the new position and the same old ID. - Make zathura_bookmark_add updates the bookmark if it already exists, rather than doing this externally in cmd_bookmark_create. This allows us to have all the relevant girara_notify messages in cmd_bookmark_create. Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
2013-06-21 06:12:12 +02:00
double x = zathura_adjustment_get_ratio(gtk_scrolled_window_get_hadjustment(GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view)));
double y = zathura_adjustment_get_ratio(gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(zathura->ui.session->gtk.view)));
zathura_bookmark_t* old = zathura_bookmark_get(zathura, id);
2011-10-23 20:18:44 +02:00
if (old != NULL) {
Bookmark the exact position, not just the page number (along with a number of fixes). This patch adds some enhancements/fixes to the bookmarking feature of Zathura: - Bookmark the exact vertical and horizontal adjustments values, along with the page number. This is done in a backward-compatible way for both the plain and sqlite database backends, so that bookmarks that was taken previously (bookmarking only the page number) will still work as expected and won't be lost. - Fix the issue of not being able to remove bookmarks from the plain database; removing a bookmark in plain_remove_bookmark using g_key_file_remove_key corrupts the bookmarks file. This is due to not truncating the entire bookmarks file in zathura_db_write_key_file_to_file prior to overriding it with a new one not containing the removed line. This is why, I guess, plain_remove_bookmark hadn't been implemented as expected using g_key_file_remove_key, because apparently, someone thought that the problem is caused by this API. - Fix not being able to update existing bookmarks persistently; updating a bookmark works only during the current session, but after the file is closed and reopened, the updated bookmark still has it's old value. This is due to changing only the bookmark structure in memory; the proper way to do it, is to call zathura_db_remove_bookmark on the old bookmark, then follow that by a call to zathura_db_add_bookmark on a bookmark structure containing the new position and the same old ID. - Make zathura_bookmark_add updates the bookmark if it already exists, rather than doing this externally in cmd_bookmark_create. This allows us to have all the relevant girara_notify messages in cmd_bookmark_create. Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
2013-06-21 06:12:12 +02:00
old->page = page;
old->x = x;
old->y = y;
if (zathura->database != NULL) {
const char* path = zathura_document_get_path(zathura->document);
if (zathura_db_remove_bookmark(zathura->database, path, old->id) == false) {
girara_warning("Failed to remove old bookmark from database.");
}
if (zathura_db_add_bookmark(zathura->database, path, old) == false) {
girara_warning("Failed to add new bookmark to database.");
}
}
return old;
2011-10-23 20:18:44 +02:00
}
2011-09-01 15:43:34 +02:00
zathura_bookmark_t* bookmark = g_malloc0(sizeof(zathura_bookmark_t));
Bookmark the exact position, not just the page number (along with a number of fixes). This patch adds some enhancements/fixes to the bookmarking feature of Zathura: - Bookmark the exact vertical and horizontal adjustments values, along with the page number. This is done in a backward-compatible way for both the plain and sqlite database backends, so that bookmarks that was taken previously (bookmarking only the page number) will still work as expected and won't be lost. - Fix the issue of not being able to remove bookmarks from the plain database; removing a bookmark in plain_remove_bookmark using g_key_file_remove_key corrupts the bookmarks file. This is due to not truncating the entire bookmarks file in zathura_db_write_key_file_to_file prior to overriding it with a new one not containing the removed line. This is why, I guess, plain_remove_bookmark hadn't been implemented as expected using g_key_file_remove_key, because apparently, someone thought that the problem is caused by this API. - Fix not being able to update existing bookmarks persistently; updating a bookmark works only during the current session, but after the file is closed and reopened, the updated bookmark still has it's old value. This is due to changing only the bookmark structure in memory; the proper way to do it, is to call zathura_db_remove_bookmark on the old bookmark, then follow that by a call to zathura_db_add_bookmark on a bookmark structure containing the new position and the same old ID. - Make zathura_bookmark_add updates the bookmark if it already exists, rather than doing this externally in cmd_bookmark_create. This allows us to have all the relevant girara_notify messages in cmd_bookmark_create. Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
2013-06-21 06:12:12 +02:00
2011-09-01 15:43:34 +02:00
bookmark->id = g_strdup(id);
bookmark->page = page;
Bookmark the exact position, not just the page number (along with a number of fixes). This patch adds some enhancements/fixes to the bookmarking feature of Zathura: - Bookmark the exact vertical and horizontal adjustments values, along with the page number. This is done in a backward-compatible way for both the plain and sqlite database backends, so that bookmarks that was taken previously (bookmarking only the page number) will still work as expected and won't be lost. - Fix the issue of not being able to remove bookmarks from the plain database; removing a bookmark in plain_remove_bookmark using g_key_file_remove_key corrupts the bookmarks file. This is due to not truncating the entire bookmarks file in zathura_db_write_key_file_to_file prior to overriding it with a new one not containing the removed line. This is why, I guess, plain_remove_bookmark hadn't been implemented as expected using g_key_file_remove_key, because apparently, someone thought that the problem is caused by this API. - Fix not being able to update existing bookmarks persistently; updating a bookmark works only during the current session, but after the file is closed and reopened, the updated bookmark still has it's old value. This is due to changing only the bookmark structure in memory; the proper way to do it, is to call zathura_db_remove_bookmark on the old bookmark, then follow that by a call to zathura_db_add_bookmark on a bookmark structure containing the new position and the same old ID. - Make zathura_bookmark_add updates the bookmark if it already exists, rather than doing this externally in cmd_bookmark_create. This allows us to have all the relevant girara_notify messages in cmd_bookmark_create. Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
2013-06-21 06:12:12 +02:00
bookmark->x = x;
bookmark->y = y;
2011-09-01 15:43:34 +02:00
girara_list_append(zathura->bookmarks.bookmarks, bookmark);
2012-02-17 00:43:01 +01:00
if (zathura->database != NULL) {
2012-03-30 18:24:00 +02:00
const char* path = zathura_document_get_path(zathura->document);
if (zathura_db_add_bookmark(zathura->database, path, bookmark) == false) {
2011-09-03 13:40:28 +02:00
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);
2012-02-17 00:43:01 +01:00
if (bookmark == NULL) {
2011-09-03 13:40:28 +02:00
return false;
2011-09-01 15:43:34 +02:00
}
2011-09-03 13:40:28 +02:00
2012-02-17 00:43:01 +01:00
if (zathura->database != NULL) {
2012-03-30 18:24:00 +02:00
const char* path = zathura_document_get_path(zathura->document);
if (zathura_db_remove_bookmark(zathura->database, path, bookmark->id) == false) {
2011-09-03 13:40:28 +02:00
girara_warning("Failed to remove bookmark from database.");
}
}
2012-02-17 00:43:01 +01:00
2011-09-03 13:40:28 +02:00
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);
2011-10-23 20:18:44 +02:00
return girara_list_find(zathura->bookmarks.bookmarks, bookmark_compare_find, id);
2011-09-01 15:43:34 +02:00
}
2011-09-03 13:40:28 +02:00
void
zathura_bookmark_free(zathura_bookmark_t* bookmark)
2011-09-01 15:43:34 +02:00
{
2012-02-17 00:43:01 +01:00
if (bookmark == NULL) {
2011-09-01 15:43:34 +02:00
return;
}
g_free(bookmark->id);
g_free(bookmark);
}
bool
2012-10-09 01:12:18 +02:00
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);
2012-02-17 00:43:01 +01:00
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);
2012-02-17 00:43:01 +01:00
if (bookmarks == NULL) {
return false;
}
girara_list_free(zathura->bookmarks.bookmarks);
zathura->bookmarks.bookmarks = bookmarks;
2012-02-17 00:43:01 +01:00
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;
}
2012-02-17 00:43:01 +01:00
2011-10-15 18:42:30 +02:00
if (lhs == NULL) {
return -1;
}
2012-02-17 00:43:01 +01:00
2011-10-15 18:42:30 +02:00
if (rhs == NULL) {
return 1;
}
return g_strcmp0(lhs->id, rhs->id);
}