-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathYLImagePreviewer.m
More file actions
320 lines (253 loc) · 9.48 KB
/
YLImagePreviewer.m
File metadata and controls
320 lines (253 loc) · 9.48 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//
// YLImagePreviewer.m
// MacBlueTelnet
//
// Created by Jjgod Jiang on 2/17/08.
// Copyright 2008 Jjgod Jiang. All rights reserved.
//
#import "CommonType.h"
#import "YLSite.h"
#import "YLImagePreviewer.h"
#import "YLImageView.h"
@interface NSHTTPURLResponse (Corrected)
- (NSString *) correctedFileNameWithEncoding: (NSStringEncoding) encoding;
@end
@implementation NSHTTPURLResponse (Corrected)
- (NSString *) correctedFileNameWithEncoding: (NSStringEncoding) encoding
{
NSString *fileName = nil, *disposition;
disposition = [[self allHeaderFields] objectForKey: @"Content-Disposition"];
if (! disposition)
return [self suggestedFilename];
NSRange start = [disposition rangeOfString: @"filename="];
if (start.location != NSNotFound)
fileName = [disposition substringFromIndex: start.location + start.length];
if (fileName)
{
int i, max = [fileName length];
char *nbytes = (char *) malloc(max + 1);
for (i = 0; i < max; i++)
{
unichar ch = [fileName characterAtIndex: i];
nbytes[i] = (char) ch;
}
nbytes[i] = '\0';
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
NSString *str = [NSString stringWithCString: nbytes
encoding: enc];
free(nbytes);
return str;
}
return nil;
}
@end
@implementation YLImagePreviewer
- (id) initWithURL: (NSURL *) url
{
if ([super init])
{
// create the request
NSURLRequest *request = [NSURLRequest requestWithURL: url
cachePolicy: NSURLRequestUseProtocolCachePolicy
timeoutInterval: 60.0];
// create the connection with the request
// and start loading the data
_connection = [[NSURLConnection alloc] initWithRequest: request
delegate: self];
if (_connection)
{
// Create the NSMutableData that will hold
// the received data
// receivedData is declared as a method instance elsewhere
_receivedData = [[NSMutableData data] retain];
[self showLoadingWindow];
} else {
// inform the user that the download could not be made
NSLog(@"inform the user that the download could not be made");
}
}
return self;
}
- (void) dealloc
{
// NSLog(@"dealloc everything in YLImagePreviewer");
// if we are still connecting, should cancel now and release
// related resource.
if (_connection)
{
[_connection cancel];
[self releaseConnection];
}
[_window release];
[super dealloc];
}
- (void) windowWillClose: (NSNotification *) notification
{
if ([_window isReleasedWhenClosed])
_window = nil;
[self autorelease];
}
- (void) showLoadingWindow
{
unsigned int style = NSTitledWindowMask |
NSMiniaturizableWindowMask | NSClosableWindowMask |
NSHUDWindowMask | NSUtilityWindowMask;
_window = [[NSPanel alloc] initWithContentRect: NSMakeRect(0, 0, 400, 30)
styleMask: style
backing: NSBackingStoreBuffered
defer: NO];
[_window setFloatingPanel: NO];
[_window setDelegate: self];
[_window setOpaque: YES];
[_window center];
[_window setTitle: @"Loading..."];
[_window setViewsNeedDisplay: NO];
[_window makeKeyAndOrderFront: nil];
_indicator = [[HMBlkProgressIndicator alloc] initWithFrame: NSMakeRect(10, 10, 380, 10)];
[[_window contentView] addSubview: _indicator];
// [_indicator release];
[_indicator startAnimation: self];
}
- (void) showImage: (NSImage *) image
withTitle: (NSString *) title
tiffData: (NSDictionary *) tiffData
{
YLImageView *view;
NSImageRep *rep = [[image representations] objectAtIndex: 0];
NSSize size = NSMakeSize([rep pixelsWide], [rep pixelsHigh]);
NSSize visibleSize = [[NSScreen mainScreen] visibleFrame].size;
NSSize viewSize = [[_window contentView] frame].size;
NSSize frameSize = [_window frame].size;
visibleSize.width -= 20;
visibleSize.height -= 20;
NSPoint origin = [[NSScreen mainScreen] visibleFrame].origin;
double aspect = size.height / size.width;
// Do some auto resizing in case the image size is too large
if (size.width > visibleSize.width)
{
size.width = visibleSize.width;
size.height = aspect * size.width;
}
if (size.height > visibleSize.height)
{
size.height = visibleSize.height;
size.width = size.height / aspect;
}
origin.x += ([[NSScreen mainScreen] visibleFrame].size.width - size.width) / 2;
// use Golden Ratio to place the window vertically
origin.y += ([[NSScreen mainScreen] visibleFrame].size.height - size.height) / 1.61803399;
[image setSize: size];
// NSLog(@"image size: %g %g", size.width, size.height);
[_window setTitle: title];
NSRect viewRect = NSMakeRect(0, 0, size.width, size.height);
view = [[YLImageView alloc] initWithFrame: viewRect previewer: self];
[_indicator removeFromSuperview];
[_indicator release];
[[_window contentView] addSubview: view];
[_window makeFirstResponder: view];
[_window setAcceptsMouseMovedEvents: YES];
[view release];
[view setImage: image];
[view setTiffData: tiffData];
[image release];
[_window setFrame: NSMakeRect(origin.x, origin.y,
size.width + frameSize.width - viewSize.width,
size.height + frameSize.height - viewSize.height)
display: YES
animate: YES];
}
NSStringEncoding encodingFromYLEncoding(YLEncoding ylenc)
{
CFStringEncoding cfenc;
switch (ylenc)
{
case YLGBKEncoding:
cfenc = kCFStringEncodingGB_18030_2000;
break;
case YLBig5Encoding:
cfenc = kCFStringEncodingBig5_E;
break;
}
return CFStringConvertEncodingToNSStringEncoding(cfenc);
}
- (void) connection: (NSURLConnection *) connection
didReceiveResponse: (NSURLResponse *) response
{
/* FIXME: a hack to retrieve the correct file name, we need better
* ways to determine whether we need this hack or not, and
* which encoding to apply
*/
NSString *fileName = [(NSHTTPURLResponse *) response correctedFileNameWithEncoding:
encodingFromYLEncoding(YLGBKEncoding)];
if (fileName)
_currentFileDownloading = [fileName retain];
_totalLength = [response expectedContentLength];
[_window setTitle: [NSString stringWithFormat: @"Loading %@...", _currentFileDownloading]];
[_indicator setIndeterminate: NO];
[_indicator setMaxValue: (double) _totalLength];
[_indicator setDoubleValue: 0];
[_receivedData setLength: 0];
}
- (void) connection: (NSURLConnection *) connection
didReceiveData: (NSData *) data
{
// NSLog(@"didReceiveData: %d bytes", [data length]);
// append the new data to the receivedData
// receivedData is declared as a method instance elsewhere
[_receivedData appendData: data];
[_indicator incrementBy: (double) [data length]];
}
- (void)connection: (NSURLConnection *) connection
didFailWithError: (NSError *) error
{
[self releaseConnection];
// inform the user
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey: NSErrorFailingURLStringKey]);
}
- (void) connectionDidFinishLoading: (NSURLConnection *) connection
{
// do something with the data
// receivedData is declared as a method instance elsewhere
// NSLog(@"Succeeded! Received %d bytes of data", [_receivedData length]);
[_indicator setDoubleValue: (double) [_receivedData length]];
CGImageSourceRef exifSource = CGImageSourceCreateWithData((CFDataRef) _receivedData, NULL);
NSDictionary *metaData = (NSDictionary*) CGImageSourceCopyPropertiesAtIndex(exifSource, 0, nil);
NSDictionary *tiffData = [metaData objectForKey: (NSString *) kCGImagePropertyTIFFDictionary];
CFRelease(exifSource);
NSImage *image = [[NSImage alloc] initWithData: _receivedData];
if (image == nil || [[image representations] count] == 0)
{
NSString *text = [NSString stringWithFormat: @"Failed to download file %@", _currentFileDownloading];
NSAlert *alert = [NSAlert alertWithMessageText: @"Failed to download image."
defaultButton: @"OK"
alternateButton: nil
otherButton: nil
informativeTextWithFormat: text];
[alert runModal];
}
else
[self showImage: image
withTitle: _currentFileDownloading
tiffData: tiffData];
// [self releaseConnection];
}
- (void) releaseConnection
{
[_connection release];
_connection = nil;
[_receivedData release];
_receivedData = nil;
[_currentFileDownloading release];
_currentFileDownloading = nil;
}
- (NSMutableData *) receivedData
{
return _receivedData;
}
- (NSString *) filename
{
return _currentFileDownloading;
}
@end