-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSFHFFaviconCache.m
188 lines (139 loc) · 5.06 KB
/
SFHFFaviconCache.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// SHFFaviconCache.m
// Delicious Client
//
// Created by Buzz Andersen on 8/31/05.
// Copyright 2005 __MyCompanyName__. All rights reserved.
//
#import "SFHFFaviconCache.h"
#import "NSFileManager+ESBExtensions.h"
#import "defines.h"
@interface SFHFFaviconCache (Private)
- (NSImage *) downloadFaviconForURL: (NSURL *) aURL;
+ (NSString *) cachedFileNameForURL: (NSURL *) aURL;
+ (NSURL *) faviconURLForURL: (NSURL *) aURL;
- (void) setMemoryCache: (NSMutableDictionary *) newMemoryCache;
- (NSMutableDictionary *) memoryCache;
@end
@implementation SFHFFaviconCache
static SFHFFaviconCache *sharedFaviconCache = nil;
+ (SFHFFaviconCache *) sharedFaviconCache {
@synchronized (self) {
if (!sharedFaviconCache) {
sharedFaviconCache = [[SFHFFaviconCache alloc] init];
}
}
return sharedFaviconCache;
}
- init {
if (self = [super init]) {
[self setMemoryCache: [[[NSMutableDictionary alloc] init] autorelease]];
[self setDefaultFavicon: [NSImage imageNamed: kDEFAULT_FAVICON_NAME]];
return self;
}
return nil;
}
- (void) setDefaultFavicon: (NSImage *) newDefaultFavicon {
if (newDefaultFavicon != defaultFavicon) {
[defaultFavicon release];
defaultFavicon = [newDefaultFavicon copy];
[defaultFavicon setCacheMode: NSImageCacheNever];
}
}
- (NSImage *) defaultFavicon {
return [[defaultFavicon retain] autorelease];
}
- (NSImage *) faviconForURL: (NSURL *) url forceRefresh: (BOOL) forceRefresh {
NSImage *favicon = nil;
NSMutableDictionary *memCache = [self memoryCache];
if (!forceRefresh) {
favicon = [memCache objectForKey: [url absoluteString]];
if (favicon) {
return favicon;
}
}
NSString * faviconFolder = [[NSFileManager defaultManager] getApplicationSupportSubpath: @"FavIcons"];
NSString * faviconName = [SFHFFaviconCache cachedFileNameForURL: url];
if (faviconName == nil) {
return nil;
}
NSString * faviconPath = [faviconFolder stringByAppendingPathComponent:faviconName];
BOOL iconExists = [[NSFileManager defaultManager] fileExistsAtPath: faviconPath];
if (forceRefresh) {
/* download icon, write to disk */
favicon = [self downloadFaviconForURL: url];
if (favicon) {
if (iconExists) {
[[NSFileManager defaultManager] removeFileAtPath: faviconPath handler: nil];
}
NSImage *resizedImage = [[NSImage alloc] initWithSize: kFAVICON_DISPLAY_SIZE];
[resizedImage lockFocus];
[[NSGraphicsContext currentContext] setImageInterpolation: NSImageInterpolationHigh];
NSSize originalSize = [favicon size];
NSSize resizedSize = [resizedImage size];
[favicon drawInRect: NSMakeRect(0, 0, resizedSize.width, resizedSize.height) fromRect: NSMakeRect(0, 0, originalSize.width, originalSize.height) operation: NSCompositeSourceOver fraction: 1.0];
[resizedImage unlockFocus];
NSData *resizedData = [resizedImage TIFFRepresentation];
if (resizedData) {
[resizedData writeToFile: faviconPath atomically:YES];
}
favicon = [resizedImage autorelease];
}
}
else if (iconExists) {
/* load icon from disk */
favicon = [[[NSImage alloc] initWithContentsOfFile: faviconPath] autorelease];
}
if (favicon) {
[favicon setCacheMode: NSImageCacheNever];
[memCache setObject: favicon forKey: [url absoluteString]];
}
return favicon;
}
- (void) clearFaviconCache {
[self setMemoryCache: nil];
[[NSFileManager defaultManager] removeFileAtPath: [[NSFileManager defaultManager] getApplicationSupportSubpath: @"FavIcons"] handler: nil];
}
@end
@implementation SFHFFaviconCache (Private)
- (NSImage *) downloadFaviconForURL: (NSURL *) aURL {
// This somehow needs to be augmented to look at the contents of any html
// file located at the URL - we should give precendence to the LINK tags.
NSURL * faviconURL = [SFHFFaviconCache faviconURLForURL: aURL];
NSMutableURLRequest * req = [NSMutableURLRequest requestWithURL:faviconURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];
[req setValue:kUSER_AGENT forHTTPHeaderField:@"User-Agent"];
NSURLResponse * resp;
NSError * error;
NSData *returnData = [NSURLConnection sendSynchronousRequest: req returningResponse: &resp error: &error];
if (returnData && !error) {
return [[[NSImage alloc] initWithData: returnData] autorelease];
}
return nil;
}
- (void) setMemoryCache: (NSMutableDictionary *) newMemoryCache {
if (newMemoryCache != memoryCache) {
[memoryCache release];
memoryCache = [newMemoryCache mutableCopy];
}
}
- (NSMutableDictionary *) memoryCache {
return [[memoryCache retain] autorelease];
}
+ (NSString *) cachedFileNameForURL: (NSURL *) aURL {
if ([aURL host] == nil) {
return nil;
}
NSMutableString * hostName = [NSMutableString stringWithString: [aURL host]];
// Convert '.' to '_'
[hostName replaceOccurrencesOfString:@"."
withString:@"_"
options:nil
range:NSMakeRange(0, [hostName length])];
[hostName appendString:@".tiff"];
return [[hostName copy] autorelease];
}
+ (NSURL *) faviconURLForURL: (NSURL *) aURL {
NSURL * faviconURL = [NSURL URLWithString: [NSString stringWithFormat:@"http://%@/favicon.ico", [aURL host]]];
return [[faviconURL copy] autorelease];
}
@end