NCShare+Menu.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // NCShare+Menu.swift
  3. // Nextcloud
  4. //
  5. // Created by Henrik Storch on 16.03.22.
  6. // Copyright © 2022 Henrik Storch. All rights reserved.
  7. //
  8. // Author Henrik Storch <henrik.storch@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import UIKit
  25. import NextcloudKit
  26. extension NCShare {
  27. func toggleShareMenu(for share: tableShare) {
  28. let capabilities = NCCapabilities.shared.getCapabilities(account: self.metadata.account)
  29. var actions = [NCMenuAction]()
  30. if share.shareType == 3, canReshare {
  31. actions.append(
  32. NCMenuAction(
  33. title: NSLocalizedString("_share_add_sharelink_", comment: ""),
  34. icon: utility.loadImage(named: "plus", colors: [NCBrandColor.shared.iconImageColor]),
  35. action: { _ in
  36. self.makeNewLinkShare()
  37. }
  38. )
  39. )
  40. }
  41. actions.append(
  42. NCMenuAction(
  43. title: NSLocalizedString("_details_", comment: ""),
  44. icon: utility.loadImage(named: "pencil", colors: [NCBrandColor.shared.iconImageColor]),
  45. action: { _ in
  46. guard
  47. let advancePermission = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "NCShareAdvancePermission") as? NCShareAdvancePermission,
  48. let navigationController = self.navigationController, !share.isInvalidated else { return }
  49. advancePermission.networking = self.networking
  50. advancePermission.share = tableShare(value: share)
  51. advancePermission.oldTableShare = tableShare(value: share)
  52. advancePermission.metadata = self.metadata
  53. navigationController.pushViewController(advancePermission, animated: true)
  54. }
  55. )
  56. )
  57. actions.append(
  58. NCMenuAction(
  59. title: NSLocalizedString("_share_unshare_", comment: ""),
  60. destructive: true,
  61. icon: utility.loadImage(named: "person.2.slash"),
  62. action: { _ in
  63. Task {
  64. if share.shareType != NCShareCommon().SHARE_TYPE_LINK, let metadata = self.metadata, metadata.e2eEncrypted && capabilities.capabilityE2EEApiVersion == NCGlobal.shared.e2eeVersionV20 {
  65. let serverUrl = metadata.serverUrl + "/" + metadata.fileName
  66. if NCNetworkingE2EE().isInUpload(account: metadata.account, serverUrl: serverUrl) {
  67. let error = NKError(errorCode: NCGlobal.shared.errorE2EEUploadInProgress, errorDescription: NSLocalizedString("_e2e_in_upload_", comment: ""))
  68. return NCContentPresenter().showInfo(error: error)
  69. }
  70. let error = await NCNetworkingE2EE().uploadMetadata(serverUrl: serverUrl, addUserId: nil, removeUserId: share.shareWith, account: metadata.account)
  71. if error != .success {
  72. return NCContentPresenter().showError(error: error)
  73. }
  74. }
  75. self.networking?.unShare(idShare: share.idShare)
  76. }
  77. }
  78. )
  79. )
  80. self.presentMenu(with: actions)
  81. }
  82. func toggleUserPermissionMenu(isDirectory: Bool, tableShare: tableShare) {
  83. var actions = [NCMenuAction]()
  84. let permissions = NCPermissions()
  85. actions.append(
  86. NCMenuAction(
  87. title: NSLocalizedString("_share_read_only_", comment: ""),
  88. icon: utility.loadImage(named: "eye", colors: [NCBrandColor.shared.iconImageColor]),
  89. selected: tableShare.permissions == (permissions.permissionReadShare + permissions.permissionShareShare) || tableShare.permissions == permissions.permissionReadShare,
  90. on: false,
  91. action: { _ in
  92. let canShare = permissions.isPermissionToCanShare(tableShare.permissions)
  93. let permissions = permissions.getPermission(canEdit: false, canCreate: false, canChange: false, canDelete: false, canShare: canShare, isDirectory: isDirectory)
  94. self.updateSharePermissions(share: tableShare, permissions: permissions)
  95. }
  96. )
  97. )
  98. actions.append(
  99. NCMenuAction(
  100. title: isDirectory ? NSLocalizedString("_share_allow_upload_", comment: "") : NSLocalizedString("_share_editing_", comment: ""),
  101. icon: utility.loadImage(named: "pencil", colors: [NCBrandColor.shared.iconImageColor]),
  102. selected: hasUploadPermission(tableShare: tableShare),
  103. on: false,
  104. action: { _ in
  105. let canShare = permissions.isPermissionToCanShare(tableShare.permissions)
  106. let permissions = permissions.getPermission(canEdit: true, canCreate: true, canChange: true, canDelete: true, canShare: canShare, isDirectory: isDirectory)
  107. self.updateSharePermissions(share: tableShare, permissions: permissions)
  108. }
  109. )
  110. )
  111. self.presentMenu(with: actions)
  112. }
  113. fileprivate func hasUploadPermission(tableShare: tableShare) -> Bool {
  114. let permissions = NCPermissions()
  115. let uploadPermissions = [
  116. permissions.permissionMaxFileShare,
  117. permissions.permissionMaxFolderShare,
  118. permissions.permissionDefaultFileRemoteShareNoSupportShareOption,
  119. permissions.permissionDefaultFolderRemoteShareNoSupportShareOption]
  120. return uploadPermissions.contains(tableShare.permissions)
  121. }
  122. func updateSharePermissions(share: tableShare, permissions: Int) {
  123. let updatedShare = tableShare(value: share)
  124. updatedShare.permissions = permissions
  125. networking?.updateShare(option: updatedShare)
  126. }
  127. }