NCShareCells.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. //
  2. // NCShareCells.swift
  3. // Nextcloud
  4. //
  5. // Created by Henrik Storch on 18.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 UIKit
  24. protocol NCShareCellConfig {
  25. var title: String { get }
  26. func getCell(for share: NCTableShareable) -> UITableViewCell
  27. func didSelect(for share: NCTableShareable)
  28. }
  29. protocol NCToggleCellConfig: NCShareCellConfig {
  30. func isOn(for share: NCTableShareable) -> Bool
  31. func didChange(_ share: NCTableShareable, to newValue: Bool)
  32. }
  33. extension NCToggleCellConfig {
  34. func getCell(for share: NCTableShareable) -> UITableViewCell {
  35. return NCShareToggleCell(isOn: isOn(for: share))
  36. }
  37. func didSelect(for share: NCTableShareable) {
  38. didChange(share, to: !isOn(for: share))
  39. }
  40. }
  41. protocol NCPermission: NCToggleCellConfig {
  42. static var forDirectory: [Self] { get }
  43. static var forDirectoryE2EE: [Self] { get }
  44. static var forFile: [Self] { get }
  45. func hasResharePermission(for parentPermission: Int) -> Bool
  46. func hasDownload() -> Bool
  47. }
  48. enum NCUserPermission: CaseIterable, NCPermission {
  49. func hasResharePermission(for parentPermission: Int) -> Bool {
  50. if self == .download { return true }
  51. return ((permissionBitFlag & parentPermission) != 0)
  52. }
  53. func hasDownload() -> Bool {
  54. return self == .download
  55. }
  56. var permissionBitFlag: Int {
  57. switch self {
  58. case .reshare: return NCGlobal.shared.permissionShareShare
  59. case .edit: return NCGlobal.shared.permissionUpdateShare
  60. case .create: return NCGlobal.shared.permissionCreateShare
  61. case .delete: return NCGlobal.shared.permissionDeleteShare
  62. case .download: return NCGlobal.shared.permissionDownloadShare
  63. }
  64. }
  65. func didChange(_ share: NCTableShareable, to newValue: Bool) {
  66. share.permissions ^= permissionBitFlag
  67. }
  68. func isOn(for share: NCTableShareable) -> Bool {
  69. if self == .download {
  70. return share.isAttributeDownloadEnabled
  71. } else {
  72. return (share.permissions & permissionBitFlag) != 0
  73. }
  74. }
  75. case reshare, edit, create, delete, download
  76. static let forDirectory: [NCUserPermission] = NCUserPermission.allCases
  77. static let forDirectoryE2EE: [NCUserPermission] = []
  78. static let forFile: [NCUserPermission] = [.reshare, .edit]
  79. var title: String {
  80. switch self {
  81. case .reshare: return NSLocalizedString("_share_can_reshare_", comment: "")
  82. case .edit: return NSLocalizedString("_share_can_change_", comment: "")
  83. case .create: return NSLocalizedString("_share_can_create_", comment: "")
  84. case .delete: return NSLocalizedString("_share_can_delete_", comment: "")
  85. case .download: return NSLocalizedString("_share_can_download_", comment: "")
  86. }
  87. }
  88. }
  89. enum NCLinkPermission: NCPermission {
  90. func didChange(_ share: NCTableShareable, to newValue: Bool) {
  91. guard self != .allowEdit || newValue else {
  92. share.permissions = NCGlobal.shared.permissionReadShare
  93. return
  94. }
  95. share.permissions = permissionValue
  96. }
  97. func hasResharePermission(for parentPermission: Int) -> Bool {
  98. permissionValue & parentPermission == permissionValue
  99. }
  100. func hasDownload() -> Bool {
  101. return true
  102. }
  103. var permissionValue: Int {
  104. switch self {
  105. case .allowEdit:
  106. return CCUtility.getPermissionsValue(
  107. byCanEdit: true,
  108. andCanCreate: true,
  109. andCanChange: true,
  110. andCanDelete: true,
  111. andCanShare: false,
  112. andIsFolder: false)
  113. case .viewOnly:
  114. return CCUtility.getPermissionsValue(
  115. byCanEdit: false,
  116. andCanCreate: false,
  117. andCanChange: false,
  118. andCanDelete: false,
  119. // not possible to create "read-only" shares without reshare option
  120. // https://github.com/nextcloud/server/blame/f99876997a9119518fe5f7ad3a3a51d33459d4cc/apps/files_sharing/lib/Controller/ShareAPIController.php#L1104-L1107
  121. andCanShare: true,
  122. andIsFolder: true)
  123. case .uploadEdit:
  124. return CCUtility.getPermissionsValue(
  125. byCanEdit: true,
  126. andCanCreate: true,
  127. andCanChange: true,
  128. andCanDelete: true,
  129. andCanShare: false,
  130. andIsFolder: true)
  131. case .fileDrop:
  132. return NCGlobal.shared.permissionCreateShare
  133. case .secureFileDrop:
  134. return NCGlobal.shared.permissionCreateShare
  135. }
  136. }
  137. func isOn(for share: NCTableShareable) -> Bool {
  138. switch self {
  139. case .allowEdit: return CCUtility.isAnyPermission(toEdit: share.permissions)
  140. case .viewOnly: return !CCUtility.isAnyPermission(toEdit: share.permissions) && share.permissions != NCGlobal.shared.permissionCreateShare
  141. case .uploadEdit: return CCUtility.isAnyPermission(toEdit: share.permissions) && share.permissions != NCGlobal.shared.permissionCreateShare
  142. case .fileDrop: return share.permissions == NCGlobal.shared.permissionCreateShare
  143. case .secureFileDrop: return share.permissions == NCGlobal.shared.permissionCreateShare
  144. }
  145. }
  146. var title: String {
  147. switch self {
  148. case .allowEdit: return NSLocalizedString("_share_can_change_", comment: "")
  149. case .viewOnly: return NSLocalizedString("_share_read_only_", comment: "")
  150. case .uploadEdit: return NSLocalizedString("_share_allow_upload_", comment: "")
  151. case .fileDrop: return NSLocalizedString("_share_file_drop_", comment: "")
  152. case .secureFileDrop: return NSLocalizedString("_share_secure_file_drop_", comment: "")
  153. }
  154. }
  155. case allowEdit, viewOnly, uploadEdit, fileDrop, secureFileDrop
  156. static let forDirectory: [NCLinkPermission] = [.viewOnly, .uploadEdit, .fileDrop]
  157. static let forFile: [NCLinkPermission] = [.allowEdit]
  158. static let forDirectoryE2EE: [NCLinkPermission] = [.secureFileDrop]
  159. }
  160. enum NCShareDetails: CaseIterable, NCShareCellConfig {
  161. func didSelect(for share: NCTableShareable) {
  162. switch self {
  163. case .hideDownload: share.hideDownload.toggle()
  164. case .expirationDate: return
  165. case .password: return
  166. case .note: return
  167. case .label: return
  168. }
  169. }
  170. func getCell(for share: NCTableShareable) -> UITableViewCell {
  171. switch self {
  172. case .hideDownload:
  173. return NCShareToggleCell(isOn: share.hideDownload)
  174. case .expirationDate:
  175. return NCShareDateCell(share: share)
  176. case .password: return NCShareToggleCell(isOn: !share.password.isEmpty, customIcons: ("lock", "lock_open"))
  177. case .note:
  178. let cell = UITableViewCell(style: .value1, reuseIdentifier: "shareNote")
  179. cell.detailTextLabel?.text = share.note
  180. cell.accessoryType = .disclosureIndicator
  181. return cell
  182. case .label:
  183. let cell = UITableViewCell(style: .value1, reuseIdentifier: "shareLabel")
  184. cell.detailTextLabel?.text = share.label
  185. return cell
  186. }
  187. }
  188. var title: String {
  189. switch self {
  190. case .hideDownload: return NSLocalizedString("_share_hide_download_", comment: "")
  191. case .expirationDate: return NSLocalizedString("_share_expiration_date_", comment: "")
  192. case .password: return NSLocalizedString("_share_password_protect_", comment: "")
  193. case .note: return NSLocalizedString("_share_note_recipient_", comment: "")
  194. case .label: return NSLocalizedString("_share_link_name_", comment: "")
  195. }
  196. }
  197. case label, hideDownload, expirationDate, password, note
  198. static let forLink: [NCShareDetails] = NCShareDetails.allCases
  199. static let forUser: [NCShareDetails] = [.expirationDate, .note]
  200. }
  201. struct NCShareConfig {
  202. let permissions: [NCPermission]
  203. let advanced: [NCShareDetails]
  204. let share: NCTableShareable
  205. let resharePermission: Int
  206. init(parentMetadata: tableMetadata, share: NCTableShareable) {
  207. self.share = share
  208. self.resharePermission = parentMetadata.sharePermissionsCollaborationServices
  209. let type: NCPermission.Type = share.shareType == NCShareCommon.shared.SHARE_TYPE_LINK ? NCLinkPermission.self : NCUserPermission.self
  210. self.permissions = parentMetadata.directory ? (parentMetadata.e2eEncrypted ? type.forDirectoryE2EE : type.forDirectory) : type.forFile
  211. self.advanced = share.shareType == NCShareCommon.shared.SHARE_TYPE_LINK ? NCShareDetails.forLink : NCShareDetails.forUser
  212. }
  213. func cellFor(indexPath: IndexPath) -> UITableViewCell? {
  214. let cellConfig = config(for: indexPath)
  215. let cell = cellConfig?.getCell(for: share)
  216. cell?.textLabel?.text = cellConfig?.title
  217. if let cellConfig = cellConfig as? NCPermission, !cellConfig.hasResharePermission(for: resharePermission), !cellConfig.hasDownload() {
  218. cell?.isUserInteractionEnabled = false
  219. cell?.textLabel?.isEnabled = false
  220. }
  221. return cell
  222. }
  223. func didSelectRow(at indexPath: IndexPath) {
  224. let cellConfig = config(for: indexPath)
  225. cellConfig?.didSelect(for: share)
  226. }
  227. func config(for indexPath: IndexPath) -> NCShareCellConfig? {
  228. if indexPath.section == 0, indexPath.row < permissions.count {
  229. return permissions[indexPath.row]
  230. } else if indexPath.section == 1, indexPath.row < advanced.count {
  231. return advanced[indexPath.row]
  232. } else { return nil }
  233. }
  234. }
  235. class NCShareToggleCell: UITableViewCell {
  236. typealias CustomToggleIcon = (onIconName: String?, offIconName: String?)
  237. init(isOn: Bool, customIcons: CustomToggleIcon? = nil) {
  238. super.init(style: .default, reuseIdentifier: "toggleCell")
  239. self.accessibilityValue = isOn ? NSLocalizedString("_on_", comment: "") : NSLocalizedString("_off_", comment: "")
  240. guard let customIcons = customIcons,
  241. let iconName = isOn ? customIcons.onIconName : customIcons.offIconName else {
  242. self.accessoryType = isOn ? .checkmark : .none
  243. return
  244. }
  245. let image = NCUtility.shared.loadImage(named: iconName, color: NCBrandColor.shared.brandElement, size: self.frame.height - 26)
  246. self.accessoryView = UIImageView(image: image)
  247. }
  248. required init?(coder: NSCoder) {
  249. fatalError("init(coder:) has not been implemented")
  250. }
  251. }
  252. class NCShareDateCell: UITableViewCell {
  253. let picker = UIDatePicker()
  254. let textField = UITextField()
  255. var shareType: Int
  256. var onReload: (() -> Void)?
  257. init(share: NCTableShareable) {
  258. self.shareType = share.shareType
  259. super.init(style: .value1, reuseIdentifier: "shareExpDate")
  260. picker.datePickerMode = .date
  261. picker.minimumDate = Date()
  262. picker.preferredDatePickerStyle = .wheels
  263. picker.action(for: .valueChanged) { datePicker in
  264. guard let datePicker = datePicker as? UIDatePicker else { return }
  265. self.detailTextLabel?.text = DateFormatter.shareExpDate.string(from: datePicker.date)
  266. }
  267. accessoryView = textField
  268. let toolbar = UIToolbar.toolbar {
  269. self.resignFirstResponder()
  270. share.expirationDate = nil
  271. self.onReload?()
  272. } completion: {
  273. self.resignFirstResponder()
  274. share.expirationDate = self.picker.date as NSDate
  275. self.onReload?()
  276. }
  277. textField.isAccessibilityElement = false
  278. textField.accessibilityElementsHidden = true
  279. textField.inputAccessoryView = toolbar.wrappedSafeAreaContainer
  280. textField.inputView = picker
  281. if let expDate = share.expirationDate {
  282. detailTextLabel?.text = DateFormatter.shareExpDate.string(from: expDate as Date)
  283. }
  284. }
  285. required public init?(coder: NSCoder) {
  286. fatalError("init(coder:) has not been implemented")
  287. }
  288. func checkMaximumDate(account: String) {
  289. let defaultExpDays = defaultExpirationDays(account: account)
  290. if defaultExpDays > 0 && isExpireDateEnforced(account: account) {
  291. let enforcedInSecs = TimeInterval(defaultExpDays * 24 * 60 * 60)
  292. self.picker.maximumDate = Date().advanced(by: enforcedInSecs)
  293. }
  294. }
  295. private func isExpireDateEnforced(account: String) -> Bool {
  296. switch self.shareType {
  297. case NCShareCommon.shared.SHARE_TYPE_LINK,
  298. NCShareCommon.shared.SHARE_TYPE_EMAIL,
  299. NCShareCommon.shared.SHARE_TYPE_GUEST:
  300. return NCGlobal.shared.capabilityFileSharingPubExpireDateEnforced
  301. case NCShareCommon.shared.SHARE_TYPE_USER,
  302. NCShareCommon.shared.SHARE_TYPE_GROUP,
  303. NCShareCommon.shared.SHARE_TYPE_CIRCLE,
  304. NCShareCommon.shared.SHARE_TYPE_ROOM:
  305. return NCGlobal.shared.capabilityFileSharingInternalExpireDateEnforced
  306. case NCShareCommon.shared.SHARE_TYPE_REMOTE,
  307. NCShareCommon.shared.SHARE_TYPE_REMOTE_GROUP:
  308. return NCGlobal.shared.capabilityFileSharingRemoteExpireDateEnforced
  309. default:
  310. return false
  311. }
  312. }
  313. private func defaultExpirationDays(account: String) -> Int {
  314. switch self.shareType {
  315. case NCShareCommon.shared.SHARE_TYPE_LINK,
  316. NCShareCommon.shared.SHARE_TYPE_EMAIL,
  317. NCShareCommon.shared.SHARE_TYPE_GUEST:
  318. return NCGlobal.shared.capabilityFileSharingPubExpireDateDays
  319. case NCShareCommon.shared.SHARE_TYPE_USER,
  320. NCShareCommon.shared.SHARE_TYPE_GROUP,
  321. NCShareCommon.shared.SHARE_TYPE_CIRCLE,
  322. NCShareCommon.shared.SHARE_TYPE_ROOM:
  323. return NCGlobal.shared.capabilityFileSharingInternalExpireDateDays
  324. case NCShareCommon.shared.SHARE_TYPE_REMOTE,
  325. NCShareCommon.shared.SHARE_TYPE_REMOTE_GROUP:
  326. return NCGlobal.shared.capabilityFileSharingRemoteExpireDateDays
  327. default:
  328. return 0
  329. }
  330. }
  331. }