KMNHomeViewController.swift 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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 = NSLocalizedString("This file format is not supported. Please enter PDF, picture, or Office file", comment: "")
  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. }
  151. }
  152. func openImageToPdfWindow(urls: Array<URL>) {
  153. if let _windowC = NSApp.mainWindow?.windowController as? KMBrowserWindowController {
  154. _windowC.showBatchWindow(type: .imageToPDF, files: urls)
  155. }
  156. }
  157. func showLimitWindowAlert(url: URL?) {
  158. if !KMDataManager.default.isTabbingWin{
  159. KMDataManager.default.isTabbingWin = true
  160. let tabbingWin: KMTabbingHintWindowController = KMTabbingHintWindowController()
  161. tabbingWin.selectCallBack = {[weak self] continueOrNot in
  162. KMDataManager.default.isTabbingWin = false
  163. if continueOrNot {
  164. self?.reopenDocument(forPaths: url)
  165. } else {
  166. }
  167. }
  168. self.km_beginSheet(windowC: tabbingWin)
  169. }
  170. }
  171. func reopenDocument(forPaths path: URL?) -> Void {
  172. if path == nil {
  173. let browser = KMBrowser.init() as KMBrowser
  174. browser.windowController = KMBrowserWindowController.init(browser: browser)
  175. browser.addHomeTabContents()
  176. browser.windowController.showWindow(self)
  177. }else {
  178. let browser = KMBrowser.init() as KMBrowser
  179. browser.windowController = KMBrowserWindowController.init(browser: browser)
  180. browser.addHomeTabContents()
  181. browser.windowController.showWindow(self)
  182. NSDocumentController.shared.km_safe_openDocument(withContentsOf: path!, display: true) { doc, open, err in
  183. }
  184. }
  185. }
  186. func fetchUniquePath(_ originalPath: String) -> String {
  187. var path = originalPath
  188. let dManager = FileManager.default
  189. if !dManager.fileExists(atPath: path) {
  190. if path.extension.count < 1 {
  191. path = path.stringByAppendingPathExtension("pdf")
  192. }
  193. return path
  194. } else {
  195. let originalFullFileName = path.lastPathComponent
  196. let originalFileName = path.lastPathComponent.deletingPathExtension.lastPathComponent
  197. let originalExtension = path.extension
  198. let startIndex: Int = 0
  199. let endIndex: Int = startIndex + originalPath.count - originalFullFileName.count - 1
  200. let fileLocatePath = originalPath.substring(to: endIndex)
  201. var i = 1
  202. while (1 != 0) {
  203. var newName = String(format: "%@%ld", originalFileName, i)
  204. newName = String(format: "%@%@", newName, originalExtension)
  205. let newPath = fileLocatePath.stringByAppendingPathComponent(newName)
  206. if !dManager.fileExists(atPath: newPath) {
  207. return newPath
  208. } else {
  209. i+=1
  210. continue
  211. }
  212. }
  213. }
  214. }
  215. func fetchDifferentFilePath(filePath: String) -> String {
  216. var resultFilePath = filePath
  217. var index: Int = 0
  218. while (FileManager.default.fileExists(atPath: resultFilePath)) {
  219. index += 1
  220. let path = NSString(string: filePath).deletingPathExtension + "(" + String(index) + ")"
  221. resultFilePath = NSString(string: path).appendingPathExtension(NSString(string: filePath).pathExtension)!
  222. }
  223. return resultFilePath;
  224. }
  225. func isDamageImage(image: NSImage?, path: String) -> Bool {
  226. if (image == nil) {
  227. return true
  228. }
  229. let addImageAnnotation = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).last!.appendingPathComponent(Bundle.main.bundleIdentifier!).appendingPathComponent("addImageAnnotation")
  230. if !FileManager.default.fileExists(atPath: addImageAnnotation.path) {
  231. try? FileManager.default.createDirectory(atPath: addImageAnnotation.path, withIntermediateDirectories: false, attributes: nil)
  232. }
  233. guard let data = image!.tiffRepresentation else { return false }
  234. guard let imageRep = NSBitmapImageRep(data: data) else { return false }
  235. imageRep.size = image!.size
  236. var imageData: Data?
  237. if path.lowercased() == "png" {
  238. imageData = imageRep.representation(using: .png, properties: [:])
  239. } else {
  240. imageData = imageRep.representation(using: .jpeg, properties: [:])
  241. }
  242. let rPath: URL = addImageAnnotation.appendingPathComponent(tagString()).appendingPathExtension("png")
  243. if let data = imageData {
  244. try?data.write(to: rPath)
  245. return false
  246. } else {
  247. return true
  248. }
  249. }
  250. func tagString() -> String {
  251. let dateFormatter = DateFormatter()
  252. dateFormatter.dateFormat = "yyMMddHHmmss"
  253. let currentDate = Date()
  254. let randomNum = Int(arc4random_uniform(10000))
  255. let str = String(format: "%@%04d", dateFormatter.string(from: Date()),randomNum)
  256. return str
  257. }
  258. func convertTIFFDataToPDF(_ tiffData: Data) -> Data? {
  259. guard let imsrc = CGImageSourceCreateWithData(tiffData as CFData, [kCGImageSourceTypeIdentifierHint: kUTTypeTIFF] as CFDictionary), CGImageSourceGetCount(imsrc) > 0, let cgImage = CGImageSourceCreateImageAtIndex(imsrc, 0, nil) else { return nil }
  260. let pdfData = NSMutableData(capacity: tiffData.count)
  261. let consumer = CGDataConsumer(data: pdfData! as CFMutableData)!
  262. var rect = CGRect(x: 0, y: 0, width: CGFloat(cgImage.width), height: CGFloat(cgImage.height))
  263. let ctxt = CGContext(consumer: consumer, mediaBox: &rect, nil)
  264. ctxt!.beginPDFPage(nil)
  265. ctxt!.draw(cgImage, in: rect)
  266. ctxt!.endPDFPage()
  267. ctxt!.closePDF()
  268. return pdfData as? Data
  269. }
  270. }
  271. //MARK: - Quick Tools Action
  272. extension KMNHomeViewController {
  273. func quickToolsActionWith(_ type: HomeQuickToolType) {
  274. switch type {
  275. case .Batch:
  276. fastTool_Batch()
  277. break
  278. case .MergePDF:
  279. fastTool_MergePDF()
  280. break
  281. case .ImageToPDF:
  282. imageToPDFAction()
  283. break
  284. case .OCR:
  285. fastTool_OCR()
  286. break
  287. case .ConvertPDF:
  288. fastTool_ConvertPDF()
  289. break
  290. case .PDFToWord:
  291. fastTool_ConvertPDF()
  292. break
  293. case .PDFToExcel:
  294. fastTool_PDFToExcel()
  295. break
  296. case .Compression:
  297. fastTool_Compression()
  298. break
  299. case .PDFToPPT:
  300. fastTool_PDFToPPT()
  301. break
  302. case .Security:
  303. fastTool_Security()
  304. break
  305. case .FileCompare:
  306. fastTool_FileCompare()
  307. break
  308. case .Watermark:
  309. fastTool_Watermark()
  310. break
  311. case .Insert:
  312. fastTool_Insert()
  313. break
  314. case .Extract:
  315. fastTool_Extract()
  316. break
  317. case .DigitalSignature:
  318. break
  319. case .Print:
  320. fastTool_Print()
  321. break
  322. }
  323. }
  324. func imageToPDFAction() {
  325. let openPanel = NSOpenPanel()
  326. openPanel.allowedFileTypes = KMImageToPDFMethod.supportedImageTypes()
  327. //MARK: 允许多选还是单选,如果是付费用户允许多选
  328. openPanel.allowsMultipleSelection = true
  329. 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: "")
  330. if IAPProductsManager.default().isAvailableAllFunction(){
  331. openPanel.allowsMultipleSelection = true
  332. } else {
  333. openPanel.allowsMultipleSelection = false
  334. }
  335. openPanel.beginSheetModal(for: NSWindow.currentWindow()) {[weak self] result in
  336. if result == NSApplication.ModalResponse.OK {
  337. guard let weakSelf = self else { return }
  338. let urls = openPanel.urls as [URL]
  339. weakSelf.openImageToPdfWindow(urls: urls)
  340. }
  341. }
  342. }
  343. func fastTool_Batch() { // Batch
  344. if !IAPProductsManager.default().isAvailableAllFunction(){
  345. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  346. return
  347. }
  348. let batchWindowController = KMBatchWindowController(windowNibName: "KMBatchWindowController")
  349. batchWindowController.window?.makeKeyAndOrderFront("")
  350. }
  351. func fastTool_MergePDF() { // MergePDF
  352. mergeWindowController = KMMergeWindowController(windowNibName: "KMMergeWindowController")
  353. mergeWindowController!.type = .merge
  354. mergeWindowController!.cancelAction = { [unowned self] controller in
  355. self.view.window?.endSheet((self.mergeWindowController!.window)!)
  356. }
  357. view.window?.beginSheet(mergeWindowController!.window!)
  358. }
  359. func fastTool_OCR() { // OCR
  360. if !IAPProductsManager.default().isAvailableAllFunction(){
  361. let winC = KMPurchaseCompareWindowController.sharedInstance()
  362. winC?.showWindow(nil)
  363. return
  364. }
  365. let openPanel = NSOpenPanel()
  366. var arr = KMImageToPDFMethod.supportedImageTypes()
  367. arr.append("pdf")
  368. openPanel.allowedFileTypes = arr
  369. openPanel.allowsMultipleSelection = false
  370. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
  371. if result == NSApplication.ModalResponse.OK {
  372. quickOcr(urls: openPanel.urls)
  373. }
  374. }
  375. }
  376. func quickOcr(urls: Array<URL>) {
  377. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  378. var document: CPDFDocument?
  379. for fileURL in urls {
  380. if fileURL.lastPathComponent.pathExtension == "pdf"{
  381. document = CPDFDocument(url: fileURL)
  382. }else{
  383. document = CPDFDocument()
  384. let image = NSImage(contentsOfFile: fileURL.path)
  385. _ = document?.km_insertPage(image!.size, withImage: fileURL.path, at: 0)
  386. }
  387. break
  388. }
  389. if document == nil{
  390. let alert = NSAlert()
  391. alert.alertStyle = .critical
  392. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  393. alert.beginSheetModal(for: NSApp.mainWindow!) { (response) in
  394. if response == .alertFirstButtonReturn {
  395. // Handle cancel action
  396. }
  397. }
  398. return
  399. }
  400. let com = KMOCRPDFWindowController(cpdfDocument: document!, pwd: document!.password)
  401. self.km_beginSheet(windowC: com)
  402. }
  403. }
  404. func fastTool_ConvertPDF() { // 转换PDF
  405. let openPanel = NSOpenPanel()
  406. var arr = KMImageToPDFMethod.supportedImageTypes()
  407. arr.append("pdf")
  408. openPanel.allowedFileTypes = ["pdf","PDF"]
  409. //MARK: 允许多选还是单选,如果是付费用户允许多选
  410. openPanel.allowsMultipleSelection = false
  411. openPanel.beginSheetModal(for: self.view.window!) { [self] (result) in
  412. if result == NSApplication.ModalResponse.OK {
  413. quickConvertPdf(urls: openPanel.urls, type: .WordAdvance)
  414. }
  415. }
  416. }
  417. func quickConvertPdf(urls: Array<URL>, type: KMConvertWithPDFType) {
  418. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  419. // var arr: Array<KMBatchOperateFile> = Array()
  420. // for fileURL in urls {
  421. // let img = NSImage(contentsOfFile: fileURL.path)
  422. // if self.isDamageImage(image: img, path: fileURL.path) {
  423. // let alert = NSAlert()
  424. // alert.alertStyle = .critical
  425. // alert.messageText = String(format: NSLocalizedString("The file \"%@\" could not be opened.", comment: ""), fileURL.path.lastPathComponent)
  426. // alert.informativeText = NSLocalizedString("It may be damaged or use a file format that PDF Reader Pro doesn’t recognize.", comment: "")
  427. // alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  428. // alert.beginSheetModal(for: NSApp.mainWindow!) { (response) in
  429. // if response == .alertFirstButtonReturn {
  430. // // Handle cancel action
  431. // }
  432. // }
  433. // continue
  434. // }
  435. // let file = KMBatchOperateFile(filePath: fileURL.path, type: .Convert)
  436. // arr.append(file)
  437. // }
  438. self.showBatchWindow(type: .convertPDF, subType: type.rawValue,files: urls)
  439. }
  440. }
  441. func fastTool_PDFToPPT() {
  442. let openPanel = NSOpenPanel()
  443. openPanel.allowedFileTypes = ["pdf","PDF"]
  444. //MARK: 允许多选还是单选,如果是付费用户允许多选
  445. openPanel.allowsMultipleSelection = false
  446. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
  447. if result == NSApplication.ModalResponse.OK {
  448. quickConvertPdf(urls: openPanel.urls, type: .PowerPoint)
  449. }
  450. }
  451. }
  452. func fastTool_PDFToExcel() {
  453. let openPanel = NSOpenPanel()
  454. openPanel.allowedFileTypes = ["pdf","PDF"]
  455. //MARK: 允许多选还是单选,如果是付费用户允许多选
  456. openPanel.allowsMultipleSelection = false
  457. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
  458. if result == NSApplication.ModalResponse.OK {
  459. quickConvertPdf(urls: openPanel.urls, type: .Excel)
  460. }
  461. }
  462. }
  463. // 压缩
  464. func fastTool_Compression() {
  465. let openPanel = NSOpenPanel()
  466. openPanel.allowedFileTypes = ["pdf","PDF"]
  467. //MARK: 允许多选还是单选,如果是付费用户允许多选
  468. openPanel.allowsMultipleSelection = false
  469. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
  470. if result == NSApplication.ModalResponse.OK {
  471. quickCompressPdf(urls: openPanel.urls)
  472. }
  473. }
  474. }
  475. func quickCompressPdf(urls: Array<URL>) {
  476. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  477. self.showBatchWindow(type: .compress, files: urls)
  478. }
  479. }
  480. // 安全
  481. func fastTool_Security() {
  482. let openPanel = NSOpenPanel()
  483. openPanel.allowedFileTypes = ["pdf","PDF"]
  484. //MARK: 允许多选还是单选,如果是付费用户允许多选
  485. openPanel.allowsMultipleSelection = true
  486. openPanel.message = NSLocalizedString("To select multiple files press cmd ⌘ button on keyboard and click on the target files one by one.", comment: "")
  487. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
  488. if result == NSApplication.ModalResponse.OK {
  489. quickPassword(urls: openPanel.urls)
  490. }
  491. }
  492. }
  493. func quickPassword(urls: Array<URL>) {
  494. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  495. self.showBatchWindow(type: .security, files: urls)
  496. }
  497. }
  498. func fastTool_FileCompare() { // 文件对比
  499. if !IAPProductsManager.default().isAvailableAllFunction(){
  500. let winC = KMPurchaseCompareWindowController.sharedInstance()
  501. winC?.showWindow(nil)
  502. return
  503. }
  504. let openPanel = NSOpenPanel()
  505. openPanel.allowsMultipleSelection = false
  506. openPanel.allowedFileTypes = ["pdf"]
  507. openPanel.beginSheetModal(for: NSApp.mainWindow!) { result in
  508. if result == .cancel {
  509. return
  510. }
  511. if !openPanel.url!.path.isPDFValid() {
  512. let alert = NSAlert()
  513. alert.alertStyle = .critical
  514. alert.messageText = NSLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.", comment: "")
  515. alert.runModal()
  516. return
  517. }
  518. KMBaseWindowController.checkPassword(url: URL(fileURLWithPath: openPanel.url!.path), type: .owner) { [unowned self] success, resultPassword in
  519. if success {
  520. DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
  521. let controller = KMCompareWindowController(windowNibName: "KMCompareWindowController")
  522. controller.password = resultPassword
  523. controller.filePath = openPanel.url!.path
  524. controller.cancelAction = { [unowned self] contr in
  525. self.view.window?.endSheet((controller.window)!)
  526. }
  527. controller.contentComplete = { [unowned self] controller, pdfCompareContent, result, oldDocument, document in
  528. DispatchQueue.main.async {
  529. self.view.window?.endSheet((controller.window)!)
  530. // self.openContentCompareVC(with: pdfCompareContent, results: result, oldDocument: oldDocument, document: document)
  531. }
  532. }
  533. controller.coveringComplete = { [unowned self] controller, document in
  534. self.view.window?.endSheet((controller.window)!)
  535. // self.openCoveringCompareVC(with: document)
  536. }
  537. controller.fileType = .content
  538. NSWindow.currentWindow().beginSheet(controller.window!)
  539. }
  540. } else {
  541. }
  542. }
  543. }
  544. }
  545. func fastTool_Watermark() { // 水印
  546. if !IAPProductsManager.default().isAvailableAllFunction(){
  547. let winC = KMPurchaseCompareWindowController.sharedInstance()
  548. winC?.showWindow(nil)
  549. return
  550. }
  551. let openPanel = NSOpenPanel()
  552. openPanel.allowedFileTypes = ["pdf","PDF"]
  553. //MARK: 允许多选还是单选,如果是付费用户允许多选
  554. openPanel.allowsMultipleSelection = false
  555. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
  556. if result == NSApplication.ModalResponse.OK {
  557. quickWaterMark(urls: openPanel.urls)
  558. }
  559. }
  560. }
  561. func quickWaterMark(urls: Array<URL>) {
  562. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  563. self.showBatchWindow(type: .watermark, files: urls)
  564. }
  565. }
  566. func fastTool_Background() { // 背景
  567. if !IAPProductsManager.default().isAvailableAllFunction(){
  568. let winC = KMPurchaseCompareWindowController.sharedInstance()
  569. winC?.showWindow(nil)
  570. return
  571. }
  572. let openPanel = NSOpenPanel()
  573. openPanel.allowedFileTypes = ["pdf","PDF"]
  574. //MARK: 允许多选还是单选,如果是付费用户允许多选
  575. openPanel.allowsMultipleSelection = false
  576. openPanel.beginSheetModal(for: NSWindow.currentWindow()) { [self] (result) in
  577. if result == NSApplication.ModalResponse.OK {
  578. quickBackgroudMark(urls: openPanel.urls)
  579. }
  580. }
  581. }
  582. func quickBackgroudMark(urls: Array<URL>) {
  583. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  584. self.showBatchWindow(type: .background, files: urls)
  585. }
  586. }
  587. func fastTool_Print() { // 打印
  588. KMPrintWindowController.openFiles(window: self.view.window!)
  589. }
  590. func fastTool_Insert() { // 插入
  591. if !IAPProductsManager.default().isAvailableAllFunction(){
  592. KMPurchaseCompareWindowController.sharedInstance().showWindow(nil)
  593. return
  594. }
  595. let openPanel = NSOpenPanel()
  596. openPanel.prompt = NSLocalizedString("Insert", comment: "")
  597. openPanel.allowsMultipleSelection = false
  598. openPanel.allowedFileTypes = ["pdf"]
  599. openPanel.beginSheetModal(for: NSApp.mainWindow!) { result in
  600. if result == .OK {
  601. let windowC = KMPDFInsertWindowController(fileURL: openPanel.url!)
  602. windowC.callback = { [weak self] idx, params in
  603. if params.count >= 4 {
  604. if let doc = params.first as? CPDFDocument {
  605. self?.savePDFDocument(doc, password: params[1] as? String ?? "")
  606. }
  607. }
  608. self?.km_endSheet()
  609. }
  610. self.km_beginSheet(windowC: windowC)
  611. }
  612. }
  613. }
  614. func savePDFDocument(_ pdf: CPDFDocument, password: String) -> Void {
  615. DispatchQueue.global(qos: .`default`).async {
  616. var isSuccessfully = false
  617. if pdf.isEncrypted {
  618. let dic = [
  619. CPDFDocumentWriteOption.userPasswordOption : password,
  620. CPDFDocumentWriteOption.ownerPasswordOption : password
  621. ]
  622. isSuccessfully = pdf.write(to: pdf.documentURL, withOptions: dic)
  623. } else {
  624. isSuccessfully = pdf.write(to: pdf.documentURL)
  625. }
  626. if !isSuccessfully {
  627. if let data = pdf.dataRepresentation() {
  628. isSuccessfully = NSData(data: data).write(to: pdf.documentURL, atomically: true)
  629. }
  630. }
  631. DispatchQueue.main.sync {
  632. if isSuccessfully {
  633. let workspace = NSWorkspace.shared
  634. let url = URL(fileURLWithPath: pdf.documentURL?.path ?? "")
  635. workspace.activateFileViewerSelecting([url])
  636. } else {
  637. let alert = NSAlert()
  638. alert.alertStyle = .critical
  639. alert.messageText = NSLocalizedString("Failed to insert page(s)!", comment: "")
  640. alert.runModal()
  641. }
  642. }
  643. }
  644. }
  645. func fastTool_Extract() { // 提取
  646. let openPanel = NSOpenPanel()
  647. openPanel.prompt = "提取"
  648. openPanel.allowsMultipleSelection = false
  649. openPanel.allowedFileTypes = ["pdf"]
  650. openPanel.beginSheetModal(for: NSApp.mainWindow!) { result in
  651. if result == .OK {
  652. DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
  653. let insertWindowController: KMPDFInsertPageWindow = KMPDFInsertPageWindow.init(documentPath: openPanel.url!, toolType: .Extract)
  654. insertWindowController.beginSheetExtractModal(for: self.view.window!) { pdfDocument, pages, oneDocumentPerPage, isDeletePage in
  655. self.extractPageAction(pdfDocument, pages, oneDocumentPerPage, isDeletePage)
  656. }
  657. }
  658. }
  659. }
  660. }
  661. func extractPageAction(_ pdfDocument: CPDFDocument, _ pages: [CPDFPage], _ oneDocumentPerPage: Bool, _ isDeletePage: Bool) -> Void {
  662. if pages.count < 1 {
  663. let alert = NSAlert()
  664. alert.alertStyle = .critical
  665. alert.messageText = NSLocalizedString("Please select two or more pages first to organize.", comment: "")
  666. alert.runModal()
  667. return
  668. }
  669. if !oneDocumentPerPage {
  670. let fileName = pdfDocument.getFileNameAccordingSelctPages(pages)
  671. let outputSavePanel = NSSavePanel()
  672. outputSavePanel.allowedFileTypes = ["pdf"]
  673. outputSavePanel.nameFieldStringValue = fileName
  674. outputSavePanel.beginSheetModal(for: self.view.window!) { result in
  675. if result == .OK {
  676. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  677. let saveFilePath = outputSavePanel.url?.path
  678. DispatchQueue.global().async {
  679. var pdf = CPDFDocument.init()
  680. let success = (pdf!.extractAsOneDocument(withPages: pages, savePath: saveFilePath)) as Bool
  681. DispatchQueue.main.async {
  682. if success {
  683. let workspace = NSWorkspace.shared
  684. let url = URL(fileURLWithPath: saveFilePath!)
  685. workspace.activateFileViewerSelecting([url])
  686. if isDeletePage {
  687. for page in pages {
  688. let indexPage = pdfDocument.index(for: page)
  689. pdfDocument.removePage(at: indexPage)
  690. }
  691. }
  692. }
  693. }
  694. }
  695. }
  696. }
  697. }
  698. } else {
  699. let panel = NSOpenPanel()
  700. panel.canChooseFiles = false
  701. panel.canChooseDirectories = true
  702. panel.canCreateDirectories = true
  703. panel.allowsMultipleSelection = false
  704. panel.beginSheetModal(for: self.view.window!) { result in
  705. if result == .OK {
  706. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  707. let outputURL = panel.url
  708. DispatchQueue.global().async {
  709. let folderName = String(pdfDocument.documentURL!.lastPathComponent.split(separator: ".")[0]) + "_extract"
  710. var filePath = URL(fileURLWithPath: outputURL!.path).appendingPathComponent(folderName).path
  711. var i = 1
  712. let testFilePath = filePath
  713. while FileManager.default.fileExists(atPath: filePath) {
  714. filePath = testFilePath + "\(i)"
  715. i += 1
  716. }
  717. try? FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false, attributes: nil)
  718. let successArray = pdfDocument.extractPerPageDocument(withPages: pages, folerPath: filePath)
  719. DispatchQueue.main.async {
  720. if successArray!.count > 0 {
  721. NSWorkspace.shared.activateFileViewerSelecting(successArray!)
  722. if !isDeletePage {
  723. for page in pages {
  724. let indexPage = pdfDocument.index(for: page)
  725. pdfDocument.removePage(at: indexPage)
  726. }
  727. }
  728. }
  729. }
  730. }
  731. }
  732. }
  733. }
  734. }
  735. }
  736. //MARK: Batch
  737. func showBatchWindow(type: KMBatchCollectionViewType, subType: Int = 0, files: [URL]?) {
  738. let batchWindowController = KMBatchWindowController.init(windowNibName: "KMBatchWindowController")
  739. batchWindowController.window?.makeKeyAndOrderFront("")
  740. // var datas: [KMBatchProcessingTableViewModel] = []
  741. // for file in files! {
  742. // let data = KMBatchProcessingTableViewModel.initWithFilePath(url: file)
  743. // datas.append(data)
  744. // }
  745. batchWindowController.inputData = files ?? []
  746. batchWindowController.type = type
  747. batchWindowController.inputSubType = subType
  748. }
  749. }