2011-09-02 20:46:16 +02:00
|
|
|
/* See LICENSE file for license and copyright information */
|
|
|
|
|
|
|
|
#include <sqlite3.h>
|
2011-10-23 17:01:15 +02:00
|
|
|
#include <girara/utils.h>
|
|
|
|
#include <girara/datastructures.h>
|
2013-03-14 22:13:39 +01:00
|
|
|
#include <girara/input-history.h>
|
2012-03-04 15:56:54 +01:00
|
|
|
#include <string.h>
|
2011-09-02 20:46:16 +02:00
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
#include "database-sqlite.h"
|
2011-09-02 20:46:16 +02:00
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
static void zathura_database_interface_init(ZathuraDatabaseInterface* iface);
|
2013-03-14 22:13:39 +01:00
|
|
|
static void io_interface_init(GiraraInputHistoryIOInterface* iface);
|
2012-03-04 15:56:54 +01:00
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE(ZathuraSQLDatabase, zathura_sqldatabase, G_TYPE_OBJECT,
|
2013-03-14 22:13:39 +01:00
|
|
|
G_IMPLEMENT_INTERFACE(ZATHURA_TYPE_DATABASE, zathura_database_interface_init)
|
|
|
|
G_IMPLEMENT_INTERFACE(GIRARA_TYPE_INPUT_HISTORY_IO, io_interface_init))
|
2012-03-04 15:56:54 +01:00
|
|
|
|
|
|
|
static void sqlite_finalize(GObject* object);
|
|
|
|
static bool sqlite_add_bookmark(zathura_database_t* db, const char* file,
|
2012-10-09 01:12:18 +02:00
|
|
|
zathura_bookmark_t* bookmark);
|
2012-03-09 08:04:54 +01:00
|
|
|
static bool sqlite_remove_bookmark(zathura_database_t* db, const char* file,
|
2012-10-09 01:12:18 +02:00
|
|
|
const char* id);
|
2012-03-09 08:04:54 +01:00
|
|
|
static girara_list_t* sqlite_load_bookmarks(zathura_database_t* db,
|
|
|
|
const char* file);
|
|
|
|
static bool sqlite_set_fileinfo(zathura_database_t* db, const char* file,
|
2012-10-09 01:12:18 +02:00
|
|
|
zathura_fileinfo_t* file_info);
|
2012-03-09 08:04:54 +01:00
|
|
|
static bool sqlite_get_fileinfo(zathura_database_t* db, const char* file,
|
2012-10-09 01:12:18 +02:00
|
|
|
zathura_fileinfo_t* file_info);
|
2012-03-09 08:04:54 +01:00
|
|
|
static void sqlite_set_property(GObject* object, guint prop_id,
|
2012-10-09 01:12:18 +02:00
|
|
|
const GValue* value, GParamSpec* pspec);
|
2013-03-14 22:13:39 +01:00
|
|
|
static void sqlite_io_append(GiraraInputHistoryIO* db, const char*);
|
|
|
|
static girara_list_t* sqlite_io_read(GiraraInputHistoryIO* db);
|
2012-03-04 15:56:54 +01:00
|
|
|
|
|
|
|
typedef struct zathura_sqldatabase_private_s {
|
2011-09-02 20:46:16 +02:00
|
|
|
sqlite3* session;
|
2012-03-04 15:56:54 +01:00
|
|
|
} zathura_sqldatabase_private_t;
|
|
|
|
|
|
|
|
#define ZATHURA_SQLDATABASE_GET_PRIVATE(obj) \
|
|
|
|
(G_TYPE_INSTANCE_GET_PRIVATE ((obj), ZATHURA_TYPE_SQLDATABASE, zathura_sqldatabase_private_t))
|
|
|
|
|
2012-10-09 01:12:18 +02:00
|
|
|
enum {
|
2012-03-04 15:56:54 +01:00
|
|
|
PROP_0,
|
|
|
|
PROP_PATH
|
2011-09-02 20:46:16 +02:00
|
|
|
};
|
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
static void
|
|
|
|
zathura_database_interface_init(ZathuraDatabaseInterface* iface)
|
|
|
|
{
|
|
|
|
/* initialize interface */
|
2012-03-09 08:04:54 +01:00
|
|
|
iface->add_bookmark = sqlite_add_bookmark;
|
2012-03-04 15:56:54 +01:00
|
|
|
iface->remove_bookmark = sqlite_remove_bookmark;
|
2012-03-09 08:04:54 +01:00
|
|
|
iface->load_bookmarks = sqlite_load_bookmarks;
|
|
|
|
iface->set_fileinfo = sqlite_set_fileinfo;
|
|
|
|
iface->get_fileinfo = sqlite_get_fileinfo;
|
2012-03-04 15:56:54 +01:00
|
|
|
}
|
|
|
|
|
2013-03-14 22:13:39 +01:00
|
|
|
static void
|
|
|
|
io_interface_init(GiraraInputHistoryIOInterface* iface)
|
|
|
|
{
|
|
|
|
/* initialize interface */
|
|
|
|
iface->append = sqlite_io_append;
|
|
|
|
iface->read = sqlite_io_read;
|
|
|
|
}
|
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
static void
|
|
|
|
zathura_sqldatabase_class_init(ZathuraSQLDatabaseClass* class)
|
|
|
|
{
|
|
|
|
/* add private members */
|
|
|
|
g_type_class_add_private(class, sizeof(zathura_sqldatabase_private_t));
|
|
|
|
|
|
|
|
/* override methods */
|
|
|
|
GObjectClass* object_class = G_OBJECT_CLASS(class);
|
2012-03-09 08:04:54 +01:00
|
|
|
object_class->finalize = sqlite_finalize;
|
2012-03-04 15:56:54 +01:00
|
|
|
object_class->set_property = sqlite_set_property;
|
|
|
|
|
|
|
|
g_object_class_install_property(object_class, PROP_PATH,
|
2012-10-09 01:12:18 +02:00
|
|
|
g_param_spec_string("path", "path", "path to the database", NULL, G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
|
2012-03-04 15:56:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
zathura_sqldatabase_init(ZathuraSQLDatabase* db)
|
|
|
|
{
|
|
|
|
zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db);
|
|
|
|
priv->session = NULL;
|
|
|
|
}
|
|
|
|
|
2011-09-02 20:46:16 +02:00
|
|
|
zathura_database_t*
|
2012-03-04 15:56:54 +01:00
|
|
|
zathura_sqldatabase_new(const char* path)
|
2011-09-21 09:22:12 +02:00
|
|
|
{
|
2012-03-04 15:56:54 +01:00
|
|
|
g_return_val_if_fail(path != NULL && strlen(path) != 0, NULL);
|
|
|
|
|
|
|
|
zathura_database_t* db = g_object_new(ZATHURA_TYPE_SQLDATABASE, "path", path, NULL);
|
|
|
|
zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db);
|
|
|
|
if (priv->session == NULL) {
|
2013-03-14 21:30:01 +01:00
|
|
|
g_object_unref(G_OBJECT(db));
|
2011-10-16 23:01:25 +02:00
|
|
|
return NULL;
|
2011-10-11 15:59:36 +02:00
|
|
|
}
|
2012-03-09 08:04:54 +01:00
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
return db;
|
|
|
|
}
|
2011-10-11 15:59:36 +02:00
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
static void
|
|
|
|
sqlite_finalize(GObject* object)
|
|
|
|
{
|
|
|
|
ZathuraSQLDatabase* db = ZATHURA_SQLDATABASE(object);
|
|
|
|
zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db);
|
|
|
|
if (priv->session) {
|
|
|
|
sqlite3_close(priv->session);
|
2011-10-10 23:46:50 +02:00
|
|
|
}
|
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
G_OBJECT_CLASS(zathura_sqldatabase_parent_class)->finalize(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sqlite_db_init(ZathuraSQLDatabase* db, const char* path)
|
|
|
|
{
|
|
|
|
zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db);
|
2011-09-29 15:23:13 +02:00
|
|
|
|
2011-09-02 20:46:16 +02:00
|
|
|
/* create bookmarks database */
|
|
|
|
static const char SQL_BOOKMARK_INIT[] =
|
|
|
|
"CREATE TABLE IF NOT EXISTS bookmarks ("
|
2012-10-09 01:12:18 +02:00
|
|
|
"file TEXT,"
|
|
|
|
"id TEXT,"
|
|
|
|
"page INTEGER,"
|
|
|
|
"PRIMARY KEY(file, id));";
|
2011-09-02 20:46:16 +02:00
|
|
|
|
|
|
|
static const char SQL_FILEINFO_INIT[] =
|
|
|
|
"CREATE TABLE IF NOT EXISTS fileinfo ("
|
2012-10-09 01:12:18 +02:00
|
|
|
"file TEXT PRIMARY KEY,"
|
|
|
|
"page INTEGER,"
|
|
|
|
"offset INTEGER,"
|
|
|
|
"scale FLOAT,"
|
|
|
|
"rotation INTEGER,"
|
|
|
|
"pages_per_row INTEGER,"
|
|
|
|
"first_page_column INTEGER,"
|
|
|
|
"position_x FLOAT,"
|
|
|
|
"position_y FLOAT"
|
|
|
|
");";
|
2011-09-02 20:46:16 +02:00
|
|
|
|
2012-04-27 21:56:41 +02:00
|
|
|
static const char SQL_FILEINFO_ALTER[] =
|
|
|
|
"ALTER TABLE fileinfo ADD COLUMN pages_per_row INTEGER;"
|
|
|
|
"ALTER TABLE fileinfo ADD COLUMN position_x FLOAT;"
|
|
|
|
"ALTER TABLE fileinfo ADD COLUMN position_y FLOAT;";
|
|
|
|
|
2012-06-27 22:34:16 +02:00
|
|
|
static const char SQL_FILEINFO_ALTER2[] =
|
|
|
|
"ALTER TABLE fileinfo ADD COLUMN first_page_column INTEGER;";
|
|
|
|
|
2013-03-14 22:13:39 +01:00
|
|
|
static const char SQL_HISTORY_INIT[] =
|
|
|
|
"CREATE TABLE IF NOT EXISTS history ("
|
|
|
|
"time TIMESTAMP,"
|
|
|
|
"line TEXT,"
|
|
|
|
"PRIMARY KEY(line));";
|
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
sqlite3* session = NULL;
|
|
|
|
if (sqlite3_open(path, &session) != SQLITE_OK) {
|
2011-09-02 20:46:16 +02:00
|
|
|
girara_error("Could not open database: %s\n", path);
|
2012-03-04 15:56:54 +01:00
|
|
|
return;
|
2011-09-02 20:46:16 +02:00
|
|
|
}
|
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
if (sqlite3_exec(session, SQL_BOOKMARK_INIT, NULL, 0, NULL) != SQLITE_OK) {
|
2011-09-02 20:46:16 +02:00
|
|
|
girara_error("Failed to initialize database: %s\n", path);
|
2012-03-04 15:56:54 +01:00
|
|
|
sqlite3_close(session);
|
|
|
|
return;
|
2011-09-02 20:46:16 +02:00
|
|
|
}
|
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
if (sqlite3_exec(session, SQL_FILEINFO_INIT, NULL, 0, NULL) != SQLITE_OK) {
|
2011-09-02 20:46:16 +02:00
|
|
|
girara_error("Failed to initialize database: %s\n", path);
|
2012-03-04 15:56:54 +01:00
|
|
|
sqlite3_close(session);
|
|
|
|
return;
|
2011-09-02 20:46:16 +02:00
|
|
|
}
|
|
|
|
|
2013-03-14 22:13:39 +01:00
|
|
|
if (sqlite3_exec(session, SQL_HISTORY_INIT, NULL, 0, NULL) != SQLITE_OK) {
|
|
|
|
girara_error("Failed to initialize database: %s\n", path);
|
|
|
|
sqlite3_close(session);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-27 21:56:41 +02:00
|
|
|
const char* data_type = NULL;
|
|
|
|
if (sqlite3_table_column_metadata(session, NULL, "fileinfo", "pages_per_row", &data_type, NULL, NULL, NULL, NULL) != SQLITE_OK) {
|
|
|
|
girara_debug("old database table layout detected; updating ...");
|
|
|
|
if (sqlite3_exec(session, SQL_FILEINFO_ALTER, NULL, 0, NULL) != SQLITE_OK) {
|
|
|
|
girara_warning("failed to update database table layout");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-27 22:34:16 +02:00
|
|
|
data_type = NULL;
|
|
|
|
if (sqlite3_table_column_metadata(session, NULL, "fileinfo", "first_page_column", &data_type, NULL, NULL, NULL, NULL) != SQLITE_OK) {
|
|
|
|
girara_debug("old database table layout detected; updating ...");
|
|
|
|
if (sqlite3_exec(session, SQL_FILEINFO_ALTER2, NULL, 0, NULL) != SQLITE_OK) {
|
|
|
|
girara_warning("failed to update database table layout");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
priv->session = session;
|
2011-09-02 20:46:16 +02:00
|
|
|
}
|
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
static void
|
|
|
|
sqlite_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec)
|
2011-09-21 09:22:12 +02:00
|
|
|
{
|
2012-03-04 15:56:54 +01:00
|
|
|
ZathuraSQLDatabase* db = ZATHURA_SQLDATABASE(object);
|
|
|
|
zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_PATH:
|
|
|
|
g_return_if_fail(priv->session == NULL);
|
|
|
|
sqlite_db_init(db, g_value_get_string(value));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
|
2011-10-10 23:46:50 +02:00
|
|
|
}
|
2011-09-02 20:46:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static sqlite3_stmt*
|
|
|
|
prepare_statement(sqlite3* session, const char* statement)
|
|
|
|
{
|
|
|
|
if (session == NULL || statement == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* pz_tail = NULL;
|
|
|
|
sqlite3_stmt* pp_stmt = NULL;
|
|
|
|
|
2011-10-15 18:26:04 +02:00
|
|
|
if (sqlite3_prepare_v2(session, statement, -1, &pp_stmt, &pz_tail) != SQLITE_OK) {
|
2011-09-02 20:46:16 +02:00
|
|
|
girara_error("Failed to prepare query: %s", statement);
|
|
|
|
sqlite3_finalize(pp_stmt);
|
|
|
|
return NULL;
|
|
|
|
} else if (pz_tail && *pz_tail != '\0') {
|
|
|
|
girara_error("Unused portion of statement: %s", pz_tail);
|
|
|
|
sqlite3_finalize(pp_stmt);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pp_stmt;
|
|
|
|
}
|
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
static bool
|
|
|
|
sqlite_add_bookmark(zathura_database_t* db, const char* file,
|
2012-10-09 01:12:18 +02:00
|
|
|
zathura_bookmark_t* bookmark)
|
2011-09-02 20:46:16 +02:00
|
|
|
{
|
2012-03-04 15:56:54 +01:00
|
|
|
zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db);
|
2011-09-02 20:46:16 +02:00
|
|
|
|
|
|
|
static const char SQL_BOOKMARK_ADD[] =
|
|
|
|
"REPLACE INTO bookmarks (file, id, page) VALUES (?, ?, ?);";
|
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
sqlite3_stmt* stmt = prepare_statement(priv->session, SQL_BOOKMARK_ADD);
|
2011-09-02 20:46:16 +02:00
|
|
|
if (stmt == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sqlite3_bind_text(stmt, 1, file, -1, NULL) != SQLITE_OK ||
|
|
|
|
sqlite3_bind_text(stmt, 2, bookmark->id, -1, NULL) != SQLITE_OK ||
|
|
|
|
sqlite3_bind_int(stmt, 3, bookmark->page) != SQLITE_OK) {
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
girara_error("Failed to bind arguments.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int res = sqlite3_step(stmt);
|
|
|
|
sqlite3_finalize(stmt);
|
2012-03-09 08:04:54 +01:00
|
|
|
|
|
|
|
return (res == SQLITE_DONE) ? true : false;
|
2011-09-02 20:46:16 +02:00
|
|
|
}
|
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
static bool
|
|
|
|
sqlite_remove_bookmark(zathura_database_t* db, const char* file, const char*
|
2012-10-09 01:12:18 +02:00
|
|
|
id)
|
2011-09-02 20:46:16 +02:00
|
|
|
{
|
2012-03-04 15:56:54 +01:00
|
|
|
zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db);
|
2011-09-02 20:46:16 +02:00
|
|
|
|
|
|
|
static const char SQL_BOOKMARK_ADD[] =
|
2011-10-15 18:26:04 +02:00
|
|
|
"DELETE FROM bookmarks WHERE file = ? AND id = ?;";
|
2011-09-02 20:46:16 +02:00
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
sqlite3_stmt* stmt = prepare_statement(priv->session, SQL_BOOKMARK_ADD);
|
2011-09-02 20:46:16 +02:00
|
|
|
if (stmt == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sqlite3_bind_text(stmt, 1, file, -1, NULL) != SQLITE_OK ||
|
|
|
|
sqlite3_bind_text(stmt, 2, id, -1, NULL) != SQLITE_OK) {
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
girara_error("Failed to bind arguments.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int res = sqlite3_step(stmt);
|
|
|
|
sqlite3_finalize(stmt);
|
2012-03-09 08:04:54 +01:00
|
|
|
|
|
|
|
return (res == SQLITE_DONE) ? true : false;
|
2011-09-02 20:46:16 +02:00
|
|
|
}
|
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
static girara_list_t*
|
|
|
|
sqlite_load_bookmarks(zathura_database_t* db, const char* file)
|
2011-09-02 20:46:16 +02:00
|
|
|
{
|
2012-03-04 15:56:54 +01:00
|
|
|
zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db);
|
2011-09-02 20:46:16 +02:00
|
|
|
|
|
|
|
static const char SQL_BOOKMARK_SELECT[] =
|
|
|
|
"SELECT id, page FROM bookmarks WHERE file = ?;";
|
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
sqlite3_stmt* stmt = prepare_statement(priv->session, SQL_BOOKMARK_SELECT);
|
2011-09-02 20:46:16 +02:00
|
|
|
if (stmt == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sqlite3_bind_text(stmt, 1, file, -1, NULL) != SQLITE_OK) {
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
girara_error("Failed to bind arguments.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-10-15 18:42:30 +02:00
|
|
|
girara_list_t* result = girara_sorted_list_new2((girara_compare_function_t) zathura_bookmarks_compare,
|
2012-10-09 01:12:18 +02:00
|
|
|
(girara_free_function_t) zathura_bookmark_free);
|
2012-03-09 08:04:54 +01:00
|
|
|
|
2011-09-02 20:46:16 +02:00
|
|
|
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
|
|
|
zathura_bookmark_t* bookmark = g_malloc0(sizeof(zathura_bookmark_t));
|
2011-10-11 23:18:21 +02:00
|
|
|
|
|
|
|
bookmark->id = g_strdup((const char*) sqlite3_column_text(stmt, 0));
|
2011-09-02 20:46:16 +02:00
|
|
|
bookmark->page = sqlite3_column_int(stmt, 1);
|
2011-09-29 15:23:13 +02:00
|
|
|
|
2011-09-02 20:46:16 +02:00
|
|
|
girara_list_append(result, bookmark);
|
|
|
|
}
|
2012-03-09 08:04:54 +01:00
|
|
|
|
2011-09-02 20:46:16 +02:00
|
|
|
sqlite3_finalize(stmt);
|
2012-03-09 08:04:54 +01:00
|
|
|
|
2011-09-02 20:46:16 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
static bool
|
2012-04-20 21:15:03 +02:00
|
|
|
sqlite_set_fileinfo(zathura_database_t* db, const char* file,
|
2012-10-09 01:12:18 +02:00
|
|
|
zathura_fileinfo_t* file_info)
|
2011-09-21 18:31:12 +02:00
|
|
|
{
|
2012-04-20 21:15:03 +02:00
|
|
|
if (db == NULL || file == NULL || file_info == NULL) {
|
2012-04-22 10:04:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-20 21:15:03 +02:00
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db);
|
2011-09-21 18:31:12 +02:00
|
|
|
|
|
|
|
static const char SQL_FILEINFO_SET[] =
|
2012-06-27 22:34:16 +02:00
|
|
|
"REPLACE INTO fileinfo (file, page, offset, scale, rotation, pages_per_row, first_page_column, position_x, position_y) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";
|
2011-09-21 18:31:12 +02:00
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
sqlite3_stmt* stmt = prepare_statement(priv->session, SQL_FILEINFO_SET);
|
2011-09-21 18:31:12 +02:00
|
|
|
if (stmt == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-27 22:34:16 +02:00
|
|
|
if (sqlite3_bind_text(stmt, 1, file, -1, NULL) != SQLITE_OK ||
|
|
|
|
sqlite3_bind_int(stmt, 2, file_info->current_page) != SQLITE_OK ||
|
|
|
|
sqlite3_bind_int(stmt, 3, file_info->page_offset) != 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, 6, file_info->pages_per_row) != SQLITE_OK ||
|
|
|
|
sqlite3_bind_int(stmt, 7, file_info->first_page_column) != SQLITE_OK ||
|
|
|
|
sqlite3_bind_double(stmt, 8, file_info->position_x) != SQLITE_OK ||
|
|
|
|
sqlite3_bind_double(stmt, 9, file_info->position_y) != SQLITE_OK) {
|
2011-09-21 18:31:12 +02:00
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
girara_error("Failed to bind arguments.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int res = sqlite3_step(stmt);
|
|
|
|
sqlite3_finalize(stmt);
|
2012-03-09 08:04:54 +01:00
|
|
|
|
|
|
|
return (res == SQLITE_DONE) ? true : false;
|
2011-09-21 18:31:12 +02:00
|
|
|
}
|
2011-09-22 15:47:58 +02:00
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
static bool
|
2012-04-20 21:15:03 +02:00
|
|
|
sqlite_get_fileinfo(zathura_database_t* db, const char* file,
|
2012-10-09 01:12:18 +02:00
|
|
|
zathura_fileinfo_t* file_info)
|
2011-09-22 15:47:58 +02:00
|
|
|
{
|
2012-04-22 10:04:46 +02:00
|
|
|
if (db == NULL || file == NULL || file_info == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-20 21:15:03 +02:00
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db);
|
2011-09-22 15:47:58 +02:00
|
|
|
|
|
|
|
static const char SQL_FILEINFO_GET[] =
|
2012-06-27 22:34:16 +02:00
|
|
|
"SELECT page, offset, scale, rotation, pages_per_row, first_page_column, position_x, position_y FROM fileinfo WHERE file = ?;";
|
2011-09-22 15:47:58 +02:00
|
|
|
|
2012-03-04 15:56:54 +01:00
|
|
|
sqlite3_stmt* stmt = prepare_statement(priv->session, SQL_FILEINFO_GET);
|
2011-09-22 15:47:58 +02:00
|
|
|
if (stmt == NULL) {
|
2012-04-22 10:04:46 +02:00
|
|
|
return false;
|
2011-09-22 15:47:58 +02:00
|
|
|
}
|
|
|
|
|
2011-09-29 11:46:52 +02:00
|
|
|
if (sqlite3_bind_text(stmt, 1, file, -1, NULL) != SQLITE_OK) {
|
2011-09-22 15:47:58 +02:00
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
girara_error("Failed to bind arguments.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sqlite3_step(stmt) != SQLITE_ROW) {
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
girara_info("No info for file %s available.", file);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-27 22:34:16 +02:00
|
|
|
file_info->current_page = sqlite3_column_int(stmt, 0);
|
|
|
|
file_info->page_offset = sqlite3_column_int(stmt, 1);
|
|
|
|
file_info->scale = sqlite3_column_double(stmt, 2);
|
|
|
|
file_info->rotation = sqlite3_column_int(stmt, 3);
|
|
|
|
file_info->pages_per_row = sqlite3_column_int(stmt, 4);
|
|
|
|
file_info->first_page_column = sqlite3_column_int(stmt, 5);
|
|
|
|
file_info->position_x = sqlite3_column_double(stmt, 6);
|
|
|
|
file_info->position_y = sqlite3_column_double(stmt, 7);
|
2012-04-20 21:15:03 +02:00
|
|
|
|
2011-09-22 15:47:58 +02:00
|
|
|
sqlite3_finalize(stmt);
|
2012-03-09 08:04:54 +01:00
|
|
|
|
2011-09-22 15:47:58 +02:00
|
|
|
return true;
|
|
|
|
}
|
2013-03-14 22:13:39 +01:00
|
|
|
|
|
|
|
static void
|
|
|
|
sqlite_io_append(GiraraInputHistoryIO* db, const char* input)
|
|
|
|
{
|
|
|
|
static const char SQL_HISTORY_SET[] =
|
|
|
|
"REPLACE INTO history (line, time) VALUES (?, DATETIME('now'));";
|
|
|
|
|
|
|
|
zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db);
|
|
|
|
sqlite3_stmt* stmt = prepare_statement(priv->session, SQL_HISTORY_SET);
|
|
|
|
if (stmt == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sqlite3_bind_text(stmt, 1, input, -1, NULL) != SQLITE_OK) {
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
girara_error("Failed to bind arguments.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlite3_step(stmt);
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static girara_list_t*
|
|
|
|
sqlite_io_read(GiraraInputHistoryIO* db)
|
|
|
|
{
|
|
|
|
static const char SQL_HISTORY_GET[] =
|
|
|
|
"SELECT line FROM history ORDER BY time";
|
|
|
|
|
|
|
|
zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db);
|
|
|
|
sqlite3_stmt* stmt = prepare_statement(priv->session, SQL_HISTORY_GET);
|
|
|
|
if (stmt == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
girara_list_t* list = girara_list_new2((girara_free_function_t) g_free);
|
|
|
|
if (list == NULL) {
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
|
|
|
girara_list_append(list, g_strdup((const char*) sqlite3_column_text(stmt, 0)));
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlite3_finalize(stmt);
|
|
|
|
return list;
|
|
|
|
}
|