NCShareCells.swift 15 KB

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