-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainController+URLDownload.m
308 lines (260 loc) · 10.8 KB
/
MainController+URLDownload.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
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
//
// DownloadPart.m
// Toucan
//
// Created by will on 23/03/10.
// Copyright 2010 ExoApps. All rights reserved.
//
#import "MainController+URLDownload.h"
@implementation MainController(URLDownload)
# pragma mark -
# pragma mark NSURLDownload delegate methods
- (IBAction)stopDownloading:(id)sender
{
// if(downloadIsRunning == NO) return;
[theDownload cancel], downloadIsRunning = NO;
// [createSearchFolder setEnabled: YES];
// [changePath setEnabled: YES];
[self updateDockWithTitle: @"" withIconName: nil];
[itemsToDownload removeAllObjects];
[pb setDoubleValue: 0];
[pb2 setDoubleValue: 0];
[cancelDownloadButton setEnabled: NO];
iii = 0;
[percents setStringValue: @""];
if([splitViewDownload isSubviewCollapsed: downloadView] == NO) [splitViewDownload toggleCollapse: nil];
[NSApp setApplicationIconImage: nil];
}
- (void)startDownloading:(NSString *)theUrl
{
// [createSearchFolder setEnabled: NO];
// [changePath setEnabled: NO];
theRequest2 = [NSURLRequest requestWithURL: [NSURL URLWithString: [theUrl stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]
cachePolicy: NSURLRequestUseProtocolCachePolicy
timeoutInterval: 30.0
];
theDownload = [[NSURLDownload alloc] initWithRequest: theRequest2 delegate: self];
[theDownload setDeletesFileUponFailure: YES];
// downloadIsRunning = YES;
if (!theDownload)
{
if([self.itemsToDownload count] > 1) iii++, [self startDownloading: [self.itemsToDownload objectAtIndex: iii]];
else
{
// NSRunAlertPanel(@"Attention !", @"L'image n'a pu être sauvegardée !", @"OK", NULL, NULL);
if(growlIsNotifying == YES)
[GrowlApplicationBridge notifyWithTitle: [NSString stringWithFormat: @"%@ - Attention !",
[[NSFileManager defaultManager] displayNameAtPath: [[NSBundle mainBundle] bundlePath]]
]
description: NSLocalizedString(@"GROWL_DOWNLOAD_ERROR_DESCRIPTION_NOTIFICATION",@"Un problème est survenue lors de l'enregistrement de l'image !")
notificationName: @"Download Fails"
iconData: [NSImage imageNamed: @"NSStopProgressFreestandingTemplate"]
priority: 1
isSticky: NO
clickContext: nil];
downloadIsRunning = NO;
}
}
else
{
downloadIsRunning = YES;
DLog(@"Download Request allocated...");
}
}
- (void)downloadDidBegin:(NSURLDownload *)download
{
downloadIsRunning = YES;
if([itemsToDownload count] > 1)
{
[pb setMaxValue: [self.itemsToDownload count]];
if(iii == 0) [pb setDoubleValue: 0];
}
else
{
[pb setDoubleValue: 0];
[pb2 setDoubleValue: 0];
}
iii++;
[progressIndicator startAnimation: self];
[saveButton setEnabled: NO forSegment: 0];
[saveButton setEnabled: NO forSegment: 1];
[saveButton setEnabled: NO forSegment: 2];
[cancelDownloadButton setEnabled: YES];
DLog(@"Download begin...");
}
- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
{
// downloadIsRunning = NO;
if(([self.itemsToDownload count] > 1) && (iii != [self.itemsToDownload count])) iii++, unfetchedItems++, [self startDownloading: [[self.itemsToDownload objectAtIndex: iii] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
[download release];
if([self.itemsToDownload count] <= 1)
{
if(growlIsNotifying == YES)
[GrowlApplicationBridge notifyWithTitle: [NSString stringWithFormat: @"%@ - Attention !",
[[NSFileManager defaultManager] displayNameAtPath: [[NSBundle mainBundle] bundlePath]]
]
description: NSLocalizedString(@"GROWL_DOWNLOAD_ERROR_DESCRIPTION_NOTIFICATION",@"Un problème est survenue lors de l'enregistrement de l'image !")
notificationName: @"Download Fails"
iconData: [NSImage imageNamed: @"NSStopProgressFreestandingTemplate"]
priority: 1
isSticky: NO
clickContext: nil];
[saveButton setEnabled: NO forSegment: 0];
[saveButton setEnabled: NO forSegment: 1];
[saveButton setEnabled: NO forSegment: 2];
[createSearchFolder setEnabled: YES];
[changePath setEnabled: YES];
[percents setStringValue: @""];
[pb setDoubleValue: 0];
[pb2 setDoubleValue: 0];
[cancelDownloadButton setEnabled: NO];
if([splitViewDownload isSubviewCollapsed: downloadView] == NO) [splitViewDownload toggleCollapse: nil];
}
else
{
DLog(@"Download failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey: NSErrorFailingURLStringKey]);
}
}
- (void)downloadDidFinish:(NSURLDownload *)download
{
[download release];
[pb setDoubleValue:([pb doubleValue]+1)];
if(([self.itemsToDownload count] > 1) && (iii != [self.itemsToDownload count]))
{
downloadIsRunning = NO;
[self startDownloading: [self.itemsToDownload objectAtIndex: iii]];
downloadIsRunning = YES;
[self updateDockWithTitle: [NSString stringWithFormat: @"%i/%i", iii, [self.itemsToDownload count]] withIconName: @"Icon.icns"];
}
else
{
downloadIsRunning = NO;
[self updateDockWithTitle: [NSString stringWithFormat: @"%i/%i", iii, [self.itemsToDownload count]] withIconName: @"Icon.icns"];
if([self.itemsToDownload count] > 1)
{
NSMutableString * fNames = [NSMutableString string];
for(NSString * fName in self.itemsToDownload) [fNames appendFormat: [NSString stringWithFormat: @"%@\n", [[fName stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding] lastPathComponent]]];
if(growlIsNotifying == YES)
[GrowlApplicationBridge notifyWithTitle: [NSString stringWithFormat: @"%@ - Info",
[[NSFileManager defaultManager] displayNameAtPath: [[NSBundle mainBundle] bundlePath]]
]
description: [NSString stringWithFormat: NSLocalizedString(@"GROWL_DOWNLOAD_SUCCESSFULL_DESCRIPTION_NOTIFICATION",@"Sauvegarde effectuée avec succès => %@"),
fNames
]
notificationName: @"Download OK"
iconData: nil
priority: 0
isSticky: NO
clickContext: nil];
[self.itemsToDownload removeAllObjects];
//--- RESTOR Dock Icon -----
// [self updateDockWithTitle: nil withIconName: nil];
[NSApp setApplicationIconImage: nil];
[cancelDownloadButton setEnabled: NO];
[[NSSound soundNamed: @"Blow"] play];
[createSearchFolder setEnabled: YES];
[changePath setEnabled: YES];
if([splitViewDownload isSubviewCollapsed: downloadView] == NO) [splitViewDownload toggleCollapse: nil];
//Bounce the Dock if iMage is not the current active application
[[NSApplication sharedApplication] requestUserAttention: NSInformationalRequest];
}
else
{
if(growlIsNotifying == YES)
[GrowlApplicationBridge notifyWithTitle: [NSString stringWithFormat: @"%@ - Info",
[[NSFileManager defaultManager] displayNameAtPath: [[NSBundle mainBundle] bundlePath]]
]
description: [NSString stringWithFormat: NSLocalizedString(@"GROWL_DOWNLOAD_SUCCESSFULL_DESCRIPTION_NOTIFICATION",@"Sauvegarde effectuée avec succès => %@"),
destinationFilename
]
notificationName: @"Download OK"
iconData: nil
priority: 0
isSticky: NO
clickContext: nil];
//Bounce the Dock if iMage is not the current active application
[[NSApplication sharedApplication] requestUserAttention: NSInformationalRequest];
}
[self.itemsToDownload removeAllObjects];
[saveButton setEnabled: YES forSegment: 0];
[saveButton setEnabled: YES forSegment: 1];
[saveButton setEnabled: YES forSegment: 2];
// [saveButton setEnabled: YES];
[createSearchFolder setEnabled: YES];
[changePath setEnabled: YES];
[progressIndicator stopAnimation: self];
[percents setStringValue: @""];
if([splitViewDownload isSubviewCollapsed: downloadView] == NO) [splitViewDownload toggleCollapse: nil];
[cancelDownloadButton setEnabled: NO];
if(unfetchedItems != 0)
{
if(growlIsNotifying == YES)
[GrowlApplicationBridge notifyWithTitle: [NSString stringWithFormat: @"%@ - Attention !",
[[NSFileManager defaultManager] displayNameAtPath: [[NSBundle mainBundle] bundlePath]]
]
description: [NSString stringWithFormat:
NSLocalizedString(@"GROWL_DOWNLOAD_ERROR_SOME_ITEMS_UNFETCHABLE_DESCRIPTION_NOTIFICATION",
@"%i image(s) non téléchargée(s). Introuvable(s) sur le serveur distant !"),
unfetchedItems]
notificationName: @"Download Fails"
iconData: [NSImage imageNamed: @"NSStopProgressFreestandingTemplate"]
priority: 1
isSticky: YES
clickContext: nil];
}
}
[self tableViewStuff];
}
- (void)download:(NSURLDownload *)download decideDestinationWithSuggestedFilename:(NSString *)filename
{
// [destinationFilename release];
// destinationFilename = [[dest stringValue] stringByAppendingPathComponent: [[imgInfosUrl stringValue] lastPathComponent]];
self.destinationFilename = [[self.itemsToDownloadOutput objectAtIndex: (iii-1)] stringByAppendingPathComponent: filename];
[download setDestination: self.destinationFilename allowOverwrite: NO];
// [destinationFilename retain];
}
- (void)download:(NSURLDownload *)download didCreateDestination:(NSString *)path
{
DLog(@"Final file destination: %@", path);
}
- (void)setDownloadResponse:(NSURLResponse *)aDownloadResponse
{
[aDownloadResponse retain];
[downloadResponse release];
downloadResponse = aDownloadResponse;
DLog(@"Download Response: %@", aDownloadResponse);
}
- (void)download:(NSURLDownload *)download didReceiveResponse:(NSURLResponse *)response
{
bytesReceived = 0;
[self setDownloadResponse: response];
DLog(@"Did Receive Response: %@", response);
}
- (void)download:(NSURLDownload *)download didReceiveDataOfLength:(unsigned)length
{
long long expectedLength = [downloadResponse expectedContentLength];
bytesReceived = bytesReceived + length;
if (expectedLength != NSURLResponseUnknownLength) {
float percentComplete = (bytesReceived/(float)expectedLength)*100.0;
if([self.itemsToDownload count] > 1)
{
[pb2 setDoubleValue: percentComplete];
[percents setStringValue: [NSString stringWithFormat: NSLocalizedString(@"COMPLETED_DOWNLOAD_PERCENTS",@"%i%% de \"%@\" effectués"),
(int)percentComplete,
[self.destinationFilename lastPathComponent]]
];
}
else
{
[pb setDoubleValue: percentComplete];
[pb2 setDoubleValue: percentComplete];
[percents setStringValue: [NSString stringWithFormat: NSLocalizedString(@"COMPLETED_DOWNLOAD_PERCENTS",@"%i%% de \"%@\" effectués"),
(int)percentComplete,
[[infosUrl title] lastPathComponent]]
];
}
DLog(@"Percent complete - %f", percentComplete);
}
else DLog(@"Bytes received - %d",bytesReceived);
}
@end