NCShare+Menu.swift 5.9 KB

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