NCShare+Menu.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 NextcloudKit
  25. extension NCShare {
  26. func toggleShareMenu(for share: tableShare) {
  27. var actions = [NCMenuAction]()
  28. if share.shareType == 3, canReshare {
  29. actions.append(
  30. NCMenuAction(
  31. title: NSLocalizedString("_share_add_sharelink_", comment: ""),
  32. icon: utility.loadImage(named: "plus", colors: [NCBrandColor.shared.iconImageColor]),
  33. action: { _ in
  34. self.makeNewLinkShare()
  35. }
  36. )
  37. )
  38. }
  39. actions.append(
  40. NCMenuAction(
  41. title: NSLocalizedString("_details_", comment: ""),
  42. icon: utility.loadImage(named: "pencil", colors: [NCBrandColor.shared.iconImageColor]),
  43. action: { _ in
  44. guard
  45. let advancePermission = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "NCShareAdvancePermission") as? NCShareAdvancePermission,
  46. let navigationController = self.navigationController, !share.isInvalidated else { return }
  47. advancePermission.networking = self.networking
  48. advancePermission.share = tableShare(value: share)
  49. advancePermission.oldTableShare = tableShare(value: share)
  50. advancePermission.metadata = self.metadata
  51. navigationController.pushViewController(advancePermission, animated: true)
  52. }
  53. )
  54. )
  55. actions.append(
  56. NCMenuAction(
  57. title: NSLocalizedString("_share_unshare_", comment: ""),
  58. destructive: true,
  59. icon: utility.loadImage(named: "trash", colors: [.red]),
  60. action: { _ in
  61. Task {
  62. if share.shareType != NCShareCommon().SHARE_TYPE_LINK, let metadata = self.metadata, metadata.e2eEncrypted && NCGlobal.shared.capabilityE2EEApiVersion == NCGlobal.shared.e2eeVersionV20 {
  63. let serverUrl = metadata.serverUrl + "/" + metadata.fileName
  64. if NCNetworkingE2EE().isInUpload(account: metadata.account, serverUrl: serverUrl) {
  65. let error = NKError(errorCode: NCGlobal.shared.errorE2EEUploadInProgress, errorDescription: NSLocalizedString("_e2e_in_upload_", comment: ""))
  66. return NCContentPresenter().showInfo(error: error)
  67. }
  68. let error = await NCNetworkingE2EE().uploadMetadata(account: metadata.account, serverUrl: serverUrl, userId: metadata.userId, addUserId: nil, removeUserId: share.shareWith)
  69. if error != .success {
  70. return NCContentPresenter().showError(error: error)
  71. }
  72. }
  73. self.networking?.unShare(idShare: share.idShare)
  74. }
  75. }
  76. )
  77. )
  78. self.presentMenu(with: actions)
  79. }
  80. func toggleUserPermissionMenu(isDirectory: Bool, tableShare: tableShare) {
  81. var actions = [NCMenuAction]()
  82. actions.append(
  83. NCMenuAction(
  84. title: NSLocalizedString("_share_read_only_", comment: ""),
  85. icon: utility.loadImage(named: "eye", colors: [NCBrandColor.shared.iconImageColor]),
  86. selected: tableShare.permissions == (NCGlobal.shared.permissionReadShare + NCGlobal.shared.permissionShareShare) || tableShare.permissions == NCGlobal.shared.permissionReadShare,
  87. on: false,
  88. action: { _ in
  89. let canShare = CCUtility.isPermission(toCanShare: tableShare.permissions)
  90. let permissions = CCUtility.getPermissionsValue(byCanEdit: false, andCanCreate: false, andCanChange: false, andCanDelete: false, andCanShare: canShare, andIsFolder: isDirectory)
  91. self.updateSharePermissions(share: tableShare, permissions: permissions)
  92. }
  93. )
  94. )
  95. actions.append(
  96. NCMenuAction(
  97. title: isDirectory ? NSLocalizedString("_share_allow_upload_", comment: "") : NSLocalizedString("_share_editing_", comment: ""),
  98. icon: utility.loadImage(named: "pencil", colors: [NCBrandColor.shared.iconImageColor]),
  99. selected: hasUploadPermission(tableShare: tableShare),
  100. on: false,
  101. action: { _ in
  102. let canShare = CCUtility.isPermission(toCanShare: tableShare.permissions)
  103. let permissions = CCUtility.getPermissionsValue(byCanEdit: true, andCanCreate: true, andCanChange: true, andCanDelete: true, andCanShare: canShare, andIsFolder: isDirectory)
  104. self.updateSharePermissions(share: tableShare, permissions: permissions)
  105. }
  106. )
  107. )
  108. self.presentMenu(with: actions)
  109. }
  110. fileprivate func hasUploadPermission(tableShare: tableShare) -> Bool {
  111. let uploadPermissions = [
  112. NCGlobal.shared.permissionMaxFileShare,
  113. NCGlobal.shared.permissionMaxFolderShare,
  114. NCGlobal.shared.permissionDefaultFileRemoteShareNoSupportShareOption,
  115. NCGlobal.shared.permissionDefaultFolderRemoteShareNoSupportShareOption]
  116. return uploadPermissions.contains(tableShare.permissions)
  117. }
  118. func updateSharePermissions(share: tableShare, permissions: Int) {
  119. let updatedShare = tableShare(value: share)
  120. updatedShare.permissions = permissions
  121. networking?.updateShare(option: updatedShare)
  122. }
  123. }