NCManageDatabase+DirectEditing.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //
  2. // NCManageDatabase+DirectEditing.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 13/11/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. class tableDirectEditingCreators: Object {
  27. @objc dynamic var account = ""
  28. @objc dynamic var editor = ""
  29. @objc dynamic var ext = ""
  30. @objc dynamic var identifier = ""
  31. @objc dynamic var mimetype = ""
  32. @objc dynamic var name = ""
  33. @objc dynamic var templates: Int = 0
  34. }
  35. class tableDirectEditingEditors: Object {
  36. @objc dynamic var account = ""
  37. @objc dynamic var editor = ""
  38. let mimetypes = List<String>()
  39. @objc dynamic var name = ""
  40. let optionalMimetypes = List<String>()
  41. @objc dynamic var secure: Int = 0
  42. }
  43. extension NCManageDatabase {
  44. func addDirectEditing(account: String, editors: [NKEditorDetailsEditors], creators: [NKEditorDetailsCreators]) {
  45. do {
  46. let realm = try Realm()
  47. try realm.write {
  48. let resultsCreators = realm.objects(tableDirectEditingCreators.self).filter("account == %@", account)
  49. realm.delete(resultsCreators)
  50. let resultsEditors = realm.objects(tableDirectEditingEditors.self).filter("account == %@", account)
  51. realm.delete(resultsEditors)
  52. for creator in creators {
  53. let addObject = tableDirectEditingCreators()
  54. addObject.account = account
  55. addObject.editor = creator.editor
  56. addObject.ext = creator.ext
  57. addObject.identifier = creator.identifier
  58. addObject.mimetype = creator.mimetype
  59. addObject.name = creator.name
  60. addObject.templates = creator.templates
  61. realm.add(addObject)
  62. }
  63. for editor in editors {
  64. let addObject = tableDirectEditingEditors()
  65. addObject.account = account
  66. for mimeType in editor.mimetypes {
  67. addObject.mimetypes.append(mimeType)
  68. }
  69. addObject.name = editor.name
  70. if editor.name.lowercased() == NCGlobal.shared.editorOnlyoffice {
  71. addObject.editor = NCGlobal.shared.editorOnlyoffice
  72. } else {
  73. addObject.editor = NCGlobal.shared.editorText
  74. }
  75. for mimeType in editor.optionalMimetypes {
  76. addObject.optionalMimetypes.append(mimeType)
  77. }
  78. addObject.secure = editor.secure
  79. realm.add(addObject)
  80. }
  81. }
  82. } catch let error {
  83. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  84. }
  85. }
  86. func getDirectEditingCreators(account: String) -> [tableDirectEditingCreators]? {
  87. do {
  88. let realm = try Realm()
  89. realm.refresh()
  90. let results = realm.objects(tableDirectEditingCreators.self).filter("account == %@", account)
  91. if results.isEmpty {
  92. return nil
  93. } else {
  94. return Array(results.map { tableDirectEditingCreators.init(value: $0) })
  95. }
  96. } catch let error as NSError {
  97. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access to database: \(error)")
  98. }
  99. return nil
  100. }
  101. func getDirectEditingCreators(predicate: NSPredicate) -> [tableDirectEditingCreators]? {
  102. do {
  103. let realm = try Realm()
  104. realm.refresh()
  105. let results = realm.objects(tableDirectEditingCreators.self).filter(predicate)
  106. if results.isEmpty {
  107. return nil
  108. } else {
  109. return Array(results.map { tableDirectEditingCreators.init(value: $0) })
  110. }
  111. } catch let error as NSError {
  112. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access to database: \(error)")
  113. }
  114. return nil
  115. }
  116. func getDirectEditingEditors(account: String) -> [tableDirectEditingEditors]? {
  117. do {
  118. let realm = try Realm()
  119. realm.refresh()
  120. let results = realm.objects(tableDirectEditingEditors.self).filter("account == %@", account)
  121. if results.isEmpty {
  122. return nil
  123. } else {
  124. return Array(results.map { tableDirectEditingEditors.init(value: $0) })
  125. }
  126. } catch let error as NSError {
  127. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access to database: \(error)")
  128. }
  129. return nil
  130. }
  131. }