Revision: 3240
          http://skim-app.svn.sourceforge.net/skim-app/?rev=3240&view=rev
Author:   hofman
Date:     2007-11-23 09:43:12 -0800 (Fri, 23 Nov 2007)

Log Message:
-----------
Add quicklook support for .skim files.

Modified Paths:
--------------
    trunk/QuickLook-Skim/GeneratePreviewForURL.m
    trunk/QuickLook-Skim/GenerateThumbnailForURL.m
    trunk/QuickLook-Skim/Info.plist

Modified: trunk/QuickLook-Skim/GeneratePreviewForURL.m
===================================================================
--- trunk/QuickLook-Skim/GeneratePreviewForURL.m        2007-11-23 13:33:38 UTC 
(rev 3239)
+++ trunk/QuickLook-Skim/GeneratePreviewForURL.m        2007-11-23 17:43:12 UTC 
(rev 3240)
@@ -35,8 +35,25 @@
 #include <CoreFoundation/CoreFoundation.h>
 #include <CoreServices/CoreServices.h>
 #include <QuickLook/QuickLook.h>
-#import <Foundation/Foundation.h>
+#import <Cocoa/Cocoa.h>
 
[EMAIL PROTECTED] NSColor (SKQLExtensions)
+- (NSString *)hexString;
[EMAIL PROTECTED]
+
[EMAIL PROTECTED] NSColor (SKQLExtensions)
+- (NSString *)hexString {
+    static char hexChars[16] = "0123456789abcdef";
+    NSColor *color = self;
+    if ([self alphaComponent] < 1.0)
+        color = [[NSColor controlBackgroundColor] 
blendedColorWithFraction:[self alphaComponent] ofColor:[self 
colorWithAlphaComponent:1.0]];
+    int red = (int)roundf(255 * [color redComponent]);
+    int green = (int)roundf(255 * [color greenComponent]);
+    int blue = (int)roundf(255 * [color blueComponent]);
+    return [NSString stringWithFormat:@"%C%C%C%C%C%C", hexChars[red / 16], 
hexChars[red % 16], hexChars[green / 16], hexChars[green % 16], hexChars[blue / 
16], hexChars[blue % 16]];
+}
[EMAIL PROTECTED]
+
 /* 
-----------------------------------------------------------------------------
    Generate a preview for file
 
@@ -71,6 +88,106 @@
             err = 2;
         }
         
+    } else if (UTTypeEqual(CFSTR("net.sourceforge.skim-app.skimnotes"), 
contentTypeUTI)) {
+        
+        NSData *data = [[NSData alloc] initWithContentsOfURL:(NSURL *)url 
options:NSUncachedRead error:NULL];
+        if (data) {
+            NSMutableString *htmlString = [[NSMutableString alloc] 
initWithString:@"<html><body><dl>\n"];
+            NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
+            [data release];
+            
+            if (array) {
+                NSEnumerator *noteEnum = [array objectEnumerator];
+                NSDictionary *note;
+                while (note = [noteEnum nextObject]) {
+                    NSString *type = [note objectForKey:@"type"];
+                    NSString *contents = [note objectForKey:@"contents"];
+                    NSString *text = [[note objectForKey:@"text"] string];
+                    NSString *color = [note objectForKey:@"color"];
+                    unsigned int pageIndex = [[note objectForKey:@"pageIndex"] 
unsignedIntValue];
+                    [htmlString appendFormat:@"<dt><img src=\"cid:%@.png\" 
style=\"background-color:#\" />%@ (page %i)</dt>", type, [color hexString], 
type, pageIndex+1];
+                    [htmlString appendFormat:@"<dd><b>%@</b>", contents];
+                    if (text)
+                        [htmlString appendFormat:@"<br />%@", text];
+                    [htmlString appendString:@"</dd>"];
+                }
+            }
+            
+            [htmlString appendString:@"</dl></body></html>"];
+            
+            NSMutableDictionary *props = [[NSMutableDictionary alloc] init];
+            [props setObject:@"UTF-8" forKey:(NSString 
*)kQLPreviewPropertyTextEncodingNameKey];
+            [props setObject:@"text/html" forKey:(NSString 
*)kQLPreviewPropertyMIMETypeKey];            
+            
+            NSBundle *bundle = [NSBundle 
bundleWithIdentifier:@"net.sourceforge.skim-app.quicklookgenerator"];
+            NSMutableDictionary *imgProps;
+            NSImage *image;
+            
+            imgProps = [[NSMutableDictionary alloc] init];
+            image = [NSData dataWithContentsOfFile:[bundle 
pathForResource:@"FreeText" ofType:@"png"]];
+            [imgProps setObject:@"image/png" forKey:(NSString 
*)kQLPreviewPropertyMIMETypeKey];
+            [imgProps setObject:image forKey:(NSString 
*)kQLPreviewPropertyAttachmentDataKey];
+            [props setObject:[NSDictionary dictionaryWithObject:imgProps 
forKey:@"FreeText.png"] forKey:(NSString *)kQLPreviewPropertyAttachmentsKey];
+            [imgProps release];
+            
+            imgProps = [[NSMutableDictionary alloc] init];
+            image = [NSData dataWithContentsOfFile:[bundle 
pathForResource:@"Note" ofType:@"png"]];
+            [imgProps setObject:@"image/png" forKey:(NSString 
*)kQLPreviewPropertyMIMETypeKey];
+            [imgProps setObject:image forKey:(NSString 
*)kQLPreviewPropertyAttachmentDataKey];
+            [props setObject:[NSDictionary dictionaryWithObject:imgProps 
forKey:@"Note.png"] forKey:(NSString *)kQLPreviewPropertyAttachmentsKey];
+            [imgProps release];
+            
+            imgProps = [[NSMutableDictionary alloc] init];
+            image = [NSData dataWithContentsOfFile:[bundle 
pathForResource:@"Circle" ofType:@"png"]];
+            [imgProps setObject:@"image/png" forKey:(NSString 
*)kQLPreviewPropertyMIMETypeKey];
+            [imgProps setObject:image forKey:(NSString 
*)kQLPreviewPropertyAttachmentDataKey];
+            [props setObject:[NSDictionary dictionaryWithObject:imgProps 
forKey:@"Circle.png"] forKey:(NSString *)kQLPreviewPropertyAttachmentsKey];
+            [imgProps release];
+            
+            imgProps = [[NSMutableDictionary alloc] init];
+            image = [NSData dataWithContentsOfFile:[bundle 
pathForResource:@"Square" ofType:@"png"]];
+            [imgProps setObject:@"image/png" forKey:(NSString 
*)kQLPreviewPropertyMIMETypeKey];
+            [imgProps setObject:image forKey:(NSString 
*)kQLPreviewPropertyAttachmentDataKey];
+            [props setObject:[NSDictionary dictionaryWithObject:imgProps 
forKey:@"Square.png"] forKey:(NSString *)kQLPreviewPropertyAttachmentsKey];
+            [imgProps release];
+            
+            imgProps = [[NSMutableDictionary alloc] init];
+            image = [NSData dataWithContentsOfFile:[bundle 
pathForResource:@"Highlight" ofType:@"png"]];
+            [imgProps setObject:@"image/png" forKey:(NSString 
*)kQLPreviewPropertyMIMETypeKey];
+            [imgProps setObject:image forKey:(NSString 
*)kQLPreviewPropertyAttachmentDataKey];
+            [props setObject:[NSDictionary dictionaryWithObject:imgProps 
forKey:@"Highlight.png"] forKey:(NSString *)kQLPreviewPropertyAttachmentsKey];
+            [imgProps release];
+            
+            imgProps = [[NSMutableDictionary alloc] init];
+            image = [NSData dataWithContentsOfFile:[bundle 
pathForResource:@"Underline" ofType:@"png"]];
+            [imgProps setObject:@"image/png" forKey:(NSString 
*)kQLPreviewPropertyMIMETypeKey];
+            [imgProps setObject:image forKey:(NSString 
*)kQLPreviewPropertyAttachmentDataKey];
+            [props setObject:[NSDictionary dictionaryWithObject:imgProps 
forKey:@"Underline.png"] forKey:(NSString *)kQLPreviewPropertyAttachmentsKey];
+            [imgProps release];
+            
+            imgProps = [[NSMutableDictionary alloc] init];
+            image = [NSData dataWithContentsOfFile:[bundle 
pathForResource:@"StrikeOut" ofType:@"png"]];
+            [imgProps setObject:@"image/png" forKey:(NSString 
*)kQLPreviewPropertyMIMETypeKey];
+            [imgProps setObject:image forKey:(NSString 
*)kQLPreviewPropertyAttachmentDataKey];
+            [props setObject:[NSDictionary dictionaryWithObject:imgProps 
forKey:@"StrikeOut.png"] forKey:(NSString *)kQLPreviewPropertyAttachmentsKey];
+            [imgProps release];
+            
+            imgProps = [[NSMutableDictionary alloc] init];
+            image = [NSData dataWithContentsOfFile:[bundle 
pathForResource:@"Line" ofType:@"png"]];
+            [imgProps setObject:@"image/png" forKey:(NSString 
*)kQLPreviewPropertyMIMETypeKey];
+            [imgProps setObject:image forKey:(NSString 
*)kQLPreviewPropertyAttachmentDataKey];
+            [props setObject:[NSDictionary dictionaryWithObject:imgProps 
forKey:@"Line.png"] forKey:(NSString *)kQLPreviewPropertyAttachmentsKey];
+            [imgProps release];
+            
+            
QLPreviewRequestSetDataRepresentation(preview,(CFDataRef)[htmlString 
dataUsingEncoding:NSUTF8StringEncoding], kUTTypeHTML, (CFDictionaryRef)props);
+            
+            [htmlString release];
+            [props release];
+            
+        } else {
+            err = 2;
+        }
+        
     }
     
     [pool release];

Modified: trunk/QuickLook-Skim/GenerateThumbnailForURL.m
===================================================================
--- trunk/QuickLook-Skim/GenerateThumbnailForURL.m      2007-11-23 13:33:38 UTC 
(rev 3239)
+++ trunk/QuickLook-Skim/GenerateThumbnailForURL.m      2007-11-23 17:43:12 UTC 
(rev 3240)
@@ -37,6 +37,36 @@
 #include <QuickLook/QuickLook.h>
 #import <Foundation/Foundation.h>
 
[EMAIL PROTECTED] NSAttributedString (SKQLExtensions)
++ (NSAttributedString *)imageAttachmentForType:(NSString *)type;
[EMAIL PROTECTED]
+
[EMAIL PROTECTED] NSAttributedString (SKQLExtensions)
++ (NSAttributedString *)imageAttachmentForType:(NSString *)type {
+    static NSMutableDictionary *imageAttachments = nil;
+    
+    NSAttributedString *attrString = nil;
+    if (attrString == nil) {
+        if (imageAttachments == nil) {
+            imageAttachments = [[NSMutableDictionary alloc] init];
+        NSBundle *bundle = [NSBundle 
bundleWithIdentifier:@"net.sourceforge.skim-app.quicklookgenerator"];
+        image = [[NSImage alloc] initWithContentsOfFile:[bundle 
pathForResource:@"Note" ofType:@"png"]];
+        [image release];
+        NSFileWrapper *wrapper = [[NSFileWrapper alloc] 
initRegularFileWithContents:[image TIFFRepresentation]];
+        [wrapper setPreferredFilename:[NSString stringWithFormat:@"[EMAIL 
PROTECTED]", type]];
+        
+        NSTextAttachment *attachment = [[NSTextAttachment alloc] 
initWithFileWrapper:wrapper];
+        [wrapper release];
+        attrString = [NSAttributedString 
attributedStringWithAttachment:attachment];
+        [imageAttachments setObject:attrString forKey:type];
+        [attachment release];
+    }
+    
+    return attrString;
+}
[EMAIL PROTECTED]
+
+
 /* 
-----------------------------------------------------------------------------
     Generate a thumbnail for file
 
@@ -73,13 +103,74 @@
                     QLThumbnailRequestSetImage(thumbnail, image, properties);
                     CGImageRelease(image);
                     CFRelease(properties);
-                } else {
-                    err = 2;
+
+                    // !!! early return
+                    [pool release];
+                    return noErr;
                 }
-            } else {
-                err = 2;
             }
             
+        } else if (UTTypeEqual(CFSTR("net.sourceforge.skim-app.skimnotes"), 
contentTypeUTI)) {
+            
+            NSData *data = [[NSData alloc] initWithContentsOfURL:(NSURL *)url 
options:NSUncachedRead error:NULL];
+            if (data) {
+                NSArray *array = [NSKeyedUnarchiver 
unarchiveObjectWithData:data];
+                [data release];
+                
+                NSMutableAttributedString *attrString = 
[[NSMutableAttributedString alloc] init];
+                NSFont *font = [self userFontOfSize:0.0];
+                NSFont *boldFont = [[NSFontManager sharedFontManager] 
convertFont:font toHaveTrait:NSBoldFontMask];
+                NSDictionary *attrs = [NSDictionary 
dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
+                NSDictionary *boldAttrs = [NSDictionary 
dictionaryWithObjectsAndKeys:boldFont, NSFontAttributeName, [NSParagraphStyle 
defaultParagraphStyle], NSParagraphStyleAttributeName, nil];
+                NSMutableParagraphStyle *noteParStyle = [[[NSParagraphStyle 
defaultParagraphStyle] mutableCopy] autorelease];
+                [noteParStyle setFirstLineHeadIndent:20.0];
+                [noteParStyle setHeadIndent:20.0];
+                 
+                if (array) {
+                    NSEnumerator *noteEnum = [array objectEnumerator];
+                    NSDictionary *note;
+                    while (note = [noteEnum nextObject]) {
+                        NSString *type = [note objectForKey:@"type"];
+                        NSString *contents = [note objectForKey:@"contents"];
+                        NSString *text = [[note objectForKey:@"text"] string];
+                        NSString *color = [note objectForKey:@"color"];
+                        unsigned int pageIndex = [[note 
objectForKey:@"pageIndex"] unsignedIntValue];
+                        int start;
+                        
+                        [attrString appendAttributedString:[NSAttributedString 
imageAttachmentForType:type]];
+                        [attrString 
addAttribute:NSBackgroundColorAttributeName value:color 
range:NSMakeRange([attrString length] - 1, 1)];
+                        [attrString 
appendAttributedString:[[[NSAttributedString alloc] initWithString:[NSString 
stringWithFormat:@"%@ (page %i)\n", type, pageIndex+1] attributes:attrs] 
autorelease]];
+                        start = [attrString length];
+                        [attrString 
appendAttributedString:[[[NSAttributedString alloc] initWithString:contents 
attributes:boldAttrs] autorelease]];
+                        if (text) {
+                            [attrString 
appendAttributedString:[[[NSAttributedString alloc] initWithString:@"\n"] 
autorelease]];
+                            [attrString 
appendAttributedString:[[[NSAttributedString alloc] initWithString:text 
attributes:attrs] autorelease]];
+                        }
+                        [attrString 
appendAttributedString:[[[NSAttributedString alloc] initWithString:@"\n"] 
autorelease]];
+                        [attrString addAttribute:NSParagraphStyleAttributeName 
value:noteParStyle range:NSMakeRange(start, [attrString length] - start)];
+                    }
+                    [attrString fixAttributesInRange:NSMakerange(0, 
[attrString length])];
+                }
+                
+                NSSize paperSize = NSMakeSize(612, 792);
+                CGContextRef ctxt = QLThumbnailRequestCreateContext(thumbnail, 
*(CGSize *)&paperSize, FALSE, NULL);
+                NSGraphicsContext *nsContext = [NSGraphicsContext 
graphicsContextWithGraphicsPort:ctxt flipped:NO];
+                [NSGraphicsContext saveGraphicsState];
+                [NSGraphicsContext setCurrentContext:nsContext];
+                [[NSColor whiteColor] setFill];
+                NSRect pageRect = NSMakeRect(0, 0, paperSize.width, 
paperSize.height);
+                NSRectFillUsingOperation(pageRect, NSCompositeSourceOver);
+                [attrString drawInRect:NSInsetRect(pageRect, 20.0f, 20.0f)];
+                QLThumbnailRequestFlushContext(thumbnail, ctxt);
+                CGContextRelease(ctxt);
+                [attrString release];
+                [NSGraphicsContext restoreGraphicsState];
+                
+                // !!! early return
+                [pool release];
+                return noErr;
+            }
+            
         }
     }
     /* fallback case: draw the file icon using Icon Services */

Modified: trunk/QuickLook-Skim/Info.plist
===================================================================
--- trunk/QuickLook-Skim/Info.plist     2007-11-23 13:33:38 UTC (rev 3239)
+++ trunk/QuickLook-Skim/Info.plist     2007-11-23 17:43:12 UTC (rev 3240)
@@ -12,6 +12,7 @@
                        <key>LSItemContentTypes</key>
                        <array>
                                <string>net.sourceforge.skim-app.pdfd</string>
+                               
<string>net.sourceforge.skim-app.skimnotes</string>
                        </array>
                </dict>
        </array>


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: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to