NCShare+Helper.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. }
  39. extension NCTableShareable {
  40. var expDateString: String? {
  41. guard let date = expirationDate else { return nil }
  42. let dateFormatter = DateFormatter()
  43. dateFormatter.dateFormat = "YYYY-MM-dd HH:mm:ss"
  44. return dateFormatter.string(from: date as Date)
  45. }
  46. func hasChanges(comparedTo other: NCTableShareable) -> Bool {
  47. return other.shareType != shareType
  48. || other.permissions != permissions
  49. || other.hideDownload != hideDownload
  50. || other.password != password
  51. || other.label != label
  52. || other.note != note
  53. || other.expirationDate != expirationDate
  54. }
  55. }
  56. class NCTableShareOptions: NCTableShareable {
  57. var shareType: Int
  58. var permissions: Int
  59. var idShare: Int = 0
  60. var shareWith: String = ""
  61. var hideDownload: Bool = false
  62. var password: String = ""
  63. var label: String = ""
  64. var note: String = ""
  65. var expirationDate: NSDate?
  66. var shareWithDisplayname: String = ""
  67. private init(shareType: Int, metadata: tableMetadata, password: String?) {
  68. self.permissions = NCManageDatabase.shared.getCapabilitiesServerInt(account: metadata.account, elements: ["ocs", "data", "capabilities", "files_sharing", "default_permissions"]) & metadata.sharePermissionsCollaborationServices
  69. self.shareType = shareType
  70. if let password = password {
  71. self.password = password
  72. }
  73. }
  74. convenience init(sharee: NKSharee, metadata: tableMetadata, password: String?) {
  75. self.init(shareType: sharee.shareType, metadata: metadata, password: password)
  76. self.shareWith = sharee.shareWith
  77. }
  78. static func shareLink(metadata: tableMetadata, password: String?) -> NCTableShareOptions {
  79. return NCTableShareOptions(shareType: NCShareCommon.shared.SHARE_TYPE_LINK, metadata: metadata, password: password)
  80. }
  81. }
  82. protocol NCShareDetail {
  83. var share: NCTableShareable! { get }
  84. }
  85. extension NCShareDetail where Self: UIViewController {
  86. func setNavigationTitle() {
  87. title = NSLocalizedString("_share_", comment: "") + " – "
  88. if share.shareType == NCShareCommon.shared.SHARE_TYPE_LINK {
  89. title! += share.label.isEmpty ? NSLocalizedString("_share_link_", comment: "") : share.label
  90. } else {
  91. title! += share.shareWithDisplayname.isEmpty ? share.shareWith : share.shareWithDisplayname
  92. }
  93. }
  94. }