Revision: 28638 http://sourceforge.net/p/bibdesk/svn/28638 Author: hofman Date: 2024-01-21 18:02:20 +0000 (Sun, 21 Jan 2024) Log Message: ----------- Move text check to FVUtilities to avoid using icon subclasses outside icon classes implementations
Modified Paths: -------------- trunk/bibdesk_vendorsrc/amaxwell/FileView/FVPreviewer.m trunk/bibdesk_vendorsrc/amaxwell/FileView/FVTextIcon.m trunk/bibdesk_vendorsrc/amaxwell/FileView/FVUtilities.h trunk/bibdesk_vendorsrc/amaxwell/FileView/FVUtilities.m Modified: trunk/bibdesk_vendorsrc/amaxwell/FileView/FVPreviewer.m =================================================================== --- trunk/bibdesk_vendorsrc/amaxwell/FileView/FVPreviewer.m 2024-01-21 17:42:39 UTC (rev 28637) +++ trunk/bibdesk_vendorsrc/amaxwell/FileView/FVPreviewer.m 2024-01-21 18:02:20 UTC (rev 28638) @@ -44,7 +44,7 @@ #import <WebKit/WebKit.h> #import <pthread.h> #import "_FVPreviewerWindow.h" -#import "FVTextIcon.h" // for NSAttributedString initialization check +#import "FVUtilities.h" // for NSAttributedString initialization check NSString * const FVPreviewerWillCloseNotification = @"FVPreviewerWillCloseNotification"; @@ -100,7 +100,7 @@ else if (UTTypeConformsTo((__bridge CFStringRef)theUTI, kUTTypePDF) || UTTypeConformsTo((__bridge CFStringRef)theUTI, FVSTR("com.adobe.postscript"))) { return NO; } - else if ([FVTextIcon canInitWithURL:aURL withType:theUTI]) { + else if (FVCanInitTextWithURLOfType(aURL, theUTI)) { NSAttributedString *string = [[NSAttributedString alloc] initWithURL:aURL options:@{} documentAttributes:NULL error:NULL]; return (string == nil); } @@ -373,7 +373,7 @@ } } */ - else if ([FVTextIcon canInitWithURL:representedURL withType:theUTI]) { + else if (FVCanInitTextWithURLOfType(representedURL, theUTI)) { theView = textView; NSDictionary *attrs; NSAttributedString *string = [[NSAttributedString alloc] initWithURL:representedURL options:@{} documentAttributes:&attrs error:NULL]; Modified: trunk/bibdesk_vendorsrc/amaxwell/FileView/FVTextIcon.m =================================================================== --- trunk/bibdesk_vendorsrc/amaxwell/FileView/FVTextIcon.m 2024-01-21 17:42:39 UTC (rev 28637) +++ trunk/bibdesk_vendorsrc/amaxwell/FileView/FVTextIcon.m 2024-01-21 18:02:20 UTC (rev 28638) @@ -38,36 +38,13 @@ #import "FVTextIcon.h" #import "FVIcon_Private.h" +#import "FVUtilities.h" @implementation FVTextIcon -// This should be very reliable, but in practice it's only as reliable as the UTI declaration. For instance, OmniGraffle declares .graffle files as public.composite-content and public.xml in its Info.plist. Since we see that it's public.xml (which is in this list), we open it as text, and it will actually open with NSAttributedString...and display as binary garbage. + (BOOL)canInitWithURL:(NSURL *)aURL withType:(NSString *)type { - /* - Problems here. TextMate claims a lot of plain text types but doesn't declare a UTI for any of them, - so I end up with a dynamic UTI, and Spotlight/Quick Look ignore the files since they have no idea of - conformance to plain text. That's broken behavior on TextMate's part, and it sucks for my purposes. - Additionally, files that are named "README" are public.data, but actually plain text files. Since - LS doesn't sniff types, we'll just try to open anything that's equal (not conforming) to public.data. - */ - if (type == nil || UTTypeEqual((__bridge CFStringRef)type, kUTTypeData)) { - // This is mainly useful to prove that the file cannot be opened; as in the case of OmniGraffle files (see comment above), it returns YES. - NSAttributedString *attributedString = [[NSAttributedString alloc] initWithURL:aURL options:@{} documentAttributes:NULL error:NULL]; - BOOL canInit = (nil != attributedString); - return canInit; - } - - static NSArray *types = nil; - if (nil == types) { - types = [[NSAttributedString textUnfilteredTypes] copy]; - } - - NSUInteger cnt = [types count]; - while (cnt--) - if (UTTypeConformsTo((__bridge CFStringRef)type, (__bridge CFStringRef)[types objectAtIndex:cnt])) - return YES; - return NO; + return FVCanInitTextWithURLOfType(aURL, type); } - (id)initWithURL:(NSURL *)aURL drawsLinkBadge:(BOOL)drawsLinkBadge isPlainText:(BOOL)isPlainText Modified: trunk/bibdesk_vendorsrc/amaxwell/FileView/FVUtilities.h =================================================================== --- trunk/bibdesk_vendorsrc/amaxwell/FileView/FVUtilities.h 2024-01-21 17:42:39 UTC (rev 28637) +++ trunk/bibdesk_vendorsrc/amaxwell/FileView/FVUtilities.h 2024-01-21 18:02:20 UTC (rev 28638) @@ -89,6 +89,13 @@ FV_PRIVATE_EXTERN bool FVCanMapFileAtURL(NSURL *fileURL); /** @internal + Returns whether we can create an NSAttributedString from the URL with the UTI. + @param fileURL the URL to check. + @param type the UTI to check. + @return true if it's safe to mmap(2) the file. */ +FV_PRIVATE_EXTERN BOOL FVCanInitTextWithURLOfType(NSURL *aURL, NSString *type); + +/** @internal Run a block with the current appearance set to the effective appearance of the object. */ FV_PRIVATE_EXTERN void FVRunWithAppearance(id object, void (^code)(void)); Modified: trunk/bibdesk_vendorsrc/amaxwell/FileView/FVUtilities.m =================================================================== --- trunk/bibdesk_vendorsrc/amaxwell/FileView/FVUtilities.m 2024-01-21 17:42:39 UTC (rev 28637) +++ trunk/bibdesk_vendorsrc/amaxwell/FileView/FVUtilities.m 2024-01-21 18:02:20 UTC (rev 28638) @@ -262,6 +262,35 @@ } #endif +// This should be very reliable, but in practice it's only as reliable as the UTI declaration. For instance, OmniGraffle declares .graffle files as public.composite-content and public.xml in its Info.plist. Since we see that it's public.xml (which is in this list), we open it as text, and it will actually open with NSAttributedString...and display as binary garbage. +FV_PRIVATE_EXTERN BOOL FVCanInitTextWithURLOfType(NSURL *aURL, NSString *type) +{ + /* + Problems here. TextMate claims a lot of plain text types but doesn't declare a UTI for any of them, + so I end up with a dynamic UTI, and Spotlight/Quick Look ignore the files since they have no idea of + conformance to plain text. That's broken behavior on TextMate's part, and it sucks for my purposes. + Additionally, files that are named "README" are public.data, but actually plain text files. Since + LS doesn't sniff types, we'll just try to open anything that's equal (not conforming) to public.data. + */ + if (type == nil || UTTypeEqual((__bridge CFStringRef)type, kUTTypeData)) { + // This is mainly useful to prove that the file cannot be opened; as in the case of OmniGraffle files (see comment above), it returns YES. + NSAttributedString *attributedString = [[NSAttributedString alloc] initWithURL:aURL options:@{} documentAttributes:NULL error:NULL]; + BOOL canInit = (nil != attributedString); + return canInit; + } + + static NSArray *types = nil; + if (nil == types) { + types = [[NSAttributedString textUnfilteredTypes] copy]; + } + + NSUInteger cnt = [types count]; + while (cnt--) + if (UTTypeConformsTo((__bridge CFStringRef)type, (__bridge CFStringRef)[types objectAtIndex:cnt])) + return YES; + return NO; +} + #pragma mark Dark mode support #if !defined(MAC_OS_X_VERSION_10_9) || MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_9 This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. _______________________________________________ Bibdesk-commit mailing list Bibdesk-commit@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/bibdesk-commit