Merge remote-tracking branch 'haggl/feature/toggle_page_mode_improvement' into develop

This commit is contained in:
Sebastian Ramacher 2015-11-12 22:15:18 +01:00
commit 43244a67d2
10 changed files with 165 additions and 33 deletions

View file

@ -731,9 +731,12 @@ Defines the number of pages that are rendered next to each other in a row.
first-page-column first-page-column
^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^
Defines the column in which the first page will be displayed. Defines the column in which the first page will be displayed.
This setting is stored separately for every value of pages-per-row according to
the following pattern <1 page per row>:[<2 pages per row>[: ...]]. Per default,
the first column is set to 2 for double-page layout.
* Value type: Integer * Value type: String
* Default value: 1 * Default value: 1:2
recolor recolor
^^^^^^^ ^^^^^^^

View file

@ -243,8 +243,13 @@ cb_page_layout_value_changed(girara_session_t* session, const char* name, girara
unsigned int pages_per_row = 1; unsigned int pages_per_row = 1;
girara_setting_get(session, "pages-per-row", &pages_per_row); girara_setting_get(session, "pages-per-row", &pages_per_row);
unsigned int first_page_column = 1; /* get list of first_page_column settings */
girara_setting_get(session, "first-page-column", &first_page_column); char* first_page_column_list = NULL;
girara_setting_get(session, "first-page-column", &first_page_column_list);
/* find value for first_page_column */
unsigned int first_page_column = find_first_page_column(first_page_column_list, pages_per_row);
g_free(first_page_column_list);
unsigned int page_padding = 1; unsigned int page_padding = 1;
girara_setting_get(zathura->ui.session, "page-padding", &page_padding); girara_setting_get(zathura->ui.session, "page-padding", &page_padding);

View file

@ -149,7 +149,7 @@ config_load_default(zathura_t* zathura)
int_value = 1; int_value = 1;
girara_setting_add(gsession, "pages-per-row", &int_value, INT, false, _("Number of pages per row"), cb_page_layout_value_changed, NULL); girara_setting_add(gsession, "pages-per-row", &int_value, INT, false, _("Number of pages per row"), cb_page_layout_value_changed, NULL);
int_value = 1; int_value = 1;
girara_setting_add(gsession, "first-page-column", &int_value, INT, false, _("Column of the first page"), cb_page_layout_value_changed, NULL); girara_setting_add(gsession, "first-page-column", "1:2", STRING, false, _("Column of the first page"), cb_page_layout_value_changed, NULL);
float_value = 40; float_value = 40;
girara_setting_add(gsession, "scroll-step", &float_value, FLOAT, false, _("Scroll step"), NULL, NULL); girara_setting_add(gsession, "scroll-step", &float_value, FLOAT, false, _("Scroll step"), NULL, NULL);
float_value = 40; float_value = 40;

View file

@ -559,7 +559,7 @@ plain_set_fileinfo(zathura_database_t* db, const char* file, zathura_fileinfo_t*
g_key_file_set_double (priv->history, name, KEY_SCALE, file_info->scale); g_key_file_set_double (priv->history, name, KEY_SCALE, file_info->scale);
g_key_file_set_integer(priv->history, name, KEY_ROTATE, file_info->rotation); g_key_file_set_integer(priv->history, name, KEY_ROTATE, file_info->rotation);
g_key_file_set_integer(priv->history, name, KEY_PAGES_PER_ROW, file_info->pages_per_row); g_key_file_set_integer(priv->history, name, KEY_PAGES_PER_ROW, file_info->pages_per_row);
g_key_file_set_integer(priv->history, name, KEY_FIRST_PAGE_COLUMN, file_info->first_page_column); g_key_file_set_string(priv->history, name, KEY_FIRST_PAGE_COLUMN, file_info->first_page_column_list);
g_key_file_set_double (priv->history, name, KEY_POSITION_X, file_info->position_x); g_key_file_set_double (priv->history, name, KEY_POSITION_X, file_info->position_x);
g_key_file_set_double (priv->history, name, KEY_POSITION_Y, file_info->position_y); g_key_file_set_double (priv->history, name, KEY_POSITION_Y, file_info->position_y);
g_key_file_set_integer(priv->history, name, KEY_TIME, time(NULL)); g_key_file_set_integer(priv->history, name, KEY_TIME, time(NULL));
@ -600,7 +600,7 @@ plain_get_fileinfo(zathura_database_t* db, const char* file, zathura_fileinfo_t*
file_info->pages_per_row = g_key_file_get_integer(priv->history, name, KEY_PAGES_PER_ROW, NULL); file_info->pages_per_row = g_key_file_get_integer(priv->history, name, KEY_PAGES_PER_ROW, NULL);
} }
if (g_key_file_has_key(priv->history, name, KEY_FIRST_PAGE_COLUMN, NULL) == TRUE) { if (g_key_file_has_key(priv->history, name, KEY_FIRST_PAGE_COLUMN, NULL) == TRUE) {
file_info->first_page_column = g_key_file_get_integer(priv->history, name, KEY_FIRST_PAGE_COLUMN, NULL); file_info->first_page_column_list = g_key_file_get_string(priv->history, name, KEY_FIRST_PAGE_COLUMN, NULL);
} }
if (g_key_file_has_key(priv->history, name, KEY_POSITION_X, NULL) == TRUE) { if (g_key_file_has_key(priv->history, name, KEY_POSITION_X, NULL) == TRUE) {
file_info->position_x = g_key_file_get_double(priv->history, name, KEY_POSITION_X, NULL); file_info->position_x = g_key_file_get_double(priv->history, name, KEY_POSITION_X, NULL);

View file

@ -5,6 +5,7 @@
#include <girara/datastructures.h> #include <girara/datastructures.h>
#include <girara/input-history.h> #include <girara/input-history.h>
#include <string.h> #include <string.h>
#include <strings.h>
#include "database-sqlite.h" #include "database-sqlite.h"
#include "utils.h" #include "utils.h"
@ -17,6 +18,7 @@ G_DEFINE_TYPE_WITH_CODE(ZathuraSQLDatabase, zathura_sqldatabase, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE(GIRARA_TYPE_INPUT_HISTORY_IO, io_interface_init)) G_IMPLEMENT_INTERFACE(GIRARA_TYPE_INPUT_HISTORY_IO, io_interface_init))
static bool check_column(sqlite3* session, const char* table, const char* col, bool* result); static bool check_column(sqlite3* session, const char* table, const char* col, bool* result);
static bool check_column_type(sqlite3* session, const char* table, const char* col, const char* type, bool* result);
static void sqlite_finalize(GObject* object); static void sqlite_finalize(GObject* object);
static bool sqlite_add_bookmark(zathura_database_t* db, const char* file, zathura_bookmark_t* bookmark); static bool sqlite_add_bookmark(zathura_database_t* db, const char* file, zathura_bookmark_t* bookmark);
static bool sqlite_remove_bookmark(zathura_database_t* db, const char* file, const char* id); static bool sqlite_remove_bookmark(zathura_database_t* db, const char* file, const char* id);
@ -148,7 +150,7 @@ sqlite_db_init(ZathuraSQLDatabase* db, const char* path)
"scale FLOAT," "scale FLOAT,"
"rotation INTEGER," "rotation INTEGER,"
"pages_per_row INTEGER," "pages_per_row INTEGER,"
"first_page_column INTEGER," "first_page_column TEXT,"
"position_x FLOAT," "position_x FLOAT,"
"position_y FLOAT," "position_y FLOAT,"
"time TIMESTAMP" "time TIMESTAMP"
@ -176,9 +178,9 @@ sqlite_db_init(ZathuraSQLDatabase* db, const char* path)
/* update fileinfo table (part 2) */ /* update fileinfo table (part 2) */
static const char SQL_FILEINFO_ALTER2[] = static const char SQL_FILEINFO_ALTER2[] =
"ALTER TABLE fileinfo ADD COLUMN first_page_column INTEGER;"; "ALTER TABLE fileinfo ADD COLUMN first_page_column TEXT;";
/* update fileinfo table (part 2) */ /* update fileinfo table (part 3) */
static const char SQL_FILEINFO_ALTER3[] = static const char SQL_FILEINFO_ALTER3[] =
"ALTER TABLE fileinfo ADD COLUMN time TIMESTAMP;"; "ALTER TABLE fileinfo ADD COLUMN time TIMESTAMP;";
@ -242,6 +244,37 @@ sqlite_db_init(ZathuraSQLDatabase* db, const char* path)
} }
} }
/* check existing tables for correct column types */
ret1 = check_column_type(session, "fileinfo", "first_page_column", "TEXT", &res1);
if (ret1 == true && res1 == false) {
girara_debug("old database table layout detected; updating ...");
/* prepare transaction */
char tx_begin[] = "BEGIN TRANSACTION;"
"ALTER TABLE fileinfo RENAME TO tmp;";
char tx_end[] = "INSERT INTO fileinfo SELECT * FROM tmp;"
"DROP TABLE tmp;"
"COMMIT;";
/* calculate requred buffer size */
size_t tx_buffer_size = strlen(tx_begin);
tx_buffer_size += strlen(SQL_FILEINFO_INIT);
tx_buffer_size += strlen(tx_end);
++tx_buffer_size;
/* assemble transaction */
char transaction[tx_buffer_size];
bzero(transaction, tx_buffer_size);
strcat(transaction, tx_begin);
strcat(transaction, SQL_FILEINFO_INIT);
strcat(transaction, tx_end);
if (sqlite3_exec(session, transaction, NULL, 0, NULL) != SQLITE_OK) {
girara_warning("failed to update database table layout");
}
}
priv->session = session; priv->session = session;
} }
@ -318,6 +351,42 @@ check_column(sqlite3* session, const char* table, const char* col, bool* res)
return true; return true;
} }
static bool
check_column_type(sqlite3* session, const char* table, const char* col, const char* type, bool* res)
{
/* we can't actually bind the argument with sqlite3_bind_text because
* sqlite3_prepare_v2 fails with "PRAGMA table_info(?);" */
char* query = sqlite3_mprintf("PRAGMA table_info(%Q);", table);
if (query == NULL) {
return false;
}
sqlite3_stmt* stmt = prepare_statement(session, query);
if (stmt == NULL) {
return false;
}
*res = false;
while (sqlite3_step(stmt) == SQLITE_ROW) {
if (strcmp((const char*) sqlite3_column_text(stmt, 1), col) == 0) {
if (strcmp((const char*) sqlite3_column_text(stmt, 2), type) == 0) {
*res = true;
break;
}
}
}
if (*res == false) {
girara_debug("column %s in table %s has wrong type", col, table);
}
sqlite3_finalize(stmt);
sqlite3_free(query);
return true;
}
static bool static bool
sqlite_add_bookmark(zathura_database_t* db, const char* file, sqlite_add_bookmark(zathura_database_t* db, const char* file,
zathura_bookmark_t* bookmark) zathura_bookmark_t* bookmark)
@ -587,7 +656,8 @@ sqlite_set_fileinfo(zathura_database_t* db, const char* file,
sqlite3_bind_double(stmt, 4, file_info->scale) != SQLITE_OK || sqlite3_bind_double(stmt, 4, file_info->scale) != SQLITE_OK ||
sqlite3_bind_int(stmt, 5, file_info->rotation) != SQLITE_OK || sqlite3_bind_int(stmt, 5, file_info->rotation) != SQLITE_OK ||
sqlite3_bind_int(stmt, 6, file_info->pages_per_row) != SQLITE_OK || sqlite3_bind_int(stmt, 6, file_info->pages_per_row) != SQLITE_OK ||
sqlite3_bind_int(stmt, 7, file_info->first_page_column) != SQLITE_OK || sqlite3_bind_text(stmt, 7, file_info->first_page_column_list, -1, NULL)
!= SQLITE_OK ||
sqlite3_bind_double(stmt, 8, file_info->position_x) != SQLITE_OK || sqlite3_bind_double(stmt, 8, file_info->position_x) != SQLITE_OK ||
sqlite3_bind_double(stmt, 9, file_info->position_y) != SQLITE_OK) { sqlite3_bind_double(stmt, 9, file_info->position_y) != SQLITE_OK) {
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
@ -631,14 +701,14 @@ sqlite_get_fileinfo(zathura_database_t* db, const char* file,
return false; return false;
} }
file_info->current_page = sqlite3_column_int(stmt, 0); file_info->current_page = sqlite3_column_int(stmt, 0);
file_info->page_offset = sqlite3_column_int(stmt, 1); file_info->page_offset = sqlite3_column_int(stmt, 1);
file_info->scale = sqlite3_column_double(stmt, 2); file_info->scale = sqlite3_column_double(stmt, 2);
file_info->rotation = sqlite3_column_int(stmt, 3); file_info->rotation = sqlite3_column_int(stmt, 3);
file_info->pages_per_row = sqlite3_column_int(stmt, 4); file_info->pages_per_row = sqlite3_column_int(stmt, 4);
file_info->first_page_column = sqlite3_column_int(stmt, 5); file_info->first_page_column_list = g_strdup((const char*) sqlite3_column_text(stmt, 5));
file_info->position_x = sqlite3_column_double(stmt, 6); file_info->position_x = sqlite3_column_double(stmt, 6);
file_info->position_y = sqlite3_column_double(stmt, 7); file_info->position_y = sqlite3_column_double(stmt, 7);
sqlite3_finalize(stmt); sqlite3_finalize(stmt);

View file

@ -15,7 +15,7 @@ typedef struct zathura_fileinfo_s {
double scale; double scale;
unsigned int rotation; unsigned int rotation;
unsigned int pages_per_row; unsigned int pages_per_row;
unsigned int first_page_column; char* first_page_column_list;
double position_x; double position_x;
double position_y; double position_y;
} zathura_fileinfo_t; } zathura_fileinfo_t;

View file

@ -1286,7 +1286,7 @@ sc_toggle_presentation(girara_session_t* session, girara_argument_t*
} }
static int pages_per_row = 1; static int pages_per_row = 1;
static int first_page_column = 1; static char* first_page_column_list = "1:2";
static double zoom = 1.0; static double zoom = 1.0;
const girara_mode_t old_mode = girara_mode_get(session); const girara_mode_t old_mode = girara_mode_get(session);
@ -1295,7 +1295,7 @@ sc_toggle_presentation(girara_session_t* session, girara_argument_t*
girara_setting_set(session, "pages-per-row", &pages_per_row); girara_setting_set(session, "pages-per-row", &pages_per_row);
/* reset first page column */ /* reset first page column */
girara_setting_set(session, "first-page-column", &first_page_column); girara_setting_set(session, "first-page-column", first_page_column_list);
/* show status bar */ /* show status bar */
gtk_widget_show(GTK_WIDGET(session->gtk.statusbar)); gtk_widget_show(GTK_WIDGET(session->gtk.statusbar));
@ -1308,14 +1308,15 @@ sc_toggle_presentation(girara_session_t* session, girara_argument_t*
render_all(zathura); render_all(zathura);
refresh_view(zathura); refresh_view(zathura);
/* setm ode */ /* set mode */
girara_mode_set(session, zathura->modes.normal); girara_mode_set(session, zathura->modes.normal);
} else if (old_mode == zathura->modes.normal) { } else if (old_mode == zathura->modes.normal) {
/* backup pages per row */ /* backup pages per row */
girara_setting_get(session, "pages-per-row", &pages_per_row); girara_setting_get(session, "pages-per-row", &pages_per_row);
/* backup first page column */ /* backup first page column */
girara_setting_get(session, "first-page-column", &first_page_column); g_free(first_page_column_list);
girara_setting_get(session, "first-page-column", &first_page_column_list);
/* set single view */ /* set single view */
int int_value = 1; int int_value = 1;
@ -1336,7 +1337,7 @@ sc_toggle_presentation(girara_session_t* session, girara_argument_t*
gtk_window_fullscreen(GTK_WINDOW(session->gtk.window)); gtk_window_fullscreen(GTK_WINDOW(session->gtk.window));
refresh_view(zathura); refresh_view(zathura);
/* setm ode */ /* set mode */
girara_mode_set(session, zathura->modes.presentation); girara_mode_set(session, zathura->modes.presentation);
} }

View file

@ -250,3 +250,33 @@ get_selection(zathura_t* zathura)
return selection; return selection;
} }
unsigned int
find_first_page_column(const char* first_page_column_list,
const unsigned int pages_per_row)
{
/* sanity checks */
unsigned int first_page_column = 1;
g_return_val_if_fail(first_page_column_list != NULL, first_page_column);
g_return_val_if_fail(strcmp(first_page_column_list, ""), first_page_column);
g_return_val_if_fail(pages_per_row > 0, first_page_column);
/* split settings list */
char** settings = g_strsplit(first_page_column_list, ":", pages_per_row+1);
size_t settings_size = 0;
while (settings[settings_size] != NULL) {
++settings_size;
}
/* read setting value corresponding to the specified pages per row */
unsigned int index = pages_per_row - 1;
if (settings_size > index && strcmp(settings[index], "")) {
first_page_column = atoi(settings[index]);
}
/* free buffers */
g_strfreev(settings);
return first_page_column;
}

View file

@ -107,4 +107,17 @@ GdkAtom* get_selection(zathura_t* zathura);
double zathura_correct_scale_value(girara_session_t* session, const double double zathura_correct_scale_value(girara_session_t* session, const double
scale); scale);
/**
* Extracts the column the first page should be rendered in from the specified
* list of settings corresponding to the specified pages per row
*
* @param[in] first_page_column_list The settings list
* @param[in] pages_per_row The current pages per row
*
* @return The column the first page should be rendered in
*/
unsigned int find_first_page_column(const char* first_page_column_list,
const unsigned int pages_per_row);
#endif // UTILS_H #endif // UTILS_H

View file

@ -7,6 +7,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <math.h> #include <math.h>
#include <string.h>
#include <girara/datastructures.h> #include <girara/datastructures.h>
#include <girara/utils.h> #include <girara/utils.h>
@ -651,13 +652,14 @@ document_open(zathura_t* zathura, const char* path, const char* password,
} }
/* read history file */ /* read history file */
char first_page_column_list_default[] = "1:2";
zathura_fileinfo_t file_info = { zathura_fileinfo_t file_info = {
.current_page = 0, .current_page = 0,
.page_offset = 0, .page_offset = 0,
.scale = 1, .scale = 1,
.rotation = 0, .rotation = 0,
.pages_per_row = 0, .pages_per_row = 0,
.first_page_column = 0, .first_page_column_list = first_page_column_list_default,
.position_x = 0, .position_x = 0,
.position_y = 0 .position_y = 0
}; };
@ -855,7 +857,7 @@ document_open(zathura_t* zathura, const char* path, const char* password,
/* view mode */ /* view mode */
unsigned int pages_per_row = 1; unsigned int pages_per_row = 1;
unsigned int first_page_column = 1; char* first_page_column_list = first_page_column_list_default;
unsigned int page_padding = 1; unsigned int page_padding = 1;
girara_setting_get(zathura->ui.session, "page-padding", &page_padding); girara_setting_get(zathura->ui.session, "page-padding", &page_padding);
@ -866,14 +868,19 @@ document_open(zathura_t* zathura, const char* path, const char* password,
girara_setting_get(zathura->ui.session, "pages-per-row", &pages_per_row); girara_setting_get(zathura->ui.session, "pages-per-row", &pages_per_row);
} }
if (file_info.first_page_column > 0) { /* read first_page_column list */
first_page_column = file_info.first_page_column; if (strcmp(file_info.first_page_column_list, "")) {
first_page_column_list = file_info.first_page_column_list;
} else { } else {
girara_setting_get(zathura->ui.session, "first-page-column", &first_page_column); girara_setting_get(zathura->ui.session, "first-page-column", &first_page_column_list);
} }
/* find value for first_page_column */
unsigned int first_page_column = find_first_page_column(first_page_column_list, pages_per_row);
girara_setting_set(zathura->ui.session, "pages-per-row", &pages_per_row); girara_setting_set(zathura->ui.session, "pages-per-row", &pages_per_row);
girara_setting_set(zathura->ui.session, "first-page-column", &first_page_column); girara_setting_set(zathura->ui.session, "first-page-column", first_page_column_list);
g_free(first_page_column_list);
page_widget_set_mode(zathura, page_padding, pages_per_row, first_page_column); page_widget_set_mode(zathura, page_padding, pages_per_row, first_page_column);
zathura_document_set_page_layout(zathura->document, page_padding, pages_per_row, first_page_column); zathura_document_set_page_layout(zathura->document, page_padding, pages_per_row, first_page_column);
@ -1072,14 +1079,14 @@ document_close(zathura_t* zathura, bool keep_monitor)
/* store file information */ /* store file information */
const char* path = zathura_document_get_path(zathura->document); const char* path = zathura_document_get_path(zathura->document);
zathura_fileinfo_t file_info = { 0, 0, 1, 0, 1, 1, 0, 0 }; zathura_fileinfo_t file_info = { 0, 0, 1, 0, 1, "1:2", 0, 0 };
file_info.current_page = zathura_document_get_current_page_number(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.page_offset = zathura_document_get_page_offset(zathura->document);
file_info.scale = zathura_document_get_scale(zathura->document); file_info.scale = zathura_document_get_scale(zathura->document);
file_info.rotation = zathura_document_get_rotation(zathura->document); file_info.rotation = zathura_document_get_rotation(zathura->document);
girara_setting_get(zathura->ui.session, "pages-per-row", &(file_info.pages_per_row)); girara_setting_get(zathura->ui.session, "pages-per-row", &(file_info.pages_per_row));
girara_setting_get(zathura->ui.session, "first-page-column", &(file_info.first_page_column)); girara_setting_get(zathura->ui.session, "first-page-column", &(file_info.first_page_column_list));
/* get position */ /* get position */
file_info.position_x = zathura_document_get_position_x(zathura->document); file_info.position_x = zathura_document_get_position_x(zathura->document);
@ -1093,6 +1100,9 @@ document_close(zathura_t* zathura, bool keep_monitor)
zathura_db_save_jumplist(zathura->database, path, zathura->jumplist.list); zathura_db_save_jumplist(zathura->database, path, zathura->jumplist.list);
} }
/* free buffers */
g_free(file_info.first_page_column_list);
girara_list_iterator_free(zathura->jumplist.cur); girara_list_iterator_free(zathura->jumplist.cur);
zathura->jumplist.cur = NULL; zathura->jumplist.cur = NULL;
girara_list_free(zathura->jumplist.list); girara_list_free(zathura->jumplist.list);