55#include < odr/html.hpp>
66#include < odr/open_document_reader.hpp>
77#include < odr/exceptions.hpp>
8+ #include < odr/http_server.hpp>
89
910#include < android/log.h>
1011
1112#include < string>
1213#include < optional>
1314
14- std::optional<odr::Html> html ;
15+ std::optional<odr::Html> s_html ;
1516
1617JNIEXPORT jobject JNICALL
1718Java_at_tomtasche_reader_background_CoreWrapper_parseNative (JNIEnv *env, jobject instance,
@@ -119,7 +120,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jobject
119120 config.text_document_margin = true ;
120121 }
121122
122- html = odr::OpenDocumentReader::html (inputPathCpp, [&passwordCpp]() -> std::string {
123+ s_html = odr::OpenDocumentReader::html (inputPathCpp, [&passwordCpp]() -> std::string {
123124 if (passwordCpp.has_value ()) {
124125 return passwordCpp.value ();
125126 }
@@ -128,7 +129,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jobject
128129
129130 {
130131 const auto extensionCpp = odr::OpenDocumentReader::type_to_string (
131- html ->file_type ());
132+ s_html ->file_type ());
132133 const auto extensionC = extensionCpp.c_str ();
133134 jstring extension = env->NewStringUTF (extensionC);
134135
@@ -137,7 +138,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jobject
137138 env->SetObjectField (result, extensionField, extension);
138139 }
139140
140- for (auto &&page: html ->pages ()) {
141+ for (auto &&page: s_html ->pages ()) {
141142 jstring pageName = env->NewStringUTF (page.name .c_str ());
142143 env->CallBooleanMethod (pageNames, addMethod, pageName);
143144
@@ -192,7 +193,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env,
192193
193194 const auto htmlDiffC = env->GetStringUTFChars (htmlDiff, &isCopy);
194195
195- const auto extension = odr::OpenDocumentReader::type_to_string (html ->file_type ());
196+ const auto extension = odr::OpenDocumentReader::type_to_string (s_html ->file_type ());
196197 const auto outputPathCpp = outputPathPrefixCpp + " ." + extension;
197198 const char *outputPathC = outputPathCpp.c_str ();
198199 jstring outputPath = env->NewStringUTF (outputPathC);
@@ -201,7 +202,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env,
201202 env->SetObjectField (result, outputPathField, outputPath);
202203
203204 try {
204- html ->edit (htmlDiffC);
205+ s_html ->edit (htmlDiffC);
205206
206207 env->ReleaseStringUTFChars (htmlDiff, htmlDiffC);
207208 } catch (...) {
@@ -212,7 +213,7 @@ Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env,
212213 }
213214
214215 try {
215- html ->save (outputPathCpp);
216+ s_html ->save (outputPathCpp);
216217 } catch (...) {
217218 env->SetIntField (result, errorField, -7 );
218219 return result;
@@ -229,5 +230,46 @@ Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env,
229230JNIEXPORT void JNICALL
230231Java_at_tomtasche_reader_background_CoreWrapper_closeNative (JNIEnv *env, jobject instance,
231232 jobject options) {
232- html.reset ();
233+ s_html.reset ();
234+ }
235+
236+ std::optional<odr::HttpServer> s_server;
237+
238+ JNIEXPORT void JNICALL
239+ Java_at_tomtasche_reader_background_CoreWrapper_createServerNative (JNIEnv *env, jobject instance) {
240+ odr::HttpServer::Config config;
241+ s_server = odr::HttpServer (config);
242+ }
243+
244+ JNIEXPORT jstring JNICALL
245+ Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative (JNIEnv *env, jobject instance, jobject options) {
246+ jboolean isCopy;
247+
248+ jclass optionsClass = env->GetObjectClass (options);
249+ jfieldID inputPathField = env->GetFieldID (optionsClass, " inputPath" , " Ljava/lang/String;" );
250+ auto inputPath = (jstring) env->GetObjectField (options, inputPathField);
251+
252+ const auto inputPathC = env->GetStringUTFChars (inputPath, &isCopy);
253+ auto inputPathCpp = std::string (inputPathC, env->GetStringUTFLength (inputPath));
254+ env->ReleaseStringUTFChars (inputPath, inputPathC);
255+
256+ odr::DecodePreference decode_preference;
257+ decode_preference.engine_priority = {
258+ odr::DecoderEngine::poppler, odr::DecoderEngine::wvware, odr::DecoderEngine::odr};
259+ odr::DecodedFile file = odr::OpenDocumentReader::open (inputPathCpp, decode_preference);
260+
261+ std::string id = s_server->host_file (file);
262+
263+ return env->NewStringUTF (id.c_str ());
264+ }
265+
266+ JNIEXPORT void JNICALL
267+ Java_at_tomtasche_reader_background_CoreWrapper_listenServerNative (JNIEnv *env, jobject instance) {
268+ s_server->listen (" 0.0.0.0" , 8080 );
269+ }
270+
271+ JNIEXPORT void JNICALL
272+ Java_at_tomtasche_reader_background_CoreWrapper_stopServerNative (JNIEnv *env, jobject instance) {
273+ s_server->stop ();
274+ s_server.reset ();
233275}
0 commit comments