AppDelegate.swift 19 KB

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