mirror of
https://git.pwmt.org/pwmt/zathura.git
synced 2025-01-16 05:35:51 +01:00
Register document plugin and free registered plugins
This commit is contained in:
parent
ef3944421b
commit
7629a30a27
9 changed files with 83 additions and 21 deletions
2
Makefile
2
Makefile
|
@ -4,7 +4,7 @@
|
|||
include config.mk
|
||||
|
||||
PROJECT = zathura
|
||||
SOURCE = $(shell find . -iname "*.c" -a ! -iwholename "*./doc*")
|
||||
SOURCE = $(shell find . -iname "*.c" -a ! -iwholename "*./doc*|*./ft*")
|
||||
OBJECTS = $(patsubst %.c, %.o, $(SOURCE))
|
||||
DOBJECTS = $(patsubst %.c, %.do, $(SOURCE))
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
#include "zathura.h"
|
||||
#include "render.h"
|
||||
#include "ft/document.h"
|
||||
#include "document.h"
|
||||
|
||||
gboolean
|
||||
cb_destroy(GtkWidget* widget, gpointer data)
|
||||
|
@ -18,6 +18,9 @@ cb_destroy(GtkWidget* widget, gpointer data)
|
|||
|
||||
document_close();
|
||||
|
||||
/* free registered plugins */
|
||||
zathura_document_plugin_free();
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
|
|
@ -10,17 +10,70 @@
|
|||
#include <limits.h>
|
||||
|
||||
#include "document.h"
|
||||
#include "pdf/pdf.h"
|
||||
#include "djvu/djvu.h"
|
||||
#include "../utils.h"
|
||||
#include "../zathura.h"
|
||||
#include "utils.h"
|
||||
#include "zathura.h"
|
||||
|
||||
#define LENGTH(x) (sizeof(x)/sizeof((x)[0]))
|
||||
|
||||
zathura_document_plugin_t zathura_document_plugins[] = {
|
||||
{ "pdf", pdf_document_open },
|
||||
{ "djvu", djvu_document_open },
|
||||
};
|
||||
zathura_document_plugin_t* zathura_document_plugins = NULL;
|
||||
|
||||
bool
|
||||
zathura_document_register_plugin(char* file_extension, zathura_document_open_t open_function)
|
||||
{
|
||||
if( (file_extension == NULL) || (open_function == NULL) ) {
|
||||
fprintf(stderr, "plugin: could not register\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
/* search existing plugins */
|
||||
zathura_document_plugin_t* plugin = zathura_document_plugins;
|
||||
while (plugin) {
|
||||
if (!strcmp(plugin->file_extension, file_extension)) {
|
||||
fprintf(stderr, "plugin: already registered for filetype %s\n", file_extension);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (plugin->next == NULL) {
|
||||
break;
|
||||
}
|
||||
|
||||
plugin = plugin->next;
|
||||
}
|
||||
|
||||
/* create new plugin */
|
||||
zathura_document_plugin_t* new_plugin = malloc(sizeof(zathura_document_plugin_t));
|
||||
|
||||
if (new_plugin == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
new_plugin->file_extension = file_extension;
|
||||
new_plugin->open_function = open_function;
|
||||
new_plugin->next = NULL;
|
||||
|
||||
/* append to list */
|
||||
if (plugin == NULL) {
|
||||
zathura_document_plugins = new_plugin;
|
||||
} else {
|
||||
plugin->next = new_plugin;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
zathura_document_plugin_free(void)
|
||||
{
|
||||
/* free registered plugins */
|
||||
zathura_document_plugin_t* plugin = zathura_document_plugins;
|
||||
while (plugin) {
|
||||
zathura_document_plugin_t* tmp = plugin->next;
|
||||
free(plugin);
|
||||
plugin = tmp;
|
||||
}
|
||||
|
||||
zathura_document_plugins = NULL;
|
||||
}
|
||||
|
||||
zathura_document_t*
|
||||
zathura_document_open(const char* path, const char* password)
|
||||
|
@ -88,11 +141,11 @@ zathura_document_open(const char* path, const char* password)
|
|||
document->functions.page_render = NULL;
|
||||
|
||||
/* init plugin with associated file type */
|
||||
for (unsigned int i = 0; i < LENGTH(zathura_document_plugins); i++)
|
||||
{
|
||||
if (!strcmp(file_extension, zathura_document_plugins[i].file_extension)) {
|
||||
if (zathura_document_plugins[i].open_function) {
|
||||
if (zathura_document_plugins[i].open_function(document)) {
|
||||
zathura_document_plugin_t* plugin = zathura_document_plugins;
|
||||
while (plugin) {
|
||||
if (!strcmp(file_extension, plugin->file_extension)) {
|
||||
if (plugin->open_function) {
|
||||
if (plugin->open_function(document)) {
|
||||
/* update statusbar */
|
||||
girara_statusbar_item_set_text(Zathura.UI.session, Zathura.UI.statusbar.file, real_path);
|
||||
|
||||
|
@ -122,6 +175,8 @@ zathura_document_open(const char* path, const char* password)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
plugin = plugin->next;
|
||||
}
|
||||
|
||||
fprintf(stderr, "error: unknown file type\n");
|
|
@ -15,8 +15,9 @@ typedef bool (*zathura_document_open_t)(zathura_document_t* document);
|
|||
|
||||
typedef struct zathura_document_plugin_s
|
||||
{
|
||||
const char* file_extension;
|
||||
char* file_extension;
|
||||
zathura_document_open_t open_function;
|
||||
struct zathura_document_plugin_s *next;
|
||||
} zathura_document_plugin_t;
|
||||
|
||||
struct zathura_list_s
|
||||
|
@ -113,6 +114,9 @@ struct zathura_document_s
|
|||
zathura_page_t** pages;
|
||||
};
|
||||
|
||||
bool zathura_document_plugin_register(char* file_extension, zathura_document_open_t open_function);
|
||||
void zathura_document_plugin_free(void);
|
||||
|
||||
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);
|
|
@ -6,7 +6,7 @@
|
|||
#include <stdbool.h>
|
||||
#include <libdjvu/ddjvuapi.h>
|
||||
|
||||
#include "../document.h"
|
||||
#include "../../document.h"
|
||||
|
||||
typedef struct djvu_document_s
|
||||
{
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
#include <stdbool.h>
|
||||
#include <poppler.h>
|
||||
|
||||
#include "../document.h"
|
||||
#include "../../document.h"
|
||||
|
||||
typedef struct pdf_document_s
|
||||
{
|
||||
|
|
2
render.h
2
render.h
|
@ -7,7 +7,7 @@
|
|||
#include <stdlib.h>
|
||||
#include <girara-datastructures.h>
|
||||
|
||||
#include "ft/document.h"
|
||||
#include "document.h"
|
||||
#include "callbacks.h"
|
||||
|
||||
typedef struct render_thread_s
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
#include "callbacks.h"
|
||||
#include "config.h"
|
||||
#include "ft/document.h"
|
||||
#include "document.h"
|
||||
#include "shortcuts.h"
|
||||
#include "zathura.h"
|
||||
#include "utils.h"
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
#include <girara.h>
|
||||
#include <render.h>
|
||||
|
||||
#include "ft/document.h"
|
||||
#include "document.h"
|
||||
|
||||
enum { NEXT, PREVIOUS, LEFT, RIGHT, UP, DOWN, BOTTOM, TOP, HIDE, HIGHLIGHT,
|
||||
DELETE_LAST_WORD, DELETE_LAST_CHAR, DEFAULT, ERROR, WARNING, NEXT_GROUP,
|
||||
|
|
Loading…
Reference in a new issue