1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- //
- // PasswordWindowController.m
- // PDF Reader
- //
- // Created by wangshuai on 13-12-3.
- // Copyright (c) 2013年 zhangjie. All rights reserved.
- //
- #import "PasswordWindowController.h"
- #import <Quartz/Quartz.h>
- @interface PasswordWindowController ()
- @property (nonatomic,retain) NSString *password;
- @end
- @implementation PasswordWindowController
- @synthesize fileURL = _fileURL;
- @synthesize passwordDelegate = _passwordDelegate;
- @synthesize tag = _tag;
- - (id)initWithWindow:(NSWindow *)window
- {
- self = [super initWithWindow:window];
- if (self) {
- // Initialization code here.
- }
- return self;
- }
- - (void)windowDidLoad
- {
- [super windowDidLoad];
-
- // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
- _titleTextField.stringValue = [_fileURL lastPathComponent]?:@"";
- _textField.stringValue = NSLocalizedString(@"This PDF is password protected. Please enter the password below to access this PDF.", nil);
- _openButton.title = NSLocalizedString(@"Open", nil);
- _cancelButton.title = NSLocalizedString(@"Cancel", nil);
- }
- - (void)dealloc
- {
- _passwordDelegate = nil;
- }
- - (void)close
- {
- // [super close];
- [NSApp endSheet:self.window];
- [[self window] orderOut:self];
- }
- - (IBAction)cancelAction:(id)sender
- {
- [self close];
- // if ([_passwordDelegate respondsToSelector:@selector(didCancelUnlockFile:)]) {
- // [_passwordDelegate didCancelUnlockFile:self];
- // }
- }
- - (IBAction)OKAction:(id)sender
- {
- PDFDocument *pdfDoc = [[PDFDocument alloc] initWithURL:_fileURL];
- if (pdfDoc) {
- if ([pdfDoc unlockWithPassword:[_passwordTextField stringValue]]) {
- self.password = [_passwordTextField stringValue];
- [self close];
- }else{
- NSAlert *alert = [[NSAlert alloc] init];
- [alert setAlertStyle:NSAlertStyleCritical];
- [alert setMessageText:NSLocalizedString(@"Incorrect password. Please check your password and try again.", nil)];
- [alert runModal];
- }
- }
- }
- #pragma mark Show Methods
- - (void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
- if (contextInfo != NULL) {
- void (^handler)(NSString *password) = (void(^)(NSString *password))CFBridgingRelease(contextInfo);
- handler(_password);
- }
- }
- - (void)beginSheetModalForWindow:(NSWindow *)window completionHandler:(void (^)(NSString *password))handler{
- // [self retain]; // make sure we stay around long enough
-
- [NSApp beginSheet:[self window]
- modalForWindow:window
- modalDelegate:self
- didEndSelector:@selector(didEndSheet:returnCode:contextInfo:)
- contextInfo:handler ? CFBridgingRetain(handler) : NULL];
- }
- @end
|