NCManageDatabase.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. //
  2. // NCManageDatabase.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 06/05/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. import RealmSwift
  9. class NCManageDatabase: NSObject {
  10. static let sharedInstance: NCManageDatabase = {
  11. let instance = NCManageDatabase()
  12. return instance
  13. }()
  14. override init() {
  15. let dirGroup = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: k_capabilitiesGroups)
  16. var config = Realm.Configuration()
  17. config.fileURL = dirGroup?.appendingPathComponent("\(appDatabaseNextcloud)/\(k_databaseDefault)")
  18. Realm.Configuration.defaultConfiguration = config
  19. }
  20. //MARK: -
  21. //MARK: Utility Database
  22. func clearTable(_ table : Object.Type, account: String?) {
  23. let results : Results<Object>
  24. let realm = try! Realm()
  25. if (account != nil) {
  26. results = realm.objects(table).filter("account = '\(account!)'")
  27. } else {
  28. results = realm.objects(table)
  29. }
  30. try! realm.write {
  31. realm.delete(results)
  32. }
  33. }
  34. func removeDB() {
  35. let realmURL = Realm.Configuration.defaultConfiguration.fileURL!
  36. let realmURLs = [
  37. realmURL,
  38. realmURL.appendingPathExtension("lock"),
  39. realmURL.appendingPathExtension("note"),
  40. realmURL.appendingPathExtension("management")
  41. ]
  42. for URL in realmURLs {
  43. do {
  44. try FileManager.default.removeItem(at: URL)
  45. } catch {
  46. // handle error
  47. }
  48. }
  49. }
  50. //MARK: -
  51. //MARK: Table Activity
  52. func addActivityServer(_ listOfActivity: [OCActivity], account: String) {
  53. let realm = try! Realm()
  54. try! realm.write {
  55. for activity in listOfActivity {
  56. // Verify
  57. let results = realm.objects(tableActivity.self).filter("idActivity = \(activity.idActivity)")
  58. if (results.count > 0) {
  59. continue
  60. }
  61. // Add new Activity
  62. let addActivity = tableActivity()
  63. addActivity.account = account
  64. addActivity.date = activity.date
  65. addActivity.idActivity = Double(activity.idActivity)
  66. addActivity.link = activity.link
  67. addActivity.note = activity.subject
  68. addActivity.type = k_activityTypeInfo
  69. realm.add(addActivity)
  70. }
  71. }
  72. }
  73. func addActivityClient(_ file: String, fileID: String, action: String, selector: String, note: String, type: String, verbose: Bool, account: String?, activeUrl: String?) {
  74. var noteReplacing : String = ""
  75. if (activeUrl != nil) {
  76. noteReplacing = note.replacingOccurrences(of: "\(activeUrl!)\(webDAV)", with: "")
  77. }
  78. noteReplacing = note.replacingOccurrences(of: "\(k_domain_session_queue).", with: "")
  79. let realm = try! Realm()
  80. try! realm.write {
  81. // Add new Activity
  82. let addActivity = tableActivity()
  83. if (account != nil) {
  84. addActivity.account = account!
  85. }
  86. addActivity.action = action
  87. addActivity.file = file
  88. addActivity.fileID = fileID
  89. addActivity.note = noteReplacing
  90. addActivity.selector = selector
  91. addActivity.type = type
  92. addActivity.verbose = verbose
  93. realm.add(addActivity)
  94. }
  95. }
  96. func getAllActivityWithPredicate(_ predicate: NSPredicate) -> [tableActivity] {
  97. let realm = try! Realm()
  98. let results = realm.objects(tableActivity.self).filter(predicate).sorted(byKeyPath: "date", ascending: false)
  99. return Array(results)
  100. }
  101. //MARK: -
  102. //MARK: Table Automatic Upload
  103. func addAutomaticUpload(_ metadataNet: CCMetadataNet, account: String) -> Bool {
  104. let realm = try! Realm()
  105. // Verify if exists
  106. let results = realm.objects(tableAutomaticUpload.self).filter("account = '\(account)' AND assetLocalIdentifier = '\(metadataNet.assetLocalIdentifier)'")
  107. if (results.count > 0) {
  108. return false
  109. }
  110. try! realm.write {
  111. // Add new AutomaticUpload
  112. let addAutomaticUpload = tableAutomaticUpload()
  113. addAutomaticUpload.account = account
  114. addAutomaticUpload.assetLocalIdentifier = metadataNet.assetLocalIdentifier
  115. addAutomaticUpload.fileName = metadataNet.fileName;
  116. addAutomaticUpload.selector = metadataNet.selector;
  117. addAutomaticUpload.selectorPost = metadataNet.selectorPost
  118. addAutomaticUpload.serverUrl = metadataNet.serverUrl
  119. addAutomaticUpload.session = metadataNet.session
  120. addAutomaticUpload.priority = metadataNet.priority
  121. realm.add(addAutomaticUpload)
  122. }
  123. return true
  124. }
  125. func getAutomaticUploadForAccount(_ account: String, selector: String) -> CCMetadataNet? {
  126. let realm = try! Realm()
  127. // Verify if exists
  128. let results = realm.objects(tableAutomaticUpload.self).filter("account = '\(account)' AND selector = '\(selector)' AND (lock == false)")
  129. if (results.count == 0) {
  130. return nil
  131. }
  132. let metadataNet = CCMetadataNet()
  133. metadataNet.action = actionUploadAsset
  134. metadataNet.assetLocalIdentifier = results[0].assetLocalIdentifier
  135. metadataNet.fileName = results[0].fileName
  136. metadataNet.priority = results[0].priority
  137. metadataNet.selector = results[0].selector
  138. metadataNet.selectorPost = results[0].selectorPost
  139. metadataNet.serverUrl = results[0].serverUrl
  140. metadataNet.session = results[0].session
  141. metadataNet.taskStatus = Int(k_taskStatusResume)
  142. // Lock True
  143. try! realm.write {
  144. results[0].lock = true
  145. }
  146. return metadataNet
  147. }
  148. func getAllLockAutomaticUploadForAccount(_ account: String) -> [tableAutomaticUpload] {
  149. let realm = try! Realm()
  150. let results = realm.objects(tableAutomaticUpload.self).filter("account = '\(account)' AND (lock = true)")
  151. return Array(results)
  152. }
  153. func unlockAutomaticUploadForAccount(_ account: String, assetLocalIdentifier: String) {
  154. let realm = try! Realm()
  155. let results = realm.objects(tableAutomaticUpload.self).filter("account = '\(account)' AND (assetLocalIdentifier = '\(assetLocalIdentifier)')")
  156. if (results.count > 0) {
  157. // Lock False
  158. try! realm.write {
  159. results[0].lock = false
  160. }
  161. }
  162. }
  163. func deleteAutomaticUploadForAccount(_ account: String, assetLocalIdentifier: String) {
  164. let realm = try! Realm()
  165. let results = realm.objects(tableAutomaticUpload.self).filter("account = '\(account)' AND (assetLocalIdentifier = '\(assetLocalIdentifier)')")
  166. if (results.count > 0) {
  167. try! realm.write {
  168. realm.delete(results)
  169. }
  170. }
  171. }
  172. func countAutomaticUploadForAccount(_ account: String, selector: String?) -> Int {
  173. let realm = try! Realm()
  174. let results : Results<tableAutomaticUpload>
  175. if (selector == nil) {
  176. results = realm.objects(tableAutomaticUpload.self).filter("account = '\(account)'")
  177. } else {
  178. results = realm.objects(tableAutomaticUpload.self).filter("account = '\(account)' AND (selector = '\(selector!)')")
  179. }
  180. return results.count
  181. }
  182. //MARK: -
  183. //MARK: Table Capabilities
  184. func addCapabilities(_ capabilities: OCCapabilities, account: String) {
  185. let realm = try! Realm()
  186. let results = realm.objects(tableCapabilities.self).filter("account = '\(account)'")
  187. try! realm.write {
  188. var resultCapabilities = tableCapabilities()
  189. if (results.count > 0) {
  190. resultCapabilities = results[0]
  191. }
  192. resultCapabilities.account = account
  193. resultCapabilities.themingBackground = capabilities.themingBackground
  194. resultCapabilities.themingColor = capabilities.themingColor
  195. resultCapabilities.themingLogo = capabilities.themingLogo
  196. resultCapabilities.themingName = capabilities.themingName
  197. resultCapabilities.themingSlogan = capabilities.themingSlogan
  198. resultCapabilities.themingUrl = capabilities.themingUrl
  199. resultCapabilities.versionMajor = capabilities.versionMajor
  200. resultCapabilities.versionMinor = capabilities.versionMinor
  201. resultCapabilities.versionMicro = capabilities.versionMicro
  202. resultCapabilities.versionString = capabilities.versionString
  203. if (results.count == 0) {
  204. realm.add(resultCapabilities)
  205. }
  206. }
  207. }
  208. func getCapabilitesForAccount(_ account: String) -> tableCapabilities? {
  209. let realm = try! Realm()
  210. let results = realm.objects(tableCapabilities.self).filter("account = '\(account)'")
  211. if (results.count > 0) {
  212. return results[0]
  213. } else {
  214. return nil
  215. }
  216. }
  217. func getServerVersionAccount(_ account: String) -> Int {
  218. let realm = try! Realm()
  219. let results = realm.objects(tableCapabilities.self).filter("account = '\(account)'")
  220. if (results.count > 0) {
  221. return results[0].versionMajor
  222. } else {
  223. return 0
  224. }
  225. }
  226. //MARK: -
  227. //MARK: Table Certificates
  228. func addCertificates(_ certificateLocation: String) {
  229. let realm = try! Realm()
  230. try! realm.write {
  231. let addCertificates = tableCertificates()
  232. addCertificates.certificateLocation = certificateLocation
  233. realm.add(addCertificates)
  234. }
  235. }
  236. func getAllCertificatesLocation(_ localCertificatesFolder: String) -> [String] {
  237. let realm = try! Realm()
  238. let results = realm.objects(tableCertificates.self)
  239. var arraycertificatePath = [String]()
  240. for result in results {
  241. arraycertificatePath.append("\(localCertificatesFolder)\(result.certificateLocation)")
  242. }
  243. return arraycertificatePath
  244. }
  245. //MARK: -
  246. //MARK: Table GPS
  247. func addGeocoderLocation(_ location: String, placemarkAdministrativeArea: String, placemarkCountry: String, placemarkLocality: String, placemarkPostalCode: String, placemarkThoroughfare: String, latitude: String, longitude: String) {
  248. let realm = try! Realm()
  249. // Verify if exists
  250. let results = realm.objects(tableGPS.self).filter("latitude = '\(latitude)' AND longitude = '\(longitude)'")
  251. if (results.count > 0) {
  252. return
  253. }
  254. try! realm.write {
  255. // Add new GPS
  256. let addGPS = tableGPS()
  257. addGPS.location = location
  258. addGPS.placemarkAdministrativeArea = placemarkAdministrativeArea
  259. addGPS.placemarkCountry = placemarkCountry
  260. addGPS.placemarkLocality = placemarkLocality
  261. addGPS.placemarkPostalCode = placemarkPostalCode
  262. addGPS.placemarkThoroughfare = placemarkThoroughfare
  263. addGPS.latitude = latitude
  264. addGPS.longitude = longitude
  265. realm.add(addGPS)
  266. }
  267. }
  268. func getLocationFromGeoLatitude(_ latitude: String, longitude: String) -> String? {
  269. let realm = try! Realm()
  270. let results = realm.objects(tableGPS.self).filter("latitude = '\(latitude)' AND longitude = '\(longitude)'")
  271. if (results.count == 0) {
  272. return nil
  273. } else {
  274. return results[0].location
  275. }
  276. }
  277. //MARK: -
  278. }