| 1 |
/* $Id$ */ |
|---|
| 2 |
|
|---|
| 3 |
/* |
|---|
| 4 |
* Copyright (c) 2003-2007 Axel Andersson |
|---|
| 5 |
* All rights reserved. |
|---|
| 6 |
* |
|---|
| 7 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 8 |
* modification, are permitted provided that the following conditions |
|---|
| 9 |
* are met: |
|---|
| 10 |
* 1. Redistributions of source code must retain the above copyright |
|---|
| 11 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 13 |
* notice, this list of conditions and the following disclaimer in the |
|---|
| 14 |
* documentation and/or other materials provided with the distribution. |
|---|
| 15 |
* |
|---|
| 16 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|---|
| 17 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|---|
| 18 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|---|
| 19 |
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
|---|
| 20 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|---|
| 21 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|---|
| 22 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 23 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|---|
| 24 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|---|
| 25 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|---|
| 26 |
* POSSIBILITY OF SUCH DAMAGE. |
|---|
| 27 |
*/ |
|---|
| 28 |
|
|---|
| 29 |
#import "FHCache.h" |
|---|
| 30 |
#import "FHImage.h" |
|---|
| 31 |
|
|---|
| 32 |
@implementation FHCache |
|---|
| 33 |
|
|---|
| 34 |
+ (FHCache *)cache { |
|---|
| 35 |
static FHCache *sharedCache; |
|---|
| 36 |
|
|---|
| 37 |
if(!sharedCache) |
|---|
| 38 |
sharedCache = [[self alloc] init]; |
|---|
| 39 |
|
|---|
| 40 |
return sharedCache; |
|---|
| 41 |
} |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
- (id)init { |
|---|
| 46 |
self = [super init]; |
|---|
| 47 |
|
|---|
| 48 |
_directoryIcon = [[[NSWorkspace sharedWorkspace] iconForFile:@"/tmp"] retain]; |
|---|
| 49 |
[_directoryIcon setSize:NSMakeSize(128.0, 128.0)]; |
|---|
| 50 |
|
|---|
| 51 |
_fileIcons = [[NSMutableDictionary alloc] initWithCapacity:1000]; |
|---|
| 52 |
_largeFileIcons = [[NSMutableDictionary alloc] initWithCapacity:1000]; |
|---|
| 53 |
_thumbnails = [[NSMutableDictionary alloc] initWithCapacity:500]; |
|---|
| 54 |
|
|---|
| 55 |
return self; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
- (void)dealloc { |
|---|
| 61 |
[_directoryIcon release]; |
|---|
| 62 |
[_fileIcons release]; |
|---|
| 63 |
[_largeFileIcons release]; |
|---|
| 64 |
[_thumbnails release]; |
|---|
| 65 |
|
|---|
| 66 |
[super dealloc]; |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
#pragma mark - |
|---|
| 72 |
|
|---|
| 73 |
- (NSImage *)directoryIcon { |
|---|
| 74 |
return _directoryIcon; |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
#pragma mark - |
|---|
| 80 |
|
|---|
| 81 |
- (NSImage *)fileIconForURL:(WIURL *)url { |
|---|
| 82 |
NSImage *image; |
|---|
| 83 |
WIURL *faviconURL; |
|---|
| 84 |
|
|---|
| 85 |
image = [_fileIcons objectForKey:[url string]]; |
|---|
| 86 |
|
|---|
| 87 |
if(!image) { |
|---|
| 88 |
if([url isFileURL]) { |
|---|
| 89 |
image = [[NSWorkspace sharedWorkspace] iconForFile:[url path]]; |
|---|
| 90 |
} else { |
|---|
| 91 |
faviconURL = [[url copy] autorelease]; |
|---|
| 92 |
[faviconURL setPath:@"/favicon.ico"]; |
|---|
| 93 |
|
|---|
| 94 |
image = [[[NSImage alloc] initWithContentsOfURL:[faviconURL URL]] autorelease]; |
|---|
| 95 |
|
|---|
| 96 |
if(!image) |
|---|
| 97 |
image = [NSImage imageNamed:@"URL"]; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
if(image) { |
|---|
| 101 |
if([_fileIcons count] > 1000) |
|---|
| 102 |
[_fileIcons removeObjectForKey:[[_fileIcons allKeys] objectAtIndex:0]]; |
|---|
| 103 |
|
|---|
| 104 |
[_fileIcons setObject:image forKey:[url string]]; |
|---|
| 105 |
} |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
return image; |
|---|
| 109 |
} |
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
- (NSImage *)largeFileIconForExtension:(NSString *)extension { |
|---|
| 114 |
NSImage *image; |
|---|
| 115 |
|
|---|
| 116 |
image = [_largeFileIcons objectForKey:extension]; |
|---|
| 117 |
|
|---|
| 118 |
if(!image) { |
|---|
| 119 |
image = [[NSWorkspace sharedWorkspace] iconForFileType:extension]; |
|---|
| 120 |
|
|---|
| 121 |
if(image) { |
|---|
| 122 |
[image setSize:NSMakeSize(128.0, 128.0)]; |
|---|
| 123 |
|
|---|
| 124 |
if([_largeFileIcons count] > 1000) |
|---|
| 125 |
[_largeFileIcons removeObjectForKey:[[_largeFileIcons allKeys] objectAtIndex:0]]; |
|---|
| 126 |
|
|---|
| 127 |
[_largeFileIcons setObject:image forKey:extension]; |
|---|
| 128 |
} |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
return image; |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
- (NSImage *)largeFileIconForURL:(WIURL *)url { |
|---|
| 137 |
NSImage *image; |
|---|
| 138 |
|
|---|
| 139 |
image = [_largeFileIcons objectForKey:[url string]]; |
|---|
| 140 |
|
|---|
| 141 |
if(!image) { |
|---|
| 142 |
image = [[NSWorkspace sharedWorkspace] iconForFile:[url path]]; |
|---|
| 143 |
|
|---|
| 144 |
if(image) { |
|---|
| 145 |
[image setSize:NSMakeSize(128.0, 128.0)]; |
|---|
| 146 |
|
|---|
| 147 |
if([_largeFileIcons count] > 1000) |
|---|
| 148 |
[_largeFileIcons removeObjectForKey:[[_largeFileIcons allKeys] objectAtIndex:0]]; |
|---|
| 149 |
|
|---|
| 150 |
[_largeFileIcons setObject:image forKey:[url string]]; |
|---|
| 151 |
} |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
return image; |
|---|
| 155 |
} |
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
#pragma mark - |
|---|
| 160 |
|
|---|
| 161 |
- (FHImage *)thumbnailForURL:(WIURL *)url { |
|---|
| 162 |
return [self thumbnailForURL:url withData:NULL]; |
|---|
| 163 |
} |
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 |
- (FHImage *)thumbnailForURL:(WIURL *)url withData:(NSData *)data { |
|---|
| 168 |
FHImage *image; |
|---|
| 169 |
|
|---|
| 170 |
image = [_thumbnails objectForKey:[url string]]; |
|---|
| 171 |
|
|---|
| 172 |
if(!image) { |
|---|
| 173 |
if(data) |
|---|
| 174 |
image = [[[FHImage alloc] initThumbnailWithData:data preferredSize:NSMakeSize(128.0, 128.0)] autorelease]; |
|---|
| 175 |
else |
|---|
| 176 |
image = [[[FHImage alloc] initThumbnailWithURL:url preferredSize:NSMakeSize(128.0, 128.0)] autorelease]; |
|---|
| 177 |
|
|---|
| 178 |
if(image) { |
|---|
| 179 |
if([_thumbnails count] > 500) |
|---|
| 180 |
[_thumbnails removeObjectForKey:[[_thumbnails allKeys] objectAtIndex:0]]; |
|---|
| 181 |
|
|---|
| 182 |
[_thumbnails setObject:image forKey:[url string]]; |
|---|
| 183 |
} |
|---|
| 184 |
} |
|---|
| 185 |
|
|---|
| 186 |
return image; |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
- (void)dropThumbnailsForURL:(WIURL *)url { |
|---|
| 192 |
NSArray *keys; |
|---|
| 193 |
NSString *key, *string; |
|---|
| 194 |
NSUInteger i, count; |
|---|
| 195 |
|
|---|
| 196 |
keys = [_thumbnails allKeys]; |
|---|
| 197 |
string = [url string]; |
|---|
| 198 |
|
|---|
| 199 |
for(i = 0, count = [keys count]; i < count; i++) { |
|---|
| 200 |
key = [keys objectAtIndex:i]; |
|---|
| 201 |
|
|---|
| 202 |
if([key hasPrefix:string]) |
|---|
| 203 |
[_thumbnails removeObjectForKey:key]; |
|---|
| 204 |
} |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
@end |
|---|