summaryrefslogtreecommitdiffstats
path: root/ios/LibreOfficeLight/LibreOfficeLight/LOKit/DocumentHolder.swift
blob: 0871c678dcaaffd40b61a5dc68efc1e5a054eb15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//
// This file is part of the LibreOffice project.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//

import Foundation
import UIKit
import QuartzCore

/**
 * Holds the document object so to enforce access in a thread safe way.
 */
public class DocumentHolder
{
    private let doc: Document

    public weak var delegate: DocumentUIDelegate? = nil
    public weak var searchDelegate: SearchDelegate? = nil

    private let cache = RenderCache()

    public let documentType: LibreOfficeKitDocumentType
    public let documentSize: CGSize
    public let views: Int32
    public let parts: Int32
    public let partNames: [String]

    public private(set) var currentPart: Int32 = 0

    init(doc: Document)
    {
        self.doc = doc

        // we go and get a bunch of document properties and store them in properties
        // this allows easy access to these without threading issues
        // when we get to editing they will have to be invalidated

        self.documentType = doc.getDocumentType()
        documentSize = doc.getDocumentSizeAsCGSize()
        views = doc.getViewsCount()
        parts = doc.getParts()

        var partNames = [String]()
        for i in 0..<parts
        {
            let n = doc.getPartName(nPart: i) ?? ""
            partNames.append(n)
        }
        self.partNames = partNames

        doc.registerCallback() {
            [weak self] typ, payload in
            self?.onDocumentEvent(type: typ, payload: payload)
        }
    }

    public var isPresentation: Bool
    {
        return documentType == LOK_DOCTYPE_PRESENTATION
    }
    public var isText: Bool
    {
        return documentType == LOK_DOCTYPE_TEXT
    }
    public var isDrawing: Bool
    {
        return documentType == LOK_DOCTYPE_DRAWING
    }
    public var isSpeadsheet: Bool
    {
        return documentType == LOK_DOCTYPE_SPREADSHEET
    }

    /// Gives async access to the document
    public func async(_ closure: @escaping (Document) -> ())
    {
        LOKitThread.instance.async
        {
            closure(self.doc)
        }
        self.invokeHandlers()
    }

    public func invokeHandlers()
    {
        LOKitThread.instance.async
        {
            self.doc.invokeHandlers()
        }
    }

    /// Gives sync access to the document - blocks until the closure runs.
    /// Careful of deadlocks.
    public func sync<R>( _ closure: @escaping (Document) -> R ) -> R
    {
        return LOKitThread.instance.sync
        {
            self.invokeHandlers()
            return closure(self.doc)
        }
    }

    /// Paints a tile and return synchronously, using a cached version if it can
    public func paintTileToImage(canvasSize: CGSize,
                                 tileRect: CGRect) -> UIImage?
    {
        if let cached = cache.get(part: currentPart, canvasSize: canvasSize, tileRect: tileRect)
        {
            return cached
        }

        let img = sync {
            $0.paintTileToImage(canvasSize: canvasSize, tileRect: tileRect)
        }
        if let image = img
        {
            cache.add(cachedRender: CachedRender(part: currentPart, canvasSize: canvasSize, tileRect: tileRect, image: image))
        }

        return img
    }

    private func onDocumentEvent(type: LibreOfficeKitCallbackType, payload: String?)
    {
        print("onDocumentEvent type:\(type) payload:\(payload ?? "")")

        switch type
        {
        case LOK_CALLBACK_INVALIDATE_TILES:
            runOnMain {
                self.delegate?.invalidateTiles( rects: decodeRects(payload) )
            }
        case LOK_CALLBACK_INVALIDATE_VISIBLE_CURSOR:
            runOnMain {
                self.delegate?.invalidateVisibleCursor( rects: decodeRects(payload) )
            }
        case LOK_CALLBACK_TEXT_SELECTION:
            runOnMain {
                self.delegate?.textSelection( rects: decodeRects(payload) )
            }
        case LOK_CALLBACK_TEXT_SELECTION_START:
            runOnMain {
                self.delegate?.textSelectionStart( rects: decodeRects(payload) )
            }
        case LOK_CALLBACK_TEXT_SELECTION_END:
            runOnMain {
                self.delegate?.textSelectionEnd( rects: decodeRects(payload) )
            }

        case LOK_CALLBACK_SEARCH_NOT_FOUND:
            runOnMain {
                self.searchDelegate?.searchNotFound()
            }
        case LOK_CALLBACK_SEARCH_RESULT_SELECTION:
            runOnMain {
                self.searchResults(payload: payload)
            }

        case LOK_CALLBACK_SET_PART:
            if let p = payload, let newPart = Int32(p)
            {
                self.currentPart = newPart
                // TODO: callback?
            }

        case LOK_CALLBACK_STATE_CHANGED:
            // TODO: call backback
            print("onDocumentEvent type: LOK_CALLBACK_STATE_CHANGED: \(payload ?? "")")

        default:
            print("onDocumentEvent type:\(type) not handled!")
        }
    }

    private func searchResults(payload: String?)
    {
        if let d = payload, let data = d.data(using: .utf8)
        {
            let decoder = JSONDecoder()
            do
            {
                let searchResults = try decoder.decode(SearchResults.self, from: data )
                self.searchDelegate?.searchResultSelection(searchResults: searchResults)
            }
            catch
            {
                print("Error decoding payload: \(error)")
            }
        }
    }

    public func search(searchString: String, forwardDirection: Bool = true, from: CGPoint)
    {
        var rootJson = JSONObject()
        addProperty(&rootJson, "SearchItem.SearchString", "string", searchString);
        addProperty(&rootJson, "SearchItem.Backward", "boolean", String(!forwardDirection) );
        addProperty(&rootJson, "SearchItem.SearchStartPointX", "long", String(describing: from.x) );
        addProperty(&rootJson, "SearchItem.SearchStartPointY", "long", String(describing: from.y) );
        addProperty(&rootJson, "SearchItem.Command", "long", "0") // String.valueOf(0)); // search all == 1

        if let jsonStr = encode(json: rootJson)
        {
            async {
                $0.postUnoCommand(command: ".uno:ExecuteSearch", arguments: jsonStr, notifyWhenFinished: true)
            }
        }
    }

    public func incrementPart(amount: Int32)
    {
        async {
            document in
            let currentPart = document.getPart()
            let numParts = document.getParts()
            let newPart = currentPart + amount
            if (newPart < numParts && newPart > 0)
            {
                document.setPart(nPart: newPart)
            }
        }
    }
}

public typealias JSONObject = Dictionary<String, AnyObject>
public func addProperty( _ json: inout JSONObject, _ parentValue: String, _ type: String, _ value: String)
{
    var child = JSONObject();
    child["type"] = type as AnyObject
    child["value"] = value as AnyObject
    json[parentValue] = child as AnyObject
}

func encode(json: JSONObject) -> String?
{
    if let data = try? JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
    {
        return String(data: data, encoding: String.Encoding.utf8)
    }
    return nil
}

/// Decodes a series of rectangles in the form: "x, y, width, height; x, y, width, height"
public func decodeRects(_ payload: String?) -> [CGRect]?
{
    guard var pl = payload else { return nil }
    pl = pl.trimmingCharacters(in: .whitespacesAndNewlines )
    if pl == "EMPTY" || pl.count == 0
    {
        return nil
    }
    var ret = [CGRect]()
    for rectStr in pl.split(separator: ";")
    {
        let coords = rectStr.split(separator: ",").flatMap { Double($0.trimmingCharacters(in: .whitespacesAndNewlines)) }
        if coords.count == 4
        {
            let rect = CGRect(x: coords[0],
                              y: coords[1],
                              width: coords[2],
                              height: coords[3])
            ret.append( rect )
        }
    }
    return ret
}

// MARK :- Delegates

public protocol DocumentUIDelegate: class
{
    func invalidateTiles(rects: [CGRect]? )
    func invalidateVisibleCursor(rects: [CGRect]? )

    func textSelection(rects: [CGRect]? )
    func textSelectionStart(rects: [CGRect]? )
    func textSelectionEnd(rects: [CGRect]? )
}

public protocol SearchDelegate: class
{
    func searchNotFound()
    func searchResultSelection(searchResults: SearchResults)
}

/**
 Encodes this example json:
 {
 "searchString": "Office",
 "highlightAll": "true",
 "searchResultSelection": [
 {
 "part": "0",
 "rectangles": "1951, 10743, 627, 239"
 },
 {
 "part": "0",
 "rectangles": "5343, 9496, 627, 287"
 },
 {
 "part": "0",
 "rectangles": "1951, 9256, 627, 239"
 },
 {
 "part": "0",
 "rectangles": "6502, 5946, 626, 287"
 },
 {
 "part": "0",
 "rectangles": "6686, 5658, 627, 287"
 },
 {
 "part": "0",
 "rectangles": "4103, 5418, 573, 239"
 },
 {
 "part": "0",
 "rectangles": "1951, 5418, 627, 239"
 },
 {
 "part": "0",
 "rectangles": "4934, 1658, 1586, 559"
 }
 ]
 }
 */
public struct SearchResults: Codable
{
    public var searchString: String?
    public var highlightAll: String?
    public var searchResultSelection: Array<PartAndRectangles>?
}

public struct PartAndRectangles: Codable
{
    public var part: String?
    public var rectangles: String?

    public var rectsAsCGRects: [CGRect]? {
        return decodeRects(self.rectangles)
    }
}