#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 if (![allImgProps objectForKey: iconFileName]) { 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; //we need this call to ensure NSApp is initialized (not done automatically for plugins) [NSApplication sharedApplication]; //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; NSBundle * bundle = [NSBundle bundleWithIdentifier: @"org.m0k.transmission.QuickLookPlugin"]; NSURL * styleURL = [bundle URLForResource: @"style" withExtension: @"css"]; NSString * styleContents = [NSString stringWithContentsOfURL: styleURL encoding: NSUTF8StringEncoding error: NULL]; NSMutableString * htmlString = [NSMutableString string]; [htmlString appendFormat: @"
", styleContents]; NSMutableDictionary * allImgProps = [NSMutableDictionary dictionary]; NSString * name = [NSString stringWithUTF8String: inf.name]; NSString * fileTypeString = inf.isMultifile ? NSFileTypeForHFSTypeCode(kGenericFolderIcon) : [name pathExtension]; const NSUInteger width = 32; [htmlString appendFormat: @"%@
", fileSizeString]; 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: NSLocalizedStringFromTableInBundle(@"Created on %@ with %@", nil, bundle, "quicklook creation info"), dateCreatedString, creatorString]; else if (dateCreatedString) creationString = [NSString stringWithFormat: NSLocalizedStringFromTableInBundle(@"Created on %@", nil, bundle, "quicklook creation info"), dateCreatedString]; else if (creatorString) creationString = [NSString stringWithFormat: NSLocalizedStringFromTableInBundle(@"Created with %@", nil, bundle, "quicklook creation info"), creatorString]; if (creationString) [htmlString appendFormat: @"%@
", creationString]; if (inf.comment) { NSString * comment = [NSString stringWithUTF8String: inf.comment]; if (![comment isEqualToString: @""]) [htmlString appendFormat: @"%@
", comment]; } NSMutableArray * lists = [NSMutableArray array]; if (inf.webseedCount > 0) { NSMutableString * listSection = [NSMutableString string]; [listSection appendString: @"%@ | |
---|---|
%s |
%@ | |
---|---|
%s |
%@ | |
---|---|
%@ |