summaryrefslogtreecommitdiff
path: root/ios/LibreOfficeLight/LibreOfficeLight/DocumentController.swift
blob: 273b0e04ea970c277d3203c767db051c57a95746 (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
//
// 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 UIKit



// DocumentController is the main viewer in the app, it displays the selected
// documents and holds a top entry to view the properties as well as a normal
// menu to handle global actions
// It is a delegate class to receive Menu events as well as file handling events
class DocumentController: UIViewController, MenuDelegate, UIDocumentBrowserViewControllerDelegate
{
    // *** Handling of DocumentController
    // this is normal functions every controller must implement


    // holds known document types
    var KnownDocumentTypes : [String] = []


    // called once controller is loaded
    override func viewDidLoad()
    {
        super.viewDidLoad()

        // loading known document types, so we can use them for the open call
        let path = Bundle.main.path(forResource: "Info", ofType: "plist")
        let plist = NSDictionary(contentsOfFile: path!)
        for dict in (plist!.object(forKey: "UTExportedTypeDeclarations") as! [NSDictionary]) +
                    (plist!.object(forKey: "UTImportedTypeDeclarations") as! [NSDictionary]) {
            let x = ((dict["UTTypeTagSpecification"]  as! NSDictionary)["public.filename-extension"] as! NSArray)
            KnownDocumentTypes.append( x[0] as! String )
        }
    }



    // called when there is a memory constraint
    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // not used in this App
    }



    // *** Handling of Background (hipernate)
    // iOS is not true multitasking, only 1 app can be active (foreground) at any time,
    // therefore apps frequently are moved to the background.
    // background really means hipernate by terminating all threads and solely keep the
    // data



    // Moving to hipernate
    public func Hipernate() -> Void
    {
        BridgeLOkit_Hipernate()
    }



    // Moving back to foreground
    public func LeaveHipernate() -> Void
    {
        BridgeLOkit_LeaveHipernate()
    }



    // *** handling of PropertiesController
    // The PropertiesController is a left sidebar, that will scroll in when activated
    // The Controller handles manipulation of properties in the document



    // Activate/Deactivate PropertiesController (from navigationController, see storyboard)
    @IBAction func doProperties(_ sender: UIBarButtonItem)
    {
        // Check if deactivation
        if (sender.tag == 99) {
            // Deactivate

            // Mark it as deactivated (it stays loaded)
            sender.tag = 0;

            // get handle of PropertiesController
            let viewMenuBack : UIView = view.subviews.last!

            // Blend out sidebar
            UIView.animate(withDuration: 0.3, animations: { () -> Void in
                var frameMenu : CGRect = viewMenuBack.frame
                frameMenu.origin.x = -1 * UIScreen.main.bounds.size.width
                viewMenuBack.frame = frameMenu
                viewMenuBack.layoutIfNeeded()
                viewMenuBack.backgroundColor = UIColor.clear
                }, completion: { (finished) -> Void in
                    viewMenuBack.removeFromSuperview()
                })
        }
        else {
            // Activate

            // Mark as activated
            sender.isEnabled = false
            sender.tag = 99

            // make instance of PropertiesController
            let prop : PropertiesController = self.storyboard!.instantiateViewController(
                withIdentifier: "PropertiesController") as! PropertiesController
            view.addSubview(prop.view)
            addChildViewController(prop)
            prop.view.layoutIfNeeded()
            prop.view.frame=CGRect(x: 0 - UIScreen.main.bounds.size.width,
                                   y: 0,
                                   width: UIScreen.main.bounds.size.width,
                                   height: UIScreen.main.bounds.size.height);

            // Blend in sidebar
            UIView.animate(withDuration: 0.3, animations: { () -> Void in
                prop.view.frame=CGRect(x: 0,
                                       y: 0,
                                       width: UIScreen.main.bounds.size.width,
                                       height: UIScreen.main.bounds.size.height);
                sender.isEnabled = true
                }, completion:nil)
        }
    }



    // *** Handling of menu popover
    // the menu contains all global functions and use seque/delegate



    var currentDocumentName : String? = nil



    // Last stop before displaying popover
    override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        // "showActions" is the name of the popover menu, see storyboard
        if segue.identifier == "showActions" {
            let vc = segue.destination as! DocumentActions
            vc.delegate = self
            vc.isDocActive = (currentDocumentName != nil)
        }
    }



    // Delegate call from menu (see protocol MenuDelegate)
    func actionMenuSelected(_ tag : Int)
    {
        // a tag can sadly enough only be a number and not a string,
        // whenever adding a menu entry, it (of course) needs to be added
        // to the Document actions scene in storyboard and assigned a tag number
        // the tag number must be repeated in the following switch
        // BE CAREFUL to keep the tags synchronized (manually)
        switch tag
        {
        case 1: // Open...
                startOpenDocument()

        case 2: // Properties
                showProperties()

        case 3: // Save
                doSave()

        case 4: // Close...
                doClose()

        case 4: // Save as...
                doSaveAs()

        case 5: // Save as PDF...
                doSaveAsPDF()

        case 6: // Print...
                startPrint()

        default: // should not happen
                 print("unknown menu" + String(tag))
        }
    }



    // *** handling of menu actions
    // This is the real base of the application

    var openMenu : UIDocumentBrowserViewController? = nil

    // Load document into LibreOfficeKit and present it
    internal func startOpenDocument()
    {
        openMenu = UIDocumentBrowserViewController()
        openMenu?.allowsDocumentCreation = true
        openMenu?.browserUserInterfaceStyle = UIDocumentBrowserViewController.BrowserUserInterfaceStyle.dark
        openMenu?.delegate = self
        self.present(openMenu!, animated: true, completion: nil)
    }



    // Show document properties (new overloaded page)
    internal func showProperties()
    {
        //FIXME
        print("menu Properties to be done")
    }



    // Save current document
    internal func doSave()
    {
        //FIXME
        print("menu Save to be done")
    }



    // Close current document (without saving)
    internal func doClose()
    {
        //FIXME
        print("menu Close to be done")
    }



    // make a copy of current document, and save
    internal func doSaveAs()
    {
        //FIXME
        print("menu Save as... to be done")
    }



    // save current document as PDF
    internal func doSaveAsPDF()
    {
        //FIXME
        print("menu Save as PDF... to be done")
    }



    // print current document
    internal func startPrint()
    {
        //FIXME
        print("menu Print... to be done")
    }



    // *** Handling of DocumentViewController delegate functions
    // this handles open/create/copy/delete document



    // Create an empty document, and present it
    internal func documentBrowser(_ controller: UIDocumentBrowserViewController,
                                  didRequestDocumentCreationWithHandler importHandler: @escaping (URL?,
                                  UIDocumentBrowserViewController.ImportMode) -> Void)
    {
        //FIXME
    }



    // import (copy from iCloud to iPad) document, open it and present it
    internal func documentBrowser(_ controller: UIDocumentBrowserViewController,
                                  didImportDocumentAt sourceURL: URL,
                                  toDestinationURL destinationURL: URL)
    {
        //FIXME
    }



    // Import failed, inform user
    internal func documentBrowser(_ controller: UIDocumentBrowserViewController,
                                  failedToImportDocumentAt documentURL: URL,
                                  error: Error?)
    {
        //FIXME
    }



    // open document and present it
    internal func documentBrowser(_ controller: UIDocumentBrowserViewController,
                                  didPickDocumentURLs documentURLs: [URL])
    {
        openMenu?.dismiss(animated: true, completion: nil)
        openMenu = nil
        doOpen(documentURLs[0])
    }



    // *** Handling of document (open/print)



    // Real open and presentation of document
    public func doOpen(_ docURL : URL)
    {
        BridgeLOkit_open(docURL.absoluteString);
        BridgeLOkit_Sizing(4, 4, 256, 256);
    }
}