-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathepub.c
103 lines (82 loc) · 1.84 KB
/
epub.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/* See LICENSE file for license and copyright information */
#include <stdlib.h>
#include <signal.h>
#include <glib.h>
#include "epub.h"
static void
randomize_string(char* dest, size_t length)
{
char charset[] = "0123456789";
for (int i = 0; i < length; i++) {
size_t index = (double) rand() / RAND_MAX * (sizeof charset - 1);
dest[i] = charset[index];
}
}
static char*
create_tmp_filename() {
char* tmp_filename = g_strdup("/tmp/tmp-XXXXXX.pdf");
randomize_string(tmp_filename + 9, 6);
return tmp_filename;
}
void
register_functions(zathura_plugin_functions_t* functions)
{
functions->document_open = epub_document_open;
}
ZATHURA_PLUGIN_REGISTER(
"epub",
0, 1, 0,
register_functions,
ZATHURA_PLUGIN_MIMETYPES({
"application/epub+zip"
})
)
zathura_error_t
epub_document_open(zathura_document_t* document)
{
if (document == NULL) {
return ZATHURA_ERROR_UNKNOWN;
}
const char* path = zathura_document_get_path(document);
if (path == NULL) {
return ZATHURA_ERROR_UNKNOWN;
}
/* create temporary file */
char* tmp_filename = create_tmp_filename();
/* convert epub to pdf */
char* argv_convert[] = {
"ebook-convert",
(char*) path,
tmp_filename,
"--enable-heuristics",
NULL
};
gint exit_status = 0;
g_spawn_sync(
NULL,
argv_convert,
NULL,
G_SPAWN_SEARCH_PATH,
NULL,
NULL,
NULL,
NULL,
&exit_status,
NULL);
if (exit_status != 0) {
g_free(tmp_filename);
return ZATHURA_ERROR_UNKNOWN;
}
/* create new instance of zathura */
char* argv_zathura[] = {
"zathura",
tmp_filename,
NULL
};
g_spawn_async(NULL, argv_zathura, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL);
/* cleanup */
g_free(tmp_filename);
/* kill calling process */
raise(SIGKILL);
return ZATHURA_ERROR_UNKNOWN;
}