KMTools.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. //
  2. // KMTools.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/3/7.
  6. //
  7. import Cocoa
  8. @objc class KMTools: NSObject {
  9. // MARK: -
  10. // MARK: 获取已打开的文件
  11. @objc class func getOpenDocumentURLs() -> [URL] {
  12. var files:[URL] = []
  13. for window in NSApp.windows {
  14. if ((window.windowController is KMBrowserWindowController) == false) {
  15. continue
  16. }
  17. let controller: KMBrowserWindowController = window.windowController as! KMBrowserWindowController
  18. let model = controller.browser?.tabStripModel
  19. guard let count = model?.count() else {
  20. continue
  21. }
  22. if (count <= 0) {
  23. continue
  24. }
  25. for i in 0 ..< count {
  26. let document = model?.tabContents(at: Int32(i))
  27. // if (document?.windowControllers == nil || document?.windowControllers.count == 0) {
  28. // continue
  29. // }
  30. if (document?.fileURL == nil) {
  31. continue
  32. }
  33. if (document?.isHome == nil || document!.isHome) {
  34. continue
  35. }
  36. files.append((document?.fileURL)!)
  37. }
  38. }
  39. return files
  40. }
  41. // MARK: -
  42. // MARK: 无法区分 [权限+开启] [开启] 这两种情况 请不要使用
  43. private class func isDocumentHasPermissionsPassword(_ url: URL) -> Bool {
  44. let document = PDFDocument(url: url)
  45. if (document == nil) {
  46. return false
  47. }
  48. if (document?.permissionsStatus == .user) {
  49. return true
  50. }
  51. // document?.permissionsStatus == .none
  52. if (document!.isLocked == false) { // 没有加锁
  53. return false
  54. }
  55. // 已加锁 [权限+开启] [开启]
  56. if (KMTools.hasPermissionsLimit(document!)) { // 有权限限制
  57. return true
  58. }
  59. return false
  60. }
  61. // MARK: -
  62. // MARK: 暂时只处理了复制和打印两项(后续项目需求有新增时,可以再此方法里扩展)
  63. @objc class func hasPermissionsLimit(_ document: PDFDocument) -> Bool {
  64. if (document.allowsCopying == false) {
  65. return true
  66. }
  67. if (document.allowsPrinting == false) {
  68. return true
  69. }
  70. return false
  71. }
  72. // MARK: -
  73. // MARK: 打开网页
  74. @objc class func openURL(url: URL?) {
  75. guard let _url = url else {
  76. KMPrint("url invalid.")
  77. return
  78. }
  79. NSWorkspace.shared.open(_url)
  80. }
  81. @objc class func openURL(urlString: String?) {
  82. guard let _urlString = urlString else {
  83. KMPrint("url invalid.")
  84. return
  85. }
  86. KMTools.openURL(url: URL(string: _urlString))
  87. }
  88. // MARK: -
  89. // MARK: 获取 App 版本号
  90. @objc class func getAppVersion() -> String {
  91. let infoDictionary = Bundle.main.infoDictionary
  92. if (infoDictionary == nil) {
  93. return "1.0.0"
  94. }
  95. var version = infoDictionary!["CFBundleShortVersionString"]
  96. if (version != nil && (version is String) && (version as! String).isEmpty == false) {
  97. return version as! String
  98. }
  99. version = infoDictionary!["CFBundleVersion"]
  100. if (version != nil && (version is String) && (version as! String).isEmpty == false) {
  101. return version as! String
  102. }
  103. return "1.0.0"
  104. }
  105. class func getSystemVersion() -> (Int, Int, Int) {
  106. let versionInfo = ProcessInfo.processInfo.operatingSystemVersion
  107. return (versionInfo.majorVersion, versionInfo.minorVersion, versionInfo.patchVersion)
  108. }
  109. @objc class func isDefaultPDFReader() -> Bool {
  110. let app = LSCopyDefaultRoleHandlerForContentType("pdf" as CFString, LSRolesMask.all)?.takeUnretainedValue()
  111. if (app == nil) {
  112. return false
  113. }
  114. return (app! as String) == Bundle.main.bundleIdentifier!
  115. }
  116. @objc class func setDefaultPDFReader(_ isOrNo: Bool) -> Bool {
  117. var bid = "com.apple.Preview"
  118. if (isOrNo) {
  119. bid = Bundle.main.bundleIdentifier!
  120. }
  121. let status: OSStatus = LSSetDefaultRoleHandlerForContentType(KMTools.UTIforFileExtension("pdf") as CFString, LSRolesMask.all, bid as CFString)
  122. if (status == 0) {
  123. return true
  124. }
  125. return false
  126. }
  127. @objc class func UTIforFileExtension(_ exn: String) -> String {
  128. return (UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, exn as CFString, nil)?.takeUnretainedValue())! as String
  129. }
  130. // MARK: -
  131. // MARK: 是否全屏
  132. @objc class func isFullScreen(_ window: NSWindow) -> Bool {
  133. return window.styleMask.contains(.fullScreen)
  134. }
  135. // MARK: -
  136. // MARK: 文件类型
  137. static let imageExtensions = ["jpg","cur","bmp","jpeg","gif","png","tiff","tif",/*@"pic",*/"ico","icns","tga","psd","eps","hdr","jp2","jpc","pict","sgi","heic"]
  138. static let pdfExtensions = ["pdf"]
  139. static let officeExtensions = ["doc", "docx", "xls", "xlsx", "ppt", "pptx"]
  140. @objc class func isImageType(_ exn: String) -> Bool {
  141. return KMTools.imageExtensions.contains(exn.lowercased())
  142. }
  143. @objc class func isPDFType(_ exn: String) -> Bool {
  144. return KMTools.pdfExtensions.contains(exn.lowercased())
  145. }
  146. @objc class func isOfficeType(_ exn: String) -> Bool {
  147. return KMTools.officeExtensions.contains(exn.lowercased())
  148. }
  149. }
  150. // MARK: -
  151. // MARK: PDFMaster
  152. extension KMTools {
  153. // 打开 [快速教学]
  154. @objc class func openQuickStartStudy() {
  155. // MARK: -
  156. // MARK: 内嵌文档需要替换
  157. var fileName = "PDF Master User Guide"
  158. let fileType = "pdf"
  159. let path = Bundle.main.path(forResource: fileName, ofType: fileType)
  160. if (path == nil || FileManager.default.fileExists(atPath: path!) == false) {
  161. KMTools.openURL(url: URL(string: "https://www.pdfreaderpro.com/help"))
  162. return
  163. }
  164. let version = KMTools.getAppVersion()
  165. fileName.append(" v\(version).\(fileType)")
  166. let folderPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last?.appending("/\(Bundle.main.bundleIdentifier!)")
  167. if (FileManager.default.fileExists(atPath: folderPath!) == false) {
  168. try?FileManager.default.createDirectory(atPath: folderPath!, withIntermediateDirectories: false)
  169. }
  170. let toPath = "\(folderPath!)/\(fileName)"
  171. if (FileManager.default.fileExists(atPath: toPath)) {
  172. try?FileManager.default.removeItem(atPath: toPath)
  173. }
  174. try?FileManager.default.copyItem(atPath: path!, toPath: toPath)
  175. if !toPath.isPDFValid() {
  176. let alert = NSAlert()
  177. alert.alertStyle = .critical
  178. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  179. alert.runModal()
  180. return
  181. }
  182. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: toPath), display: true) { document, result, error in
  183. if (error != nil) {
  184. NSApp.presentError(error!)
  185. }
  186. }
  187. }
  188. // 打开 [FAQ] 网站
  189. @objc class func openFAQWebsite() {
  190. // KMTools.openURL(URL(string: "")!)
  191. }
  192. // 打开 [更多产品] 网站
  193. @objc class func openMoreProductWebsite() {
  194. KMTools.openURL(url: URL(string: "https://www.pdfreaderpro.com/product?utm_source=MacApp&utm_campaign=ProductLink&utm_medium=PdfProduct"))
  195. }
  196. // 打开 [免费 PDF 模板] 网站
  197. @objc class func openFreePDFTemplatesWebsite() {
  198. KMTools.openURL(url: URL(string: "https://www.pdfreaderpro.com/templates?utm_source=MacApp&utm_campaign=TemplatesLink&utm_medium=PdfTemplates"))
  199. }
  200. // 打开 [ComPDFKit 授权] 网站
  201. @objc class func openComPDFKitPowerWebsite() {
  202. KMTools.openURL(url: URL(string: "https://www.compdf.com/?utm_source=macapp&utm_medium=pdfmac&utm_campaign=compdfkit-promp"))
  203. }
  204. // 打开 [官网 下载页] 网站
  205. // 测试环境 http://test-pdf-pro.kdan.cn:3021/pdf-master-mac-download
  206. @objc class func openDownloadDMGWebsite() {
  207. KMTools.openURL(urlString: "https://www.pdfreaderpro.com/pdf-master-mac-download")
  208. }
  209. // 意见反馈
  210. @objc class func feekback() {
  211. let (major, minor, bugFix) = KMTools.getSystemVersion()
  212. let versionInfoString = "\(KMTools.getRawSystemInfo()) - \(major).\(minor).\(bugFix)"
  213. let appVersion = KMTools.getAppVersion()
  214. let appName = KMTools.getAppName()
  215. let subjects = "\(appName) - \(appVersion);\(NSLocalizedString("Propose a New Feature", comment: ""));\(versionInfoString)"
  216. // MARK: -
  217. // MARK TODO: 邮箱域名需要替换
  218. let email = "support@pdfreaderpro.com"
  219. // MARK: -
  220. // MARK TODO: 邮箱域名需要替换
  221. KMMailHelper.newEmail(withContacts: email, andSubjects: subjects)
  222. }
  223. @objc class func getRawSystemInfo() -> String {
  224. let info = GBDeviceInfo.deviceInfo().rawSystemInfoString
  225. if (info == nil) {
  226. return ""
  227. }
  228. return info!
  229. }
  230. @objc class func getAppName() -> String {
  231. let appTarget = KMTools_OC.getAppTarget()
  232. if (appTarget == .free) {
  233. return "PDF Master"
  234. } else if (appTarget == .pro) {
  235. return "PDF Master Pro"
  236. } else if (appTarget == .DMG) {
  237. // return "PDF Master DMG"
  238. return "PDF Master"
  239. }
  240. return "PDF Master"
  241. }
  242. @objc class func pageRangeTypeString(pageRange: KMPageRange) -> String {
  243. switch pageRange {
  244. case .all:
  245. return NSLocalizedString("All Pages", comment: "")
  246. case .current:
  247. return NSLocalizedString("Current Page", comment: "")
  248. case .odd:
  249. return NSLocalizedString("Odd Pages", comment: "")
  250. case .even:
  251. return NSLocalizedString("Even Pages", comment: "")
  252. case .custom:
  253. return NSLocalizedString("Customize", comment: "")
  254. }
  255. }
  256. }