// // BPAttachment.m // Piles3 // // Created by Bea on 14/07/05. // Copyright 2005 __MyCompanyName__. All rights reserved. // #import "BPAttachment.h" #import "BPIconManager.h" #import "BPUtil.h" #import #import @implementation BPAttachment + (void)initialize { defaultFilePath = @""; } + (NSImage *)iconForData:(NSData *)data withFilenameExt:(NSString *)fileExt { // if the data is known to contain an image, use the image as the icon // PDFs thumbnails don't turn out very well (they may just show blank spots // in the PDF? anyway, sometimes they look blank) so don't render them as images BOOL isPDF = ([fileExt caseInsensitiveCompare:@"pdf"] == NSOrderedSame); if (!isPDF && [[NSImage imageFileTypes] containsObject:fileExt]) { NSImage *dataAsImage = [[NSImage alloc] initWithData:data]; if (dataAsImage != nil && [dataAsImage isValid]) { [dataAsImage autorelease]; return dataAsImage; } } // otherwise, use the icon for the file type return [BPIconManager iconForFileType:fileExt]; } + (id)attachmentWithMimePart:(MimePart *)theMimePart { return [[[BPAttachment alloc] initWithMimePart:theMimePart] autorelease]; } - (void)compressIcon { if (icon != nil) { NSData *compressedData = [icon TIFFRepresentation]; NSImage *compressedIcon = [[NSImage alloc] initWithData:compressedData]; [icon release]; icon = compressedIcon; } } - (id)initWithName:(NSString *)aName icon:(NSImage *)anIcon size:(int)theSize data:(NSData *)theData filePath:(NSString *)filePath { [super init]; [aName retain]; name = aName; // make icon if one wasn't given if (anIcon == nil) { anIcon = [BPAttachment iconForData:theData withFilenameExt:[aName pathExtension]]; } [anIcon retain]; icon = anIcon; [self compressIcon]; size = theSize; humanReadableSize = [[BPUtil formattedFileSizeForBytes:size] retain]; // save the file somewhere // have to do this cos couldn't open attachment data in encoded/decoded BPAttachment if (filePath == nil) { NSString *savedPath = [defaultFilePath stringByAppendingPathComponent:name]; if ([theData writeToFile:savedPath atomically:YES]) { savedFilePath = [savedPath retain]; } else { NSLog(@"BPAttachment init error:" "couldn't save attachment %@ to path %@", self, savedPath); } } else { savedFilePath = [filePath retain]; } return self; } - (id)initWithName:(NSString *)aName icon:(NSImage *)anIcon size:(int)theSize data:(NSData *)theData { return [self initWithName:aName icon:anIcon size:theSize data:theData filePath:nil]; } - (id)initWithMimePart:(MimePart *)theMimePart { return [self initWithName:[theMimePart attachmentFilename] icon:nil size:(int)[theMimePart approximateRawSize] data:[theMimePart bodyData]]; } - (NSString *)name { return name; } - (int)size; { return size; } - (NSString *)humanReadableSize { return humanReadableSize; } - (NSString *)filePath { return savedFilePath; } - (void)setIcon:(NSImage *)anIcon { [anIcon retain]; [icon release]; icon = anIcon; } - (NSImage *)icon { return icon; } - (void)openInDefaultApp { if (savedFilePath != nil) { [[NSWorkspace sharedWorkspace] openTempFile:savedFilePath]; } } - (void)dealloc { NSLog(@"Destroying attachment"); [name release]; [icon release]; //[fileData release]; [humanReadableSize release]; [savedFilePath release]; [super dealloc]; } #pragma - #pragma archival - (void)encodeWithCoder:(NSCoder *)coder { [coder encodeObject:name forKey:@"name"]; [coder encodeObject:icon forKey:@"icon"]; [coder encodeInt:size forKey:@"size"]; //[coder encodeObject:fileData forKey:@"fileData"]; [coder encodeObject:humanReadableSize forKey:@"humanReadableSize"]; [coder encodeObject:savedFilePath forKey:@"savedFilePath"]; } - (id)initWithCoder:(NSCoder *)coder { [super init]; name = [[coder decodeObjectForKey:@"name"] retain]; [self setIcon:[coder decodeObjectForKey:@"icon"]]; size = [coder decodeIntForKey:@"size"]; //fileData = [[coder decodeObjectForKey:@"fileData"] retain]; humanReadableSize = [[coder decodeObjectForKey:@"humanReadableSize"] retain]; savedFilePath = [[coder decodeObjectForKey:@"savedFilePath"] retain]; return self; } @end