Check basepath in database backend

Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
This commit is contained in:
Sebastian Ramacher 2017-04-30 12:07:29 +02:00
parent 1317675c47
commit ae97de3ab5
5 changed files with 23 additions and 20 deletions

View file

@ -191,18 +191,15 @@ list_files_for_cc(zathura_t* zathura, const char* input, bool check_file_ext, in
}
if (show_recent > 0 && zathura->database != NULL) {
girara_list_t* recent_files = zathura_db_get_recent_files(zathura->database, show_recent);
girara_list_t* recent_files = zathura_db_get_recent_files(zathura->database, show_recent, path);
if (recent_files == NULL) {
goto error_free;
}
if (girara_list_size(recent_files) != 0) {
const size_t path_len = strlen(path);
GIRARA_LIST_FOREACH(recent_files, const char*, iter, file)
if (strncmp(path, file, path_len) == 0) {
girara_debug("adding %s (recent file)", file);
girara_completion_group_add_element(history_group, file, NULL);
}
girara_debug("adding %s (recent file)", file);
girara_completion_group_add_element(history_group, file, NULL);
GIRARA_LIST_FOREACH_END(recent_files, const char*, iter, file);
girara_list_free(recent_files);
} else {

View file

@ -63,7 +63,7 @@ static bool plain_get_fileinfo(zathura_database_t* db, const char* fil
static void plain_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec);
static void plain_io_append(GiraraInputHistoryIO* db, const char*);
static girara_list_t* plain_io_read(GiraraInputHistoryIO* db);
static girara_list_t* plain_get_recent_files(zathura_database_t* db, int max);
static girara_list_t* plain_get_recent_files(zathura_database_t* db, int max, const char* basepath);
/* forward declaration */
static bool zathura_db_check_file(const char* path);
@ -852,7 +852,7 @@ compare_time(const void* l, const void* r, void* data)
}
static girara_list_t*
plain_get_recent_files(zathura_database_t* db, int max)
plain_get_recent_files(zathura_database_t* db, int max, const char* basepath)
{
zathura_plaindatabase_private_t* priv = ZATHURA_PLAINDATABASE_GET_PRIVATE(db);
@ -868,12 +868,15 @@ plain_get_recent_files(zathura_database_t* db, int max)
g_qsort_with_data(groups, groups_size, sizeof(gchar*), compare_time, priv->history);
}
if (max >= 0 && (gsize) max < groups_size) {
groups_size = max;
}
const size_t basepath_len = basepath != NULL ? strlen(basepath) : 0;
for (gsize s = 0; s != groups_size && max != 0; ++s) {
if (basepath != NULL && strncmp(groups[s], basepath, basepath_len) != 0) {
continue;
}
for (gsize s = 0; s != groups_size; ++s) {
girara_list_append(result, g_strdup(groups[s]));
--max;
}
g_strfreev(groups);

View file

@ -30,7 +30,7 @@ static bool sqlite_get_fileinfo(zathura_database_t* db, const char* fi
static void sqlite_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec);
static void sqlite_io_append(GiraraInputHistoryIO* db, const char*);
static girara_list_t* sqlite_io_read(GiraraInputHistoryIO* db);
static girara_list_t* sqlite_get_recent_files(zathura_database_t* db, int max);
static girara_list_t* sqlite_get_recent_files(zathura_database_t* db, int max, const char* basepath);
typedef struct zathura_sqldatabase_private_s {
sqlite3* session;
@ -761,13 +761,15 @@ sqlite_io_read(GiraraInputHistoryIO* db)
}
static girara_list_t*
sqlite_get_recent_files(zathura_database_t* db, int max)
sqlite_get_recent_files(zathura_database_t* db, int max, const char* basepath)
{
static const char SQL_HISTORY_GET[] =
"SELECT file FROM fileinfo ORDER BY time DESC LIMIT ?";
static const char SQL_HISTORY_GET_WITH_BASEPATH[] =
"SELECT file FROM fileinfo WHERE file LIKE '?%' ORDER BY time DESC LIMIT ?";
zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db);
sqlite3_stmt* stmt = prepare_statement(priv->session, SQL_HISTORY_GET);
sqlite3_stmt* stmt = prepare_statement(priv->session, basepath == NULL ? SQL_HISTORY_GET : SQL_HISTORY_GET_WITH_BASEPATH);
if (stmt == NULL) {
return NULL;
}
@ -776,7 +778,8 @@ sqlite_get_recent_files(zathura_database_t* db, int max)
max = INT_MAX;
}
if (sqlite3_bind_int(stmt, 1, max) != SQLITE_OK) {
if (sqlite3_bind_int(stmt, 1, max) != SQLITE_OK &&
(basepath == NULL || sqlite3_bind_text(stmt, 2, basepath, -1, NULL) != SQLITE_OK)) {
sqlite3_finalize(stmt);
girara_error("Failed to bind arguments.");
return false;

View file

@ -70,9 +70,9 @@ zathura_db_get_fileinfo(zathura_database_t* db, const char* file,
}
girara_list_t*
zathura_db_get_recent_files(zathura_database_t* db, int max)
zathura_db_get_recent_files(zathura_database_t* db, int max, const char* basepath)
{
g_return_val_if_fail(ZATHURA_IS_DATABASE(db), NULL);
return ZATHURA_DATABASE_GET_INTERFACE(db)->get_recent_files(db, max);
return ZATHURA_DATABASE_GET_INTERFACE(db)->get_recent_files(db, max, basepath);
}

View file

@ -51,7 +51,7 @@ struct _ZathuraDatabaseInterface
bool (*get_fileinfo)(ZathuraDatabase* db, const char* file, zathura_fileinfo_t* file_info);
girara_list_t* (*get_recent_files)(ZathuraDatabase* db, int max);
girara_list_t* (*get_recent_files)(ZathuraDatabase* db, int max, const char* basepath);
};
GType zathura_database_get_type(void) G_GNUC_CONST;
@ -138,7 +138,7 @@ bool zathura_db_get_fileinfo(zathura_database_t* db, const char* file,
* limit is applied.
* @return list of files
*/
girara_list_t* zathura_db_get_recent_files(zathura_database_t* db, int max);
girara_list_t* zathura_db_get_recent_files(zathura_database_t* db, int max, const char* basepath);
#endif // DATABASE_H