Revision: 27373
          http://sourceforge.net/p/bibdesk/svn/27373
Author:   hofman
Date:     2022-04-15 14:25:23 +0000 (Fri, 15 Apr 2022)
Log Message:
-----------
Cache file wrappers for image attachments for icons for templates, so multiple 
icons can share the same attachment image

Modified Paths:
--------------
    trunk/bibdesk/NSURL_BDSKExtensions.m

Modified: trunk/bibdesk/NSURL_BDSKExtensions.m
===================================================================
--- trunk/bibdesk/NSURL_BDSKExtensions.m        2022-04-15 14:11:09 UTC (rev 
27372)
+++ trunk/bibdesk/NSURL_BDSKExtensions.m        2022-04-15 14:25:23 UTC (rev 
27373)
@@ -398,79 +398,109 @@
     return [[[NSAttributedString alloc] initWithString:([self isFileURL] ? 
[[NSFileManager defaultManager] displayNameAtPath:[self path]] : [self 
absoluteString]) attributeName:NSLinkAttributeName attributeValue:self] 
autorelease];
 }
 
+static NSString *fileTypeOfURL(NSURL *url) {
+    NSString *type = nil;
+    if ([url isFileURL]) {
+        type = [[NSWorkspace sharedWorkspace] typeOfFile:[url path] 
error:NULL];
+    } else {
+        type = [(NSString 
*)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, 
(CFStringRef)[url pathExtension], NULL) autorelease];
+    }
+    return type ?: @"";
+}
+
+static NSFileWrapper *fileWrapperForFileType(NSString *type) {
+    static NSMutableDictionary *typeIconWrappers = nil;
+    NSFileWrapper *wrapper = [typeIconWrappers objectForKey:type];
+    if (wrapper == nil) {
+        NSImage *image = [[NSWorkspace sharedWorkspace] iconForFileType:type];
+        if (image == nil)
+            image = [[NSWorkspace sharedWorkspace] 
iconForFileType:NSFileTypeForHFSTypeCode(kGenericDocumentIcon)];
+        if (typeIconWrappers == nil)
+            typeIconWrappers = [[NSMutableDictionary alloc] init];
+        NSString *name = [type stringByAppendingPathExtension:@"tiff"];
+        wrapper = [[NSFileWrapper alloc] initRegularFileWithContents:[image 
TIFFRepresentation]];
+        [wrapper setFilename:name];
+        [wrapper setPreferredFilename:name];
+        [typeIconWrappers setObject:wrapper forKey:type];
+        [wrapper release];
+    }
+    return wrapper;
+}
+
+static NSFileWrapper *smallFileWrapperForFileType(NSString *type) {
+    static NSMutableDictionary *typeIconWrappers = nil;
+    NSFileWrapper *wrapper = [typeIconWrappers objectForKey:type];
+    if (wrapper == nil) {
+        NSImage *fileImage = [[NSWorkspace sharedWorkspace] 
iconForFileType:type];
+        if (fileImage == nil)
+            fileImage = [[NSWorkspace sharedWorkspace] 
iconForFileType:NSFileTypeForHFSTypeCode(kGenericDocumentIcon)];
+        NSImage *image = [[NSImage alloc] initBitmapWithSize:NSMakeSize(16.0, 
16.0) drawingHandler:^(NSRect rect){
+            [fileImage drawInRect:NSMakeRect(0.0, 0.0, 16.0, 16.0) 
fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0];
+        }];
+        if (typeIconWrappers == nil)
+            typeIconWrappers = [[NSMutableDictionary alloc] init];
+        NSString *name = [type stringByAppendingPathExtension:@"tiff"];
+        wrapper = [[NSFileWrapper alloc] initRegularFileWithContents:[image 
TIFFRepresentation]];
+        [wrapper setFilename:name];
+        [wrapper setPreferredFilename:name];
+        [typeIconWrappers setObject:wrapper forKey:type];
+        [wrapper release];
+        [image release];
+    }
+    return wrapper;
+}
+
 - (NSAttributedString *)icon {
-    NSImage *image = [NSImage imageForURL:self];
-    NSString *name = ([self isFileURL]) ? [self path] : [self relativeString];
-    name = [[[name lastPathComponent] stringByDeletingPathExtension] 
stringByAppendingPathExtension:@"tiff"];
-    
-    NSFileWrapper *wrapper = [[NSFileWrapper alloc] 
initRegularFileWithContents:[image TIFFRepresentation]];
-    [wrapper setFilename:name];
-    [wrapper setPreferredFilename:name];
-
-    NSTextAttachment *attachment = [[NSTextAttachment alloc] 
initWithFileWrapper:wrapper];
-    [wrapper release];
-    NSAttributedString *attrString = [NSAttributedString 
attributedStringWithAttachment:attachment];
-    [attachment release];
-    
+    NSAttributedString *attrString = nil;
+    NSFileWrapper *wrapper = fileWrapperForFileType(fileTypeOfURL(self));
+    if (wrapper) {
+        NSTextAttachment *attachment = [[NSTextAttachment alloc] 
initWithFileWrapper:wrapper];
+        [wrapper release];
+        attrString = [NSAttributedString 
attributedStringWithAttachment:attachment];
+        [attachment release];
+        
+    }
     return attrString;
 }
 
 - (NSAttributedString *)smallIcon {
-    NSImage *image = [[NSImage alloc] initBitmapWithSize:NSMakeSize(16, 16) 
drawingHandler:^(NSRect rect){
-        [[NSImage imageForURL:self] drawInRect:rect fromRect:NSZeroRect 
operation:NSCompositeCopy fraction:1.0];
-    }];
-    NSString *name = ([self isFileURL]) ? [self path] : [self relativeString];
-    name = [[[name lastPathComponent] stringByDeletingPathExtension] 
stringByAppendingPathExtension:@"tiff"];
-    
-    NSFileWrapper *wrapper = [[NSFileWrapper alloc] 
initRegularFileWithContents:[image TIFFRepresentation]];
-    [wrapper setFilename:name];
-    [wrapper setPreferredFilename:name];
-    [image release];
-    
-    NSTextAttachment *attachment = [[NSTextAttachment alloc] 
initWithFileWrapper:wrapper];
-    [wrapper release];
-    NSAttributedString *attrString = [NSAttributedString 
attributedStringWithAttachment:attachment];
-    [attachment release];
-    
+    NSAttributedString *attrString = nil;
+    NSFileWrapper *wrapper = smallFileWrapperForFileType(fileTypeOfURL(self));
+    if (wrapper) {
+        NSTextAttachment *attachment = [[NSTextAttachment alloc] 
initWithFileWrapper:wrapper];
+        [wrapper release];
+        attrString = [NSAttributedString 
attributedStringWithAttachment:attachment];
+        [attachment release];
+        
+    }
     return attrString;
 }
 
 - (NSAttributedString *)linkedIcon {
-    NSImage *image = [NSImage imageForURL:self];
-    NSString *name = ([self isFileURL]) ? [self path] : [self relativeString];
-    name = [[[name lastPathComponent] stringByDeletingPathExtension] 
stringByAppendingPathExtension:@"tiff"];
-    
-    NSFileWrapper *wrapper = [[NSFileWrapper alloc] 
initRegularFileWithContents:[image TIFFRepresentation]];
-    [wrapper setFilename:name];
-    [wrapper setPreferredFilename:name];
-
-    NSTextAttachment *attachment = [[NSTextAttachment alloc] 
initWithFileWrapper:wrapper];
-    [wrapper release];
-    NSMutableAttributedString *attrString = [[NSAttributedString 
attributedStringWithAttachment:attachment] mutableCopy];
-    [attachment release];
+    NSMutableAttributedString *attrString = nil;
+    NSFileWrapper *wrapper = fileWrapperForFileType(fileTypeOfURL(self));
+    if (wrapper) {
+        NSTextAttachment *attachment = [[NSTextAttachment alloc] 
initWithFileWrapper:wrapper];
+        [wrapper release];
+        attrString = [[NSAttributedString 
attributedStringWithAttachment:attachment] mutableCopy];
+        [attachment release];
+        
+    }
     [attrString addAttribute:NSLinkAttributeName value:self 
range:NSMakeRange(0, [attrString length])];
-    
     return [attrString autorelease];
 }
 
 - (NSAttributedString *)linkedSmallIcon {
-    NSImage *image = [[NSImage alloc] initBitmapWithSize:NSMakeSize(16, 16) 
drawingHandler:^(NSRect rect){
-        [[NSImage imageForURL:self] drawInRect:rect fromRect:NSZeroRect 
operation:NSCompositeCopy fraction:1.0];
-    }];
-    NSString *name = ([self isFileURL]) ? [self path] : [self relativeString];
-    name = [[[name lastPathComponent] stringByDeletingPathExtension] 
stringByAppendingPathExtension:@"tiff"];
-    
-    NSFileWrapper *wrapper = [[NSFileWrapper alloc] 
initRegularFileWithContents:[image TIFFRepresentation]];
-    [wrapper setFilename:name];
-    [wrapper setPreferredFilename:name];
-    [image release];
-    
-    NSTextAttachment *attachment = [[NSTextAttachment alloc] 
initWithFileWrapper:wrapper];
-    [wrapper release];
-    NSMutableAttributedString *attrString = [[NSAttributedString 
attributedStringWithAttachment:attachment] mutableCopy];
-    [attachment release];
+    NSMutableAttributedString *attrString = nil;
+    NSFileWrapper *wrapper = smallFileWrapperForFileType(fileTypeOfURL(self));
+    if (wrapper) {
+        NSTextAttachment *attachment = [[NSTextAttachment alloc] 
initWithFileWrapper:wrapper];
+        [wrapper release];
+        attrString = [[NSAttributedString 
attributedStringWithAttachment:attachment] mutableCopy];
+        [attachment release];
+        
+    }
     [attrString addAttribute:NSLinkAttributeName value:self 
range:NSMakeRange(0, [attrString length])];
-    
     return [attrString autorelease];
 }
 

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to