NCManageDatabase+Capabilities.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // NCManageDatabase+Capabilities.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 29/05/23.
  6. // Copyright © 2023 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@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. import RealmSwift
  25. import NextcloudKit
  26. import SwiftyJSON
  27. class tableCapabilities: Object {
  28. @objc dynamic var account = ""
  29. @objc dynamic var jsondata: Data?
  30. override static func primaryKey() -> String {
  31. return "account"
  32. }
  33. }
  34. extension NCManageDatabase {
  35. func addCapabilitiesJSon(_ data: Data, account: String) {
  36. let realm = try! Realm()
  37. do {
  38. try realm.write {
  39. let addObject = tableCapabilities()
  40. addObject.account = account
  41. addObject.jsondata = data
  42. realm.add(addObject, update: .all)
  43. }
  44. } catch let error {
  45. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  46. }
  47. }
  48. func getCapabilities(account: String) -> String? {
  49. let realm = try! Realm()
  50. guard let result = realm.objects(tableCapabilities.self).filter("account == %@", account).first else {
  51. return nil
  52. }
  53. guard let jsondata = result.jsondata else {
  54. return nil
  55. }
  56. let json = JSON(jsondata)
  57. return json.rawString()?.replacingOccurrences(of: "\\/", with: "/")
  58. }
  59. @objc func getCapabilitiesServerString(account: String, elements: [String]) -> String? {
  60. let realm = try! Realm()
  61. guard let result = realm.objects(tableCapabilities.self).filter("account == %@", account).first else {
  62. return nil
  63. }
  64. guard let jsondata = result.jsondata else {
  65. return nil
  66. }
  67. let json = JSON(jsondata)
  68. return json[elements].string
  69. }
  70. func getCapabilitiesServerInt(account: String, elements: [String]) -> Int {
  71. let realm = try! Realm()
  72. guard let result = realm.objects(tableCapabilities.self).filter("account == %@", account).first,
  73. let jsondata = result.jsondata else {
  74. return 0
  75. }
  76. let json = JSON(jsondata)
  77. return json[elements].intValue
  78. }
  79. @objc func getCapabilitiesServerBool(account: String, elements: [String], exists: Bool) -> Bool {
  80. let realm = try! Realm()
  81. guard let result = realm.objects(tableCapabilities.self).filter("account == %@", account).first else {
  82. return false
  83. }
  84. guard let jsondata = result.jsondata else {
  85. return false
  86. }
  87. let json = JSON(jsondata)
  88. if exists {
  89. return json[elements].exists()
  90. } else {
  91. return json[elements].boolValue
  92. }
  93. }
  94. func getCapabilitiesServerArray(account: String, elements: [String]) -> [String]? {
  95. let realm = try! Realm()
  96. var resultArray: [String] = []
  97. guard let result = realm.objects(tableCapabilities.self).filter("account == %@", account).first else {
  98. return nil
  99. }
  100. guard let jsondata = result.jsondata else {
  101. return nil
  102. }
  103. let json = JSON(jsondata)
  104. if let results = json[elements].array {
  105. for result in results {
  106. resultArray.append(result.string ?? "")
  107. }
  108. return resultArray
  109. }
  110. return nil
  111. }
  112. }