Skip to content

Commit 8ff096d

Browse files
committed
handle presentations and spreadsheets
1 parent a79d6f8 commit 8ff096d

5 files changed

Lines changed: 69 additions & 34 deletions

File tree

app/conanfile.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[requires]
2-
odrcore/5.0.0-pre4
2+
odrcore/5.0.0-pre5
33

44
[generators]
55
CMakeToolchain

app/src/main/cpp/CoreWrapper.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,25 @@ Java_at_tomtasche_reader_background_CoreWrapper_createServer(JNIEnv *env, jclass
276276
s_server = odr::HttpServer(config);
277277
}
278278

279-
JNIEXPORT void JNICALL
279+
JNIEXPORT jobject JNICALL
280280
Java_at_tomtasche_reader_background_CoreWrapper_hostFile(JNIEnv *env, jclass clazz, jstring prefix, jobject options) {
281281
__android_log_print(ANDROID_LOG_INFO, "smn", "host file");
282282

283+
jclass resultClass = env->FindClass("at/tomtasche/reader/background/CoreWrapper$CoreResult");
284+
jmethodID resultConstructor = env->GetMethodID(resultClass, "<init>", "()V");
285+
jobject result = env->NewObject(resultClass, resultConstructor);
286+
287+
jfieldID errorField = env->GetFieldID(resultClass, "errorCode", "I");
288+
289+
jclass listClass = env->FindClass("java/util/List");
290+
jmethodID addMethod = env->GetMethodID(listClass, "add", "(Ljava/lang/Object;)Z");
291+
292+
jfieldID pageNamesField = env->GetFieldID(resultClass, "pageNames", "Ljava/util/List;");
293+
auto pageNames = (jobject) env->GetObjectField(result, pageNamesField);
294+
295+
jfieldID pagePathsField = env->GetFieldID(resultClass, "pagePaths", "Ljava/util/List;");
296+
auto pagePaths = (jobject) env->GetObjectField(result, pagePathsField);
297+
283298
std::string inputPathCpp = getStringField(env, options, "inputPath");
284299
std::string prefixCpp = convertString(env, prefix);
285300

@@ -292,12 +307,29 @@ Java_at_tomtasche_reader_background_CoreWrapper_hostFile(JNIEnv *env, jclass cla
292307
htmlConfig.embed_shipped_resources = false;
293308

294309
try {
295-
s_server->serve_file(file, prefixCpp, odr::HtmlConfig());
310+
odr::HtmlViews htmlViews = s_server->serve_file(file, prefixCpp, odr::HtmlConfig());
311+
312+
for (const auto &view: htmlViews) {
313+
if (file.is_document_file() &&
314+
file.document_file().document_type() != odr::DocumentType::text &&
315+
view.name() == "document") {
316+
continue;
317+
}
318+
319+
jstring pageName = env->NewStringUTF(view.name().c_str());
320+
env->CallBooleanMethod(pageNames, addMethod, pageName);
321+
322+
std::string pagePathCpp = "http://localhost:29665/file/" + prefixCpp + "/" + view.path();
323+
jstring pagePath = env->NewStringUTF(pagePathCpp.c_str());
324+
env->CallBooleanMethod(pagePaths, addMethod, pagePath);
325+
}
296326
} catch (const std::exception &e) {
297327
__android_log_print(ANDROID_LOG_ERROR, "smn", "Unhandled C++ exception: %s", e.what());
298328
} catch (...) {
299329
__android_log_print(ANDROID_LOG_ERROR, "smn", "Unhandled C++ exception without further information");
300330
}
331+
332+
return result;
301333
}
302334

303335
JNIEXPORT void JNICALL

app/src/main/cpp/CoreWrapper.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extern "C" {
2121
JNIEXPORT void JNICALL
2222
Java_at_tomtasche_reader_background_CoreWrapper_createServer(JNIEnv *env, jclass clazz, jstring outputPath);
2323

24-
JNIEXPORT void JNICALL
24+
JNIEXPORT jobject JNICALL
2525
Java_at_tomtasche_reader_background_CoreWrapper_hostFile(JNIEnv *env, jclass clazz, jstring prefix, jobject options);
2626

2727
JNIEXPORT void JNICALL

app/src/main/java/at/tomtasche/reader/background/CoreLoader.java

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -122,41 +122,44 @@ private void translate(Options options, Result result) throws Exception {
122122
lastCoreOptions = coreOptions;
123123

124124
if (doHttp) {
125-
String prefix = "odr";
126-
CoreWrapper.hostFile(prefix, coreOptions);
125+
CoreWrapper.CoreResult coreResult = CoreWrapper.hostFile("odr", coreOptions);
127126

128-
result.partTitles.add("document");
129-
result.partUris.add(Uri.parse("http://localhost:29665/file/" + prefix + "/document.html"));
130-
131-
return;
132-
}
133-
134-
CoreWrapper.CoreResult coreResult = lastCore.parse(coreOptions);
135-
136-
String coreExtension = coreResult.extension;
137-
if (coreResult.exception == null && "pdf".equals(coreExtension)) {
138-
// some PDFs do not cause an error in the core
139-
// https://github.com/opendocument-app/OpenDocument.droid/issues/348#issuecomment-2446888981
140-
throw new CoreWrapper.CoreCouldNotTranslateException();
141-
} else if (!"unnamed".equals(coreExtension)) {
142-
// "unnamed" refers to default of Meta::typeToString
143-
options.fileExtension = coreExtension;
127+
if (coreResult.exception != null) {
128+
throw coreResult.exception;
129+
}
144130

145-
String fileType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(coreExtension);
146-
if (fileType != null) {
147-
options.fileType = fileType;
131+
for (int i = 0; i < coreResult.pagePaths.size(); i++) {
132+
result.partTitles.add(coreResult.pageNames.get(i));
133+
result.partUris.add(Uri.parse(coreResult.pagePaths.get(i)));
134+
}
135+
} else {
136+
CoreWrapper.CoreResult coreResult = CoreWrapper.parse(coreOptions);
137+
138+
String coreExtension = coreResult.extension;
139+
if (coreResult.exception == null && "pdf".equals(coreExtension)) {
140+
// some PDFs do not cause an error in the core
141+
// https://github.com/opendocument-app/OpenDocument.droid/issues/348#issuecomment-2446888981
142+
throw new CoreWrapper.CoreCouldNotTranslateException();
143+
} else if (!"unnamed".equals(coreExtension)) {
144+
// "unnamed" refers to default of Meta::typeToString
145+
options.fileExtension = coreExtension;
146+
147+
String fileType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(coreExtension);
148+
if (fileType != null) {
149+
options.fileType = fileType;
150+
}
148151
}
149-
}
150152

151-
if (coreResult.exception != null) {
152-
throw coreResult.exception;
153-
}
153+
if (coreResult.exception != null) {
154+
throw coreResult.exception;
155+
}
154156

155-
for (int i = 0; i < coreResult.pagePaths.size(); i++) {
156-
File entryFile = new File(coreResult.pagePaths.get(i));
157+
for (int i = 0; i < coreResult.pagePaths.size(); i++) {
158+
File entryFile = new File(coreResult.pagePaths.get(i));
157159

158-
result.partTitles.add(coreResult.pageNames.get(i));
159-
result.partUris.add(Uri.fromFile(entryFile));
160+
result.partTitles.add(coreResult.pageNames.get(i));
161+
result.partUris.add(Uri.fromFile(entryFile));
162+
}
160163
}
161164
}
162165

app/src/main/java/at/tomtasche/reader/background/CoreWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public static void close() {
132132

133133
public static native void createServer(String outputPath);
134134

135-
public static native void hostFile(String prefix, CoreOptions options);
135+
public static native CoreResult hostFile(String prefix, CoreOptions options);
136136

137137
public static native void listenServer(int port);
138138

0 commit comments

Comments
 (0)