PasswordWindowController.m 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // PasswordWindowController.m
  3. // PDF Reader
  4. //
  5. // Created by wangshuai on 13-12-3.
  6. // Copyright (c) 2013年 zhangjie. All rights reserved.
  7. //
  8. #import "PasswordWindowController.h"
  9. #import <Quartz/Quartz.h>
  10. @interface PasswordWindowController ()
  11. @property (nonatomic,retain) NSString *password;
  12. @end
  13. @implementation PasswordWindowController
  14. @synthesize fileURL = _fileURL;
  15. @synthesize passwordDelegate = _passwordDelegate;
  16. @synthesize tag = _tag;
  17. - (id)initWithWindow:(NSWindow *)window
  18. {
  19. self = [super initWithWindow:window];
  20. if (self) {
  21. // Initialization code here.
  22. }
  23. return self;
  24. }
  25. - (void)windowDidLoad
  26. {
  27. [super windowDidLoad];
  28. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  29. _titleTextField.stringValue = [_fileURL lastPathComponent]?:@"";
  30. _textField.stringValue = NSLocalizedString(@"This PDF is password protected. Please enter the password below to access this PDF.", nil);
  31. _openButton.title = NSLocalizedString(@"Open", nil);
  32. _cancelButton.title = NSLocalizedString(@"Cancel", nil);
  33. }
  34. - (void)dealloc
  35. {
  36. _passwordDelegate = nil;
  37. }
  38. - (void)close
  39. {
  40. // [super close];
  41. [NSApp endSheet:self.window];
  42. [[self window] orderOut:self];
  43. }
  44. - (IBAction)cancelAction:(id)sender
  45. {
  46. [self close];
  47. // if ([_passwordDelegate respondsToSelector:@selector(didCancelUnlockFile:)]) {
  48. // [_passwordDelegate didCancelUnlockFile:self];
  49. // }
  50. }
  51. - (IBAction)OKAction:(id)sender
  52. {
  53. PDFDocument *pdfDoc = [[PDFDocument alloc] initWithURL:_fileURL];
  54. if (pdfDoc) {
  55. if ([pdfDoc unlockWithPassword:[_passwordTextField stringValue]]) {
  56. self.password = [_passwordTextField stringValue];
  57. [self close];
  58. }else{
  59. NSAlert *alert = [[NSAlert alloc] init];
  60. [alert setAlertStyle:NSAlertStyleCritical];
  61. [alert setMessageText:NSLocalizedString(@"Incorrect password. Please check your password and try again.", nil)];
  62. [alert runModal];
  63. }
  64. }
  65. }
  66. #pragma mark Show Methods
  67. - (void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
  68. if (contextInfo != NULL) {
  69. void (^handler)(NSString *password) = (void(^)(NSString *password))CFBridgingRelease(contextInfo);
  70. handler(_password);
  71. }
  72. }
  73. - (void)beginSheetModalForWindow:(NSWindow *)window completionHandler:(void (^)(NSString *password))handler{
  74. // [self retain]; // make sure we stay around long enough
  75. [NSApp beginSheet:[self window]
  76. modalForWindow:window
  77. modalDelegate:self
  78. didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
  79. contextInfo:handler ? CFBridgingRetain(handler) : NULL];
  80. }
  81. @end