NCManageDatabase+DirectEditing.swift 5.4 KB

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