2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2014-2022 Transmission authors and contributors.
|
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
// Created by Mitchell Livingston on 1/10/14.
|
2014-01-10 17:50:05 +00:00
|
|
|
|
|
|
|
#import "ShareTorrentFileHelper.h"
|
|
|
|
#import "Controller.h"
|
|
|
|
#import "Torrent.h"
|
|
|
|
|
|
|
|
@implementation ShareTorrentFileHelper
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
+ (ShareTorrentFileHelper*)sharedHelper
|
2014-01-10 17:50:05 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
static ShareTorrentFileHelper* helper;
|
2014-01-10 17:50:05 +00:00
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
helper = [[ShareTorrentFileHelper alloc] init];
|
|
|
|
});
|
|
|
|
return helper;
|
|
|
|
}
|
|
|
|
|
2022-04-29 22:51:40 +00:00
|
|
|
- (NSArray<NSURL*>*)shareTorrentURLs
|
2014-01-10 17:50:05 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSArray* torrents = ((Controller*)NSApp.delegate).selectedTorrents;
|
|
|
|
NSMutableArray* fileURLs = [NSMutableArray arrayWithCapacity:torrents.count];
|
|
|
|
for (Torrent* torrent in torrents)
|
2014-01-10 17:50:05 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* location = torrent.torrentLocation;
|
|
|
|
if (location.length > 0)
|
|
|
|
{
|
|
|
|
[fileURLs addObject:[NSURL fileURLWithPath:location]];
|
2014-01-10 17:50:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return fileURLs;
|
|
|
|
}
|
|
|
|
|
2022-04-29 22:51:40 +00:00
|
|
|
- (NSArray<NSMenuItem*>*)menuItems
|
2014-01-10 17:50:05 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSArray* services = [NSSharingService sharingServicesForItems:self.shareTorrentURLs];
|
|
|
|
NSMutableArray* items = [NSMutableArray arrayWithCapacity:services.count];
|
|
|
|
for (NSSharingService* service in services)
|
2014-01-10 17:50:05 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMenuItem* item = [[NSMenuItem alloc] initWithTitle:service.title // 10.9: change to menuItemTitle
|
|
|
|
action:@selector(performShareAction:)
|
|
|
|
keyEquivalent:@""];
|
2014-01-10 17:50:05 +00:00
|
|
|
item.image = service.image;
|
|
|
|
item.representedObject = service;
|
2021-08-15 09:41:48 +00:00
|
|
|
service.delegate = (Controller*)NSApp.delegate;
|
2014-01-10 17:50:05 +00:00
|
|
|
item.target = self;
|
2021-08-15 09:41:48 +00:00
|
|
|
[items addObject:item];
|
2014-01-10 17:50:05 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2014-01-10 17:50:05 +00:00
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)performShareAction:(NSMenuItem*)item
|
2014-01-10 17:50:05 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSSharingService* service = item.representedObject;
|
|
|
|
[service performWithItems:self.shareTorrentURLs]; // on 10.9, use attachmentFileURLs?
|
2014-01-10 17:50:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|