@@ -111,162 +111,6 @@ Java_at_tomtasche_reader_background_CoreWrapper_mimetypeNative(JNIEnv *env, jcla
111111 return mimetype;
112112}
113113
114- JNIEXPORT jobject JNICALL
115- Java_at_tomtasche_reader_background_CoreWrapper_parseNative (JNIEnv *env, jclass clazz,
116- jobject options) {
117- std::error_code ec;
118- auto logger = std::make_shared<AndroidLogger>();
119-
120- jclass resultClass = env->FindClass (" at/tomtasche/reader/background/CoreWrapper$CoreResult" );
121- jmethodID resultConstructor = env->GetMethodID (resultClass, " <init>" , " ()V" );
122- jobject result = env->NewObject (resultClass, resultConstructor);
123-
124- jfieldID errorField = env->GetFieldID (resultClass, " errorCode" , " I" );
125-
126- jclass optionsClass = env->GetObjectClass (options);
127- std::string inputPathCpp = getStringField (env, optionsClass, options, " inputPath" );
128-
129- try {
130- std::optional<std::string> passwordCpp;
131- jfieldID passwordField = env->GetFieldID (optionsClass, " password" , " Ljava/lang/String;" );
132- auto password = (jstring) env->GetObjectField (options, passwordField);
133- if (password != nullptr ) {
134- passwordCpp = convertString (env, password);
135- }
136-
137- jfieldID editableField = env->GetFieldID (optionsClass, " editable" , " Z" );
138- jboolean editable = env->GetBooleanField (options, editableField);
139-
140- std::string outputPathCpp = getStringField (env, optionsClass, options, " outputPath" );
141- std::string cachePathCpp = getStringField (env, optionsClass, options, " cachePath" );
142-
143- jclass listClass = env->FindClass (" java/util/List" );
144- jmethodID addMethod = env->GetMethodID (listClass, " add" , " (Ljava/lang/Object;)Z" );
145-
146- jfieldID pageNamesField = env->GetFieldID (resultClass, " pageNames" , " Ljava/util/List;" );
147- auto pageNames = (jobject) env->GetObjectField (result, pageNamesField);
148-
149- jfieldID pagePathsField = env->GetFieldID (resultClass, " pagePaths" , " Ljava/util/List;" );
150- auto pagePaths = (jobject) env->GetObjectField (result, pagePathsField);
151-
152- jfieldID ooxmlField = env->GetFieldID (optionsClass, " ooxml" , " Z" );
153- jboolean ooxml = env->GetBooleanField (options, ooxmlField);
154-
155- jfieldID txtField = env->GetFieldID (optionsClass, " txt" , " Z" );
156- jboolean txt = env->GetBooleanField (options, txtField);
157-
158- jfieldID pdfField = env->GetFieldID (optionsClass, " pdf" , " Z" );
159- jboolean pdf = env->GetBooleanField (options, pdfField);
160-
161- jfieldID pagingField = env->GetFieldID (optionsClass, " paging" , " Z" );
162- jboolean paging = env->GetBooleanField (options, pagingField);
163-
164- try {
165- odr::FileType fileType;
166- try {
167- const auto types = odr::list_file_types (inputPathCpp, *logger);
168- if (types.empty ()) {
169- env->SetIntField (result, errorField, -5 );
170- return result;
171- }
172-
173- fileType = types.back ();
174- } catch (odr::UnsupportedFileType &e) {
175- fileType = e.file_type ;
176- }
177-
178- std::string extensionCpp = odr::file_type_to_string (fileType);
179- jstring extension = env->NewStringUTF (extensionCpp.c_str ());
180- jfieldID extensionField = env->GetFieldID (resultClass, " extension" ,
181- " Ljava/lang/String;" );
182- env->SetObjectField (result, extensionField, extension);
183-
184- __android_log_print (ANDROID_LOG_VERBOSE, " smn" , " Open %s" , inputPathCpp.c_str ());
185-
186- auto file = odr::open (inputPathCpp, *logger);
187-
188- if (file.password_encrypted ()) {
189- if (!passwordCpp.has_value ()) {
190- env->SetIntField (result, errorField, -2 );
191- return result;
192- }
193- try {
194- file = file.decrypt (passwordCpp.value ());
195- } catch (...) {
196- env->SetIntField (result, errorField, -2 );
197- return result;
198- }
199- }
200-
201- // .doc-files are not real documents in core
202- if (file.is_document_file () && fileType != odr::FileType::legacy_word_document) {
203- // TODO this will cause a second load
204- s_document = file.as_document_file ().document ();
205- }
206-
207- extensionCpp = odr::file_type_to_string (file.file_type ());
208- extension = env->NewStringUTF (extensionCpp.c_str ());
209- env->SetObjectField (result, extensionField, extension);
210-
211- odr::HtmlConfig htmlConfig;
212- htmlConfig.editable = editable;
213- htmlConfig.text_document_margin = paging;
214-
215- __android_log_print (ANDROID_LOG_VERBOSE, " smn" , " Translate to HTML" );
216-
217- std::filesystem::remove_all (cachePathCpp, ec);
218- std::filesystem::create_directories (cachePathCpp);
219- odr::HtmlService service = odr::html::translate (file, cachePathCpp, htmlConfig, logger);
220- odr::Html html = service.bring_offline (outputPathCpp);
221- std::filesystem::remove_all (cachePathCpp);
222-
223- for (const odr::HtmlPage &page: html.pages ()) {
224- // Filter out unwanted views based on document type
225- if (file.is_document_file () && (
226- (((file.as_document_file ().document_type () ==
227- odr::DocumentType::presentation) ||
228- (file.as_document_file ().document_type () ==
229- odr::DocumentType::drawing)) &&
230- (page.name != " document" )) ||
231- ((file.as_document_file ().document_type () ==
232- odr::DocumentType::spreadsheet) &&
233- (page.name == " document" )))) {
234- continue ;
235- }
236-
237- jstring pageName = env->NewStringUTF (page.name .c_str ());
238- env->CallBooleanMethod (pageNames, addMethod, pageName);
239-
240- jstring pagePath = env->NewStringUTF (page.path .c_str ());
241- env->CallBooleanMethod (pagePaths, addMethod, pagePath);
242- }
243- } catch (const odr::UnknownFileType &e) {
244- __android_log_print (ANDROID_LOG_ERROR, " smn" , " Unknown file type: %s" , e.what ());
245- env->SetIntField (result, errorField, -5 );
246- return result;
247- } catch (const odr::UnsupportedFileType &e) {
248- __android_log_print (ANDROID_LOG_ERROR, " smn" , " Unsupported file type: %s" , e.what ());
249- env->SetIntField (result, errorField, -5 );
250- return result;
251- } catch (const std::exception &e) {
252- __android_log_print (ANDROID_LOG_ERROR, " smn" , " Unhandled C++ exception: %s" , e.what ());
253- env->SetIntField (result, errorField, -4 );
254- return result;
255- } catch (...) {
256- __android_log_print (ANDROID_LOG_ERROR, " smn" ,
257- " Unhandled C++ exception without further information" );
258- env->SetIntField (result, errorField, -4 );
259- return result;
260- }
261- } catch (...) {
262- env->SetIntField (result, errorField, -3 );
263- return result;
264- }
265-
266- env->SetIntField (result, errorField, 0 );
267- return result;
268- }
269-
270114JNIEXPORT jobject JNICALL
271115Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative (JNIEnv *env, jclass clazz,
272116 jobject options,
0 commit comments