2022-06-29 00:15:52 +00:00
|
|
|
@import AppKit;
|
|
|
|
@import CoreFoundation;
|
|
|
|
@import QuickLook;
|
2016-09-10 17:08:58 +00:00
|
|
|
|
2021-12-26 03:12:32 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <libtransmission/torrent-metainfo.h>
|
|
|
|
|
2012-08-12 12:11:33 +00:00
|
|
|
#import "NSStringAdditions.h"
|
|
|
|
|
2022-05-20 04:07:17 +00:00
|
|
|
QL_EXTERN_C_BEGIN
|
2021-08-15 09:41:48 +00:00
|
|
|
OSStatus GeneratePreviewForURL(void* thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options);
|
|
|
|
void CancelPreviewGeneration(void* thisInterface, QLPreviewRequestRef preview);
|
2022-05-20 04:07:17 +00:00
|
|
|
QL_EXTERN_C_END
|
2012-08-12 12:11:33 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* generateIconData(NSString* fileExtension, NSUInteger width, NSMutableDictionary* allImgProps)
|
2012-08-12 12:11:33 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* rawFilename = ![fileExtension isEqualToString:@""] ? fileExtension : @"blank_file_name_transmission";
|
|
|
|
// we need to do this once per file extension, per size
|
|
|
|
NSString* iconFileName = [NSString stringWithFormat:@"%ldx%@.tiff", width, rawFilename];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-06-29 04:20:42 +00:00
|
|
|
if (!allImgProps[iconFileName])
|
2012-08-12 12:11:33 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSImage* icon = [[NSWorkspace sharedWorkspace] iconForFileType:fileExtension];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSRect const iconFrame = NSMakeRect(0.0, 0.0, width, width);
|
|
|
|
NSImage* renderedIcon = [[NSImage alloc] initWithSize:iconFrame.size];
|
2012-08-12 12:11:33 +00:00
|
|
|
[renderedIcon lockFocus];
|
2021-10-31 15:18:27 +00:00
|
|
|
[icon drawInRect:iconFrame fromRect:NSZeroRect operation:NSCompositingOperationCopy fraction:1.0];
|
2012-08-12 12:11:33 +00:00
|
|
|
[renderedIcon unlockFocus];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-06-29 04:20:42 +00:00
|
|
|
NSData* iconData = renderedIcon.TIFFRepresentation;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSDictionary* imgProps = @{
|
|
|
|
(NSString*)kQLPreviewPropertyMIMETypeKey : @"image/png",
|
|
|
|
(NSString*)kQLPreviewPropertyAttachmentDataKey : iconData
|
|
|
|
};
|
2022-06-29 04:20:42 +00:00
|
|
|
allImgProps[iconFileName] = imgProps;
|
2012-08-12 12:11:33 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
return [@"cid:" stringByAppendingString:iconFileName];
|
2012-08-12 12:11:33 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
OSStatus GeneratePreviewForURL(void* thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
|
2012-08-12 12:11:33 +00:00
|
|
|
{
|
|
|
|
// Before proceeding make sure the user didn't cancel the request
|
|
|
|
if (QLPreviewRequestIsCancelled(preview))
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2012-08-12 12:11:33 +00:00
|
|
|
return noErr;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-09-06 01:15:31 +00:00
|
|
|
//we need this call to ensure NSApp is initialized (not done automatically for plugins)
|
|
|
|
[NSApplication sharedApplication];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-08-12 12:11:33 +00:00
|
|
|
//try to parse the torrent file
|
2021-12-26 03:12:32 +00:00
|
|
|
auto metainfo = tr_torrent_metainfo{};
|
2022-06-29 04:20:42 +00:00
|
|
|
if (!metainfo.parseTorrentFile(((__bridge NSURL*)url).path.UTF8String))
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2012-08-12 12:11:33 +00:00
|
|
|
return noErr;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSBundle* bundle = [NSBundle bundleWithIdentifier:@"org.m0k.transmission.QuickLookPlugin"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSURL* styleURL = [bundle URLForResource:@"style" withExtension:@"css"];
|
|
|
|
NSString* styleContents = [NSString stringWithContentsOfURL:styleURL encoding:NSUTF8StringEncoding error:NULL];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableString* htmlString = [NSMutableString string];
|
|
|
|
[htmlString appendFormat:@"<html><style type=\"text/css\">%@</style><body>", styleContents];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableDictionary* allImgProps = [NSMutableDictionary dictionary];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-06-29 04:20:42 +00:00
|
|
|
NSString* name = @(metainfo.name().c_str());
|
2021-12-26 03:12:32 +00:00
|
|
|
|
2022-01-08 18:53:35 +00:00
|
|
|
auto const n_files = metainfo.fileCount();
|
2021-12-26 03:12:32 +00:00
|
|
|
auto const is_multifile = n_files > 1;
|
2022-06-29 04:20:42 +00:00
|
|
|
NSString* fileTypeString = is_multifile ? NSFileTypeForHFSTypeCode(kGenericFolderIcon) : name.pathExtension;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSUInteger const width = 32;
|
|
|
|
[htmlString appendFormat:@"<h2><img class=\"icon\" src=\"%@\" width=\"%ld\" height=\"%ld\" />%@</h2>",
|
|
|
|
generateIconData(fileTypeString, width, allImgProps),
|
|
|
|
width,
|
|
|
|
width,
|
|
|
|
name];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-12-26 03:12:32 +00:00
|
|
|
NSString* fileSizeString = [NSString stringForFileSize:metainfo.totalSize()];
|
|
|
|
if (is_multifile)
|
2012-08-12 12:11:33 +00:00
|
|
|
{
|
2022-03-30 21:52:23 +00:00
|
|
|
NSString* fileCountString = [NSString
|
2022-11-14 18:30:03 +00:00
|
|
|
localizedStringWithFormat:NSLocalizedStringFromTableInBundle(@"%lu files", nil, bundle, "quicklook file count"), n_files];
|
2021-08-15 09:41:48 +00:00
|
|
|
fileSizeString = [NSString stringWithFormat:@"%@, %@", fileCountString, fileSizeString];
|
2012-08-12 12:11:33 +00:00
|
|
|
}
|
2021-08-15 09:41:48 +00:00
|
|
|
[htmlString appendFormat:@"<p>%@</p>", fileSizeString];
|
|
|
|
|
2021-12-26 03:12:32 +00:00
|
|
|
auto const date_created = metainfo.dateCreated();
|
|
|
|
NSString* dateCreatedString = date_created > 0 ?
|
|
|
|
[NSDateFormatter localizedStringFromDate:[NSDate dateWithTimeIntervalSince1970:date_created] dateStyle:NSDateFormatterLongStyle
|
2021-08-15 09:41:48 +00:00
|
|
|
timeStyle:NSDateFormatterShortStyle] :
|
|
|
|
nil;
|
2021-12-26 03:12:32 +00:00
|
|
|
auto const& creator = metainfo.creator();
|
2022-06-29 04:20:42 +00:00
|
|
|
NSString* creatorString = !std::empty(creator) ? @(creator.c_str()) : nil;
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([creatorString isEqualToString:@""])
|
|
|
|
{
|
|
|
|
creatorString = nil;
|
|
|
|
}
|
|
|
|
NSString* creationString = nil;
|
2012-08-12 14:05:55 +00:00
|
|
|
if (dateCreatedString && creatorString)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
creationString = [NSString
|
|
|
|
stringWithFormat:NSLocalizedStringFromTableInBundle(@"Created on %@ with %@", nil, bundle, "quicklook creation info"),
|
|
|
|
dateCreatedString,
|
|
|
|
creatorString];
|
|
|
|
}
|
2012-08-12 14:05:55 +00:00
|
|
|
else if (dateCreatedString)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
creationString = [NSString
|
|
|
|
stringWithFormat:NSLocalizedStringFromTableInBundle(@"Created on %@", nil, bundle, "quicklook creation info"), dateCreatedString];
|
|
|
|
}
|
2012-08-12 14:05:55 +00:00
|
|
|
else if (creatorString)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
creationString = [NSString
|
|
|
|
stringWithFormat:NSLocalizedStringFromTableInBundle(@"Created with %@", nil, bundle, "quicklook creation info"), creatorString];
|
|
|
|
}
|
2012-08-12 14:05:55 +00:00
|
|
|
if (creationString)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
[htmlString appendFormat:@"<p>%@</p>", creationString];
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-12-26 03:12:32 +00:00
|
|
|
auto const& commentStr = metainfo.comment();
|
|
|
|
if (!std::empty(commentStr))
|
2012-08-12 12:11:33 +00:00
|
|
|
{
|
2022-06-29 04:20:42 +00:00
|
|
|
NSString* comment = @(commentStr.c_str());
|
2021-08-15 09:41:48 +00:00
|
|
|
if (![comment isEqualToString:@""])
|
|
|
|
[htmlString appendFormat:@"<p>%@</p>", comment];
|
2012-08-12 12:11:33 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableArray* lists = [NSMutableArray array];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-01-09 16:55:09 +00:00
|
|
|
auto const n_webseeds = metainfo.webseedCount();
|
2021-12-26 03:12:32 +00:00
|
|
|
if (n_webseeds > 0)
|
2012-08-12 12:11:33 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableString* listSection = [NSMutableString string];
|
|
|
|
[listSection appendString:@"<table>"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-12-26 03:12:32 +00:00
|
|
|
NSString* headerTitleString = n_webseeds == 1 ?
|
2021-08-15 09:41:48 +00:00
|
|
|
NSLocalizedStringFromTableInBundle(@"1 Web Seed", nil, bundle, "quicklook web seed header") :
|
2022-11-14 18:30:03 +00:00
|
|
|
[NSString localizedStringWithFormat:NSLocalizedStringFromTableInBundle(@"%lu Web Seeds", nil, bundle, "quicklook web seed header"),
|
|
|
|
n_webseeds];
|
2021-08-15 09:41:48 +00:00
|
|
|
[listSection appendFormat:@"<tr><th>%@</th></tr>", headerTitleString];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-01-09 16:55:09 +00:00
|
|
|
for (size_t i = 0; i < n_webseeds; ++i)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-01-09 16:55:09 +00:00
|
|
|
[listSection appendFormat:@"<tr><td>%s<td></tr>", metainfo.webseed(i).c_str()];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-08-12 20:14:26 +00:00
|
|
|
[listSection appendString:@"</table>"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[lists addObject:listSection];
|
2012-08-12 12:11:33 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-12-26 03:12:32 +00:00
|
|
|
auto const& announce_list = metainfo.announceList();
|
|
|
|
if (!std::empty(announce_list))
|
2012-08-12 12:11:33 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableString* listSection = [NSMutableString string];
|
|
|
|
[listSection appendString:@"<table>"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-12-26 03:12:32 +00:00
|
|
|
auto const n = std::size(announce_list);
|
2021-12-14 20:59:40 +00:00
|
|
|
NSString* headerTitleString = n == 1 ?
|
2021-08-15 09:41:48 +00:00
|
|
|
NSLocalizedStringFromTableInBundle(@"1 Tracker", nil, bundle, "quicklook tracker header") :
|
2022-11-14 18:30:03 +00:00
|
|
|
[NSString localizedStringWithFormat:NSLocalizedStringFromTableInBundle(@"%lu Trackers", nil, bundle, "quicklook tracker header"), n];
|
2021-08-15 09:41:48 +00:00
|
|
|
[listSection appendFormat:@"<tr><th>%@</th></tr>", headerTitleString];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-08-12 12:11:33 +00:00
|
|
|
#warning handle tiers?
|
2021-12-26 03:12:32 +00:00
|
|
|
for (auto const& tracker : announce_list)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-04-16 20:41:51 +00:00
|
|
|
[listSection appendFormat:@"<tr><td>%s<td></tr>", tracker.announce.c_str()];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-08-12 20:14:26 +00:00
|
|
|
[listSection appendString:@"</table>"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[lists addObject:listSection];
|
2012-08-12 12:11:33 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-12-26 03:12:32 +00:00
|
|
|
if (is_multifile)
|
2012-08-12 12:11:33 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableString* listSection = [NSMutableString string];
|
|
|
|
[listSection appendString:@"<table>"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-03-30 21:52:23 +00:00
|
|
|
NSString* fileTitleString = [NSString
|
2022-11-14 18:30:03 +00:00
|
|
|
localizedStringWithFormat:NSLocalizedStringFromTableInBundle(@"%lu Files", nil, bundle, "quicklook file header"), n_files];
|
2021-08-15 09:41:48 +00:00
|
|
|
[listSection appendFormat:@"<tr><th>%@</th></tr>", fileTitleString];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-08-12 12:11:33 +00:00
|
|
|
#warning display folders?
|
2022-05-20 04:07:17 +00:00
|
|
|
for (auto const& [path, size] : metainfo.files().sortedByPath())
|
2012-08-12 12:11:33 +00:00
|
|
|
{
|
2022-06-29 04:20:42 +00:00
|
|
|
NSString* fullFilePath = @(path.c_str());
|
2021-08-15 09:41:48 +00:00
|
|
|
NSCAssert([fullFilePath hasPrefix:[name stringByAppendingString:@"/"]], @"Expected file path %@ to begin with %@/", fullFilePath, name);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-06-29 04:20:42 +00:00
|
|
|
NSString* shortenedFilePath = [fullFilePath substringFromIndex:name.length + 1];
|
2022-05-20 04:07:17 +00:00
|
|
|
NSString* shortenedFilePathAndSize = [NSString
|
|
|
|
stringWithFormat:@"%@ - %@", shortenedFilePath, [NSString stringForFileSize:size]];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSUInteger const width = 16;
|
|
|
|
[listSection appendFormat:@"<tr><td><img class=\"icon\" src=\"%@\" width=\"%ld\" height=\"%ld\" />%@<td></tr>",
|
2022-06-29 04:20:42 +00:00
|
|
|
generateIconData(shortenedFilePath.pathExtension, width, allImgProps),
|
2021-08-15 09:41:48 +00:00
|
|
|
width,
|
|
|
|
width,
|
2022-05-20 04:07:17 +00:00
|
|
|
shortenedFilePathAndSize];
|
2012-08-12 12:11:33 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-08-12 20:14:26 +00:00
|
|
|
[listSection appendString:@"</table>"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[lists addObject:listSection];
|
2012-08-12 12:11:33 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-06-29 04:20:42 +00:00
|
|
|
if (lists.count > 0)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
|
|
|
[htmlString appendFormat:@"<hr/><br>%@", [lists componentsJoinedByString:@"<br>"]];
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[htmlString appendString:@"</body></html>"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSDictionary* props = @{
|
|
|
|
(NSString*)kQLPreviewPropertyTextEncodingNameKey : @"UTF-8",
|
|
|
|
(NSString*)kQLPreviewPropertyMIMETypeKey : @"text/html",
|
|
|
|
(NSString*)kQLPreviewPropertyAttachmentsKey : allImgProps
|
|
|
|
};
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
QLPreviewRequestSetDataRepresentation(
|
|
|
|
preview,
|
|
|
|
(__bridge CFDataRef)[htmlString dataUsingEncoding:NSUTF8StringEncoding],
|
|
|
|
kUTTypeHTML,
|
|
|
|
(__bridge CFDictionaryRef)props);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2012-08-12 12:11:33 +00:00
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
void CancelPreviewGeneration(void* thisInterface, QLPreviewRequestRef preview)
|
2012-08-12 12:11:33 +00:00
|
|
|
{
|
|
|
|
// Implement only if supported
|
|
|
|
}
|