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>
1920
2021#include < string>
2324#include < optional>
2425
2526@implementation CoreWrapper {
27+ std::optional<odr::Document> document;
2628 std::optional<odr::Html> html;
2729}
2830
@@ -32,24 +34,22 @@ - (bool)translate:(NSString *)inputPath into:(NSString *)outputPath with:(NSStri
3234 _errorCode = 0 ;
3335 _pageNames = nil ;
3436 _pagePaths = nil ;
35-
36- if (html.has_value ()) {
37- html.reset ();
38- }
39-
37+
38+ html.reset ();
39+
4040 odr::HtmlConfig config;
4141 config.editable = editable;
42-
42+
4343 if (password == nil ) {
4444 password = @" " ;
4545 }
46-
46+
4747 auto inputPathC = [inputPath cStringUsingEncoding: NSUTF8StringEncoding];
4848 auto inputPathCpp = std::string (inputPathC);
49-
49+
5050 std::vector<odr::FileType> fileTypes;
5151 try {
52- fileTypes = odr::OpenDocumentReader:: types (inputPathCpp);
52+ fileTypes = odr::types (inputPathCpp);
5353 if (fileTypes.empty ()) {
5454 _errorCode = @(-5 );
5555 return false ;
@@ -63,33 +63,45 @@ - (bool)translate:(NSString *)inputPath into:(NSString *)outputPath with:(NSStri
6363 _errorCode = @(-5 );
6464 return false ;
6565 }
66-
66+
6767 auto outputPathC = [outputPath cStringUsingEncoding: NSUTF8StringEncoding];
6868 auto outputPathCpp = std::string (outputPathC);
69-
70- html = odr::OpenDocumentReader::html (inputPathCpp, [password]() { return std::string ([password UTF8String ]); }, outputPathCpp, config);
71-
69+
70+ odr::DecodedFile file = odr::open (inputPathCpp);
71+ if (file.password_encrypted ()) {
72+ try {
73+ file = file.decrypt (std::string ([password UTF8String ]));
74+ } catch (odr::WrongPasswordError &) {
75+ _errorCode = @(-2 );
76+ return false ;
77+ }
78+ }
79+ if (!file.is_document_file ()) {
80+ _errorCode = @(-5 );
81+ return false ;
82+ }
83+ document = file.document_file ().document ();
84+
85+ html = odr::html::translate (*document, outputPathCpp, config).bring_offline (outputPathCpp);
86+
7287 NSMutableArray *pageNames = [[NSMutableArray alloc ] init ];
7388 NSMutableArray *pagePaths = [[NSMutableArray alloc ] init ];
7489 for (auto &&page : html->pages ()) {
7590 [pageNames addObject: [NSString stringWithCString: page.name.c_str () encoding: [NSString defaultCStringEncoding ]]];
7691
7792 [pagePaths addObject: [NSString stringWithCString: page.path.c_str () encoding: [NSString defaultCStringEncoding ]]];
7893 }
79-
94+
8095 _pageNames = pageNames;
8196 _pagePaths = pagePaths;
8297 } catch (odr::UnknownFileType&) {
8398 _errorCode = @(-5 );
8499 return false ;
85- } catch (odr::WrongPassword&) {
86- _errorCode = @(-2 );
87- return false ;
88100 } catch (...) {
89101 _errorCode = @(-3 );
90102 return false ;
91103 }
92-
104+
93105 return true ;
94106 }
95107}
@@ -98,10 +110,10 @@ - (bool)backTranslate:(NSString *)diff into:(NSString *)outputPath {
98110 @synchronized (self) {
99111 try {
100112 _errorCode = 0 ;
101-
102- html-> edit ([diff cStringUsingEncoding: NSUTF8StringEncoding]);
103-
104- html ->save ([outputPath cStringUsingEncoding: NSUTF8StringEncoding]);
113+
114+ odr:: html:: edit (*document, [diff cStringUsingEncoding: NSUTF8StringEncoding]);
115+
116+ document ->save ([outputPath cStringUsingEncoding: NSUTF8StringEncoding]);
105117 } catch (...) {
106118 _errorCode = @(-3 );
107119 return false ;
0 commit comments