From 3ecf0dfccd627c1a137bd6b5103f9190e0d96af6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?A=20C=C5=93ur?= Date: Thu, 24 Nov 2022 07:09:32 +0800 Subject: [PATCH] Fix "NSFilenamesPboardType is deprecated" (#4221) --- macosx/Controller.h | 6 ++--- macosx/Controller.mm | 49 +++++++++++++++++++++---------------- macosx/DragOverlayWindow.h | 2 +- macosx/DragOverlayWindow.mm | 2 +- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/macosx/Controller.h b/macosx/Controller.h index fc121f496..1b884e0c1 100644 --- a/macosx/Controller.h +++ b/macosx/Controller.h @@ -28,7 +28,7 @@ typedef NS_ENUM(unsigned int, addType) { // @interface Controller : NSObject -- (void)openFiles:(NSArray*)filenames addType:(addType)type forcePath:(NSString*)path; +- (void)openFiles:(NSArray*)filenames addType:(addType)type forcePath:(NSString*)path; - (void)askOpenConfirmed:(AddWindowController*)addController add:(BOOL)add; - (void)openCreatedFile:(NSNotification*)notification; @@ -52,7 +52,7 @@ typedef NS_ENUM(unsigned int, addType) { // - (IBAction)resumeSelectedTorrents:(id)sender; - (IBAction)resumeAllTorrents:(id)sender; -- (void)resumeTorrents:(NSArray*)torrents; +- (void)resumeTorrents:(NSArray*)torrents; - (IBAction)resumeSelectedTorrentsNoWait:(id)sender; - (IBAction)resumeWaitingTorrents:(id)sender; @@ -84,7 +84,7 @@ typedef NS_ENUM(unsigned int, addType) { // - (IBAction)announceSelectedTorrents:(id)sender; - (IBAction)verifySelectedTorrents:(id)sender; -- (void)verifyTorrents:(NSArray*)torrents; +- (void)verifyTorrents:(NSArray*)torrents; @property(nonatomic, readonly) NSArray* selectedTorrents; diff --git a/macosx/Controller.mm b/macosx/Controller.mm index 3cc812c12..a2984aa41 100644 --- a/macosx/Controller.mm +++ b/macosx/Controller.mm @@ -665,7 +665,7 @@ void onTorrentCompletenessChanged(tr_torrent* tor, tr_completeness status, bool "Main window -> 3rd bottom left button (remove all) tooltip"); [self.fTableView registerForDraggedTypes:@[ kTorrentTableViewDataType ]]; - [self.fWindow registerForDraggedTypes:@[ NSFilenamesPboardType, NSURLPboardType ]]; + [self.fWindow registerForDraggedTypes:@[ NSPasteboardTypeFileURL, NSPasteboardTypeURL ]]; //sort the sort menu items (localization is from strings file) NSMutableArray* sortMenuItems = [NSMutableArray arrayWithCapacity:7]; @@ -1184,12 +1184,12 @@ void onTorrentCompletenessChanged(tr_torrent* tor, tr_completeness status, bool } } -- (void)application:(NSApplication*)app openFiles:(NSArray*)filenames +- (void)application:(NSApplication*)app openFiles:(NSArray*)filenames { [self openFiles:filenames addType:ADD_MANUAL forcePath:nil]; } -- (void)openFiles:(NSArray*)filenames addType:(addType)type forcePath:(NSString*)path +- (void)openFiles:(NSArray*)filenames addType:(addType)type forcePath:(NSString*)path { BOOL deleteTorrentFile, canToggleDelete = NO; switch (type) @@ -1695,7 +1695,7 @@ void onTorrentCompletenessChanged(tr_torrent* tor, tr_completeness status, bool [self resumeTorrents:torrents]; } -- (void)resumeTorrents:(NSArray*)torrents +- (void)resumeTorrents:(NSArray*)torrents { for (Torrent* torrent in torrents) { @@ -3896,25 +3896,31 @@ void onTorrentCompletenessChanged(tr_torrent* tor, tr_completeness status, bool - (NSDragOperation)draggingEntered:(id)info { NSPasteboard* pasteboard = info.draggingPasteboard; - if ([pasteboard.types containsObject:NSFilenamesPboardType]) + if ([pasteboard.types containsObject:NSPasteboardTypeFileURL]) { //check if any torrent files can be added BOOL torrent = NO; - NSArray* files = [pasteboard propertyListForType:NSFilenamesPboardType]; - for (NSString* file in files) + NSArray* files = [pasteboard readObjectsForClasses:@[ NSURL.class ] + options:@{ NSPasteboardURLReadingFileURLsOnlyKey : @YES }]; + for (NSURL* file in files) { - if ([[NSWorkspace.sharedWorkspace typeOfFile:file error:NULL] isEqualToString:@"org.bittorrent.torrent"] || + if ([[NSWorkspace.sharedWorkspace typeOfFile:file.path error:NULL] isEqualToString:@"org.bittorrent.torrent"] || [file.pathExtension caseInsensitiveCompare:@"torrent"] == NSOrderedSame) { torrent = YES; auto metainfo = tr_torrent_metainfo{}; - if (metainfo.parseTorrentFile(file.UTF8String)) + if (metainfo.parseTorrentFile(file.path.UTF8String)) { if (!self.fOverlayWindow) { self.fOverlayWindow = [[DragOverlayWindow alloc] initWithLib:self.fLib forWindow:self.fWindow]; } - [self.fOverlayWindow setTorrents:files]; + NSMutableArray* filesToOpen = [NSMutableArray arrayWithCapacity:files.count]; + for (NSURL* file in files) + { + [filesToOpen addObject:file.path]; + } + [self.fOverlayWindow setTorrents:filesToOpen]; return NSDragOperationCopy; } @@ -3933,7 +3939,7 @@ void onTorrentCompletenessChanged(tr_torrent* tor, tr_completeness status, bool return NSDragOperationCopy; } } - else if ([pasteboard.types containsObject:NSURLPboardType]) + else if ([pasteboard.types containsObject:NSPasteboardTypeURL]) { if (!self.fOverlayWindow) { @@ -3963,23 +3969,24 @@ void onTorrentCompletenessChanged(tr_torrent* tor, tr_completeness status, bool } NSPasteboard* pasteboard = info.draggingPasteboard; - if ([pasteboard.types containsObject:NSFilenamesPboardType]) + if ([pasteboard.types containsObject:NSPasteboardTypeFileURL]) { BOOL torrent = NO, accept = YES; //create an array of files that can be opened - NSArray* files = [pasteboard propertyListForType:NSFilenamesPboardType]; - NSMutableArray* filesToOpen = [NSMutableArray arrayWithCapacity:files.count]; - for (NSString* file in files) + NSArray* files = [pasteboard readObjectsForClasses:@[ NSURL.class ] + options:@{ NSPasteboardURLReadingFileURLsOnlyKey : @YES }]; + NSMutableArray* filesToOpen = [NSMutableArray arrayWithCapacity:files.count]; + for (NSURL* file in files) { - if ([[NSWorkspace.sharedWorkspace typeOfFile:file error:NULL] isEqualToString:@"org.bittorrent.torrent"] || + if ([[NSWorkspace.sharedWorkspace typeOfFile:file.path error:NULL] isEqualToString:@"org.bittorrent.torrent"] || [file.pathExtension caseInsensitiveCompare:@"torrent"] == NSOrderedSame) { torrent = YES; auto metainfo = tr_torrent_metainfo{}; - if (metainfo.parseTorrentFile(file.UTF8String)) + if (metainfo.parseTorrentFile(file.path.UTF8String)) { - [filesToOpen addObject:file]; + [filesToOpen addObject:file.path]; } } } @@ -3992,7 +3999,7 @@ void onTorrentCompletenessChanged(tr_torrent* tor, tr_completeness status, bool { if (!torrent && files.count == 1) { - [CreatorWindowController createTorrentFile:self.fLib forFile:[NSURL fileURLWithPath:files[0]]]; + [CreatorWindowController createTorrentFile:self.fLib forFile:files[0]]; } else { @@ -4002,7 +4009,7 @@ void onTorrentCompletenessChanged(tr_torrent* tor, tr_completeness status, bool return accept; } - else if ([pasteboard.types containsObject:NSURLPboardType]) + else if ([pasteboard.types containsObject:NSPasteboardTypeURL]) { NSURL* url; if ((url = [NSURL URLFromPasteboard:pasteboard])) @@ -4094,7 +4101,7 @@ void onTorrentCompletenessChanged(tr_torrent* tor, tr_completeness status, bool [self.fWindow.toolbar validateVisibleItems]; } -- (NSArray*)quickLookableTorrents +- (NSArray*)quickLookableTorrents { NSArray* selectedTorrents = self.fTableView.selectedTorrents; NSMutableArray* qlArray = [NSMutableArray arrayWithCapacity:selectedTorrents.count]; diff --git a/macosx/DragOverlayWindow.h b/macosx/DragOverlayWindow.h index 515670ae8..66f07faef 100644 --- a/macosx/DragOverlayWindow.h +++ b/macosx/DragOverlayWindow.h @@ -10,7 +10,7 @@ - (instancetype)initWithLib:(tr_session*)lib forWindow:(NSWindow*)window; -- (void)setTorrents:(NSArray*)files; +- (void)setTorrents:(NSArray*)files; - (void)setFile:(NSString*)file; - (void)setURL:(NSString*)url; diff --git a/macosx/DragOverlayWindow.mm b/macosx/DragOverlayWindow.mm index b6cc19309..4d6c707a5 100644 --- a/macosx/DragOverlayWindow.mm +++ b/macosx/DragOverlayWindow.mm @@ -59,7 +59,7 @@ [NSNotificationCenter.defaultCenter removeObserver:self]; } -- (void)setTorrents:(NSArray*)files +- (void)setTorrents:(NSArray*)files { uint64_t size = 0; NSUInteger count = 0;