123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857 |
- //
- // KMPDFEditAppendWindow.m
- // PDF Reader
- //
- // Created by 丁林圭 on 2017/5/11.
- // Copyright © 2017年 zhangjie. All rights reserved.
- //
- #import "KMPDFEditAppendWindow.h"
- #import <Quartz/Quartz.h>
- #import <Masonry/Masonry.h>
- #import <PDF_Reader_Pro-Swift.h>
- #import "CTBrowserWindowController.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) 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 registerDragged];
- [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 == KMBlankViewMouseEventTypeMouseEnter) {
- weakSelf.myBlankView.imageView.image = [NSImage imageNamed:@"KMImageNameEmptyAddHover"];
- } else if (mouseType == KMBlankViewMouseEventTypeMouseExit) {
- weakSelf.myBlankView.imageView.image = [NSImage imageNamed:@"KMImageNameEmptyAddNormal"];
- } else if (mouseType == KMBlankViewMouseEventTypeMouseDown) {
- weakSelf.myBlankView.imageView.image = [NSImage imageNamed:@"KMImageNameEmptyAddHover"];
- [weakSelf itemAddFileAction];
- } else if (mouseType == KMBlankViewMouseEventTypeMouseUp) {
- weakSelf.myBlankView.imageView.image = [NSImage imageNamed:@"KMImageNameEmptyAddHover"];
- }
- };
- self.myBlankView.dragSuccessBlock = ^(NSArray<NSString *> * 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;
- if (![IAPProductsManager defaultManager].isAvailableAllFunction) {
- //免費版只支援2個檔案做合併小于20M的文件合并
- if (self->_files.count >= 2 || self.allFileSize > (20 * 1024 * 1024)) {
- [[KMPurchaseCompareWindowController sharedInstance] showWindow:nil];
- self.allFileSize = self.allFileSize - fileSize;
- [self addFiles:array];
- return;
- }
- }
- [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 = KMPageRangeAll;
-
- [self.window makeFirstResponder:self];
- } else if (itemIndex == 1) {
- KMFileAttribute *currentFile = [_files objectAtIndex:index];
- currentFile.pagesType = KMPageRangeOdd;
- [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 = KMPageRangeEven;
- [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 = KMPageRangeCustom;
- 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/pdftecheditor"];
- [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.fetchSelectPages) {
- // 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;
- if (![IAPProductsManager defaultManager].isAvailableAllFunction) {
- //免費版只支援2個檔案做合併小于20M的文件合并
- if (_files.count >= 2 || self.allFileSize > (20 * 1024 * 1024)) {
- [[KMPurchaseCompareWindowController sharedInstance] showWindow:nil];
- self.allFileSize = self.allFileSize - fileSize;
- [self addFiles:array];
- result = YES;
- return NO;
- }
- }
- [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;
- }
- }
- //计算所有文件的大小,非会员如果超过20M,不允许继续操作
- CGFloat allSize;
-
- for (KMFileAttribute *file in _files) {
- NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:[file filePath] error:nil];
- allSize = [[attrib objectForKey:NSFileSize] floatValue] + allSize;
-
- }
-
- if (![IAPProductsManager defaultManager].isAvailableAllFunction) {
- if (allSize > (20 * 1024 * 1024) || _files.count > 2) {
- [[KMPurchaseCompareWindowController sharedInstance] showWindow:nil];
- return;
- }
- }
-
- _isSuccessfully = NO;
- [self.nCancelVC setEnabled:NO];
- self.canMerge = NO;
-
- BOOL allPage = YES;//只有是全部才支持大纲的合并
- for (KMFileAttribute *file in _files) {
- if (!file.fetchSelectPages) {
- 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.fetchSelectPages) {
- 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
- {
- if (![IAPProductsManager defaultManager].isAvailableAllFunction) {
- //免費版只支援2個檔案做合併小于20M的文件合并
- if (_files.count >= 2 || self.allFileSize > (20 * 1024 * 1024)) {
- [[KMPurchaseCompareWindowController sharedInstance] showWindow:nil];
- return;
- }
-
- }
- 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
|