Revision: 5254
          http://skim-app.svn.sourceforge.net/skim-app/?rev=5254&view=rev
Author:   hofman
Date:     2009-03-04 19:30:52 +0000 (Wed, 04 Mar 2009)

Log Message:
-----------
Ask for password when converting notes in encrypted docs that couldn't be 
unlocked using the keychain

Modified Paths:
--------------
    trunk/SKPDFDocument.m

Modified: trunk/SKPDFDocument.m
===================================================================
--- trunk/SKPDFDocument.m       2009-03-04 17:13:26 UTC (rev 5253)
+++ trunk/SKPDFDocument.m       2009-03-04 19:30:52 UTC (rev 5254)
@@ -75,6 +75,7 @@
 #import "SKApplication.h"
 #import "NSResponder_SKExtensions.h"
 #import "SKRuntime.h"
+#import "SKTextFieldSheetController.h"
 
 #define BUNDLE_DATA_FILENAME @"data"
 #define OPEN_META_TAGS_KEY @"com.apple.metadata:kOMUserTags"
@@ -98,7 +99,7 @@
 - (void)setOpenMetaTags:(NSArray *)array;
 - (void)setOpenMetaRating:(double)rating;
 
-- (void)tryToUnlockDocument:(PDFDocument *)document;
+- (BOOL)tryToUnlockDocument:(PDFDocument *)document;
 
 - (void)checkFileUpdatesIfNeeded;
 - (void)stopCheckingFileUpdates;
@@ -891,10 +892,27 @@
                        contextInfo:NULL];              
 }
 
-- (void)convertNotesSheetDidEnd:(NSAlert *)alert returnCode:(int)returnCode 
contextInfo:(void *)contextInfo {
-    if (returnCode == NSAlertAlternateReturn)
+- (void)convertNotesPasswordSheetDidEnd:(SKPasswordSheetController 
*)controller returnCode:(int)returnCode contextInfo:(void *)contextInfo {
+    PDFDocument *pdfDocWithoutNotes = (PDFDocument *)contextInfo;
+    
+    if (returnCode == NSCancelButton) {
+        [pdfDocWithoutNotes release];
         return;
+    }
     
+    if (pdfDocWithoutNotes && [pdfDocWithoutNotes 
unlockWithPassword:[[controller textField] stringValue]] == NO) {
+        [[controller window] orderOut:self];
+        
+        SKPasswordSheetController *passwordSheetController = 
[[[SKPasswordSheetController alloc] init] autorelease];
+        
+        [passwordSheetController beginSheetModalForWindow:[[self 
mainWindowController] window]
+            modalDelegate:self 
+           
didEndSelector:@selector(convertNotesPasswordSheetDidEnd:returnCode:contextInfo:)
+              contextInfo:pdfDocWithoutNotes];
+        
+        return;
+    }
+    
     [[[self windowForSheet] attachedSheet] orderOut:self];
         
     [[self progressController] setMessage:[NSLocalizedString(@"Converting 
notes", @"Message for progress sheet") stringByAppendingEllipsis]];
@@ -927,12 +945,13 @@
     }
     
     if (didConvert) {
-        pdfDoc = [[PDFDocument alloc] initWithData:pdfData];
-        if ([pdfDoc isLocked])
-            [self tryToUnlockDocument:pdfDoc];
-        count = [pdfDoc pageCount];
+        if (pdfDocWithoutNotes == nil)
+            pdfDocWithoutNotes = [[PDFDocument alloc] initWithData:pdfData];
+        if ([pdfDocWithoutNotes isLocked])
+            [self tryToUnlockDocument:pdfDocWithoutNotes];
+        count = [pdfDocWithoutNotes pageCount];
         for (i = 0; i < count; i++) {
-            PDFPage *page = [pdfDoc pageAtIndex:i];
+            PDFPage *page = [pdfDocWithoutNotes pageAtIndex:i];
             NSEnumerator *annEnum = [[[[page annotations] copy] autorelease] 
objectEnumerator];
             PDFAnnotation *annotation;
             
@@ -942,15 +961,38 @@
             }
         }
         
-        [self setPDFDataUndoable:[pdfDoc dataRepresentation]];
-        [pdfDoc release];
+        [self setPDFDataUndoable:[pdfDocWithoutNotes dataRepresentation]];
         
         [[self undoManager] setActionName:NSLocalizedString(@"Convert Notes", 
@"Undo action name")];
     }
     
+    [pdfDocWithoutNotes release];
+    
     [[self progressController] endSheet];
 }
 
+- (void)convertNotesSheetDidEnd:(NSAlert *)alert returnCode:(int)returnCode 
contextInfo:(void *)contextInfo {
+    if (returnCode == NSAlertAlternateReturn)
+        return;
+    
+    PDFDocument *pdfDocWithoutNotes = nil;
+    
+    if ([[self pdfDocument] isEncrypted]) {
+        pdfDocWithoutNotes = [[PDFDocument alloc] initWithData:pdfData];
+        if ([pdfDocWithoutNotes isLocked] && [self 
tryToUnlockDocument:pdfDocWithoutNotes] == NO) {
+            SKPasswordSheetController *passwordSheetController = 
[[[SKPasswordSheetController alloc] init] autorelease];
+            
+            [passwordSheetController beginSheetModalForWindow:[[self 
mainWindowController] window]
+                modalDelegate:self 
+               
didEndSelector:@selector(convertNotesPasswordSheetDidEnd:returnCode:contextInfo:)
+                  contextInfo:pdfDocWithoutNotes];
+            
+            return;
+        }
+    }
+    [self convertNotesPasswordSheetDidEnd:nil returnCode:NSOKButton 
contextInfo:pdfDocWithoutNotes];
+}
+
 - (IBAction)convertNotes:(id)sender {
     NSString *message = NSLocalizedString(@"This will convert PDF annotations 
to Skim notes. Do you want to proceed?", @"Informative text in alert dialog");
     if (floor(NSAppKitVersionNumber) <= NSAppKitVersionNumber10_4)
@@ -1791,7 +1833,7 @@
     }
 }
 
-- (void)tryToUnlockDocument:(PDFDocument *)document {
+- (BOOL)tryToUnlockDocument:(PDFDocument *)document {
     if (NSAlertAlternateReturn != [[NSUserDefaults standardUserDefaults] 
integerForKey:SKSavePasswordOptionKey]) {
         const char *serviceName = [self keychainServiceName];
         if (serviceName != NULL) {
@@ -1805,10 +1847,11 @@
                 data = [NSData dataWithBytes:passwordData 
length:passwordLength];
                 SecKeychainItemFreeContent(NULL, passwordData);
                 aPassword = [[[NSString alloc] initWithData:data 
encoding:NSUTF8StringEncoding] autorelease];
-                [document unlockWithPassword:aPassword];
+                return [document unlockWithPassword:aPassword];
             }
         }
     }
+    return NO;
 }
 
 #pragma mark Scripting support


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to