Permalink
Browse files

Now works with ISO encoded files as well

Originally, worked only with ASCII and Unicode files. Now tries to
determine the file encoding itself. If it can't, as is the case with my
ISO-encoded test files, it guesses that the encoding is ISO Latin 1,
which is a likely guess. It now will work with these files.
  • Loading branch information...
1 parent 7d5c06e commit c9c1a333d8d1c08cb2c533ca8394205b68725141 @roblourens roblourens committed Oct 13, 2011
@@ -3,40 +3,44 @@
#include <QuickLook/QuickLook.h>
#import <Foundation/Foundation.h>
-/* -----------------------------------------------------------------------------
- Generate a preview for file
-
- This function's job is to create preview for designated file
- ----------------------------------------------------------------------------- */
-
+// Generate a preview for the document with the given url
OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
-{
- if (QLPreviewRequestIsCancelled(preview))
+{
+ if (QLPreviewRequestIsCancelled(preview))
return noErr;
-
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-
- NSMutableDictionary *props = [[NSMutableDictionary alloc] init];
- [props setObject:@"UTF-8" forKey:(NSString *)kQLPreviewPropertyTextEncodingNameKey];
- [props setObject:@"text/plain" forKey:(NSString *)kQLPreviewPropertyMIMETypeKey];
- [props setObject:[NSNumber numberWithInt:700] forKey:(NSString *)kQLPreviewPropertyWidthKey];
- [props setObject:[NSNumber numberWithInt:500] forKey:(NSString *)kQLPreviewPropertyHeightKey];
-
- NSString *text = [NSString stringWithContentsOfURL:(NSURL *)url
- encoding:NSUTF8StringEncoding
- error:nil];
-
- QLPreviewRequestSetDataRepresentation(
- preview,
- (CFDataRef)[text dataUsingEncoding:NSUTF8StringEncoding],
- kUTTypeHTML,
- (CFDictionaryRef)props);
-
-
- [pool release];
-
- return noErr;
+
+ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+
+ NSMutableDictionary *props = [[NSMutableDictionary alloc] init];
+ [props setObject:@"text/plain" forKey:(NSString *)kQLPreviewPropertyMIMETypeKey];
+ [props setObject:[NSNumber numberWithInt:700] forKey:(NSString *)kQLPreviewPropertyWidthKey];
+ [props setObject:[NSNumber numberWithInt:800] forKey:(NSString *)kQLPreviewPropertyHeightKey];
+
+ NSStringEncoding encodingUsed;
+ NSString *text = [NSString stringWithContentsOfURL:(NSURL *)url
+ usedEncoding:&encodingUsed
+ error:nil];
+
+ // If the encoding could not be determined using usedEncoding, try Latin1
+ if (text == nil)
+ {
+ text = [NSString stringWithContentsOfURL:(NSURL *)url
+ encoding:NSISOLatin1StringEncoding
+ error:nil];
+ encodingUsed = NSISOLatin1StringEncoding;
+ }
+
+ QLPreviewRequestSetDataRepresentation(
+ preview,
+ (CFDataRef)[text dataUsingEncoding:encodingUsed],
+ kUTTypeHTML,
+ (CFDictionaryRef)props);
+
+
+ [pool release];
+
+ return noErr;
}
void CancelPreviewGeneration(void* thisInterface, QLPreviewRequestRef preview)
@@ -60,7 +60,7 @@
<key>QLPreviewWidth</key>
<real>800</real>
<key>QLSupportsConcurrentRequests</key>
- <false/>
+ <true/>
<key>QLThumbnailMinimumSize</key>
<real>17</real>
</dict>
@@ -153,7 +153,11 @@
isa = PBXProject;
buildConfigurationList = 2CA326220896AD4900168862 /* Build configuration list for PBXProject "QuickLookStephen" */;
compatibilityVersion = "Xcode 3.1";
+ developmentRegion = English;
hasScannedForEncodings = 1;
+ knownRegions = (
+ en,
+ );
mainGroup = 089C166AFE841209C02AAC07 /* QuickLookStephen */;
projectDirPath = "";
projectRoot = "";
@@ -212,6 +216,7 @@
2CA3261F0896AD4900168862 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
+ ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
COPY_PHASE_STRIP = NO;
GCC_DYNAMIC_NO_PIC = NO;
GCC_ENABLE_FIX_AND_CONTINUE = YES;
@@ -228,6 +233,7 @@
2CA326200896AD4900168862 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
+ ARCHS = "$(ARCHS_STANDARD_32_64_BIT)";
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
GCC_MODEL_TUNING = G5;
GCC_PRECOMPILE_PREFIX_HEADER = NO;
@@ -247,7 +253,7 @@
GCC_WARN_UNUSED_VARIABLE = YES;
ONLY_ACTIVE_ARCH = YES;
PREBINDING = NO;
- SDKROOT = macosx10.5;
+ SDKROOT = macosx10.6;
};
name = Debug;
};
@@ -259,7 +265,7 @@
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
PREBINDING = NO;
- SDKROOT = macosx10.5;
+ SDKROOT = macosx10.6;
};
name = Release;
};

0 comments on commit c9c1a33

Please sign in to comment.