Skip to content

Commit f49feb9

Browse files
authored
Merge pull request #11 from TomTasche/development
v1.2
2 parents ef29d35 + b710c7c commit f49feb9

52 files changed

Lines changed: 2481 additions & 796 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

OpenDocument.core

OpenDocumentReader.xcodeproj/project.pbxproj

Lines changed: 128 additions & 32 deletions
Large diffs are not rendered by default.

OpenDocumentReader/AppDelegate.swift

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1616

1717
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
1818
FirebaseApp.configure()
19+
20+
StoreReviewHelper.incrementAppOpenedCount()
21+
1922
return true
2023
}
2124

@@ -42,12 +45,27 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
4245
}
4346

4447
func application(_ app: UIApplication, open inputURL: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
45-
// Ensure the URL is a file URL
46-
guard inputURL.isFileURL else { return false }
48+
guard let documentBrowserViewController = window?.rootViewController as? DocumentBrowserViewController else {
49+
print("*** The root view is not a document browser! ***")
50+
return false
51+
}
4752

48-
guard let controller = window?.rootViewController as? UINavigationController else { return false }
49-
let browserController = controller.viewControllers.first as! DocumentBrowserViewController
50-
browserController.importAndLoad(inputURL)
53+
documentBrowserViewController.revealDocument(at: inputURL, importIfNeeded: true) { (revealedDocumentURL, error) in
54+
55+
guard error == nil else {
56+
print("*** Failed to reveal the document at %@. Error: %@. ***")
57+
return
58+
}
59+
60+
guard let url = revealedDocumentURL else {
61+
print("*** No URL revealed. ***")
62+
return
63+
}
64+
65+
print("==> Revealed URL")
66+
67+
documentBrowserViewController.presentDocument(at: url)
68+
}
5169

5270
return true
5371
}

OpenDocumentReader/Base.lproj/LaunchScreen.storyboard

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
33
<device id="retina4_7" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
77
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
99
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
1010
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1111
</dependencies>
@@ -15,11 +15,11 @@
1515
<objects>
1616
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
1717
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
18-
<rect key="frame" x="0.0" y="0.0" width="414" height="736"/>
18+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
1919
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
2020
<subviews>
2121
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" fixedFrame="YES" usesAttributedText="YES" translatesAutoresizingMaskIntoConstraints="NO" id="NIw-eq-wmR">
22-
<rect key="frame" x="48" y="141" width="318" height="146"/>
22+
<rect key="frame" x="29" y="141" width="318" height="146"/>
2323
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES"/>
2424
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
2525
<attributedString key="attributedText">
@@ -33,11 +33,11 @@
3333
<textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
3434
</textView>
3535
<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" fixedFrame="YES" usesAttributedText="YES" translatesAutoresizingMaskIntoConstraints="NO" id="6bP-eF-b7K">
36-
<rect key="frame" x="65" y="318" width="287" height="128"/>
36+
<rect key="frame" x="45" y="318" width="287" height="128"/>
3737
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxX="YES"/>
3838
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
3939
<attributedString key="attributedText">
40-
<fragment content="Choose a .odt-file to get started! Enjoy.">
40+
<fragment content="Choose a compatible file to get started!">
4141
<attributes>
4242
<font key="NSFont" size="14" name="ArialMT"/>
4343
<paragraphStyle key="NSParagraphStyle" alignment="center" lineBreakMode="wordWrapping" baseWritingDirection="natural" tighteningFactorForTruncation="0.0"/>

OpenDocumentReader/Base.lproj/Main.storyboard

Lines changed: 0 additions & 99 deletions
This file was deleted.

OpenDocumentReader/CoreWrapper.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
#import <Foundation/Foundation.h>
1313

1414
@interface CoreWrapper : NSObject
15-
- (int)translate:(NSString *)inputPath into:(NSString *)outputPath at:(NSNumber *)page;
15+
16+
@property NSArray *pageNames;
17+
@property NSNumber *errorCode;
18+
19+
- (bool)translate:(NSString *)inputPath into:(NSString *)outputPath at:(NSNumber *)page with:(NSString *)password;
1620
@end
1721

1822
#endif /* CoreWrapper_h */

OpenDocumentReader/CoreWrapper.mm

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,52 @@
1212

1313
#include "TranslationHelper.h"
1414
#include "TranslationConfig.h"
15-
#include "DocumentMeta.h"
15+
#include "FileMeta.h"
1616

1717
@implementation CoreWrapper
18-
- (int)translate:(NSString *)inputPath into:(NSString *)outputPath at:(NSNumber *)page {
19-
odr::TranslationConfig config = {};
20-
18+
- (bool)translate:(NSString *)inputPath into:(NSString *)outputPath at:(NSNumber *)page with:(NSString *)password {
2119
try {
2220
auto translator = odr::TranslationHelper::create();
23-
translator->open([inputPath cStringUsingEncoding:NSUTF8StringEncoding]);
24-
25-
config.entryOffset = page.intValue;
21+
bool opened = translator->open([inputPath cStringUsingEncoding:NSUTF8StringEncoding]);
22+
if (!opened) {
23+
_errorCode = @(-1);
24+
return false;
25+
}
26+
27+
const odr::FileMeta& meta = translator->getMeta();
2628

27-
const odr::DocumentMeta& meta = translator->getMeta();
28-
config.entryCount = meta.entryCount;
29+
bool decrypted = !meta.encrypted;
30+
if (password != nil) {
31+
decrypted = translator->decrypt([password cStringUsingEncoding:NSUTF8StringEncoding]);
32+
}
33+
34+
if (!decrypted) {
35+
_errorCode = @(-2);
36+
return false;
37+
}
38+
39+
odr::TranslationConfig config = {};
40+
config.entryOffset = page.intValue;
41+
config.entryCount = 1;
2942

30-
if (meta.type == odr::DocumentType::TEXT) {
31-
config.entryCount = 1;
43+
NSMutableArray *pageNames = [[NSMutableArray alloc] init];
44+
if (meta.type == odr::FileType::OPENDOCUMENT_TEXT) {
45+
[pageNames addObject:@"Text document"];
46+
} else {
47+
for (auto page = meta.entries.begin(); page != meta.entries.end(); page++) {
48+
auto pageName = page->name;
49+
50+
[pageNames addObject:[NSString stringWithCString:pageName.c_str() encoding:[NSString defaultCStringEncoding]]];
51+
}
3252
}
53+
_pageNames = pageNames;
3354

3455
translator->translate([outputPath cStringUsingEncoding:NSUTF8StringEncoding], config);
3556
} catch (...) {
36-
return -1;
57+
_errorCode = @(-3);
58+
return false;
3759
}
3860

39-
return config.entryCount;
61+
return true;
4062
}
4163
@end

OpenDocumentReader/Document.swift

Lines changed: 96 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,106 @@
1-
//
2-
// Document.swift
3-
// OpenDocument Reader
4-
//
5-
// Created by Thomas Taschauer on 06.02.19.
6-
// Copyright © 2019 Thomas Taschauer. All rights reserved.
7-
//
1+
/*
2+
See LICENSE folder for this sample’s licensing information.
3+
4+
Abstract:
5+
A document that manages UTF8 text files.
6+
*/
87

98
import UIKit
109

10+
protocol DocumentDelegate: class {
11+
func documentUpdateContent(_ doc: Document)
12+
func documentEncrypted(_ doc: Document)
13+
func documentLoadingError(_ doc: Document)
14+
func documentLoadingStarted(_ doc: Document)
15+
func documentLoadingCompleted(_ doc: Document)
16+
func documentPagesChanged(_ doc: Document)
17+
}
18+
1119
class Document: UIDocument {
1220

13-
override func contents(forType typeName: String) throws -> Any {
14-
// Encode your document with an instance of NSData or NSFileWrapper
15-
return Data()
21+
public var result: URL?
22+
public var pageNames: [String]?
23+
24+
public weak var delegate: DocumentDelegate?
25+
public var loadProgress = Progress(totalUnitCount: 5)
26+
27+
private var page: Int = 0
28+
private var password: String?
29+
30+
private var wasPageCountAnnounced = false
31+
32+
override init(fileURL url: URL) {
33+
super.init(fileURL: url)
1634
}
1735

1836
override func load(fromContents contents: Any, ofType typeName: String?) throws {
19-
// Load your document from contents
37+
parse()
2038
}
21-
}
39+
40+
func parse() {
41+
delegate?.documentLoadingStarted(self)
42+
43+
loadProgress.completedUnitCount = 2
44+
loadProgress.resume()
45+
46+
result = nil
47+
delegate?.documentUpdateContent(self)
2248

49+
var tempPath = URL(fileURLWithPath: NSTemporaryDirectory())
50+
tempPath.appendPathComponent("temp.html")
51+
52+
let core = CoreWrapper()
53+
core.translate(fileURL.path, into: tempPath.path, at: NSNumber(value: page), with: password)
54+
55+
let errorCode = core.errorCode != nil ? core.errorCode.intValue : 0
56+
57+
if (errorCode == -2) {
58+
// delay because ViewController might not be visible yet
59+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
60+
self.delegate?.documentEncrypted(self)
61+
})
62+
63+
return;
64+
}
65+
66+
if (errorCode < 0) {
67+
delegate?.documentLoadingError(self)
68+
69+
return
70+
}
71+
72+
loadProgress.completedUnitCount = loadProgress.totalUnitCount
73+
74+
var pageNames = [String]()
75+
for object in core.pageNames {
76+
if let pageName = object as? String {
77+
pageNames.append(pageName)
78+
}
79+
}
80+
81+
self.pageNames = pageNames
82+
result = tempPath
83+
84+
delegate?.documentUpdateContent(self)
85+
86+
if (!wasPageCountAnnounced) {
87+
delegate?.documentPagesChanged(self)
88+
89+
wasPageCountAnnounced = true
90+
}
91+
92+
delegate?.documentLoadingCompleted(self)
93+
}
94+
95+
func setPassword(password: String) {
96+
self.password = password
97+
98+
parse()
99+
}
100+
101+
func setPage(page: Int) {
102+
self.page = page
103+
104+
parse()
105+
}
106+
}

0 commit comments

Comments
 (0)