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 ;
0 commit comments