NCManageDatabase+Directory.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //
  2. // NCManageDatabase+Directory.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 26/11/22.
  6. // Copyright © 2022 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 tableDirectory: Object {
  27. @objc dynamic var account = ""
  28. @objc dynamic var colorFolder: String?
  29. @objc dynamic var e2eEncrypted: Bool = false
  30. @objc dynamic var etag = ""
  31. @objc dynamic var favorite: Bool = false
  32. @objc dynamic var fileId = ""
  33. @objc dynamic var ocId = ""
  34. @objc dynamic var offline: Bool = false
  35. @objc dynamic var permissions = ""
  36. @objc dynamic var richWorkspace: String?
  37. @objc dynamic var serverUrl = ""
  38. override static func primaryKey() -> String {
  39. return "ocId"
  40. }
  41. }
  42. extension NCManageDatabase {
  43. func addDirectory(encrypted: Bool, favorite: Bool, ocId: String, fileId: String, etag: String? = nil, permissions: String? = nil, serverUrl: String, account: String) {
  44. do {
  45. let realm = try Realm()
  46. try realm.write {
  47. var addObject = tableDirectory()
  48. if let result = realm.objects(tableDirectory.self).filter("ocId == %@", ocId).first {
  49. addObject = result
  50. } else {
  51. addObject.ocId = ocId
  52. }
  53. addObject.account = account
  54. addObject.e2eEncrypted = encrypted
  55. addObject.favorite = favorite
  56. addObject.fileId = fileId
  57. if let etag = etag {
  58. addObject.etag = etag
  59. }
  60. if let permissions = permissions {
  61. addObject.permissions = permissions
  62. }
  63. addObject.serverUrl = serverUrl
  64. realm.add(addObject, update: .all)
  65. }
  66. } catch let error {
  67. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  68. }
  69. }
  70. func deleteDirectoryAndSubDirectory(serverUrl: String, account: String) {
  71. #if !EXTENSION
  72. DispatchQueue.main.async {
  73. if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
  74. appDelegate.listFilesVC[serverUrl] = nil
  75. }
  76. }
  77. #endif
  78. do {
  79. let realm = try Realm()
  80. let results = realm.objects(tableDirectory.self).filter("account == %@ AND serverUrl BEGINSWITH %@", account, serverUrl)
  81. for result in results {
  82. self.deleteMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", result.account, result.serverUrl))
  83. self.deleteLocalFile(predicate: NSPredicate(format: "ocId == %@", result.ocId))
  84. }
  85. try realm.write {
  86. realm.delete(results)
  87. }
  88. } catch let error {
  89. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  90. }
  91. }
  92. func setDirectory(serverUrl: String, serverUrlTo: String? = nil, etag: String? = nil, ocId: String? = nil, fileId: String? = nil, encrypted: Bool, richWorkspace: String? = nil, account: String) {
  93. do {
  94. let realm = try Realm()
  95. try realm.write {
  96. guard let result = realm.objects(tableDirectory.self).filter("account == %@ AND serverUrl == %@", account, serverUrl).first else { return }
  97. let directory = tableDirectory.init(value: result)
  98. realm.delete(result)
  99. directory.e2eEncrypted = encrypted
  100. if let etag = etag {
  101. directory.etag = etag
  102. }
  103. if let ocId = ocId {
  104. directory.ocId = ocId
  105. }
  106. if let fileId = fileId {
  107. directory.fileId = fileId
  108. }
  109. if let serverUrlTo = serverUrlTo {
  110. directory.serverUrl = serverUrlTo
  111. }
  112. if let richWorkspace = richWorkspace {
  113. directory.richWorkspace = richWorkspace
  114. }
  115. realm.add(directory, update: .all)
  116. }
  117. } catch let error {
  118. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  119. }
  120. }
  121. func cleanEtagDirectory(account: String, serverUrl: String) {
  122. do {
  123. let realm = try Realm()
  124. try realm.write {
  125. if let result = realm.objects(tableDirectory.self).filter("account == %@ AND serverUrl == %@", account, serverUrl).first {
  126. result.etag = ""
  127. }
  128. }
  129. } catch let error {
  130. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  131. }
  132. }
  133. func getTableDirectory(predicate: NSPredicate) -> tableDirectory? {
  134. do {
  135. let realm = try Realm()
  136. realm.refresh()
  137. guard let result = realm.objects(tableDirectory.self).filter(predicate).first else { return nil }
  138. return tableDirectory.init(value: result)
  139. } catch let error as NSError {
  140. NextcloudKit.shared.nkCommonInstance.writeLog("Could not access database: \(error)")
  141. }
  142. return nil
  143. }
  144. func getTableDirectory(account: String, serverUrl: String) -> tableDirectory? {
  145. do {
  146. let realm = try Realm()
  147. return realm.objects(tableDirectory.self).filter("account == %@ AND serverUrl == %@", account, serverUrl).first
  148. } catch let error as NSError {
  149. NextcloudKit.shared.nkCommonInstance.writeLog("Could not access database: \(error)")
  150. }
  151. return nil
  152. }
  153. func getTableDirectory(ocId: String) -> tableDirectory? {
  154. do {
  155. let realm = try Realm()
  156. realm.refresh()
  157. return realm.objects(tableDirectory.self).filter("ocId == %@", ocId).first
  158. } catch let error as NSError {
  159. NextcloudKit.shared.nkCommonInstance.writeLog("Could not access database: \(error)")
  160. }
  161. return nil
  162. }
  163. func getTablesDirectory(predicate: NSPredicate, sorted: String, ascending: Bool) -> [tableDirectory]? {
  164. do {
  165. let realm = try Realm()
  166. realm.refresh()
  167. let results = realm.objects(tableDirectory.self).filter(predicate).sorted(byKeyPath: sorted, ascending: ascending)
  168. if results.isEmpty {
  169. return nil
  170. } else {
  171. return Array(results.map { tableDirectory.init(value: $0) })
  172. }
  173. } catch let error as NSError {
  174. NextcloudKit.shared.nkCommonInstance.writeLog("Could not access database: \(error)")
  175. }
  176. return nil
  177. }
  178. func renameDirectory(ocId: String, serverUrl: String) {
  179. do {
  180. let realm = try Realm()
  181. try realm.write {
  182. let result = realm.objects(tableDirectory.self).filter("ocId == %@", ocId).first
  183. result?.serverUrl = serverUrl
  184. }
  185. } catch let error {
  186. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  187. }
  188. }
  189. func setDirectory(serverUrl: String, offline: Bool, account: String) {
  190. do {
  191. let realm = try Realm()
  192. try realm.write {
  193. let result = realm.objects(tableDirectory.self).filter("account == %@ AND serverUrl == %@", account, serverUrl).first
  194. result?.offline = offline
  195. }
  196. } catch let error {
  197. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  198. }
  199. }
  200. @discardableResult
  201. func setDirectory(serverUrl: String, richWorkspace: String?, account: String) -> tableDirectory? {
  202. var result: tableDirectory?
  203. do {
  204. let realm = try Realm()
  205. try realm.write {
  206. result = realm.objects(tableDirectory.self).filter("account == %@ AND serverUrl == %@", account, serverUrl).first
  207. result?.richWorkspace = richWorkspace
  208. }
  209. } catch let error {
  210. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  211. }
  212. if let result = result {
  213. return tableDirectory.init(value: result)
  214. } else {
  215. return nil
  216. }
  217. }
  218. @discardableResult
  219. func setDirectory(serverUrl: String, colorFolder: String?, account: String) -> tableDirectory? {
  220. var result: tableDirectory?
  221. do {
  222. let realm = try Realm()
  223. try realm.write {
  224. result = realm.objects(tableDirectory.self).filter("account == %@ AND serverUrl == %@", account, serverUrl).first
  225. result?.colorFolder = colorFolder
  226. }
  227. } catch let error {
  228. NextcloudKit.shared.nkCommonInstance.writeLog("Could not write to database: \(error)")
  229. }
  230. if let result = result {
  231. return tableDirectory.init(value: result)
  232. } else {
  233. return nil
  234. }
  235. }
  236. }