Make show-recent both an option to show recent files and the maximum number to be shown

Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
This commit is contained in:
Sebastian Ramacher 2015-07-26 23:19:22 +02:00
parent 4e5ae52bc5
commit 4f3b1b257b
7 changed files with 41 additions and 21 deletions

View file

@ -837,10 +837,12 @@ Defines if hidden files and directories should be displayed in completion.
show-recent
^^^^^^^^^^^
Defines if recent files should be displayed in completion.
Defines the number of recent files that should be displayed in completion.
If the value is negative, no upper bounds are applied. If the value is 0, no
recent files are shown.
* Value type: Boolean
* Default value: true
* Value type: Integer
* Default value: 10
scroll-page-aware
^^^^^^^^^^^^^^^^^

View file

@ -114,7 +114,7 @@ error_free:
}
static girara_completion_t*
list_files_for_cc(zathura_t* zathura, const char* input, bool check_file_ext, bool include_history)
list_files_for_cc(zathura_t* zathura, const char* input, bool check_file_ext, int show_recent)
{
girara_completion_t* completion = girara_completion_init();
girara_completion_group_t* group = girara_completion_group_create(zathura->ui.session, "files");
@ -123,11 +123,11 @@ list_files_for_cc(zathura_t* zathura, const char* input, bool check_file_ext, bo
gchar* path = NULL;
gchar* current_path = NULL;
if (include_history == true) {
if (show_recent > 0) {
history_group = girara_completion_group_create(zathura->ui.session, "recent files");
}
if (completion == NULL || group == NULL || (include_history == true && history_group == NULL)) {
if (completion == NULL || group == NULL || (show_recent > 0 && history_group == NULL)) {
goto error_free;
}
@ -190,8 +190,8 @@ list_files_for_cc(zathura_t* zathura, const char* input, bool check_file_ext, bo
girara_list_free(names);
}
if (include_history == true) {
girara_list_t* recent_files = zathura_db_get_recent_files(zathura->database);
if (show_recent > 0) {
girara_list_t* recent_files = zathura_db_get_recent_files(zathura->database, show_recent);
if (recent_files == NULL) {
goto error_free;
}
@ -208,8 +208,10 @@ list_files_for_cc(zathura_t* zathura, const char* input, bool check_file_ext, bo
g_free(path);
g_free(current_path);
if (history_group != NULL) {
girara_completion_add_group(completion, history_group);
}
girara_completion_add_group(completion, group);
girara_completion_add_group(completion, history_group);
return completion;
@ -238,7 +240,7 @@ cc_open(girara_session_t* session, const char* input)
g_return_val_if_fail(session->global.data != NULL, NULL);
zathura_t* zathura = session->global.data;
bool show_recent = true;
int show_recent = 0;
girara_setting_get(zathura->ui.session, "show-recent", &show_recent);
return list_files_for_cc(zathura, input, true, show_recent);

View file

@ -212,8 +212,8 @@ config_load_default(zathura_t* zathura)
girara_setting_add(gsession, "show-hidden", &bool_value, BOOLEAN, false, _("Show hidden files and directories"), NULL, NULL);
bool_value = true;
girara_setting_add(gsession, "show-directories", &bool_value, BOOLEAN, false, _("Show directories"), NULL, NULL);
bool_value = true;
girara_setting_add(gsession, "show-recent", &bool_value, BOOLEAN, false, _("Show recent files"), NULL, NULL);
int_value = 10;
girara_setting_add(gsession, "show-recent", &int_value, INT, false, _("Show recent files"), NULL, NULL);
bool_value = false;
girara_setting_add(gsession, "open-first-page", &bool_value, BOOLEAN, false, _("Always open on first page"), NULL, NULL);
bool_value = false;

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);
static girara_list_t* plain_get_recent_files(zathura_database_t* db, int max);
/* forward declaration */
static bool zathura_db_check_file(const char* path);
@ -853,7 +853,7 @@ compare_time(const void* l, const void* r, void* data)
}
static girara_list_t*
plain_get_recent_files(zathura_database_t* db)
plain_get_recent_files(zathura_database_t* db, int max)
{
zathura_plaindatabase_private_t* priv = ZATHURA_PLAINDATABASE_GET_PRIVATE(db);
@ -869,6 +869,10 @@ plain_get_recent_files(zathura_database_t* db)
g_qsort_with_data(groups, groups_size, sizeof(gchar*), compare_time, priv->history);
}
if (max >= 0 && (gsize) max < groups_size) {
groups_size = max;
}
for (gsize s = 0; s != groups_size; ++s) {
girara_list_append(result, groups[s]);
}

View file

@ -28,7 +28,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);
static girara_list_t* sqlite_get_recent_files(zathura_database_t* db, int max);
typedef struct zathura_sqldatabase_private_s {
sqlite3* session;
@ -694,10 +694,10 @@ sqlite_io_read(GiraraInputHistoryIO* db)
}
static girara_list_t*
sqlite_get_recent_files(zathura_database_t* db)
sqlite_get_recent_files(zathura_database_t* db, int max)
{
static const char SQL_HISTORY_GET[] =
"SELECT file FROM fileinfo ORDER BY time DESC";
"SELECT file FROM fileinfo 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);
@ -705,6 +705,16 @@ sqlite_get_recent_files(zathura_database_t* db)
return NULL;
}
if (max < 0) {
max = INT_MAX;
}
if (sqlite3_bind_int(stmt, 1, max) != SQLITE_OK) {
sqlite3_finalize(stmt);
girara_error("Failed to bind arguments.");
return false;
}
girara_list_t* list = girara_list_new2((girara_free_function_t) g_free);
if (list == NULL) {
sqlite3_finalize(stmt);

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)
zathura_db_get_recent_files(zathura_database_t* db, int max)
{
g_return_val_if_fail(ZATHURA_IS_DATABASE(db), NULL);
return ZATHURA_DATABASE_GET_INTERFACE(db)->get_recent_files(db);
return ZATHURA_DATABASE_GET_INTERFACE(db)->get_recent_files(db, max);
}

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);
girara_list_t* (*get_recent_files)(ZathuraDatabase* db, int max);
};
GType zathura_database_get_type(void);
@ -134,9 +134,11 @@ bool zathura_db_get_fileinfo(zathura_database_t* db, const char* file,
* first.
*
* @param db The database instance
* @param max The maximum number of recent files. If max is less than zero, now
* limit is applied.
* @return list of files
*/
girara_list_t* zathura_db_get_recent_files(zathura_database_t* db);
girara_list_t* zathura_db_get_recent_files(zathura_database_t* db, int max);
#endif // DATABASE_H