KMHeaderFooterManager.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. import Foundation
  2. let kHeaderFooterFolderPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("headerFooter")
  3. let kHeaderFooterPlistPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("headerFooter").stringByAppendingPathComponent("headerFooter2025.plist")
  4. let kHeaderFooterRemovedKey = "kHeaderFooterRemovedKey"
  5. private let kHeaderFooterInfoSaveKey = "kHeaderFooterInfoSaveKey"
  6. let kHeaderFooterManagerInit = "kHeaderFooterManagerInit"
  7. class KMHeaderFooterManager: NSObject {
  8. var headFooterObjects: [KMHeaderFooterObject] = []
  9. static let defaultManager = KMHeaderFooterManager()
  10. override init() {
  11. super.init()
  12. print("kHeaderFooterPlistPath = \(kHeaderFooterPlistPath ?? "")")
  13. if (FileManager.default.fileExists(atPath: kHeaderFooterPlistPath!)) {
  14. let dataDict = NSDictionary(contentsOfFile: kHeaderFooterPlistPath!)
  15. if (dataDict == nil) {
  16. return
  17. }
  18. for keyIndex in 0 ..< (dataDict?.allKeys.count)! {
  19. let key: String = dataDict?.allKeys[keyIndex] as! String
  20. let backgroundDict: NSDictionary = dataDict?.object(forKey: key) as! NSDictionary
  21. let model = parseDictionary(dict: backgroundDict)
  22. model.tag = key
  23. self.headFooterObjects.append(model)
  24. }
  25. }
  26. }
  27. func addHeaderFooter(_ obj: KMHeaderFooterObject) -> Bool {
  28. if (!FileManager.default.fileExists(atPath: kHeaderFooterFolderPath!)) {
  29. let create: ()? = try?FileManager.default.createDirectory(atPath: kHeaderFooterFolderPath!, withIntermediateDirectories: false)
  30. if (create == nil) {
  31. return false
  32. }
  33. }
  34. if (!FileManager.default.fileExists(atPath: kHeaderFooterPlistPath!)) {
  35. let create = try?FileManager.default.createFile(atPath: kHeaderFooterPlistPath!, contents: nil)
  36. if (create == nil) {
  37. return false
  38. }
  39. }
  40. let dict = NSDictionary(contentsOfFile: kHeaderFooterPlistPath!)
  41. var newDict:NSMutableDictionary!
  42. if (dict != nil) {
  43. newDict = NSMutableDictionary(dictionary: dict!)
  44. } else {
  45. newDict = NSMutableDictionary()
  46. }
  47. let backgroundDict = self.parseModel(model: obj)
  48. if (backgroundDict.isEmpty) {
  49. let alert = NSAlert()
  50. alert.alertStyle = .critical
  51. // alert.messageText = NSLocalizedString("文件\(obj.imagePath?.lastPathComponent)已损坏", comment: "")
  52. alert.runModal()
  53. return false
  54. }
  55. let tag = obj.tag
  56. newDict.addEntries(from: [tag : backgroundDict])
  57. let result = newDict.write(toFile: kHeaderFooterPlistPath!, atomically: true)
  58. if (result) {
  59. if (self.headFooterObjects.count < 1) {
  60. self.headFooterObjects.append(obj)
  61. } else {
  62. self.headFooterObjects.insert(obj, at: 0)
  63. }
  64. }
  65. return result
  66. }
  67. func removeHeaderFooter(_ obj: KMHeaderFooterObject) -> Bool {
  68. if (obj.tag.isEmpty) {
  69. return false
  70. }
  71. if (!FileManager.default.fileExists(atPath: kHeaderFooterPlistPath!)) {
  72. return false
  73. }
  74. let key: String = obj.tag
  75. let dictionary = NSDictionary(contentsOfFile: kHeaderFooterPlistPath!)
  76. var newDictionary: NSMutableDictionary!
  77. if (dictionary != nil) {
  78. newDictionary = NSMutableDictionary(dictionary: dictionary!)
  79. } else {
  80. newDictionary = NSMutableDictionary()
  81. }
  82. newDictionary.removeObject(forKey: key)
  83. let result = newDictionary.write(toFile: kHeaderFooterPlistPath!, atomically: true)
  84. if (result) {
  85. if (self.headFooterObjects.contains(obj)) {
  86. self.headFooterObjects.removeObject(obj)
  87. }
  88. }
  89. return result
  90. }
  91. func updateHeaderFooter(theModel: KMHeaderFooterObject) -> Bool {
  92. var flagModel: KMHeaderFooterObject!
  93. for model in self.headFooterObjects {
  94. if (model.tag == theModel.tag) {
  95. flagModel = model
  96. break
  97. }
  98. }
  99. if (flagModel == nil) {
  100. return false
  101. }
  102. let dict = NSDictionary(contentsOfFile: kHeaderFooterPlistPath!)
  103. var newDict:NSMutableDictionary!
  104. if (dict != nil) {
  105. newDict = NSMutableDictionary(dictionary: dict!)
  106. } else {
  107. newDict = NSMutableDictionary()
  108. }
  109. let watermarkDict = self.parseModel(model: theModel)
  110. if (watermarkDict.isEmpty) {
  111. let alert = NSAlert()
  112. alert.alertStyle = .critical
  113. // alert.messageText = NSLocalizedString("文件\(watermark.imagePath?.lastPathComponent)已损坏", comment: "")
  114. alert.runModal()
  115. return false
  116. }
  117. newDict.setObject(watermarkDict, forKey: flagModel.tag as NSCopying)
  118. let result = newDict.write(toFile: kHeaderFooterPlistPath!, atomically: true)
  119. if (result) {
  120. if let index = self.headFooterObjects.firstIndex(of: flagModel) {
  121. self.headFooterObjects[index] = theModel
  122. }
  123. }
  124. return result
  125. }
  126. func updateModel(_ model: KMHeaderFooterObject, with dict: NSDictionary) {
  127. model.fontName = dict.object(forKey: "fontName") as! String
  128. model.fontsize = dict.object(forKey: "fontsize") as! CGFloat
  129. if let value = dict.object(forKey: "color") {
  130. model.color = NSColor.km_init(hex: value as! String)
  131. }
  132. model.leftMargin = dict.object(forKey: "leftMargin") as! Int
  133. model.rightMargin = dict.object(forKey: "rightMargin") as! Int
  134. model.bottomMargin = dict.object(forKey: "bottomMargin") as! Int
  135. model.topMargin = dict.object(forKey: "topMargin") as! Int
  136. model.topLeftString = dict.object(forKey: "topLeftString") as! String
  137. model.topCenterString = dict.object(forKey: "topCenterString") as! String
  138. model.topRightString = dict.object(forKey: "topRightString") as! String
  139. model.bottomLeftString = dict.object(forKey: "bottomLeftString") as! String
  140. model.bottomCenterString = dict.object(forKey: "bottomCenterString") as! String
  141. model.bottomRightString = dict.object(forKey: "bottomRightString") as! String
  142. model.dateFormatString = dict.object(forKey: "dateFormatString") as! String
  143. model.pageFormatString = dict.object(forKey: "pageFormatString") as! String
  144. model.startString = dict.object(forKey: "startString") as! String
  145. model.name = dict.object(forKey: "name") as! String
  146. model.tag = dict.object(forKey: "tag") as! String
  147. }
  148. //Parse
  149. func parseModel(model: KMHeaderFooterObject) -> Dictionary<String, Any> {
  150. var dict: [String : Any] = [:]
  151. dict["fontName"] = model.fontName
  152. dict["fontsize"] = model.fontsize
  153. dict["color"] = model.color.toHex()
  154. dict["leftMargin"] = model.leftMargin
  155. dict["rightMargin"] = model.rightMargin
  156. dict["bottomMargin"] = model.bottomMargin
  157. dict["topMargin"] = model.topMargin
  158. dict["topLeftString"] = model.topLeftString
  159. dict["topCenterString"] = model.topCenterString
  160. dict["topRightString"] = model.topRightString
  161. dict["bottomLeftString"] = model.bottomLeftString
  162. dict["bottomCenterString"] = model.bottomCenterString
  163. dict["bottomRightString"] = model.bottomRightString
  164. dict["dateFormatString"] = model.dateFormatString
  165. dict["pageFormatString"] = model.pageFormatString
  166. dict["startString"] = model.startString
  167. dict["name"] = model.name
  168. dict["tag"] = model.tag
  169. return dict
  170. }
  171. private func parseDictionary(dict: NSDictionary) -> KMHeaderFooterObject {
  172. let model = KMHeaderFooterObject()
  173. model.fontName = dict.object(forKey: "fontName") as! String
  174. model.fontsize = dict.object(forKey: "fontsize") as! CGFloat
  175. if let value = dict.object(forKey: "color") {
  176. model.color = NSColor.km_init(hex: value as! String)
  177. }
  178. model.leftMargin = dict.object(forKey: "leftMargin") as! Int
  179. model.rightMargin = dict.object(forKey: "rightMargin") as! Int
  180. model.bottomMargin = dict.object(forKey: "bottomMargin") as! Int
  181. model.topMargin = dict.object(forKey: "topMargin") as! Int
  182. model.topLeftString = dict.object(forKey: "topLeftString") as! String
  183. model.topCenterString = dict.object(forKey: "topCenterString") as! String
  184. model.topRightString = dict.object(forKey: "topRightString") as! String
  185. model.bottomLeftString = dict.object(forKey: "bottomLeftString") as! String
  186. model.bottomCenterString = dict.object(forKey: "bottomCenterString") as! String
  187. model.bottomRightString = dict.object(forKey: "bottomRightString") as! String
  188. model.dateFormatString = dict.object(forKey: "dateFormatString") as! String
  189. model.pageFormatString = dict.object(forKey: "pageFormatString") as! String
  190. model.startString = dict.object(forKey: "startString") as! String
  191. model.name = dict.object(forKey: "name") as! String
  192. model.tag = dict.object(forKey: "tag") as! String
  193. return model
  194. }
  195. func fetchHeaderFooterAvailableName() -> String {
  196. var availableIndex = 0
  197. for item in headFooterObjects {
  198. if item.name.hasPrefix("HeaderFooter") {
  199. if let index = Int(item.name.dropFirst("HeaderFooter".count)), index >= availableIndex {
  200. availableIndex = index + 1
  201. }
  202. }
  203. }
  204. return "HeaderFooter\(availableIndex)"
  205. }
  206. }
  207. extension KMHeaderFooterManager {
  208. func fetchPlistData() -> [KMHeaderFooterObject] {
  209. return []
  210. }
  211. func savePlistData() {
  212. }
  213. }
  214. extension KMHeaderFooterManager {
  215. class func getdateFormatArray() -> [String] {
  216. return [
  217. "m/d",
  218. "m/d/yy",
  219. "m/d/yyyy",
  220. "mm/dd/yy",
  221. "mm/dd/yyyy",
  222. "d/m/yy",
  223. "d/m/yyyy",
  224. "dd/mm/yy",
  225. "dd/mm/yyyy",
  226. "mm/yy",
  227. "mm/yyyy",
  228. "m.d.yy",
  229. "m.d.yyyy",
  230. "mm.dd.yy",
  231. "mm.dd.yyyy",
  232. "mm.yy",
  233. "mm.yyyy",
  234. "d.m.yy",
  235. "d.m.yyyy",
  236. "dd.mm.yy",
  237. "dd.mm.yyyy",
  238. "yy-mm-dd",
  239. "yyyy-mm-dd"
  240. ]
  241. }
  242. class func getPageFormats() -> [String] {
  243. return ["1",
  244. "1 of n",
  245. "1/n",
  246. "Page 1",
  247. "Page 1 of n"]
  248. }
  249. class func getFontSize() -> [String] {
  250. return ["6","8","10","12","14",
  251. "16","18","20","22","24",
  252. "26","28","30","32","34",
  253. "36","40","48","64","80",
  254. "96","112"]
  255. }
  256. }