File: ShareOptionsView.swift

package info (click to toggle)
nextcloud-desktop 4.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 40,404 kB
  • sloc: cpp: 118,401; objc: 752; python: 606; sh: 395; ansic: 391; ruby: 174; makefile: 44; javascript: 32; xml: 6
file content (390 lines) | stat: -rw-r--r-- 16,762 bytes parent folder | download
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//
//  ShareOptionsView.swift
//  FileProviderUIExt
//
//  SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
//  SPDX-License-Identifier: GPL-2.0-or-later
//

import AppKit
import Combine
import NextcloudFileProviderKit
import NextcloudKit
import OSLog
import SuggestionsTextFieldKit

class ShareOptionsView: NSView {
    var logger: FileProviderLogger?

    @IBOutlet private weak var optionsTitleTextField: NSTextField!
    @IBOutlet private weak var shareRecipientTextField: NSTextField!  // Hide if public link share
    @IBOutlet private weak var labelTextField: NSTextField!
    @IBOutlet private weak var uploadEditPermissionCheckbox: NSButton!
    @IBOutlet private weak var hideDownloadCheckbox: NSButton!
    @IBOutlet private weak var passwordProtectCheckbox: NSButton!
    @IBOutlet private weak var passwordSecureField: NSSecureTextField!
    @IBOutlet private weak var expirationDateCheckbox: NSButton!
    @IBOutlet private weak var expirationDatePicker: NSDatePicker!
    @IBOutlet private weak var noteForRecipientCheckbox: NSButton!
    @IBOutlet private weak var noteTextField: NSTextField!
    @IBOutlet private weak var saveButton: NSButton!
    @IBOutlet private weak var deleteButton: NSButton!
    @IBOutlet private weak var shareTypePicker: NSPopUpButton!

    // Share type picker options
    @IBOutlet private weak var publicLinkShareMenuItem: NSMenuItem!
    @IBOutlet private weak var userShareMenuItem: NSMenuItem!
    @IBOutlet private weak var groupShareMenuItem: NSMenuItem!
    @IBOutlet private weak var emailShareMenuItem: NSMenuItem!
    @IBOutlet private weak var federatedCloudShareMenuItem: NSMenuItem!
    @IBOutlet private weak var teamShare: NSMenuItem!
    @IBOutlet private weak var talkConversationShare: NSMenuItem!

    let kit = NextcloudKit.shared
    var account: Account? {
        didSet {
            logger?.info("Setting up account.")

            guard let account else {
                logger?.error("Could not configure suggestions data source.")
                return
            }

            guard let controller else {
                return
            }

            suggestionsTextFieldDelegate.suggestionsDataSource = ShareeSuggestionsDataSource(account: account, kit: kit, log: controller.log)

            suggestionsTextFieldDelegate.confirmationHandler = { suggestion in
                guard let sharee = suggestion?.data as? NKSharee else {
                    return
                }

                self.shareRecipientTextField.stringValue = sharee.shareWith
                self.logger?.debug("Chose sharee \(sharee.shareWith)")
            }

            suggestionsTextFieldDelegate.targetTextField = shareRecipientTextField
        }
    }
    var dataSource: ShareTableViewDataSource?
    var controller: ShareController? {
        didSet {
            guard controller != nil else { return }
            optionsTitleTextField.stringValue = String(localized: "Share options")
            deleteButton.title = String(localized: "Delete")
            deleteButton.image = NSImage(systemSymbolName: "trash", accessibilityDescription: String(localized: "Delete trash icon"))
            deleteButton.bezelColor = NSColor.systemRed
            cancellable?.cancel()
            createMode = false
            update()
            cancellable = controller.publisher.sink { _ in self.update() }
        }
    }
    var createMode = false {
        didSet {
            logger?.info("Create mode set: \(self.createMode)")
            shareTypePicker.isHidden = !createMode
            shareRecipientTextField.isHidden = !createMode
            labelTextField.isHidden = createMode  // Cannot set label on create API call
            guard createMode else { return }
            optionsTitleTextField.stringValue = String(localized: "Create new share")
            deleteButton.title = String(localized: "Cancel")
            deleteButton.image = NSImage(
                systemSymbolName: "xmark.bin", accessibilityDescription: String(localized: "Cancel create icon")
            )
            deleteButton.bezelColor = NSColor.controlColor
            cancellable?.cancel()
            cancellable = nil
            controller = nil
            reset()
            setupCreateForm()
        }
    }
    private var cancellable: AnyCancellable?
    private var suggestionsWindowController = SuggestionsWindowController()
    private var suggestionsTextFieldDelegate = SuggestionsTextFieldDelegate()

    func applyLocalizedStrings() {
        publicLinkShareMenuItem.title = String(localized: "Public link share")
        userShareMenuItem.title = String(localized: "User share")
        groupShareMenuItem.title = String(localized: "Group share")
        emailShareMenuItem.title = String(localized: "Email share")
        federatedCloudShareMenuItem.title = String(localized: "Federated cloud share")
        teamShare.title = String(localized: "Team share")
        talkConversationShare.title = String(localized: "Talk conversation share")

        shareRecipientTextField.placeholderString = String(localized: "Share recipient")
        labelTextField.placeholderString = String(localized: "Share label")
        uploadEditPermissionCheckbox.title = String(localized: "Allow upload and editing")
        hideDownloadCheckbox.title = String(localized: "Hide download")
        passwordProtectCheckbox.title = String(localized: "Password protect")
        passwordSecureField.placeholderString = String(localized: "Enter a new password")
        expirationDateCheckbox.title = String(localized: "Expiration date")
        noteForRecipientCheckbox.title = String(localized: "Note for the recipient")
        noteTextField.placeholderString = String(localized: "Note for the recipient")

        deleteButton.title = String(localized: "Delete")
        saveButton.title = String(localized: "Save")
    }

    private func update() {
        guard let controller else {
            reset()
            setAllFields(enabled: false)
            saveButton.isEnabled = false
            deleteButton.isEnabled = false
            return
        }

        logger = FileProviderLogger(category: "ShareOptionsView", log: controller.log)
        let share = controller.share

        deleteButton.isEnabled = share.canDelete
        saveButton.isEnabled = share.canEdit

        setAllFields(enabled: share.canEdit)
        reset()

        shareRecipientTextField.stringValue = share.shareWithDisplayname
        labelTextField.stringValue = share.label
        uploadEditPermissionCheckbox.state = share.shareesCanEdit ? .on : .off
        hideDownloadCheckbox.state = share.hideDownload ? .on : .off
        passwordProtectCheckbox.state = share.password.isEmpty ? .off : .on
        passwordSecureField.isHidden = passwordProtectCheckbox.state == .off
        passwordSecureField.stringValue = share.password
        expirationDateCheckbox.state = share.expirationDate == nil ? .off : .on
        expirationDatePicker.isHidden = expirationDateCheckbox.state == .off
        expirationDatePicker.dateValue = share.expirationDate as? Date ?? Date()
        noteForRecipientCheckbox.state = share.note.isEmpty ? .off : .on
        noteTextField.isHidden = noteForRecipientCheckbox.state == .off
        noteForRecipientCheckbox.stringValue = share.note
    }

    private func reset() {
        shareRecipientTextField.stringValue = ""
        labelTextField.stringValue = ""
        uploadEditPermissionCheckbox.state = .off
        hideDownloadCheckbox.state = .off
        passwordProtectCheckbox.state = .off
        passwordSecureField.isHidden = true
        passwordSecureField.stringValue = ""
        expirationDateCheckbox.state = .off
        expirationDatePicker.isHidden = true
        expirationDatePicker.dateValue = NSDate.now
        expirationDatePicker.minDate = NSDate.now
        expirationDatePicker.maxDate = nil
        noteForRecipientCheckbox.state = .off
        noteTextField.isHidden = true
        noteTextField.stringValue = ""
    }

    private func setupCreateForm() {
        guard createMode else { return }

        setAllFields(enabled: true)

        let type = pickedShareType()
        shareRecipientTextField.isHidden = type == .publicLink

        if let caps = dataSource?.capabilities?.filesSharing {
            uploadEditPermissionCheckbox.state =
                caps.defaultPermissions & NKShare.PermissionValues.updateShare.rawValue != 0
                ? .on : .off

            switch type {
            case .publicLink:
                passwordProtectCheckbox.isHidden = false
                passwordProtectCheckbox.state = caps.publicLink?.passwordEnforced == true ? .on : .off
                passwordProtectCheckbox.isEnabled = caps.publicLink?.passwordEnforced == false
                expirationDateCheckbox.state = caps.publicLink?.expireDateEnforced == true ? .on : .off
                expirationDateCheckbox.isEnabled = caps.publicLink?.expireDateEnforced == false
                expirationDatePicker.dateValue = Date(
                    timeIntervalSinceNow: 
                        TimeInterval((caps.publicLink?.expireDateDays ?? 1) * 24 * 60 * 60)
                )
                if caps.publicLink?.expireDateEnforced == true {
                    expirationDatePicker.maxDate = expirationDatePicker.dateValue
                }
            case .email:
                passwordProtectCheckbox.isHidden = caps.email?.passwordEnabled == false
                passwordProtectCheckbox.state = caps.email?.passwordEnforced == true ? .on : .off
            default:
                passwordProtectCheckbox.isHidden = true
                passwordProtectCheckbox.state = .off
                break
            }
        }

        passwordSecureField.isHidden = passwordProtectCheckbox.state == .off
        expirationDatePicker.isHidden = expirationDateCheckbox.state == .off
    }

    private func setAllFields(enabled: Bool) {
        shareTypePicker.isEnabled = enabled
        shareRecipientTextField.isEnabled = enabled
        labelTextField.isEnabled = enabled
        uploadEditPermissionCheckbox.isEnabled = enabled
        hideDownloadCheckbox.isEnabled = enabled
        passwordProtectCheckbox.isEnabled = enabled
        passwordSecureField.isEnabled = enabled
        expirationDateCheckbox.isEnabled = enabled
        expirationDatePicker.isEnabled = enabled
        noteForRecipientCheckbox.isEnabled = enabled
        noteTextField.isEnabled = enabled
        saveButton.isEnabled = enabled
        deleteButton.isEnabled = enabled
    }

    private func pickedShareType() -> NKShare.ShareType {
        let selectedShareTypeItem = shareTypePicker.selectedItem
        var selectedShareType = NKShare.ShareType.publicLink
        if selectedShareTypeItem == publicLinkShareMenuItem {
            selectedShareType = .publicLink
        } else if selectedShareTypeItem == userShareMenuItem {
            selectedShareType = .user
        } else if selectedShareTypeItem == groupShareMenuItem {
            selectedShareType = .group
        } else if selectedShareTypeItem == emailShareMenuItem {
            selectedShareType = .email
        } else if selectedShareTypeItem == federatedCloudShareMenuItem {
            selectedShareType = .federatedCloud
        } else if selectedShareTypeItem == teamShare {
            selectedShareType = .team
        } else if selectedShareTypeItem == talkConversationShare {
            selectedShareType = .talkConversation
        }
        return selectedShareType
    }

    @IBAction func shareTypePickerAction(_ sender: Any) {
        if createMode {
            setupCreateForm()
        }
    }

    @IBAction func passwordCheckboxAction(_ sender: Any) {
        passwordSecureField.isHidden = passwordProtectCheckbox.state == .off
    }

    @IBAction func expirationDateCheckboxAction(_ sender: Any) {
        expirationDatePicker.isHidden = expirationDateCheckbox.state == .off
    }

    @IBAction func noteForRecipientCheckboxAction(_ sender: Any) {
        noteTextField.isHidden = noteForRecipientCheckbox.state == .off
    }

    @IBAction func save(_ sender: Any) {
        Task { @MainActor in
            let password = passwordProtectCheckbox.state == .on
                ? passwordSecureField.stringValue
                : ""
            let expireDate = expirationDateCheckbox.state == .on
                ? NKShare.formattedDateString(date: expirationDatePicker.dateValue)
                : ""
            let note = noteForRecipientCheckbox.state == .on
                ? noteTextField.stringValue
                : ""
            let label = labelTextField.stringValue
            let hideDownload = hideDownloadCheckbox.state == .on
            let uploadAndEdit = uploadEditPermissionCheckbox.state == .on

            guard !createMode else {
                logger?.info("Creating new share!")

                guard let dataSource,
                      let account,
                      let itemServerRelativePath = dataSource.itemServerRelativePath
                else {
                    logger?.error("Cannot create new share due to missing data. dataSource: \(String(describing: self.dataSource)) account: \(self.account != nil) path: \(self.dataSource?.itemServerRelativePath ?? "")")
                    return
                }

                let selectedShareType = pickedShareType()
                let shareWith = shareRecipientTextField.stringValue

                var permissions = NKShare.PermissionValues.all.rawValue
                permissions = uploadAndEdit
                    ? permissions | NKShare.PermissionValues.updateShare.rawValue
                    : permissions & ~NKShare.PermissionValues.updateShare.rawValue

                setAllFields(enabled: false)
                deleteButton.isEnabled = false
                saveButton.isEnabled = false
                let error = await ShareController.create(
                    account: account,
                    kit: kit,
                    shareType: selectedShareType,
                    itemServerRelativePath: itemServerRelativePath,
                    shareWith: shareWith,
                    password: password,
                    expireDate: expireDate,
                    permissions: permissions,
                    note: note,
                    label: label,
                    hideDownload: hideDownload
                )
                if let error = error, error != .success {
                    dataSource.uiDelegate?.showError(String(localized: "Error creating: \(error.errorDescription)"))
                    setAllFields(enabled: true)
                } else {
                    dataSource.uiDelegate?.hideOptions(self)
                    await dataSource.reload()
                }
                return
            }

            logger?.info("Editing existing share!")

            guard let controller = controller else {
                logger?.error("No valid share controller, cannot edit share.")
                return
            }
            let share = controller.share
            let permissions = uploadAndEdit
                ? share.permissions | NKShare.PermissionValues.updateShare.rawValue
                : share.permissions & ~NKShare.PermissionValues.updateShare.rawValue

            setAllFields(enabled: false)
            deleteButton.isEnabled = false
            saveButton.isEnabled = false
            let error = await controller.save(
                password: password,
                expireDate: expireDate,
                permissions: permissions,
                note: note,
                label: label,
                hideDownload: hideDownload
            )
            if let error = error, error != .success {
                dataSource?.uiDelegate?.showError("Error updating share: \(error.errorDescription)")
                setAllFields(enabled: true)
            } else {
                dataSource?.uiDelegate?.hideOptions(self)
                await dataSource?.reload()
            }
        }
    }

    @IBAction func delete(_ sender: Any) {
        Task { @MainActor in
            guard !createMode else {
                dataSource?.uiDelegate?.hideOptions(self)
                reset()
                return
            }

            setAllFields(enabled: false)
            deleteButton.isEnabled = false
            saveButton.isEnabled = false
            let error = await controller?.delete()
            if let error = error, error != .success {
                dataSource?.uiDelegate?.showError("Error deleting share: \(error.errorDescription)")
            }
            await dataSource?.reload()
        }
    }
}