12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247 |
- //
- // KMMainViewController+Action.swift
- // PDF Reader Pro
- //
- // Created by wanjun on 2022/12/15.
- //
- import Foundation
- import KMComponentLibrary
- extension KMNThumbnailBaseViewController {
-
- private func insertImageFilePath(imagePath:String,pageDex:Int)->Bool{
- var isSuccessFul:Bool = false
- if (FileManager.default.fileExists(atPath: imagePath)) {
- if let image = NSImage(contentsOfFile: imagePath) {
- isSuccessFul = showDocument?.km_insertPage(image.size, withImage: imagePath, at: UInt(pageDex)) == true
- }
- }
- return isSuccessFul
- }
-
- private func undoDeleteIndexPaths(deleteIndexPath: Set<IndexPath>) {
- var changeIndex:IndexSet = []
- var deletePages:[CPDFPage] = []
-
- let pageIndexs = KMNTools.indexpathsToIndexs(indexpaths: deleteIndexPath)
- for i in pageIndexs {
- if let page = showDocument?.page(at: UInt(i)) {
- deletePages.append(page)
- changeIndex.insert(i)
- }
- }
-
- showDocument?.removePage(at: pageIndexs)
- refreshDatas()
- collectionView.reloadData()
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.undoInsertPages(insertPages: deletePages, indexs: pageIndexs)
- }
- }
-
- private func undoInsertPages(insertPages:[CPDFPage],indexs: IndexSet) {
- var indexpaths = Set<IndexPath>()
- var count: Int = 0
- for index in indexs {
- guard let pageCount = showDocument?.pageCount, index <= pageCount, count < insertPages.count, index != -1 else {
- KMPrint("index invalid. index: \(index)")
- break
- }
-
- showDocument?.insertPageObject(insertPages[count], at: UInt(index))
- indexpaths.insert(IndexPath(item: index, section: 0))
- count += 1
- }
-
- refreshDatas()
- collectionView.reloadData()
- collectionView.scrollToItems(at: indexpaths, scrollPosition: .centeredVertically)
- collectionView.selectionIndexPaths = indexpaths
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.deletePages(indexpaths: indexpaths)
- }
- }
-
- private func undoReplacePages(of targetIndexpaths: Set<IndexPath>, with documents: [CPDFDocument],insertIndexSet:IndexSet,orgPages:[CPDFPage],orgIndexs: IndexSet) {
-
- showDocument?.removePage(at: insertIndexSet)
-
- var indexpaths = Set<IndexPath>()
- var count: Int = 0
- for index in orgIndexs {
- guard let pageCount = showDocument?.pageCount, index <= pageCount, count < orgPages.count, index != -1 else {
- KMPrint("index invalid. index: \(index)")
- break
- }
-
- showDocument?.insertPageObject(orgPages[count], at: UInt(index))
- indexpaths.insert(IndexPath(item: index, section: 0))
- count += 1
- }
-
- refreshDatas()
- collectionView.reloadData()
- collectionView.scrollToItems(at: indexpaths, scrollPosition: .centeredVertically)
- collectionView.selectionIndexPaths = indexpaths
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.replacePages(of: targetIndexpaths, with: documents)
- }
- }
-
- private func undoMovePages(movePages:[CPDFPage],destinationDex:Int,orgPages:[CPDFPage],orgPageDexs:[Int]) {
-
- for (i, page) in orgPages.enumerated() {
- let dragIndex = page.pageIndex()
- let index:UInt = UInt(orgPageDexs[i])
- showDocument?.movePage(at: dragIndex, withPageAt: index)
- }
-
- var indexpaths = Set<IndexPath>()
- for (_, page) in movePages.enumerated() {
- let index = page.pageIndex()
- indexpaths.insert(IndexPath(item: Int(index), section: 0))
- }
- refreshDatas()
- collectionView.reloadData()
- collectionView.scrollToItems(at: indexpaths, scrollPosition: .centeredVertically)
- collectionView.selectionIndexPaths = indexpaths
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.movePages(dragPages: movePages, destinationDex: destinationDex)
- }
- }
-
- func insertFileComplete(newSelectIndexs: Set<IndexPath>){
- refreshDatas()
- collectionView.reloadData()
-
- collectionView.selectionIndexPaths = newSelectIndexs
-
- if newSelectIndexs.isEmpty { return }
-
- let firstIndexPath = newSelectIndexs.first
- collectionView.scrollToItems(at: [firstIndexPath ?? IndexPath(item: 0, section: 0)], scrollPosition: .top)
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.undoDeleteIndexPaths(deleteIndexPath: newSelectIndexs)
- }
- }
-
- public func insertFormPages(insertPages: [CPDFPage],pageDex:Int) {
- let pageIndexDex: Int = pageDex
- var indexpaths = Set<IndexPath>()
-
- let page = insertPages.first
- if page?.document == showDocument {
- for (i, page) in insertPages.enumerated() {
- let isSuccessFul:Bool = showDocument?.insertPageObject(page, at: UInt(pageIndexDex+i)) == true
- if(isSuccessFul == true) {
- indexpaths.insert(IndexPath(item: pageIndexDex+i, section: 0))
- }
- }
- } else {
- if let pasteDocument = page?.document {
- var indexs: IndexSet = IndexSet()
- for (i, page) in insertPages.enumerated() {
- indexs.insert(Int(pasteDocument.index(for: page)))
- indexpaths.insert(IndexPath(item: pageIndexDex+i, section: 0))
- }
-
- let isSuccessFul:Bool = showDocument?.importPages(indexs, from: pasteDocument, at: UInt(pageIndexDex)) == true
- if(isSuccessFul == false){
- return
- }
- }
- }
-
- refreshDatas()
- collectionView.reloadData()
- collectionView.scrollToItems(at: indexpaths, scrollPosition: .centeredVertically)
- collectionView.selectionIndexPaths = indexpaths
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.undoDeleteIndexPaths(deleteIndexPath: indexpaths)
- }
- }
-
- public func insertBlankSize(pageSize: CGSize,pageDex:Int) {
- var indexpaths = Set<IndexPath>()
- let isSuccessFul = showDocument?.insertBlankPage(pageSize: pageSize, at: pageDex)
- if(isSuccessFul == true) {
- indexpaths.insert(IndexPath(item: pageDex, section: 0))
- }
-
- refreshDatas()
- collectionView.reloadData()
- collectionView.scrollToItems(at: indexpaths, scrollPosition: .centeredVertically)
- collectionView.selectionIndexPaths = indexpaths
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.undoDeleteIndexPaths(deleteIndexPath: indexpaths)
- }
- }
-
- public func rotatePages(indexPaths: Set<IndexPath>, rotateAngle: Int) {
- var tIndexPaths: Set<IndexPath> = []
- tIndexPaths = indexPaths
- for targetIndexPath in indexPaths {
- if let page = showDocument?.page(at: UInt(targetIndexPath.item)) {
- var pageRotate = page.rotation + rotateAngle
- if(pageRotate == -90) {
- pageRotate = 270
- } else if (pageRotate == 450) {
- pageRotate = 90
-
- }
- page.rotation = pageRotate
-
- let cellView = collectionView.item(at: targetIndexPath) as? KMNThumbnailCollectionViewItem
- if(cellView != nil) {
- cellView?.thumbnailMode.removeCacheImage()
- }
- }
- }
-
- collectionView.reloadItems(at: tIndexPaths) // Ensure correct type conversion
- collectionView.selectionIndexPaths = tIndexPaths
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.rotatePages(indexPaths: tIndexPaths, rotateAngle: -rotateAngle)
- }
- }
-
- public func reversePages(indexs: IndexSet) {
- if let doc = showDocument {
- var theIdxs = indexs
- var res = false
- for _ in 0 ..< indexs.count {
- guard let first = theIdxs.first else {
- break
- }
- guard let last = theIdxs.last else {
- break
- }
- if first == last {
- break
- }
- res = doc.exchangePage(at: UInt(first), withPageAt: UInt(last))
- if res {
- theIdxs.remove(first)
- theIdxs.remove(last)
-
- }
- }
- if res {
- refreshDatas()
- let selected_indexpaths = KMNTools.indexsToIndexpaths(indexs: indexs)
-
- collectionView.reloadItems(at: selected_indexpaths)
-
- collectionView.selectionIndexPaths = selected_indexpaths
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.reversePages(indexs: indexs)
- }
- }
- }
- }
-
- public func replacePages(of targetIndexpaths: Set<IndexPath>, with documents: [CPDFDocument]) {
- if (targetIndexpaths.count == 0 || documents.count == 0) {
- KMPrint("replace invalid.")
- return
- }
- var index = targetIndexpaths.sorted().first!.item
-
- var deletePages:[CPDFPage] = []
-
- let indexSet = KMNTools.indexpathsToIndexs(indexpaths: targetIndexpaths)
- for i in indexSet {
- if let page = showDocument?.page(at: UInt(i)) {
- deletePages.append(page)
- }
- }
- showDocument?.removePage(at: indexSet)
-
- var tIndexPaths: Set<IndexPath> = []
- for document in documents {
- for i in 0 ..< document.pageCount {
- if let page = document.page(at: i) {
- showDocument?.insertPageObject(page, at: UInt(index))
- tIndexPaths.insert(IndexPath(item: index, section: 0))
- index += 1
- }
- }
- thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: document)
- }
-
- refreshDatas()
- collectionView.reloadData()
- collectionView.selectionIndexPaths = tIndexPaths
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.undoReplacePages(of: targetIndexpaths, with: documents, insertIndexSet: KMNTools.indexpathsToIndexs(indexpaths: tIndexPaths), orgPages: deletePages,orgIndexs: indexSet)
- }
- }
-
- public func deletePages(indexpaths:Set<IndexPath>) {
- var changeIndex:IndexSet = []
- var deletePages:[CPDFPage] = []
-
- let pageIndexs = KMNTools.indexpathsToIndexs(indexpaths: indexpaths)
- for i in pageIndexs {
- if let page = showDocument?.page(at: UInt(i)) {
- deletePages.append(page)
- changeIndex.insert(i)
- }
- }
- showDocument?.removePage(at: changeIndex)
- collectionView.selectionIndexPaths = []
- refreshDatas()
- collectionView.reloadData()
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.undoInsertPages(insertPages: deletePages, indexs: pageIndexs)
- }
- }
-
- public func movePages(dragPages:[CPDFPage],destinationDex:Int) {
- guard !dragPages.isEmpty else { return }
-
- var destinationIndex = destinationDex
- let maxDragPageIndex = dragPages.last?.pageIndex() ?? 0
- let minDragPageIndex = dragPages.first?.pageIndex() ?? 0
-
- let rangeStart = min(Int(minDragPageIndex), destinationIndex)
- let rangeEnd = max(Int(maxDragPageIndex), destinationIndex)
-
- var changePages:[CPDFPage] = []
- var changePageIndexs:[Int] = []
- for index in (rangeStart...rangeEnd) {
- if let changePage = showDocument?.page(at: UInt(index)) {
- changePages.append(changePage)
- changePageIndexs.append(index)
- }
- }
- for dragPage in dragPages {
- let dragIndex = dragPage.pageIndex()
- if destinationIndex > dragIndex {
- destinationIndex -= 1
- showDocument?.movePage(at: dragIndex, withPageAt: UInt(destinationIndex))
- } else {
- showDocument?.movePage(at: dragIndex, withPageAt: UInt(destinationIndex))
- }
- destinationIndex += 1
- }
-
- var selectIndexPaths: Set<IndexPath> = []
-
- for dragPage in dragPages {
- let dragIndex = dragPage.pageIndex()
- selectIndexPaths.insert(IndexPath(item: Int(dragIndex), section: 0))
- }
-
- refreshDatas()
- collectionView.reloadData()
- collectionView.selectionIndexPaths = selectIndexPaths
- if selectIndexPaths.isEmpty { return }
- let firstIndexPath = selectIndexPaths.first
- collectionView.scrollToItems(at: [firstIndexPath ?? IndexPath(item: 0, section: 0)], scrollPosition: .top)
-
- currentUndoManager?.registerUndo(withTarget: self) { [weak self] targetType in
- self?.undoMovePages(movePages: dragPages, destinationDex: destinationDex, orgPages: changePages, orgPageDexs: changePageIndexs)
- }
- }
-
- public func insertFromFilePath(fileNames:[String],formDex:Int,indexDex:UInt,selectIndexs:Set<IndexPath>,completionBlock:@escaping (Set<IndexPath>)->Void)-> Void {
- let path = fileNames[formDex]
- var insertDex = indexDex
- var tSelectIndex = selectIndexs
-
- let pathExtension = URL(fileURLWithPath: path).pathExtension.lowercased()
- if pathExtension == "pdf", let pdf = CPDFDocument(url: URL(fileURLWithPath: path)) {
- if pdf.isLocked {
- NSWindowController.checkPassword(url: pdf.documentURL, type: .owner) { success, resultPassword in
- if (resultPassword.isEmpty == false) {
- pdf.unlock(withPassword: resultPassword)
-
- for i in 0 ..< pdf.pageCount {
- let insertPage = pdf.page(at: i)
- self.showDocument?.insertPageObject(insertPage, at: insertDex)
- tSelectIndex.insert(IndexPath(item: Int(insertDex), section:0))
- insertDex += 1
- }
-
- var tFormDex = formDex
- tFormDex += 1
- self.thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: pdf)
- if(tFormDex < fileNames.count) {
- return self.insertFromFilePath(fileNames: fileNames, formDex: tFormDex, indexDex: indexDex, selectIndexs: tSelectIndex,completionBlock: completionBlock)
- } else {
- self.insertFileComplete(newSelectIndexs: tSelectIndex)
- return completionBlock(tSelectIndex)
- }
- }
- }
- } else {
- for i in 0 ..< pdf.pageCount {
- let insertPage = pdf.page(at: i)
- showDocument?.insertPageObject(insertPage ?? CPDFPage(), at: insertDex)
- tSelectIndex.insert(IndexPath(item: Int(insertDex), section:0))
- insertDex += 1
- }
- var tFormDex = formDex
- tFormDex += 1
- thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: pdf)
- if(tFormDex < fileNames.count) {
- return insertFromFilePath(fileNames: fileNames, formDex: tFormDex, indexDex: indexDex, selectIndexs: tSelectIndex,completionBlock: completionBlock)
- } else {
- insertFileComplete(newSelectIndexs: tSelectIndex)
- return completionBlock(tSelectIndex)
- }
-
- }
- } else if supportDragFileTypes().contains(pathExtension) {
- if KMConvertPDFManager.supportImages().contains(pathExtension) {
- let isSueccessFul = insertImageFilePath(imagePath: path, pageDex: Int(indexDex))
- if(isSueccessFul) {
- tSelectIndex.insert(IndexPath(item: Int(insertDex), section:0))
- insertDex += 1
- }
-
- var tFormDex = formDex
- tFormDex += 1
- if(tFormDex < fileNames.count) {
- return insertFromFilePath(fileNames: fileNames, formDex: tFormDex, indexDex: indexDex, selectIndexs: tSelectIndex,completionBlock: completionBlock)
- } else {
- insertFileComplete(newSelectIndexs: tSelectIndex)
- return completionBlock(tSelectIndex)
- }
-
- } else {
- KMNConvertTool.convertOffice(filePath: path) { convertPDFPath in
- if (convertPDFPath != nil) {
- let pathExtension = URL(fileURLWithPath: convertPDFPath!).pathExtension.lowercased()
- if pathExtension == "pdf", let pdf = CPDFDocument(url: URL(fileURLWithPath: convertPDFPath!)) {
- for i in 0 ..< pdf.pageCount {
- let insertPage = pdf.page(at: i)
- self.showDocument?.insertPageObject(insertPage, at: insertDex)
- tSelectIndex.insert(IndexPath(item: Int(insertDex), section:0))
- insertDex += 1
- }
- self.thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: pdf)
-
- var tFormDex = formDex
- tFormDex += 1
- if(tFormDex < fileNames.count) {
- return self.insertFromFilePath(fileNames: fileNames, formDex: tFormDex, indexDex: indexDex, selectIndexs: tSelectIndex,completionBlock: completionBlock)
- } else {
- self.insertFileComplete(newSelectIndexs: tSelectIndex)
- return completionBlock(tSelectIndex)
- }
-
- }
- }
- }
- }
- }
- }
-
- public func extractPages(indexpaths: Set<IndexPath>, oneDocumentPerPage: Bool, callback: @escaping KMResultBlock) {
- let pageIndexs = KMNTools.indexpathsToIndexs(indexpaths: indexpaths)
- let oneDocument = !oneDocumentPerPage
- let document = self.showDocument!
- /// 提取的页面
- var extractPages: Array<CPDFPage> = []
- for i in pageIndexs {
- guard let page = document.page(at: UInt(i)) else {
- continue
- }
- extractPages.append(page)
- }
-
- if (oneDocument) { /// 提取为一个文档
- var fileName = document.documentURL.deletingPathExtension().lastPathComponent
- fileName.append(" pages ")
- fileName.append(KMNTools.newParseSelectedIndexs(selectedIndex: pageIndexs.sorted()))
- fileName.append(".pdf")
- NSPanel.savePanel(self.view.window!, true) { panel in
- panel.nameFieldStringValue = fileName
- } completion: { response, url, isOpen in
- if (response != .OK) {
- callback(.cancel)
- return
- }
-
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
- DispatchQueue.global().async {
- var success = false
- let pdf = CPDFDocument.init()
- success = pdf!.extractAsOneDocument(withPages: extractPages, savePath: url!.path)
- DispatchQueue.main.async {
- if (success == false) {
- callback(.failure)
- return
- }
-
- if (isOpen == false) {
- NSWorkspace.shared.activateFileViewerSelecting([url!])
- } else {
- NSDocumentController.shared.km_safe_openDocument(withContentsOf: url!, display: true) { _, _, _ in
- }
- }
- callback(.success, [url!])
- }
- }
- }
- }
- } else {
- let panel = NSOpenPanel()
- panel.canChooseFiles = false
- panel.canChooseDirectories = true
- panel.canCreateDirectories = true
- panel.allowsMultipleSelection = false
- panel.beginSheetModal(for: self.view.window!) { response in
- if response != .OK {
- callback(.cancel)
- return
- }
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
- let outputURL = panel.url
- DispatchQueue.global().async {
- let folderName = String((document.documentURL.lastPathComponent.split(separator: ".")[0])) + "_extract"
-
- var filePath = URL(fileURLWithPath: outputURL!.path).appendingPathComponent(folderName).path
- var i = 1
- let testFilePath = filePath
- while FileManager.default.fileExists(atPath: filePath) {
- filePath = testFilePath + "\(i)"
- i += 1
- }
-
- try? FileManager.default.createDirectory(atPath: filePath, withIntermediateDirectories: false, attributes: nil)
-
- var successArray: [URL]?
- successArray = document.extractPerPageDocument(withPages: extractPages, folerPath: filePath)
-
- DispatchQueue.main.async {
- if successArray!.count == 0 {
- callback(.failure)
- return
- }
- NSWorkspace.shared.activateFileViewerSelecting(successArray!)
- callback(.success, successArray ?? NSURL())
- }
- }
- }
- }
- }
- }
-
- // MARK: - MenuItem
- @objc public func copyMenuItemAciton() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- let winC = KMPurchaseCompareWindowController.sharedInstance()
- winC?.showWindow(nil)
- return
- }
- KMNThumbnailManager.manager.copyPages = []
-
- let indexpaths = collectionView.selectionIndexPaths
- for indexpath in indexpaths.sorted() {
- guard let page = showDocument?.page(at: UInt(indexpath.item))?.copy() as? CPDFPage else {
- continue
- }
-
- KMNThumbnailManager.manager.copyDocument.append(showDocument ?? CPDFDocument())
- KMNThumbnailManager.manager.copyPages.append(page)
- }
- }
-
- @objc public func pastMenuItemAciton(menuitemProperty:ComponentMenuitemProperty) {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- let winC = KMPurchaseCompareWindowController.sharedInstance()
- winC?.showWindow(nil)
- return
- }
-
- var pastIndex = 1
- let point = menuitemProperty.representedObject as? NSPoint
-
- let selectedIndexPaths = collectionView.selectionIndexPaths
- if(selectedIndexPaths.count > 0) {
- let maxmumIndexPath = selectedIndexPaths.max(by: { $0 < $1 })
- pastIndex = ((maxmumIndexPath?.item ?? 0) + 1)
- } else {
- if(point != nil) {
- let pointInCollectionView = collectionView.convert(point!, from: nil)
-
- let visibleItems = collectionView.visibleItems()
- let mouseX = pointInCollectionView.x
- let mouseY = pointInCollectionView.y
-
- // 获取当前行的所有 cell
- let currentRowItems = visibleItems.filter { item in
- if let indexPath = collectionView.indexPath(for: item),
- let cellAttributes = collectionView.layoutAttributesForItem(at: indexPath) {
- return cellAttributes.frame.minY <= mouseY && cellAttributes.frame.maxY >= mouseY
- }
- return false
- }
- if(mouseX < 24) { //点击区域在最左边
- // 找到最近右边的 cell
- let rightMostCell = currentRowItems.compactMap { collectionView.indexPath(for: $0) }
- .filter { indexPath in
- let cellRect = collectionView.layoutAttributesForItem(at: indexPath)?.frame
- return cellRect?.minX ?? 0 > mouseX // 只选择右侧的 cell
- }
- .sorted { ($0.item < $1.item) } // 按 item 的顺序排列
- .first // 选择第一个,即最右边的 cell
- pastIndex = (rightMostCell?.item ?? (Int(showDocument?.pageCount ?? 1)))
- } else {
- // 找到最近右边的 cell
- let leftMostCell = currentRowItems.compactMap { collectionView.indexPath(for: $0) }
- .filter { indexPath in
- let cellRect = collectionView.layoutAttributesForItem(at: indexPath)?.frame
- return cellRect?.maxX ?? 0 < mouseX // 只选择左侧的 cell
- }
- .sorted { $0.item > $1.item } // 按 item 的逆序排列,以选择最近的左侧 cell
- .first // 选择第一个,即最近的左边的 cell
-
- pastIndex = (leftMostCell?.item ?? (Int(showDocument?.pageCount ?? 1)) - 1) + 1
- }
- } else {
- pastIndex = Int(showDocument?.pageCount ?? 0)
- }
- }
- let copyPages = KMNThumbnailManager.manager.copyPages
-
- insertFormPages(insertPages: copyPages, pageDex: pastIndex)
- }
-
- @objc public func cutMenuItemAciton() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- let winC = KMPurchaseCompareWindowController.sharedInstance()
- winC?.showWindow(nil)
- return
- }
-
- KMNThumbnailManager.manager.copyPages = []
- let indexpaths = collectionView.selectionIndexPaths
- for indexpath in indexpaths.sorted() {
- guard let page = showDocument?.page(at: UInt(indexpath.item))?.copy() as? CPDFPage else {
- continue
- }
-
- KMNThumbnailManager.manager.copyDocument.append(showDocument ?? CPDFDocument())
- KMNThumbnailManager.manager.copyPages.append(page)
- }
-
- deletePages(indexpaths: indexpaths)
- }
-
- @objc public func deleteMenuItemAciton() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- let winC = KMPurchaseCompareWindowController.sharedInstance()
- winC?.showWindow(nil)
- return
- }
- let indexpaths = collectionView.selectionIndexPaths
- deletePages(indexpaths: indexpaths)
- }
-
- @objc func sharePageItemAction(menuItem:NSMenuItem) {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- let winC = KMPurchaseCompareWindowController.sharedInstance()
- winC?.showWindow(nil)
- return
- }
- let indexpaths = collectionView.selectionIndexPaths
-
- let doucument = showDocument
- let filename : String = doucument?.documentURL.lastPathComponent ?? ""
- let folderPath = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(filename)) ?? ""
-
- try? FileManager.default.removeItem(atPath: folderPath)
-
- let pdfdocument = CPDFDocument()
- let ret = pdfdocument?.importPages(KMNTools.indexpathsToIndexs(indexpaths: indexpaths), from: doucument, at: 0) ?? false
- let url = URL(fileURLWithPath: folderPath)
- if ret {
- let success = pdfdocument?.write(toFile: folderPath)
- let represent = menuItem.representedObject as? NSSharingService
- represent?.perform(withItems: [url])
- }
- }
-
- @objc public func displayPageSizeAction() {
- let indexpaths = collectionView.selectionIndexPaths
- isShowPageSize = !isShowPageSize
- collectionView.selectionIndexPaths = indexpaths
- }
-
- private func insertFilePath(filePath:String,pdfPassword:String?) {
- var selectedIndexPaths = collectionView.selectionIndexPaths
- if(selectedIndexPaths.count <= 0) {
- selectedIndexPaths = pdfviewSelectionIndexPaths
- }
- let insertPDF = KMNPDFInsertPDFWindowController(showDocument, filePath: filePath, password: pdfPassword, selectionIndexPaths: selectedIndexPaths)
- insertPDF.pdfCallback = { [weak self] fileAttribute, insertIdx in
- let doc = fileAttribute.pdfDocument
-
- var insertPages: [CPDFPage] = []
-
- for number in fileAttribute.fetchSelectPages() {
- if let page = doc?.page(at: UInt(number-1)) {
- insertPages.append(page)
- }
- }
- self?.thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: doc)
-
- self?.insertFormPages(insertPages: insertPages, pageDex: insertIdx)
- }
- insertPDF.fileCallback = { [weak self] filePath, insertIdx in
- self?.insertFromFilePath(fileNames: [filePath], formDex: 0, indexDex: UInt(insertIdx), selectIndexs: [], completionBlock: { newSelectIndexs in
- })
- }
-
- insertPDF.own_beginSheetModal(for: self.view.window, completionHandler: nil)
- }
-
- // MARK: - public
- func updateThumnailItem(updateIndexPaths: Set<IndexPath>,isSelect:Bool) {
-
- for targetIndexPath in updateIndexPaths {
- let cellView = collectionView.item(at: targetIndexPath) as? KMNThumbnailCollectionViewItem
- if(cellView != nil) {
- cellView?.thumbnailMode.removeCacheImage()
- }
- }
-
- collectionView.reloadItems(at: updateIndexPaths)
- if(isSelect) {
- if updateIndexPaths.isEmpty { return }
- let firstIndexPath = updateIndexPaths.first
- collectionView.scrollToItems(at: [firstIndexPath ?? IndexPath(item: 0, section: 0)], scrollPosition: .top)
-
- collectionView.selectionIndexPaths = updateIndexPaths
- }
- }
-
- func updateAllThumnailItems() {
- KMNThumbnailManager.clearCacheFilePath(filePath: showDocument?.documentURL.path ?? "")
-
- collectionView.reloadData()
- }
-
- @objc public func insertFromPDFAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
-
- let openPanel = NSOpenPanel()
- openPanel.allowedFileTypes = supportDragFileTypes()
- openPanel.allowsMultipleSelection = false
- openPanel.beginSheetModal(for: NSWindow.currentWindow()) {[weak self] result in
- if result == NSApplication.ModalResponse.OK {
- let fileURL = openPanel.url
- if(fileURL?.pathExtension == "pdf") {
- let pdfDoc = CPDFDocument(url: fileURL)
- if let data = pdfDoc?.isLocked, data {
- DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
- NSWindowController.checkPassword(url: fileURL ?? NSURL.fileURL(withPath: ""), type: .owner) { result, pwd in
- if (pwd.isEmpty == false) {
- self?.insertFilePath(filePath: fileURL?.path ?? "", pdfPassword: pwd)
- }
- }
- }
- } else {
- self?.insertFilePath(filePath: fileURL?.path ?? "", pdfPassword: nil)
- }
- } else {
- self?.insertFilePath(filePath: fileURL?.path ?? "", pdfPassword: nil)
- }
- }
- }
- }
-
- @objc public func insertFromBlankAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
-
- var selectedIndexPaths = collectionView.selectionIndexPaths
- if(selectedIndexPaths.count <= 0) {
- selectedIndexPaths = pdfviewSelectionIndexPaths
- }
-
- let insertPDF = KMNPDFInsertBlankWindowController(self.showDocument, selectionIndexPaths: selectedIndexPaths)
- insertPDF.callback = { [weak self] pageSize, insertIdx in
- self?.insertBlankSize(pageSize: pageSize, pageDex: insertIdx)
- }
- insertPDF.own_beginSheetModal(for: self.view.window, completionHandler: nil)
- }
-
- public func insertFromClipboardAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
- var selectedIndexPaths = collectionView.selectionIndexPaths
- if(selectedIndexPaths.count <= 0) {
- selectedIndexPaths = pdfviewSelectionIndexPaths
- }
- var maxmumIndex = 1
- if(selectedIndexPaths.count > 0) {
- let maxmumIndexPath = selectedIndexPaths.max(by: { $0 < $1 })
- maxmumIndex = (maxmumIndexPath?.item ?? 0) + 1
- }
-
- var error: NSError?
- guard let document: CPDFDocument = KMNConvertTool.openDocumentWithImageFromPasteboard(NSPasteboard.general, error: &error) else {
- return
- }
- var insetPages:[CPDFPage] = []
- for i in 0 ..< document.pageCount {
- let page: CPDFPage = (document.page(at: i))
- insetPages.append(page)
- }
- if insetPages.count > 0 {
- insertFormPages(insertPages: insetPages, pageDex: maxmumIndex)
- }
-
- self.thumbnailBaseViewDelegate?.insertPDFThumbnailViewControlle?(pageEditVC: self, pdfDocment: document)
- let ips: Set<IndexPath> = [IndexPath(item: maxmumIndex, section: 0)]
-
- refreshDatas()
- collectionView.selectionIndexPaths = ips
- collectionView.scrollToItems(at: ips, scrollPosition: .centeredVertically)
- }
-
- public func insertFromScannerAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
-
- let selectedIndexPaths = collectionView.selectionIndexPaths
- let maxmumIndexPath = selectedIndexPaths.max(by: { $0 < $1 })
-
- let vc = KMDeviceBrowserWindowController.shared
- vc.type = .scanner
- vc.importScannerFileCallback = { [weak self] (url: NSURL) -> Void in
- if let imag = NSImage(contentsOfFile: url.path! ) {
- let index = (maxmumIndexPath?.item ?? 0) + 1
- _ = self?.showDocument?.km_insertPage(imag.size, withImage: url.path! , at:UInt(index))
-
- self?.refreshDatas()
-
- let ips: Set<IndexPath> = [IndexPath(item: index, section: 0)]
-
- self?.collectionView.selectionIndexPaths = ips
- self?.collectionView.scrollToItems(at: ips, scrollPosition: .centeredVertically)
- }
- }
- vc.showWindow(nil)
- vc.window?.center()
- }
-
-
- public func canUndo()->Bool {
- return true
- }
-
- public func undoPDFAction() {
- currentUndoManager?.undo()
- }
-
- public func canRodo()->Bool {
- return true
- }
-
- public func redoPDFAction() {
- currentUndoManager?.redo()
- }
-
- @objc public func extractPDFAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
-
- let selectedIndexPaths = collectionView.selectionIndexPaths
-
- if selectedIndexPaths.count < 1 {
- _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Please select one or more pages first to organize."), type: .info, fromView: self.view, point:CGPointMake(self.view.frame.origin.x + self.view.frame.size.width/2, self.view.bounds.size.height - 30))
- return
- }
-
- let extractPDF = KMNExtractPDFWindowController(self.showDocument, selectionIndexPaths: collectionView.selectionIndexPaths)
- extractPDF.callback = { [weak self] oneDocumentPerPage, isDeletePage in
- extractPDF.own_closeEndSheet()
- if let _ = self?.showDocument {
- self?.extractPages(indexpaths: selectedIndexPaths, oneDocumentPerPage: oneDocumentPerPage, callback: { [weak self] result, params in
- if (result == .failure || result == .cancel) {
- return
- }
- if (isDeletePage) {
- self?.deletePages(indexpaths: selectedIndexPaths)
- }
- })
- }
- }
- extractPDF.own_beginSheetModal(for: self.view.window, completionHandler: nil)
- }
-
- @objc public func replacePDFAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
-
- let selectedIndexPaths = collectionView.selectionIndexPaths
-
- if selectedIndexPaths.count < 1 {
- _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Please select one or more pages first to organize."), type: .info, fromView: self.view, point:CGPointMake(self.view.frame.origin.x + self.view.frame.size.width/2, self.view.bounds.size.height - 30))
- return
- }
-
- self.km_open_file_multi { [unowned self] index, params in
- if (self.fetchProgressBlockParamsIsPasswordFile(params: params)) { // 加密文档进度回调
- return
- }
-
- let tFileUrl = self.fetchProgressBlockParamsForFileUrl(params: params)
- let pdfExtensions = KMNConvertTool.supportPDFFileType()
- if let exn = tFileUrl?.pathExtension, pdfExtensions.contains(exn) {
- if (tFileUrl!.path.isPDFValid() == false) {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = KMLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.")
- alert.runModal()
- }
- }
- } completionBlock: { [unowned self] documents in
- self.replacePages(of: selectedIndexPaths, with: documents)
- }
- }
-
- public func splitPDFAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
-
- let selectedIndexPaths = collectionView.selectionIndexPaths
-
- if collectionView.selectionIndexPaths.count < 1 {
- _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Please select one or more pages first to organize."), type: .info, fromView: self.view, point:CGPointMake(self.view.bounds.origin.x + self.view.bounds.size.width/2, self.view.bounds.size.height - 30))
- return
- }
-
- let splitPDF = KMNSplitPDFWindowController(self.showDocument,selectionIndexPaths: selectedIndexPaths)
- splitPDF.own_beginSheetModal(for: self.view.window, completionHandler: nil)
- }
-
- public func reversePDFAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
- let selectedIndexPaths = collectionView.selectionIndexPaths
-
- if selectedIndexPaths.count < 2 {
- _ = KMNCustomAlertView.alertView(message: KMLocalizedString("No page selected. Please select at least two pages to organize."), type: .info, fromView: self.view, point:CGPointMake(self.view.frame.origin.x + self.view.frame.size.width/2, self.view.bounds.size.height - 30))
- return
- }
- reversePages(indexs:KMNTools.indexpathsToIndexs(indexpaths: selectedIndexPaths))
- }
-
- @objc public func rotatePageLeftAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
-
- if collectionView.selectionIndexPaths.count < 1 {
- _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Please select one or more pages first to organize."), type: .info, fromView: self.view, point:CGPointMake(self.view.frame.origin.x + self.view.frame.size.width/2, self.view.bounds.size.height - 30))
- return
- }
- rotatePages(indexPaths: collectionView.selectionIndexPaths, rotateAngle: -90)
- }
-
- @objc public func rotatePageRightAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
-
- if collectionView.selectionIndexPaths.count < 1 {
- _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Please select one or more pages first to organize."), type: .info, fromView: self.view, point:CGPointMake(self.view.frame.origin.x + self.view.frame.size.width/2, self.view.bounds.size.height - 30))
- return
- }
- rotatePages(indexPaths: collectionView.selectionIndexPaths, rotateAngle: 90)
- }
-
- public func deletePageAction() {
- if IAPProductsManager.default().isAvailableAllFunction() == false {
- KMPurchaseCompareWindowController.sharedInstance()?.showWindow(nil)
- return
- }
-
- let selectedIndexPaths = collectionView.selectionIndexPaths
-
- if selectedIndexPaths.count < 1 {
- _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Please select one or more pages first to organize."), type: .info, fromView: self.view, point:CGPointMake(self.view.frame.origin.x + self.view.frame.size.width/2, self.view.bounds.size.height - 30))
- return
- } else if selectedIndexPaths.count == (showDocument?.pageCount ?? 0) {
- _ = KMNCustomAlertView.alertView(message: KMLocalizedString("Can not delete all pages."), type: .info, fromView: self.view, point:CGPointMake(self.view.frame.origin.x + self.view.frame.size.width/2, self.view.bounds.size.height - 30))
- return
-
- }
-
- deletePages(indexpaths: selectedIndexPaths)
- }
-
- // MARK: - private
- private func fetchProgressBlockParamsIsPasswordFile(params: Any...) -> Bool {
- if (params.count <= 2) {
- return false
- }
- return true
- }
-
- private func fetchProgressBlockParamsForFileUrl(params: Any...) -> URL? {
- if (params.count < 2) {
- return nil
- }
- return params[1] as? URL
- }
-
- private func km_open_file_multi(type: KMPasswordInputWindowType = .open, progressBlock: ((_ index: Int, _ params: Any...)->Void)? = nil, completionBlock:@escaping ([CPDFDocument])->Void) {
- NSPanel.km_open_multi_success(self.view.window!) { panel in
- var array: [String] = []
- for fileType in KMConvertPDFManager.supportFileType() {
- array.append(fileType)
- }
- panel.allowedFileTypes = KMNConvertTool.pdfExtensions + array
- } completion: { urls in
- self.km_add_file_multi(fileUrls: urls, type: type, progressBlock: progressBlock, completionBlock: completionBlock)
- }
- }
-
- private func km_add_file_multi(fileUrls: [URL] ,type: KMPasswordInputWindowType = .open, progressBlock: ((_ index: Int, _ params: Any...)->Void)? = nil, completionBlock:@escaping ([CPDFDocument])->Void) {
- var pdfUrls: [URL] = []
- var imageUrls: [URL] = []
- var officeUrls: [URL] = []
- for url in fileUrls {
- let type = url.pathExtension.lowercased()
- if (KMNConvertTool.isPDFType(type)) {
- pdfUrls.append(url)
- }
- if (KMNConvertTool.isImageType(type)) {
- imageUrls.append(url)
- }
- if (KMNConvertTool.isOfficeType(type)) {
- officeUrls.append(url)
- }
- }
-
- if (officeUrls.count == 0) {
- self.km_add_pdf_multi(fileUrls: pdfUrls, type: type, progressBlock: progressBlock) { documents in
- var index = documents.count
- var _documents: [CPDFDocument] = []
- for imageUrl in imageUrls {
- index += 1
- let document = CPDFDocument()
- let image = NSImage(contentsOfFile: imageUrl.path)
- let _ = document?.km_insertPage(image?.size ?? .zero, withImage: imageUrl.path, at: 0)
- _documents.append(document!)
-
- if let _callback = progressBlock { // 回调进度
- _callback(index, document as Any, imageUrl)
- }
- }
-
- completionBlock(documents + _documents)
- }
- return
- }
-
- self.km_add_office_multi(fileUrls: officeUrls) { [unowned self] fileUrlStrings in
- var officeDocuments: [CPDFDocument] = []
- var index = 0
- for fileUrlString in fileUrlStrings {
- index += 1
- let document = CPDFDocument(url: URL(fileURLWithPath: fileUrlString))
- officeDocuments.append(document!)
-
- if let _callback = progressBlock { // 回调进度
- _callback(index, document as Any, URL(fileURLWithPath: fileUrlString))
- }
- }
-
- self.km_add_pdf_multi(fileUrls: pdfUrls) { documents in
- var index = documents.count + officeDocuments.count
- var _documents: [CPDFDocument] = []
- for imageUrl in imageUrls {
- index += 1
- let document = CPDFDocument()
- let image = NSImage(contentsOfFile: imageUrl.path)
- let _ = document?.km_insertPage(image!.size, withImage: imageUrl.path, at: 0)
- _documents.append(document!)
-
- if let _callback = progressBlock { // 回调进度
- _callback(index, document as Any, imageUrl)
- }
- }
-
- completionBlock(officeDocuments + documents + _documents)
- }
- }
- }
-
- private func km_add_pdf_multi(fileUrls: [URL] ,type: KMPasswordInputWindowType = .open, progressBlock: ((_ index: Int, _ params: Any...)->Void)? = nil, completionBlock:@escaping ([CPDFDocument])->Void) {
- var results: [CPDFDocument] = []
-
- self.lockedFiles.removeAll()
- var index = 0
- for url in fileUrls {
- let document = CPDFDocument(url: url)
- if (document!.isLocked) {
- self.lockedFiles.append(url)
- continue
- }
-
- if let _document = document {
- results.append(_document)
- }
-
- index += 1
- if let _callback = progressBlock {
- _callback(index, ((document != nil) ? document : CPDFDocument()) as Any, url)
- }
- }
-
- if (self.lockedFiles.count == 0) {
- completionBlock(results)
- return
- }
-
- self._openPasswordWindow_loop(fileUrl: self.lockedFiles.first!, type: type) { params in
- index += 1
- if (params.count <= 2) { // 参数错误
- if let _callback = progressBlock { // 回调进度
- _callback(index)
- }
- return
- }
-
- let fileUrl = params[0] as! URL
- let result = params[1] as! KMPasswordInputWindowResult
- let password = params[2] as? String
- if (result == .cancel) {
- if let _callback = progressBlock { // 回调进度
- _callback(index, CPDFDocument() as Any, fileUrl, result)
- }
- return
- }
-
- let document = CPDFDocument(url: fileUrl)
- if let _password = password { // 将文档进行解密
- document?.unlock(withPassword: _password)
- }
- if let _callback = progressBlock { // 回调进度
- _callback(index, document as Any, fileUrl, result, password as Any)
- }
- // 将文档加入返回数据
- if let _document = document {
- results.append(_document)
- }
- } completionBlock: {
- completionBlock(results)
- }
- }
-
- private func km_add_office_multi(fileUrls: [URL], completionBlock:@escaping ([String])->Void) -> Void {
- var fileUrlStrings: [String] = []
-
- let dispatchGroup = Dispatch.DispatchGroup()
- for (index, fileUrl) in fileUrls.enumerated() {
- let filePath = fileUrl.path
- let folderPath = "convertToPDF_\(index).pdf"
- let savePath: String? = folderPath.kUrlToPDFFolderPath() as String
- if (savePath == nil) {
- continue
- }
-
- dispatchGroup.enter()
- KMConvertPDFManager.convertFile(filePath, savePath: savePath!) { success, errorDic in
- if errorDic != nil || !success || !FileManager.default.fileExists(atPath: savePath!) {
- dispatchGroup.leave()
-
- if FileManager.default.fileExists(atPath: savePath!) {
- try?FileManager.default.removeItem(atPath: savePath!)
- }
- let alert = NSAlert.init()
- alert.alertStyle = .critical
- var infoString = ""
- if errorDic != nil {
- for key in (errorDic! as Dictionary).keys {
- infoString = infoString.appendingFormat("%@\n", errorDic![key] as! CVarArg)
- }
- }
- alert.informativeText = KMLocalizedString("Please install Microsoft Office to create PDFs from Office files")
- alert.messageText = KMLocalizedString("Failed to Create PDF", comment: "")
- alert.addButton(withTitle: KMLocalizedString("OK"))
- alert.runModal()
- return
- }
-
- if !savePath!.isPDFValid() {
- dispatchGroup.leave()
-
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = KMLocalizedString("An error occurred while opening this document. The file is damaged and could not be repaired.")
- alert.runModal()
- return
- }
-
- fileUrlStrings.append(savePath!)
- dispatchGroup.leave()
- }
- }
-
- dispatchGroup.notify(queue: DispatchQueue.main) {
- completionBlock(fileUrlStrings)
- }
- }
-
- fileprivate func _openPasswordWindow_loop(fileUrl: URL, type: KMPasswordInputWindowType, progressBlock: ((_ params: Any...)->Void)?, completionBlock:@escaping ()->Void) {
- KMPasswordInputWindow.openWindow(window: self.view.window!, type: type, url: fileUrl) { [weak self] result, password in
- // 将结果返回
- if let _callback = progressBlock {
- _callback(fileUrl, result, password as Any)
- }
-
- // 进行下一个
- self?.lockedFiles.removeFirst()
- if let _fileUrl = self?.lockedFiles.first {
- self?._openPasswordWindow_loop(fileUrl: _fileUrl, type: type, progressBlock: progressBlock, completionBlock: completionBlock)
- } else {
- completionBlock()
- }
- }
- }
- }
|