From caa058e4d243bdd7814248d7732a5b2c0bf16333 Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Wed, 25 May 2011 00:54:16 +0200 Subject: [PATCH] cc_open: List only supported file types --- completion.c | 27 +++++++++++++++++++++++++-- utils.c | 25 +++++++++++++++++++++++++ utils.h | 10 ++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/completion.c b/completion.c index 4936767..944d79c 100644 --- a/completion.c +++ b/completion.c @@ -67,10 +67,14 @@ cc_print(girara_session_t* session, char* input) girara_completion_t* cc_open(girara_session_t* session, char* input) { + g_return_val_if_fail(session != NULL, NULL); + g_return_val_if_fail(session->global.data != NULL, NULL); + zathura_t* zathura = session->global.data; + girara_completion_t* completion = girara_completion_init(); girara_completion_group_t* group = girara_completion_group_create(session, NULL); - if (!session || !input || !completion || !group) { + if (!input || !completion || !group) { goto error_free; } @@ -113,11 +117,30 @@ cc_open(girara_session_t* session, char* input) /* read files */ char* name = NULL; while ((name = (char*) g_dir_read_name(dir)) != NULL) { - char* e_name = g_filename_display_name(name); + char* e_name = g_filename_display_name(name); + if (e_name == NULL) { + goto error_free; + } + + char* full_path = g_strdup_printf("%s/%s", path, e_name); + if (full_path == NULL) { + goto error_free; + } + + if (file_valid_extension(zathura, full_path) == true) { + girara_completion_group_add_element(group, full_path, NULL); + } + + g_free(full_path); g_free(e_name); } g_dir_close(dir); + /* given path is a file */ + } else if (g_file_test(path, G_FILE_TEST_IS_REGULAR) == TRUE) { + if (file_valid_extension(zathura, path) == true) { + girara_completion_group_add_element(group, path, NULL); + } } g_free(path); diff --git a/utils.c b/utils.c index 63ba736..dd37079 100644 --- a/utils.c +++ b/utils.c @@ -48,6 +48,31 @@ file_get_extension(const char* path) return path + i + 1; } +bool +file_valid_extension(zathura_t* zathura, const char* path) +{ + if (path == NULL) { + return false; + } + + const char* file_extension = file_get_extension(path); + + girara_list_iterator_t* iter = girara_list_iterator(zathura->plugins.plugins); + if (iter == NULL) { + return false; + } + + do { + zathura_document_plugin_t* plugin = (zathura_document_plugin_t*) girara_list_iterator_data(iter); + if (!strcmp(file_extension, plugin->file_extension)) { + return true; + } + } while (girara_list_iterator_next(iter)); + girara_list_iterator_free(iter); + + return false; +} + bool execute_command(char* const argv[], char** output) { diff --git a/utils.h b/utils.h index 8c10724..62db090 100644 --- a/utils.h +++ b/utils.h @@ -31,6 +31,16 @@ bool file_exists(const char* path); */ const char* file_get_extension(const char* path); +/** + * This function checks if the file has a valid extension. A extension is + * evaluated as valid if it matches a supported filetype. + * + * @param zathura Zathura object + * @param path The path to the file + * @return true if the extension is valid, otherwise false + */ +bool file_valid_extension(zathura_t* zathura, const char* path); + /** * Executes a system command and saves its output into output *