66#include < odr/odr.hpp>
77#include < odr/exceptions.hpp>
88#include < odr/http_server.hpp>
9+ #include < odr/global_params.hpp>
910
1011#include < android/log.h>
1112
1213#include < string>
1314#include < optional>
1415#include < filesystem>
1516
17+ namespace {
18+
19+ std::string convertString (JNIEnv *env, jstring string) {
20+ jboolean isCopy;
21+ const char * cstring = env->GetStringUTFChars (string, &isCopy);
22+ auto cppstring = std::string (cstring, env->GetStringUTFLength (string));
23+ env->ReleaseStringUTFChars (string, cstring);
24+ return cppstring;
25+ }
26+
27+ std::string getStringField (JNIEnv *env, jclass clazz, const char *name) {
28+ jfieldID field = env->GetFieldID (clazz, name, " Ljava/lang/String;" );
29+ auto string = (jstring) env->GetObjectField (clazz, field);
30+ return convertString (env, string);
31+ }
32+
33+ }
34+
1635std::optional<odr::Html> s_html;
1736
37+ JNIEXPORT void JNICALL
38+ Java_at_tomtasche_reader_background_CoreWrapper_setGlobalParams (JNIEnv *env, jclass clazz,
39+ jobject params) {
40+ jboolean isCopy;
41+
42+ jclass paramsClass = env->GetObjectClass (params);
43+
44+ std::string odrCoreDataPath = getStringField (env, paramsClass, " coreDataPath" );
45+ std::string fontconfigDataPath = getStringField (env, paramsClass, " fontconfigDataPath" );
46+ std::string popplerDataPath = getStringField (env, paramsClass, " popplerDataPath" );
47+ std::string pdf2htmlexDataPath = getStringField (env, paramsClass, " pdf2htmlexDataPath" );
48+
49+ odr::GlobalParams::set_odr_core_data_path (odrCoreDataPath);
50+ odr::GlobalParams::set_fontconfig_data_path (fontconfigDataPath);
51+ odr::GlobalParams::set_poppler_data_path (popplerDataPath);
52+ odr::GlobalParams::set_pdf2htmlex_data_path (pdf2htmlexDataPath);
53+ }
54+
1855JNIEXPORT jobject JNICALL
1956Java_at_tomtasche_reader_background_CoreWrapper_parseNative (JNIEnv *env, jclass clazz,
2057 jobject options) {
21- jboolean isCopy;
22-
2358 jclass resultClass = env->FindClass (" at/tomtasche/reader/background/CoreWrapper$CoreResult" );
2459 jmethodID resultConstructor = env->GetMethodID (resultClass, " <init>" , " ()V" );
2560 jobject result = env->NewObject (resultClass, resultConstructor);
2661
2762 jfieldID errorField = env->GetFieldID (resultClass, " errorCode" , " I" );
2863
2964 jclass optionsClass = env->GetObjectClass (options);
30- jfieldID inputPathField = env->GetFieldID (optionsClass, " inputPath" , " Ljava/lang/String;" );
31- auto inputPath = (jstring) env->GetObjectField (options, inputPathField);
32-
33- const auto inputPathC = env->GetStringUTFChars (inputPath, &isCopy);
34- auto inputPathCpp = std::string (inputPathC, env->GetStringUTFLength (inputPath));
35- env->ReleaseStringUTFChars (inputPath, inputPathC);
65+ std::string inputPathCpp = getStringField (env, optionsClass, " inputPath" );
3666
3767 try {
3868 std::optional<std::string> passwordCpp;
3969 jfieldID passwordField = env->GetFieldID (optionsClass, " password" , " Ljava/lang/String;" );
4070 auto password = (jstring) env->GetObjectField (options, passwordField);
4171 if (password != nullptr ) {
42- const auto passwordC = env->GetStringUTFChars (password, &isCopy);
43- passwordCpp = std::string (passwordC, env->GetStringUTFLength (password));
44- env->ReleaseStringUTFChars (password, passwordC);
72+ passwordCpp = convertString (env, password);
4573 }
4674
4775 jfieldID editableField = env->GetFieldID (optionsClass, " editable" , " Z" );
4876 jboolean editable = env->GetBooleanField (options, editableField);
4977
50- jfieldID outputPathField = env->GetFieldID (optionsClass, " outputPath" ,
51- " Ljava/lang/String;" );
52- auto outputPath = (jstring) env->GetObjectField (options, outputPathField);
53-
54- const auto outputPathC = env->GetStringUTFChars (outputPath, &isCopy);
55- auto outputPathCpp = std::string (outputPathC, env->GetStringUTFLength (outputPath));
56- env->ReleaseStringUTFChars (outputPath, outputPathC);
78+ std::string outputPathCpp = getStringField (env, optionsClass, " outputPath" );
5779
5880 jclass listClass = env->FindClass (" java/util/List" );
5981 jmethodID addMethod = env->GetMethodID (listClass, " add" , " (Ljava/lang/Object;)Z" );
@@ -122,20 +144,24 @@ Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass
122144 return result;
123145 }
124146
147+ if (file.is_document_file ()) {
148+ odr::DocumentFile document_file = file.document_file ();
149+ if (document_file.password_encrypted ()) {
150+ if (!passwordCpp.has_value () || !document_file.decrypt (passwordCpp.value ())) {
151+ env->SetIntField (result, errorField, -2 );
152+ return result;
153+ }
154+ }
155+ }
156+
125157 odr::HtmlConfig config;
126158 config.editable = editable;
127159
128160 if (paging) {
129161 config.text_document_margin = true ;
130162 }
131163
132- s_html = odr::html::translate (odr::File (inputPathCpp), outputPathCpp, config,
133- [&passwordCpp]() -> std::string {
134- if (passwordCpp.has_value ()) {
135- return passwordCpp.value ();
136- }
137- return " " ;
138- });
164+ s_html = odr::html::translate (file, outputPathCpp, config);
139165
140166 {
141167 const auto extensionCpp = odr::type_to_string (
@@ -158,9 +184,6 @@ Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass
158184 } catch (odr::UnknownFileType &) {
159185 env->SetIntField (result, errorField, -5 );
160186 return result;
161- } catch (odr::WrongPassword &) {
162- env->SetIntField (result, errorField, -2 );
163- return result;
164187 } catch (odr::UnsupportedFileType &) {
165188 env->SetIntField (result, errorField, -5 );
166189 return result;
@@ -181,8 +204,6 @@ JNIEXPORT jobject JNICALL
181204Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative (JNIEnv *env, jclass clazz,
182205 jobject options,
183206 jstring htmlDiff) {
184- jboolean isCopy;
185-
186207 jclass optionsClass = env->GetObjectClass (options);
187208
188209 jclass resultClass = env->FindClass (" at/tomtasche/reader/background/CoreWrapper$CoreResult" );
@@ -192,15 +213,9 @@ Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env,
192213 jfieldID errorField = env->GetFieldID (resultClass, " errorCode" , " I" );
193214
194215 try {
195- jfieldID outputPathPrefixField = env->GetFieldID (optionsClass, " outputPath" ,
196- " Ljava/lang/String;" );
197- jstring outputPathPrefix = (jstring) env->GetObjectField (options, outputPathPrefixField);
198-
199- const auto outputPathPrefixC = env->GetStringUTFChars (outputPathPrefix, &isCopy);
200- auto outputPathPrefixCpp = std::string (outputPathPrefixC,
201- env->GetStringUTFLength (outputPathPrefix));
202- env->ReleaseStringUTFChars (outputPathPrefix, outputPathPrefixC);
216+ std::string outputPathPrefixCpp = getStringField (env, optionsClass, " outputPath" );
203217
218+ jboolean isCopy;
204219 const auto htmlDiffC = env->GetStringUTFChars (htmlDiff, &isCopy);
205220
206221 const auto extension = odr::type_to_string (s_html->file_type ());
@@ -246,10 +261,8 @@ Java_at_tomtasche_reader_background_CoreWrapper_closeNative(JNIEnv *env, jclass
246261std::optional<odr::HttpServer> s_server;
247262
248263JNIEXPORT void JNICALL
249- Java_at_tomtasche_reader_background_CoreWrapper_createServerNative (JNIEnv *env, jclass clazz, jstring outputPath) {
250- const char * outputPathC = env->GetStringUTFChars (outputPath, nullptr );
251- std::string output_path = outputPathC;
252- env->ReleaseStringUTFChars (outputPath, outputPathC);
264+ Java_at_tomtasche_reader_background_CoreWrapper_createServer (JNIEnv *env, jclass clazz, jstring outputPath) {
265+ std::string output_path = convertString (env, outputPath);
253266
254267 std::filesystem::create_directories (output_path);
255268
@@ -258,17 +271,14 @@ Java_at_tomtasche_reader_background_CoreWrapper_createServerNative(JNIEnv *env,
258271 s_server = odr::HttpServer (config);
259272}
260273
261- JNIEXPORT jstring JNICALL
262- Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative (JNIEnv *env, jclass clazz, jobject options) {
274+ JNIEXPORT void JNICALL
275+ Java_at_tomtasche_reader_background_CoreWrapper_hostFile (JNIEnv *env, jclass clazz, jstring prefix , jobject options) {
263276 jboolean isCopy;
264277
265278 jclass optionsClass = env->GetObjectClass (options);
266- jfieldID inputPathField = env->GetFieldID (optionsClass, " inputPath" , " Ljava/lang/String;" );
267- auto inputPath = (jstring) env->GetObjectField (options, inputPathField);
268279
269- const auto inputPathC = env->GetStringUTFChars (inputPath, &isCopy);
270- auto inputPathCpp = std::string (inputPathC, env->GetStringUTFLength (inputPath));
271- env->ReleaseStringUTFChars (inputPath, inputPathC);
280+ std::string inputPathCpp = getStringField (env, optionsClass, " inputPath" );
281+ std::string prefixCpp = convertString (env, prefix);
272282
273283 odr::DecodePreference decodePreference;
274284decodePreference.engine_priority = {
@@ -278,22 +288,19 @@ decodePreference.engine_priority = {
278288 __android_log_print (ANDROID_LOG_INFO, " smn" , " file type %i" , file.file_type ());
279289
280290 try {
281- std::string prefix = " hi" ;
282- s_server->serve_file (file, prefix, odr::HtmlConfig ());
283- return env->NewStringUTF (prefix.c_str ());
291+ s_server->serve_file (file, prefixCpp, odr::HtmlConfig ());
284292 } catch (...) {
285293 __android_log_print (ANDROID_LOG_ERROR, " smn" , " error" );
286- return env->NewStringUTF (" error" );
287294 }
288295}
289296
290297JNIEXPORT void JNICALL
291- Java_at_tomtasche_reader_background_CoreWrapper_listenServerNative (JNIEnv *env, jclass clazz) {
292- s_server->listen (" 127.0.0.1" , 29665 );
298+ Java_at_tomtasche_reader_background_CoreWrapper_listenServer (JNIEnv *env, jclass clazz, jint port ) {
299+ s_server->listen (" 127.0.0.1" , port );
293300}
294301
295302JNIEXPORT void JNICALL
296- Java_at_tomtasche_reader_background_CoreWrapper_stopServerNative (JNIEnv *env, jclass clazz) {
303+ Java_at_tomtasche_reader_background_CoreWrapper_stopServer (JNIEnv *env, jclass clazz) {
297304 s_server->stop ();
298305 s_server.reset ();
299306}
0 commit comments