12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829 |
- //
- // KMPDFEditAppendWindow.m
- // PDF Reader
- //
- // Created by 丁林圭 on 2017/5/11.
- // Copyright © 2017年 zhangjie. All rights reserved.
- //
- #import "KMPDFEditAppendWindow.h"
- #import "PasswordWindowController.h"
- #import "KMFileAttribute.h"
- #import <Quartz/Quartz.h>
- #import "KMSavePanelAccessoryController.h"
- #import "KMBatchTableRowView.h"
- #import "KMBlankView.h"
- #import <Masonry/Masonry.h>
- #import "KMImageAccessoryController.h"
- #if VERSION_DMG
- #import <PDF_Master-Swift.h>
- #else
- #import <PDF_Master-Swift.h>
- #endif
- #import "CTBrowserWindowController.h"
- #import "NSButton+TitleColor.h"
- #import "KMPDFMergeFileNameTabelViewCell.h"
- #import "PDFPage_SKExtensions.h"
- #import "TextFieldFormatter.h"
- @interface KMPDFEditAppendTabelViewCell()
- @property (assign) IBOutlet NSTextField *pageCountLabel;
- @property (assign) IBOutlet NSTextField *indexLabel;
- @property (assign) IBOutlet NSTextField *fileNameLabel;
- @property (assign) IBOutlet NSTextField *pageRangeLabel;
- @property (assign) IBOutlet NSTextField *sizeLabel;
- @property (assign) IBOutlet NSImageView *fileImage;
- @property (assign) IBOutlet NSComboBox *pageRangeBox;
- @property (nonatomic, copy) void(^removeCallBack)(void);
- @end
- @implementation KMPDFEditAppendTabelViewCell
- - (void)dealloc {}
- - (IBAction)buttonClicked_Remove:(id)sender {
- if (self.removeCallBack) {
- self.removeCallBack();
- }
- }
- - (void)awakeFromNib {
- [super awakeFromNib];
- }
- @end
- @interface KMAppendPDFPage: PDFPage
- @property (nonatomic, strong)PDFPage *drawingPage;
- @end
- @implementation KMAppendPDFPage
- - (void)dealloc {}
- //- (void)drawWithBox:(PDFDisplayBox)box {
- // [super drawWithBox:box];
- //
- // CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
- // CGSize pageSize = [self boundsForBox:kPDFDisplayBoxCropBox].size;
- // [self drawPageWithContext:context Page:self.drawingPage PageSize:pageSize];
- //}
- - (void)drawWithBox:(PDFDisplayBox)box toContext:(CGContextRef)context {
- [super drawWithBox:box toContext:context];
- CGSize pageSize = [self boundsForBox:kPDFDisplayBoxCropBox].size;
- [self drawPageWithContext:context Page:self.drawingPage PageSize:pageSize];
- }
- - (void)drawPageWithContext:(CGContextRef) context
- Page:(PDFPage *)page
- PageSize:(CGSize)pageSize
- {
- CGSize originalSize = [page boundsForBox:kPDFDisplayBoxCropBox].size;
-
- //如果page的旋转角度为90,或者270,宽高交换
- if (page.rotation%180) {
- originalSize = CGSizeMake(originalSize.height, originalSize.width);
- }
- CGFloat wRatio = pageSize.width/originalSize.width;
- CGFloat hRatio = pageSize.height/originalSize.height;
- CGFloat ratio = MIN(wRatio, hRatio);
-
- CGContextSaveGState(context);
- CGFloat xTransform = (pageSize.width - originalSize.width * ratio)/2;
- CGFloat yTransform = (pageSize.height - originalSize.height * ratio)/2;
- CGContextTranslateCTM(context, xTransform, yTransform);
- CGContextScaleCTM(context, ratio, ratio);
-
- NSImage *image = [NSImage imageNamed:@"KMImageNameWatermark"];
- if (@available(macOS 10.12, *)) {
- [page drawWithBox:kPDFDisplayBoxCropBox toContext:context];
-
- // [image drawInRect:NSMakeRect(10, 350, 100, 30) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
- NSData *data = [image TIFFRepresentation];
- CGImageRef imageRef;
- if (data) {
- CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)data, NULL);
- imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
-
- CGContextDrawImage(context, CGRectMake(0, pageSize.height-32, 120, 32), imageRef);
- }
- [page transformContext:context forBox:kPDFDisplayBoxCropBox];
- } else {
- [NSGraphicsContext saveGraphicsState];
- [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:YES]];
- [page drawWithBox:kPDFDisplayBoxCropBox toContext:context];
-
- [NSGraphicsContext restoreGraphicsState];
- [page transformContext:context forBox:kPDFDisplayBoxCropBox];
- }
- CGContextRestoreGState(context);
- }
- @end
- #define MyTableCellViewDataType @"MyTableCellViewDataType"
- static NSString * const KMTableColumnFileNameID = @"fileName";
- static NSString * const KMTableColumnPageRangeID = @"pageRangD";
- static NSString * const KMTableColumnSizeID = @"size";
- static KMPDFEditAppendWindow *windowController = nil;
- @interface KMPDFEditAppendWindow ()<NSTableViewDataSource,NSTableViewDelegate,NSComboBoxDelegate, KMSelectPopButtonDelegate>
- {
- BOOL _isSuccessfully;
- NSInteger _insertRow;
- }
- @property (nonatomic, assign) NSInteger insertRow;
- @property (weak) IBOutlet NSBox *tableHeaderBox;
- @property (weak) IBOutlet NSTextField *fileNameLabel;
- @property (weak) IBOutlet NSTextField *pageRangeLabel;
- @property (weak) IBOutlet NSTextField *sizeLabel;
- @property (weak) IBOutlet NSBox *tableHeaderBottomLine;
- @property (assign) IBOutlet NSTableView *tableView;
- @property (assign) IBOutlet NSButton *removeButton;
- @property (assign) IBOutlet NSButton *appendButton;
- @property (assign) IBOutlet NSButton *cancelButton;
- //@property (assign) IBOutlet NSButton *addFileButton;
- @property (assign) IBOutlet NSBox *clearBox;
- @property (assign) IBOutlet NSBox *nMergeBox;
- @property (assign) IBOutlet NSBox *nCancelBox;
- @property (weak) IBOutlet NSBox *addBox;
- @property (nonatomic, strong) KMDesignSelect *addFileButton;
- @property (assign) IBOutlet NSProgressIndicator *progress;
- //宽输入框
- @property (assign) IBOutlet NSTextField *pageSizeWidthTextField;
- //宽高输入连接符
- @property (assign) IBOutlet NSTextField *pageSizeWidthHeightConnectorTextField;
- //高输入框
- @property (assign) IBOutlet NSTextField *pageSizeHeightTextField;
- //宽高单位
- @property (assign) IBOutlet NSTextField *pageSizeUnitLabel;
- @property (nonatomic,retain) NSMutableArray *files;
- @property (nonatomic,retain) NSArray *addFiles;
- @property (nonatomic, retain) NSMutableIndexSet *insertIndexSet;
- @property (nonatomic,retain) NSString *password;
- @property (nonatomic,retain) PDFDocument *PDFDocument;
- @property (nonatomic,assign) KMPDFPageEditType editType;
- @property (nonatomic,assign) CGFloat allFileSize;
- @property (nonatomic,retain) NSMutableArray *rootPDFOutlineArray;
- @property (nonatomic,assign) CGSize newPageSize;
- @property (nonatomic,retain) NSMutableArray *lockFilePathArr;
- @property (nonatomic,assign) NSInteger lockFileIndex;
- @property (weak) IBOutlet NSView *originalSizeButton;
- @property (weak) IBOutlet KMImageTitleButton *A4SizeButton;
- @property (weak) IBOutlet KMImageTitleButton *A3SizeButton;
- @property (weak) IBOutlet KMImageTitleButton *USLetterSizeButton;
- @property (weak) IBOutlet KMImageTitleButton *USLegalButton;
- @property (weak) IBOutlet KMImageTitleButton *customSizeButton;
- @property (assign) IBOutlet NSBox *box;
- @property (assign) IBOutlet NSTextField *boxLabel;
- @property (assign) IBOutlet KMBlankView *myBlankView;
- @property (weak) IBOutlet NSTextField *limitLable;
- @property (weak) IBOutlet NSImageView *limitTipIv;
- @property (nonatomic, strong) PasswordWindowController *passwordWindowController;
- @property (nonatomic, strong) KMSavePanelAccessoryController *savePanelAccessoryViewController;
- @property (nonatomic, assign) BOOL canMerge;
- @property (nonatomic, strong) KMDesignButton *clearVC;
- @property (nonatomic, strong) KMDesignButton *nMergeVC;
- @property (nonatomic, strong) KMDesignButton *nCancelVC;
- @property (nonatomic, strong) NSButton *loading_backgroundView;
- @property (nonatomic, strong) NSAlert *alert;
- @property (nonatomic, assign) BOOL limit;
- @end
- @implementation KMPDFEditAppendWindow
- - (void)dealloc
- {
- _tableView.delegate = nil;
- _tableView.dataSource = nil;
- #if VERSION_DMG
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- #endif
- }
- //没有打开文档,选择he'bing
- - (id)initWithFilePaths:(NSArray *)files
- {
- if (self = [super initWithWindowNibName:@"KMPDFEditAppendWindow"]) {
-
- self.PDFDocument = [[PDFDocument alloc] init];
- self.addFiles = files;
- self.editType = KMPDFPageEditMerge;
- _lockFilePathArr = [[NSMutableArray alloc] init];
- _files = [[NSMutableArray alloc] init];
- }
- return self;
- }
- - (id)initWithPDFDocument:(PDFDocument *)document password:(NSString *)password
- {
- if (self = [super initWithWindowNibName:@"KMPDFEditAppendWindow"]) {
-
- // self.PDFDocument = document;
- self.PDFDocument = [[PDFDocument alloc] init];
- self.editType = KMPDFPageEditAppend;
- _lockFilePathArr = [[NSMutableArray alloc] init];
- _files = [[NSMutableArray alloc] init];
- KMFileAttribute *file = [[KMFileAttribute alloc] init];
- file.myPDFDocument = document;
- file.filePath = document.documentURL.path;
- file.oriFilePath = self.oriDucumentUrl.path;
- if (password && password.length > 0) {
- file.password = password;
- file.isLocked = YES;
- }
- [self.files addObject:file];
- }
- return self;
- }
- - (void)setOriDucumentUrl:(NSURL *)oriDucumentUrl {
- _oriDucumentUrl = oriDucumentUrl;
-
- KMFileAttribute *file = self.files.firstObject;
- file.oriFilePath = oriDucumentUrl.path;
- }
- - (void)km_windowWillCloseNotifation:(NSNotification *)noti {
- __weak typeof(self) weakSelf = self;
- BOOL result = [KMLightMemberManager.manager canPayFunction];
- self.limit = !result;
-
- if (result) {
- self.limitLable.hidden = YES;
- self.limitTipIv.hidden = YES;
- } else {
- [KMLightMemberManager.manager canUseAdvancedWithNeedNetworking:NO completionHandler:^(BOOL isLogin) {
- dispatch_sync(dispatch_get_main_queue(), ^{
- if (isLogin) {
- weakSelf.limitLable.hidden = NO;
- weakSelf.limitTipIv.hidden = NO;
- } else {
- weakSelf.limitLable.hidden = YES;
- weakSelf.limitTipIv.hidden = YES;
- }
- });
- }];
- }
- }
- - (void)windowDidLoad {
-
- [super windowDidLoad];
-
- #if VERSION_DMG
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(km_windowWillCloseNotifation:) name:NSWindowWillCloseNotification object:nil];
- #endif
-
- if ([self mergeCountIsReach]) {
- NSLog(@"合并次数已达到限制. count: %ld", [self getMergeCount]);
- }
-
- self.window.contentView.wantsLayer = YES;
- self.window.appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
-
- self.clearVC = [[KMDesignButton alloc] initWithType: DesignButtonTypeImage];
- self.nMergeVC = [[KMDesignButton alloc] initWithType: DesignButtonTypeText];
- self.nCancelVC = [[KMDesignButton alloc] initWithType: DesignButtonTypeText];
- // self.nMergeVC.button.keyEquivalent = @"\\r";
- self.clearBox.fillColor = NSColor.clearColor;
- self.clearBox.contentView = self.clearVC.view;
- self.nMergeBox.fillColor = NSColor.clearColor;
- self.nMergeBox.contentView = self.nMergeVC.view;
- self.nCancelBox.fillColor = NSColor.clearColor;
- self.nCancelBox.contentView = self.nCancelVC.view;
- self.box.cornerRadius = 0;
- self.box.fillColor = [NSColor colorWithRed:247/255.f green:248/255.f blue:250/255.f alpha:1.f];
-
- self.window.title = NSLocalizedString(@"Merge PDF Files",nil);
- self.pageSizeWidthTextField.enabled = NO;
- self.pageSizeHeightTextField.enabled = NO;
- self.pageSizeWidthTextField.stringValue = @"595";
- self.pageSizeHeightTextField.stringValue = @"841";
- self.pageSizeWidthTextField.formatter = [[TextFieldFormatter alloc] init];
- self.pageSizeHeightTextField.formatter = [[TextFieldFormatter alloc] init];
-
- NSArray *titles = @[NSLocalizedString(@"Original Size", nil),@"A4",@"A3",
- NSLocalizedString(@"U.S.Letter", nil),NSLocalizedString(@"U.S.Legal", nil),NSLocalizedString(@"Custom", nil)];
- __weak typeof(self) weakSelf = self;
- int i = 0;
- for (KMImageTitleButton *button in @[self.originalSizeButton, self.A4SizeButton, self.A3SizeButton,
- self.USLetterSizeButton, self.USLegalButton, self.customSizeButton]) {
- KMImageTitleButton *radio = [[KMImageTitleButton alloc] init];
- [button addSubview:radio];
- radio.frame = button.bounds;
- radio.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
- radio.titleLabel.textColor = [NSColor colorWithRed:223/255.f green:225/255.f blue:229/255.f alpha:1.f];
- radio.title = titles[i];
- if (i == 0) {
- radio.state = NSControlStateValueOn;
- radio.imageName = @"btn_radio_sel_on";
- } else {
- radio.state = NSControlStateValueOff;
- radio.imageName = @"btn_radio_unsel_on";
- }
-
- radio.titleLabel.textColor = [NSColor colorWithRed:223/255.f green:225/255.f blue:229/255.f alpha:1.f];
- radio.titleLabel.font = [NSFont fontWithName:@"SFProText-Regular" size:14];
- radio.action = ^(KMImageTitleButton *view, NSButton *button) {
- [weakSelf buttonClicked_ChooseSize:view];
- };
- radio.contentButton.tag = i;
- i += 1;
- }
-
- self.nCancelVC.target = self;
- self.nCancelVC.action = @selector(buttonItemClicked_Cancel:);
- self.nCancelVC.stringValue = NSLocalizedString(@"Cancel", nil);
- [self.nCancelVC buttonWithType:TokenButtonTypeSec size:TokenButtonSizeM height:[[NSLayoutConstraint alloc] init]];
-
- _removeButton.title = NSLocalizedString(@"Remove", nil);
- self.clearVC.target = self;
- self.clearVC.action = @selector(buttonItemClicked_Clear:);
- self.clearVC.image = [NSImage imageNamed:@"icon_btn_clear_false_norm"];
- self.clearVC.image_hov = [NSImage imageNamed:@"icon_btn_clear_false_hov"];
- self.clearVC.image_act = [NSImage imageNamed:@"icon_btn_clear_false_act"];
- self.clearVC.image_disabled = [NSImage imageNamed:@"icon_btn_clear_Disabled_norm"];
- [self.clearVC buttonWithType:TokenButtonTypeSec_Icon size:TokenButtonSizeM height:[[NSLayoutConstraint alloc] init]];
-
- [_addFileButton setTitle:NSLocalizedString(@"Add Files", nil)];
-
- self.nMergeVC.target = self;
- self.nMergeVC.action = @selector(buttonItemClicked_Append:);
- self.nMergeVC.stringValue = NSLocalizedString(@"Merge", nil);
- [self.nMergeVC buttonWithType:TokenButtonTypeCta size:TokenButtonSizeM height:[[NSLayoutConstraint alloc] init]];
- self.tableView.delegate = self;
- self.tableView.dataSource = self;
- self.tableView.allowsMultipleSelection = YES;
- [_tableView registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, NSPasteboardTypeString, NSPasteboardTypePDF, MyTableCellViewDataType, nil]];
-
- NSArray *tableHeaderTitles = @[NSLocalizedString(@"File Name", nil),NSLocalizedString(@"Page Range", nil),NSLocalizedString(@"Size", nil)];
- i = 0;
- for (NSTextField *label in @[self.fileNameLabel, self.pageRangeLabel, self.sizeLabel]) {
- label.stringValue = tableHeaderTitles[i];
- label.textColor = [NSColor colorWithRed:97/255.f green:100/255.f blue:105/255.f alpha:1.f];
- label.font = [NSFont fontWithName:@"SFProText-Regular" size:12];
- i += 1;
- }
- self.tableHeaderBottomLine.fillColor = [NSColor colorWithRed:223/255.f green:225/255.f blue:229/255.f alpha:1.f];
- [self.tableView removeTableColumn:self.tableView.tableColumns.firstObject];
- NSTableColumn *column1 = [[NSTableColumn alloc] initWithIdentifier:KMTableColumnFileNameID];
- column1.width = 446+16;
- column1.title = NSLocalizedString(@"File Name", nil);
- [self.tableView addTableColumn:column1];
- NSTableColumn *column2 = [[NSTableColumn alloc] initWithIdentifier:KMTableColumnPageRangeID];
- column2.width = 160+8;
- column2.title = NSLocalizedString(@"Page Range", nil);
- [self.tableView addTableColumn:column2];
- NSTableColumn *column3 = [[NSTableColumn alloc] initWithIdentifier:KMTableColumnSizeID];
- column3.width = 139+12;
- column3.title = NSLocalizedString(@"Size", nil);
- [self.tableView addTableColumn:column3];
- self.tableView.rowHeight = 76;
-
- _insertIndexSet = [[NSMutableIndexSet alloc] init];
-
- [self addFiles:_addFiles];
-
- [_progress setHidden:YES];
-
- [self.tableView addSubview:self.myBlankView];
- [self.myBlankView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.top.width.equalTo(self.tableView);
- make.height.equalTo(self.tableView).offset(0);
- }];
- self.myBlankView.wantsLayer = YES;
- self.myBlankView.layer.backgroundColor = [NSColor clearColor].CGColor;
- self.myBlankView.titleLabel.stringValue = NSLocalizedString(@"Select Files", nil);
- self.myBlankView.titleLabel.textColor = [NSColor colorWithRed:97/255.f green:100/255.f blue:105/255.f alpha:1];
- self.myBlankView.titleLabel.font = [NSFont fontWithName:@"SFProText-Regular" size:14];
-
- self.myBlankView.secondTitleLabel.stringValue = NSLocalizedString(@"Drop files here or Click “+”. Drag files to reorder as you need. You can also add files or folders via the lower left button.", nil);
- self.myBlankView.secondTitleLabel.textColor = [NSColor colorWithRed:148/255.f green:152/255.f blue:156/255.f alpha:1];
- self.myBlankView.secondTitleLabel.font = [NSFont fontWithName:@"SFProText-Regular" size:12];
-
- self.myBlankView.imageView.image = [NSImage imageNamed:@"KMImageNameEmptyAddNormal"];
- self.myBlankView.allowedFileTypes = [KMImageAccessoryController supportedImageTypes];
- self.myBlankView.mouseActionCallBack = ^(KMBlankViewMouseEventType mouseType) {
- if (mouseType == KMBlankViewMouseEventType_MouseEnter) {
- weakSelf.myBlankView.imageView.image = [NSImage imageNamed:@"KMImageNameEmptyAddHover"];
- } else if (mouseType == KMBlankViewMouseEventType_MouseExit) {
- weakSelf.myBlankView.imageView.image = [NSImage imageNamed:@"KMImageNameEmptyAddNormal"];
- } else if (mouseType == KMBlankViewMouseEventType_MouseDown) {
- weakSelf.myBlankView.imageView.image = [NSImage imageNamed:@"KMImageNameEmptyAddHover"];
- [weakSelf itemAddFileAction];
- } else if (mouseType == KMBlankViewMouseEventType_MouseUp) {
- weakSelf.myBlankView.imageView.image = [NSImage imageNamed:@"KMImageNameEmptyAddHover"];
- }
- };
- self.myBlankView.dragSuccessBlock = ^(NSMutableArray *fileNames) {
- NSMutableArray *array = [NSMutableArray array];
-
- for(NSString *path in fileNames){
- if (![weakSelf isExistAtFilepath:path]) {
- continue;
- }
- NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
- CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
-
- weakSelf.allFileSize = weakSelf.allFileSize + fileSize;
- // weakSelf.allFileSize = weakSelf.allFileSize - fileSize;
- [array addObject:path];
- }
- [weakSelf addFiles:array];
- };
-
- [self updateButtonState];
-
- id outputBookMarkObject = [[NSUserDefaults standardUserDefaults] objectForKey:@"kmAppendOutputPath"];
- if ([outputBookMarkObject isKindOfClass:[NSData class]]) {
- NSData* outputBookMarkData = (NSData*)outputBookMarkObject;
- NSURL *bookmarkedURL = [NSURL URLByResolvingBookmarkData:outputBookMarkData options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:nil error:nil];
- [bookmarkedURL startAccessingSecurityScopedResource];
- if (bookmarkedURL) {
- // _outputText.stringValue = [bookmarkedURL path];
- }
- }
-
- self.boxLabel.stringValue = NSLocalizedString(@"Page size:", nil);
- self.boxLabel.textColor = [NSColor colorWithRed:37/255.f green:38/255.f blue:41/255.f alpha:1.f];
- self.boxLabel.font = [NSFont fontWithName:@"SFProText-Regular" size:16];
-
- self.addFileButton = [[KMDesignSelect alloc] initWithType:1];
- self.addBox.fillColor = [NSColor clearColor];
- self.addBox.contentView = self.addFileButton.view;
-
- [self.addFileButton removeAllItems];
- [self.addFileButton addItemsWithObjectValues:@[NSLocalizedString(@"Add Files", ""),NSLocalizedString(@"Add Folder", ""),NSLocalizedString(@"Add Open Files", "")]];
- [self.addFileButton selectItemAt:0];
- self.addFileButton.editable = false; //默认属性,初始化可不设置
- self.addFileButton.delete = self;
- [self.addFileButton updateUI];
-
- self.loading_backgroundView = [[NSButton alloc] init];
- [self.window.contentView addSubview:self.loading_backgroundView];
- self.loading_backgroundView.frame = self.window.contentView.bounds;
- self.loading_backgroundView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
- self.loading_backgroundView.title = @"";
- self.loading_backgroundView.bordered = NO;
- self.loading_backgroundView.wantsLayer = YES;
- self.loading_backgroundView.layer.backgroundColor = [NSColor clearColor].CGColor;
- self.loading_backgroundView.hidden = YES;
-
- self.limitLable.stringValue = NSLocalizedString(@"Merge 2 files at a time", "");
- self.limitLable.font = [NSFont SFProTextRegularFont:12];
- self.limitLable.textColor = [NSColor km_initWithHex:@"#F3465B" alpha:1];
- self.limit = NO;
-
- BOOL result = [KMLightMemberManager.manager canPayFunction];
- self.limit = !result;
-
- if (result) {
- self.limitLable.hidden = YES;
- self.limitTipIv.hidden = YES;
- } else {
- #if VERSION_DMG
- [KMLightMemberManager.manager canUseAdvancedWithNeedNetworking:NO completionHandler:^(BOOL isLogin) {
- dispatch_sync(dispatch_get_main_queue(), ^{
- if (isLogin) {
- weakSelf.limitLable.hidden = NO;
- weakSelf.limitTipIv.hidden = NO;
- } else {
- weakSelf.limitLable.hidden = YES;
- weakSelf.limitTipIv.hidden = YES;
- }
- });
- }];
-
- #else
- self.limitLable.hidden = NO;
- self.limitTipIv.hidden = NO;
- #endif
- }
- }
- - (void)updateButtonState
- {
- if (_files.count > 0) {
- self.myBlankView.hidden = YES;
- self.tableHeaderBox.hidden = NO;
- [_removeButton setEnabled:YES];
- [self.clearVC setEnabled:YES];
-
- if (self.files.count > 1) {
- [self.nMergeVC setEnabled:YES];
- self.canMerge = YES;
- } else {
- self.canMerge = NO;
- [self.nMergeVC setEnabled:NO];
- }
- } else {
- self.myBlankView.hidden = NO;
- self.tableHeaderBox.hidden = YES;
- [_removeButton setEnabled:NO];
- [self.clearVC setEnabled:NO];
- self.canMerge = NO;
- [self.nMergeVC setEnabled:NO];
- }
- }
- #pragma mark -NSTextFieldDelegate
- - (void)controlTextDidChange:(NSNotification *)obj
- {
- NSTextField *textField = (NSTextField*)[obj object];
- NSInteger index = textField.tag;
- [[_files objectAtIndex:index] setPagesString:textField.stringValue];
- }
- #pragma mark Show Methods
- - (void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
- if (contextInfo != NULL) {
- void (^handler)(BOOL isSuccessfully ,NSIndexSet * selectedIndexSet) = CFBridgingRelease(contextInfo);
- handler(_isSuccessfully,self.insertIndexSet);
- }
- windowController = nil;
- }
- - (void)beginSheetModalForWindow:(NSWindow *)window completionHandler:(void (^)(BOOL isSuccessfully,NSIndexSet * selectedIndexSet))handler
- {
- if (windowController.window.isSheet) {
- #if DEBUG
- NSLog(@"合并窗口已显示");
- #endif
- return;
- }
- windowController = self;
-
- [NSApp beginSheet:[self window]
- modalForWindow:window
- modalDelegate:self
- didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
- contextInfo:handler ? CFBridgingRetain(handler) : NULL];
- }
- #pragma mark Private methor
- - (void)viewFileAtFinder:(NSString *)filePath
- {
- NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
- NSURL *url = [NSURL fileURLWithPath:filePath];
- [workspace activateFileViewerSelectingURLs:[NSArray arrayWithObject:url]];
- }
- - (void)addFiles:(NSArray*)files
- {
- [_lockFilePathArr removeAllObjects];
- _lockFileIndex = 0;
- for (NSString *filePath in files) {
- if ([filePath.pathExtension.lowercaseString isEqualToString:@"pdf"]) {
- KMFileAttribute *file = [[KMFileAttribute alloc] init];
- file.filePath = filePath;
- PDFDocument *document = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:filePath]];
-
- NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
- self.allFileSize = [[attrib objectForKey:NSFileSize] floatValue];
-
- if ([document isLocked]) {
- [_lockFilePathArr addObject:filePath];
- } else if ([KMTools hasPermissionsLimit:document]) {
- [_lockFilePathArr addObject:filePath];
- } else {
- if ([NSDocument isDamageWithUrl:[NSURL fileURLWithPath:filePath]]) {
- NSAlert *alert = [[NSAlert alloc] init];
- alert.messageText = NSLocalizedString(@"The file was not added successfully, please select the file again.", @"");
- [alert runModal];
- } else {
- [_files addObject:file];
- }
- }
- } else {
- KMFileAttribute *file = [[KMFileAttribute alloc] init];
- file.filePath = filePath;
- PDFDocument *document = [[PDFDocument alloc] init];
- NSImage *image = [[NSImage alloc] initWithContentsOfFile:filePath];
- if (image == nil) {
- continue;
- }
- // [document insertPage:image.size withImage:filePath atIndex:0];
- [document insertPage:[[PDFPage alloc] initWithImage:image] atIndex:0];
- file.myPDFDocument = document;
-
- NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
- self.allFileSize = [[attrib objectForKey:NSFileSize] floatValue];
-
- if ([NSDocument isDamageWithUrl:[NSURL fileURLWithPath:filePath]]) {
- NSAlert *alert = [[NSAlert alloc] init];
- alert.messageText = NSLocalizedString(@"The file was not added successfully, please select the file again.", @"");
- [alert runModal];
- } else {
- [_files addObject:file];
- }
- }
- }
-
- [self updateButtonState];
- [_tableView reloadData];
-
- if ([_lockFilePathArr count]) {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- if (self.files.count == 0) {
- self.insertRow = 0;
- } else {
- self.insertRow = self.files.count;
- }
- [self openPasswordWindow];
- });
- }
- }
- - (BOOL)isExistAtFilepath:(NSString *)filePath
- {
- NSMutableArray *array = [NSMutableArray array];
- for (KMFileAttribute *file in self.files) {
- [array addObject:file.filePath];
- }
- if ([array containsObject:filePath]) {
- return NO;
- }
- return YES;
- }
- -(void)openPasswordWindow
- {
- if ([_lockFilePathArr count] > _lockFileIndex) {
- NSString *filePath = [_lockFilePathArr objectAtIndex:_lockFileIndex];
- __weak typeof(self) weakSelf = self;
- [KMPasswordInputWindow openWindowWithWindow:self.window type:KMPasswordInputWindowTypeOwner url:[NSURL fileURLWithPath:filePath] callback:^(enum KMPasswordInputWindowResult result, NSString * _Nullable password) {
- if (password.length > 0) {
- KMFileAttribute *file = [[KMFileAttribute alloc] init];
- file.filePath = filePath;
- file.isLocked = YES;
- file.password = password;
- [weakSelf.files insertObject:file atIndex:weakSelf.insertRow];
- weakSelf.insertRow ++;
- }
-
- weakSelf.lockFileIndex++;
- // if ([self.lockFilePathArr count] > self.lockFileIndex) {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- [weakSelf openPasswordWindow];
- });
- // } else {
- [weakSelf.tableView reloadData];
- [weakSelf updateButtonState];
- // }
- }];
- }
- }
- - (void)comboBoxAction:(NSInteger)itemIndex atRow:(NSInteger)row
- {
- NSInteger index = row;
- // 数组不存在,或者索引超出数组长度
- if ((!_files) || (index > ((NSInteger)[_files count] - 1))) {
- return;
- }
-
- if (itemIndex == 0) {
- KMFileAttribute *currentFile = [_files objectAtIndex:index];
- [currentFile setBAllPage:YES];
- currentFile.pagesType = KMPDFSeleectPageType_AllPages;
-
- [self.window makeFirstResponder:self];
- } else if (itemIndex == 1) {
- KMFileAttribute *currentFile = [_files objectAtIndex:index];
- currentFile.pagesType = KMPDFSeleectPageType_OnlyOdd;
- [currentFile setBAllPage:NO];
- PDFDocument *tDocument = currentFile.myPDFDocument;
- if (tDocument == nil) {
- tDocument = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:currentFile.filePath]];
- }
- if ([currentFile isLocked]) {
- [tDocument unlockWithPassword:currentFile.password];
- }
-
- NSString *tPagesString = @"";
- for (NSInteger i = 0; i < tDocument.pageCount; i++) {
- if (i%2 == 0) {
- if (tPagesString.length == 0) {
- tPagesString = [tPagesString stringByAppendingString:[NSString stringWithFormat:@"%ld",i +1]];
- }else{
- tPagesString = [tPagesString stringByAppendingString:[NSString stringWithFormat:@",%ld",i +1]];
- }
-
- }
- }
- currentFile.pagesString = tPagesString;
-
- [self.window makeFirstResponder:self];
- } else if (itemIndex == 2) {
- KMFileAttribute *currentFile = [_files objectAtIndex:index];
- currentFile.pagesType = KMPDFSeleectPageType_OnlyEven;
- [currentFile setBAllPage:NO];
- NSString *tPagesString = @"";
- PDFDocument *tDocument = currentFile.myPDFDocument;
- if (tDocument == nil) {
- tDocument = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:currentFile.filePath]];
- }
- if ([currentFile isLocked]) {
- [tDocument unlockWithPassword:currentFile.password];
- }
-
- for (NSInteger i = 0; i <tDocument.pageCount; i++) {
- if (i%2) {
- if (tPagesString.length == 0) {
- tPagesString = [tPagesString stringByAppendingString:[NSString stringWithFormat:@"%ld",i +1]];
- }else{
- tPagesString = [tPagesString stringByAppendingString:[NSString stringWithFormat:@",%ld",i +1]];
- }
- }
- }
- currentFile.pagesString = tPagesString;
-
- [self.window makeFirstResponder:self];
- } else {
- KMFileAttribute *currentFile = [_files objectAtIndex:index];
- currentFile.pagesType = KMPDFSeleectPageType_PagesString;
- currentFile.pagesString = @"";
- [currentFile setBAllPage:NO];
- }
- }
- - (void)handerReDraw
- {
- //如果选择的是原始尺寸,不处理
- // KMImageTitleButton *originalSizeButton = self.originalSizeButton.subviews.lastObject;
- // if (originalSizeButton.state == NSControlStateValueOn) {
-
- // }else{
- //重新选择了界面尺寸,那么就要重绘
- // if (self.newPageSize.width < 0) {
- // NSBeep();
- // return;
- // }else{
- NSMutableArray *pagesArray = [NSMutableArray array];
-
- NSInteger pageCount = self.PDFDocument.pageCount;
- for (NSInteger i = 0; i < pageCount; i ++) {
- [pagesArray addObject:[self.PDFDocument pageAtIndex:0]];
- [self.PDFDocument removePageAtIndex:0];
- }
-
- for (NSInteger i = 0; i < pageCount; i ++) {
- PDFPage *oPage = pagesArray[i];
- NSRect pageBounds = [oPage boundsForBox:kPDFDisplayBoxCropBox];
- KMAppendPDFPage *page = [[KMAppendPDFPage alloc] init];
- [page setBounds:NSMakeRect(0, 0, pageBounds.size.width, pageBounds.size.height) forBox:kPDFDisplayBoxMediaBox];
- page.drawingPage = oPage;
- // [self.PDFDocument insertPageObject:page atIndex:i];
-
- PDFAnnotationLink *linkA = [[PDFAnnotationLink alloc] init];
- linkA.bounds = NSMakeRect(0, pageBounds.size.height-32, 120, 32);
- linkA.URL = [NSURL URLWithString:@"https://www.pdfreaderpro.com/store"];
- [page addAnnotation:linkA];
-
- [self.PDFDocument insertPage:page atIndex:i];
- }
- //如果是自定义大小,删除所有的outline,因为合并出来的outline是无效的
- if (self.PDFDocument.outlineRoot) {
- NSInteger childCount = [self.PDFDocument.outlineRoot numberOfChildren];
- NSMutableArray *PDFOutlineArray = [NSMutableArray array];
- for (NSInteger i = 0 ; i < childCount; i ++) {
- [PDFOutlineArray addObject:[self.PDFDocument.outlineRoot childAtIndex:i]];
- }
-
- for (PDFOutline *deleteOutline in PDFOutlineArray) {
- [deleteOutline removeFromParent];
- }
- }
- // }
- // }
- }
- - (BOOL)hasOpenFiles {
- NSArray *array = [KMTools getOpenDocumentURLs];
- if (array.count == 0) {
- return NO;
- }
-
- for (NSURL *fileURL in array) {
- if ([self isExistAtFilepath:fileURL.path]) {
- return YES;
- }
- }
- return NO;
- }
- #pragma mark - KMSelectPopButtonDelegate
- - (void)km_comboBoxSelectionDidChange:(KMDesignSelect *)obj {
- if ([self.addFileButton isEqual:obj]) {
- NSInteger index = self.addFileButton.indexOfSelectedItem;
- if (index < 0) {
- index = 0;
- }
-
- if (index == 0) {
- [self itemAddFileAction];
- } else if (index == 1) {
- [self itemAddFolderAction];
- } else if (index == 2) {
- [self itemAddOpenFileAction];
- }
- }
- }
- - (void)km_controlTextDidChange:(KMDesignSelect *)obj {
-
- }
- - (void)km_controlTextDidEndEditing:(KMDesignSelect *)obj {
-
- }
- - (void)km_SelectPopoverWillShow:(KMDesignSelect *)obj {
- if ([self hasOpenFiles]) {
- obj.disItems = @[];
- } else {
- obj.disItems = @[NSLocalizedString(@"Add Open Files", "")];
- }
- }
- #pragma mark tabelView
- - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
- {
- return _files.count;
- }
- - (nullable id)tableView:(NSTableView *)tableView viewForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row
- {
- KMFileAttribute *fileAttribute = [_files objectAtIndex:row];
-
- PDFDocument *pdf = fileAttribute.myPDFDocument;
- if (pdf == nil) {
- pdf = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:fileAttribute.filePath]];
- }
- if ([pdf isLocked]) {
- [pdf unlockWithPassword:fileAttribute.password];
- }
-
- if (tableColumn.identifier == KMTableColumnPageRangeID) {
- KMPDFMergePageRangeTabelViewCell *cell = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
- if (cell == nil) {
- cell = [[KMPDFMergePageRangeTabelViewCell alloc] init];
- }
- cell.wantsLayer = YES;
- cell.layer.backgroundColor = [NSColor clearColor].CGColor;
- cell.pageCount = pdf.pageCount;
- [cell updatePageRange:fileAttribute.pagesType pageStrings:fileAttribute.pagesString isFirstResponser:NO];
- // if (fileAttribute.pageRangeError) {
- // [cell updateRageRangeState:10];
- // }
-
- __weak typeof(self) weakSelf = self;
- cell.callback = ^(NSInteger index) {
- [weakSelf comboBoxAction:index atRow:row];
- };
- cell.textDidChange = ^(NSString * _Nonnull string) {
- if (row >= weakSelf.files.count) {
- return;
- }
-
- KMFileAttribute *file = [weakSelf.files objectAtIndex:row];
- [file setPagesString:string];
- // file.pageRangeError = NO;
- // dispatch_async(dispatch_get_main_queue(), ^{
- // [weakSelf.tableView reloadData];
- // });
-
- };
- cell.textDidEndEdit = ^(NSString * _Nonnull string, KMPDFMergePageRangeTabelViewCell *pageRangeCell) {
- if (row >= weakSelf.files.count || [string isEqual: @""]) {
- return;
- }
- KMFileAttribute *file = [weakSelf.files objectAtIndex:row];
- if (!file.selectPages) {
- // file.pageRangeError = YES;
- if (weakSelf.alert) {
- return;
- }
-
- dispatch_async(dispatch_get_main_queue(), ^{
- // [weakSelf.tableView reloadData];
- NSAlert *alert = [[NSAlert alloc] init];
- // weakSelf.alert = alert;
- [alert setAlertStyle:NSAlertStyleCritical];
- [alert setMessageText:[NSString stringWithFormat:@"%@ %@",[file.filePath lastPathComponent],NSLocalizedString(@"Invalid page range or the page number is out of range. Please try again.", nil)]];
- [alert runModal];
-
- file.pagesString = @"";
- [pageRangeCell updatePageRange:file.pagesType pageStrings:file.pagesString isFirstResponser:NO];
-
- // dispatch_async(dispatch_get_main_queue(), ^{
- // weakSelf.alert = nil;
- // });
- // [weakSelf.tableView reloadData];
- // [weakSelf.tableView reloadDataForRowIndexes:[NSIndexSet indexSetWithIndex:row] columnIndexes:[NSIndexSet indexSetWithIndex:1]];
- });
- } else {
- [pageRangeCell updatePageRange:file.pagesType pageStrings:file.pagesString isFirstResponser:YES];
- }
- };
-
- return cell;
- }
- if (tableColumn.identifier == KMTableColumnSizeID) {
- KMPDFMergeSizeTabelViewCell *cell = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
- if (cell == nil) {
- cell = [[KMPDFMergeSizeTabelViewCell alloc] init];
- }
-
- NSString *filePath = pdf.documentURL.path;
- if (!filePath) {
- filePath = fileAttribute.filePath;
- }
- NSDictionary *attributesDic = [[NSFileManager defaultManager]attributesOfItemAtPath:filePath error:nil];
- float size = [[attributesDic objectForKey:NSFileSize] floatValue]/1024;
- cell.label.stringValue = [NSString stringWithFormat:@"%0.1f %c", truesize(size) , moniker(size)];
- cell.button.hidden = YES;
-
- __weak typeof(self) weakSelf = self;
- cell.callback = ^{
- NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:fileAttribute.filePath error:nil];
- CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
- weakSelf.allFileSize = weakSelf.allFileSize - fileSize;
-
- [weakSelf.files removeObject:fileAttribute];
- [weakSelf.tableView reloadData];
- [weakSelf updateButtonState];
- };
-
- return cell;
- }
-
- KMPDFMergeFileNameTabelViewCell *cell = [tableView makeViewWithIdentifier:@"fileName" owner:self];
- if (cell == nil) {
- cell = [[KMPDFMergeFileNameTabelViewCell alloc] init];
- }
-
- NSString *fileName = [fileAttribute.filePath lastPathComponent];
- if (fileAttribute.oriFilePath.length > 0) {
- fileName = fileAttribute.oriFilePath.stringByDeletingPathExtension.lastPathComponent;
- }
-
- cell.numberLabel.stringValue = [NSString stringWithFormat:@"%ld",(row + 1)];;
- cell.fileNameLabel.stringValue = fileName;
- cell.pageNumberLabel.stringValue = [NSString stringWithFormat:@"%ld %@",pdf.pageCount,NSLocalizedString(@"Pages", nil)];;
-
- // cell.iconImageView.image = [[pdf pageAtIndex:0] thumbnailWithSize:60*2 forBox:kPDFDisplayBoxCropBox];
- if (pdf.documentURL) {
- if (fileAttribute.password.length > 0) {
- cell.iconImageView.image = [[pdf pageAtIndex:0] thumbnailOfSize:NSMakeSize(120, 120) forBox:kPDFDisplayBoxCropBox];
- } else {
- cell.iconImageView.image = [NSImage previewForFileWithPath:pdf.documentURL ofSize:NSMakeSize(120, 120) asIcon:YES];
- }
- } else if (fileAttribute.filePath) {
- // if ([fileAttribute.filePath.pathExtension.lowercaseString isEqualToString:@"pdf"]) {
- cell.iconImageView.image = [NSImage previewForFileWithPath:[NSURL fileURLWithPath:fileAttribute.filePath] ofSize:NSMakeSize(120, 120) asIcon:YES];
- // } else {
- // cell.iconImageView.image = [NSImage previewForFileWithPath:fileAttribute.filePath ofSize:NSMakeSize(120, 120) asIcon:YES];
- // }
- } else {
- cell.iconImageView.image = nil;
- }
-
- return cell;
- }
- - (BOOL)tableView:(NSTableView *)tableView shouldSelectTableColumn:(NSTableColumn *)tableColumn
- {
- return NO;
- }
- #pragma mark -NSTableViewDelegate
- - (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard
- {
- // Copy the row numbers to the pasteboard.
- NSData *zNSIndexSetData = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
- [pboard declareTypes:[NSArray arrayWithObject:MyTableCellViewDataType] owner:self];
- [pboard setData:zNSIndexSetData forType:MyTableCellViewDataType];
- return YES;
- }
- - (NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)dropOperation
- {
- if (dropOperation == NSTableViewDropOn) {
-
- return NSDragOperationNone;
- }
-
- BOOL isCanDrag = NO;
- NSDragOperation result = NSDragOperationNone;
- NSPasteboard *pboard = [info draggingPasteboard];
- if ([pboard availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]])
- {
- NSArray *fileNames = [pboard propertyListForType:NSFilenamesPboardType];
- for (NSString* path in fileNames) {
- if ([[KMImageAccessoryController supportedImageTypes] containsObject:path.pathExtension.lowercaseString]) {
- isCanDrag = YES;
- }else{
- isCanDrag = NO;
- break;
- }
- }
- }else if ([pboard availableTypeFromArray:[NSArray arrayWithObject:MyTableCellViewDataType]]){
-
- result = NSDragOperationEvery;
- }
-
- if (isCanDrag) {
-
- result = NSDragOperationCopy;
- }
- return result;
- }
- - (BOOL)tableView:(NSTableView *)tableView acceptDrop:(id <NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)dropOperation
- {
- BOOL result = NO;
- NSPasteboard *pboard = [info draggingPasteboard];
- _insertRow = row;
- if ([pboard availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]])
- {
- NSMutableArray *fileNames = [pboard propertyListForType:NSFilenamesPboardType];
-
- NSMutableArray *array = [NSMutableArray array];
-
- for(NSString *path in fileNames){
- if (![self isExistAtFilepath:path]) {
- continue;
- }
- NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
- CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
-
- self.allFileSize = self.allFileSize + fileSize;
- [array addObject:path];
- }
- [self addFiles:array];
- result = YES;
- } else if ([pboard availableTypeFromArray:[NSArray arrayWithObject:MyTableCellViewDataType]]){
- NSData* rowData = [pboard dataForType:MyTableCellViewDataType];
- NSIndexSet* rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];
- NSMutableArray *moveArray = [[NSMutableArray alloc] init];
- NSMutableArray *allPhoto = [_files mutableCopy];
- NSInteger index = 0;
-
- [rowIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
- KMFileAttribute * file = allPhoto[idx];
- [moveArray addObject:file];
- [_files removeObject:file];
- }];
- if (row > 0) {
- KMFileAttribute * indexFile = allPhoto[row -1];
- while([moveArray containsObject:indexFile]){
- row --;
- if ((row < 0)) {
- indexFile = nil;
- break;
- } else {
- indexFile = allPhoto[row];
- }
- }
- if(indexFile){
- index = [_files indexOfObject:indexFile]+1;
- }
- }
- for (NSInteger i = 0; i< moveArray.count; i++) {
- [_files insertObject:moveArray[i] atIndex:(index +i)];
- }
-
- [_tableView reloadData];
- result = YES;
- // [moveArray release];
- // [allPhoto release];
- } else {
- result = NO;
- }
- return result;
- }
- - (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row {
- KMBatchTableRowView *rowView = [[KMBatchTableRowView alloc] init];
- rowView.backgroundView.layer.backgroundColor = [NSColor colorWithRed:237/255.f green:238/255.f blue:240/255.f alpha:1.f].CGColor;
- rowView.backgroundView.layer.cornerRadius = 4.f;
- rowView.selectionInset = NSEdgeInsetsMake(0, 16, 0, 16);
- rowView.selectionRadius = 4.f;
- rowView.selectionBackgroundColorBlock = ^NSColor * _Nonnull{
- return [NSColor colorWithRed:206/255.f green:208/255.f blue:212/255.f alpha:0.6f];
- };
-
- return rowView;
- }
- - (void)tableViewSelectionDidChange:(NSNotification *)notification {
-
- }
- #pragma mark Buutton Action
- - (void)_clearData {
- for (KMFileAttribute *file in self.files) {
- if (file.oriFilePath.length > 0 && [[NSFileManager defaultManager] fileExistsAtPath:file.filePath]) {
- [[NSFileManager defaultManager] removeItemAtURL:[NSURL fileURLWithPath:file.filePath] error:nil];
- }
- }
- }
- - (IBAction)buttonItemClicked_Cancel:(id)sender
- {
- [self _clearData];
-
- _isSuccessfully = NO;
- [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
- [[self window] orderOut:self];
- }
- - (IBAction)buttonItemClicked_Append:(id)sender
- {
- if (!self.canMerge) {
- return;
- }
- #if VERSION_DMG
- [KMLightMemberManager.manager canUseAdvancedWithNeedNetworking:NO completionHandler:^(BOOL result) {
- dispatch_async(dispatch_get_main_queue(), ^{
- if (result) {
- [self doAppend];
- } else {
- [KMComparativeTableViewController showWithWindow:self.window :KMComparativeInputTypeMerge inputType:KMSubscribeWaterMarkTypeMerge];
- }
- });
- }];
- #else
- [self doAppend];
- #endif
- }
- - (IBAction)doAppend {
- if (KMLightMemberManager.manager.purchaseState != KMPurchaseManagerStateSubscription && _files.count > 2) {
- // [KMSubscribeWaterMarkWindowController showWithWindow: self.window isContinue: false limit type:KMSubscribeWaterMarkTypeMerge subscribeDidClick:^{
- //
- // } completion:^(BOOL isSub, BOOL isWater, BOOL isClose) {
- //
- // }];
- [KMSubscribeWaterMarkWindowController showWithWindow:self.window isContinue:false limit:[self mergeCountIsReach] type:KMSubscribeWaterMarkTypeMerge subscribeDidClick:^{
-
- } completion:^(BOOL, BOOL, BOOL) {
-
- }];
- return;
- } else if (KMLightMemberManager.manager.purchaseState != KMPurchaseManagerStateSubscription && _files.count <= 2) {
- __weak typeof(self) weakSelf = self;
- // [KMSubscribeWaterMarkWindowController showWithWindow: self.window isContinue:true type:KMSubscribeWaterMarkTypeMerge subscribeDidClick:^{
- //
- // } completion:^(BOOL isSub, BOOL isWater, BOOL isClose) {
- // if (isWater) {
- // dispatch_async(dispatch_get_main_queue(), ^{
- // [weakSelf buttonItemClick_Append: sender];
- // });
- // }
- // }];
- BOOL isContinue = YES;
- BOOL limit = [self mergeCountIsReach];
- if (limit) {
- isContinue = NO;
- }
-
- [KMSubscribeWaterMarkWindowController showWithWindow:self.window isContinue:isContinue limit:limit type:KMSubscribeWaterMarkTypeMerge subscribeDidClick:^{
-
- } completion:^(BOOL isSub, BOOL isWater, BOOL isClose) {
- if (isClose) {
- return;
- }
- if (isSub) {
- dispatch_async(dispatch_get_main_queue(), ^{
- [weakSelf buttonItemClick_Append: nil];
- });
- return;
- }
- if (isWater && !limit) {
- dispatch_async(dispatch_get_main_queue(), ^{
- [weakSelf buttonItemClick_Append: nil];
- });
- return;
- }
- }];
- return;
- }
-
- [self buttonItemClick_Append: nil];
- }
- - (void)buttonItemClick_Append:(id)sender {
- KMImageTitleButton *customSizeButton = self.customSizeButton.subviews.lastObject;
- if (customSizeButton.state == NSControlStateValueOn) {
- NSInteger widthFloat = [self.pageSizeWidthTextField.stringValue integerValue];
- NSInteger heightFloat = [self.pageSizeHeightTextField.stringValue integerValue];
-
- NSString *widthString = [NSString stringWithFormat:@"%ld",widthFloat];
- NSString *heightString = [NSString stringWithFormat:@"%ld",heightFloat];
-
- if (![widthString isEqualToString:self.pageSizeWidthTextField.stringValue] ||![heightString isEqualToString:self.pageSizeHeightTextField.stringValue]||widthFloat <= 0 ||heightFloat <= 0 ) {
- NSBeep();
- return;
- }else{
- self.newPageSize = CGSizeMake(widthFloat, heightFloat);
- }
- }
-
- //初始化数组
- if (!self.rootPDFOutlineArray) {
- self.rootPDFOutlineArray = [NSMutableArray new];
- }else{
- [self.rootPDFOutlineArray removeAllObjects];
- }
- //如果是合并,文件路径不存在,或者文件个数小于等于1 return
- if (_editType == KMPDFPageEditMerge) {
- if (_files.count <= 1 ) {
- NSAlert *alert = [[NSAlert alloc] init];
- [alert setAlertStyle:NSAlertStyleCritical];
- [alert setMessageText:NSLocalizedString(@"To start merging, please select at least 2 files.", nil)];
- [alert runModal];
- return;
- }
- }
-
- _isSuccessfully = NO;
- [self.nCancelVC setEnabled:NO];
- self.canMerge = NO;
-
- BOOL allPage = YES;//只有是全部才支持大纲的合并
- for (KMFileAttribute *file in _files) {
- if (!file.selectPages) {
- dispatch_async(dispatch_get_main_queue(), ^{
- NSAlert *alert = [[NSAlert alloc] init];
- [alert setAlertStyle:NSAlertStyleCritical];
- [alert setMessageText:[NSString stringWithFormat:@"%@ %@",[file.filePath lastPathComponent],NSLocalizedString(@"Invalid page range or the page number is out of range. Please try again.", nil)]];
- [alert runModal];
-
- [self.nCancelVC setEnabled:YES];
- self.canMerge = YES;
- });
- return;
- }
- }
-
- [_progress setHidden:NO];
- [_progress startAnimation:nil];
- self.loading_backgroundView.hidden = NO;
-
- for(KMFileAttribute *file in _files){
- //只要有一个文件不是全部page,那么就不合并大纲
- if (allPage == YES) {
- allPage = file.bAllPage;
- }
-
- //通过路径获取文件
- PDFDocument *tdocument = file.myPDFDocument;
- if (tdocument == nil) {
- tdocument = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:[file filePath]]];
- file.myPDFDocument = tdocument;
- }
-
- //所有的outline数组
- NSMutableArray *PDFOutlineArr = [NSMutableArray new];
- //需要解锁
- if (file.isLocked) {
- [tdocument unlockWithPassword:file.password];
- }
- //如果根存在,那么遍历出所有的outline,添加到数组中,如果不存在,那么就创建一个根
- if (tdocument.outlineRoot) {
- [self.rootPDFOutlineArray addObject:tdocument.outlineRoot];
- [self fetchAllOfChildren:tdocument.outlineRoot containerArray:PDFOutlineArr];
- [PDFOutlineArr removeObject:tdocument.outlineRoot];
- } else {
- PDFOutline *rootOutline = [[PDFOutline alloc] init];
- tdocument.outlineRoot = rootOutline;
- if (tdocument.outlineRoot) {
- [self.rootPDFOutlineArray addObject:tdocument.outlineRoot];
- }
- }
-
- // for (CPDFOutline *tOutline in PDFOutlineArr) {
- //这段代码主要是调用他的getter方法;outline显示正确
- // [tOutline.destination.page dataRepresentation];
- // }
-
- for (NSNumber *number in file.selectPages) {
- PDFPage *page = [tdocument pageAtIndex:[number integerValue]-1];
- [self.PDFDocument insertPage:page atIndex:[self.PDFDocument pageCount]];
- // [self.PDFDocument insertPageObject:page atIndex:self.PDFDocument.pageCount];
- [self.insertIndexSet addIndex:(self.PDFDocument.pageCount - 1)];
- }
-
- _isSuccessfully = YES;
- }
-
- KMFileAttribute *file = self.files.firstObject;
- NSString *fileName = file.filePath.stringByDeletingPathExtension.lastPathComponent;
- if (file.oriFilePath.length > 0) {
- fileName = file.oriFilePath.stringByDeletingPathExtension.lastPathComponent;
- }
- fileName = [NSString stringWithFormat:@"%@_Merged", fileName];
- if (_editType == KMPDFPageEditMerge) {
- _isSuccessfully = NO;
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- PDFOutline *rootline = [[PDFOutline alloc] init];
- self.PDFDocument.outlineRoot = rootline;
- if (allPage) {
- NSInteger insertIndex = 0;
- for (NSUInteger i = 0; i < self.rootPDFOutlineArray.count; i++) {
- PDFOutline *rootPDFOutline = self->_rootPDFOutlineArray[i];
- for (NSUInteger j = 0; j < rootPDFOutline.numberOfChildren; j ++) {
- [self.PDFDocument.outlineRoot insertChild:[rootPDFOutline childAtIndex:j] atIndex: insertIndex];
- insertIndex ++;
- }
- }
- }
-
- dispatch_async(dispatch_get_main_queue(), ^{
- if (self.limit) {
- [self handerReDraw];
- }
-
- KMSavePanelAccessoryController *savePanelAccessoryViewController = [[KMSavePanelAccessoryController alloc] init];
- NSSavePanel *savePanel = [NSSavePanel savePanel];
- savePanel.nameFieldStringValue = fileName;
- savePanel.allowedFileTypes = @[@"pdf"];
- savePanel.accessoryView = savePanelAccessoryViewController.view;
- self.savePanelAccessoryViewController = savePanelAccessoryViewController;
- [savePanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
- if (result) {
- NSString * outputSavePanel = savePanel.URL.path;
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- self->_isSuccessfully = [self.PDFDocument writeToFile:outputSavePanel];
-
- if (!self->_isSuccessfully) {
- self->_isSuccessfully = [self.PDFDocument.dataRepresentation writeToFile:outputSavePanel atomically:YES];
- }
- dispatch_async(dispatch_get_main_queue(), ^{
- [self->_progress setHidden:YES];
- [self.nCancelVC setEnabled:YES];
- self.canMerge = YES;
- self.loading_backgroundView.hidden = YES;
-
- if (self->_isSuccessfully) {
- [self _clearData];
- [self recordMergeCount];
-
- [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
- [[self window] orderOut:self];
- if (self.saveAsPDFFilePath) {
- BOOL autoOpen = self.savePanelAccessoryViewController.needOpen;
- if (autoOpen) {
- self.saveAsPDFFilePath(outputSavePanel);
- self.saveAsPDFFilePath = nil;
- } else {
- [self viewFileAtFinder:outputSavePanel];
-
- }
- } else {
- BOOL autoOpen = self.savePanelAccessoryViewController.needOpen;
- if (autoOpen) {
- [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:[NSURL fileURLWithPath:outputSavePanel] display:YES completionHandler:^(NSDocument * _Nullable document, BOOL documentWasAlreadyOpen, NSError * _Nullable error) {
-
- }];
- } else {
- [self viewFileAtFinder:outputSavePanel];
- }
-
- }
- } else {
- NSAlert *alert = [[NSAlert alloc] init];
- [alert setAlertStyle:NSAlertStyleCritical];
- [alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Failed to merge!", nil)]];
- NSModalResponse response = [alert runModal];
- if (response == 0 ) {
- [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
- [[self window] orderOut:self];
- }
- }
- });
- });
- } else {
- [self->_progress setHidden:YES];
- [self.nCancelVC setEnabled:YES];
- self.canMerge = YES;
- self.loading_backgroundView.hidden = YES;
- }
- }];
- });
- });
- } else {
- [self.nCancelVC setEnabled:YES];
- self.canMerge = YES;
-
- if (allPage) {
- if (!self.PDFDocument.outlineRoot) {
- PDFOutline *outline = [[PDFOutline alloc] init];
- self.PDFDocument.outlineRoot = outline;
- }
-
- for (NSInteger i = 0; i < self.rootPDFOutlineArray.count; i++) {
- PDFOutline *rootPDFOutline = _rootPDFOutlineArray[i];
- for (NSInteger j = 0; j < rootPDFOutline.numberOfChildren; j ++) {
- [self.PDFDocument.outlineRoot insertChild:[rootPDFOutline childAtIndex:j] atIndex:self.PDFDocument.outlineRoot.numberOfChildren];
-
- }
- }
- }
- if (_isSuccessfully) {
- if (self.limit) {
- [self handerReDraw];
- }
-
- KMSavePanelAccessoryController *savePanelAccessoryViewController = [[KMSavePanelAccessoryController alloc] init];
- NSSavePanel *savePanel = [NSSavePanel savePanel];
- savePanel.nameFieldStringValue = fileName;
- savePanel.allowedFileTypes = @[@"pdf"];
- savePanel.accessoryView = savePanelAccessoryViewController.view;
- self.savePanelAccessoryViewController = savePanelAccessoryViewController;
- [savePanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
- if (result) {
- NSString * outputSavePanel = savePanel.URL.path;
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
- self->_isSuccessfully = [self.PDFDocument writeToFile:outputSavePanel];
-
- if (!self->_isSuccessfully) {
- self->_isSuccessfully = [self.PDFDocument.dataRepresentation writeToFile:outputSavePanel atomically:YES];
- }
- dispatch_async(dispatch_get_main_queue(), ^{
- [self->_progress setHidden:YES];
- [self.nCancelVC setEnabled:YES];
- self.canMerge = YES;
- self.loading_backgroundView.hidden = YES;
-
- if (self->_isSuccessfully) {
- [self _clearData];
- [self recordMergeCount];
-
- [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
- [[self window] orderOut:self];
-
- if (self.saveAsPDFFilePath) {
- BOOL autoOpen = self.savePanelAccessoryViewController.needOpen;
- if (autoOpen) {
- self.saveAsPDFFilePath(outputSavePanel);
- self.saveAsPDFFilePath = nil;
- } else {
- [self viewFileAtFinder:outputSavePanel];
-
- }
- } else {
- BOOL autoOpen = self.savePanelAccessoryViewController.needOpen;
- if (autoOpen) {
- [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:[NSURL fileURLWithPath:outputSavePanel] display:YES completionHandler:^(NSDocument * _Nullable document, BOOL documentWasAlreadyOpen, NSError * _Nullable error) {
-
- }];
- } else {
- [self viewFileAtFinder:outputSavePanel];
- }
-
- }
- } else {
- NSAlert *alert = [[NSAlert alloc] init];
- [alert setAlertStyle:NSAlertStyleCritical];
- [alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Failed to merge!", nil)]];
- NSModalResponse response = [alert runModal];
- if (response == 0 ) {
- [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
- [[self window] orderOut:self];
- }
- }
- });
- });
- } else {
- [self->_progress setHidden:YES];
- [self.nCancelVC setEnabled:YES];
- self.canMerge = YES;
- self.loading_backgroundView.hidden = YES;
- }
- }];
- } else {
-
- NSAlert *alert = [[NSAlert alloc] init];
- [alert setAlertStyle:NSAlertStyleCritical];
- [alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Failed to merge!", nil)]];
- NSModalResponse response = [alert runModal];
- if (response == 0 ) {
-
- [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
- [[self window] orderOut:self];
- }
- }
- }
- }
- - (IBAction)buttonItemClicked_Clear:(id)sender
- {
- [_files removeAllObjects];
- [self updateButtonState];
- [self.tableView reloadData];
- self.allFileSize = 0;
- }
- - (IBAction)buttonItemClicked_AddFile:(id)sender
- {
- NSMenu *menu = [[NSMenu alloc] init];
- NSMenuItem *addFileItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"add files", "") action:@selector(itemAddFileAction) keyEquivalent:@""];
- addFileItem.target = self;
- [menu addItem:addFileItem];
-
- NSMenuItem *addFolderitem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"add Folder", "") action:@selector(itemAddFolderAction) keyEquivalent:@""];
- addFolderitem.target = self;
- [menu addItem:addFolderitem];
-
- NSMenuItem *addOpenFileitem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"add Open Files", "") action:@selector(itemAddOpenFileAction) keyEquivalent:@""];
- addOpenFileitem.target = self;
- [menu addItem:addOpenFileitem];
-
- [menu popUpMenuPositioningItem:nil atLocation:NSMakePoint(100, 10) inView:sender];
- }
- - (void)itemAddFileAction {
- NSOpenPanel *openPanel = [[NSOpenPanel alloc] init];
- [openPanel setAllowedFileTypes:[KMImageAccessoryController supportedImageTypes]];
- [openPanel setAllowsMultipleSelection:YES];
- openPanel.message = NSLocalizedString(@"Press ⌘/⇧ to select multiple files", nil);
- [openPanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result){
-
- if (result == NSModalResponseOK) {
-
- NSMutableArray *array = [NSMutableArray array];
- for (NSURL *fileURL in [openPanel URLs]) {
-
- if (fileURL) {
- if (![self isExistAtFilepath:fileURL.path]) {
- continue;
- }
-
- NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil];
- CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
- self.allFileSize = self.allFileSize + fileSize;
- [array addObject:fileURL.path];
- }
- }
- [self addFiles:array];
- }
- }];
- }
- - (void)itemAddFolderAction {
- NSOpenPanel *openPanel = [[NSOpenPanel alloc] init];
- [openPanel setAllowedFileTypes:[KMImageAccessoryController supportedImageTypes]];
- [openPanel setAllowsMultipleSelection:YES];
- openPanel.canChooseFiles = NO;
- openPanel.canChooseDirectories = YES;
- openPanel.message = NSLocalizedString(@"Press ⌘/⇧ to select multiple files", nil);
- [openPanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result){
-
- if (result == NSModalResponseOK) {
- NSMutableArray *result = [NSMutableArray array];
- for (NSURL *url in openPanel.URLs) {
- [self findAllFiles:url result:result];
- }
-
- NSMutableArray *array = [NSMutableArray array];
- for (NSURL *fileURL in result) {
- if (fileURL) {
- if (![self isExistAtFilepath:fileURL.path]) {
- continue;
- }
-
- NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil];
- CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
- self.allFileSize = self.allFileSize + fileSize;
- [array addObject:fileURL.path];
- }
- }
- [self addFiles:array];
- }
- }];
- }
- - (void)itemAddOpenFileAction {
- [_lockFilePathArr removeAllObjects];
- _lockFileIndex = 0;
-
- for (NSWindow *window in NSApp.windows) {
- NSWindowController *controller = [window windowController];
- if (![controller isKindOfClass:[CTBrowserWindowController class]]) {
- continue;
- }
-
- CTTabStripModel *model = ((CTBrowserWindowController *)controller).browser.tabStripModel;
- for (int i = 0; i < model.count; i++) {
- KMMainDocument *document = (KMMainDocument *)[model tabContentsAtIndex:i];
- // if (document.windowControllers.count == 0) {
- // break;
- // }
- if (document.isHome) {
- continue;
- }
-
- KMMainViewController *mainViewController = document.mainViewController;
- if ([self.PDFDocument.documentURL.path isEqualToString:document.fileURL.path]) {
- continue;
- }
-
- if (![self isExistAtFilepath:document.fileURL.path]) {
- continue;
- }
-
- CPDFDocument *pdfDocument = mainViewController.listView.document;
- if (pdfDocument.isLocked || (!pdfDocument.allowsCopying || !pdfDocument.allowsPrinting)) {
- [_lockFilePathArr addObject:pdfDocument.documentURL.path];
- } else {
- KMFileAttribute *file = [[KMFileAttribute alloc] init];
- file.filePath = document.fileURL.path;
- file.pdfDocument = mainViewController.document;
- [_files addObject:file];
- }
- }
- }
-
- [self updateButtonState];
- [_tableView reloadData];
-
- if ([_lockFilePathArr count]) {
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- if (self.files.count == 0) {
- self.insertRow = 0;
- } else {
- self.insertRow = self.files.count;
- }
- [self openPasswordWindow];
- });
- }
- }
- - (void)findAllFiles:(NSURL *)folderURL result:(NSMutableArray *)result {
- NSFileManager *fm = [NSFileManager defaultManager];
- BOOL isDirectory = NO;
- [fm fileExistsAtPath:folderURL.path isDirectory:&isDirectory];
- if (!isDirectory) {
- return;
- }
-
- NSArray *contents = [fm contentsOfDirectoryAtURL:folderURL includingPropertiesForKeys:nil options:0 error:nil];
- if (contents.count == 0) {
- return;
- }
-
- NSArray *array = @[@"jpg",@"cur",@"bmp",@"jpeg",@"gif",@"png",@"tiff",@"tif",/*@"pic",*/@"ico",@"icns",@"tga",@"psd",@"eps",@"hdr",@"jp2",@"jpc",@"pict",@"sgi",@"heic",@"pdf"];
- for (NSURL *documentURL in contents) {
- BOOL isDirectory = NO;
- [fm fileExistsAtPath:documentURL.path isDirectory:&isDirectory];
- if (isDirectory) {
- [self findAllFiles:documentURL result:result];
- } else {
- if (![array containsObject:[documentURL.pathExtension lowercaseString]]) {
- continue;
- }
-
- [result addObject:documentURL];
- }
- }
- }
- - (IBAction)buttonClicked_ChooseSize:(KMImageTitleButton *)sender {
- int i = 0;
- for (KMImageTitleButton *button in @[self.originalSizeButton, self.A4SizeButton, self.A3SizeButton,
- self.USLetterSizeButton, self.USLegalButton, self.customSizeButton]) {
- KMImageTitleButton *radio = button.subviews.lastObject;
- radio.state = NSControlStateValueOff;
- radio.imageName = @"btn_radio_unsel_on";
- i += 1;
- }
- sender.state = NSControlStateValueOn;
- sender.imageName = @"btn_radio_sel_on";
-
- KMImageTitleButton *customSizeButton = self.customSizeButton.subviews.lastObject;
- self.pageSizeHeightTextField.enabled = self.pageSizeWidthTextField.enabled = [sender isEqual:customSizeButton];
- switch (sender.contentButton.tag) {
- case 0:
-
- break;
-
- case 1:
- self.newPageSize = CGSizeMake(595, 841);
- break;
- case 2:
- self.newPageSize = CGSizeMake(841, 1190);
- break;
- case 3:
- self.newPageSize = CGSizeMake(612, 792);
- break;
- case 4:
- self.newPageSize = CGSizeMake(612, 1108);
- break;
- case 5:
- self.newPageSize = CGSizeMake(595, 841);
- self.pageSizeWidthTextField.stringValue = [NSString stringWithFormat:@"%.0f",self.newPageSize.width];
- self.pageSizeHeightTextField.stringValue = [NSString stringWithFormat:@"%.0f",self.newPageSize.height];
- break;
- default:
- break;
- }
- }
- #pragma mark - Private Method
- //获取所有的子节点
- - (void)fetchAllOfChildren:(PDFOutline *)aOutline containerArray:(NSMutableArray *)aMArray
- {
- if (![aMArray containsObject:aOutline]) {
- [aMArray addObject:aOutline];
- }
- for (NSInteger i = 0; i < aOutline.numberOfChildren; i++) {
- [aMArray addObject:[aOutline childAtIndex:i]];
- [self fetchAllOfChildren:[aOutline childAtIndex:i] containerArray:aMArray];
- }
- }
- @end
- static NSString *kKMMergeCountKey = @"KMMergeCountKey";
- static NSInteger kKMMergeCountMax = 2;
- @implementation KMPDFEditAppendWindow (KMExtension)
- - (BOOL)mergeCountIsReach {
- #if VERSION_DMG
- return NO;
- #else
- if ([KMLightMemberManager manager].isLogin) {
- return NO;
- }
- NSInteger cnt = [self getMergeCount];
- if (cnt >= kKMMergeCountMax) {
- return YES;
- }
- return NO;
- #endif
- }
- - (void)recordMergeCount {
- NSInteger cnt = [self getMergeCount];
- [[NSUserDefaults standardUserDefaults] setInteger:cnt+1 forKey:kKMMergeCountKey];
- [[NSUserDefaults standardUserDefaults] synchronize];
- }
- - (void)clearMergeCount {
- [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:kKMMergeCountKey];
- [[NSUserDefaults standardUserDefaults] synchronize];
- }
- - (NSInteger)getMergeCount {
- return [[NSUserDefaults standardUserDefaults] integerForKey:kKMMergeCountKey];
- }
- @end
|