KMNHomeViewController.swift 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. //
  2. // KMNHomeViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/10/8.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMNHomeViewController: NSViewController {
  10. @IBOutlet var leftContendBox: NSBox!
  11. @IBOutlet var leftDivider: ComponentDivider!
  12. @IBOutlet var homeOpenView: KMHomeOpenView!
  13. @IBOutlet var homeRecommondView: KMHomeRecommondView!
  14. @IBOutlet var rightContendBox: NSBox!
  15. @IBOutlet var rightInfoView: KMHomeRightView!
  16. @IBOutlet var homeDragView: KMHomeDragView!
  17. var demoVC: WCCompWindowController = WCCompWindowController(windowNibName: "WCCompWindowController")
  18. var createWC: KMURLCreatePDFWindowController = KMURLCreatePDFWindowController(windowNibName: "KMURLCreatePDFWindowController")
  19. //合并
  20. var mergeWindowController: KMMergeWindowController?
  21. override func viewDidLoad() {
  22. super.viewDidLoad()
  23. // Do view setup here.
  24. self.configLeftContendView()
  25. self.configRightContendView()
  26. self.initAdvertisementData()
  27. homeDragView.delegate = self
  28. }
  29. override func viewDidAppear() {
  30. super.viewDidAppear()
  31. rightInfoView.resetScrollerStyle()
  32. rightInfoView.reloadData()
  33. }
  34. func configLeftContendView() {
  35. leftContendBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-middle")
  36. leftDivider.properties = ComponentDividerProperty(type: .vertical, dash: false)
  37. homeOpenView.delegate = self
  38. self.homeRecommondView.reloadData()
  39. }
  40. func configRightContendView() {
  41. rightContendBox.fillColor = ComponentLibrary.shared.getComponentColorFromKey("colorBg/layout-low")
  42. rightInfoView.delegate = self
  43. rightInfoView.reloadData()
  44. }
  45. func initAdvertisementData() {
  46. KMAdvertisementManager.manager.fetchDataWithResponseObject { [weak self] data, responseObject, error in
  47. KMPrint("获取广告数据成功")
  48. if data != nil {
  49. let content = data!.recommondContent
  50. let item = content?.recommondContentPDFPro
  51. var infos: [KMAdvertisementItemInfo] = []
  52. for info in item?.content ?? [] {
  53. if info.version == "recommondPDF-PDFtoOfficePack" {
  54. if IAPProductsManager.default().isAvailableAdvancedPDFToOffice() == false {
  55. infos.append(info)
  56. }
  57. } else {
  58. infos.append(info)
  59. }
  60. }
  61. item?.content = infos
  62. if KMAdvertisementManager.manager.infoDict.allKeys.count > 0 {
  63. if let adsInfo = KMAdvertisementManager.manager.infoDict["adsInfo"] {
  64. let infoDict: NSDictionary = KMAdvertisementManager.manager.infoDict["adsInfo"] as! NSDictionary
  65. let array: [[String: Any]] = infoDict["content"] as! [[String : Any]]
  66. let arrM = NSMutableArray.init()
  67. for dict in array {
  68. let adsInfo = KMAdsInfo.init()
  69. let mutableDictionary = NSMutableDictionary(dictionary: dict)
  70. adsInfo.infoDict = mutableDictionary
  71. arrM.add(adsInfo)
  72. }
  73. KMAdsInfoManager.shareInstance.adsInfoArrM = arrM
  74. }
  75. if let couponInfo = KMAdvertisementManager.manager.infoDict["couponContent"] {
  76. let infoDict: NSDictionary = KMAdvertisementManager.manager.infoDict["couponContent"] as! NSDictionary
  77. let array: [[String: Any]] = infoDict["content"] as! [[String : Any]]
  78. if array.isEmpty == false {
  79. let dict = array[0]
  80. let adsInfo = KMCouponInfo.init()
  81. let mutableDictionary = NSMutableDictionary(dictionary: dict)
  82. adsInfo.infoDict = mutableDictionary
  83. KMAdsInfoManager.shareInstance.couponInfo = adsInfo
  84. }
  85. }
  86. }
  87. }
  88. DispatchQueue.main.async {
  89. self?.homeRecommondView.reloadData()
  90. }
  91. }
  92. }
  93. @IBAction func showDemo(_ sender: Any) {
  94. demoVC.window?.center()
  95. demoVC.showWindow(nil)
  96. }
  97. }
  98. //MARK: - KMHomeOpenViewDelegate
  99. extension KMNHomeViewController: KMHomeOpenViewDelegate {
  100. func homeOpenViewDidChooseFileURL(_ view: KMHomeOpenView?, _ url: URL) {
  101. self.openFile(withFilePath: url)
  102. }
  103. }
  104. //MARK: - KMHomeRightViewDelegate
  105. extension KMNHomeViewController: KMHomeRightViewDelegate {
  106. //点击管理快捷工具按钮
  107. func homeRightViewDidManageQuickTools(_ view: KMHomeRightView) {
  108. let quickToolWindowController: KMNQuickToolWindowController = KMNQuickToolWindowController.init(windowNibName: "KMNQuickToolWindowController")
  109. quickToolWindowController.delegate = self
  110. quickToolWindowController.own_beginSheetModal(for: self.view.window) { string in
  111. }
  112. }
  113. //点击快捷工具列表中的某一项
  114. func homeRightViewDidQuickToolsItemClicked(_ view: KMHomeRightView, _ toolType: HomeQuickToolType) {
  115. self.quickToolsActionWith(toolType)
  116. }
  117. //最近文件列表删除更新结束后回调
  118. func homeRightViewDidRecentFilesUpdated(_ view: KMHomeRightView) {
  119. }
  120. //选择打开文件
  121. func homeRightViewDidChooseFileToOpen(_ view: KMHomeRightView, _ fileURL: URL) {
  122. self.openFile(withFilePath: fileURL)
  123. }
  124. }
  125. //MARK: - KMNQuickToolWindowDelegate
  126. extension KMNHomeViewController: KMNQuickToolWindowDelegate {
  127. func quickToolWindowControllerUpdate() {
  128. rightInfoView.reloadData()
  129. }
  130. }
  131. //MARK: - KMHomeDragViewDelegate
  132. extension KMNHomeViewController: KMHomeDragViewDelegate {
  133. func homeDragView(_ viewController: KMHomeDragView, filePath: URL) {
  134. self.openFile(withFilePath: filePath)
  135. }
  136. func homeDragView(_ viewController: KMHomeDragView, notSupport: Bool) {
  137. if notSupport {
  138. let alert = NSAlert()
  139. alert.alertStyle = .critical
  140. alert.messageText = KMLocalizedString("This file format is not supported. Please enter PDF, picture, or Office file")
  141. alert.runModal()
  142. }
  143. }
  144. }
  145. //MARK: - Open Files
  146. extension KMNHomeViewController {
  147. func openFile(withFilePath path: URL) -> Void {
  148. if let _windowC = NSApp.mainWindow?.windowController as? KMBrowserWindowController {
  149. _windowC.openFile(withFilePath: path)
  150. } else {
  151. for window in NSApp.windows {
  152. if let browseWindow = window as? KMBrowserWindow {
  153. if let browseWC = browseWindow.windowController as? KMBrowserWindowController {
  154. browseWC.openFile(withFilePath: path)
  155. break
  156. }
  157. }
  158. }
  159. }
  160. }
  161. func openImageToPdfWindow(urls: Array<URL>) {
  162. if let _windowC = NSApp.mainWindow?.windowController as? KMBrowserWindowController {
  163. _windowC.showBatchWindow(type: .imageToPDF, files: urls)
  164. } else {
  165. for window in NSApp.windows {
  166. if let browseWindow = window as? KMBrowserWindow {
  167. if let browseWC = browseWindow.windowController as? KMBrowserWindowController {
  168. browseWC.showBatchWindow(type: .imageToPDF, files: urls)
  169. break
  170. }
  171. }
  172. }
  173. }
  174. }
  175. func showLimitWindowAlert(url: URL?) {
  176. if !KMDataManager.default.isTabbingWin{
  177. KMDataManager.default.isTabbingWin = true
  178. let tabbingWin: KMTabbingHintWindowController = KMTabbingHintWindowController()
  179. tabbingWin.selectCallBack = {[weak self] continueOrNot in
  180. KMDataManager.default.isTabbingWin = false
  181. if continueOrNot {
  182. self?.reopenDocument(forPaths: url)
  183. } else {
  184. }
  185. }
  186. self.km_beginSheet(windowC: tabbingWin)
  187. }
  188. }
  189. func reopenDocument(forPaths path: URL?) -> Void {
  190. if path == nil {
  191. let browser = KMBrowser.init() as KMBrowser
  192. browser.windowController = KMBrowserWindowController.init(browser: browser)
  193. browser.addHomeTabContents()
  194. browser.windowController.showWindow(self)
  195. }else {
  196. let browser = KMBrowser.init() as KMBrowser
  197. browser.windowController = KMBrowserWindowController.init(browser: browser)
  198. browser.addHomeTabContents()
  199. browser.windowController.showWindow(self)
  200. NSDocumentController.shared.km_safe_openDocument(withContentsOf: path!, display: true) { doc, open, err in
  201. }
  202. }
  203. }
  204. func fetchUniquePath(_ originalPath: String) -> String {
  205. var path = originalPath
  206. let dManager = FileManager.default
  207. if !dManager.fileExists(atPath: path) {
  208. if path.extension.count < 1 {
  209. path = path.stringByAppendingPathExtension("pdf")
  210. }
  211. return path
  212. } else {
  213. let originalFullFileName = path.lastPathComponent
  214. let originalFileName = path.lastPathComponent.deletingPathExtension.lastPathComponent
  215. let originalExtension = path.extension
  216. let startIndex: Int = 0
  217. let endIndex: Int = startIndex + originalPath.count - originalFullFileName.count - 1
  218. let fileLocatePath = originalPath.substring(to: endIndex)
  219. var i = 1
  220. while (1 != 0) {
  221. var newName = String(format: "%@%ld", originalFileName, i)
  222. newName = String(format: "%@%@", newName, originalExtension)
  223. let newPath = fileLocatePath.stringByAppendingPathComponent(newName)
  224. if !dManager.fileExists(atPath: newPath) {
  225. return newPath
  226. } else {
  227. i+=1
  228. continue
  229. }
  230. }
  231. }
  232. }
  233. func fetchDifferentFilePath(filePath: String) -> String {
  234. var resultFilePath = filePath
  235. var index: Int = 0
  236. while (FileManager.default.fileExists(atPath: resultFilePath)) {
  237. index += 1
  238. let path = NSString(string: filePath).deletingPathExtension + "(" + String(index) + ")"
  239. resultFilePath = NSString(string: path).appendingPathExtension(NSString(string: filePath).pathExtension)!
  240. }
  241. return resultFilePath;
  242. }
  243. func isDamageImage(image: NSImage?, path: String) -> Bool {
  244. if (image == nil) {
  245. return true
  246. }
  247. let addImageAnnotation = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).last!.appendingPathComponent(Bundle.main.bundleIdentifier!).appendingPathComponent("addImageAnnotation")
  248. if !FileManager.default.fileExists(atPath: addImageAnnotation.path) {
  249. try? FileManager.default.createDirectory(atPath: addImageAnnotation.path, withIntermediateDirectories: false, attributes: nil)
  250. }
  251. guard let data = image!.tiffRepresentation else { return false }
  252. guard let imageRep = NSBitmapImageRep(data: data) else { return false }
  253. imageRep.size = image!.size
  254. var imageData: Data?
  255. if path.lowercased() == "png" {
  256. imageData = imageRep.representation(using: .png, properties: [:])
  257. } else {
  258. imageData = imageRep.representation(using: .jpeg, properties: [:])
  259. }
  260. let rPath: URL = addImageAnnotation.appendingPathComponent(tagString()).appendingPathExtension("png")
  261. if let data = imageData {
  262. try?data.write(to: rPath)
  263. return false
  264. } else {
  265. return true
  266. }
  267. }
  268. func tagString() -> String {
  269. let dateFormatter = DateFormatter()
  270. dateFormatter.dateFormat = "yyMMddHHmmss"
  271. let currentDate = Date()
  272. let randomNum = Int(arc4random_uniform(10000))
  273. let str = String(format: "%@%04d", dateFormatter.string(from: Date()),randomNum)
  274. return str
  275. }
  276. func convertTIFFDataToPDF(_ tiffData: Data) -> Data? {
  277. guard let imsrc = CGImageSourceCreateWithData(tiffData as CFData, [kCGImageSourceTypeIdentifierHint: kUTTypeTIFF] as CFDictionary), CGImageSourceGetCount(imsrc) > 0, let cgImage = CGImageSourceCreateImageAtIndex(imsrc, 0, nil) else { return nil }
  278. let pdfData = NSMutableData(capacity: tiffData.count)
  279. let consumer = CGDataConsumer(data: pdfData! as CFMutableData)!
  280. var rect = CGRect(x: 0, y: 0, width: CGFloat(cgImage.width), height: CGFloat(cgImage.height))
  281. let ctxt = CGContext(consumer: consumer, mediaBox: &rect, nil)
  282. ctxt!.beginPDFPage(nil)
  283. ctxt!.draw(cgImage, in: rect)
  284. ctxt!.endPDFPage()
  285. ctxt!.closePDF()
  286. return pdfData as? Data
  287. }
  288. }
  289. //MARK: - Quick Tools Action
  290. extension KMNHomeViewController {
  291. func quickToolsActionWith(_ type: HomeQuickToolType) {
  292. switch type {
  293. case .Batch:
  294. fastTool_Batch()
  295. break
  296. case .MergePDF:
  297. fastTool_MergePDF()
  298. break
  299. case .ImageToPDF:
  300. imageToPDFAction()
  301. break
  302. case .OCR:
  303. fastTool_OCR()
  304. break
  305. case .ConvertPDF:
  306. fastTool_ConvertPDF()
  307. break
  308. case .PDFToWord:
  309. fastTool_ConvertPDF()
  310. break
  311. case .PDFToExcel:
  312. fastTool_PDFToExcel()
  313. break
  314. case .Compression:
  315. fastTool_Compression()
  316. break
  317. case .PDFToPPT:
  318. fastTool_PDFToPPT()
  319. break
  320. case .Security:
  321. fastTool_Security()
  322. break
  323. case .FileCompare:
  324. fastTool_FileCompare()
  325. break
  326. case .Watermark:
  327. fastTool_Watermark()
  328. break
  329. case .Insert:
  330. fastTool_Insert()
  331. break
  332. case .Extract:
  333. fastTool_Extract()
  334. break
  335. case .DigitalSignature:
  336. break
  337. case .Print:
  338. fastTool_Print()
  339. break
  340. }
  341. }
  342. func imageToPDFAction() {
  343. let openPanel = NSOpenPanel()
  344. openPanel.allowedFileTypes = KMBatchManager.supportedImageTypes()
  345. //MARK: 允许多选还是单选,如果是付费用户允许多选
  346. openPanel.allowsMultipleSelection = true
  347. openPanel.message = NSLocalizedString("Select images to create a new document. To select multiple files press cmd ⌘ button on keyboard and click on the target files one by one.", comment: "")
  348. if IAPProductsManager.default().isAvailableAllFunction(){
  349. openPanel.allowsMultipleSelection = true
  350. } else {
  351. openPanel.allowsMultipleSelection = false
  352. }
  353. openPanel.beginSheetModal(for: NSWindow.currentWindow()) {[weak self] result in
  354. if result == NSApplication.ModalResponse.OK {
  355. guard let weakSelf = self else { return }
  356. let urls = openPanel.urls as [URL]
  357. weakSelf.openImageToPdfWindow(urls: urls)
  358. }
  359. }
  360. }
  361. func fastTool_Batch() { // Batch
  362. if !IAPProductsManager.default().isAvailableAllFunction(){
  363. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  364. return
  365. }
  366. let batchWindowController = KMBatchWindowController.manager
  367. batchWindowController.window?.makeKeyAndOrderFront("")
  368. }
  369. func fastTool_MergePDF() { // MergePDF
  370. mergeWindowController = KMMergeWindowController(windowNibName: "KMMergeWindowController")
  371. mergeWindowController!.type = .merge
  372. mergeWindowController!.cancelAction = { [unowned self] controller in
  373. self.view.window?.endSheet((self.mergeWindowController!.window)!)
  374. }
  375. view.window?.beginSheet(mergeWindowController!.window!)
  376. }
  377. func fastTool_OCR() { // OCR
  378. if !IAPProductsManager.default().isAvailableAllFunction(){
  379. let winC = KMPurchaseCompareWindowController.sharedInstance()
  380. winC?.showWindow(nil)
  381. return
  382. }
  383. let openPanel = NSOpenPanel()
  384. var arr = KMBatchManager.supportedImageTypes()
  385. arr.append("pdf")
  386. openPanel.allowedFileTypes = arr
  387. openPanel.allowsMultipleSelection = false
  388. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
  389. if result == NSApplication.ModalResponse.OK {
  390. quickOcr(urls: openPanel.urls)
  391. }
  392. }
  393. }
  394. func quickOcr(urls: Array<URL>) {
  395. showBatchWindow(type: .OCR, files: urls)
  396. }
  397. func fastTool_ConvertPDF() { // 转换PDF
  398. let openPanel = NSOpenPanel()
  399. var arr = KMBatchManager.supportedImageTypes()
  400. arr.append("pdf")
  401. openPanel.allowedFileTypes = ["pdf","PDF"]
  402. //MARK: 允许多选还是单选,如果是付费用户允许多选
  403. openPanel.allowsMultipleSelection = false
  404. openPanel.beginSheetModal(for: self.view.window!) { [self] (result) in
  405. if result == NSApplication.ModalResponse.OK {
  406. quickConvertPdf(urls: openPanel.urls, type: .WordAdvance)
  407. }
  408. }
  409. }
  410. func quickConvertPdf(urls: Array<URL>, type: KMConvertWithPDFType) {
  411. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  412. self.showBatchWindow(type: .convertPDF, subType: type.rawValue,files: urls)
  413. }
  414. }
  415. func fastTool_PDFToPPT() {
  416. let openPanel = NSOpenPanel()
  417. openPanel.allowedFileTypes = ["pdf","PDF"]
  418. //MARK: 允许多选还是单选,如果是付费用户允许多选
  419. openPanel.allowsMultipleSelection = false
  420. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
  421. if result == NSApplication.ModalResponse.OK {
  422. quickConvertPdf(urls: openPanel.urls, type: .PowerPoint)
  423. }
  424. }
  425. }
  426. func fastTool_PDFToExcel() {
  427. let openPanel = NSOpenPanel()
  428. openPanel.allowedFileTypes = ["pdf","PDF"]
  429. //MARK: 允许多选还是单选,如果是付费用户允许多选
  430. openPanel.allowsMultipleSelection = false
  431. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
  432. if result == NSApplication.ModalResponse.OK {
  433. quickConvertPdf(urls: openPanel.urls, type: .Excel)
  434. }
  435. }
  436. }
  437. // 压缩
  438. func fastTool_Compression() {
  439. let openPanel = NSOpenPanel()
  440. openPanel.allowedFileTypes = ["pdf","PDF"]
  441. //MARK: 允许多选还是单选,如果是付费用户允许多选
  442. openPanel.allowsMultipleSelection = false
  443. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
  444. if result == NSApplication.ModalResponse.OK {
  445. quickCompressPdf(urls: openPanel.urls)
  446. }
  447. }
  448. }
  449. func quickCompressPdf(urls: Array<URL>) {
  450. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  451. self.showBatchWindow(type: .compress, files: urls)
  452. }
  453. }
  454. // 安全
  455. func fastTool_Security() {
  456. let openPanel = NSOpenPanel()
  457. openPanel.allowedFileTypes = ["pdf","PDF"]
  458. //MARK: 允许多选还是单选,如果是付费用户允许多选
  459. openPanel.allowsMultipleSelection = true
  460. openPanel.message = NSLocalizedString("To select multiple files press cmd ⌘ button on keyboard and click on the target files one by one.", comment: "")
  461. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
  462. if result == NSApplication.ModalResponse.OK {
  463. quickPassword(urls: openPanel.urls)
  464. }
  465. }
  466. }
  467. func quickPassword(urls: Array<URL>) {
  468. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  469. self.showBatchWindow(type: .security, files: urls)
  470. }
  471. }
  472. func fastTool_FileCompare() { // 文件对比
  473. if !IAPProductsManager.default().isAvailableAllFunction(){
  474. let winC = KMPurchaseCompareWindowController.sharedInstance()
  475. winC?.showWindow(nil)
  476. return
  477. }
  478. let openPanel = NSOpenPanel()
  479. openPanel.allowsMultipleSelection = false
  480. openPanel.allowedFileTypes = ["pdf"]
  481. openPanel.beginSheetModal(for: NSApp.mainWindow!) { result in
  482. if result == .cancel {
  483. return
  484. }
  485. if !openPanel.url!.path.isPDFValid() {
  486. let alert = NSAlert()
  487. alert.alertStyle = .critical
  488. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  489. alert.runModal()
  490. return
  491. }
  492. NSWindowController.checkPassword(url: URL(fileURLWithPath: openPanel.url!.path), type: .owner) { [unowned self] success, resultPassword in
  493. if success {
  494. DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
  495. let controller = KMCompareWindowController(windowNibName: "KMCompareWindowController")
  496. controller.password = resultPassword
  497. controller.filePath = openPanel.url!.path
  498. controller.cancelAction = { [unowned self] contr in
  499. self.view.window?.endSheet((controller.window)!)
  500. }
  501. controller.contentComplete = { [unowned self] controller, pdfCompareContent, result, oldDocument, document in
  502. DispatchQueue.main.async {
  503. self.view.window?.endSheet((controller.window)!)
  504. }
  505. }
  506. controller.coveringComplete = { [unowned self] controller, document in
  507. self.view.window?.endSheet((controller.window)!)
  508. }
  509. controller.fileType = .content
  510. NSWindow.currentWindow().beginSheet(controller.window!)
  511. }
  512. } else {
  513. }
  514. }
  515. }
  516. }
  517. func fastTool_Watermark() { // 水印
  518. if !IAPProductsManager.default().isAvailableAllFunction(){
  519. let winC = KMPurchaseCompareWindowController.sharedInstance()
  520. winC?.showWindow(nil)
  521. return
  522. }
  523. let openPanel = NSOpenPanel()
  524. openPanel.allowedFileTypes = ["pdf","PDF"]
  525. //MARK: 允许多选还是单选,如果是付费用户允许多选
  526. openPanel.allowsMultipleSelection = false
  527. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
  528. if result == NSApplication.ModalResponse.OK {
  529. quickWaterMark(urls: openPanel.urls)
  530. }
  531. }
  532. }
  533. func quickWaterMark(urls: Array<URL>) {
  534. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  535. self.showBatchWindow(type: .watermark, files: urls)
  536. }
  537. }
  538. func fastTool_Background() { // 背景
  539. if !IAPProductsManager.default().isAvailableAllFunction(){
  540. let winC = KMPurchaseCompareWindowController.sharedInstance()
  541. winC?.showWindow(nil)
  542. return
  543. }
  544. let openPanel = NSOpenPanel()
  545. openPanel.allowedFileTypes = ["pdf","PDF"]
  546. //MARK: 允许多选还是单选,如果是付费用户允许多选
  547. openPanel.allowsMultipleSelection = false
  548. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
  549. if result == NSApplication.ModalResponse.OK {
  550. quickBackgroudMark(urls: openPanel.urls)
  551. }
  552. }
  553. }
  554. func quickBackgroudMark(urls: Array<URL>) {
  555. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  556. self.showBatchWindow(type: .background, files: urls)
  557. }
  558. }
  559. //MARK: 打印
  560. func fastTool_Print() {
  561. let openPanel = NSOpenPanel()
  562. openPanel.allowedFileTypes = ["pdf","PDF"]
  563. openPanel.allowsMultipleSelection = false
  564. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
  565. if result == NSApplication.ModalResponse.OK {
  566. if let url = openPanel.url {
  567. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  568. self.showPrintWindowWithURL(url)
  569. }
  570. }
  571. }
  572. }
  573. }
  574. func showPrintWindowWithURL(_ url: URL) {
  575. if let cPDFDocument = CPDFDocument(url: url) {
  576. KMPrintWindowController.openDocument(inputDocument: cPDFDocument, inputPageRange: KMPrintPageRange(type: .allPage, selectPages: []))
  577. }
  578. }
  579. func fastTool_Insert() { // 插入
  580. if !IAPProductsManager.default().isAvailableAllFunction(){
  581. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  582. return
  583. }
  584. let openPanel = NSOpenPanel()
  585. openPanel.prompt = NSLocalizedString("Insert", comment: "")
  586. openPanel.allowsMultipleSelection = false
  587. openPanel.allowedFileTypes = ["pdf"]
  588. openPanel.beginSheetModal(for: NSApp.mainWindow!) { result in
  589. if result == .OK {
  590. let windowC = KMPDFInsertWindowController(fileURL: openPanel.url!)
  591. windowC.callback = { [weak self] idx, params in
  592. if params.count >= 4 {
  593. if let doc = params.first as? CPDFDocument {
  594. self?.savePDFDocument(doc, password: params[1] as? String ?? "")
  595. }
  596. }
  597. windowC.own_closeEndSheet()
  598. }
  599. windowC.own_beginSheetModal(for: self.view.window) { result in
  600. }
  601. }
  602. }
  603. }
  604. func savePDFDocument(_ pdf: CPDFDocument, password: String) -> Void {
  605. DispatchQueue.global(qos: .`default`).async {
  606. var isSuccessfully = false
  607. if pdf.isEncrypted {
  608. let dic = [
  609. CPDFDocumentWriteOption.userPasswordOption : password,
  610. CPDFDocumentWriteOption.ownerPasswordOption : password
  611. ]
  612. isSuccessfully = pdf.write(to: pdf.documentURL, withOptions: dic)
  613. } else {
  614. isSuccessfully = pdf.write(to: pdf.documentURL)
  615. }
  616. if !isSuccessfully {
  617. if let data = pdf.dataRepresentation() {
  618. isSuccessfully = NSData(data: data).write(to: pdf.documentURL, atomically: true)
  619. }
  620. }
  621. DispatchQueue.main.sync {
  622. if isSuccessfully {
  623. let workspace = NSWorkspace.shared
  624. let url = URL(fileURLWithPath: pdf.documentURL?.path ?? "")
  625. workspace.activateFileViewerSelecting([url])
  626. } else {
  627. let alert = NSAlert()
  628. alert.alertStyle = .critical
  629. alert.messageText = NSLocalizedString("Failed to insert page(s)!", comment: "")
  630. alert.runModal()
  631. }
  632. }
  633. }
  634. }
  635. func fastTool_Extract() { // 提取
  636. let openPanel = NSOpenPanel()
  637. openPanel.prompt = "提取"
  638. openPanel.allowsMultipleSelection = false
  639. openPanel.allowedFileTypes = ["pdf"]
  640. openPanel.beginSheetModal(for: NSApp.mainWindow!) { result in
  641. if result == .OK {
  642. DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
  643. let insertWindowController: KMPDFInsertPageWindow = KMPDFInsertPageWindow.init(documentPath: openPanel.url!, toolType: .Extract)
  644. insertWindowController.beginSheetExtractModal(for: self.view.window!) { pdfDocument, pages, oneDocumentPerPage, isDeletePage in
  645. self.extractPageAction(pdfDocument, pages, oneDocumentPerPage, isDeletePage)
  646. }
  647. }
  648. }
  649. }
  650. }
  651. func extractPageAction(_ pdfDocument: CPDFDocument, _ pages: [CPDFPage], _ oneDocumentPerPage: Bool, _ isDeletePage: Bool) -> Void {
  652. if pages.count < 1 {
  653. let alert = NSAlert()
  654. alert.alertStyle = .critical
  655. alert.messageText = NSLocalizedString("Please select two or more pages first to organize.", comment: "")
  656. alert.runModal()
  657. return
  658. }
  659. if !oneDocumentPerPage {
  660. let fileName = pdfDocument.getFileNameAccordingSelctPages(pages)
  661. let outputSavePanel = NSSavePanel()
  662. outputSavePanel.allowedFileTypes = ["pdf"]
  663. outputSavePanel.nameFieldStringValue = fileName
  664. outputSavePanel.beginSheetModal(for: self.view.window!) { result in
  665. if result == .OK {
  666. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  667. let saveFilePath = outputSavePanel.url?.path
  668. DispatchQueue.global().async {
  669. let pdf = CPDFDocument.init()
  670. let success = (pdf!.extractAsOneDocument(withPages: pages, savePath: saveFilePath)) as Bool
  671. DispatchQueue.main.async {
  672. if success {
  673. let workspace = NSWorkspace.shared
  674. let url = URL(fileURLWithPath: saveFilePath!)
  675. workspace.activateFileViewerSelecting([url])
  676. if isDeletePage {
  677. for page in pages {
  678. let indexPage = pdfDocument.index(for: page)
  679. pdfDocument.removePage(at: indexPage)
  680. }
  681. }
  682. }
  683. }
  684. }
  685. }
  686. }
  687. }
  688. } else {
  689. let panel = NSOpenPanel()
  690. panel.canChooseFiles = false
  691. panel.canChooseDirectories = true
  692. panel.canCreateDirectories = true
  693. panel.allowsMultipleSelection = false
  694. panel.beginSheetModal(for: self.view.window!) { result in
  695. if result == .OK {
  696. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  697. let outputURL = panel.url
  698. DispatchQueue.global().async {
  699. let folderName = String(pdfDocument.documentURL!.lastPathComponent.split(separator: ".")[0]) + "_extract"
  700. var filePath = URL(fileURLWithPath: outputURL!.path).appendingPathComponent(folderName).path
  701. var i = 1
  702. let testFilePath = filePath
  703. while FileManager.default.fileExists(atPath: filePath) {
  704. filePath = testFilePath + "\(i)"
  705. i += 1
  706. }
  707. try? FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false, attributes: nil)
  708. let successArray = pdfDocument.extractPerPageDocument(withPages: pages, folerPath: filePath)
  709. DispatchQueue.main.async {
  710. if successArray!.count > 0 {
  711. NSWorkspace.shared.activateFileViewerSelecting(successArray!)
  712. if !isDeletePage {
  713. for page in pages {
  714. let indexPage = pdfDocument.index(for: page)
  715. pdfDocument.removePage(at: indexPage)
  716. }
  717. }
  718. }
  719. }
  720. }
  721. }
  722. }
  723. }
  724. }
  725. }
  726. //MARK: Batch
  727. func showBatchWindow(type: KMBatchCollectionViewType, subType: Int = 0, files: [URL]?) {
  728. let batchWindowController = KMBatchWindowController.manager
  729. batchWindowController.window?.makeKeyAndOrderFront("")
  730. // var datas: [KMBatchProcessingTableViewModel] = []
  731. // for file in files! {
  732. // let data = KMBatchProcessingTableViewModel.initWithFilePath(url: file)
  733. // datas.append(data)
  734. // }
  735. batchWindowController.inputData = files ?? []
  736. batchWindowController.type = type
  737. batchWindowController.inputSubType = subType
  738. }
  739. }