Skip to content

Commit 0ea1c0a

Browse files
committed
Now also recognizes tab files. Row limit increased from 200 to 500 rows.
1 parent dffc2b1 commit 0ea1c0a

File tree

5 files changed

+200
-180
lines changed

5 files changed

+200
-180
lines changed

GeneratePreviewForURL.m

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,13 @@
1414
#import "CSVDocument.h"
1515
#import "CSVRowObject.h"
1616

17-
#define MAX_ROWS 200
17+
#define MAX_ROWS 500
1818

1919
static char* htmlReadableFileEncoding(NSStringEncoding stringEncoding);
2020
static char* humanReadableFileEncoding(NSStringEncoding stringEncoding);
2121
static char* formatFilesize(float bytes);
2222

2323

24-
/* -----------------------------------------------------------------------------
25-
Generate a preview for file
26-
27-
This function's job is to create preview for designated file
28-
----------------------------------------------------------------------------- */
29-
3024
OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
3125
{
3226
NSError *theErr = nil;
@@ -59,8 +53,8 @@ OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview,
5953
if (false == QLPreviewRequestIsCancelled(preview)) {
6054
CSVDocument *csvDoc = [CSVDocument csvDoc];
6155
csvDoc.autoDetectSeparator = YES;
62-
NSUInteger numRowsParsed = [csvDoc numRowsFromCSVString:fileString maxRows:MAX_ROWS error:NULL];
6356

57+
NSUInteger numRowsParsed = [csvDoc numRowsFromCSVString:fileString maxRows:MAX_ROWS error:NULL];
6458

6559
// Create HTML of the data if still interested in the preview
6660
if (false == QLPreviewRequestIsCancelled(preview)) {

GenerateThumbnailForURL.m

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#define THUMB_SIZE 512.0
99
#define ASPECT 0.8 // aspect ratio
1010
#define NUM_ROWS 18
11-
#define BADGE @"csv"
11+
#define BADGE_CSV @"csv"
12+
#define BADGE_TSV @"tab"
1213

1314
static CGContextRef createRGBABitmapContext(CGSize pixelSize);
1415
//static CGContextRef createVectorContext(CGSize pixelSize)
@@ -51,7 +52,7 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum
5152
// Parse the data if still interested in the thumbnail
5253
if (false == QLThumbnailRequestIsCancelled(thumbnail)) {
5354
CGFloat rowHeight = ceilf(THUMB_SIZE / NUM_ROWS);
54-
CGFloat fontSize = roundf(0.666 * rowHeight);
55+
CGFloat fontSize = roundf(0.666f * rowHeight);
5556

5657
CSVDocument *csvDoc = [CSVDocument csvDoc];
5758
csvDoc.autoDetectSeparator = YES;
@@ -60,8 +61,8 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum
6061

6162
// Draw an icon if still interested in the thumbnail
6263
if ((gotRows > 0) && (false == QLThumbnailRequestIsCancelled(thumbnail))) {
63-
CGRect maxBounds = CGRectMake(0.0, 0.0, THUMB_SIZE, THUMB_SIZE);
64-
CGRect usedBounds = CGRectMake(0.0, 0.0, 0.0, 0.0);
64+
CGRect maxBounds = CGRectMake(0.f, 0.f, THUMB_SIZE, THUMB_SIZE);
65+
CGRect usedBounds = CGRectMake(0.f, 0.f, 0.f, 0.f);
6566
CGFloat badgeMaxSize = THUMB_SIZE;
6667

6768
CGContextRef context = createRGBABitmapContext(maxBounds.size);
@@ -70,27 +71,27 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum
7071
//CGPDFContextBeginPage(context, NULL);
7172

7273
// Flip CoreGraphics coordinate system
73-
CGContextScaleCTM(context, 1.0, -1.0);
74+
CGContextScaleCTM(context, 1.f, -1.f);
7475
CGContextTranslateCTM(context, 0, -maxBounds.size.height);
7576

7677
// Create colors
77-
CGColorRef borderColor = CGColorCreateGenericRGB(0.67, 0.67, 0.67, 1.0);
78-
CGColorRef rowBG = CGColorCreateGenericRGB(1.0, 1.0, 1.0, 1.0);
79-
CGColorRef altRowBG = CGColorCreateGenericRGB(0.9, 0.9, 0.9, 1.0);
78+
CGColorRef borderColor = CGColorCreateGenericRGB(0.67f, 0.67f, 0.67f, 1.f);
79+
CGColorRef rowBG = CGColorCreateGenericRGB(1.f, 1.f, 1.f, 1.f);
80+
CGColorRef altRowBG = CGColorCreateGenericRGB(0.9f, 0.9f, 0.9f, 1.f);
8081

81-
CGFloat borderWidth = 1.0;
82+
CGFloat borderWidth = 1.f;
8283

8384
// We use NSGraphicsContext for the strings due to easier string drawing :P
8485
NSGraphicsContext *nsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:(void *)context flipped:YES];
8586
[NSGraphicsContext setCurrentContext:nsContext];
8687
if (nil != nsContext) {
8788
NSFont *myFont = [NSFont systemFontOfSize:fontSize];
88-
NSColor *rowTextColor = [NSColor colorWithCalibratedWhite:0.25 alpha:1.0];
89+
NSColor *rowTextColor = [NSColor colorWithCalibratedWhite:0.25f alpha:1.f];
8990
NSDictionary *stringAttributes = [NSDictionary dictionaryWithObjectsAndKeys:myFont, NSFontAttributeName,
9091
rowTextColor, NSForegroundColorAttributeName, nil];
9192

92-
CGFloat textXPadding = 5.0;
93-
CGFloat cellX = 0.0;
93+
CGFloat textXPadding = 5.f;
94+
CGFloat cellX = 0.f;
9495
CGFloat maxCellStringWidth;
9596

9697
// loop each column
@@ -99,8 +100,8 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum
99100
break;
100101
}
101102

102-
CGRect rowRect = CGRectMake(cellX, 0.0, maxBounds.size.width - cellX, rowHeight);
103-
maxCellStringWidth = 0.0;
103+
CGRect rowRect = CGRectMake(cellX, 0.f, maxBounds.size.width - cellX, rowHeight);
104+
maxCellStringWidth = 0.f;
104105
BOOL isFirstColumn = [csvDoc isFirstColumn:colKey];
105106
BOOL altRow = NO;
106107

@@ -158,7 +159,7 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum
158159

159160
CGFloat my_height = usedBounds.size.width * ASPECT;
160161
if (usedBounds.size.height < my_height) {
161-
CGRect missingRect = CGRectMake(0.0, usedBounds.size.height, ceilf(usedBounds.size.width), ceilf(my_height - usedBounds.size.height));
162+
CGRect missingRect = CGRectMake(0.f, usedBounds.size.height, ceilf(usedBounds.size.width), ceilf(my_height - usedBounds.size.height));
162163
CGContextSetFillColorWithColor(context, rowBG);
163164
CGContextFillRect(context, missingRect);
164165
}
@@ -192,13 +193,13 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum
192193
NSGraphicsContext *thumbNsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:(void *)thumbContext flipped:NO];
193194
[NSGraphicsContext setCurrentContext:thumbNsContext];
194195
if (nil != thumbNsContext) {
195-
NSString *badgeString = BADGE;
196-
CGFloat badgeFontSize = ceilf(badgeMaxSize * 0.28);
196+
NSString *badgeString = [@" " isEqualToString:csvDoc.separator] ? BADGE_TSV : BADGE_CSV;
197+
CGFloat badgeFontSize = ceilf(badgeMaxSize * 0.28f);
197198
NSFont *badgeFont = [NSFont boldSystemFontOfSize:badgeFontSize];
198-
NSColor *badgeColor = [NSColor colorWithCalibratedRed:0.05 green:0.25 blue:0.1 alpha:1.0];
199+
NSColor *badgeColor = [NSColor colorWithCalibratedRed:0.05f green:0.25f blue:0.1f alpha:1.f];
199200
NSShadow *badgeShadow = [[[NSShadow alloc] init] autorelease];
200-
[badgeShadow setShadowOffset:NSMakeSize(0.0, 0.0)];
201-
[badgeShadow setShadowBlurRadius:badgeFontSize * 0.01];
201+
[badgeShadow setShadowOffset:NSMakeSize(0.f, 0.f)];
202+
[badgeShadow setShadowBlurRadius:badgeFontSize * 0.01f];
202203
[badgeShadow setShadowColor:[NSColor whiteColor]];
203204

204205
// Set attributes and draw
@@ -208,8 +209,8 @@ OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thum
208209

209210
NSSize badgeSize = [badgeString sizeWithAttributes:badgeAttributes];
210211
CGFloat badge_x = (usedBounds.size.width / 2) - (badgeSize.width / 2);
211-
CGFloat badge_y = 0.025 * badgeMaxSize;
212-
NSRect badgeRect = NSMakeRect(badge_x, badge_y, 0.0, 0.0);
212+
CGFloat badge_y = 0.025f * badgeMaxSize;
213+
NSRect badgeRect = NSMakeRect(badge_x, badge_y, 0.f, 0.f);
213214
badgeRect.size = badgeSize;
214215

215216
[badgeString drawWithRect:badgeRect options:NSStringDrawingUsesLineFragmentOrigin attributes:badgeAttributes];
@@ -269,7 +270,7 @@ static CGContextRef createRGBABitmapContext(CGSize pixelSize)
269270
/*
270271
static CGContextRef createVectorContext(CGSize pixelSize)
271272
{
272-
CGRect mediaBox = CGRectMake(0.0, 0.0, 0.0, 0.0);
273+
CGRect mediaBox = CGRectMake(0.f, 0.f, 0.f, 0.f);
273274
mediaBox.size = pixelSize;
274275
275276
// allocate needed bytes

Info.plist

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,32 @@
77
<key>CFBundleDocumentTypes</key>
88
<array>
99
<dict>
10+
<key>CFBundleTypeExtensions</key>
11+
<array>
12+
<string>tsv</string>
13+
<string>tab</string>
14+
</array>
15+
<key>CFBundleTypeName</key>
16+
<string>Tab</string>
17+
<key>CFBundleTypeRole</key>
18+
<string>QLGenerator</string>
19+
<key>LSItemContentTypes</key>
20+
<array>
21+
<string>public.tab-separated-values-text</string>
22+
<string>dyn.ah62d4rv4ge81k2pc</string>
23+
</array>
24+
<key>LSTypeIsPackage</key>
25+
<false/>
26+
<key>NSPersistentStoreTypeKey</key>
27+
<string>XML</string>
28+
</dict>
29+
<dict>
30+
<key>CFBundleTypeExtensions</key>
31+
<array>
32+
<string>csv</string>
33+
</array>
34+
<key>CFBundleTypeName</key>
35+
<string>CSV</string>
1036
<key>CFBundleTypeRole</key>
1137
<string>QLGenerator</string>
1238
<key>LSItemContentTypes</key>
@@ -15,6 +41,27 @@
1541
<string>dyn.age80g650</string>
1642
<string>dyn.ah62d4rv4ge80g650</string>
1743
</array>
44+
<key>NSPersistentStoreTypeKey</key>
45+
<string>XML</string>
46+
</dict>
47+
<dict>
48+
<key>CFBundleTypeExtensions</key>
49+
<array>
50+
<string>tsv</string>
51+
<string>tab</string>
52+
</array>
53+
<key>CFBundleTypeName</key>
54+
<string>FileMaker</string>
55+
<key>CFBundleTypeRole</key>
56+
<string>QLGenerator</string>
57+
<key>LSItemContentTypes</key>
58+
<array>
59+
<string>com.apple.traditional-mac-plain-text</string>
60+
</array>
61+
<key>LSTypeIsPackage</key>
62+
<false/>
63+
<key>NSPersistentStoreTypeKey</key>
64+
<string>XML</string>
1865
</dict>
1966
</array>
2067
<key>CFBundleExecutable</key>
@@ -30,7 +77,7 @@
3077
<key>CFBundleShortVersionString</key>
3178
<string>1</string>
3279
<key>CFBundleVersion</key>
33-
<string>1.0</string>
80+
<string>1.1</string>
3481
<key>CFPlugInDynamicRegisterFunction</key>
3582
<string></string>
3683
<key>CFPlugInDynamicRegistration</key>

0 commit comments

Comments
 (0)