123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- import Cocoa
- @objc class KMTools: NSObject {
-
-
-
- @objc class func getOpenDocumentURLs() -> [URL] {
- var files:[URL] = []
- for window in NSApp.windows {
- if ((window.windowController is KMBrowserWindowController) == false) {
- continue
- }
-
- let controller: KMBrowserWindowController = window.windowController as! KMBrowserWindowController
- let model = controller.browser?.tabStripModel
- guard let count = model?.count() else {
- continue
- }
-
- if (count <= 0) {
- continue
- }
-
- for i in 0 ..< count {
- let document = model?.tabContents(at: Int32(i))
- if (document?.fileURL == nil) {
- continue
- }
-
- if (document?.isHome == nil || document!.isHome) {
- continue
- }
-
- files.append((document?.fileURL)!)
- }
- }
- return files
- }
-
-
-
- private class func isDocumentHasPermissionsPassword(_ url: URL) -> Bool {
- let document = PDFDocument(url: url)
- if (document == nil) {
- return false
- }
-
- if (document?.permissionsStatus == .user) {
- return true
- }
-
-
- if (document!.isLocked == false) {
- return false
- }
-
-
- if (KMTools.hasPermissionsLimit(document!)) {
- return true
- }
-
- return false
- }
-
-
-
- @objc class func hasPermissionsLimit(_ document: PDFDocument) -> Bool {
- if (document.allowsCopying == false) {
- return true
- }
- if (document.allowsPrinting == false) {
- return true
- }
- return false
- }
-
-
-
-
- @objc class func openURL(_ url: URL) {
- NSWorkspace.shared.open(url)
- }
-
-
-
- @objc class func getAppVersion() -> String {
- let infoDictionary = Bundle.main.infoDictionary
- if (infoDictionary == nil) {
- return "1.0.0"
- }
-
- var version = infoDictionary!["CFBundleShortVersionString"]
- if (version != nil && (version is String) && (version as! String).isEmpty == false) {
- return version as! String
- }
-
- version = infoDictionary!["CFBundleVersion"]
- if (version != nil && (version is String) && (version as! String).isEmpty == false) {
- return version as! String
- }
-
- return "1.0.0"
- }
-
- class func getSystemVersion() -> (Int, Int, Int) {
- let versionInfo = ProcessInfo.processInfo.operatingSystemVersion
- return (versionInfo.majorVersion, versionInfo.minorVersion, versionInfo.patchVersion)
- }
-
- @objc class func isDefaultPDFReader() -> Bool {
- let app = LSCopyDefaultRoleHandlerForContentType("pdf" as CFString, LSRolesMask.all)?.takeUnretainedValue()
- if (app == nil) {
- return false
- }
-
- return (app! as String) == Bundle.main.bundleIdentifier!
- }
-
- @objc class func setDefaultPDFReader(_ isOrNo: Bool) -> Bool {
- var bid = "com.apple.Preview"
- if (isOrNo) {
- bid = Bundle.main.bundleIdentifier!
- }
- let status: OSStatus = LSSetDefaultRoleHandlerForContentType(KMTools.UTIforFileExtension("pdf") as CFString, LSRolesMask.all, bid as CFString)
- if (status == 0) {
- return true
- }
- return false
- }
-
- @objc class func UTIforFileExtension(_ exn: String) -> String {
- return (UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, exn as CFString, nil)?.takeUnretainedValue())! as String
- }
-
-
-
-
- @objc class func isFullScreen(_ window: NSWindow) -> Bool {
- return window.styleMask.contains(.fullScreen)
- }
-
-
-
-
- static let imageExtensions = ["jpg","cur","bmp","jpeg","gif","png","tiff","tif","ico","icns","tga","psd","eps","hdr","jp2","jpc","pict","sgi","heic"]
- static let pdfExtensions = ["pdf"]
- static let officeExtensions = ["doc", "docx", "xls", "xlsx", "ppt", "pptx"]
-
- @objc class func isImageType(_ exn: String) -> Bool {
- return KMTools.imageExtensions.contains(exn.lowercased())
- }
-
- @objc class func isPDFType(_ exn: String) -> Bool {
- return KMTools.pdfExtensions.contains(exn.lowercased())
- }
-
- @objc class func isOfficeType(_ exn: String) -> Bool {
- return KMTools.officeExtensions.contains(exn.lowercased())
- }
- }
- extension KMTools {
-
- @objc class func openQuickStartStudy() {
-
-
- var fileName = "PDF Master User Guide"
- let fileType = "pdf"
-
- let path = Bundle.main.path(forResource: fileName, ofType: fileType)
- if (path == nil || FileManager.default.fileExists(atPath: path!) == false) {
-
-
- KMTools.openURL(URL(string: "https://www.pdfreaderpro.com/help")!)
- return
- }
-
- let version = KMTools.getAppVersion()
- fileName.append(" v\(version).\(fileType)")
-
- let folderPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last?.appending("/\(Bundle.main.bundleIdentifier!)")
- if (FileManager.default.fileExists(atPath: folderPath!) == false) {
- try?FileManager.default.createDirectory(atPath: folderPath!, withIntermediateDirectories: false)
- }
-
- let toPath = "\(folderPath!)/\(fileName)"
- if (FileManager.default.fileExists(atPath: toPath)) {
- try?FileManager.default.removeItem(atPath: toPath)
- }
- try?FileManager.default.copyItem(atPath: path!, toPath: toPath)
-
- if !toPath.isPDFValid() {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
- alert.runModal()
- return
- }
- NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: toPath), display: true) { document, result, error in
- if (error != nil) {
- NSApp.presentError(error!)
- }
- }
- }
-
-
- @objc class func openFAQWebsite() {
- }
-
-
- @objc class func openMoreProductWebsite() {
-
-
- KMTools.openURL(URL(string: "https://www.pdfreaderpro.com/product?utm_source=MacApp&utm_campaign=ProductLink&utm_medium=PdfProduct")!)
- }
-
-
- @objc class func openFreePDFTemplatesWebsite() {
-
-
- KMTools.openURL(URL(string: "https://www.pdfreaderpro.com/templates?utm_source=MacApp&utm_campaign=TemplatesLink&utm_medium=PdfTemplates")!)
- }
-
-
- @objc class func openComPDFKitPowerWebsite() {
-
-
- KMTools.openURL(URL(string: "https://www.compdf.com/?utm_source=macapp&utm_medium=pdfmac&utm_campaign=compdfkit-promp")!)
- }
-
-
- @objc class func feekback() {
- let (major, minor, bugFix) = KMTools.getSystemVersion()
- let versionInfoString = "\(KMTools.getRawSystemInfo()) - \(major).\(minor).\(bugFix)"
-
- let appVersion = KMTools.getAppVersion()
- let appName = KMTools.getAppName()
- let subjects = "\(appName) - \(appVersion);\(NSLocalizedString("Propose a New Feature", comment: ""));\(versionInfoString)"
-
-
- let email = "support@pdfreaderpro.com"
-
-
-
- KMMailHelper.newEmail(withContacts: email, andSubjects: subjects)
- }
-
- @objc class func getRawSystemInfo() -> String {
- let info = GBDeviceInfo.deviceInfo().rawSystemInfoString
- if (info == nil) {
- return ""
- }
- return info!
- }
-
- @objc class func getAppName() -> String {
- let appTarget = KMTools_OC.getAppTarget()
- if (appTarget == .free) {
- return "PDF Master"
- } else if (appTarget == .pro) {
- return "PDF Master Pro"
- } else if (appTarget == .DMG) {
- return "PDF Master DMG"
- }
- return "PDF Master"
- }
-
- @objc class func pageRangeTypeString(pageRange: KMPageRange) -> String {
- switch pageRange {
- case .all:
- return NSLocalizedString("All Pages", comment: "")
- case .current:
- return NSLocalizedString("Current Page", comment: "")
- case .odd:
- return NSLocalizedString("Odd Pages", comment: "")
- case .even:
- return NSLocalizedString("Even Pages", comment: "")
- case .custom:
- return NSLocalizedString("Customize", comment: "")
- }
-
- return ""
- }
- }
|