SafeConfigManager.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2019-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. protocol SafeConfigManagerProtocol {
  22. func destroy()
  23. func getKey() -> [UInt8]?
  24. func setKey(_ value: [UInt8]?)
  25. func getCustomServer() -> String?
  26. func setCustomServer(_ value: String?)
  27. func getServer() -> String?
  28. func setServer(_ value: String?)
  29. func getMaxBackupBytes() -> Int?
  30. func setMaxBackupBytes(_ value: Int?)
  31. func getRetentionDays() -> Int?
  32. func setRetentionDays(_ value: Int?)
  33. func getBackupSize() -> Int64?
  34. func setBackupSize(_ value: Int64?)
  35. func getBackupStartedAt() -> Date?
  36. func setBackupStartedAt(_ value: Date?)
  37. func getLastBackup() -> Date?
  38. func setLastBackup(_ value: Date?)
  39. func getLastResult() -> String?
  40. func setLastResult(_ value: String?)
  41. func getLastChecksum() -> [UInt8]?
  42. func setLastChecksum(_ value: [UInt8]?)
  43. func getLastAlertBackupFailed() -> Date?
  44. func setLastAlertBackupFailed(_ value: Date?)
  45. func getIsTriggered() -> Bool
  46. func setIsTriggered(_ value: Bool)
  47. }
  48. @objc class SafeConfigManager: NSObject, SafeConfigManagerProtocol {
  49. private static let safeConfigMutaionLock: DispatchQueue = DispatchQueue(label: "safeConfigMutaionLock")
  50. private static var safeConfig: SafeData?
  51. //MARK: - safe config
  52. @objc public func destroy() {
  53. SafeConfigManager.safeConfig = nil
  54. }
  55. public func getKey() -> [UInt8]? {
  56. return getConfig().key
  57. }
  58. public func setKey(_ value: [UInt8]?) {
  59. let config = getConfig()
  60. config.key = value
  61. setConfig(config)
  62. }
  63. public func getCustomServer() -> String? {
  64. return getConfig().customServer
  65. }
  66. @objc public func setCustomServer(_ value: String?) {
  67. let config = getConfig()
  68. config.customServer = value
  69. setConfig(config)
  70. }
  71. public func getServer() -> String? {
  72. return getConfig().server
  73. }
  74. @objc public func setServer(_ value: String?) {
  75. let config = getConfig()
  76. config.server = value
  77. setConfig(config)
  78. }
  79. public func getMaxBackupBytes() -> Int? {
  80. return getConfig().maxBackupBytes
  81. }
  82. public func setMaxBackupBytes(_ value: Int?) {
  83. let config = getConfig()
  84. config.maxBackupBytes = value
  85. setConfig(config)
  86. }
  87. public func getRetentionDays() -> Int? {
  88. return getConfig().retentionDays
  89. }
  90. public func setRetentionDays(_ value: Int?) {
  91. let config = getConfig()
  92. config.retentionDays = value
  93. setConfig(config)
  94. }
  95. public func getBackupSize() -> Int64? {
  96. return getConfig().backupSize
  97. }
  98. public func setBackupSize(_ value: Int64?) {
  99. let config = getConfig()
  100. config.backupSize = value
  101. setConfig(config)
  102. }
  103. public func getBackupStartedAt() -> Date? {
  104. return getConfig().backupStartedAt
  105. }
  106. public func setBackupStartedAt(_ value: Date?) {
  107. let config = getConfig()
  108. config.backupStartedAt = value
  109. setConfig(config)
  110. }
  111. public func getLastBackup() -> Date? {
  112. return getConfig().lastBackup
  113. }
  114. public func setLastBackup(_ value: Date?) {
  115. let config = getConfig()
  116. config.lastBackup = value
  117. setConfig(config)
  118. }
  119. public func getLastResult() -> String? {
  120. return getConfig().lastResult
  121. }
  122. public func setLastResult(_ value: String?) {
  123. let config = getConfig()
  124. config.lastResult = value
  125. setConfig(config)
  126. }
  127. public func getLastChecksum() -> [UInt8]? {
  128. return getConfig().lastChecksum
  129. }
  130. public func setLastChecksum(_ value: [UInt8]?) {
  131. let config = getConfig()
  132. config.lastChecksum = value
  133. setConfig(config)
  134. }
  135. public func getLastAlertBackupFailed() -> Date? {
  136. return getConfig().lastAlertBackupFailed
  137. }
  138. public func setLastAlertBackupFailed(_ value: Date?) {
  139. let config = getConfig()
  140. config.lastAlertBackupFailed = value
  141. setConfig(config)
  142. }
  143. public func getIsTriggered() -> Bool {
  144. return getConfig().isTriggered != 0
  145. }
  146. public func setIsTriggered(_ value: Bool) {
  147. let config = getConfig()
  148. config.isTriggered = value ? 1 : 0
  149. setConfig(config)
  150. }
  151. private func getConfig() -> SafeData {
  152. SafeConfigManager.safeConfigMutaionLock.sync {
  153. if SafeConfigManager.safeConfig == nil {
  154. if let data = UserSettings.shared().safeConfig,
  155. data.count > 0 {
  156. SafeConfigManager.safeConfig = NSKeyedUnarchiver.unarchiveObject(with: data) as? SafeData
  157. } else {
  158. SafeConfigManager.safeConfig = SafeData(key: nil, customServer: nil, server: nil, maxBackupBytes: nil, retentionDays: nil, backupSize: nil, backupStartedAt: nil, lastBackup: nil, lastResult: nil, lastChecksum: nil, lastAlertBackupFailed: nil, isTriggered: 0)
  159. UserSettings.shared().safeConfig = NSKeyedArchiver.archivedData(withRootObject: SafeConfigManager.safeConfig as Any)
  160. }
  161. }
  162. }
  163. return SafeConfigManager.safeConfig!
  164. }
  165. private func setConfig(_ config: SafeData?) {
  166. SafeConfigManager.safeConfigMutaionLock.sync {
  167. if let data = config {
  168. UserSettings.shared().safeConfig = NSKeyedArchiver.archivedData(withRootObject: data)
  169. } else {
  170. UserSettings.shared().safeConfig = nil
  171. destroy()
  172. }
  173. }
  174. }
  175. }