Move jumplist code

This commit is contained in:
Sebastian Ramacher 2015-12-07 12:41:06 +01:00
parent bd25ce0af2
commit f1a5fb4544
2 changed files with 181 additions and 175 deletions

181
zathura/jumplist.c Normal file
View file

@ -0,0 +1,181 @@
#include "zathura.h"
#include "document.h"
#include "database.h"
#include <girara/utils.h>
static void zathura_jumplist_reset_current(zathura_t* zathura);
static void zathura_jumplist_append_jump(zathura_t* zathura);
static void zathura_jumplist_save(zathura_t* zathura);
bool
zathura_jumplist_has_previous(zathura_t* zathura)
{
return girara_list_iterator_has_previous(zathura->jumplist.cur);
}
bool
zathura_jumplist_has_next(zathura_t* zathura)
{
return girara_list_iterator_has_next(zathura->jumplist.cur);
}
zathura_jump_t*
zathura_jumplist_current(zathura_t* zathura)
{
if (zathura->jumplist.cur != NULL) {
return girara_list_iterator_data(zathura->jumplist.cur);
} else {
return NULL;
}
}
void
zathura_jumplist_forward(zathura_t* zathura)
{
if (girara_list_iterator_has_next(zathura->jumplist.cur)) {
girara_list_iterator_next(zathura->jumplist.cur);
}
}
void
zathura_jumplist_backward(zathura_t* zathura)
{
if (girara_list_iterator_has_previous(zathura->jumplist.cur)) {
girara_list_iterator_previous(zathura->jumplist.cur);
}
}
static void
zathura_jumplist_reset_current(zathura_t* zathura)
{
g_return_if_fail(zathura != NULL && zathura->jumplist.cur != NULL);
while (true) {
if (girara_list_iterator_has_next(zathura->jumplist.cur) == false) {
return;
}
girara_list_iterator_next(zathura->jumplist.cur);
}
}
static void
zathura_jumplist_append_jump(zathura_t* zathura)
{
g_return_if_fail(zathura != NULL && zathura->jumplist.list != NULL);
zathura_jump_t* jump = g_try_malloc0(sizeof(zathura_jump_t));
if (jump == NULL) {
return;
}
jump->page = 0;
jump->x = 0.0;
jump->y = 0.0;
girara_list_append(zathura->jumplist.list, jump);
if (zathura->jumplist.size == 0) {
zathura->jumplist.cur = girara_list_iterator(zathura->jumplist.list);
}
++zathura->jumplist.size;
zathura_jumplist_trim(zathura);
}
void
zathura_jumplist_trim(zathura_t* zathura)
{
g_return_if_fail(zathura != NULL && zathura->jumplist.list != NULL && zathura->jumplist.size != 0);
girara_list_iterator_t* cur = girara_list_iterator(zathura->jumplist.list);
while (zathura->jumplist.size > zathura->jumplist.max_size) {
if (girara_list_iterator_data(cur) == girara_list_iterator_data(zathura->jumplist.cur)) {
girara_list_iterator_free(zathura->jumplist.cur);
zathura->jumplist.cur = NULL;
}
girara_list_iterator_remove(cur);
--zathura->jumplist.size;
}
if (zathura->jumplist.size == 0 || (zathura->jumplist.size != 0 && zathura->jumplist.cur != NULL)) {
girara_list_iterator_free(cur);
} else {
zathura->jumplist.cur = cur;
}
}
void
zathura_jumplist_add(zathura_t* zathura)
{
g_return_if_fail(zathura != NULL && zathura->document != NULL && zathura->jumplist.list != NULL);
unsigned int pagenum = zathura_document_get_current_page_number(zathura->document);
double x = zathura_document_get_position_x(zathura->document);
double y = zathura_document_get_position_y(zathura->document);
if (zathura->jumplist.size != 0) {
zathura_jumplist_reset_current(zathura);
zathura_jump_t* cur = zathura_jumplist_current(zathura);
if (cur != NULL) {
if (cur->page == pagenum && cur->x == x && cur->y == y) {
return;
}
}
}
zathura_jumplist_append_jump(zathura);
zathura_jumplist_reset_current(zathura);
zathura_jumplist_save(zathura);
}
bool
zathura_jumplist_load(zathura_t* zathura, const char* file)
{
g_return_val_if_fail(zathura != NULL && file != NULL, false);
if (zathura->database == NULL) {
return false;
}
zathura->jumplist.list = zathura_db_load_jumplist(zathura->database, file);
if (zathura->jumplist.list == NULL) {
girara_error("Failed to load the jumplist from the database");
return false;
}
zathura->jumplist.size = girara_list_size(zathura->jumplist.list);
if (zathura->jumplist.size != 0) {
zathura->jumplist.cur = girara_list_iterator(zathura->jumplist.list);
zathura_jumplist_reset_current(zathura);
zathura_jumplist_trim(zathura);
girara_debug("Loaded the jumplist from the database");
} else {
girara_debug("No jumplist for this file in the database yet");
}
return true;
}
static void
zathura_jumplist_save(zathura_t* zathura)
{
g_return_if_fail(zathura != NULL && zathura->document != NULL);
zathura_jump_t* cur = zathura_jumplist_current(zathura);
unsigned int pagenum = zathura_document_get_current_page_number(zathura->document);
if (cur != NULL) {
cur->page = pagenum;
cur->x = zathura_document_get_position_x(zathura->document);
cur->y = zathura_document_get_position_y(zathura->document);
}
}

View file

@ -55,9 +55,6 @@ typedef struct zathura_document_info_s {
static gboolean document_info_open(gpointer data);
static void zathura_jumplist_reset_current(zathura_t* zathura);
static void zathura_jumplist_append_jump(zathura_t* zathura);
static void zathura_jumplist_save(zathura_t* zathura);
#ifdef G_OS_UNIX
static gboolean zathura_signal_sigterm(gpointer data);
@ -1437,178 +1434,6 @@ error_ret:
return false;
}
bool
zathura_jumplist_has_previous(zathura_t* zathura)
{
return girara_list_iterator_has_previous(zathura->jumplist.cur);
}
bool
zathura_jumplist_has_next(zathura_t* zathura)
{
return girara_list_iterator_has_next(zathura->jumplist.cur);
}
zathura_jump_t*
zathura_jumplist_current(zathura_t* zathura)
{
if (zathura->jumplist.cur != NULL) {
return girara_list_iterator_data(zathura->jumplist.cur);
} else {
return NULL;
}
}
void
zathura_jumplist_forward(zathura_t* zathura)
{
if (girara_list_iterator_has_next(zathura->jumplist.cur)) {
girara_list_iterator_next(zathura->jumplist.cur);
}
}
void
zathura_jumplist_backward(zathura_t* zathura)
{
if (girara_list_iterator_has_previous(zathura->jumplist.cur)) {
girara_list_iterator_previous(zathura->jumplist.cur);
}
}
static void
zathura_jumplist_reset_current(zathura_t* zathura)
{
g_return_if_fail(zathura != NULL && zathura->jumplist.cur != NULL);
while (true) {
if (girara_list_iterator_has_next(zathura->jumplist.cur) == false) {
return;
}
girara_list_iterator_next(zathura->jumplist.cur);
}
}
static void
zathura_jumplist_append_jump(zathura_t* zathura)
{
g_return_if_fail(zathura != NULL && zathura->jumplist.list != NULL);
zathura_jump_t* jump = g_try_malloc0(sizeof(zathura_jump_t));
if (jump == NULL) {
return;
}
jump->page = 0;
jump->x = 0.0;
jump->y = 0.0;
girara_list_append(zathura->jumplist.list, jump);
if (zathura->jumplist.size == 0) {
zathura->jumplist.cur = girara_list_iterator(zathura->jumplist.list);
}
++zathura->jumplist.size;
zathura_jumplist_trim(zathura);
}
void
zathura_jumplist_trim(zathura_t* zathura)
{
g_return_if_fail(zathura != NULL && zathura->jumplist.list != NULL && zathura->jumplist.size != 0);
girara_list_iterator_t* cur = girara_list_iterator(zathura->jumplist.list);
while (zathura->jumplist.size > zathura->jumplist.max_size) {
if (girara_list_iterator_data(cur) == girara_list_iterator_data(zathura->jumplist.cur)) {
girara_list_iterator_free(zathura->jumplist.cur);
zathura->jumplist.cur = NULL;
}
girara_list_iterator_remove(cur);
--zathura->jumplist.size;
}
if (zathura->jumplist.size == 0 || (zathura->jumplist.size != 0 && zathura->jumplist.cur != NULL)) {
girara_list_iterator_free(cur);
} else {
zathura->jumplist.cur = cur;
}
}
void
zathura_jumplist_add(zathura_t* zathura)
{
g_return_if_fail(zathura != NULL && zathura->document != NULL && zathura->jumplist.list != NULL);
unsigned int pagenum = zathura_document_get_current_page_number(zathura->document);
double x = zathura_document_get_position_x(zathura->document);
double y = zathura_document_get_position_y(zathura->document);
if (zathura->jumplist.size != 0) {
zathura_jumplist_reset_current(zathura);
zathura_jump_t* cur = zathura_jumplist_current(zathura);
if (cur != NULL) {
if (cur->page == pagenum && cur->x == x && cur->y == y) {
return;
}
}
}
zathura_jumplist_append_jump(zathura);
zathura_jumplist_reset_current(zathura);
zathura_jumplist_save(zathura);
}
bool
zathura_jumplist_load(zathura_t* zathura, const char* file)
{
g_return_val_if_fail(zathura != NULL && file != NULL, false);
if (zathura->database == NULL) {
return false;
}
zathura->jumplist.list = zathura_db_load_jumplist(zathura->database, file);
if (zathura->jumplist.list == NULL) {
girara_error("Failed to load the jumplist from the database");
return false;
}
zathura->jumplist.size = girara_list_size(zathura->jumplist.list);
if (zathura->jumplist.size != 0) {
zathura->jumplist.cur = girara_list_iterator(zathura->jumplist.list);
zathura_jumplist_reset_current(zathura);
zathura_jumplist_trim(zathura);
girara_debug("Loaded the jumplist from the database");
} else {
girara_debug("No jumplist for this file in the database yet");
}
return true;
}
static void
zathura_jumplist_save(zathura_t* zathura)
{
g_return_if_fail(zathura != NULL && zathura->document != NULL);
zathura_jump_t* cur = zathura_jumplist_current(zathura);
unsigned int pagenum = zathura_document_get_current_page_number(zathura->document);
if (cur != NULL) {
cur->page = pagenum;
cur->x = zathura_document_get_position_x(zathura->document);
cur->y = zathura_document_get_position_y(zathura->document);
}
}
#ifdef G_OS_UNIX
static gboolean
zathura_signal_sigterm(gpointer data)