NCManageDatabase+Directory.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 UIKit
  25. import RealmSwift
  26. import NextcloudKit
  27. class tableDirectory: Object {
  28. @objc dynamic var account = ""
  29. @objc dynamic var colorFolder: String?
  30. @objc dynamic var e2eEncrypted: Bool = false
  31. @objc dynamic var etag = ""
  32. @objc dynamic var favorite: Bool = false
  33. @objc dynamic var fileId = ""
  34. @objc dynamic var ocId = ""
  35. @objc dynamic var offline: Bool = false
  36. @objc dynamic var offlineDate: Date?
  37. @objc dynamic var permissions = ""
  38. @objc dynamic var richWorkspace: String?
  39. @objc dynamic var serverUrl = ""
  40. override static func primaryKey() -> String {
  41. return "ocId"
  42. }
  43. }
  44. extension NCManageDatabase {
  45. func addDirectory(e2eEncrypted: Bool, favorite: Bool, ocId: String, fileId: String, etag: String? = nil, permissions: String? = nil, richWorkspace: String? = nil, serverUrl: String, account: String) {
  46. do {
  47. let realm = try Realm()
  48. let result = realm.objects(tableDirectory.self).filter("account == %@ AND ocId == %@", account, ocId).first
  49. try realm.write {
  50. if let result {
  51. result.e2eEncrypted = e2eEncrypted
  52. result.favorite = favorite
  53. if let etag { result.etag = etag }
  54. if let permissions { result.permissions = permissions }
  55. if let richWorkspace { result.richWorkspace = richWorkspace }
  56. } else {
  57. let result = tableDirectory()
  58. result.e2eEncrypted = e2eEncrypted
  59. result.favorite = favorite
  60. result.ocId = ocId
  61. result.fileId = fileId
  62. if let etag { result.etag = etag }
  63. if let permissions { result.permissions = permissions }
  64. if let richWorkspace { result.richWorkspace = richWorkspace }
  65. result.serverUrl = serverUrl
  66. result.account = account
  67. realm.add(result, update: .modified)
  68. }
  69. }
  70. } catch let error {
  71. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  72. }
  73. }
  74. func deleteDirectoryAndSubDirectory(serverUrl: String, account: String) {
  75. #if !EXTENSION
  76. DispatchQueue.main.async {
  77. let windowScenes = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }
  78. for windowScene in windowScenes {
  79. if let controller = windowScene.keyWindow?.rootViewController as? NCMainTabBarController {
  80. controller.navigationCollectionViewCommon.remove(where: { $0.serverUrl == serverUrl})
  81. }
  82. }
  83. }
  84. #endif
  85. do {
  86. let realm = try Realm()
  87. let results = realm.objects(tableDirectory.self).filter("account == %@ AND serverUrl BEGINSWITH %@", account, serverUrl)
  88. try realm.write {
  89. for result in results {
  90. let metadatas = realm.objects(tableMetadata.self).filter("account == %@ AND serverUrl == %@", account, result.serverUrl)
  91. for metadata in metadatas {
  92. let localFile = realm.objects(tableLocalFile.self).filter("ocId == %@", metadata.ocId)
  93. realm.delete(localFile)
  94. }
  95. realm.delete(metadatas)
  96. }
  97. realm.delete(results)
  98. }
  99. } catch let error {
  100. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  101. }
  102. }
  103. func setDirectory(serverUrl: String, serverUrlTo: String? = nil, etag: String? = nil, ocId: String? = nil, fileId: String? = nil, encrypted: Bool, richWorkspace: String? = nil, account: String) {
  104. do {
  105. let realm = try Realm()
  106. try realm.write {
  107. guard let result = realm.objects(tableDirectory.self).filter("account == %@ AND serverUrl == %@", account, serverUrl).first else { return }
  108. let directory = tableDirectory.init(value: result)
  109. realm.delete(result)
  110. directory.e2eEncrypted = encrypted
  111. if let etag = etag {
  112. directory.etag = etag
  113. }
  114. if let ocId = ocId {
  115. directory.ocId = ocId
  116. }
  117. if let fileId = fileId {
  118. directory.fileId = fileId
  119. }
  120. if let serverUrlTo = serverUrlTo {
  121. directory.serverUrl = serverUrlTo
  122. }
  123. if let richWorkspace = richWorkspace {
  124. directory.richWorkspace = richWorkspace
  125. }
  126. realm.add(directory, update: .all)
  127. }
  128. } catch let error {
  129. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  130. }
  131. }
  132. func getTableDirectory(predicate: NSPredicate) -> tableDirectory? {
  133. do {
  134. let realm = try Realm()
  135. realm.refresh()
  136. guard let result = realm.objects(tableDirectory.self).filter(predicate).first else { return nil }
  137. return tableDirectory.init(value: result)
  138. } catch let error as NSError {
  139. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  140. }
  141. return nil
  142. }
  143. func getTableDirectory(account: String, serverUrl: String) -> tableDirectory? {
  144. do {
  145. let realm = try Realm()
  146. return realm.objects(tableDirectory.self).filter("account == %@ AND serverUrl == %@", account, serverUrl).first
  147. } catch let error as NSError {
  148. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  149. }
  150. return nil
  151. }
  152. func getTableDirectory(ocId: String) -> tableDirectory? {
  153. do {
  154. let realm = try Realm()
  155. realm.refresh()
  156. if let result = realm.objects(tableDirectory.self).filter("ocId == %@", ocId).first {
  157. return tableDirectory(value: result)
  158. } else {
  159. return nil
  160. }
  161. } catch let error as NSError {
  162. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  163. }
  164. return nil
  165. }
  166. func getTablesDirectory(predicate: NSPredicate, sorted: String, ascending: Bool) -> [tableDirectory]? {
  167. do {
  168. let realm = try Realm()
  169. realm.refresh()
  170. let results = realm.objects(tableDirectory.self).filter(predicate).sorted(byKeyPath: sorted, ascending: ascending)
  171. if results.isEmpty {
  172. return nil
  173. } else {
  174. return Array(results.map { tableDirectory.init(value: $0) })
  175. }
  176. } catch let error as NSError {
  177. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not access database: \(error)")
  178. }
  179. return nil
  180. }
  181. func renameDirectory(ocId: String, serverUrl: String) {
  182. do {
  183. let realm = try Realm()
  184. try realm.write {
  185. let result = realm.objects(tableDirectory.self).filter("ocId == %@", ocId).first
  186. result?.serverUrl = serverUrl
  187. }
  188. } catch let error {
  189. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  190. }
  191. }
  192. func setDirectory(serverUrl: String, offline: Bool, metadata: tableMetadata) {
  193. do {
  194. let realm = try Realm()
  195. try realm.write {
  196. if let result = realm.objects(tableDirectory.self).filter("account == %@ AND serverUrl == %@", metadata.account, serverUrl).first {
  197. result.offline = offline
  198. } else {
  199. let directory = tableDirectory()
  200. directory.account = metadata.account
  201. directory.serverUrl = serverUrl
  202. directory.offline = offline
  203. directory.e2eEncrypted = metadata.e2eEncrypted
  204. directory.favorite = metadata.favorite
  205. directory.fileId = metadata.fileId
  206. directory.ocId = metadata.ocId
  207. directory.permissions = metadata.permissions
  208. directory.richWorkspace = metadata.richWorkspace
  209. realm.add(directory, update: .all)
  210. }
  211. }
  212. } catch let error {
  213. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  214. }
  215. }
  216. func setDirectorySynchronizationDate(serverUrl: String, account: String) {
  217. do {
  218. let realm = try Realm()
  219. try realm.write {
  220. let result = realm.objects(tableDirectory.self).filter("account == %@ AND serverUrl == %@", account, serverUrl).first
  221. result?.offlineDate = Date()
  222. }
  223. } catch let error {
  224. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  225. }
  226. }
  227. func updateDirectoryRichWorkspace(_ richWorkspace: String?, account: String, serverUrl: String) {
  228. var result: tableDirectory?
  229. do {
  230. let realm = try Realm()
  231. try realm.write {
  232. result = realm.objects(tableDirectory.self).filter("account == %@ AND serverUrl == %@", account, serverUrl).first
  233. result?.richWorkspace = richWorkspace
  234. }
  235. } catch let error {
  236. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  237. }
  238. }
  239. func updateDirectoryColorFolder(_ colorFolder: String?, metadata: tableMetadata, serverUrl: String) {
  240. do {
  241. let realm = try Realm()
  242. try realm.write {
  243. if let result = realm.objects(tableDirectory.self).filter("account == %@ AND serverUrl == %@", metadata.account, serverUrl).first {
  244. result.colorFolder = colorFolder
  245. } else {
  246. let directory = tableDirectory()
  247. directory.account = metadata.account
  248. directory.serverUrl = serverUrl
  249. directory.colorFolder = colorFolder
  250. directory.e2eEncrypted = metadata.e2eEncrypted
  251. directory.favorite = metadata.favorite
  252. directory.fileId = metadata.fileId
  253. directory.ocId = metadata.ocId
  254. directory.permissions = metadata.permissions
  255. directory.richWorkspace = metadata.richWorkspace
  256. realm.add(directory)
  257. }
  258. }
  259. } catch let error {
  260. NextcloudKit.shared.nkCommonInstance.writeLog("[ERROR] Could not write to database: \(error)")
  261. }
  262. }
  263. }