Revision: 4967
          http://skim-app.svn.sourceforge.net/skim-app/?rev=4967&view=rev
Author:   hofman
Date:     2008-11-17 16:23:58 +0000 (Mon, 17 Nov 2008)

Log Message:
-----------
avoid code duplication

Modified Paths:
--------------
    trunk/SkimImporter/GetMetadataForFile.m

Modified: trunk/SkimImporter/GetMetadataForFile.m
===================================================================
--- trunk/SkimImporter/GetMetadataForFile.m     2008-11-17 16:08:40 UTC (rev 
4966)
+++ trunk/SkimImporter/GetMetadataForFile.m     2008-11-17 16:23:58 UTC (rev 
4967)
@@ -42,6 +42,28 @@
 #import <Quartz/Quartz.h>
 #import <SkimNotesBase/SkimNotesBase.h>
 
+static BOOL GetTextAndAttributesForPDFFile(NSURL *url, NSString **text, 
NSDictionary **info)
+{
+    PDFDocument *pdfDoc = [[PDFDocument alloc] initWithURL:url];
+    if (pdfDoc) {
+        if (text != NULL)
+            *text = [pdfDoc string];
+        if (info != NULL) {
+            unsigned int pageCount = [pdfDoc pageCount];
+            NSSize size = pageCount ? [[pdfDoc pageAtIndex:0] 
boundsForBox:kPDFDisplayBoxCropBox].size : NSZeroSize;
+            NSMutableDictionary *mutableInfo = [[[pdfDoc documentAttributes] 
mutableCopy] autorelease];
+            [mutableInfo setValue:[NSString stringWithFormat: @"%d.%d", 
[pdfDoc majorVersion], [pdfDoc minorVersion]] forKey:@"Version"];
+            [mutableInfo setValue:[NSNumber numberWithBool:[pdfDoc 
isEncrypted]] forKey:@"Encrypted"];
+            [mutableInfo setValue:[NSNumber numberWithUnsignedInt:pageCount] 
forKey:@"PageCount"];
+            [mutableInfo setValue:[NSNumber numberWithFloat:size.width] 
forKey:@"PageWidth"];
+            [mutableInfo setValue:[NSNumber numberWithFloat:size.height] 
forKey:@"PageHeight"];
+            *info = mutableInfo;
+        }
+        [pdfDoc release];
+    }
+    return pdfDoc != nil;
+}
+
 Boolean GetMetadataForFile(void* thisInterface, 
                            CFMutableDictionaryRef attributes, 
                            CFStringRef contentTypeUTI,
@@ -71,20 +93,7 @@
             sourcePath = [[(NSString *)pathToFile 
stringByDeletingPathExtension] stringByAppendingPathExtension:@"pdf"];
         } else if (isPDF) {
             notes = [fm readSkimNotesFromExtendedAttributesAtURL:fileURL 
error:NULL];
-            PDFDocument *pdfDoc = [[PDFDocument alloc] initWithURL:fileURL];
-            if (pdfDoc) {
-                pdfText = [pdfDoc string];
-                unsigned int pageCount = [pdfDoc pageCount];
-                NSSize size = pageCount ? [[pdfDoc pageAtIndex:0] 
boundsForBox:kPDFDisplayBoxCropBox].size : NSZeroSize;
-                NSMutableDictionary *mutableInfo = [[[pdfDoc 
documentAttributes] mutableCopy] autorelease];
-                [mutableInfo setValue:[NSString stringWithFormat: @"%d.%d", 
[pdfDoc majorVersion], [pdfDoc minorVersion]] forKey:@"Version"];
-                [mutableInfo setValue:[NSNumber numberWithBool:[pdfDoc 
isEncrypted]] forKey:@"Encrypted"];
-                [mutableInfo setValue:[NSNumber 
numberWithUnsignedInt:pageCount] forKey:@"PageCount"];
-                [mutableInfo setValue:[NSNumber numberWithFloat:size.width] 
forKey:@"PageWidth"];
-                [mutableInfo setValue:[NSNumber numberWithFloat:size.height] 
forKey:@"PageHeight"];
-                info = mutableInfo;
-                [pdfDoc release];
-            }
+            GetTextAndAttributesForPDFFile(fileURL, &pdfText, &info);
         } else if (isPDFBundle) {
             notes = [fm readSkimNotesFromPDFBundleAtURL:fileURL error:NULL];
             NSString *textPath = [(NSString *)pathToFile 
stringByAppendingPathComponent:@"data.txt"];
@@ -94,23 +103,8 @@
             info = plistData ? [NSPropertyListSerialization 
propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable 
format:NULL errorDescription:NULL] : nil;
             if (pdfText == nil || info == nil) {
                 NSString *pdfPath = [fm bundledFileWithExtension:@"pdf" 
inPDFBundleAtPath:(NSString *)pathToFile error:NULL];
-                PDFDocument *pdfDoc = [[PDFDocument alloc] initWithURL:[NSURL 
fileURLWithPath:pdfPath]];
-                if (pdfDoc) {
-                    if (pdfText == nil)
-                        pdfText = [pdfDoc string];
-                    if (info == nil) {
-                        unsigned int pageCount = [pdfDoc pageCount];
-                        NSSize size = pageCount ? [[pdfDoc pageAtIndex:0] 
boundsForBox:kPDFDisplayBoxCropBox].size : NSZeroSize;
-                        NSMutableDictionary *mutableInfo = [[[pdfDoc 
documentAttributes] mutableCopy] autorelease];
-                        [mutableInfo setValue:[NSString stringWithFormat: 
@"%d.%d", [pdfDoc majorVersion], [pdfDoc minorVersion]] forKey:@"Version"];
-                        [mutableInfo setValue:[NSNumber numberWithBool:[pdfDoc 
isEncrypted]] forKey:@"Encrypted"];
-                        [mutableInfo setValue:[NSNumber 
numberWithUnsignedInt:pageCount] forKey:@"PageCount"];
-                        [mutableInfo setValue:[NSNumber 
numberWithFloat:size.width] forKey:@"PageWidth"];
-                        [mutableInfo setValue:[NSNumber 
numberWithFloat:size.height] forKey:@"PageHeight"];
-                        info = mutableInfo;
-                    }
-                    [pdfDoc release];
-                }
+                if (pdfPath)
+                    GetTextAndAttributesForPDFFile([NSURL 
fileURLWithPath:pdfPath], pdfText == nil ? &pdfText : NULL, info == nil ? &info 
: NULL);
             }
         }
         


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

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to