From 6647df9be6c76704ebfcac2b55a5e541387d9869 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C5=93ur?= Date: Tue, 2 Jan 2024 05:35:57 +0100 Subject: [PATCH] feat: support multiple URL objects from pasteboard (#6467) --- macosx/Controller.h | 2 ++ macosx/Controller.mm | 41 ++++++++++++++++++++++++++++++++++++++ macosx/TorrentTableView.mm | 34 +------------------------------ 3 files changed, 44 insertions(+), 33 deletions(-) diff --git a/macosx/Controller.h b/macosx/Controller.h index 953a277be..34debc17d 100644 --- a/macosx/Controller.h +++ b/macosx/Controller.h @@ -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; diff --git a/macosx/Controller.mm b/macosx/Controller.mm index 687db0551..2c7897082 100644 --- a/macosx/Controller.mm +++ b/macosx/Controller.mm @@ -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* 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* 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* 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]; diff --git a/macosx/TorrentTableView.mm b/macosx/TorrentTableView.mm index aa8be98fb..c137b6e46 100644 --- a/macosx/TorrentTableView.mm +++ b/macosx/TorrentTableView.mm @@ -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* items = [NSPasteboard.generalPasteboard readObjectsForClasses:@[ [NSString class] ] options:nil]; - if (!items) - { - return; - } - NSDataDetector* detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil]; - for (NSString* itemString in items) - { - NSArray* 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