Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,6 @@ import AppKit
import NextcloudKit

extension NKShare {
enum ShareType: Int {
case internalLink = -1
case user = 0
case group = 1
case publicLink = 3
case email = 4
case federatedCloud = 6
case team = 7
case talkConversation = 10
}

enum PermissionValues: Int {
case readShare = 1
case updateShare = 2
case createShare = 4
case deleteShare = 8
case shareShare = 16
case all = 31
}

var typeImage: NSImage? {
var image: NSImage?
switch shareType {
Expand Down Expand Up @@ -121,12 +101,15 @@ extension NKShare {
}

var shareesCanEdit: Bool {
get { (permissions & PermissionValues.updateShare.rawValue) != 0 }
get {
NKShare.Permission(rawValue: permissions).contains(.update)
}

set {
if newValue {
permissions |= NKShare.PermissionValues.updateShare.rawValue
permissions = NKShare.Permission(rawValue: permissions).union(.update).rawValue
} else {
permissions &= ~NKShare.PermissionValues.updateShare.rawValue
permissions = NKShare.Permission(rawValue: permissions).subtracting(.update).rawValue
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,49 +27,37 @@ class ShareController: ObservableObject {
shareWith: String?,
password: String? = nil,
expireDate: String? = nil,
permissions: Int = 1,
publicUpload: Bool = false,
permissions: NKShare.Permission,
publicUpload: Bool,
note: String? = nil,
label: String? = nil,
hideDownload: Bool,
attributes: String? = nil,
options: NKRequestOptions = NKRequestOptions()
) async -> NKError? {
return await withCheckedContinuation { continuation in
if shareType == .publicLink {
kit.createShare(
path: itemServerRelativePath,
shareType: ShareType.publicLink.rawValue,
shareWith: nil,
publicUpload: publicUpload,
hideDownload: hideDownload,
password: password,
permissions: permissions,
account: account.ncKitAccount,
options: options
) { account, share, data, error in
continuation.resume(returning: error)
}
} else {
guard let shareWith = shareWith else {
let error = NKError(statusCode: 0, fallbackDescription: "No recipient for share!")
continuation.resume(returning: error)
return
}
let sharee: String? = if shareType == .publicLink {
nil
} else {
shareWith
}

kit.createShare(
path: itemServerRelativePath,
shareType: shareType.rawValue,
shareWith: shareWith,
password: password,
permissions: permissions,
attributes: attributes,
account: account.ncKitAccount
) { account, share, data, error in
continuation.resume(returning: error)
}
}
if shareType != .publicLink, sharee == nil {
// Any share requires a recipient, except public links.
return NKError(statusCode: 0, fallbackDescription: "No recipient for share!")
}

let (_, _, _, error) = await kit.createShareAsync(
path: itemServerRelativePath,
shareType: shareType.rawValue,
shareWith: sharee,
hideDownload: hideDownload,
password: password,
permissions: permissions.rawValue,
account: account.ncKitAccount,
options: options
)

return error
}

init(share: NKShare, account: Account, kit: NextcloudKit, log: any FileProviderLogging) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ class ShareOptionsView: NSView {

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

suggestionsTextFieldDelegate.confirmationHandler = { suggestion in
guard let sharee = suggestion?.data as? NKSharee else {
return
suggestionsTextFieldDelegate.confirmationHandler = { [weak self] suggestion in
Task { @MainActor in
guard let sharee = suggestion?.data as? NKSharee else {
return
}
self?.shareRecipientTextField.stringValue = sharee.shareWith
self?.logger?.debug("Chose sharee \(sharee.shareWith)")
}

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

suggestionsTextFieldDelegate.targetTextField = shareRecipientTextField
Expand Down Expand Up @@ -190,7 +191,7 @@ class ShareOptionsView: NSView {

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

switch type {
Expand All @@ -201,7 +202,7 @@ class ShareOptionsView: NSView {
expirationDateCheckbox.state = caps.publicLink?.expireDateEnforced == true ? .on : .off
expirationDateCheckbox.isEnabled = caps.publicLink?.expireDateEnforced == false
expirationDatePicker.dateValue = Date(
timeIntervalSinceNow:
timeIntervalSinceNow:
TimeInterval((caps.publicLink?.expireDateDays ?? 1) * 24 * 60 * 60)
)
if caps.publicLink?.expireDateEnforced == true {
Expand Down Expand Up @@ -281,12 +282,15 @@ class ShareOptionsView: NSView {
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
Expand All @@ -305,14 +309,17 @@ class ShareOptionsView: NSView {
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
var permissions = NKShare.Permission.defaultPermission(for: selectedShareType)

if uploadAndEdit {
permissions.formUnion(.create)
permissions.formUnion(.update)
}

setAllFields(enabled: false)
deleteButton.isEnabled = false
saveButton.isEnabled = false

let error = await ShareController.create(
account: account,
kit: kit,
Expand All @@ -322,17 +329,20 @@ class ShareOptionsView: NSView {
password: password,
expireDate: expireDate,
permissions: permissions,
publicUpload: permissions.contains(.create),
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
}

Expand All @@ -342,14 +352,16 @@ class ShareOptionsView: NSView {
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
? share.permissions | NKShare.Permission.update.rawValue
: share.permissions & ~NKShare.Permission.update.rawValue

setAllFields(enabled: false)
deleteButton.isEnabled = false
saveButton.isEnabled = false

let error = await controller.save(
password: password,
expireDate: expireDate,
Expand All @@ -358,6 +370,7 @@ class ShareOptionsView: NSView {
label: label,
hideDownload: hideDownload
)

if let error = error, error != .success {
dataSource?.uiDelegate?.showError("Error updating share: \(error.errorDescription)")
setAllFields(enabled: true)
Expand Down Expand Up @@ -387,4 +400,3 @@ class ShareOptionsView: NSView {
}
}
}

Loading