Skip to content

Commit 3601911

Browse files
authored
Use odrcore v5 (#88)
1 parent 9084b33 commit 3601911

File tree

13 files changed

+213
-54
lines changed

13 files changed

+213
-54
lines changed

.github/workflows/ios_main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ jobs:
5252
--output-folder=conan-output
5353
--build=missing
5454
--profile:host=conan/profiles/ios-simulator-arm64
55+
--deployer=conan/conandeployer.py
56+
--deployer-folder=conan-assets
5557
-s build_type=Release
5658
-s "&:build_type=RelWithDebInfo"
5759
-s "odrcore/*:build_type=RelWithDebInfo"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,5 @@ fastlane/test_output
7272
fastlane/report.xml
7373

7474
conan-output/
75+
conan-assets/
7576
graph_info.json

OpenDocumentReader.xcodeproj/project.pbxproj

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@
270270
F435F37405C0FE5E8503721A /* [CP] Embed Pods Frameworks */,
271271
E2C41D0F226DEF390049EF8E /* Run Script */,
272272
F8CA8E81987336E1DB902130 /* [CP] Copy Pods Resources */,
273+
E158977C2DF640E700319830 /* [CP] Copy Conan Resources */,
273274
);
274275
buildRules = (
275276
);
@@ -404,6 +405,26 @@
404405
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
405406
showEnvVarsInLog = 0;
406407
};
408+
E158977C2DF640E700319830 /* [CP] Copy Conan Resources */ = {
409+
isa = PBXShellScriptBuildPhase;
410+
buildActionMask = 2147483647;
411+
files = (
412+
);
413+
inputFileListPaths = (
414+
"${PROJECT_DIR}/conan-assets/input-files.xcfilelist",
415+
);
416+
inputPaths = (
417+
);
418+
name = "[CP] Copy Conan Resources";
419+
outputFileListPaths = (
420+
"${PROJECT_DIR}/conan-assets/output-files.xcfilelist",
421+
);
422+
outputPaths = (
423+
);
424+
runOnlyForDeploymentPostprocessing = 0;
425+
shellPath = /bin/sh;
426+
shellScript = "\"${PROJECT_DIR}/conan/copy-resources.sh\"\n";
427+
};
407428
E2C41D0F226DEF390049EF8E /* Run Script */ = {
408429
isa = PBXShellScriptBuildPhase;
409430
buildActionMask = 2147483647;
@@ -431,10 +452,14 @@
431452
inputFileListPaths = (
432453
"${PODS_ROOT}/Target Support Files/Pods-OpenDocumentReader/Pods-OpenDocumentReader-frameworks-${CONFIGURATION}-input-files.xcfilelist",
433454
);
455+
inputPaths = (
456+
);
434457
name = "[CP] Embed Pods Frameworks";
435458
outputFileListPaths = (
436459
"${PODS_ROOT}/Target Support Files/Pods-OpenDocumentReader/Pods-OpenDocumentReader-frameworks-${CONFIGURATION}-output-files.xcfilelist",
437460
);
461+
outputPaths = (
462+
);
438463
runOnlyForDeploymentPostprocessing = 0;
439464
shellPath = /bin/sh;
440465
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-OpenDocumentReader/Pods-OpenDocumentReader-frameworks.sh\"\n";
@@ -448,10 +473,14 @@
448473
inputFileListPaths = (
449474
"${PODS_ROOT}/Target Support Files/Pods-OpenDocumentReader/Pods-OpenDocumentReader-resources-${CONFIGURATION}-input-files.xcfilelist",
450475
);
476+
inputPaths = (
477+
);
451478
name = "[CP] Copy Pods Resources";
452479
outputFileListPaths = (
453480
"${PODS_ROOT}/Target Support Files/Pods-OpenDocumentReader/Pods-OpenDocumentReader-resources-${CONFIGURATION}-output-files.xcfilelist",
454481
);
482+
outputPaths = (
483+
);
455484
runOnlyForDeploymentPostprocessing = 0;
456485
shellPath = /bin/sh;
457486
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-OpenDocumentReader/Pods-OpenDocumentReader-resources.sh\"\n";
@@ -606,6 +635,7 @@
606635
CODE_SIGN_STYLE = Automatic;
607636
CURRENT_PROJECT_VERSION = 42;
608637
DEFINES_MODULE = YES;
638+
DEVELOPMENT_ASSET_PATHS = "conan-assets";
609639
DEVELOPMENT_TEAM = 5LS6X97G6J;
610640
ENABLE_BITCODE = NO;
611641
ENABLE_USER_SCRIPT_SANDBOXING = NO;
@@ -697,6 +727,7 @@
697727
CODE_SIGN_STYLE = Automatic;
698728
CURRENT_PROJECT_VERSION = 42;
699729
DEFINES_MODULE = YES;
730+
DEVELOPMENT_ASSET_PATHS = "conan-assets";
700731
DEVELOPMENT_TEAM = 5LS6X97G6J;
701732
ENABLE_BITCODE = NO;
702733
ENABLE_USER_SCRIPT_SANDBOXING = NO;
@@ -932,6 +963,7 @@
932963
CODE_SIGN_STYLE = Automatic;
933964
CURRENT_PROJECT_VERSION = 42;
934965
DEFINES_MODULE = YES;
966+
DEVELOPMENT_ASSET_PATHS = "conan-assets";
935967
DEVELOPMENT_TEAM = 5LS6X97G6J;
936968
ENABLE_BITCODE = NO;
937969
ENABLE_USER_SCRIPT_SANDBOXING = NO;
@@ -967,6 +999,7 @@
967999
CODE_SIGN_STYLE = Automatic;
9681000
CURRENT_PROJECT_VERSION = 42;
9691001
DEFINES_MODULE = YES;
1002+
DEVELOPMENT_ASSET_PATHS = "conan-assets";
9701003
DEVELOPMENT_TEAM = 5LS6X97G6J;
9711004
ENABLE_BITCODE = NO;
9721005
ENABLE_USER_SCRIPT_SANDBOXING = NO;

OpenDocumentReader/CoreWrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
@property NSArray *pagePaths;
1818
@property NSNumber *errorCode;
1919

20-
- (bool)translate:(NSString *)inputPath into:(NSString *)outputPath with:(NSString *)password editable:(bool)editable;
20+
- (bool)translate:(NSString *)inputPath cache:(NSString *)cachePath into:(NSString *)outputPath with:(NSString *)password editable:(bool)editable;
2121
- (bool)backTranslate:(NSString *)diff into:(NSString *)outputPath;
2222
@end
2323

OpenDocumentReader/CoreWrapper.mm

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,47 @@
1414
#include <odr/document_element.hpp>
1515
#include <odr/file.hpp>
1616
#include <odr/html.hpp>
17-
#include <odr/open_document_reader.hpp>
17+
#include <odr/html_service.hpp>
18+
#include <odr/odr.hpp>
1819
#include <odr/exceptions.hpp>
20+
#include <odr/global_params.hpp>
1921

2022
#include <string>
2123
#include <optional>
2224
#include <string>
2325
#include <optional>
2426

2527
@implementation CoreWrapper {
28+
std::optional<odr::Document> document;
2629
std::optional<odr::Html> html;
2730
}
2831

29-
- (bool)translate:(NSString *)inputPath into:(NSString *)outputPath with:(NSString *)password editable:(bool)editable {
32+
- (bool)translate:(NSString *)inputPath cache:(NSString *)cachePath into:(NSString *)outputPath with:(NSString *)password editable:(bool)editable {
3033
@synchronized(self) {
3134
try {
3235
_errorCode = 0;
3336
_pageNames = nil;
3437
_pagePaths = nil;
35-
36-
if (html.has_value()) {
37-
html.reset();
38-
}
39-
38+
39+
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
40+
std::string bundlePathCpp = std::string([bundlePath UTF8String]);
41+
odr::GlobalParams::set_odr_core_data_path(bundlePathCpp + "/odrcore");
42+
43+
html.reset();
44+
4045
odr::HtmlConfig config;
4146
config.editable = editable;
42-
47+
4348
if (password == nil) {
4449
password = @"";
4550
}
46-
47-
auto inputPathC = [inputPath cStringUsingEncoding:NSUTF8StringEncoding];
51+
52+
auto inputPathC = [inputPath UTF8String];
4853
auto inputPathCpp = std::string(inputPathC);
49-
54+
5055
std::vector<odr::FileType> fileTypes;
5156
try {
52-
fileTypes = odr::OpenDocumentReader::types(inputPathCpp);
57+
fileTypes = odr::types(inputPathCpp);
5358
if (fileTypes.empty()) {
5459
_errorCode = @(-5);
5560
return false;
@@ -63,33 +68,52 @@ - (bool)translate:(NSString *)inputPath into:(NSString *)outputPath with:(NSStri
6368
_errorCode = @(-5);
6469
return false;
6570
}
66-
67-
auto outputPathC = [outputPath cStringUsingEncoding:NSUTF8StringEncoding];
71+
72+
auto outputPathC = [outputPath UTF8String];
6873
auto outputPathCpp = std::string(outputPathC);
69-
70-
html = odr::OpenDocumentReader::html(inputPathCpp, [password]() { return std::string([password UTF8String]); }, outputPathCpp, config);
71-
74+
75+
auto cachePathC = [cachePath UTF8String];
76+
auto cachePathCpp = std::string(cachePathC);
77+
78+
odr::DecodedFile file = odr::open(inputPathCpp);
79+
if (file.password_encrypted()) {
80+
try {
81+
file = file.decrypt(std::string([password UTF8String]));
82+
} catch (odr::WrongPasswordError &) {
83+
_errorCode = @(-2);
84+
return false;
85+
}
86+
}
87+
if (!file.is_document_file()) {
88+
_errorCode = @(-5);
89+
return false;
90+
}
91+
document = file.document_file().document();
92+
93+
html = odr::html::translate(*document, cachePathCpp, config).bring_offline(outputPathCpp);
94+
7295
NSMutableArray *pageNames = [[NSMutableArray alloc] init];
7396
NSMutableArray *pagePaths = [[NSMutableArray alloc] init];
7497
for (auto &&page : html->pages()) {
7598
[pageNames addObject:[NSString stringWithCString:page.name.c_str() encoding:[NSString defaultCStringEncoding]]];
7699

77100
[pagePaths addObject:[NSString stringWithCString:page.path.c_str() encoding:[NSString defaultCStringEncoding]]];
78101
}
79-
102+
80103
_pageNames = pageNames;
81104
_pagePaths = pagePaths;
82105
} catch (odr::UnknownFileType&) {
83106
_errorCode = @(-5);
84107
return false;
85-
} catch (odr::WrongPassword&) {
86-
_errorCode = @(-2);
108+
} catch (std::runtime_error &e) {
109+
std::cout << e.what() << std::endl;
110+
_errorCode = @(-3);
87111
return false;
88112
} catch (...) {
89113
_errorCode = @(-3);
90114
return false;
91115
}
92-
116+
93117
return true;
94118
}
95119
}
@@ -98,10 +122,10 @@ - (bool)backTranslate:(NSString *)diff into:(NSString *)outputPath {
98122
@synchronized(self) {
99123
try {
100124
_errorCode = 0;
101-
102-
html->edit([diff cStringUsingEncoding:NSUTF8StringEncoding]);
103-
104-
html->save([outputPath cStringUsingEncoding:NSUTF8StringEncoding]);
125+
126+
odr::html::edit(*document, [diff UTF8String]);
127+
128+
document->save([outputPath UTF8String]);
105129
} catch (...) {
106130
_errorCode = @(-3);
107131
return false;

OpenDocumentReader/Document.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ class Document: UIDocument {
6767
result = nil
6868
delegate?.documentUpdateContent(self)
6969

70-
let tempPath = URL(fileURLWithPath: NSTemporaryDirectory())
71-
coreWrapper.translate(fileURL.path, into: tempPath.path, with: password, editable: edit)
72-
70+
let cachePath = URL(fileURLWithPath: NSTemporaryDirectory())
71+
let outputPath = URL(fileURLWithPath: NSTemporaryDirectory())
72+
coreWrapper.translate(fileURL.path, cache: cachePath.path, into: outputPath.path, with: password, editable: edit)
73+
7374
let errorCode = coreWrapper.errorCode != nil ? coreWrapper.errorCode.intValue : 0
7475
if (errorCode == -2) {
7576
self.delegate?.documentEncrypted(self)

OpenDocumentReaderTests/OpenDocumentReaderTests.swift

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,59 +11,56 @@ import XCTest
1111
@testable import OpenDocumentReader
1212

1313
class OpenDocumentReaderTests: XCTestCase {
14-
1514
// ONLINE fails on GitHub Actions for some reason
1615
private let ONLINE = false
17-
16+
1817
private var saveURL: URL?
1918

2019
override func setUpWithError() throws {
2120
var fileURL: URL?
22-
21+
2322
let documentsURL = try
2423
FileManager.default.url(for: .documentDirectory,
2524
in: .userDomainMask,
2625
appropriateFor: nil,
2726
create: false)
28-
27+
2928
saveURL = documentsURL.appendingPathComponent("test.odt")
30-
29+
3130
if (FileManager.default.fileExists(atPath: saveURL!.path)) {
3231
return
3332
}
34-
33+
3534
if (ONLINE) {
3635
let url = URL(string: "https://api.libreoffice.org/examples/cpp/DocumentLoader/test.odt")
3736

3837
let downloadTask = URLSession.shared.downloadTask(with: url!) {
39-
urlOrNil, responseOrNil, errorOrNil in
40-
41-
fileURL = urlOrNil
38+
urlOrNil, responseOrNil, errorOrNil in fileURL = urlOrNil
4239
}
4340
downloadTask.resume()
4441
} else {
4542
let filePath = Bundle(for: type(of: self)).path(forResource: "test", ofType: "odt")
4643
fileURL = URL(fileURLWithPath: filePath!)
4744
}
48-
49-
try FileManager.default.moveItem(at: fileURL!, to: self.saveURL!)
45+
46+
try FileManager.default.copyItem(at: fileURL!, to: self.saveURL!)
5047
}
51-
48+
5249
func testExample() throws {
5350
measure {
5451
let coreWrapper = CoreWrapper()
55-
56-
var translatePath = URL(fileURLWithPath: NSTemporaryDirectory())
57-
translatePath.appendPathComponent("translate.html")
58-
59-
coreWrapper.translate(saveURL?.path, into: translatePath.path, with: nil, editable: true)
52+
53+
let cachePath = URL(fileURLWithPath: NSTemporaryDirectory())
54+
let outputPath = URL(fileURLWithPath: NSTemporaryDirectory())
55+
56+
coreWrapper.translate(saveURL?.path, cache: cachePath.path, into: outputPath.path, with: nil, editable: true)
6057
XCTAssertNil(coreWrapper.errorCode)
61-
58+
6259
var backTranslatePath = URL(fileURLWithPath: NSTemporaryDirectory())
63-
backTranslatePath.appendPathComponent("backtranslate.html")
64-
60+
backTranslatePath.appendPathComponent("test-edited.odt")
61+
6562
let diff = "{\"modifiedText\":{\"/child:3/child:0\":\"This is a simple test document to demonstrate the DocumentLoaderwwww example!\"}}"
66-
63+
6764
coreWrapper.backTranslate(diff, into: backTranslatePath.path)
6865
XCTAssertNil(coreWrapper.errorCode)
6966
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ It's Android's first OpenOffice Document Reader... for iOS!
33

44
## Setup
55
1. run "pod install"
6-
2. run "bash conan/setup-all.sh"
6+
2. run "conan/setup-all.sh"
77
3. open workspace in Xcode

0 commit comments

Comments
 (0)