Welcome to mirror list, hosted at ThFree Co, Russian Federation.

NCViewer.swift « Viewer « iOSClient - github.com/nextcloud/ios.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 691c7537b97e09edd820935011d96d8545c0f74d (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
//
//  NCViewer.swift
//  Nextcloud
//
//  Created by Marino Faggiana on 16/10/2020.
//  Copyright © 2020 Marino Faggiana. All rights reserved.
//
//  Author Marino Faggiana <marino.faggiana@nextcloud.com>
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

import UIKit
import NextcloudKit
import QuickLook

class NCViewer: NSObject {
    @objc static let shared: NCViewer = {
        let instance = NCViewer()
        return instance
    }()

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    private var viewerQuickLook: NCViewerQuickLook?
    private var metadata = tableMetadata()
    private var metadatas: [tableMetadata] = []

    func view(viewController: UIViewController, metadata: tableMetadata, metadatas: [tableMetadata], imageIcon: UIImage?, editor: String = "", isRichDocument: Bool = false) {

        self.metadata = metadata
        self.metadatas = metadatas

        var editor = editor
        var xxxxxxx = NKCommon.shared.getInternalTypeIdentifier(typeIdentifier: metadata.contentType)

        // URL
        if metadata.classFile == NKCommon.typeClassFile.url.rawValue {

            // nextcloudtalk://open-conversation?server={serverURL}&user={userId}&withRoomToken={roomToken}
            if metadata.name == NCGlobal.shared.talkName {
                let pathComponents = metadata.url.components(separatedBy: "/")
                if pathComponents.contains("call") {
                    let talkComponents = pathComponents.last?.components(separatedBy: "#")
                    if let roomToken = talkComponents?.first {
                        let urlString = "nextcloudtalk://open-conversation?server=\(appDelegate.urlBase)&user=\(appDelegate.userId)&withRoomToken=\(roomToken)"
                        if let url = URL(string: urlString), UIApplication.shared.canOpenURL(url) {
                            UIApplication.shared.open(url)
                            return
                        }
                    }
                }
            }

            if let url = URL(string: metadata.url) {
                UIApplication.shared.open(url)
            }
            return
        }

        // IMAGE AUDIO VIDEO
        if metadata.classFile == NKCommon.typeClassFile.image.rawValue || metadata.classFile == NKCommon.typeClassFile.audio.rawValue || metadata.classFile == NKCommon.typeClassFile.video.rawValue {

            if let navigationController = viewController.navigationController {

                let viewerMediaPageContainer: NCViewerMediaPage = UIStoryboard(name: "NCViewerMediaPage", bundle: nil).instantiateInitialViewController() as! NCViewerMediaPage
                var index = 0
                for medatasImage in metadatas {
                    if medatasImage.ocId == metadata.ocId {
                        viewerMediaPageContainer.currentIndex = index
                        break
                    }
                    index += 1
                }
                viewerMediaPageContainer.metadatas = metadatas
                navigationController.pushViewController(viewerMediaPageContainer, animated: true)
            }

            return
        }

        // DOCUMENTS
        if metadata.classFile == NKCommon.typeClassFile.document.rawValue {

            // PDF
            if metadata.contentType == "application/pdf" || metadata.contentType == "com.adobe.pdf" {

                if let navigationController = viewController.navigationController {

                    let viewController: NCViewerPDF = UIStoryboard(name: "NCViewerPDF", bundle: nil).instantiateInitialViewController() as! NCViewerPDF

                    viewController.metadata = metadata
                    viewController.imageIcon = imageIcon

                    navigationController.pushViewController(viewController, animated: true)
                }
                return
            }

            // EDITORS
            let editors = NCUtility.shared.isDirectEditing(account: metadata.account, contentType: metadata.contentType)
            let availableRichDocument = NCUtility.shared.isRichDocument(metadata)

            // RichDocument: Collabora
            if (isRichDocument || (availableRichDocument && editors.count == 0)) && NextcloudKit.shared.isNetworkReachable() {

                if metadata.url == "" {

                    NCActivityIndicator.shared.start(backgroundView: viewController.view)
                    NextcloudKit.shared.createUrlRichdocuments(fileID: metadata.fileId) { account, url, data, error in

                        NCActivityIndicator.shared.stop()

                        if error == .success && account == self.appDelegate.account && url != nil {

                            if let navigationController = viewController.navigationController {

                                let viewController: NCViewerRichdocument = UIStoryboard(name: "NCViewerRichdocument", bundle: nil).instantiateInitialViewController() as! NCViewerRichdocument

                                viewController.metadata = metadata
                                viewController.link = url!
                                viewController.imageIcon = imageIcon

                                navigationController.pushViewController(viewController, animated: true)
                            }

                        } else if error != .success {

                            NCContentPresenter.shared.showError(error: error)
                        }
                    }

                } else {

                    if let navigationController = viewController.navigationController {

                        let viewController: NCViewerRichdocument = UIStoryboard(name: "NCViewerRichdocument", bundle: nil).instantiateInitialViewController() as! NCViewerRichdocument

                        viewController.metadata = metadata
                        viewController.link = metadata.url
                        viewController.imageIcon = imageIcon

                        navigationController.pushViewController(viewController, animated: true)
                    }
                }

                return
            }

            // DirectEditing: Nextcloud Text - OnlyOffice
            if editors.count > 0 && NextcloudKit.shared.isNetworkReachable() {

                if editor == "" {
                    if editors.contains(NCGlobal.shared.editorText) {
                        editor = NCGlobal.shared.editorText
                    } else if editors.contains(NCGlobal.shared.editorOnlyoffice) {
                        editor = NCGlobal.shared.editorOnlyoffice
                    }
                }

                if editor == NCGlobal.shared.editorText || editor == NCGlobal.shared.editorOnlyoffice {

                    if metadata.url == "" {

                        let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, account: metadata.account)!

                        var options = NKRequestOptions()
                        if editor == NCGlobal.shared.editorOnlyoffice {
                            options = NKRequestOptions(customUserAgent: NCUtility.shared.getCustomUserAgentOnlyOffice())
                        } else {
                            options = NKRequestOptions(customUserAgent: NCUtility.shared.getCustomUserAgentNCText())
                        }

                        NCActivityIndicator.shared.start(backgroundView: viewController.view)
                        NextcloudKit.shared.NCTextOpenFile(fileNamePath: fileNamePath, editor: editor, options: options) { account, url, data, error in

                            NCActivityIndicator.shared.stop()

                            if error == .success && account == self.appDelegate.account && url != nil {

                                if let navigationController = viewController.navigationController {

                                    let viewController: NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as! NCViewerNextcloudText

                                    viewController.metadata = metadata
                                    viewController.editor = editor
                                    viewController.link = url!
                                    viewController.imageIcon = imageIcon

                                    navigationController.pushViewController(viewController, animated: true)
                                }

                            } else if error != .success {

                                NCContentPresenter.shared.showError(error: error)
                            }
                        }

                    } else {

                        if let navigationController = viewController.navigationController {

                            let viewController: NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as! NCViewerNextcloudText

                            viewController.metadata = metadata
                            viewController.editor = editor
                            viewController.link = metadata.url
                            viewController.imageIcon = imageIcon

                            navigationController.pushViewController(viewController, animated: true)
                        }
                    }

                } else {
                    let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_editor_unknown_")
                    NCContentPresenter.shared.showError(error: error)
                }

                return
            }
        }

        // QLPreview
        let item = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
        if QLPreviewController.canPreview(item as QLPreviewItem) {
            let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
            CCUtility.copyFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
            let viewerQuickLook = NCViewerQuickLook(with: URL(fileURLWithPath: fileNamePath), isEditingEnabled: false, metadata: metadata)
            viewController.present(viewerQuickLook, animated: true)
        } else {
        // Document Interaction Controller
            NCFunctionCenter.shared.openDocumentController(metadata: metadata)
        }
    }
}

// MARK: - SELECT

extension NCViewer: NCSelectDelegate {
    func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], overwrite: Bool, copy: Bool, move: Bool) {
        if let serverUrl = serverUrl {
            let metadata = items[0] as! tableMetadata
            if move {
                NCNetworking.shared.moveMetadata(metadata, serverUrlTo: serverUrl, overwrite: overwrite) { error in
                    if error != .success {

                        NCContentPresenter.shared.showError(error: error)
                    }
                }
            } else if copy {
                NCNetworking.shared.copyMetadata(metadata, serverUrlTo: serverUrl, overwrite: overwrite) { error in
                    if error != .success {

                        NCContentPresenter.shared.showError(error: error)
                    }
                }
            }
        }
    }
}