From 4857d62fdea0736970db0400c0268dc767ed85ae Mon Sep 17 00:00:00 2001 From: Moritz Lipp Date: Thu, 18 Nov 2010 02:35:33 +0100 Subject: [PATCH] Init document, some changes --- ft/document.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++-- ft/document.h | 26 +++++++++++------ ft/pdf/pdf.c | 9 ++++++ ft/pdf/pdf.h | 7 +++++ utils.c | 22 +++++++++++++++ utils.h | 5 ++++ zathura.c | 21 ++++++++++---- 7 files changed, 151 insertions(+), 17 deletions(-) diff --git a/ft/document.c b/ft/document.c index c9a899c..7f6b62f 100644 --- a/ft/document.c +++ b/ft/document.c @@ -4,10 +4,63 @@ #include #include "document.h" +#include "../utils.h" +#include "pdf/pdf.h" + +#define LENGTH(x) (sizeof(x)/sizeof((x)[0])) + +zathura_document_plugin_t zathura_document_plugins[] = { + { "pdf", pdf_document_open }, +}; zathura_document_t* -zathura_document_create(const char* path) +zathura_document_open(const char* path, const char* password) { + if(!path) { + return NULL; + } + + if(!file_exists(path)) { + fprintf(stderr, "error: file does not exist\n"); + return NULL; + } + + const char* file_extension = file_get_extension(path); + if(!file_extension) { + fprintf(stderr, "error: could not determine file type\n"); + return NULL; + } + + zathura_document_t* document = malloc(sizeof(zathura_document_t)); + if(!document) { + return NULL; + } + + + document->file_path = path; + document->password = password; + document->current_page_number = 0; + document->number_of_pages = 0; + document->scale = 100; + document->rotate = 0; + + document->functions.document_free = NULL; + document->functions.document_index_generate = NULL; + document->functions.document_save_as = NULL; + document->functions.document_attachments_get = NULL; + document->functions.page_get = NULL; + document->functions.page_free = NULL; + document->functions.page_search_text = NULL; + document->functions.page_links_get = NULL; + document->functions.page_form_fields_get = NULL; + document->functions.page_render = NULL; + + /* init plugin with associated file type */ + for(unsigned int i = 0; i < LENGTH(zathura_document_plugins); i++) + { + + } + return NULL; } @@ -23,7 +76,11 @@ zathura_document_free(zathura_document_t* document) return NULL; } - return document->functions.document_free(document); + bool r = document->functions.document_free(document); + + free(document); + + return r; } bool @@ -86,7 +143,7 @@ zathura_document_attachments_free(zathura_list_t* list) zathura_page_t* zathura_page_get(zathura_document_t* document, unsigned int page) { - if(!document) { + if(!document || !page) { return NULL; } @@ -98,6 +155,21 @@ zathura_page_get(zathura_document_t* document, unsigned int page) return document->functions.page_get(document, page); } +bool +zathura_page_free(zathura_page_t* page) +{ + if(!page || !page->document) { + return false; + } + + if(!page->document->functions.page_free) { + fprintf(stderr, "error: %s not implemented\n", __FUNCTION__); + return false; + } + + return page->document->functions.page_free(page); +} + zathura_list_t* zathura_page_search_text(zathura_page_t* page, const char* text) { diff --git a/ft/document.h b/ft/document.h index ada8507..28b18d9 100644 --- a/ft/document.h +++ b/ft/document.h @@ -9,6 +9,14 @@ typedef struct zathura_list_s zathura_list_t; typedef struct zathura_document_s zathura_document_t; +typedef bool (*zathura_document_open_t)(zathura_document_t* document); + +typedef struct zathura_document_plugin_s +{ + const char* file_type; + zathura_document_open_t open_function; +} zathura_document_plugin_t; + struct zathura_list_s { void* data; @@ -62,8 +70,8 @@ typedef struct zathura_page_s struct zathura_document_s { - char* file_path; - char* password; + const char* file_path; + const char* password; unsigned int current_page_number; unsigned int number_of_pages; int scale; @@ -71,20 +79,21 @@ struct zathura_document_s struct { + bool (*document_free)(zathura_document_t* document); + zathura_list_t* (*document_index_generate)(zathura_document_t* document); + bool (*document_save_as)(zathura_document_t* document, const char* path); + zathura_list_t* (*document_attachments_get)(zathura_document_t* document); + zathura_page_t* (*page_get)(zathura_document_t* document, unsigned int page); zathura_list_t* (*page_search_text)(zathura_page_t* page, const char* text); zathura_list_t* (*page_links_get)(zathura_page_t* page); zathura_list_t* (*page_form_fields_get)(zathura_page_t* page); cairo_surface_t* (*page_render)(zathura_page_t* page); - - zathura_list_t* (*document_index_generate)(zathura_document_t* document); - bool (*document_save_as)(zathura_document_t* document, const char* path); - zathura_list_t* (*document_attachments_get)(zathura_document_t* document); - bool (*document_free)(zathura_document_t* document); + bool (*page_free)(zathura_page_t* page); } functions; }; -zathura_document_t* zathura_document_create(const char* path); +zathura_document_t* zathura_document_open(const char* path, const char* password); bool zathura_document_free(zathura_document_t* document); bool zathura_document_save_as(zathura_document_t* document, const char* path); zathura_list_t* zathura_document_index_generate(zathura_document_t* document); @@ -93,6 +102,7 @@ zathura_list_t* zathura_document_attachments_get(zathura_document_t* document); bool zathura_document_attachments_free(zathura_list_t* list); zathura_page_t* zathura_page_get(zathura_document_t* document, unsigned int page); +bool zathura_page_free(zathura_page_t* page); zathura_list_t* zathura_page_search_text(zathura_page_t* page, const char* text); zathura_list_t* zathura_page_links_get(zathura_page_t* page); bool zathura_page_links_free(zathura_list_t* list); diff --git a/ft/pdf/pdf.c b/ft/pdf/pdf.c index e69de29..83bc422 100644 --- a/ft/pdf/pdf.c +++ b/ft/pdf/pdf.c @@ -0,0 +1,9 @@ +/* See LICENSE file for license and copyright information */ + +#include "pdf.h" + +bool +pdf_document_open(zathura_document_t* document) +{ + return true; +} diff --git a/ft/pdf/pdf.h b/ft/pdf/pdf.h index e69de29..e207dd2 100644 --- a/ft/pdf/pdf.h +++ b/ft/pdf/pdf.h @@ -0,0 +1,7 @@ +/* See LICENSE file for license and copyright information */ + +#include + +#include "../document.h" + +bool pdf_document_open(zathura_document_t* document); diff --git a/utils.c b/utils.c index e69de29..47d1690 100644 --- a/utils.c +++ b/utils.c @@ -0,0 +1,22 @@ +/* See LICENSE file for license and copyright information */ + +#include +#include + +#include "utils.h" + +bool +file_exists(const char* path) +{ + if(!access(path, F_OK)) { + return true; + } else { + return false; + } +} + +const char* +file_get_extension(const char* path) +{ + return NULL; +} diff --git a/utils.h b/utils.h index 931b644..bacc9b8 100644 --- a/utils.h +++ b/utils.h @@ -3,4 +3,9 @@ #ifndef UTILS_H #define UTILS_H +#include + +bool file_exists(const char* path); +const char* file_get_extension(const char* path); + #endif // UTILS_H diff --git a/zathura.c b/zathura.c index 586edb6..59bd0ed 100644 --- a/zathura.c +++ b/zathura.c @@ -2,6 +2,7 @@ #include "callbacks.h" #include "config.h" +#include "ft/document.h" #include "shortcuts.h" #include "zathura.h" @@ -43,14 +44,22 @@ int main(int argc, char* argv[]) gdk_threads_init(); gtk_init(&argc, &argv); - if(!init_zathura()) { - printf("error: coult not initialize zathura\n"); - return -1; + /*if(!init_zathura()) {*/ + /*printf("error: coult not initialize zathura\n");*/ + /*return -1;*/ + /*}*/ + + if(argc > 1) { + zathura_document_t* document = zathura_document_open(argv[1], NULL); + if(!document) { + return -1; + } + zathura_document_free(document); } - gdk_threads_enter(); - gtk_main(); - gdk_threads_leave(); + /*gdk_threads_enter();*/ + /*gtk_main();*/ + /*gdk_threads_leave();*/ return 0; }