Attempt to clean up 10.13+ stuff with fallbacks to the old ways

CQTexperiment
Christopher Snowhill 2021-09-23 00:49:51 -07:00
parent 505061031e
commit 217b3d4cf9
5 changed files with 88 additions and 20 deletions

View File

@ -151,7 +151,12 @@ static NSURL *defaultMusicDirectory(void) {
- (id <NSPasteboardWriting>)outlineView:(NSOutlineView *)outlineView pasteboardWriterForItem:(id)item {
NSPasteboardItem *paste = [[NSPasteboardItem alloc] init];
[paste setData:[[item URL] dataRepresentation] forType:NSPasteboardTypeFileURL];
if (@available(macOS 10.13, *)) {
[paste setData:[[item URL] dataRepresentation] forType:NSPasteboardTypeFileURL];
}
else {
[paste setPropertyList:@[[item URL]] forType:NSFilenamesPboardType];
}
return paste;
}

View File

@ -12,9 +12,16 @@ NSString *iTunesDropType = @"com.apple.tv.metadata";
- (void)awakeFromNib {
[super awakeFromNib];
// register for drag and drop
NSPasteboardType fileType;
if (@available(macOS 10.13, *)) {
fileType = NSPasteboardTypeFileURL;
}
else {
fileType = NSFilenamesPboardType;
}
[self.tableView registerForDraggedTypes:@[CogDNDIndexType,
CogUrlsPboardType,
NSPasteboardTypeFileURL,
fileType,
iTunesDropType]];
}

View File

@ -184,7 +184,12 @@
PlaylistEntry *song = [[self arrangedObjects] objectAtIndex:row];
[filenames addObject:[[song path] stringByExpandingTildeInPath]];
[item setData:[song.URL dataRepresentation] forType:NSPasteboardTypeFileURL];
if (@available(macOS 10.13, *)) {
[item setData:[song.URL dataRepresentation] forType:NSPasteboardTypeFileURL];
}
else {
[item setPropertyList:@[song.URL] forType:NSFilenamesPboardType];
}
return item;
}
@ -199,8 +204,15 @@
if (row < 0) row = 0;
// Determine the type of object that was dropped
NSPasteboardType fileType;
if (@available(macOS 10.13, *)) {
fileType = NSPasteboardTypeFileURL;
}
else {
fileType = NSFilenamesPboardType;
}
NSArray *supportedTypes =
@[CogUrlsPboardType, NSPasteboardTypeFileURL, iTunesDropType];
@[CogUrlsPboardType, fileType, iTunesDropType];
NSPasteboard *pboard = [info draggingPasteboard];
NSString *bestType = [pboard availableTypeFromArray:supportedTypes];
@ -216,8 +228,15 @@
fromData:data
error:&error];
} else {
NSSet *allowed = [NSSet setWithArray:@[[NSArray class], [NSURL class]]];
urls = [NSKeyedUnarchiver unarchivedObjectOfClasses:allowed fromData:data error:&error];
if (@available(macOS 10.13, *)) {
NSSet *allowed = [NSSet setWithArray:@[[NSArray class], [NSURL class]]];
urls = [NSKeyedUnarchiver unarchivedObjectOfClasses:allowed
fromData:data
error:&error];
}
else {
urls = [NSUnarchiver unarchiveObjectWithData:data];
}
}
if (!urls) {
DLog(@"%@", error);
@ -229,7 +248,7 @@
}
// Get files from a normal file drop (such as from Finder)
if ([bestType isEqualToString:NSPasteboardTypeFileURL]) {
if ([bestType isEqualToString:fileType]) {
NSArray<Class> *classes = @[[NSURL class]];
NSDictionary *options = @{};
NSArray<NSURL*> *files = [pboard readObjectsForClasses:classes options:options];

View File

@ -225,9 +225,16 @@
}
NSError *error;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:selectedURLs
requiringSecureCoding:YES
error:&error];
NSData *data;
if (@available(macOS 10.13, *)) {
data = [NSKeyedArchiver archivedDataWithRootObject:selectedURLs
requiringSecureCoding:YES
error:&error];
}
else {
data = [NSArchiver archivedDataWithRootObject:selectedURLs];
}
if (!data) {
DLog(@"Error: %@", error);
}
@ -267,7 +274,14 @@
- (IBAction)paste:(id)sender {
// Determine the type of object that was dropped
NSArray *supportedTypes = @[CogUrlsPboardType, NSPasteboardTypeFileURL, iTunesDropType];
NSPasteboardType fileType;
if (@available(macOS 10.13, *)) {
fileType = NSPasteboardTypeFileURL;
}
else {
fileType = NSFilenamesPboardType;
}
NSArray *supportedTypes = @[CogUrlsPboardType, fileType, iTunesDropType];
NSPasteboard *pboard = [NSPasteboard generalPasteboard];
NSPasteboardType bestType = [pboard availableTypeFromArray:supportedTypes];
#ifdef _DEBUG
@ -294,8 +308,15 @@
fromData:data
error:&error];
} else {
NSSet *allowed = [NSSet setWithArray:@[[NSArray class], [NSURL class]]];
urls = [NSKeyedUnarchiver unarchivedObjectOfClasses:allowed fromData:data error:&error];
if (@available(macOS 10.13, *)) {
NSSet *allowed = [NSSet setWithArray:@[[NSArray class], [NSURL class]]];
urls = [NSKeyedUnarchiver unarchivedObjectOfClasses:allowed
fromData:data
error:&error];
}
else {
urls = [NSUnarchiver unarchiveObjectWithData:data];
}
}
if (!urls) {
DLog(@"%@", error);
@ -307,10 +328,10 @@
}
// Get files from a normal file drop (such as from Finder)
if ([bestType isEqualToString:NSPasteboardTypeFileURL]) {
if ([bestType isEqualToString:fileType]) {
NSMutableArray *urls = [[NSMutableArray alloc] init];
for (NSString *file in [pboard propertyListForType:NSPasteboardTypeFileURL]) {
for (NSString *file in [pboard propertyListForType:fileType]) {
[urls addObject:[NSURL fileURLWithPath:file]];
}
@ -364,8 +385,16 @@
}
if (action == @selector(paste:)) {
NSPasteboard *pboard = [NSPasteboard generalPasteboard];
NSPasteboardType fileType;
if (@available(macOS 10.13, *)) {
fileType = NSPasteboardTypeFileURL;
}
else {
fileType = NSFilenamesPboardType;
}
NSArray *supportedTypes = @[CogUrlsPboardType, NSPasteboardTypeFileURL, iTunesDropType];
NSArray *supportedTypes = @[CogUrlsPboardType, fileType, iTunesDropType];
NSString *bestType = [pboard availableTypeFromArray:supportedTypes];

View File

@ -20,10 +20,18 @@
NSArray *urls = [[self selectedObjects]valueForKey:@"URL"];
NSError *error = nil;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:urls
requiringSecureCoding:YES
error:&error];
if (error) return NO;
NSData *data;
if (@available(macOS 10.13, *))
{
data = [NSKeyedArchiver archivedDataWithRootObject:urls
requiringSecureCoding:YES
error:&error];
if (error) return NO;
}
else
{
data = [NSArchiver archivedDataWithRootObject:urls];
}
[pboard declareTypes:[NSArray arrayWithObjects:CogUrlsPboardType,nil] owner:nil]; //add it to pboard
[pboard setData:data forType:CogUrlsPboardType];