KMFileAttribute.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // KMFileAttribute.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/10/12.
  6. //
  7. import Cocoa
  8. @objcMembers class KMFileAttribute: NSObject {
  9. var filePath: String?
  10. var oriFilePath: String?
  11. var myPDFDocument: PDFDocument?
  12. var pdfDocument: CPDFDocument?
  13. var bAllPage = true
  14. private var _selectPages: [NSNumber] = []
  15. var pagesString: String = ""
  16. var isLocked = false
  17. var password: String?
  18. var pagesType: KMPageRange = .all
  19. /*
  20. @property (nonatomic, assign) BOOL pageRangeError;
  21. */
  22. func fetchSelectPages() -> [NSNumber] {
  23. let _ = self.isInvalidString(self.pagesString)
  24. if(!self.bAllPage) {
  25. self.QuickSort(&self._selectPages, startIndex: 0, endIndex: self._selectPages.count-1)
  26. }
  27. return self._selectPages
  28. }
  29. func isInvalidString(_ text: String) -> Bool {
  30. var document: PDFDocument?
  31. if (self.myPDFDocument != nil) {
  32. document = self.myPDFDocument
  33. } else {
  34. document = PDFDocument(url: URL(fileURLWithPath: self.filePath ?? ""))
  35. }
  36. if let data = document?.isLocked, data {
  37. document?.unlock(withPassword: self.password ?? "")
  38. }
  39. let pageNumber = document?.pageCount ?? 1
  40. if (self.bAllPage) {
  41. self._selectPages = []
  42. for i in 1 ... pageNumber {
  43. self._selectPages.append(NSNumber(value: i))
  44. }
  45. return false
  46. }
  47. var pageNumbers: [NSNumber] = []
  48. var isInvalid = false
  49. var c: unichar = 0
  50. for c in text {
  51. if (c != "0" && c != "1" && c != "2" && c != "3" && c != "4" && c != "5" && c != "6" && c != "7" && c != "8" && c != "9" && c != "," && c != "-") {
  52. isInvalid = true
  53. break
  54. }else{
  55. isInvalid = false
  56. }
  57. }
  58. if (!isInvalid) {
  59. let array = text.components(separatedBy: ",")
  60. for s in array {
  61. if s.isEmpty {
  62. isInvalid = true
  63. break
  64. }else{
  65. let pages = s.components(separatedBy: "-")
  66. if (pages.count>2) {
  67. isInvalid = true
  68. break
  69. }else if(pages.count==1){
  70. let p = pages.first!
  71. if p.isEmpty || Int(p)! > pageNumber || Int(p) == 0 {
  72. isInvalid = true
  73. break
  74. }else{
  75. var isEqual = false
  76. for pageNumber in pageNumbers {
  77. if pageNumber.stringValue == p {
  78. isEqual = true
  79. isInvalid = true
  80. break
  81. }
  82. }
  83. if (!isEqual) {
  84. pageNumbers.append(NSNumber(value: Int(p)!))
  85. }
  86. }
  87. }else if(pages.count==2){
  88. let p1 = pages[0]
  89. let p2 = pages[1]
  90. if p1.isEmpty || p2.isEmpty || Int(p1)! >= Int(p2)! || Int(p2)! > pageNumber || Int(p1) == 0 {
  91. isInvalid = true
  92. break
  93. }else{
  94. var isEqual = false
  95. for i in Int(p1)! ... Int(p2)! {
  96. for pageNumber in pageNumbers {
  97. if pageNumber.intValue == i {
  98. isEqual = true
  99. isInvalid = true
  100. break
  101. }
  102. }
  103. }
  104. if (!isEqual) {
  105. for i in Int(p1)! ... Int(p2)! {
  106. pageNumbers.append(NSNumber(value: i))
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }
  114. if (text.isEmpty) {
  115. isInvalid = true
  116. }
  117. if (isInvalid) {
  118. self._selectPages = []
  119. }else{
  120. self._selectPages = pageNumbers
  121. }
  122. return isInvalid
  123. }
  124. func QuickSort(_ list: inout [NSNumber],startIndex: Int, endIndex: Int) {
  125. if(startIndex >= endIndex) {
  126. return
  127. }
  128. let temp = list[startIndex]
  129. var tempIndex = startIndex
  130. for i in startIndex+1 ... endIndex {
  131. let t = list[i]
  132. if (temp.intValue > t.intValue) {
  133. tempIndex = tempIndex + 1
  134. let tmp = list[tempIndex]
  135. list[tempIndex] = list[i]
  136. list[i] = tmp
  137. }
  138. }
  139. let tmp = list[tempIndex]
  140. list[tempIndex] = list[startIndex]
  141. list[startIndex] = tmp
  142. self.QuickSort(&list, startIndex: startIndex, endIndex: tempIndex-1)
  143. self.QuickSort(&list, startIndex: tempIndex+1, endIndex: endIndex)
  144. }
  145. /*
  146. /* give our representation to the image browser */
  147. - (id)imageRepresentation
  148. {
  149. return self.filePath;
  150. }
  151. /* use the absolute filepath as identifier */
  152. - (NSString *)imageUID
  153. {
  154. return self.filePath;
  155. }
  156. - (NSString*)imageTitle
  157. {
  158. return [[self.filePath lastPathComponent] stringByDeletingPathExtension];
  159. }
  160. */
  161. }