2012-08-12 12:11:33 +00:00
# import "transmission.h"
# import "NSStringAdditions.h"
OSStatus GeneratePreviewForURL ( void * thisInterface , QLPreviewRequestRef preview , CFURLRef url , CFStringRef contentTypeUTI , CFDictionaryRef options ) ;
void CancelPreviewGeneration ( void * thisInterface , QLPreviewRequestRef preview ) ;
NSString * generateIconData ( NSString * fileExtension , NSUInteger width , NSMutableDictionary * allImgProps )
{
NSString * rawFilename = ! [ fileExtension isEqualToString : @ "" ] ? fileExtension : @ "blank_file_name_transmission" ;
NSString * iconFileName = [ NSString stringWithFormat : @ "%ldx%@.tiff" , width , rawFilename ] ; // we need to do this once per file extension , per size
2012-08-12 14:05:55 +00:00
if ( ! [ allImgProps objectForKey : iconFileName ] )
2012-08-12 12:11:33 +00:00
{
NSImage * icon = [ [ NSWorkspace sharedWorkspace ] iconForFileType : fileExtension ] ;
const NSRect iconFrame = NSMakeRect ( 0.0 , 0.0 , width , width ) ;
NSImage * renderedIcon = [ [ NSImage alloc ] initWithSize : iconFrame . size ] ;
[ renderedIcon lockFocus ] ;
[ icon drawInRect : iconFrame fromRect : NSZeroRect operation : NSCompositeCopy fraction : 1.0 ] ;
[ renderedIcon unlockFocus ] ;
NSData * iconData = [ renderedIcon TIFFRepresentation ] ;
[ renderedIcon release ] ;
NSDictionary * imgProps = @ {
( NSString * ) kQLPreviewPropertyMIMETypeKey : @ "image/png" ,
( NSString * ) kQLPreviewPropertyAttachmentDataKey : iconData } ;
[ allImgProps setObject : imgProps forKey : iconFileName ] ;
}
return [ @ "cid:" stringByAppendingString : iconFileName ] ;
}
OSStatus GeneratePreviewForURL ( void * thisInterface , QLPreviewRequestRef preview , CFURLRef url , CFStringRef contentTypeUTI , CFDictionaryRef options )
{
// Before proceeding make sure the user didn ' t cancel the request
if ( QLPreviewRequestIsCancelled ( preview ) )
return noErr ;
// try to parse the torrent file
tr_info inf ;
tr_ctor * ctor = tr_ctorNew ( NULL ) ;
tr_ctorSetMetainfoFromFile ( ctor , [ [ ( NSURL * ) url path ] UTF8String ] ) ;
const int err = tr_torrentParse ( ctor , & inf ) ;
tr_ctorFree ( ctor ) ;
if ( err )
return noErr ;
NSURL * styleURL = [ [ NSBundle bundleWithIdentifier : @ "org.m0k.transmission.QuickLookPlugin" ] URLForResource : @ "style" withExtension : @ "css" ] ;
NSString * styleContents = [ NSString stringWithContentsOfURL : styleURL encoding : NSUTF8StringEncoding error : NULL ] ;
NSMutableString * htmlString = [ NSMutableString string ] ;
[ htmlString appendFormat : @ "<html><style type=\" text / css \ ">%@</style><body>" , styleContents ] ;
NSMutableDictionary * allImgProps = [ NSMutableDictionary dictionary ] ;
NSString * name = [ NSString stringWithUTF8String : inf . name ] ;
NSString * fileTypeString = inf . isMultifile ? NSFileTypeForHFSTypeCode ( kGenericFolderIcon ) : [ name pathExtension ] ;
const NSUInteger width = 32 ;
2012-08-12 20:14:26 +00:00
[ htmlString appendFormat : @ "<h2><img class=\" icon \ " src=\" % @ \ " width=\" % ld \ " height=\" % ld \ " />%@</h2>" , generateIconData ( fileTypeString , width , allImgProps ) , width , width , name ] ;
2012-08-12 12:11:33 +00:00
NSString * fileSizeString = [ NSString stringForFileSize : inf . totalSize ] ;
if ( inf . isMultifile )
{
NSString * fileCountString ;
if ( inf . fileCount = = 1 )
fileCountString = NSLocalizedString ( @ "1 file" , "quicklook file count" ) ;
else
fileCountString = [ NSString stringWithFormat : NSLocalizedString ( @ "%@ files" , "quicklook file count" ) , [ NSString formattedUInteger : inf . fileCount ] ] ;
fileSizeString = [ NSString stringWithFormat : @ "%@, %@" , fileCountString , fileSizeString ] ;
}
[ htmlString appendFormat : @ "<p>%@</p>" , fileSizeString ] ;
2012-08-12 14:05:55 +00:00
NSString * dateCreatedString = inf . dateCreated > 0 ? [ NSDateFormatter localizedStringFromDate : [ NSDate dateWithTimeIntervalSince1970 : inf . dateCreated ] dateStyle : NSDateFormatterLongStyle timeStyle : NSDateFormatterShortStyle ] : nil ;
NSString * creatorString = inf . creator ? [ NSString stringWithUTF8String : inf . creator ] : nil ;
if ( [ creatorString isEqualToString : @ "" ] ) creatorString = nil ;
NSString * creationString = nil ;
if ( dateCreatedString && creatorString )
creationString = [ NSString stringWithFormat : NSLocalizedString ( @ "Created on %@ with %@" , "quicklook creation info" ) , dateCreatedString , creatorString ] ;
else if ( dateCreatedString )
creationString = [ NSString stringWithFormat : NSLocalizedString ( @ "Created on %@" , "quicklook creation info" ) , dateCreatedString ] ;
else if ( creatorString )
creationString = [ NSString stringWithFormat : NSLocalizedString ( @ "Created with %@" , "quicklook creation info" ) , creatorString ] ;
if ( creationString )
[ htmlString appendFormat : @ "<p>%@</p>" , creationString ] ;
2012-08-12 12:11:33 +00:00
if ( inf . comment )
{
NSString * comment = [ NSString stringWithUTF8String : inf . comment ] ;
if ( ! [ comment isEqualToString : @ "" ] )
[ htmlString appendFormat : @ "<p>%@</p>" , comment ] ;
}
2012-08-12 20:14:26 +00:00
NSMutableArray * lists = [ NSMutableArray array ] ;
2012-08-12 12:11:33 +00:00
if ( inf . webseedCount > 0 )
{
2012-08-12 20:14:26 +00:00
NSMutableString * listSection = [ NSMutableString string ] ;
[ listSection appendString : @ "<table>" ] ;
2012-08-12 12:11:33 +00:00
NSString * headerTitleString = inf . webseedCount = = 1 ? NSLocalizedString ( @ "1 Web Seed" , "quicklook web seed header" ) : [ NSString stringWithFormat : NSLocalizedString ( @ "%@ Web Seeds" , "quicklook web seed header" ) , [ NSString formattedUInteger : inf . webseedCount ] ] ;
2012-08-12 20:14:26 +00:00
[ listSection appendFormat : @ "<tr><th>%@</th></tr>" , headerTitleString ] ;
2012-08-12 12:11:33 +00:00
for ( int i = 0 ; i < inf . webseedCount ; + + i )
2012-08-12 20:14:26 +00:00
[ listSection appendFormat : @ "<tr><td>%s<td></tr>" , inf . webseeds [ i ] ] ;
2012-08-12 12:11:33 +00:00
2012-08-12 20:14:26 +00:00
[ listSection appendString : @ "</table>" ] ;
[ lists addObject : listSection ] ;
2012-08-12 12:11:33 +00:00
}
if ( inf . trackerCount > 0 )
{
2012-08-12 20:14:26 +00:00
NSMutableString * listSection = [ NSMutableString string ] ;
[ listSection appendString : @ "<table>" ] ;
2012-08-12 12:11:33 +00:00
NSString * headerTitleString = inf . trackerCount = = 1 ? NSLocalizedString ( @ "1 Tracker" , "quicklook tracker header" ) : [ NSString stringWithFormat : NSLocalizedString ( @ "%@ Trackers" , "quicklook tracker header" ) , [ NSString formattedUInteger : inf . trackerCount ] ] ;
2012-08-12 20:14:26 +00:00
[ listSection appendFormat : @ "<tr><th>%@</th></tr>" , headerTitleString ] ;
2012-08-12 12:11:33 +00:00
# warning handle tiers ?
for ( int i = 0 ; i < inf . trackerCount ; + + i )
2012-08-12 20:14:26 +00:00
[ listSection appendFormat : @ "<tr><td>%s<td></tr>" , inf . trackers [ i ] . announce ] ;
[ listSection appendString : @ "</table>" ] ;
2012-08-12 12:11:33 +00:00
2012-08-12 20:14:26 +00:00
[ lists addObject : listSection ] ;
2012-08-12 12:11:33 +00:00
}
if ( inf . isMultifile )
{
2012-08-12 20:14:26 +00:00
NSMutableString * listSection = [ NSMutableString string ] ;
[ listSection appendString : @ "<table>" ] ;
2012-08-12 12:11:33 +00:00
NSString * fileTitleString = inf . fileCount = = 1 ? NSLocalizedString ( @ "1 File" , "quicklook file header" ) : [ NSString stringWithFormat : NSLocalizedString ( @ "%@ Files" , "quicklook file header" ) , [ NSString formattedUInteger : inf . fileCount ] ] ;
2012-08-12 20:14:26 +00:00
[ listSection appendFormat : @ "<tr><th>%@</th></tr>" , fileTitleString ] ;
2012-08-12 12:11:33 +00:00
# warning display size ?
# warning display folders ?
for ( int i = 0 ; i < inf . fileCount ; + + i )
{
NSString * fullFilePath = [ NSString stringWithUTF8String : inf . files [ i ] . name ] ;
NSCAssert ( [ fullFilePath hasPrefix : [ name stringByAppendingString : @ "/" ] ] , @ "Expected file path %@ to begin with %@/" , fullFilePath , name ) ;
NSString * shortenedFilePath = [ fullFilePath substringFromIndex : [ name length ] + 1 ] ;
const NSUInteger width = 16 ;
2012-08-12 20:14:26 +00:00
[ listSection appendFormat : @ "<tr><td><img class=\" icon \ " src=\" % @ \ " width=\" % ld \ " height=\" % ld \ " />%@<td></tr>" , generateIconData ( [ shortenedFilePath pathExtension ] , width , allImgProps ) , width , width , shortenedFilePath ] ;
2012-08-12 12:11:33 +00:00
}
2012-08-12 20:14:26 +00:00
[ listSection appendString : @ "</table>" ] ;
[ lists addObject : listSection ] ;
2012-08-12 12:11:33 +00:00
}
2012-08-12 20:14:26 +00:00
if ( [ lists count ] > 0 )
2012-08-13 00:52:04 +00:00
[ htmlString appendFormat : @ "<hr/><br>%@" , [ lists componentsJoinedByString : @ "<br>" ] ] ;
2012-08-12 20:14:26 +00:00
2012-08-12 12:11:33 +00:00
[ htmlString appendString : @ "</body></html>" ] ;
tr_metainfoFree ( & inf ) ;
NSDictionary * props = @ { ( NSString * ) kQLPreviewPropertyTextEncodingNameKey : @ "UTF-8" ,
( NSString * ) kQLPreviewPropertyMIMETypeKey : @ "text/html" ,
( NSString * ) kQLPreviewPropertyAttachmentsKey : allImgProps } ;
QLPreviewRequestSetDataRepresentation ( preview , ( CFDataRef ) [ htmlString dataUsingEncoding : NSUTF8StringEncoding ] , kUTTypeHTML , ( CFDictionaryRef ) props ) ;
return noErr ;
}
void CancelPreviewGeneration ( void * thisInterface , QLPreviewRequestRef preview )
{
// Implement only if supported
}