NCManageDatabase+Directory.swift 10 KB

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