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>
|
2023-10-12 23:53:57 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
2021-12-26 03:12:32 +00:00
|
|
|
|
|
|
|
#include <libtransmission/torrent-metainfo.h>
|
2023-10-12 23:53:57 +00:00
|
|
|
#include <libtransmission/utils.h>
|
2021-12-26 03:12:32 +00:00
|
|
|
|
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
|
|
|
|
2023-10-12 23:53:57 +00:00
|
|
|
static NSUInteger const kIconWidth = 16;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
class FileTreeNode
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FileTreeNode() = default;
|
|
|
|
~FileTreeNode() = default;
|
|
|
|
|
|
|
|
auto MaybeCreateChild(std::string_view child_name)
|
|
|
|
{
|
|
|
|
return children_.try_emplace(std::string{ child_name });
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
FileTreeNode(FileTreeNode const&) = delete;
|
|
|
|
FileTreeNode& operator=(FileTreeNode&) = delete;
|
|
|
|
|
|
|
|
std::unordered_map<std::string, FileTreeNode> children_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +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{};
|
2023-04-23 01:25:55 +00:00
|
|
|
if (!metainfo.parse_torrent_file(((__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
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const n_files = metainfo.file_count();
|
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
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
NSString* fileSizeString = [NSString stringForFileSize:metainfo.total_size()];
|
2021-12-26 03:12:32 +00:00
|
|
|
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];
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const date_created = metainfo.date_created();
|
2021-12-26 03:12:32 +00:00
|
|
|
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
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const n_webseeds = metainfo.webseed_count();
|
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
|
|
|
{
|
2023-04-15 22:07:03 +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
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
auto const& announce_list = metainfo.announce_list();
|
2021-12-26 03:12:32 +00:00
|
|
|
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
|
|
|
|
2023-04-15 22:07:03 +00:00
|
|
|
// TODO: handle tiers?
|
2021-12-26 03:12:32 +00:00
|
|
|
for (auto const& tracker : announce_list)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2023-04-15 22:07:03 +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
|
|
|
|
2023-10-12 23:53:57 +00:00
|
|
|
FileTreeNode root{};
|
|
|
|
|
2022-05-20 04:07:17 +00:00
|
|
|
for (auto const& [path, size] : metainfo.files().sortedByPath())
|
2012-08-12 12:11:33 +00:00
|
|
|
{
|
2023-10-12 23:53:57 +00:00
|
|
|
FileTreeNode* curNode = &root;
|
|
|
|
size_t level = 0;
|
|
|
|
|
|
|
|
auto subpath = std::string_view{ path };
|
|
|
|
auto path_vec = std::vector<std::string_view>{};
|
|
|
|
auto token = std::string_view{};
|
|
|
|
while (tr_strv_sep(&subpath, &token, '/'))
|
|
|
|
{
|
|
|
|
path_vec.emplace_back(token);
|
|
|
|
}
|
|
|
|
size_t const last = path_vec.size() - 1;
|
|
|
|
|
|
|
|
for (auto const& part : path_vec)
|
|
|
|
{
|
|
|
|
auto [it, inserted] = curNode->MaybeCreateChild(part);
|
|
|
|
if (inserted)
|
|
|
|
{
|
|
|
|
NSString* prefix = @"";
|
|
|
|
for (size_t i = 0; i < level; ++i)
|
|
|
|
{
|
|
|
|
prefix = [prefix stringByAppendingString:@" "];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString* pathPart = @(it->first.c_str());
|
|
|
|
NSString* pathExt = nil;
|
|
|
|
NSString* fileSize = nil;
|
|
|
|
if (level < last)
|
|
|
|
{
|
|
|
|
// This node is a directory.
|
|
|
|
pathExt = NSFileTypeForHFSTypeCode(kGenericFolderIcon);
|
|
|
|
fileSize = @"";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This node is a leaf file.
|
|
|
|
pathExt = pathPart.pathExtension;
|
|
|
|
fileSize = [NSString stringForFileSize:size];
|
|
|
|
}
|
|
|
|
|
|
|
|
[listSection appendFormat:@"<tr><td>%@<img class=\"icon\" src=\"%@\" width=\"%ld\" height=\"%ld\" />%@</td><td class=\"grey\">%@</td></tr>",
|
|
|
|
prefix,
|
|
|
|
generateIconData(pathExt, kIconWidth, allImgProps),
|
|
|
|
kIconWidth,
|
|
|
|
kIconWidth,
|
|
|
|
pathPart,
|
|
|
|
fileSize];
|
|
|
|
}
|
|
|
|
|
|
|
|
curNode = &it->second;
|
|
|
|
level++;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
void CancelPreviewGeneration(void* /*thisInterface*/, QLPreviewRequestRef preview)
|
2012-08-12 12:11:33 +00:00
|
|
|
{
|
|
|
|
// Implement only if supported
|
|
|
|
}
|