feat: support multiple URL objects from pasteboard (#6467)

This commit is contained in:
Cœur 2024-01-02 05:35:57 +01:00 committed by GitHub
parent 376d806b2e
commit 6647df9be6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 33 deletions

View File

@ -46,6 +46,8 @@ typedef NS_ENUM(NSUInteger, AddType) { //
- (void)openURL:(NSString*)urlString;
- (IBAction)openURLShowSheet:(id)sender;
- (void)openPasteboard;
@property(nonatomic, readonly) tr_session* sessionHandle;
- (IBAction)createFile:(id)sender;

View File

@ -1680,6 +1680,47 @@ void onTorrentCompletenessChanged(tr_torrent* tor, tr_completeness status, bool
}
}
- (void)openPasteboard
{
// 1. If Pasteboard contains URL objects, we treat those and only those
NSArray<NSURL*>* arrayOfURLs = [NSPasteboard.generalPasteboard readObjectsForClasses:@[ [NSURL class] ] options:nil];
if (arrayOfURLs.count > 0)
{
for (NSURL* url in arrayOfURLs)
{
[self openURL:url.absoluteString];
}
return;
}
// 2. If Pasteboard contains String objects, we'll search for magnet or links
NSArray<NSString*>* arrayOfStrings = [NSPasteboard.generalPasteboard readObjectsForClasses:@[ [NSString class] ] options:nil];
if (arrayOfStrings.count == 0)
{
return;
}
NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
for (NSString* itemString in arrayOfStrings)
{
NSArray<NSString*>* itemLines = [itemString componentsSeparatedByCharactersInSet:NSCharacterSet.newlineCharacterSet];
for (__strong NSString* pbItem in itemLines)
{
pbItem = [pbItem stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
if ([pbItem rangeOfString:@"magnet:" options:(NSAnchoredSearch | NSCaseInsensitiveSearch)].location != NSNotFound)
{
[self openURL:pbItem];
}
else
{
#warning only accept full text?
for (NSTextCheckingResult* result in [detector matchesInString:pbItem options:0 range:NSMakeRange(0, pbItem.length)])
[self openURL:result.URL.absoluteString];
}
}
}
}
- (void)createFile:(id)sender
{
[CreatorWindowController createTorrentFile:self.fLib];

View File

@ -661,39 +661,7 @@ static NSTimeInterval const kToggleProgressSeconds = 0.175;
- (void)paste:(id)sender
{
NSURL* url;
if ((url = [NSURL URLFromPasteboard:NSPasteboard.generalPasteboard]))
{
[self.fController openURL:url.absoluteString];
}
else
{
NSArray<NSString*>* items = [NSPasteboard.generalPasteboard readObjectsForClasses:@[ [NSString class] ] options:nil];
if (!items)
{
return;
}
NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
for (NSString* itemString in items)
{
NSArray<NSString*>* itemLines = [itemString componentsSeparatedByCharactersInSet:NSCharacterSet.newlineCharacterSet];
for (__strong NSString* pbItem in itemLines)
{
pbItem = [pbItem stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
if ([pbItem rangeOfString:@"magnet:" options:(NSAnchoredSearch | NSCaseInsensitiveSearch)].location != NSNotFound)
{
[self.fController openURL:pbItem];
}
else
{
#warning only accept full text?
for (NSTextCheckingResult* result in [detector matchesInString:pbItem options:0
range:NSMakeRange(0, pbItem.length)])
[self.fController openURL:result.URL.absoluteString];
}
}
}
}
[self.fController openPasteboard];
}
- (BOOL)validateMenuItem:(NSMenuItem*)menuItem