Commit 798c97f4 authored by Moritz Lipp's avatar Moritz Lipp
Browse files

Introduce zathura_fileinfo_t and update database interface

parent 3abf0968
...@@ -45,9 +45,9 @@ static bool plain_remove_bookmark(zathura_database_t* db, const char* file, ...@@ -45,9 +45,9 @@ static bool plain_remove_bookmark(zathura_database_t* db, const char* file,
static girara_list_t* plain_load_bookmarks(zathura_database_t* db, static girara_list_t* plain_load_bookmarks(zathura_database_t* db,
const char* file); const char* file);
static bool plain_set_fileinfo(zathura_database_t* db, const char* file, static bool plain_set_fileinfo(zathura_database_t* db, const char* file,
unsigned int page, unsigned int offset, double scale, unsigned int rotation); zathura_fileinfo_t* file_info);
static bool plain_get_fileinfo(zathura_database_t* db, const char* file, static bool plain_get_fileinfo(zathura_database_t* db, const char* file,
unsigned int* page, unsigned int* offset, double* scale, unsigned int* rotation); zathura_fileinfo_t* file_info);
static void plain_set_property(GObject* object, guint prop_id, static void plain_set_property(GObject* object, guint prop_id,
const GValue* value, GParamSpec* pspec); const GValue* value, GParamSpec* pspec);
...@@ -367,21 +367,21 @@ plain_load_bookmarks(zathura_database_t* db, const char* file) ...@@ -367,21 +367,21 @@ plain_load_bookmarks(zathura_database_t* db, const char* file)
} }
static bool static bool
plain_set_fileinfo(zathura_database_t* db, const char* file, unsigned int plain_set_fileinfo(zathura_database_t* db, const char* file, zathura_fileinfo_t*
page, unsigned int offset, double scale, unsigned int rotation) file_info)
{ {
zathura_plaindatabase_private_t* priv = ZATHURA_PLAINDATABASE_GET_PRIVATE(db); zathura_plaindatabase_private_t* priv = ZATHURA_PLAINDATABASE_GET_PRIVATE(db);
if (priv->history == NULL) { if (priv->history == NULL || file_info == NULL || file == NULL) {
return false; return false;
} }
char* tmp = g_strdup_printf("%f", scale); char* tmp = g_strdup_printf("%f", file_info->scale);
char* name = prepare_filename(file); char* name = prepare_filename(file);
g_key_file_set_integer(priv->history, name, KEY_PAGE, page); g_key_file_set_integer(priv->history, name, KEY_PAGE, file_info->current_page);
g_key_file_set_integer(priv->history, name, KEY_OFFSET, offset); g_key_file_set_integer(priv->history, name, KEY_OFFSET, file_info->page_offset);
g_key_file_set_string (priv->history, name, KEY_SCALE, tmp); g_key_file_set_string (priv->history, name, KEY_SCALE, tmp);
g_key_file_set_integer(priv->history, name, KEY_ROTATE, rotation); g_key_file_set_integer(priv->history, name, KEY_ROTATE, file_info->rotation);
g_free(name); g_free(name);
g_free(tmp); g_free(tmp);
...@@ -392,9 +392,13 @@ plain_set_fileinfo(zathura_database_t* db, const char* file, unsigned int ...@@ -392,9 +392,13 @@ plain_set_fileinfo(zathura_database_t* db, const char* file, unsigned int
} }
static bool static bool
plain_get_fileinfo(zathura_database_t* db, const char* file, unsigned int* plain_get_fileinfo(zathura_database_t* db, const char* file, zathura_fileinfo_t*
page, unsigned int* offset, double* scale, unsigned int* rotation) file_info)
{ {
if (db == NULL || file == NULL || file_info == NULL) {
return false;
}
zathura_plaindatabase_private_t* priv = ZATHURA_PLAINDATABASE_GET_PRIVATE(db); zathura_plaindatabase_private_t* priv = ZATHURA_PLAINDATABASE_GET_PRIVATE(db);
if (priv->history == NULL) { if (priv->history == NULL) {
return false; return false;
...@@ -405,12 +409,12 @@ plain_get_fileinfo(zathura_database_t* db, const char* file, unsigned int* ...@@ -405,12 +409,12 @@ plain_get_fileinfo(zathura_database_t* db, const char* file, unsigned int*
return false; return false;
} }
*page = g_key_file_get_integer(priv->history, name, KEY_PAGE, NULL); file_info->current_page = g_key_file_get_integer(priv->history, name, KEY_PAGE, NULL);
*offset = g_key_file_get_integer(priv->history, name, KEY_OFFSET, NULL); file_info->page_offset = g_key_file_get_integer(priv->history, name, KEY_OFFSET, NULL);
*rotation = g_key_file_get_integer(priv->history, name, KEY_ROTATE, NULL); file_info->rotation = g_key_file_get_integer(priv->history, name, KEY_ROTATE, NULL);
char* scale_string = g_key_file_get_string(priv->history, name, KEY_SCALE, NULL); char* scale_string = g_key_file_get_string(priv->history, name, KEY_SCALE, NULL);
*scale = strtod(scale_string, NULL); file_info->scale = strtod(scale_string, NULL);
g_free(scale_string); g_free(scale_string);
g_free(name); g_free(name);
......
...@@ -20,9 +20,9 @@ static bool sqlite_remove_bookmark(zathura_database_t* db, const char* file, ...@@ -20,9 +20,9 @@ static bool sqlite_remove_bookmark(zathura_database_t* db, const char* file,
static girara_list_t* sqlite_load_bookmarks(zathura_database_t* db, static girara_list_t* sqlite_load_bookmarks(zathura_database_t* db,
const char* file); const char* file);
static bool sqlite_set_fileinfo(zathura_database_t* db, const char* file, static bool sqlite_set_fileinfo(zathura_database_t* db, const char* file,
unsigned int page, unsigned int offset, double scale, unsigned int rotation); zathura_fileinfo_t* file_info);
static bool sqlite_get_fileinfo(zathura_database_t* db, const char* file, static bool sqlite_get_fileinfo(zathura_database_t* db, const char* file,
unsigned int* page, unsigned int* offset, double* scale, unsigned int* rotation); zathura_fileinfo_t* file_info);
static void sqlite_set_property(GObject* object, guint prop_id, static void sqlite_set_property(GObject* object, guint prop_id,
const GValue* value, GParamSpec* pspec); const GValue* value, GParamSpec* pspec);
...@@ -272,9 +272,13 @@ sqlite_load_bookmarks(zathura_database_t* db, const char* file) ...@@ -272,9 +272,13 @@ sqlite_load_bookmarks(zathura_database_t* db, const char* file)
} }
static bool static bool
sqlite_set_fileinfo(zathura_database_t* db, const char* file, unsigned int sqlite_set_fileinfo(zathura_database_t* db, const char* file,
page, unsigned int offset, double scale, unsigned int rotation) zathura_fileinfo_t* file_info)
{ {
if (db == NULL || file == NULL || file_info == NULL) {
return false;
}
zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db); zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db);
static const char SQL_FILEINFO_SET[] = static const char SQL_FILEINFO_SET[] =
...@@ -285,11 +289,11 @@ sqlite_set_fileinfo(zathura_database_t* db, const char* file, unsigned int ...@@ -285,11 +289,11 @@ sqlite_set_fileinfo(zathura_database_t* db, const char* file, unsigned int
return false; return false;
} }
if (sqlite3_bind_text(stmt, 1, file, -1, NULL) != SQLITE_OK || if (sqlite3_bind_text(stmt, 1, file, -1, NULL) != SQLITE_OK ||
sqlite3_bind_int(stmt, 2, page) != SQLITE_OK || sqlite3_bind_int(stmt, 2, file_info->current_page) != SQLITE_OK ||
sqlite3_bind_int(stmt, 3, offset) != SQLITE_OK || sqlite3_bind_int(stmt, 3, file_info->page_offset) != SQLITE_OK ||
sqlite3_bind_double(stmt, 4, scale) != SQLITE_OK || sqlite3_bind_double(stmt, 4, file_info->scale) != SQLITE_OK ||
sqlite3_bind_int(stmt, 5, rotation) != SQLITE_OK) { sqlite3_bind_int(stmt, 5, file_info->rotation) != SQLITE_OK) {
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
girara_error("Failed to bind arguments."); girara_error("Failed to bind arguments.");
return false; return false;
...@@ -302,9 +306,13 @@ sqlite_set_fileinfo(zathura_database_t* db, const char* file, unsigned int ...@@ -302,9 +306,13 @@ sqlite_set_fileinfo(zathura_database_t* db, const char* file, unsigned int
} }
static bool static bool
sqlite_get_fileinfo(zathura_database_t* db, const char* file, unsigned int* sqlite_get_fileinfo(zathura_database_t* db, const char* file,
page, unsigned int* offset, double* scale, unsigned int* rotation) zathura_fileinfo_t* file_info)
{ {
if (db == NULL || file == NULL || file_info == NULL) {
return false;
}
zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db); zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db);
static const char SQL_FILEINFO_GET[] = static const char SQL_FILEINFO_GET[] =
...@@ -312,7 +320,7 @@ sqlite_get_fileinfo(zathura_database_t* db, const char* file, unsigned int* ...@@ -312,7 +320,7 @@ sqlite_get_fileinfo(zathura_database_t* db, const char* file, unsigned int*
sqlite3_stmt* stmt = prepare_statement(priv->session, SQL_FILEINFO_GET); sqlite3_stmt* stmt = prepare_statement(priv->session, SQL_FILEINFO_GET);
if (stmt == NULL) { if (stmt == NULL) {
return false; return false;
} }
if (sqlite3_bind_text(stmt, 1, file, -1, NULL) != SQLITE_OK) { if (sqlite3_bind_text(stmt, 1, file, -1, NULL) != SQLITE_OK) {
...@@ -327,10 +335,11 @@ sqlite_get_fileinfo(zathura_database_t* db, const char* file, unsigned int* ...@@ -327,10 +335,11 @@ sqlite_get_fileinfo(zathura_database_t* db, const char* file, unsigned int*
return false; return false;
} }
*page = sqlite3_column_int(stmt, 0); file_info->current_page = sqlite3_column_int(stmt, 0);
*offset = sqlite3_column_int(stmt, 1); file_info->page_offset = sqlite3_column_int(stmt, 1);
*scale = sqlite3_column_double(stmt, 2); file_info->scale = sqlite3_column_double(stmt, 2);
*rotation = sqlite3_column_int(stmt, 3); file_info->rotation = sqlite3_column_int(stmt, 3);
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
return true; return true;
......
...@@ -46,22 +46,19 @@ zathura_db_load_bookmarks(zathura_database_t* db, const char* file) ...@@ -46,22 +46,19 @@ zathura_db_load_bookmarks(zathura_database_t* db, const char* file)
} }
bool bool
zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned int zathura_db_set_fileinfo(zathura_database_t* db, const char* file,
page, unsigned int offset, double scale, unsigned int rotation) zathura_fileinfo_t* file_info)
{ {
g_return_val_if_fail(ZATHURA_IS_DATABASE(db) && file != NULL, false); g_return_val_if_fail(ZATHURA_IS_DATABASE(db) && file != NULL && file_info != NULL, false);
return ZATHURA_DATABASE_GET_INTERFACE(db)->set_fileinfo(db, file, page, offset, return ZATHURA_DATABASE_GET_INTERFACE(db)->set_fileinfo(db, file, file_info);
scale, rotation);
} }
bool bool
zathura_db_get_fileinfo(zathura_database_t* db, const char* file, unsigned int* zathura_db_get_fileinfo(zathura_database_t* db, const char* file,
page, unsigned int* offset, double* scale, unsigned int* rotation) zathura_fileinfo_t* file_info)
{ {
g_return_val_if_fail(ZATHURA_IS_DATABASE(db) && file != NULL && page != NULL && g_return_val_if_fail(ZATHURA_IS_DATABASE(db) && file != NULL && file_info != NULL, false);
offset != NULL && scale != NULL && rotation != NULL, false);
return ZATHURA_DATABASE_GET_INTERFACE(db)->get_fileinfo(db, file, page, offset, return ZATHURA_DATABASE_GET_INTERFACE(db)->get_fileinfo(db, file, file_info);
scale, rotation);
} }
...@@ -9,6 +9,13 @@ ...@@ -9,6 +9,13 @@
#include "bookmarks.h" #include "bookmarks.h"
typedef struct zathura_fileinfo_s {
unsigned int current_page;
unsigned int page_offset;
double scale;
unsigned int rotation;
} zathura_fileinfo_t;
#define ZATHURA_TYPE_DATABASE \ #define ZATHURA_TYPE_DATABASE \
(zathura_database_get_type ()) (zathura_database_get_type ())
#define ZATHURA_DATABASE(obj) \ #define ZATHURA_DATABASE(obj) \
...@@ -29,13 +36,12 @@ struct _ZathuraDatabaseInterface ...@@ -29,13 +36,12 @@ struct _ZathuraDatabaseInterface
bool (*add_bookmark)(ZathuraDatabase* db, const char* file, zathura_bookmark_t* bookmark); bool (*add_bookmark)(ZathuraDatabase* db, const char* file, zathura_bookmark_t* bookmark);
bool (*remove_bookmark)(ZathuraDatabase* db, const char* file, const char* id); bool (*remove_bookmark)(ZathuraDatabase* db, const char* file, const char* id);
girara_list_t* (*load_bookmarks)(ZathuraDatabase* db, const char* file); girara_list_t* (*load_bookmarks)(ZathuraDatabase* db, const char* file);
bool (*set_fileinfo)(ZathuraDatabase* db, const char* file, unsigned bool (*set_fileinfo)(ZathuraDatabase* db, const char* file, zathura_fileinfo_t* file_info);
int page, unsigned int offset, double scale, unsigned int rotation);
bool (*get_fileinfo)(ZathuraDatabase* db, const char* file, unsigned bool (*get_fileinfo)(ZathuraDatabase* db, const char* file, zathura_fileinfo_t* file_info);
int* page, unsigned int* offset, double* scale, unsigned int* rotation);
}; };
GType zathura_database_get_type(void); GType zathura_database_get_type(void);
...@@ -84,26 +90,20 @@ girara_list_t* zathura_db_load_bookmarks(zathura_database_t* db, const char* ...@@ -84,26 +90,20 @@ girara_list_t* zathura_db_load_bookmarks(zathura_database_t* db, const char*
* *
* @param db The database instance * @param db The database instance
* @param file The file to which the file info belongs to. * @param file The file to which the file info belongs to.
* @param page The last page. * @param file_info The file info
* @param offset The last offset.
* @param scale The last scale.
* @param rotation The last rotation.
* @return true on success, false otherwise. * @return true on success, false otherwise.
*/ */
bool zathura_db_set_fileinfo(zathura_database_t* db, const char* file, unsigned bool zathura_db_set_fileinfo(zathura_database_t* db, const char* file,
int page, unsigned int offset, double scale, unsigned int rotation); zathura_fileinfo_t* file_info);
/* Get file info (last site, ...) from the database. /* Get file info (last site, ...) from the database.
* *
* @param db The database instance * @param db The database instance
* @param file The file to which the file info belongs to. * @param file The file to which the file info belongs to.
* @param page The last page. * @param file_info The file info
* @param offset The last offset.
* @param scale The last scale.
* @param rotation The rotation.
* @return true on success, false otherwise. * @return true on success, false otherwise.
*/ */
bool zathura_db_get_fileinfo(zathura_database_t* db, const char* file, unsigned bool zathura_db_get_fileinfo(zathura_database_t* db, const char* file,
int* page, unsigned int* offset, double* scale, unsigned int* rotation); zathura_fileinfo_t* file_info);
#endif // DATABASE_H #endif // DATABASE_H
...@@ -462,41 +462,38 @@ document_open(zathura_t* zathura, const char* path, const char* password) ...@@ -462,41 +462,38 @@ document_open(zathura_t* zathura, const char* path, const char* password)
goto error_out; goto error_out;
} }
const char* file_path = zathura_document_get_path(document); const char* file_path = zathura_document_get_path(document);
unsigned int number_of_pages = zathura_document_get_number_of_pages(document); unsigned int number_of_pages = zathura_document_get_number_of_pages(document);
unsigned int current_page_number = 0;
double scale = 1;
unsigned int page_offset = 0;
unsigned int rotate = 0;
/* read history file */ /* read history file */
zathura_db_get_fileinfo(zathura->database, file_path, &current_page_number, zathura_fileinfo_t file_info = { 0, 0, 1, 0 };
&page_offset, &scale, &rotate); zathura_db_get_fileinfo(zathura->database, file_path, &file_info);
zathura_document_set_page_offset(document, page_offset); /* set page offset */
zathura_document_set_page_offset(document, file_info.page_offset);
/* check for valid scale value */ /* check for valid scale value */
if (scale <= FLT_EPSILON) { if (file_info.scale <= FLT_EPSILON) {
girara_warning("document info: '%s' has non positive scale", file_path); girara_warning("document info: '%s' has non positive scale", file_path);
zathura_document_set_scale(document, 1); zathura_document_set_scale(document, 1);
} else { } else {
zathura_document_set_scale(document, scale); zathura_document_set_scale(document, file_info.scale);
} }
/* check current page number */ /* check current page number */
if (current_page_number > number_of_pages) { if (file_info.current_page > number_of_pages) {
girara_warning("document info: '%s' has an invalid page number", file_path); girara_warning("document info: '%s' has an invalid page number", file_path);
zathura_document_set_current_page_number(document, 0); zathura_document_set_current_page_number(document, 0);
} else { } else {
zathura_document_set_current_page_number(document, current_page_number); zathura_document_set_current_page_number(document, file_info.current_page);
} }
/* check for valid rotation */ /* check for valid rotation */
if (rotate % 90 != 0) { if (file_info.rotation % 90 != 0) {
girara_warning("document info: '%s' has an invalid rotation", file_path); girara_warning("document info: '%s' has an invalid rotation", file_path);
zathura_document_set_rotation(document, 0); zathura_document_set_rotation(document, 0);
} else { } else {
zathura_document_set_rotation(document, rotate % 360); zathura_document_set_rotation(document, file_info.rotation % 360);
} }
/* jump to first page if setting enabled */ /* jump to first page if setting enabled */
...@@ -698,15 +695,16 @@ document_close(zathura_t* zathura, bool keep_monitor) ...@@ -698,15 +695,16 @@ document_close(zathura_t* zathura, bool keep_monitor)
} }
} }
const char* path = zathura_document_get_path(zathura->document); /* store file information */
unsigned int current_page_number = zathura_document_get_current_page_number(zathura->document); const char* path = zathura_document_get_path(zathura->document);
unsigned int page_offset = zathura_document_get_page_offset(zathura->document);
double scale = zathura_document_get_scale(zathura->document); zathura_fileinfo_t file_info = { 0 };
unsigned int rotation = zathura_document_get_rotation(zathura->document); file_info.current_page = zathura_document_get_current_page_number(zathura->document);
file_info.page_offset = zathura_document_get_page_offset(zathura->document);
file_info.scale = zathura_document_get_scale(zathura->document);
file_info.rotation = zathura_document_get_rotation(zathura->document);
/* store last seen page */ zathura_db_set_fileinfo(zathura->database, path, &file_info);
zathura_db_set_fileinfo(zathura->database, path, current_page_number,
page_offset, scale, rotation);
/* release render thread */ /* release render thread */
render_free(zathura->sync.render_thread); render_free(zathura->sync.render_thread);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment