Revision: 2522
          http://skim-app.svn.sourceforge.net/skim-app/?rev=2522&view=rev
Author:   hofman
Date:     2007-07-17 16:35:02 -0700 (Tue, 17 Jul 2007)

Log Message:
-----------
Override method to determine the document type from a file. If it's a type we 
can't open, try to check for the file header, it might be PDF or PS with the 
wrong extension. 

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

Modified: trunk/SKDocumentController.m
===================================================================
--- trunk/SKDocumentController.m        2007-07-17 21:19:16 UTC (rev 2521)
+++ trunk/SKDocumentController.m        2007-07-17 23:35:02 UTC (rev 2522)
@@ -67,6 +67,39 @@
        return type;
 }
 
+- (NSString *)typeForContentsOfURL:(NSURL *)inAbsoluteURL error:(NSError 
**)outError {
+    static NSData *pdfHeaderData = nil;
+    if (nil == pdfHeaderData) {
+        char *h = "%PDF-";
+        pdfHeaderData = [[NSData alloc] initWithBytes:h length:strlen(h)];
+    }
+    static NSData *psHeaderData = nil;
+    if (nil == psHeaderData) {
+        char *h = "%PS-";
+        psHeaderData = [[NSData alloc] initWithBytes:h length:strlen(h)];
+    }
+    
+    NSError *error = nil;
+    NSString *type = [super typeForContentsOfURL:inAbsoluteURL error:&error];
+    
+    if (type == nil || [type isEqualToString:SKNotesRTFDocumentType] || [type 
isEqualToString:SKNotesTextDocumentType]) {
+        if ([inAbsoluteURL isFileURL]) {
+            NSString *fileName = [inAbsoluteURL path];
+            NSFileHandle *fh = [NSFileHandle 
fileHandleForReadingAtPath:fileName];
+            NSData *leadingData = [fh readDataOfLength:5];
+            if ([leadingData length] >= [pdfHeaderData length] && 
[pdfHeaderData isEqual:[leadingData subdataWithRange:NSMakeRange(0, 
[pdfHeaderData length])]]) {
+                type = SKPDFDocumentType;
+            } else if ([leadingData length] >= [psHeaderData length] && 
[psHeaderData isEqual:[leadingData subdataWithRange:NSMakeRange(0, 
[psHeaderData length])]]) {
+                type = SKPostScriptDocumentType;
+            }
+        }
+        if (type == nil && outError)
+            *outError = error;
+    }
+    
+    return type;
+}
+
 static NSData *convertTIFFDataToPDF(NSData *tiffData)
 {
     // this should accept any image data types we're likely to run across, but 
PICT returns a zero size image


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 DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to