Commit 334b4fbb authored by Moritz Lipp's avatar Moritz Lipp
Browse files

Introduce and use zathura_document wrapper functions

parent c82c2aaf
......@@ -34,7 +34,8 @@ zathura_bookmark_add(zathura_t* zathura, const gchar* id, unsigned int page)
girara_list_append(zathura->bookmarks.bookmarks, bookmark);
if (zathura->database != NULL) {
if (zathura_db_add_bookmark(zathura->database, zathura->document->file_path, bookmark) == false) {
const char* path = zathura_document_get_path(zathura->document);
if (zathura_db_add_bookmark(zathura->database, path, bookmark) == false) {
girara_warning("Failed to add bookmark to database.");
}
}
......@@ -54,7 +55,8 @@ zathura_bookmark_remove(zathura_t* zathura, const gchar* id)
}
if (zathura->database != NULL) {
if (zathura_db_remove_bookmark(zathura->database, zathura->document->file_path, bookmark->id) == false) {
const char* path = zathura_document_get_path(zathura->document);
if (zathura_db_remove_bookmark(zathura->database, path, bookmark->id) == false) {
girara_warning("Failed to remove bookmark from database.");
}
}
......
......@@ -51,8 +51,7 @@ void
cb_view_vadjustment_value_changed(GtkAdjustment* GIRARA_UNUSED(adjustment), gpointer data)
{
zathura_t* zathura = data;
if (zathura == NULL || zathura->document == NULL || zathura->document->pages == NULL
|| zathura->ui.page_widget == NULL) {
if (zathura == NULL || zathura->document == NULL || zathura->ui.page_widget == NULL) {
return;
}
......@@ -74,23 +73,26 @@ cb_view_vadjustment_value_changed(GtkAdjustment* GIRARA_UNUSED(adjustment), gpoi
center.y = (view_rect.height + 1) / 2;
center.height = center.width = (2 * page_padding) + 1;
unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document);
double scale = zathura_document_get_scale(zathura->document);
bool updated = false;
/* find page that fits */
for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; page_id++) {
zathura_page_t* page = zathura->document->pages[page_id];
for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) {
zathura_page_t* page = zathura_document_get_page(zathura->document, page_id);
GdkRectangle page_rect;
GtkWidget* page_widget = zathura_page_get_widget(page);
gtk_widget_translate_coordinates(page_widget,
zathura->ui.session->gtk.view, 0, 0, &page_rect.x, &page_rect.y);
page_rect.width = zathura_page_get_width(page) * zathura->document->scale;
page_rect.height = zathura_page_get_height(page) * zathura->document->scale;
page_rect.width = zathura_page_get_width(page) * scale;
page_rect.height = zathura_page_get_height(page) * scale;
if (gdk_rectangle_intersect(&view_rect, &page_rect, NULL) == TRUE) {
zathura_page_set_visibility(page, true);
if (zathura->global.update_page_number == true && updated == false
&& gdk_rectangle_intersect(&center, &page_rect, NULL) == TRUE) {
zathura->document->current_page_number = page_id;
zathura_document_set_current_page_number(zathura->document, page_id);
updated = true;
}
} else {
......@@ -119,7 +121,7 @@ cb_pages_per_row_value_changed(girara_session_t* session, const char* UNUSED(nam
page_widget_set_mode(zathura, pages_per_row);
if (zathura->document != NULL) {
unsigned int current_page = zathura->document->current_page_number;
unsigned int current_page = zathura_document_get_current_page_number(zathura->document);
page_set_delayed(zathura, current_page);
}
}
......@@ -185,8 +187,9 @@ cb_sc_follow(GtkEntry* entry, girara_session_t* session)
/* set pages to draw links */
bool invalid_index = true;
for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; page_id++) {
zathura_page_t* page = zathura->document->pages[page_id];
unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document);
for (unsigned int page_id = 0; page_id < number_of_pages; page_id++) {
zathura_page_t* page = zathura_document_get_page(zathura->document, page_id);
if (page == NULL || zathura_page_get_visibility(page) == false) {
continue;
}
......@@ -322,7 +325,7 @@ cb_view_resized(GtkWidget* UNUSED(widget), GtkAllocation* allocation, zathura_t*
/* adjust only if the allocation changed */
if (width != allocation->width || height != allocation->height) {
girara_argument_t argument = { zathura->document->adjust_mode, NULL };
girara_argument_t argument = { zathura_document_get_adjust_mode(zathura->document), NULL };
sc_adjust_window(zathura->ui.session, &argument, NULL, 0);
width = allocation->width;
......
......@@ -40,12 +40,12 @@ cmd_bookmark_create(girara_session_t* session, girara_list_t* argument_list)
const char* bookmark_name = girara_list_nth(argument_list, 0);
zathura_bookmark_t* bookmark = zathura_bookmark_get(zathura, bookmark_name);
if (bookmark != NULL) {
bookmark->page = zathura->document->current_page_number + 1;
bookmark->page = zathura_document_get_current_page_number(zathura->document) + 1;
girara_notify(session, GIRARA_INFO, _("Bookmark successfuly updated: %s"), bookmark_name);
return true;
}
bookmark = zathura_bookmark_add(zathura, bookmark_name, zathura->document->current_page_number + 1);
bookmark = zathura_bookmark_add(zathura, bookmark_name, zathura_document_get_current_page_number(zathura->document) + 1);
if (bookmark == NULL) {
girara_notify(session, GIRARA_ERROR, _("Could not create bookmark: %s"), bookmark_name);
return false;
......@@ -304,8 +304,13 @@ cmd_search(girara_session_t* session, const char* input, girara_argument_t* argu
bool firsthit = true;
zathura_error_t error = ZATHURA_ERROR_OK;
for (unsigned int page_id = 0; page_id < zathura->document->number_of_pages; ++page_id) {
zathura_page_t* page = zathura->document->pages[(page_id + zathura->document->current_page_number) % zathura->document->number_of_pages];
unsigned int number_of_pages = zathura_document_get_number_of_pages(zathura->document);
unsigned int current_page_number = zathura_document_get_current_page_number(zathura->document);
for (unsigned int page_id = 0; page_id < number_of_pages; ++page_id) {
unsigned int index = (page_id + current_page_number) % number_of_pages;
zathura_page_t* page = zathura_document_get_page(zathura->document, index);
if (page == NULL) {
continue;
}
......@@ -385,7 +390,7 @@ cmd_offset(girara_session_t* session, girara_list_t* argument_list)
}
/* no argument: take current page as offset */
unsigned int page_offset = zathura->document->current_page_number;
unsigned int page_offset = zathura_document_get_current_page_number(zathura->document);
/* retrieve offset from argument */
if (girara_list_size(argument_list) == 1) {
......@@ -399,8 +404,8 @@ cmd_offset(girara_session_t* session, girara_list_t* argument_list)
}
}
if (page_offset < zathura->document->number_of_pages) {
zathura->document->page_offset = page_offset;
if (page_offset < zathura_document_get_number_of_pages(zathura->document)) {
zathura_document_set_page_offset(zathura->document, page_offset);
}
return true;
......
......@@ -126,8 +126,8 @@ config_load_default(zathura_t* zathura)
girara_shortcut_add(gsession, GDK_CONTROL_MASK, GDK_KEY_c, NULL, sc_abort, 0, 0, NULL);
girara_shortcut_add(gsession, 0, GDK_KEY_Escape, NULL, sc_abort, 0, 0, NULL);
girara_shortcut_add(gsession, 0, GDK_KEY_a, NULL, sc_adjust_window, NORMAL, ADJUST_BESTFIT, NULL);
girara_shortcut_add(gsession, 0, GDK_KEY_s, NULL, sc_adjust_window, NORMAL, ADJUST_WIDTH, NULL);
girara_shortcut_add(gsession, 0, GDK_KEY_a, NULL, sc_adjust_window, NORMAL, ZATHURA_ADJUST_BESTFIT, NULL);
girara_shortcut_add(gsession, 0, GDK_KEY_s, NULL, sc_adjust_window, NORMAL, ZATHURA_ADJUST_WIDTH, NULL);
girara_shortcut_add(gsession, 0, GDK_KEY_m, NULL, sc_change_mode, NORMAL, ADD_MARKER, NULL);
girara_shortcut_add(gsession, 0, GDK_KEY_apostrophe, NULL, sc_change_mode, NORMAL, EVAL_MARKER, NULL);
......@@ -302,8 +302,8 @@ config_load_default(zathura_t* zathura)
girara_argument_mapping_add(gsession, "specific", ZOOM_SPECIFIC);
girara_argument_mapping_add(gsession, "top", TOP);
girara_argument_mapping_add(gsession, "up", UP);
girara_argument_mapping_add(gsession, "best-fit", ADJUST_BESTFIT);
girara_argument_mapping_add(gsession, "width", ADJUST_WIDTH);
girara_argument_mapping_add(gsession, "best-fit", ZATHURA_ADJUST_BESTFIT);
girara_argument_mapping_add(gsession, "width", ZATHURA_ADJUST_WIDTH);
girara_argument_mapping_add(gsession, "rotate-cw", ROTATE_CW);
girara_argument_mapping_add(gsession, "rotate-ccw", ROTATE_CCW);
}
......
......@@ -32,6 +32,34 @@ static const size_t GT_MAX_READ = 1 << 16;
static const gchar* guess_type(const char* path);
/**
* Document
*/
struct zathura_document_s
{
char* file_path; /**< File path of the document */
const char* password; /**< Password of the document */
unsigned int current_page_number; /**< Current page number */
unsigned int number_of_pages; /**< Number of pages */
double scale; /**< Scale value */
unsigned int rotate; /**< Rotation */
void* data; /**< Custom data */
zathura_adjust_mode_t adjust_mode; /**< Adjust mode (best-fit, width) */
zathura_t* zathura; /**< Zathura instance */
unsigned int page_offset; /**< Page offset */
/**
* Document pages
*/
zathura_page_t** pages;
/**
* Used plugin
*/
zathura_plugin_t* plugin;
};
zathura_document_t*
zathura_document_open(zathura_t* zathura, const char* path, const char* password)
{
......@@ -94,8 +122,8 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password
document->file_path = real_path;
document->password = password;
document->scale = 1.0;
document->zathura = zathura;
document->plugin = plugin;
document->zathura = zathura;
/* open document */
if (plugin->functions.document_open == NULL) {
......@@ -170,14 +198,14 @@ zathura_document_open(zathura_t* zathura, const char* path, const char* password
/* apply open adjustment */
char* adjust_open = "best-fit";
document->adjust_mode = ADJUST_BESTFIT;
document->adjust_mode = ZATHURA_ADJUST_BESTFIT;
if (girara_setting_get(zathura->ui.session, "adjust-open", &(adjust_open)) == true) {
if (g_strcmp0(adjust_open, "best-fit") == 0) {
document->adjust_mode = ADJUST_BESTFIT;
document->adjust_mode = ZATHURA_ADJUST_BESTFIT;
} else if (g_strcmp0(adjust_open, "width") == 0) {
document->adjust_mode = ADJUST_WIDTH;
document->adjust_mode = ZATHURA_ADJUST_WIDTH;
} else {
document->adjust_mode = ADJUST_NONE;
document->adjust_mode = ZATHURA_ADJUST_NONE;
}
g_free(adjust_open);
}
......@@ -203,8 +231,7 @@ error_free:
zathura_error_t
zathura_document_free(zathura_document_t* document)
{
if (document == NULL || document->plugin == NULL || document->zathura == NULL
|| document->zathura->ui.session == NULL) {
if (document == NULL || document->plugin == NULL) {
return ZATHURA_ERROR_INVALID_ARGUMENTS;
}
......@@ -219,8 +246,6 @@ zathura_document_free(zathura_document_t* document)
/* free document */
zathura_error_t error = ZATHURA_ERROR_OK;
if (document->plugin->functions.document_free == NULL) {
girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
girara_error("%s not implemented", __FUNCTION__);
error = ZATHURA_ERROR_NOT_IMPLEMENTED;
} else {
error = document->plugin->functions.document_free(document, document->data);
......@@ -255,6 +280,16 @@ zathura_document_get_password(zathura_document_t* document)
return document->password;
}
zathura_page_t*
zathura_document_get_page(zathura_document_t* document, unsigned int index)
{
if (document == NULL || document->pages == NULL || (document->number_of_pages <= index)) {
return NULL;
}
return document->pages[index];
}
void*
zathura_document_get_data(zathura_document_t* document)
{
......@@ -296,7 +331,7 @@ zathura_document_set_number_of_pages(zathura_document_t* document, unsigned int
}
unsigned int
zathura_document_get_current_page(zathura_document_t* document)
zathura_document_get_current_page_number(zathura_document_t* document)
{
if (document == NULL) {
return 0;
......@@ -306,7 +341,7 @@ zathura_document_get_current_page(zathura_document_t* document)
}
void
zathura_document_set_current_page(zathura_document_t* document, unsigned int
zathura_document_set_current_page_number(zathura_document_t* document, unsigned int
current_page)
{
if (document == NULL) {
......@@ -316,17 +351,110 @@ zathura_document_set_current_page(zathura_document_t* document, unsigned int
document->current_page_number = current_page;
}
double
zathura_document_get_scale(zathura_document_t* document)
{
if (document == NULL) {
return 0;
}
return document->scale;
}
void
zathura_document_set_scale(zathura_document_t* document, double scale)
{
if (document == NULL) {
return;
}
document->scale = scale;
}
unsigned int
zathura_document_get_rotation(zathura_document_t* document)
{
if (document == NULL) {
return 0;
}
return document->rotate;
}
void
zathura_document_set_rotation(zathura_document_t* document, unsigned int rotation)
{
if (document == NULL) {
return;
}
document->rotate = rotation % 360;
if (document->rotate > 0 && document->rotate < 90) {
document->rotate = 90;
} else if (document->rotate > 0 && document->rotate < 180) {
document->rotate = 180;
} else if (document->rotate > 0 && document->rotate < 270) {
document->rotate = 270;
} else {
document->rotate = 0;
}
}
zathura_adjust_mode_t
zathura_document_get_adjust_mode(zathura_document_t* document)
{
if (document == NULL) {
return ZATHURA_ADJUST_NONE;
}
return document->adjust_mode;
}
void
zathura_document_set_adjust_mode(zathura_document_t* document, zathura_adjust_mode_t mode)
{
if (document == NULL) {
return;
}
if (mode == ZATHURA_ADJUST_BESTFIT || mode == ZATHURA_ADJUST_WIDTH) {
document->adjust_mode = mode;
} else {
document->adjust_mode = ZATHURA_ADJUST_NONE;
}
}
unsigned int
zathura_document_get_page_offset(zathura_document_t* document)
{
if (document == NULL) {
return 0;
}
return document->page_offset;
}
void
zathura_document_set_page_offset(zathura_document_t* document, unsigned int page_offset)
{
if (document == NULL) {
return;
}
if (page_offset < document->number_of_pages) {
document->page_offset = page_offset;
}
}
zathura_error_t
zathura_document_save_as(zathura_document_t* document, const char* path)
{
if (document == NULL || document->plugin == NULL || path == NULL ||
document->zathura == NULL || document->zathura->ui.session == NULL) {
if (document == NULL || document->plugin == NULL || path == NULL) {
return ZATHURA_ERROR_UNKNOWN;
}
if (document->plugin->functions.document_save_as == NULL) {
girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
girara_error("%s not implemented", __FUNCTION__);
return ZATHURA_ERROR_NOT_IMPLEMENTED;
}
......@@ -336,8 +464,7 @@ zathura_document_save_as(zathura_document_t* document, const char* path)
girara_tree_node_t*
zathura_document_index_generate(zathura_document_t* document, zathura_error_t* error)
{
if (document == NULL || document->plugin == NULL || document->zathura == NULL
|| document->zathura->ui.session == NULL) {
if (document == NULL || document->plugin == NULL) {
if (error != NULL) {
*error = ZATHURA_ERROR_INVALID_ARGUMENTS;
}
......@@ -345,8 +472,6 @@ zathura_document_index_generate(zathura_document_t* document, zathura_error_t* e
}
if (document->plugin->functions.document_index_generate == NULL) {
girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
girara_error("%s not implemented", __FUNCTION__);
if (error != NULL) {
*error = ZATHURA_ERROR_NOT_IMPLEMENTED;
}
......@@ -359,8 +484,7 @@ zathura_document_index_generate(zathura_document_t* document, zathura_error_t* e
girara_list_t*
zathura_document_attachments_get(zathura_document_t* document, zathura_error_t* error)
{
if (document == NULL || document->plugin == NULL || document->zathura == NULL
|| document->zathura->ui.session == NULL) {
if (document == NULL || document->plugin == NULL) {
if (error != NULL) {
*error = ZATHURA_ERROR_INVALID_ARGUMENTS;
}
......@@ -368,8 +492,6 @@ zathura_document_attachments_get(zathura_document_t* document, zathura_error_t*
}
if (document->plugin->functions.document_attachments_get == NULL) {
girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
girara_error("%s not implemented", __FUNCTION__);
if (error != NULL) {
*error = ZATHURA_ERROR_NOT_IMPLEMENTED;
}
......@@ -382,14 +504,11 @@ zathura_document_attachments_get(zathura_document_t* document, zathura_error_t*
zathura_error_t
zathura_document_attachment_save(zathura_document_t* document, const char* attachment, const char* file)
{
if (document == NULL || document->plugin == NULL || document->zathura == NULL
|| document->zathura->ui.session == NULL) {
if (document == NULL || document->plugin == NULL) {
return ZATHURA_ERROR_INVALID_ARGUMENTS;
}
if (document->plugin->functions.document_attachment_save == NULL) {
girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
girara_error("%s not implemented", __FUNCTION__);
return ZATHURA_ERROR_NOT_IMPLEMENTED;
}
......@@ -399,7 +518,7 @@ zathura_document_attachment_save(zathura_document_t* document, const char* attac
char*
zathura_document_meta_get(zathura_document_t* document, zathura_document_meta_t meta, zathura_error_t* error)
{
if (document == NULL || document->plugin == NULL || document->zathura == NULL || document->zathura->ui.session == NULL) {
if (document == NULL || document->plugin == NULL) {
if (error != NULL) {
*error = ZATHURA_ERROR_INVALID_ARGUMENTS;
}
......@@ -410,8 +529,6 @@ zathura_document_meta_get(zathura_document_t* document, zathura_document_meta_t
if (error != NULL) {
*error = ZATHURA_ERROR_NOT_IMPLEMENTED;
}
girara_notify(document->zathura->ui.session, GIRARA_WARNING, _("%s not implemented"), __FUNCTION__);
girara_error("%s not implemented", __FUNCTION__);
return NULL;
}
......@@ -493,3 +610,23 @@ guess_type(const char* path)
g_strdelimit(out, "\n\r", '\0');
return out;
}
zathura_t*
zathura_document_get_zathura(zathura_document_t* document)
{
if (document == NULL) {
return NULL;
}
return document->zathura;
}
zathura_plugin_t*
zathura_document_get_plugin(zathura_document_t* document)
{
if (document == NULL) {
return NULL;
}
return document->plugin;
}
......@@ -26,33 +26,6 @@ typedef enum zathura_document_meta_e
ZATHURA_DOCUMENT_MODIFICATION_DATE /**< Modification data */
} zathura_document_meta_t;
/**
* Document
*/
struct zathura_document_s
{
char* file_path; /**< File path of the document */
const char* password; /**< Password of the document */
unsigned int current_page_number; /**< Current page number */
unsigned int number_of_pages; /**< Number of pages */
double scale; /**< Scale value */
unsigned int rotate; /**< Rotation */
void* data; /**< Custom data */
zathura_t* zathura; /** Zathura object */
int adjust_mode; /**< Adjust mode (best-fit, width) */
unsigned int page_offset; /**< Page offset */
/**
* Document pages
*/
zathura_page_t** pages;
/**
* Used plugin
*/
zathura_plugin_t* plugin;
};
/**
* Open the document
*
......@@ -64,6 +37,15 @@ struct zathura_document_s
zathura_document_t* zathura_document_open(zathura_t* zathura, const char* path,
const char* password);
/**
* Free the document
*
* @param document
* @return ZATHURA_ERROR_OK when no error occured, otherwise see
* zathura_error_t
*/
zathura_error_t zathura_document_free(zathura_document_t* document);
/**
* Returns the path of the document
*
......@@ -81,21 +63,13 @@ const char* zathura_document_get_path(zathura_document_t* document);
const char* zathura_document_get_password(zathura_document_t* document);
/**
* Free the document
*
* @param document
* @return ZATHURA_ERROR_OK when no error occured, otherwise see
* zathura_error_t
*/
zathura_error_t zathura_document_free(zathura_document_t* document);
/**
* Returns the private data of the document
* Returns the page at the given index
*
* @param document The document
* @return The private data or NULL
* @param index The index of the page
* @return The page or NULL if an error occured
*/
void* zathura_document_get_data(zathura_document_t* document);
zathura_page_t* zathura_document_get_page(zathura_document_t* document, unsigned int index);
/**
* Returns the number of pages
......@@ -120,7 +94,7 @@ void zathura_document_set_number_of_pages(zathura_document_t* document, unsigned
* @param document The document
* @return Current page
*/
unsigned int zathura_document_get_current_page(zathura_document_t* document);
unsigned int zathura_document_get_current_page_number(zathura_document_t* document);
/**
* Sets the number of pages
......@@ -128,9 +102,81 @@ unsigned int zathura_document_get_current_page(zathura_document_t* document);
* @param document The document
* @param current_page The current page number
*/
void zathura_document_set_current_page(zathura_document_t* document, unsigned
void zathura_document_set_current_page_number(zathura_document_t* document, unsigned
int current_page);
/**
* Returns the current scale value of the document
*
* @param document The document
* @return The current scale value
*/
double zathura_document_get_scale(zathura_document_t* document);
/**
* Sets the new scale value of the document
*
* @param document The document
* @param scale The new scale value
*/
void zathura_document_set_scale(zathura_document_t* document, double scale);
/**
* Returns the rotation value of zathura (0..360)
*
* @param document The document
* @return The current rotation value
*/
unsigned int zathura_document_get_rotation(zathura_document_t* document);
/**
* Sets the new rotation value
*
* @param document The document
* @param rotation The new rotation value
*/
void zathura_document_set_rotation(zathura_document_t* document, unsigned int rotation);
/**
* Returns the adjust mode of the document
*
* @param document The document
* @return The adjust mode
*/
zathura_adjust_mode_t zathura_document_get_adjust_mode(zathura_document_t* document);
/**
* Sets the new adjust mode of the document
*
* @param document The document
* @param mode The new adjust mode
*/
void zathura_document_set_adjust_mode(zathura_document_t* document, zathura_adjust_mode_t mode);
/**
* Returns the page offset of the document