NCShare+Helper.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // NCShare+Helper.swift
  3. // Nextcloud
  4. //
  5. // Created by Henrik Storch on 19.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. import NextcloudKit
  25. extension tableShare: NCTableShareable { }
  26. extension NKShare: NCTableShareable { }
  27. protocol NCTableShareable: AnyObject {
  28. var shareType: Int { get set }
  29. var permissions: Int { get set }
  30. var idShare: Int { get set }
  31. var shareWith: String { get set }
  32. var hideDownload: Bool { get set }
  33. var password: String { get set }
  34. var label: String { get set }
  35. var note: String { get set }
  36. var expirationDate: NSDate? { get set }
  37. var shareWithDisplayname: String { get set }
  38. var attributes: String? { get set }
  39. }
  40. extension NCTableShareable {
  41. var expDateString: String? {
  42. guard let date = expirationDate else { return nil }
  43. let dateFormatter = DateFormatter()
  44. dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
  45. return dateFormatter.string(from: date as Date)
  46. }
  47. func hasChanges(comparedTo other: NCTableShareable) -> Bool {
  48. return other.shareType != shareType
  49. || other.permissions != permissions
  50. || other.hideDownload != hideDownload
  51. || other.password != password
  52. || other.label != label
  53. || other.note != note
  54. || other.expirationDate != expirationDate
  55. }
  56. }
  57. class NCTableShareOptions: NCTableShareable {
  58. var shareType: Int
  59. var permissions: Int
  60. var idShare: Int = 0
  61. var shareWith: String = ""
  62. var hideDownload: Bool = false
  63. var password: String = ""
  64. var label: String = ""
  65. var note: String = ""
  66. var expirationDate: NSDate?
  67. var shareWithDisplayname: String = ""
  68. var attributes: String?
  69. private init(shareType: Int, metadata: tableMetadata, password: String?) {
  70. if metadata.e2eEncrypted, NCCapabilities.shared.getCapabilities(account: metadata.account).capabilityE2EEApiVersion == NCGlobal.shared.e2eeVersionV12 {
  71. self.permissions = NCPermissions().permissionCreateShare
  72. } else {
  73. self.permissions = NCCapabilities.shared.getCapabilities(account: metadata.account).capabilityFileSharingDefaultPermission & metadata.sharePermissionsCollaborationServices
  74. }
  75. self.shareType = shareType
  76. if let password = password {
  77. self.password = password
  78. }
  79. }
  80. convenience init(sharee: NKSharee, metadata: tableMetadata, password: String?) {
  81. self.init(shareType: sharee.shareType, metadata: metadata, password: password)
  82. self.shareWith = sharee.shareWith
  83. }
  84. static func shareLink(metadata: tableMetadata, password: String?) -> NCTableShareOptions {
  85. return NCTableShareOptions(shareType: NCShareCommon().SHARE_TYPE_LINK, metadata: metadata, password: password)
  86. }
  87. }
  88. protocol NCShareDetail {
  89. var share: NCTableShareable! { get }
  90. }
  91. extension NCShareDetail where Self: UIViewController {
  92. func setNavigationTitle() {
  93. title = NSLocalizedString("_share_", comment: "") + " – "
  94. if share.shareType == NCShareCommon().SHARE_TYPE_LINK {
  95. title! += share.label.isEmpty ? NSLocalizedString("_share_link_", comment: "") : share.label
  96. } else {
  97. title! += share.shareWithDisplayname.isEmpty ? share.shareWith : share.shareWithDisplayname
  98. }
  99. }
  100. }