KMPDFEditAppendWindow.m 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  1. //
  2. // KMPDFEditAppendWindow.m
  3. // PDF Reader
  4. //
  5. // Created by 丁林圭 on 2017/5/11.
  6. // Copyright © 2017年 zhangjie. All rights reserved.
  7. //
  8. #import "KMPDFEditAppendWindow.h"
  9. #import "PasswordWindowController.h"
  10. #import "KMFileAttribute.h"
  11. #import <Quartz/Quartz.h>
  12. #import "KMSavePanelAccessoryController.h"
  13. #import "KMBatchTableRowView.h"
  14. #import "KMBlankView.h"
  15. #import <Masonry/Masonry.h>
  16. #import "KMImageAccessoryController.h"
  17. #if VERSION_DMG
  18. #import <PDF_Master-Swift.h>
  19. #else
  20. #import <PDF_Master-Swift.h>
  21. #endif
  22. #import "CTBrowserWindowController.h"
  23. #import "NSButton+TitleColor.h"
  24. #import "KMPDFMergeFileNameTabelViewCell.h"
  25. #import "PDFPage_SKExtensions.h"
  26. #import "TextFieldFormatter.h"
  27. @interface KMPDFEditAppendTabelViewCell()
  28. @property (assign) IBOutlet NSTextField *pageCountLabel;
  29. @property (assign) IBOutlet NSTextField *indexLabel;
  30. @property (assign) IBOutlet NSTextField *fileNameLabel;
  31. @property (assign) IBOutlet NSTextField *pageRangeLabel;
  32. @property (assign) IBOutlet NSTextField *sizeLabel;
  33. @property (assign) IBOutlet NSImageView *fileImage;
  34. @property (assign) IBOutlet NSComboBox *pageRangeBox;
  35. @property (nonatomic, copy) void(^removeCallBack)(void);
  36. @end
  37. @implementation KMPDFEditAppendTabelViewCell
  38. - (void)dealloc {}
  39. - (IBAction)buttonClicked_Remove:(id)sender {
  40. if (self.removeCallBack) {
  41. self.removeCallBack();
  42. }
  43. }
  44. - (void)awakeFromNib {
  45. [super awakeFromNib];
  46. }
  47. @end
  48. @interface KMAppendPDFPage: PDFPage
  49. @property (nonatomic, strong)PDFPage *drawingPage;
  50. @end
  51. @implementation KMAppendPDFPage
  52. - (void)dealloc {}
  53. //- (void)drawWithBox:(PDFDisplayBox)box {
  54. // [super drawWithBox:box];
  55. //
  56. // CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
  57. // CGSize pageSize = [self boundsForBox:kPDFDisplayBoxCropBox].size;
  58. // [self drawPageWithContext:context Page:self.drawingPage PageSize:pageSize];
  59. //}
  60. - (void)drawWithBox:(PDFDisplayBox)box toContext:(CGContextRef)context {
  61. [super drawWithBox:box toContext:context];
  62. CGSize pageSize = [self boundsForBox:kPDFDisplayBoxCropBox].size;
  63. [self drawPageWithContext:context Page:self.drawingPage PageSize:pageSize];
  64. }
  65. - (void)drawPageWithContext:(CGContextRef) context
  66. Page:(PDFPage *)page
  67. PageSize:(CGSize)pageSize
  68. {
  69. CGSize originalSize = [page boundsForBox:kPDFDisplayBoxCropBox].size;
  70. //如果page的旋转角度为90,或者270,宽高交换
  71. if (page.rotation%180) {
  72. originalSize = CGSizeMake(originalSize.height, originalSize.width);
  73. }
  74. CGFloat wRatio = pageSize.width/originalSize.width;
  75. CGFloat hRatio = pageSize.height/originalSize.height;
  76. CGFloat ratio = MIN(wRatio, hRatio);
  77. CGContextSaveGState(context);
  78. CGFloat xTransform = (pageSize.width - originalSize.width * ratio)/2;
  79. CGFloat yTransform = (pageSize.height - originalSize.height * ratio)/2;
  80. CGContextTranslateCTM(context, xTransform, yTransform);
  81. CGContextScaleCTM(context, ratio, ratio);
  82. if (@available(macOS 10.12, *)) {
  83. [page drawWithBox:kPDFDisplayBoxCropBox toContext:context];
  84. [page transformContext:context forBox:kPDFDisplayBoxCropBox];
  85. } else {
  86. [NSGraphicsContext saveGraphicsState];
  87. [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:YES]];
  88. [page drawWithBox:kPDFDisplayBoxCropBox toContext:context];
  89. [NSGraphicsContext restoreGraphicsState];
  90. [page transformContext:context forBox:kPDFDisplayBoxCropBox];
  91. }
  92. CGContextRestoreGState(context);
  93. }
  94. @end
  95. #define MyTableCellViewDataType @"MyTableCellViewDataType"
  96. static NSString * const KMTableColumnFileNameID = @"fileName";
  97. static NSString * const KMTableColumnPageRangeID = @"pageRangD";
  98. static NSString * const KMTableColumnSizeID = @"size";
  99. static KMPDFEditAppendWindow *windowController = nil;
  100. @interface KMPDFEditAppendWindow ()<NSTableViewDataSource,NSTableViewDelegate,NSComboBoxDelegate, KMSelectPopButtonDelegate>
  101. {
  102. BOOL _isSuccessfully;
  103. NSInteger _insertRow;
  104. }
  105. @property (nonatomic, assign) NSInteger insertRow;
  106. @property (weak) IBOutlet NSBox *tableHeaderBox;
  107. @property (weak) IBOutlet NSTextField *fileNameLabel;
  108. @property (weak) IBOutlet NSTextField *pageRangeLabel;
  109. @property (weak) IBOutlet NSTextField *sizeLabel;
  110. @property (weak) IBOutlet NSBox *tableHeaderBottomLine;
  111. @property (assign) IBOutlet NSTableView *tableView;
  112. @property (assign) IBOutlet NSButton *removeButton;
  113. @property (assign) IBOutlet NSButton *appendButton;
  114. @property (assign) IBOutlet NSButton *cancelButton;
  115. //@property (assign) IBOutlet NSButton *addFileButton;
  116. @property (assign) IBOutlet NSBox *clearBox;
  117. @property (assign) IBOutlet NSBox *nMergeBox;
  118. @property (assign) IBOutlet NSBox *nCancelBox;
  119. @property (weak) IBOutlet NSBox *addBox;
  120. @property (nonatomic, strong) KMDesignSelect *addFileButton;
  121. @property (assign) IBOutlet NSProgressIndicator *progress;
  122. //宽输入框
  123. @property (assign) IBOutlet NSTextField *pageSizeWidthTextField;
  124. //宽高输入连接符
  125. @property (assign) IBOutlet NSTextField *pageSizeWidthHeightConnectorTextField;
  126. //高输入框
  127. @property (assign) IBOutlet NSTextField *pageSizeHeightTextField;
  128. //宽高单位
  129. @property (assign) IBOutlet NSTextField *pageSizeUnitLabel;
  130. @property (nonatomic,retain) NSMutableArray *files;
  131. @property (nonatomic,retain) NSArray *addFiles;
  132. @property (nonatomic, retain) NSMutableIndexSet *insertIndexSet;
  133. @property (nonatomic,retain) NSString *password;
  134. @property (nonatomic,retain) PDFDocument *PDFDocument;
  135. @property (nonatomic,assign) KMPDFPageEditType editType;
  136. @property (nonatomic,assign) CGFloat allFileSize;
  137. @property (nonatomic,retain) NSMutableArray *rootPDFOutlineArray;
  138. @property (nonatomic,assign) CGSize newPageSize;
  139. @property (nonatomic,retain) NSMutableArray *lockFilePathArr;
  140. @property (nonatomic,assign) NSInteger lockFileIndex;
  141. @property (weak) IBOutlet NSView *originalSizeButton;
  142. @property (weak) IBOutlet KMImageTitleButton *A4SizeButton;
  143. @property (weak) IBOutlet KMImageTitleButton *A3SizeButton;
  144. @property (weak) IBOutlet KMImageTitleButton *USLetterSizeButton;
  145. @property (weak) IBOutlet KMImageTitleButton *USLegalButton;
  146. @property (weak) IBOutlet KMImageTitleButton *customSizeButton;
  147. @property (assign) IBOutlet NSBox *box;
  148. @property (assign) IBOutlet NSTextField *boxLabel;
  149. @property (assign) IBOutlet KMBlankView *myBlankView;
  150. @property (nonatomic, strong) PasswordWindowController *passwordWindowController;
  151. @property (nonatomic, strong) KMSavePanelAccessoryController *savePanelAccessoryViewController;
  152. @property (nonatomic, assign) BOOL canMerge;
  153. @property (nonatomic, strong) KMDesignButton *clearVC;
  154. @property (nonatomic, strong) KMDesignButton *nMergeVC;
  155. @property (nonatomic, strong) KMDesignButton *nCancelVC;
  156. @property (nonatomic, strong) NSButton *loading_backgroundView;
  157. @property (nonatomic, strong) NSAlert *alert;
  158. @end
  159. @implementation KMPDFEditAppendWindow
  160. - (void)dealloc
  161. {
  162. _tableView.delegate = nil;
  163. _tableView.dataSource = nil;
  164. }
  165. //没有打开文档,选择he'bing
  166. - (id)initWithFilePaths:(NSArray *)files
  167. {
  168. if (self = [super initWithWindowNibName:@"KMPDFEditAppendWindow"]) {
  169. self.PDFDocument = [[PDFDocument alloc] init];
  170. self.addFiles = files;
  171. self.editType = KMPDFPageEditMerge;
  172. _lockFilePathArr = [[NSMutableArray alloc] init];
  173. _files = [[NSMutableArray alloc] init];
  174. }
  175. return self;
  176. }
  177. - (id)initWithPDFDocument:(PDFDocument *)document password:(NSString *)password
  178. {
  179. if (self = [super initWithWindowNibName:@"KMPDFEditAppendWindow"]) {
  180. // self.PDFDocument = document;
  181. self.PDFDocument = [[PDFDocument alloc] init];
  182. self.editType = KMPDFPageEditAppend;
  183. _lockFilePathArr = [[NSMutableArray alloc] init];
  184. _files = [[NSMutableArray alloc] init];
  185. KMFileAttribute *file = [[KMFileAttribute alloc] init];
  186. file.myPDFDocument = document;
  187. file.filePath = document.documentURL.path;
  188. if (password && password.length > 0) {
  189. file.password = password;
  190. file.isLocked = YES;
  191. }
  192. [self.files addObject:file];
  193. }
  194. return self;
  195. }
  196. - (void)windowDidLoad {
  197. [super windowDidLoad];
  198. self.window.contentView.wantsLayer = YES;
  199. self.window.appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
  200. self.clearVC = [[KMDesignButton alloc] initWithType: DesignButtonTypeImage];
  201. self.nMergeVC = [[KMDesignButton alloc] initWithType: DesignButtonTypeText];
  202. self.nCancelVC = [[KMDesignButton alloc] initWithType: DesignButtonTypeText];
  203. // self.nMergeVC.button.keyEquivalent = @"\\r";
  204. self.clearBox.fillColor = NSColor.clearColor;
  205. self.clearBox.contentView = self.clearVC.view;
  206. self.nMergeBox.fillColor = NSColor.clearColor;
  207. self.nMergeBox.contentView = self.nMergeVC.view;
  208. self.nCancelBox.fillColor = NSColor.clearColor;
  209. self.nCancelBox.contentView = self.nCancelVC.view;
  210. self.box.cornerRadius = 0;
  211. self.box.fillColor = [NSColor colorWithRed:247/255.f green:248/255.f blue:250/255.f alpha:1.f];
  212. self.window.title = NSLocalizedString(@"Merge PDF Files",nil);
  213. self.pageSizeWidthTextField.enabled = NO;
  214. self.pageSizeHeightTextField.enabled = NO;
  215. self.pageSizeWidthTextField.stringValue = @"595";
  216. self.pageSizeHeightTextField.stringValue = @"841";
  217. self.pageSizeWidthTextField.formatter = [[TextFieldFormatter alloc] init];
  218. self.pageSizeHeightTextField.formatter = [[TextFieldFormatter alloc] init];
  219. NSArray *titles = @[NSLocalizedString(@"Original Size", nil),@"A4",@"A3",
  220. NSLocalizedString(@"U.S.Letter", nil),NSLocalizedString(@"U.S.Legal", nil),NSLocalizedString(@"Custom", nil)];
  221. __weak typeof(self) weakSelf = self;
  222. int i = 0;
  223. for (KMImageTitleButton *button in @[self.originalSizeButton, self.A4SizeButton, self.A3SizeButton,
  224. self.USLetterSizeButton, self.USLegalButton, self.customSizeButton]) {
  225. KMImageTitleButton *radio = [[KMImageTitleButton alloc] init];
  226. [button addSubview:radio];
  227. radio.frame = button.bounds;
  228. radio.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
  229. radio.titleLabel.textColor = [NSColor colorWithRed:223/255.f green:225/255.f blue:229/255.f alpha:1.f];
  230. radio.title = titles[i];
  231. if (i == 0) {
  232. radio.state = NSControlStateValueOn;
  233. radio.imageName = @"btn_radio_sel_on";
  234. } else {
  235. radio.state = NSControlStateValueOff;
  236. radio.imageName = @"btn_radio_unsel_on";
  237. }
  238. radio.titleLabel.textColor = [NSColor colorWithRed:223/255.f green:225/255.f blue:229/255.f alpha:1.f];
  239. radio.titleLabel.font = [NSFont fontWithName:@"SFProText-Regular" size:14];
  240. radio.action = ^(KMImageTitleButton *view, NSButton *button) {
  241. [weakSelf buttonClicked_ChooseSize:view];
  242. };
  243. radio.contentButton.tag = i;
  244. i += 1;
  245. }
  246. self.nCancelVC.target = self;
  247. self.nCancelVC.action = @selector(buttonItemClicked_Cancel:);
  248. self.nCancelVC.stringValue = NSLocalizedString(@"Cancel", nil);
  249. [self.nCancelVC buttonWithType:TokenButtonTypeSec size:TokenButtonSizeM height:[[NSLayoutConstraint alloc] init]];
  250. _removeButton.title = NSLocalizedString(@"Remove", nil);
  251. self.clearVC.target = self;
  252. self.clearVC.action = @selector(buttonItemClicked_Clear:);
  253. self.clearVC.image = [NSImage imageNamed:@"icon_btn_clear_false_norm"];
  254. self.clearVC.image_hov = [NSImage imageNamed:@"icon_btn_clear_false_hov"];
  255. self.clearVC.image_act = [NSImage imageNamed:@"icon_btn_clear_false_act"];
  256. self.clearVC.image_disabled = [NSImage imageNamed:@"icon_btn_clear_Disabled_norm"];
  257. [self.clearVC buttonWithType:TokenButtonTypeSec_Icon size:TokenButtonSizeM height:[[NSLayoutConstraint alloc] init]];
  258. [_addFileButton setTitle:NSLocalizedString(@"Add Files", nil)];
  259. self.nMergeVC.target = self;
  260. self.nMergeVC.action = @selector(buttonItemClicked_Append:);
  261. self.nMergeVC.stringValue = NSLocalizedString(@"Merge", nil);
  262. [self.nMergeVC buttonWithType:TokenButtonTypeCta size:TokenButtonSizeM height:[[NSLayoutConstraint alloc] init]];
  263. self.tableView.delegate = self;
  264. self.tableView.dataSource = self;
  265. self.tableView.allowsMultipleSelection = YES;
  266. [_tableView registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, NSPasteboardTypeString, NSPasteboardTypePDF, MyTableCellViewDataType, nil]];
  267. NSArray *tableHeaderTitles = @[NSLocalizedString(@"File Name", nil),NSLocalizedString(@"Page Range", nil),NSLocalizedString(@"Size", nil)];
  268. i = 0;
  269. for (NSTextField *label in @[self.fileNameLabel, self.pageRangeLabel, self.sizeLabel]) {
  270. label.stringValue = tableHeaderTitles[i];
  271. label.textColor = [NSColor colorWithRed:97/255.f green:100/255.f blue:105/255.f alpha:1.f];
  272. label.font = [NSFont fontWithName:@"SFProText-Regular" size:12];
  273. i += 1;
  274. }
  275. self.tableHeaderBottomLine.fillColor = [NSColor colorWithRed:223/255.f green:225/255.f blue:229/255.f alpha:1.f];
  276. [self.tableView removeTableColumn:self.tableView.tableColumns.firstObject];
  277. NSTableColumn *column1 = [[NSTableColumn alloc] initWithIdentifier:KMTableColumnFileNameID];
  278. column1.width = 446+16;
  279. column1.title = NSLocalizedString(@"File Name", nil);
  280. [self.tableView addTableColumn:column1];
  281. NSTableColumn *column2 = [[NSTableColumn alloc] initWithIdentifier:KMTableColumnPageRangeID];
  282. column2.width = 160+8;
  283. column2.title = NSLocalizedString(@"Page Range", nil);
  284. [self.tableView addTableColumn:column2];
  285. NSTableColumn *column3 = [[NSTableColumn alloc] initWithIdentifier:KMTableColumnSizeID];
  286. column3.width = 139+12;
  287. column3.title = NSLocalizedString(@"Size", nil);
  288. [self.tableView addTableColumn:column3];
  289. self.tableView.rowHeight = 76;
  290. _insertIndexSet = [[NSMutableIndexSet alloc] init];
  291. [self addFiles:_addFiles];
  292. [_progress setHidden:YES];
  293. [self.tableView addSubview:self.myBlankView];
  294. [self.myBlankView mas_makeConstraints:^(MASConstraintMaker *make) {
  295. make.left.top.width.equalTo(self.tableView);
  296. make.height.equalTo(self.tableView).offset(0);
  297. }];
  298. self.myBlankView.wantsLayer = YES;
  299. self.myBlankView.layer.backgroundColor = [NSColor clearColor].CGColor;
  300. self.myBlankView.titleLabel.stringValue = NSLocalizedString(@"Select Files", nil);
  301. self.myBlankView.titleLabel.textColor = [NSColor colorWithRed:97/255.f green:100/255.f blue:105/255.f alpha:1];
  302. self.myBlankView.titleLabel.font = [NSFont fontWithName:@"SFProText-Regular" size:14];
  303. 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);
  304. self.myBlankView.secondTitleLabel.textColor = [NSColor colorWithRed:148/255.f green:152/255.f blue:156/255.f alpha:1];
  305. self.myBlankView.secondTitleLabel.font = [NSFont fontWithName:@"SFProText-Regular" size:12];
  306. self.myBlankView.imageView.image = [NSImage imageNamed:@"KMImageNameEmptyAddNormal"];
  307. self.myBlankView.allowedFileTypes = [KMImageAccessoryController supportedImageTypes];
  308. self.myBlankView.mouseActionCallBack = ^(KMBlankViewMouseEventType mouseType) {
  309. if (mouseType == KMBlankViewMouseEventType_MouseEnter) {
  310. weakSelf.myBlankView.imageView.image = [NSImage imageNamed:@"KMImageNameEmptyAddHover"];
  311. } else if (mouseType == KMBlankViewMouseEventType_MouseExit) {
  312. weakSelf.myBlankView.imageView.image = [NSImage imageNamed:@"KMImageNameEmptyAddNormal"];
  313. } else if (mouseType == KMBlankViewMouseEventType_MouseDown) {
  314. weakSelf.myBlankView.imageView.image = [NSImage imageNamed:@"KMImageNameEmptyAddHover"];
  315. [weakSelf itemAddFileAction];
  316. } else if (mouseType == KMBlankViewMouseEventType_MouseUp) {
  317. weakSelf.myBlankView.imageView.image = [NSImage imageNamed:@"KMImageNameEmptyAddHover"];
  318. }
  319. };
  320. self.myBlankView.dragSuccessBlock = ^(NSMutableArray *fileNames) {
  321. NSMutableArray *array = [NSMutableArray array];
  322. for(NSString *path in fileNames){
  323. if (![weakSelf isExistAtFilepath:path]) {
  324. continue;
  325. }
  326. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
  327. CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
  328. weakSelf.allFileSize = weakSelf.allFileSize + fileSize;
  329. // weakSelf.allFileSize = weakSelf.allFileSize - fileSize;
  330. [array addObject:path];
  331. }
  332. [weakSelf addFiles:array];
  333. };
  334. [self updateButtonState];
  335. id outputBookMarkObject = [[NSUserDefaults standardUserDefaults] objectForKey:@"kmAppendOutputPath"];
  336. if ([outputBookMarkObject isKindOfClass:[NSData class]]) {
  337. NSData* outputBookMarkData = (NSData*)outputBookMarkObject;
  338. NSURL *bookmarkedURL = [NSURL URLByResolvingBookmarkData:outputBookMarkData options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:nil error:nil];
  339. [bookmarkedURL startAccessingSecurityScopedResource];
  340. if (bookmarkedURL) {
  341. // _outputText.stringValue = [bookmarkedURL path];
  342. }
  343. }
  344. self.boxLabel.stringValue = NSLocalizedString(@"Page size:", nil);
  345. self.boxLabel.textColor = [NSColor colorWithRed:37/255.f green:38/255.f blue:41/255.f alpha:1.f];
  346. self.boxLabel.font = [NSFont fontWithName:@"SFProText-Regular" size:16];
  347. self.addFileButton = [[KMDesignSelect alloc] initWithType:1];
  348. self.addBox.fillColor = [NSColor clearColor];
  349. self.addBox.contentView = self.addFileButton.view;
  350. [self.addFileButton removeAllItems];
  351. [self.addFileButton addItemsWithObjectValues:@[NSLocalizedString(@"Add Files", ""),NSLocalizedString(@"Add Folder", ""),NSLocalizedString(@"Add Open Files", "")]];
  352. [self.addFileButton selectItemAt:0];
  353. self.addFileButton.editable = false; //默认属性,初始化可不设置
  354. self.addFileButton.delete = self;
  355. [self.addFileButton updateUI];
  356. self.loading_backgroundView = [[NSButton alloc] init];
  357. [self.window.contentView addSubview:self.loading_backgroundView];
  358. self.loading_backgroundView.frame = self.window.contentView.bounds;
  359. self.loading_backgroundView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
  360. self.loading_backgroundView.title = @"";
  361. self.loading_backgroundView.bordered = NO;
  362. self.loading_backgroundView.wantsLayer = YES;
  363. self.loading_backgroundView.layer.backgroundColor = [NSColor clearColor].CGColor;
  364. self.loading_backgroundView.hidden = YES;
  365. }
  366. - (void)updateButtonState
  367. {
  368. if (_files.count > 0) {
  369. self.myBlankView.hidden = YES;
  370. self.tableHeaderBox.hidden = NO;
  371. [_removeButton setEnabled:YES];
  372. [self.clearVC setEnabled:YES];
  373. if (self.files.count > 1) {
  374. [self.nMergeVC setEnabled:YES];
  375. self.canMerge = YES;
  376. } else {
  377. self.canMerge = NO;
  378. [self.nMergeVC setEnabled:NO];
  379. }
  380. } else {
  381. self.myBlankView.hidden = NO;
  382. self.tableHeaderBox.hidden = YES;
  383. [_removeButton setEnabled:NO];
  384. [self.clearVC setEnabled:NO];
  385. self.canMerge = NO;
  386. [self.nMergeVC setEnabled:NO];
  387. }
  388. }
  389. #pragma mark -NSTextFieldDelegate
  390. - (void)controlTextDidChange:(NSNotification *)obj
  391. {
  392. NSTextField *textField = (NSTextField*)[obj object];
  393. NSInteger index = textField.tag;
  394. [[_files objectAtIndex:index] setPagesString:textField.stringValue];
  395. }
  396. #pragma mark Show Methods
  397. - (void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
  398. if (contextInfo != NULL) {
  399. void (^handler)(BOOL isSuccessfully ,NSIndexSet * selectedIndexSet) = CFBridgingRelease(contextInfo);
  400. handler(_isSuccessfully,self.insertIndexSet);
  401. }
  402. windowController = nil;
  403. }
  404. - (void)beginSheetModalForWindow:(NSWindow *)window completionHandler:(void (^)(BOOL isSuccessfully,NSIndexSet * selectedIndexSet))handler
  405. {
  406. if (windowController.window.isSheet) {
  407. #if DEBUG
  408. NSLog(@"合并窗口已显示");
  409. #endif
  410. return;
  411. }
  412. windowController = self;
  413. [NSApp beginSheet:[self window]
  414. modalForWindow:window
  415. modalDelegate:self
  416. didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
  417. contextInfo:handler ? CFBridgingRetain(handler) : NULL];
  418. }
  419. #pragma mark Private methor
  420. - (void)viewFileAtFinder:(NSString *)filePath
  421. {
  422. NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
  423. NSURL *url = [NSURL fileURLWithPath:filePath];
  424. [workspace activateFileViewerSelectingURLs:[NSArray arrayWithObject:url]];
  425. }
  426. - (void)addFiles:(NSArray*)files
  427. {
  428. [_lockFilePathArr removeAllObjects];
  429. _lockFileIndex = 0;
  430. for (NSString *filePath in files) {
  431. if ([filePath.pathExtension.lowercaseString isEqualToString:@"pdf"]) {
  432. KMFileAttribute *file = [[KMFileAttribute alloc] init];
  433. file.filePath = filePath;
  434. PDFDocument *document = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:filePath]];
  435. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
  436. self.allFileSize = [[attrib objectForKey:NSFileSize] floatValue];
  437. if ([document isLocked]) {
  438. [_lockFilePathArr addObject:filePath];
  439. } else if ([KMTools hasPermissionsLimit:document]) {
  440. [_lockFilePathArr addObject:filePath];
  441. } else {
  442. if ([NSDocument isDamageWithUrl:[NSURL fileURLWithPath:filePath]]) {
  443. NSAlert *alert = [[NSAlert alloc] init];
  444. alert.messageText = NSLocalizedString(@"The file was not added successfully, please select the file again.", @"");
  445. [alert runModal];
  446. } else {
  447. [_files addObject:file];
  448. }
  449. }
  450. } else {
  451. KMFileAttribute *file = [[KMFileAttribute alloc] init];
  452. file.filePath = filePath;
  453. PDFDocument *document = [[PDFDocument alloc] init];
  454. NSImage *image = [[NSImage alloc] initWithContentsOfFile:filePath];
  455. if (image == nil) {
  456. continue;
  457. }
  458. // [document insertPage:image.size withImage:filePath atIndex:0];
  459. [document insertPage:[[PDFPage alloc] initWithImage:image] atIndex:0];
  460. file.myPDFDocument = document;
  461. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
  462. self.allFileSize = [[attrib objectForKey:NSFileSize] floatValue];
  463. if ([NSDocument isDamageWithUrl:[NSURL fileURLWithPath:filePath]]) {
  464. NSAlert *alert = [[NSAlert alloc] init];
  465. alert.messageText = NSLocalizedString(@"The file was not added successfully, please select the file again.", @"");
  466. [alert runModal];
  467. } else {
  468. [_files addObject:file];
  469. }
  470. }
  471. }
  472. [self updateButtonState];
  473. [_tableView reloadData];
  474. if ([_lockFilePathArr count]) {
  475. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  476. if (self.files.count == 0) {
  477. self.insertRow = 0;
  478. } else {
  479. self.insertRow = self.files.count;
  480. }
  481. [self openPasswordWindow];
  482. });
  483. }
  484. }
  485. - (BOOL)isExistAtFilepath:(NSString *)filePath
  486. {
  487. NSMutableArray *array = [NSMutableArray array];
  488. for (KMFileAttribute *file in self.files) {
  489. [array addObject:file.filePath];
  490. }
  491. if ([array containsObject:filePath]) {
  492. return NO;
  493. }
  494. return YES;
  495. }
  496. -(void)openPasswordWindow
  497. {
  498. if ([_lockFilePathArr count] > _lockFileIndex) {
  499. NSString *filePath = [_lockFilePathArr objectAtIndex:_lockFileIndex];
  500. __weak typeof(self) weakSelf = self;
  501. [KMPasswordInputWindow openWindowWithWindow:self.window type:KMPasswordInputWindowTypeOwner url:[NSURL fileURLWithPath:filePath] callback:^(enum KMPasswordInputWindowResult result, NSString * _Nullable password) {
  502. if (password.length > 0) {
  503. KMFileAttribute *file = [[KMFileAttribute alloc] init];
  504. file.filePath = filePath;
  505. file.isLocked = YES;
  506. file.password = password;
  507. [weakSelf.files insertObject:file atIndex:weakSelf.insertRow];
  508. weakSelf.insertRow ++;
  509. }
  510. weakSelf.lockFileIndex++;
  511. // if ([self.lockFilePathArr count] > self.lockFileIndex) {
  512. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  513. [weakSelf openPasswordWindow];
  514. });
  515. // } else {
  516. [weakSelf.tableView reloadData];
  517. [weakSelf updateButtonState];
  518. // }
  519. }];
  520. }
  521. }
  522. - (void)comboBoxAction:(NSInteger)itemIndex atRow:(NSInteger)row
  523. {
  524. NSInteger index = row;
  525. // 数组不存在,或者索引超出数组长度
  526. if ((!_files) || (index > ((NSInteger)[_files count] - 1))) {
  527. return;
  528. }
  529. if (itemIndex == 0) {
  530. KMFileAttribute *currentFile = [_files objectAtIndex:index];
  531. [currentFile setBAllPage:YES];
  532. currentFile.pagesType = KMPDFSeleectPageType_AllPages;
  533. [self.window makeFirstResponder:self];
  534. } else if (itemIndex == 1) {
  535. KMFileAttribute *currentFile = [_files objectAtIndex:index];
  536. currentFile.pagesType = KMPDFSeleectPageType_OnlyOdd;
  537. [currentFile setBAllPage:NO];
  538. PDFDocument *tDocument = currentFile.myPDFDocument;
  539. if (tDocument == nil) {
  540. tDocument = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:currentFile.filePath]];
  541. }
  542. if ([currentFile isLocked]) {
  543. [tDocument unlockWithPassword:currentFile.password];
  544. }
  545. NSString *tPagesString = @"";
  546. for (NSInteger i = 0; i < tDocument.pageCount; i++) {
  547. if (i%2 == 0) {
  548. if (tPagesString.length == 0) {
  549. tPagesString = [tPagesString stringByAppendingString:[NSString stringWithFormat:@"%ld",i +1]];
  550. }else{
  551. tPagesString = [tPagesString stringByAppendingString:[NSString stringWithFormat:@",%ld",i +1]];
  552. }
  553. }
  554. }
  555. currentFile.pagesString = tPagesString;
  556. [self.window makeFirstResponder:self];
  557. } else if (itemIndex == 2) {
  558. KMFileAttribute *currentFile = [_files objectAtIndex:index];
  559. currentFile.pagesType = KMPDFSeleectPageType_OnlyEven;
  560. [currentFile setBAllPage:NO];
  561. NSString *tPagesString = @"";
  562. PDFDocument *tDocument = currentFile.myPDFDocument;
  563. if (tDocument == nil) {
  564. tDocument = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:currentFile.filePath]];
  565. }
  566. if ([currentFile isLocked]) {
  567. [tDocument unlockWithPassword:currentFile.password];
  568. }
  569. for (NSInteger i = 0; i <tDocument.pageCount; i++) {
  570. if (i%2) {
  571. if (tPagesString.length == 0) {
  572. tPagesString = [tPagesString stringByAppendingString:[NSString stringWithFormat:@"%ld",i +1]];
  573. }else{
  574. tPagesString = [tPagesString stringByAppendingString:[NSString stringWithFormat:@",%ld",i +1]];
  575. }
  576. }
  577. }
  578. currentFile.pagesString = tPagesString;
  579. [self.window makeFirstResponder:self];
  580. } else {
  581. KMFileAttribute *currentFile = [_files objectAtIndex:index];
  582. currentFile.pagesType = KMPDFSeleectPageType_PagesString;
  583. currentFile.pagesString = @"";
  584. [currentFile setBAllPage:NO];
  585. }
  586. }
  587. - (void)handerReDraw
  588. {
  589. //如果选择的是原始尺寸,不处理
  590. KMImageTitleButton *originalSizeButton = self.originalSizeButton.subviews.lastObject;
  591. if (originalSizeButton.state == NSControlStateValueOn) {
  592. }else{
  593. //重新选择了界面尺寸,那么就要重绘
  594. if (self.newPageSize.width < 0) {
  595. NSBeep();
  596. return;
  597. }else{
  598. NSMutableArray *pagesArray = [NSMutableArray array];
  599. NSInteger pageCount = self.PDFDocument.pageCount;
  600. for (NSInteger i = 0; i < pageCount; i ++) {
  601. [pagesArray addObject:[self.PDFDocument pageAtIndex:0]];
  602. [self.PDFDocument removePageAtIndex:0];
  603. }
  604. for (NSInteger i = 0; i < pageCount; i ++) {
  605. KMAppendPDFPage *page = [[KMAppendPDFPage alloc] init];
  606. [page setBounds:NSMakeRect(0, 0, self.newPageSize.width, self.newPageSize.height) forBox:kPDFDisplayBoxMediaBox];
  607. page.drawingPage = pagesArray[i];
  608. // [self.PDFDocument insertPageObject:page atIndex:i];
  609. [self.PDFDocument insertPage:page atIndex:i];
  610. }
  611. //如果是自定义大小,删除所有的outline,因为合并出来的outline是无效的
  612. if (self.PDFDocument.outlineRoot) {
  613. NSInteger childCount = [self.PDFDocument.outlineRoot numberOfChildren];
  614. NSMutableArray *PDFOutlineArray = [NSMutableArray array];
  615. for (NSInteger i = 0 ; i < childCount; i ++) {
  616. [PDFOutlineArray addObject:[self.PDFDocument.outlineRoot childAtIndex:i]];
  617. }
  618. for (PDFOutline *deleteOutline in PDFOutlineArray) {
  619. [deleteOutline removeFromParent];
  620. }
  621. }
  622. }
  623. }
  624. }
  625. - (BOOL)hasOpenFiles {
  626. NSArray *array = [KMTools getOpenDocumentURLs];
  627. if (array.count == 0) {
  628. return NO;
  629. }
  630. for (NSURL *fileURL in array) {
  631. if ([self isExistAtFilepath:fileURL.path]) {
  632. return YES;
  633. }
  634. }
  635. return NO;
  636. }
  637. #pragma mark - KMSelectPopButtonDelegate
  638. - (void)km_comboBoxSelectionDidChange:(KMDesignSelect *)obj {
  639. if ([self.addFileButton isEqual:obj]) {
  640. NSInteger index = self.addFileButton.indexOfSelectedItem;
  641. if (index < 0) {
  642. index = 0;
  643. }
  644. if (index == 0) {
  645. [self itemAddFileAction];
  646. } else if (index == 1) {
  647. [self itemAddFolderAction];
  648. } else if (index == 2) {
  649. [self itemAddOpenFileAction];
  650. }
  651. }
  652. }
  653. - (void)km_controlTextDidChange:(KMDesignSelect *)obj {
  654. }
  655. - (void)km_controlTextDidEndEditing:(KMDesignSelect *)obj {
  656. }
  657. - (void)km_SelectPopoverWillShow:(KMDesignSelect *)obj {
  658. if ([self hasOpenFiles]) {
  659. obj.disItems = @[];
  660. } else {
  661. obj.disItems = @[NSLocalizedString(@"Add Open Files", "")];
  662. }
  663. }
  664. #pragma mark tabelView
  665. - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
  666. {
  667. return _files.count;
  668. }
  669. - (nullable id)tableView:(NSTableView *)tableView viewForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row
  670. {
  671. KMFileAttribute *fileAttribute = [_files objectAtIndex:row];
  672. PDFDocument *pdf = fileAttribute.myPDFDocument;
  673. if (pdf == nil) {
  674. pdf = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:fileAttribute.filePath]];
  675. }
  676. if ([pdf isLocked]) {
  677. [pdf unlockWithPassword:fileAttribute.password];
  678. }
  679. if (tableColumn.identifier == KMTableColumnPageRangeID) {
  680. KMPDFMergePageRangeTabelViewCell *cell = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
  681. if (cell == nil) {
  682. cell = [[KMPDFMergePageRangeTabelViewCell alloc] init];
  683. }
  684. cell.wantsLayer = YES;
  685. cell.layer.backgroundColor = [NSColor clearColor].CGColor;
  686. cell.pageCount = pdf.pageCount;
  687. [cell updatePageRange:fileAttribute.pagesType pageStrings:fileAttribute.pagesString isFirstResponser:NO];
  688. // if (fileAttribute.pageRangeError) {
  689. // [cell updateRageRangeState:10];
  690. // }
  691. __weak typeof(self) weakSelf = self;
  692. cell.callback = ^(NSInteger index) {
  693. [weakSelf comboBoxAction:index atRow:row];
  694. };
  695. cell.textDidChange = ^(NSString * _Nonnull string) {
  696. if (row >= weakSelf.files.count) {
  697. return;
  698. }
  699. KMFileAttribute *file = [weakSelf.files objectAtIndex:row];
  700. [file setPagesString:string];
  701. // file.pageRangeError = NO;
  702. // dispatch_async(dispatch_get_main_queue(), ^{
  703. // [weakSelf.tableView reloadData];
  704. // });
  705. };
  706. cell.textDidEndEdit = ^(NSString * _Nonnull string, KMPDFMergePageRangeTabelViewCell *pageRangeCell) {
  707. if (row >= weakSelf.files.count) {
  708. return;
  709. }
  710. KMFileAttribute *file = [weakSelf.files objectAtIndex:row];
  711. if (!file.selectPages) {
  712. // file.pageRangeError = YES;
  713. if (weakSelf.alert) {
  714. return;
  715. }
  716. dispatch_async(dispatch_get_main_queue(), ^{
  717. // [weakSelf.tableView reloadData];
  718. NSAlert *alert = [[NSAlert alloc] init];
  719. // weakSelf.alert = alert;
  720. [alert setAlertStyle:NSAlertStyleCritical];
  721. [alert setMessageText:[NSString stringWithFormat:@"%@ %@",[file.filePath lastPathComponent],NSLocalizedString(@"Invalid page range or the page number is out of range. Please try again.", nil)]];
  722. [alert runModal];
  723. file.pagesString = @"";
  724. [pageRangeCell updatePageRange:file.pagesType pageStrings:file.pagesString isFirstResponser:NO];
  725. // dispatch_async(dispatch_get_main_queue(), ^{
  726. // weakSelf.alert = nil;
  727. // });
  728. // [weakSelf.tableView reloadData];
  729. // [weakSelf.tableView reloadDataForRowIndexes:[NSIndexSet indexSetWithIndex:row] columnIndexes:[NSIndexSet indexSetWithIndex:1]];
  730. });
  731. }
  732. };
  733. return cell;
  734. }
  735. if (tableColumn.identifier == KMTableColumnSizeID) {
  736. KMPDFMergeSizeTabelViewCell *cell = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
  737. if (cell == nil) {
  738. cell = [[KMPDFMergeSizeTabelViewCell alloc] init];
  739. }
  740. NSString *filePath = pdf.documentURL.path;
  741. if (!filePath) {
  742. filePath = fileAttribute.filePath;
  743. }
  744. NSDictionary *attributesDic = [[NSFileManager defaultManager]attributesOfItemAtPath:filePath error:nil];
  745. float size = [[attributesDic objectForKey:NSFileSize] floatValue]/1024;
  746. cell.label.stringValue = [NSString stringWithFormat:@"%0.1f %c", truesize(size) , moniker(size)];
  747. cell.button.hidden = YES;
  748. __weak typeof(self) weakSelf = self;
  749. cell.callback = ^{
  750. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:fileAttribute.filePath error:nil];
  751. CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
  752. weakSelf.allFileSize = weakSelf.allFileSize - fileSize;
  753. [weakSelf.files removeObject:fileAttribute];
  754. [weakSelf.tableView reloadData];
  755. [weakSelf updateButtonState];
  756. };
  757. return cell;
  758. }
  759. KMPDFMergeFileNameTabelViewCell *cell = [tableView makeViewWithIdentifier:@"fileName" owner:self];
  760. if (cell == nil) {
  761. cell = [[KMPDFMergeFileNameTabelViewCell alloc] init];
  762. }
  763. cell.numberLabel.stringValue = [NSString stringWithFormat:@"%ld",(row + 1)];;
  764. cell.fileNameLabel.stringValue = [fileAttribute.filePath lastPathComponent];
  765. cell.pageNumberLabel.stringValue = [NSString stringWithFormat:@"%ld %@",pdf.pageCount,NSLocalizedString(@"Pages", nil)];;
  766. // cell.iconImageView.image = [[pdf pageAtIndex:0] thumbnailWithSize:60*2 forBox:kPDFDisplayBoxCropBox];
  767. if (pdf.documentURL) {
  768. cell.iconImageView.image = [NSImage previewForFileWithPath:pdf.documentURL ofSize:NSMakeSize(120, 120) asIcon:YES];
  769. } else if (fileAttribute.filePath) {
  770. // if ([fileAttribute.filePath.pathExtension.lowercaseString isEqualToString:@"pdf"]) {
  771. cell.iconImageView.image = [NSImage previewForFileWithPath:[NSURL fileURLWithPath:fileAttribute.filePath] ofSize:NSMakeSize(120, 120) asIcon:YES];
  772. // } else {
  773. // cell.iconImageView.image = [NSImage previewForFileWithPath:fileAttribute.filePath ofSize:NSMakeSize(120, 120) asIcon:YES];
  774. // }
  775. } else {
  776. cell.iconImageView.image = nil;
  777. }
  778. return cell;
  779. }
  780. - (BOOL)tableView:(NSTableView *)tableView shouldSelectTableColumn:(NSTableColumn *)tableColumn
  781. {
  782. return NO;
  783. }
  784. #pragma mark -NSTableViewDelegate
  785. - (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard
  786. {
  787. // Copy the row numbers to the pasteboard.
  788. NSData *zNSIndexSetData = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
  789. [pboard declareTypes:[NSArray arrayWithObject:MyTableCellViewDataType] owner:self];
  790. [pboard setData:zNSIndexSetData forType:MyTableCellViewDataType];
  791. return YES;
  792. }
  793. - (NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id <NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)dropOperation
  794. {
  795. if (dropOperation == NSTableViewDropOn) {
  796. return NSDragOperationNone;
  797. }
  798. BOOL isCanDrag = NO;
  799. NSDragOperation result = NSDragOperationNone;
  800. NSPasteboard *pboard = [info draggingPasteboard];
  801. if ([pboard availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]])
  802. {
  803. NSArray *fileNames = [pboard propertyListForType:NSFilenamesPboardType];
  804. for (NSString* path in fileNames) {
  805. if ([[KMImageAccessoryController supportedImageTypes] containsObject:path.pathExtension.lowercaseString]) {
  806. isCanDrag = YES;
  807. }else{
  808. isCanDrag = NO;
  809. break;
  810. }
  811. }
  812. }else if ([pboard availableTypeFromArray:[NSArray arrayWithObject:MyTableCellViewDataType]]){
  813. result = NSDragOperationEvery;
  814. }
  815. if (isCanDrag) {
  816. result = NSDragOperationCopy;
  817. }
  818. return result;
  819. }
  820. - (BOOL)tableView:(NSTableView *)tableView acceptDrop:(id <NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)dropOperation
  821. {
  822. BOOL result = NO;
  823. NSPasteboard *pboard = [info draggingPasteboard];
  824. _insertRow = row;
  825. if ([pboard availableTypeFromArray:[NSArray arrayWithObject:NSFilenamesPboardType]])
  826. {
  827. NSMutableArray *fileNames = [pboard propertyListForType:NSFilenamesPboardType];
  828. NSMutableArray *array = [NSMutableArray array];
  829. for(NSString *path in fileNames){
  830. if (![self isExistAtFilepath:path]) {
  831. continue;
  832. }
  833. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
  834. CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
  835. self.allFileSize = self.allFileSize + fileSize;
  836. [array addObject:path];
  837. }
  838. [self addFiles:array];
  839. result = YES;
  840. } else if ([pboard availableTypeFromArray:[NSArray arrayWithObject:MyTableCellViewDataType]]){
  841. NSData* rowData = [pboard dataForType:MyTableCellViewDataType];
  842. NSIndexSet* rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];
  843. NSMutableArray *moveArray = [[NSMutableArray alloc] init];
  844. NSMutableArray *allPhoto = [_files mutableCopy];
  845. NSInteger index = 0;
  846. [rowIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
  847. KMFileAttribute * file = allPhoto[idx];
  848. [moveArray addObject:file];
  849. [_files removeObject:file];
  850. }];
  851. if (row > 0) {
  852. KMFileAttribute * indexFile = allPhoto[row -1];
  853. while([moveArray containsObject:indexFile]){
  854. row --;
  855. if ((row < 0)) {
  856. indexFile = nil;
  857. break;
  858. } else {
  859. indexFile = allPhoto[row];
  860. }
  861. }
  862. if(indexFile){
  863. index = [_files indexOfObject:indexFile]+1;
  864. }
  865. }
  866. for (NSInteger i = 0; i< moveArray.count; i++) {
  867. [_files insertObject:moveArray[i] atIndex:(index +i)];
  868. }
  869. [_tableView reloadData];
  870. result = YES;
  871. // [moveArray release];
  872. // [allPhoto release];
  873. } else {
  874. result = NO;
  875. }
  876. return result;
  877. }
  878. - (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row {
  879. KMBatchTableRowView *rowView = [[KMBatchTableRowView alloc] init];
  880. rowView.backgroundView.layer.backgroundColor = [NSColor colorWithRed:237/255.f green:238/255.f blue:240/255.f alpha:1.f].CGColor;
  881. rowView.backgroundView.layer.cornerRadius = 4.f;
  882. rowView.selectionInset = NSEdgeInsetsMake(0, 16, 0, 16);
  883. rowView.selectionRadius = 4.f;
  884. rowView.selectionBackgroundColorBlock = ^NSColor * _Nonnull{
  885. return [NSColor colorWithRed:206/255.f green:208/255.f blue:212/255.f alpha:0.6f];
  886. };
  887. return rowView;
  888. }
  889. - (void)tableViewSelectionDidChange:(NSNotification *)notification {
  890. }
  891. #pragma mark Buutton Action
  892. - (IBAction)buttonItemClicked_Cancel:(id)sender
  893. {
  894. _isSuccessfully = NO;
  895. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  896. [[self window] orderOut:self];
  897. }
  898. - (IBAction)buttonItemClicked_Append:(id)sender
  899. {
  900. if (!self.canMerge) {
  901. return;
  902. }
  903. KMImageTitleButton *customSizeButton = self.customSizeButton.subviews.lastObject;
  904. if (customSizeButton.state == NSControlStateValueOn) {
  905. NSInteger widthFloat = [self.pageSizeWidthTextField.stringValue integerValue];
  906. NSInteger heightFloat = [self.pageSizeHeightTextField.stringValue integerValue];
  907. NSString *widthString = [NSString stringWithFormat:@"%ld",widthFloat];
  908. NSString *heightString = [NSString stringWithFormat:@"%ld",heightFloat];
  909. if (![widthString isEqualToString:self.pageSizeWidthTextField.stringValue] ||![heightString isEqualToString:self.pageSizeHeightTextField.stringValue]||widthFloat <= 0 ||heightFloat <= 0 ) {
  910. NSBeep();
  911. return;
  912. }else{
  913. self.newPageSize = CGSizeMake(widthFloat, heightFloat);
  914. }
  915. }
  916. //初始化数组
  917. if (!self.rootPDFOutlineArray) {
  918. self.rootPDFOutlineArray = [NSMutableArray new];
  919. }else{
  920. [self.rootPDFOutlineArray removeAllObjects];
  921. }
  922. //如果是合并,文件路径不存在,或者文件个数小于等于1 return
  923. if (_editType == KMPDFPageEditMerge) {
  924. if (_files.count <= 1 ) {
  925. NSAlert *alert = [[NSAlert alloc] init];
  926. [alert setAlertStyle:NSAlertStyleCritical];
  927. [alert setMessageText:NSLocalizedString(@"To start merging, please select at least 2 files.", nil)];
  928. [alert runModal];
  929. return;
  930. }
  931. }
  932. _isSuccessfully = NO;
  933. [self.nCancelVC setEnabled:NO];
  934. self.canMerge = NO;
  935. BOOL allPage = YES;//只有是全部才支持大纲的合并
  936. for (KMFileAttribute *file in _files) {
  937. if (!file.selectPages) {
  938. dispatch_async(dispatch_get_main_queue(), ^{
  939. NSAlert *alert = [[NSAlert alloc] init];
  940. [alert setAlertStyle:NSAlertStyleCritical];
  941. [alert setMessageText:[NSString stringWithFormat:@"%@ %@",[file.filePath lastPathComponent],NSLocalizedString(@"Invalid page range or the page number is out of range. Please try again.", nil)]];
  942. [alert runModal];
  943. [self.nCancelVC setEnabled:YES];
  944. self.canMerge = YES;
  945. });
  946. return;
  947. }
  948. }
  949. [_progress setHidden:NO];
  950. [_progress startAnimation:nil];
  951. self.loading_backgroundView.hidden = NO;
  952. for(KMFileAttribute *file in _files){
  953. //只要有一个文件不是全部page,那么就不合并大纲
  954. if (allPage == YES) {
  955. allPage = file.bAllPage;
  956. }
  957. //通过路径获取文件
  958. PDFDocument *tdocument = file.myPDFDocument;
  959. if (tdocument == nil) {
  960. tdocument = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:[file filePath]]];
  961. file.myPDFDocument = tdocument;
  962. }
  963. //所有的outline数组
  964. NSMutableArray *PDFOutlineArr = [NSMutableArray new];
  965. //需要解锁
  966. if (file.isLocked) {
  967. [tdocument unlockWithPassword:file.password];
  968. }
  969. //如果根存在,那么遍历出所有的outline,添加到数组中,如果不存在,那么就创建一个根
  970. if (tdocument.outlineRoot) {
  971. [self.rootPDFOutlineArray addObject:tdocument.outlineRoot];
  972. [self fetchAllOfChildren:tdocument.outlineRoot containerArray:PDFOutlineArr];
  973. [PDFOutlineArr removeObject:tdocument.outlineRoot];
  974. } else {
  975. PDFOutline *rootOutline = [[PDFOutline alloc] init];
  976. tdocument.outlineRoot = rootOutline;
  977. if (tdocument.outlineRoot) {
  978. [self.rootPDFOutlineArray addObject:tdocument.outlineRoot];
  979. }
  980. }
  981. // for (CPDFOutline *tOutline in PDFOutlineArr) {
  982. //这段代码主要是调用他的getter方法;outline显示正确
  983. // [tOutline.destination.page dataRepresentation];
  984. // }
  985. for (NSNumber *number in file.selectPages) {
  986. PDFPage *page = [tdocument pageAtIndex:[number integerValue]-1];
  987. [self.PDFDocument insertPage:page atIndex:[self.PDFDocument pageCount]];
  988. // [self.PDFDocument insertPageObject:page atIndex:self.PDFDocument.pageCount];
  989. [self.insertIndexSet addIndex:(self.PDFDocument.pageCount - 1)];
  990. }
  991. _isSuccessfully = YES;
  992. }
  993. KMFileAttribute *file = self.files.firstObject;
  994. NSString *fileName = file.filePath.stringByDeletingPathExtension.lastPathComponent;
  995. fileName = [NSString stringWithFormat:@"%@_Merged", fileName];
  996. if (_editType == KMPDFPageEditMerge) {
  997. _isSuccessfully = NO;
  998. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  999. PDFOutline *rootline = [[PDFOutline alloc] init];
  1000. self.PDFDocument.outlineRoot = rootline;
  1001. if (allPage) {
  1002. NSInteger insertIndex = 0;
  1003. for (NSUInteger i = 0; i < self.rootPDFOutlineArray.count; i++) {
  1004. PDFOutline *rootPDFOutline = self->_rootPDFOutlineArray[i];
  1005. for (NSUInteger j = 0; j < rootPDFOutline.numberOfChildren; j ++) {
  1006. [self.PDFDocument.outlineRoot insertChild:[rootPDFOutline childAtIndex:j] atIndex: insertIndex];
  1007. insertIndex ++;
  1008. }
  1009. }
  1010. }
  1011. dispatch_async(dispatch_get_main_queue(), ^{
  1012. // [self handerReDraw];
  1013. KMSavePanelAccessoryController *savePanelAccessoryViewController = [[KMSavePanelAccessoryController alloc] init];
  1014. NSSavePanel *savePanel = [NSSavePanel savePanel];
  1015. savePanel.nameFieldStringValue = fileName;
  1016. savePanel.allowedFileTypes = @[@"pdf"];
  1017. savePanel.accessoryView = savePanelAccessoryViewController.view;
  1018. self.savePanelAccessoryViewController = savePanelAccessoryViewController;
  1019. [savePanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
  1020. if (result) {
  1021. NSString * outputSavePanel = savePanel.URL.path;
  1022. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1023. self->_isSuccessfully = [self.PDFDocument writeToFile:outputSavePanel];
  1024. if (!self->_isSuccessfully) {
  1025. self->_isSuccessfully = [self.PDFDocument.dataRepresentation writeToFile:outputSavePanel atomically:YES];
  1026. }
  1027. dispatch_async(dispatch_get_main_queue(), ^{
  1028. [self->_progress setHidden:YES];
  1029. [self.nCancelVC setEnabled:YES];
  1030. self.canMerge = YES;
  1031. self.loading_backgroundView.hidden = YES;
  1032. if (self->_isSuccessfully) {
  1033. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  1034. [[self window] orderOut:self];
  1035. if (self.saveAsPDFFilePath) {
  1036. BOOL autoOpen = self.savePanelAccessoryViewController.needOpen;
  1037. if (autoOpen) {
  1038. self.saveAsPDFFilePath(outputSavePanel);
  1039. self.saveAsPDFFilePath = nil;
  1040. } else {
  1041. [self viewFileAtFinder:outputSavePanel];
  1042. }
  1043. } else {
  1044. BOOL autoOpen = self.savePanelAccessoryViewController.needOpen;
  1045. if (autoOpen) {
  1046. [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:[NSURL fileURLWithPath:outputSavePanel] display:YES completionHandler:^(NSDocument * _Nullable document, BOOL documentWasAlreadyOpen, NSError * _Nullable error) {
  1047. }];
  1048. } else {
  1049. [self viewFileAtFinder:outputSavePanel];
  1050. }
  1051. }
  1052. } else {
  1053. NSAlert *alert = [[NSAlert alloc] init];
  1054. [alert setAlertStyle:NSAlertStyleCritical];
  1055. [alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Failed to merge!", nil)]];
  1056. NSModalResponse response = [alert runModal];
  1057. if (response == 0 ) {
  1058. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  1059. [[self window] orderOut:self];
  1060. }
  1061. }
  1062. });
  1063. });
  1064. } else {
  1065. [self->_progress setHidden:YES];
  1066. [self.nCancelVC setEnabled:YES];
  1067. self.canMerge = YES;
  1068. self.loading_backgroundView.hidden = YES;
  1069. }
  1070. }];
  1071. });
  1072. });
  1073. } else {
  1074. [self.nCancelVC setEnabled:YES];
  1075. self.canMerge = YES;
  1076. if (allPage) {
  1077. if (!self.PDFDocument.outlineRoot) {
  1078. PDFOutline *outline = [[PDFOutline alloc] init];
  1079. self.PDFDocument.outlineRoot = outline;
  1080. }
  1081. for (NSInteger i = 0; i < self.rootPDFOutlineArray.count; i++) {
  1082. PDFOutline *rootPDFOutline = _rootPDFOutlineArray[i];
  1083. for (NSInteger j = 0; j < rootPDFOutline.numberOfChildren; j ++) {
  1084. [self.PDFDocument.outlineRoot insertChild:[rootPDFOutline childAtIndex:j] atIndex:self.PDFDocument.outlineRoot.numberOfChildren];
  1085. }
  1086. }
  1087. }
  1088. if (_isSuccessfully) {
  1089. // [self handerReDraw];
  1090. KMSavePanelAccessoryController *savePanelAccessoryViewController = [[KMSavePanelAccessoryController alloc] init];
  1091. NSSavePanel *savePanel = [NSSavePanel savePanel];
  1092. savePanel.nameFieldStringValue = fileName;
  1093. savePanel.allowedFileTypes = @[@"pdf"];
  1094. savePanel.accessoryView = savePanelAccessoryViewController.view;
  1095. self.savePanelAccessoryViewController = savePanelAccessoryViewController;
  1096. [savePanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
  1097. if (result) {
  1098. NSString * outputSavePanel = savePanel.URL.path;
  1099. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  1100. self->_isSuccessfully = [self.PDFDocument writeToFile:outputSavePanel];
  1101. if (!self->_isSuccessfully) {
  1102. self->_isSuccessfully = [self.PDFDocument.dataRepresentation writeToFile:outputSavePanel atomically:YES];
  1103. }
  1104. dispatch_async(dispatch_get_main_queue(), ^{
  1105. [self->_progress setHidden:YES];
  1106. [self.nCancelVC setEnabled:YES];
  1107. self.canMerge = YES;
  1108. self.loading_backgroundView.hidden = YES;
  1109. if (self->_isSuccessfully) {
  1110. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  1111. [[self window] orderOut:self];
  1112. if (self.saveAsPDFFilePath) {
  1113. BOOL autoOpen = self.savePanelAccessoryViewController.needOpen;
  1114. if (autoOpen) {
  1115. self.saveAsPDFFilePath(outputSavePanel);
  1116. self.saveAsPDFFilePath = nil;
  1117. } else {
  1118. [self viewFileAtFinder:outputSavePanel];
  1119. }
  1120. } else {
  1121. BOOL autoOpen = self.savePanelAccessoryViewController.needOpen;
  1122. if (autoOpen) {
  1123. [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:[NSURL fileURLWithPath:outputSavePanel] display:YES completionHandler:^(NSDocument * _Nullable document, BOOL documentWasAlreadyOpen, NSError * _Nullable error) {
  1124. }];
  1125. } else {
  1126. [self viewFileAtFinder:outputSavePanel];
  1127. }
  1128. }
  1129. } else {
  1130. NSAlert *alert = [[NSAlert alloc] init];
  1131. [alert setAlertStyle:NSAlertStyleCritical];
  1132. [alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Failed to merge!", nil)]];
  1133. NSModalResponse response = [alert runModal];
  1134. if (response == 0 ) {
  1135. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  1136. [[self window] orderOut:self];
  1137. }
  1138. }
  1139. });
  1140. });
  1141. } else {
  1142. [self->_progress setHidden:YES];
  1143. [self.nCancelVC setEnabled:YES];
  1144. self.canMerge = YES;
  1145. self.loading_backgroundView.hidden = YES;
  1146. }
  1147. }];
  1148. } else {
  1149. NSAlert *alert = [[NSAlert alloc] init];
  1150. [alert setAlertStyle:NSAlertStyleCritical];
  1151. [alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Failed to merge!", nil)]];
  1152. NSModalResponse response = [alert runModal];
  1153. if (response == 0 ) {
  1154. [NSApp endSheet:[self window] returnCode:[(NSView *)sender tag]];
  1155. [[self window] orderOut:self];
  1156. }
  1157. }
  1158. }
  1159. }
  1160. - (IBAction)buttonItemClicked_Clear:(id)sender
  1161. {
  1162. [_files removeAllObjects];
  1163. [self updateButtonState];
  1164. [self.tableView reloadData];
  1165. self.allFileSize = 0;
  1166. }
  1167. - (IBAction)buttonItemClicked_AddFile:(id)sender
  1168. {
  1169. NSMenu *menu = [[NSMenu alloc] init];
  1170. NSMenuItem *addFileItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"add files", "") action:@selector(itemAddFileAction) keyEquivalent:@""];
  1171. addFileItem.target = self;
  1172. [menu addItem:addFileItem];
  1173. NSMenuItem *addFolderitem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"add Folder", "") action:@selector(itemAddFolderAction) keyEquivalent:@""];
  1174. addFolderitem.target = self;
  1175. [menu addItem:addFolderitem];
  1176. NSMenuItem *addOpenFileitem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"add Open Files", "") action:@selector(itemAddOpenFileAction) keyEquivalent:@""];
  1177. addOpenFileitem.target = self;
  1178. [menu addItem:addOpenFileitem];
  1179. [menu popUpMenuPositioningItem:nil atLocation:NSMakePoint(100, 10) inView:sender];
  1180. }
  1181. - (void)itemAddFileAction {
  1182. NSOpenPanel *openPanel = [[NSOpenPanel alloc] init];
  1183. [openPanel setAllowedFileTypes:[KMImageAccessoryController supportedImageTypes]];
  1184. [openPanel setAllowsMultipleSelection:YES];
  1185. openPanel.message = NSLocalizedString(@"Press ⌘/⇧ to select multiple files", nil);
  1186. [openPanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result){
  1187. if (result == NSModalResponseOK) {
  1188. NSMutableArray *array = [NSMutableArray array];
  1189. for (NSURL *fileURL in [openPanel URLs]) {
  1190. if (fileURL) {
  1191. if (![self isExistAtFilepath:fileURL.path]) {
  1192. continue;
  1193. }
  1194. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil];
  1195. CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
  1196. self.allFileSize = self.allFileSize + fileSize;
  1197. [array addObject:fileURL.path];
  1198. }
  1199. }
  1200. [self addFiles:array];
  1201. }
  1202. }];
  1203. }
  1204. - (void)itemAddFolderAction {
  1205. NSOpenPanel *openPanel = [[NSOpenPanel alloc] init];
  1206. [openPanel setAllowedFileTypes:[KMImageAccessoryController supportedImageTypes]];
  1207. [openPanel setAllowsMultipleSelection:YES];
  1208. openPanel.canChooseFiles = NO;
  1209. openPanel.canChooseDirectories = YES;
  1210. openPanel.message = NSLocalizedString(@"Press ⌘/⇧ to select multiple files", nil);
  1211. [openPanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result){
  1212. if (result == NSModalResponseOK) {
  1213. NSMutableArray *result = [NSMutableArray array];
  1214. for (NSURL *url in openPanel.URLs) {
  1215. [self findAllFiles:url result:result];
  1216. }
  1217. NSMutableArray *array = [NSMutableArray array];
  1218. for (NSURL *fileURL in result) {
  1219. if (fileURL) {
  1220. if (![self isExistAtFilepath:fileURL.path]) {
  1221. continue;
  1222. }
  1223. NSDictionary *attrib = [[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil];
  1224. CGFloat fileSize = [[attrib objectForKey:NSFileSize] floatValue];
  1225. self.allFileSize = self.allFileSize + fileSize;
  1226. [array addObject:fileURL.path];
  1227. }
  1228. }
  1229. [self addFiles:array];
  1230. }
  1231. }];
  1232. }
  1233. - (void)itemAddOpenFileAction {
  1234. [_lockFilePathArr removeAllObjects];
  1235. _lockFileIndex = 0;
  1236. for (NSWindow *window in NSApp.windows) {
  1237. NSWindowController *controller = [window windowController];
  1238. if (![controller isKindOfClass:[CTBrowserWindowController class]]) {
  1239. continue;
  1240. }
  1241. CTTabStripModel *model = ((CTBrowserWindowController *)controller).browser.tabStripModel;
  1242. for (int i = 0; i < model.count; i++) {
  1243. KMMainDocument *document = (KMMainDocument *)[model tabContentsAtIndex:i];
  1244. // if (document.windowControllers.count == 0) {
  1245. // break;
  1246. // }
  1247. if (document.isHome) {
  1248. continue;
  1249. }
  1250. KMMainViewController *mainViewController = document.mainViewController;
  1251. if ([self.PDFDocument.documentURL.path isEqualToString:document.fileURL.path]) {
  1252. continue;
  1253. }
  1254. if (![self isExistAtFilepath:document.fileURL.path]) {
  1255. continue;
  1256. }
  1257. CPDFDocument *pdfDocument = mainViewController.listView.document;
  1258. if (pdfDocument.isLocked || (!pdfDocument.allowsCopying || !pdfDocument.allowsPrinting)) {
  1259. [_lockFilePathArr addObject:pdfDocument.documentURL.path];
  1260. } else {
  1261. KMFileAttribute *file = [[KMFileAttribute alloc] init];
  1262. file.filePath = document.fileURL.path;
  1263. file.pdfDocument = mainViewController.document;
  1264. [_files addObject:file];
  1265. }
  1266. }
  1267. }
  1268. [self updateButtonState];
  1269. [_tableView reloadData];
  1270. if ([_lockFilePathArr count]) {
  1271. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  1272. if (self.files.count == 0) {
  1273. self.insertRow = 0;
  1274. } else {
  1275. self.insertRow = self.files.count;
  1276. }
  1277. [self openPasswordWindow];
  1278. });
  1279. }
  1280. }
  1281. - (void)findAllFiles:(NSURL *)folderURL result:(NSMutableArray *)result {
  1282. NSFileManager *fm = [NSFileManager defaultManager];
  1283. BOOL isDirectory = NO;
  1284. [fm fileExistsAtPath:folderURL.path isDirectory:&isDirectory];
  1285. if (!isDirectory) {
  1286. return;
  1287. }
  1288. NSArray *contents = [fm contentsOfDirectoryAtURL:folderURL includingPropertiesForKeys:nil options:0 error:nil];
  1289. if (contents.count == 0) {
  1290. return;
  1291. }
  1292. NSArray *array = @[@"jpg",@"cur",@"bmp",@"jpeg",@"gif",@"png",@"tiff",@"tif",/*@"pic",*/@"ico",@"icns",@"tga",@"psd",@"eps",@"hdr",@"jp2",@"jpc",@"pict",@"sgi",@"heic",@"pdf"];
  1293. for (NSURL *documentURL in contents) {
  1294. BOOL isDirectory = NO;
  1295. [fm fileExistsAtPath:documentURL.path isDirectory:&isDirectory];
  1296. if (isDirectory) {
  1297. [self findAllFiles:documentURL result:result];
  1298. } else {
  1299. if (![array containsObject:[documentURL.pathExtension lowercaseString]]) {
  1300. continue;
  1301. }
  1302. [result addObject:documentURL];
  1303. }
  1304. }
  1305. }
  1306. - (IBAction)buttonClicked_ChooseSize:(KMImageTitleButton *)sender {
  1307. int i = 0;
  1308. for (KMImageTitleButton *button in @[self.originalSizeButton, self.A4SizeButton, self.A3SizeButton,
  1309. self.USLetterSizeButton, self.USLegalButton, self.customSizeButton]) {
  1310. KMImageTitleButton *radio = button.subviews.lastObject;
  1311. radio.state = NSControlStateValueOff;
  1312. radio.imageName = @"btn_radio_unsel_on";
  1313. i += 1;
  1314. }
  1315. sender.state = NSControlStateValueOn;
  1316. sender.imageName = @"btn_radio_sel_on";
  1317. KMImageTitleButton *customSizeButton = self.customSizeButton.subviews.lastObject;
  1318. self.pageSizeHeightTextField.enabled = self.pageSizeWidthTextField.enabled = [sender isEqual:customSizeButton];
  1319. switch (sender.contentButton.tag) {
  1320. case 0:
  1321. break;
  1322. case 1:
  1323. self.newPageSize = CGSizeMake(595, 841);
  1324. break;
  1325. case 2:
  1326. self.newPageSize = CGSizeMake(841, 1190);
  1327. break;
  1328. case 3:
  1329. self.newPageSize = CGSizeMake(612, 792);
  1330. break;
  1331. case 4:
  1332. self.newPageSize = CGSizeMake(612, 1108);
  1333. break;
  1334. case 5:
  1335. self.newPageSize = CGSizeMake(595, 841);
  1336. self.pageSizeWidthTextField.stringValue = [NSString stringWithFormat:@"%.0f",self.newPageSize.width];
  1337. self.pageSizeHeightTextField.stringValue = [NSString stringWithFormat:@"%.0f",self.newPageSize.height];
  1338. break;
  1339. default:
  1340. break;
  1341. }
  1342. }
  1343. #pragma mark - Private Method
  1344. //获取所有的子节点
  1345. - (void)fetchAllOfChildren:(PDFOutline *)aOutline containerArray:(NSMutableArray *)aMArray
  1346. {
  1347. if (![aMArray containsObject:aOutline]) {
  1348. [aMArray addObject:aOutline];
  1349. }
  1350. for (NSInteger i = 0; i < aOutline.numberOfChildren; i++) {
  1351. [aMArray addObject:[aOutline childAtIndex:i]];
  1352. [self fetchAllOfChildren:[aOutline childAtIndex:i] containerArray:aMArray];
  1353. }
  1354. }
  1355. @end