AppDelegate.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. //
  2. // AppDelegate.swift
  3. // PDF Master
  4. //
  5. // Created by Niehaoyu on 2022/9/23.
  6. //
  7. import Cocoa
  8. import KMAdvertisement
  9. @main
  10. class AppDelegate: NSObject, NSApplicationDelegate {
  11. func applicationWillBecomeActive(_ notification: Notification) {
  12. // NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMApplicationWillBecomeActive"), object: nil)
  13. }
  14. func applicationDidBecomeActive(_ notification: Notification) {
  15. }
  16. func applicationWillFinishLaunching(_ notification: Notification) {
  17. // CPDFKit.setLicenseKey("xUFaayt1VimDAZzI8rl5vjMf3HeLMrZpbQlFmxjZSSPCyawmiNOtoq+8tl8GityGbtdviwlZloQSHJUOtVXHSHprGFZbT87K7oYXMdQG1QIYXgCkwOcMzrtm1bF+LFzME+M7DccaTwMWnDcxJou5oRb7R2YNIjRyY5kfxBovkYw=", secret: "mG0c3O3Mzeu5dkZJW3gpqh188cTuhYlGRPrbR/gfX4r6S6mauFy6uswZqWk724t+6kpmKW2iaSG29olBx71C/OvIQk+6n3+LtOlO0cMNWyRhrp9sGpqfp0B228KI+IMTu4aGVjtYuk+Uxs/kosIBw1367/WkJ00tM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OZ3pnCWorSKPeQWqupmvSjMv7C2jvq4o0q6sQtN3jR4897KG6mIUGJSRuOsvjYroXP4neJlIQii5tjFEKC59L5S32wgJX2ceb3xBQSCnO9vtnsa41fJuMqWbPW2c3qJUemdtKP7E4eFYSl6tjgcetK")
  20. CPDFConvertKit.setLicenseKey("HcZ7sehqdnIvgtFnAF85D2B6tQjbMAklBEZyrSKYYSY/bdNOfGvcNIKpDTUchwAtZLw1fkJ3uN49bgUVSz8UEHm2LpoXPcRK9b+laQ17UHE7qFx6twVZ2IIyEURhMClmFSDD5yAxxpndYl8mmwjIoWspgAdDGX8pixFDQUE3/9w=", secret: "mG0c3O3Mzeu5dkZJW3gpqh188cTuhYlGRPrbR/gfX4qccr16WMBclnQACarlyblNUvAm/S8uFbhXMvxwgBSgULSsrbuyF/SIZ3I+dl0qU8GohiEHQBcAOj1ste3ppDvnC1Ca6fL/tThqwHk5EkhX8DeDWXm8oYxIQxh+yoOd+MxU9avJIxe/9rGTVfLJgjbpoOXUTNa/8X/QbDtDTanKkAwWgQy2MrGRWbL6WItb270=")
  21. // CDocumentAIKit.sharedInstance().setLicenseKey("1F8B081800599C640000616C7068610034413635373236463644363535393631364536370001F0010FFE33C3D4634F3E0B148EA10B8E59B57FA6B17EC34749CDBBD02D697F1A481C00666DCDDC517B4F576C4F8A9961080E556DF332DA7C816D3A3C212DCBE34749B6845E561A60C7E13BF8F84EC77FCF314333E1DFFC92E88A6CD97F65F6153452E9677F7A248FD45C98E87647367A29D678DA15EECB5E28854C674FE4DFE810740CB43F4DC1A7444B1EFA3C8C3E3F4D5859A9BAEDF1CF87917E86E6541B924A1AD0FBEC61BDFA0886A1E8FDFECCDE64FCCB645A89323F0177FE0B3CB7E3D72EA9A3DE1823FEB5D967F90751F46B6C97A1C250B669FD145747ADF6571859F78FDA05DA5B963B799DA5CC53ECE0FB8D53165EB3B0188FF8BB201D8AD7A34F01EC1BA855378F2CB8B9176EE463B1F2B1D8BD776113BAEF4C9A78EE7BC12EF192DABD2FBAB2E252A09B38A85BFCD76B4AFBEFF5A4E9FFE499A77B76E97B9C01CC7485688CD696FD1145D47AD969074FD300783583B70460617A0EC2D7579D0EC5AC33DB3F32E0CC920AC25B6D8F177D2F73FF26457534D6233844BEB4B223A37AA59EFB3D2469C8367C0CBF29C8A3E505AA53E62661FFC59771E77C3C274B51E2C066E87C796BA6AC2D2567D25560A7E9DFE3433FC5233A2C0CB4320D57EB8C0422B416CB5E25F94DB1CC0DCE1A39684827B61BCAE552A05607748113F3D6194A29A8C5C400A02FE3C69204D754550FA5E24BC3FF068B99DDF0010000")
  22. ComPDFUIConfig.initializeUIConfig();
  23. KMDesignToken.shared.parserExcel(withPath: "")
  24. }
  25. func applicationDidFinishLaunching(_ aNotification: Notification) {
  26. // Insert code here to initialize your application
  27. // MARK: 广告数据初始化
  28. KMAdvertisementManager.manager.initConfig(appName: .PDFReaderProMac,
  29. subscribeType: .unsubscribed,
  30. platform: .Mac)
  31. // 测试模式,默认false
  32. KMAdvertisementManager.manager.debug = true
  33. // 初版不支持暗黑模式
  34. NSApp.appearance = NSAppearance(named: .aqua)
  35. //第一次开启app
  36. var info = UserDefaults.standard.value(forKey: "kFirstOpenAppCount")
  37. var count = 0
  38. if info != nil {
  39. count = info as? Int ?? 0
  40. }
  41. count += 1
  42. UserDefaults.standard.set(count, forKey: "kFirstOpenAppCount")
  43. UserDefaults.standard.synchronize()
  44. self.initMainMenu()
  45. /// pro
  46. // [DBClientsManager setupWithAppKeyDesktop:@"amipzwtlg9lrwyl"];
  47. DBClientsManager.setup(withAppKeyDesktop: "jxjlqfm1cfp2mjj")
  48. reopenDocument(forPaths: [])
  49. KMPreferenceManager.shared.author = NSFullUserName()
  50. }
  51. func application(_ application: NSApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
  52. }
  53. func application(_ application: NSApplication, didReceiveRemoteNotification userInfo: [String : Any]) {
  54. }
  55. func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
  56. if !flag {
  57. let browser: KMBrowser = KMBrowser.init()
  58. browser.windowController = KMBrowserWindowController.init(browser: browser)
  59. browser.addHomeTabContents()
  60. browser.windowController.showWindow(self)
  61. }
  62. return flag
  63. }
  64. func applicationShouldOpenUntitledFile(_ sender: NSApplication) -> Bool {
  65. if (KMPreferenceManager.shared.openLastUnclosedDocumentWhenAppStart == false) {
  66. return false
  67. }
  68. let filepaths = KMPreferenceManager.shared.lastOpenFilepaths
  69. if (filepaths == nil || filepaths!.count == 0) {
  70. return false
  71. }
  72. DispatchQueue.main.async {
  73. for filepath in filepaths! {
  74. if filepath.isPDFValid() {
  75. NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: filepath), display: true) { document, result, error in
  76. }
  77. } else {
  78. let alert = NSAlert()
  79. alert.alertStyle = .critical
  80. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  81. alert.runModal()
  82. }
  83. }
  84. }
  85. return false
  86. }
  87. func applicationWillTerminate(_ aNotification: Notification) {
  88. if (NSApp.orderedDocuments.count > 0) {
  89. var filepaths: [String] = []
  90. for document in NSApp.orderedDocuments {
  91. // KMPrint(document.fileURL as Any)
  92. if (document.fileURL?.path != nil) {
  93. filepaths.append((document.fileURL?.path)!)
  94. }
  95. }
  96. KMPreferenceManager.shared.lastOpenFilepaths = filepaths
  97. } else {
  98. KMPreferenceManager.shared.lastOpenFilepaths = nil
  99. }
  100. }
  101. func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
  102. return true
  103. }
  104. // MARK: private
  105. func reopenDocument(forPaths paths: [String]) -> Void {
  106. let browser = KMBrowser.init() as KMBrowser
  107. browser.windowController = KMBrowserWindowController.init(browser: browser)
  108. browser.addHomeTabContents()
  109. browser.windowController.showWindow(self)
  110. }
  111. // MARK: Menu
  112. func initMainMenu() {
  113. // 处理菜单栏多语
  114. let mainMenu = NSApp.mainMenu
  115. if (mainMenu == nil) {
  116. return
  117. }
  118. for menu in mainMenu!.items {
  119. let subMenu = menu.submenu
  120. for item in subMenu!.items {
  121. item.title = NSLocalizedString(item.title, comment: "")
  122. let itemSubMenu = item.submenu
  123. if (itemSubMenu == nil) {
  124. continue
  125. }
  126. for item in itemSubMenu!.items {
  127. item.title = NSLocalizedString(item.title, comment: "")
  128. }
  129. }
  130. }
  131. let PDFOfficeMenu = mainMenu?.item(at: 0)?.submenu
  132. // VERSION_DMG=1
  133. // VERSION_FREE=1
  134. // VERSION_PRO=1
  135. #if VERSION_DMG
  136. PDFOfficeMenu?.insertItem(withTitle: NSLocalizedString("Check for Updates", comment: ""), action: #selector(checkForUpdates), target: self, at: 1)
  137. #endif
  138. let fileMenu = mainMenu?.item(at: 1)?.submenu
  139. let shareItem: NSMenuItem = NSMenuItem(title: NSLocalizedString("Share", comment: ""), action: nil, target: nil)
  140. fileMenu!.insertItem(shareItem, at: (fileMenu?.items.count)!-1)
  141. shareItem.submenu = NSSharingServicePicker.menu(forSharingItems: [""], subjectContext: "", withTarget: self, selector: #selector(sharePageItemAction), serviceDelegate: nil)
  142. let viewMenu = mainMenu?.item(at: 3)?.submenu
  143. var zoomMenu: NSMenu?
  144. for item in viewMenu!.items {
  145. if (item.title == NSLocalizedString("Zoom", comment: "")) {
  146. zoomMenu = item.submenu
  147. break
  148. }
  149. }
  150. if (zoomMenu != nil) {
  151. for item in zoomMenu!.items {
  152. if (item.title == NSLocalizedString("Zoom In", comment: "")) {
  153. item.keyEquivalent = "+"
  154. item.keyEquivalentModifierMask = [.command]
  155. break
  156. }
  157. }
  158. }
  159. }
  160. @objc func sharePageItemAction(sender: NSMenuItem) {
  161. if ((NSApp.mainWindow?.windowController is KMBrowserWindowController) == false) {
  162. return
  163. }
  164. let windowControler = NSApp.mainWindow?.windowController as! KMBrowserWindowController
  165. let model: CTTabStripModel = windowControler.browser.tabStripModel
  166. if (model.count() <= 0) {
  167. return
  168. }
  169. if (model.activeTabContents().isHome) {
  170. return
  171. }
  172. let document: KMMainDocument = model.activeTabContents() as! KMMainDocument
  173. let represent : NSSharingService = sender.representedObject as! NSSharingService
  174. represent.perform(withItems: [document.fileURL as Any])
  175. }
  176. func updateAccountMenus() {
  177. let mainMenu = NSApp.mainMenu
  178. let PDFOfficeMenu: NSMenu = (mainMenu?.item(at: 0)?.submenu)!
  179. var index: Int = 4
  180. #if VERSION_DMG
  181. index = 5
  182. #endif
  183. let isLogin = false
  184. if (isLogin) {
  185. PDFOfficeMenu.insertItem(withTitle: NSLocalizedString("user@mobile.com", comment: ""), action: #selector(showAccount), target: self, at: index)
  186. let manageAcctounItem = PDFOfficeMenu.item(at: index+1)
  187. manageAcctounItem?.title = NSLocalizedString("View my account", comment: "")
  188. PDFOfficeMenu.insertItem(withTitle: NSLocalizedString("Resume purchase", comment: ""), action: #selector(resumePurchase), target: self, at: index+2)
  189. let loginItem = PDFOfficeMenu.item(at: index+3)
  190. loginItem?.title = NSLocalizedString("Logout", comment: "")
  191. } else {
  192. var hasLoginItems = false
  193. for item in PDFOfficeMenu.items {
  194. if (item.action == #selector(showAccount) ||
  195. item.action == #selector(resumePurchase)) {
  196. hasLoginItems = true
  197. break
  198. }
  199. }
  200. if (hasLoginItems) {
  201. let loginItem = PDFOfficeMenu.item(at: index+3)
  202. loginItem?.title = NSLocalizedString("Log In", comment: "")
  203. PDFOfficeMenu.removeItem(at: index+2)
  204. let manageAcctounItem = PDFOfficeMenu.item(at: index+1)
  205. manageAcctounItem?.title = NSLocalizedString("Manage my account", comment: "")
  206. PDFOfficeMenu.removeItem(at: index)
  207. } else {
  208. let manageAcctounItem = PDFOfficeMenu.item(at: index+1)
  209. manageAcctounItem?.title = NSLocalizedString("Manage my account", comment: "")
  210. let loginItem = PDFOfficeMenu.item(at: index+2)
  211. loginItem?.title = NSLocalizedString("Log In", comment: "")
  212. }
  213. }
  214. }
  215. // MARK: PDF Master Menu
  216. @IBAction func openPreferenceWindow(_ sender: Any) {
  217. KMPreferenceWindowController.shared.showWindow(nil)
  218. }
  219. @objc @IBAction func manageAcctounMenuAction(_ sender: Any) {
  220. if KMLightMemberManager.manager.isLogin() {
  221. KMAccountInfoWindowController.show(window: NSApp.mainWindow!)
  222. }
  223. }
  224. @IBAction func loginMenuItemAction(_ sender: Any) {
  225. // Login & Logout
  226. if KMLightMemberManager.manager.isLogin() {
  227. } else {
  228. KMLoginWindowController.show(window: NSApp.mainWindow!)
  229. }
  230. }
  231. @IBAction func logoutMenuItemAction(_ sender: Any) {
  232. // Login & Logout
  233. if KMLightMemberManager.manager.isLogin() {
  234. KMRequestServerManager.manager.logout { success, result in
  235. if success {
  236. print("登出成功")
  237. }
  238. }
  239. }
  240. }
  241. @objc func showAccount() {
  242. }
  243. @objc func resumePurchase() {
  244. }
  245. // MARK: help Menu
  246. @IBAction func menuItemAction_quickStudy(_ sender: NSMenuItem) -> Void {
  247. KMTools.openQuickStartStudy()
  248. }
  249. @IBAction func menuItemAction_onlineHelp(_ sender: NSMenuItem) -> Void {
  250. KMTools.openFAQWebsite()
  251. }
  252. @IBAction func menuItemAction_morePudact(_ sender: NSMenuItem) -> Void {
  253. KMTools.openMoreProductWebsite()
  254. }
  255. @IBAction func menuItemAction_freePDFTemplate(_ sender: NSMenuItem) -> Void {
  256. KMTools.openFreePDFTemplatesWebsite()
  257. }
  258. @IBAction func menuItemAction_payEmail(_ sender: NSMenuItem) -> Void {
  259. KMEmailSubWindowController().showWindow(nil)
  260. }
  261. @IBAction func menuItemAction_poweredByComPDFKit(_ sender: NSMenuItem) -> Void {
  262. KMTools.openComPDFKitPowerWebsite()
  263. }
  264. @IBAction func menuItemAction_feekback(_ sender: NSMenuItem) -> Void {
  265. KMTools.feekback()
  266. }
  267. @IBAction func menuItemAction_giveFiveStar(_ sender: NSMenuItem) -> Void {
  268. }
  269. // MARK: DMG
  270. #if VERSION_DMG
  271. @objc func checkForUpdates(_ sender: NSMenuItem) {
  272. }
  273. #endif
  274. }
  275. extension AppDelegate : NSMenuDelegate,NSMenuItemValidation {
  276. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  277. let action = menuItem.action
  278. if action == #selector(manageAcctounMenuAction) {
  279. self.updateLoginMenuItem()
  280. return KMLightMemberManager.manager.isLogin()
  281. }
  282. if action == #selector(loginMenuItemAction) {
  283. self.updateLoginMenuItem()
  284. return !KMLightMemberManager.manager.isLogin()
  285. }
  286. if action == #selector(logoutMenuItemAction) {
  287. self.updateLoginMenuItem()
  288. return KMLightMemberManager.manager.isLogin()
  289. }
  290. return true
  291. }
  292. func updateLoginMenuItem() {
  293. let firstItem = NSApp.mainMenu?.item(at: 0)?.submenu
  294. if firstItem != nil {
  295. let item1 = firstItem?.item(withTag: 11)
  296. let item2 = firstItem?.item(withTag: 12)
  297. let item3 = firstItem?.item(withTag: 13)
  298. let item4 = firstItem?.item(withTag: 14)
  299. if KMLightMemberManager.manager.isLogin() {
  300. item1?.title = KMLightMemberManager.manager.info.email
  301. item1?.isHidden = false
  302. item2?.isHidden = true
  303. item3?.isHidden = false
  304. item4?.isHidden = false
  305. } else {
  306. item1?.isHidden = true
  307. item2?.isHidden = false
  308. item3?.isHidden = true
  309. item4?.isHidden = true
  310. }
  311. }
  312. }
  313. @objc func clickOutline(menuItem: NSMenuItem) {
  314. print("更新菜单栏")
  315. }
  316. func menuNeedsUpdate(_ menu: NSMenu) {
  317. print("menuNeedsUpdate")
  318. // menu.removeAllItems()
  319. //
  320. // var item = NSMenuItem()
  321. // item = menu.addItem(withTitle: NSLocalizedString("Insert", comment: ""), action: nil, target: self)
  322. // item.representedObject = row
  323. //
  324. // var subMenu = NSMenu()
  325. // var subitem = NSMenuItem()
  326. // subitem = subMenu.addItem(withTitle: NSLocalizedString("Insert File", comment: ""), action: #selector(insertPageItemAction), target: self, tag:0)
  327. // subitem.representedObject = row
  328. // subitem = subMenu.addItem(withTitle: NSLocalizedString("Insert blank page", comment: ""), action: #selector(insertPageItemAction), target: self, tag:1)
  329. // subitem.representedObject = row
  330. // subitem = subMenu.addItem(withTitle: NSLocalizedString("Insert page", comment: ""), action: #selector(insertPageItemAction), target: self, tag:2)
  331. // subitem.representedObject = row
  332. // item.submenu = subMenu
  333. //
  334. // item = menu.addItem(withTitle: NSLocalizedString("Extract", comment: ""), action: #selector(extractPageItemAction), target: self)
  335. // item.representedObject = row
  336. // item = menu.addItem(withTitle: NSLocalizedString("Delete Page", comment: ""), action: #selector(deletePageItemAction), target: self)
  337. // item.representedObject = row
  338. // item = menu.addItem(withTitle: NSLocalizedString("Page Edit", comment: ""), action: #selector(pageEditItemAction), target: self)
  339. // item.representedObject = row
  340. //
  341. // menu.addItem(NSMenuItem.separator())
  342. //
  343. // item = menu.addItem(withTitle: NSLocalizedString("90 Rotate", comment: ""), action: #selector(rotatePageItemAction), target: self)
  344. // item.representedObject = row
  345. // item = menu.addItem(withTitle: NSLocalizedString("-90 Rotate", comment: ""), action: #selector(leftRotatePageItemAction), target: self)
  346. // item.representedObject = row
  347. //
  348. // menu.addItem(NSMenuItem.separator())
  349. //
  350. // item = menu.addItem(withTitle: NSLocalizedString("copy", comment: ""), action: #selector(copyItemAction), target: self)
  351. // item.representedObject = row
  352. // item = menu.addItem(withTitle: NSLocalizedString("cut", comment: ""), action: #selector(cutItemAction), target: self)
  353. // item.representedObject = row
  354. // item = menu.addItem(withTitle: NSLocalizedString("paste", comment: ""), action: #selector(pastePageItemAction), target: self)
  355. //
  356. // menu.addItem(NSMenuItem.separator())
  357. //
  358. // item = menu.addItem(withTitle: NSLocalizedString("print", comment: ""), action: #selector(printItemAction), target: self)
  359. //
  360. // item = menu.addItem(withTitle: NSLocalizedString("Share", comment: ""), action: nil, target: self)
  361. //
  362. // item.submenu = NSSharingServicePicker.menu(forSharingItems: [self.listView.document.documentURL ?? ""], subjectContext: "", withTarget: self, selector: #selector(sharePageItemAction), serviceDelegate: nil)
  363. // }
  364. }
  365. func menu(_ menu: NSMenu, update item: NSMenuItem, at index: Int, shouldCancel: Bool) -> Bool {
  366. return true
  367. }
  368. }