1 | /****************************************************************************** |
---|
2 | * $Id: Torrent.m 2357 2007-07-15 17:25:06Z livings124 $ |
---|
3 | * |
---|
4 | * Copyright (c) 2006-2007 Transmission authors and contributors |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a |
---|
7 | * copy of this software and associated documentation files (the "Software"), |
---|
8 | * to deal in the Software without restriction, including without limitation |
---|
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
---|
10 | * and/or sell copies of the Software, and to permit persons to whom the |
---|
11 | * Software is furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
---|
21 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
22 | * DEALINGS IN THE SOFTWARE. |
---|
23 | *****************************************************************************/ |
---|
24 | |
---|
25 | #import "Torrent.h" |
---|
26 | #import "StringAdditions.h" |
---|
27 | |
---|
28 | #define BAR_HEIGHT 12.0 |
---|
29 | |
---|
30 | #define MAX_PIECES 324 |
---|
31 | #define BLANK_PIECE -99 |
---|
32 | |
---|
33 | static int static_lastid = 0; |
---|
34 | |
---|
35 | @interface Torrent (Private) |
---|
36 | |
---|
37 | - (id) initWithHash: (NSString *) hashString path: (NSString *) path lib: (tr_handle_t *) lib |
---|
38 | publicTorrent: (NSNumber *) publicTorrent |
---|
39 | downloadFolder: (NSString *) downloadFolder |
---|
40 | useIncompleteFolder: (NSNumber *) useIncompleteFolder incompleteFolder: (NSString *) incompleteFolder |
---|
41 | dateAdded: (NSDate *) dateAdded dateCompleted: (NSDate *) dateCompleted |
---|
42 | dateActivity: (NSDate *) dateActivity |
---|
43 | ratioSetting: (NSNumber *) ratioSetting ratioLimit: (NSNumber *) ratioLimit |
---|
44 | limitSpeedCustom: (NSNumber *) limitCustom |
---|
45 | checkUpload: (NSNumber *) checkUpload uploadLimit: (NSNumber *) uploadLimit |
---|
46 | checkDownload: (NSNumber *) checkDownload downloadLimit: (NSNumber *) downloadLimit |
---|
47 | pex: (NSNumber *) pex |
---|
48 | waitToStart: (NSNumber *) waitToStart orderValue: (NSNumber *) orderValue; |
---|
49 | |
---|
50 | - (BOOL) shouldUseIncompleteFolderForName: (NSString *) name; |
---|
51 | - (void) updateDownloadFolder; |
---|
52 | |
---|
53 | - (void) createFileList; |
---|
54 | - (void) insertPath: (NSMutableArray *) components forSiblings: (NSMutableArray *) siblings |
---|
55 | withParent: (NSMutableDictionary *) parent previousPath: (NSString *) previousPath |
---|
56 | flatList: (NSMutableArray *) flatList fileSize: (uint64_t) size index: (int) index; |
---|
57 | - (NSImage *) advancedBar; |
---|
58 | - (void) trashFile: (NSString *) path; |
---|
59 | |
---|
60 | @end |
---|
61 | |
---|
62 | @implementation Torrent |
---|
63 | |
---|
64 | // Used to optimize drawing. They contain packed RGBA pixels for every color needed. |
---|
65 | #define BE OSSwapBigToHostConstInt32 |
---|
66 | |
---|
67 | static uint32_t kRed = BE(0xFF6450FF), //255, 100, 80 |
---|
68 | kBlue = BE(0x50A0FFFF), //80, 160, 255 |
---|
69 | kBlue2 = BE(0x1E46B4FF), //30, 70, 180 |
---|
70 | kGray = BE(0x969696FF), //150, 150, 150 |
---|
71 | kGreen1 = BE(0x99FFCCFF), //153, 255, 204 |
---|
72 | kGreen2 = BE(0x66FF99FF), //102, 255, 153 |
---|
73 | kGreen3 = BE(0x00FF66FF), //0, 255, 102 |
---|
74 | kWhite = BE(0xFFFFFFFF); //255, 255, 255 |
---|
75 | |
---|
76 | - (id) initWithPath: (NSString *) path location: (NSString *) location forceDeleteTorrent: (BOOL) delete lib: (tr_handle_t *) lib |
---|
77 | { |
---|
78 | self = [self initWithHash: nil path: path lib: lib |
---|
79 | publicTorrent: delete ? [NSNumber numberWithBool: NO] : nil |
---|
80 | downloadFolder: location |
---|
81 | useIncompleteFolder: nil incompleteFolder: nil |
---|
82 | dateAdded: nil dateCompleted: nil |
---|
83 | dateActivity: nil |
---|
84 | ratioSetting: nil ratioLimit: nil |
---|
85 | limitSpeedCustom: nil |
---|
86 | checkUpload: nil uploadLimit: nil |
---|
87 | checkDownload: nil downloadLimit: nil |
---|
88 | pex: nil |
---|
89 | waitToStart: nil orderValue: nil]; |
---|
90 | |
---|
91 | if (self) |
---|
92 | { |
---|
93 | if (!fPublicTorrent) |
---|
94 | [self trashFile: path]; |
---|
95 | } |
---|
96 | return self; |
---|
97 | } |
---|
98 | |
---|
99 | - (id) initWithHistory: (NSDictionary *) history lib: (tr_handle_t *) lib |
---|
100 | { |
---|
101 | self = [self initWithHash: [history objectForKey: @"TorrentHash"] |
---|
102 | path: [history objectForKey: @"TorrentPath"] lib: lib |
---|
103 | publicTorrent: [history objectForKey: @"PublicCopy"] |
---|
104 | downloadFolder: [history objectForKey: @"DownloadFolder"] |
---|
105 | useIncompleteFolder: [history objectForKey: @"UseIncompleteFolder"] |
---|
106 | incompleteFolder: [history objectForKey: @"IncompleteFolder"] |
---|
107 | dateAdded: [history objectForKey: @"Date"] |
---|
108 | dateCompleted: [history objectForKey: @"DateCompleted"] |
---|
109 | dateActivity: [history objectForKey: @"DateActivity"] |
---|
110 | ratioSetting: [history objectForKey: @"RatioSetting"] |
---|
111 | ratioLimit: [history objectForKey: @"RatioLimit"] |
---|
112 | limitSpeedCustom: [history objectForKey: @"LimitSpeedCustom"] |
---|
113 | checkUpload: [history objectForKey: @"CheckUpload"] |
---|
114 | uploadLimit: [history objectForKey: @"UploadLimit"] |
---|
115 | checkDownload: [history objectForKey: @"CheckDownload"] |
---|
116 | downloadLimit: [history objectForKey: @"DownloadLimit"] |
---|
117 | pex: [history objectForKey: @"Pex"] |
---|
118 | waitToStart: [history objectForKey: @"WaitToStart"] |
---|
119 | orderValue: [history objectForKey: @"OrderValue"]]; |
---|
120 | |
---|
121 | if (self) |
---|
122 | { |
---|
123 | //start transfer |
---|
124 | NSNumber * active; |
---|
125 | if ((active = [history objectForKey: @"Active"]) && [active boolValue]) |
---|
126 | { |
---|
127 | fStat = tr_torrentStat(fHandle); |
---|
128 | [self startTransfer]; |
---|
129 | } |
---|
130 | } |
---|
131 | return self; |
---|
132 | } |
---|
133 | |
---|
134 | - (NSDictionary *) history |
---|
135 | { |
---|
136 | NSMutableDictionary * history = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
---|
137 | [NSNumber numberWithBool: fPublicTorrent], @"PublicCopy", |
---|
138 | [self hashString], @"TorrentHash", |
---|
139 | fDownloadFolder, @"DownloadFolder", |
---|
140 | [NSNumber numberWithBool: fUseIncompleteFolder], @"UseIncompleteFolder", |
---|
141 | [NSNumber numberWithBool: [self isActive]], @"Active", |
---|
142 | fDateAdded, @"Date", |
---|
143 | [NSNumber numberWithInt: fRatioSetting], @"RatioSetting", |
---|
144 | [NSNumber numberWithFloat: fRatioLimit], @"RatioLimit", |
---|
145 | [NSNumber numberWithInt: fCheckUpload], @"CheckUpload", |
---|
146 | [NSNumber numberWithInt: fUploadLimit], @"UploadLimit", |
---|
147 | [NSNumber numberWithInt: fCheckDownload], @"CheckDownload", |
---|
148 | [NSNumber numberWithInt: fDownloadLimit], @"DownloadLimit", |
---|
149 | [NSNumber numberWithBool: fWaitToStart], @"WaitToStart", |
---|
150 | [self orderValue], @"OrderValue", nil]; |
---|
151 | |
---|
152 | if (fIncompleteFolder) |
---|
153 | [history setObject: fIncompleteFolder forKey: @"IncompleteFolder"]; |
---|
154 | |
---|
155 | if (fPublicTorrent) |
---|
156 | [history setObject: [self publicTorrentLocation] forKey: @"TorrentPath"]; |
---|
157 | |
---|
158 | if (![self privateTorrent]) |
---|
159 | [history setObject: [NSNumber numberWithBool: fPex] forKey: @"Pex"]; |
---|
160 | |
---|
161 | if (fDateCompleted) |
---|
162 | [history setObject: fDateCompleted forKey: @"DateCompleted"]; |
---|
163 | |
---|
164 | NSDate * dateActivity = [self dateActivity]; |
---|
165 | if (dateActivity) |
---|
166 | [history setObject: dateActivity forKey: @"DateActivity"]; |
---|
167 | |
---|
168 | return history; |
---|
169 | } |
---|
170 | |
---|
171 | - (void) dealloc |
---|
172 | { |
---|
173 | [fDownloadFolder release]; |
---|
174 | [fIncompleteFolder release]; |
---|
175 | |
---|
176 | [fPublicTorrentLocation release]; |
---|
177 | |
---|
178 | [fDateAdded release]; |
---|
179 | [fDateCompleted release]; |
---|
180 | [fDateActivity release]; |
---|
181 | |
---|
182 | [fAnnounceDate release]; |
---|
183 | |
---|
184 | [fIcon release]; |
---|
185 | [fIconFlipped release]; |
---|
186 | [fIconSmall release]; |
---|
187 | |
---|
188 | [fProgressString release]; |
---|
189 | [fStatusString release]; |
---|
190 | [fShortStatusString release]; |
---|
191 | [fRemainingTimeString release]; |
---|
192 | |
---|
193 | [fFileList release]; |
---|
194 | [fFlatFileList release]; |
---|
195 | |
---|
196 | [fBitmap release]; |
---|
197 | |
---|
198 | if (fPieces) |
---|
199 | free(fPieces); |
---|
200 | |
---|
201 | [super dealloc]; |
---|
202 | } |
---|
203 | |
---|
204 | - (void) closeTorrent |
---|
205 | { |
---|
206 | tr_torrentClose(fHandle); |
---|
207 | } |
---|
208 | |
---|
209 | - (void) closeRemoveTorrent |
---|
210 | { |
---|
211 | tr_torrentRemoveSaved(fHandle); |
---|
212 | [self closeTorrent]; |
---|
213 | } |
---|
214 | |
---|
215 | - (void) changeIncompleteDownloadFolder: (NSString *) folder |
---|
216 | { |
---|
217 | fUseIncompleteFolder = folder != nil; |
---|
218 | if (fIncompleteFolder) |
---|
219 | { |
---|
220 | [fIncompleteFolder release]; |
---|
221 | fIncompleteFolder = nil; |
---|
222 | } |
---|
223 | |
---|
224 | if (folder) |
---|
225 | fIncompleteFolder = [folder retain]; |
---|
226 | |
---|
227 | [self updateDownloadFolder]; |
---|
228 | } |
---|
229 | |
---|
230 | - (void) changeDownloadFolder: (NSString *) folder |
---|
231 | { |
---|
232 | if (fDownloadFolder) |
---|
233 | [fDownloadFolder release]; |
---|
234 | fDownloadFolder = [folder retain]; |
---|
235 | |
---|
236 | [self updateDownloadFolder]; |
---|
237 | } |
---|
238 | |
---|
239 | - (NSString *) downloadFolder |
---|
240 | { |
---|
241 | return [NSString stringWithUTF8String: tr_torrentGetFolder(fHandle)]; |
---|
242 | } |
---|
243 | |
---|
244 | - (void) getAvailability: (int8_t *) tab size: (int) size |
---|
245 | { |
---|
246 | tr_torrentAvailability(fHandle, tab, size); |
---|
247 | } |
---|
248 | |
---|
249 | - (void) getAmountFinished: (float *) tab size: (int) size |
---|
250 | { |
---|
251 | tr_torrentAmountFinished(fHandle, tab, size); |
---|
252 | } |
---|
253 | |
---|
254 | - (void) update |
---|
255 | { |
---|
256 | fStat = tr_torrentStat(fHandle); |
---|
257 | |
---|
258 | //notification when downloading finished |
---|
259 | if (tr_getComplete(fHandle) || tr_getDone(fHandle)) |
---|
260 | { |
---|
261 | BOOL canMove = YES; |
---|
262 | |
---|
263 | //move file from incomplete folder to download folder |
---|
264 | if (fUseIncompleteFolder && ![[self downloadFolder] isEqualToString: fDownloadFolder] |
---|
265 | && (canMove = [self alertForMoveFolderAvailable])) |
---|
266 | { |
---|
267 | //pause without actually stopping |
---|
268 | tr_setUseCustomUpload(fHandle, 1); |
---|
269 | tr_setUploadLimit(fHandle, 0); |
---|
270 | |
---|
271 | tr_setUseCustomDownload(fHandle, 1); |
---|
272 | tr_setDownloadLimit(fHandle, 0); |
---|
273 | |
---|
274 | if ([[NSFileManager defaultManager] movePath: [[self downloadFolder] stringByAppendingPathComponent: [self name]] |
---|
275 | toPath: [fDownloadFolder stringByAppendingPathComponent: [self name]] handler: nil]) |
---|
276 | [self updateDownloadFolder]; |
---|
277 | |
---|
278 | [self updateSpeedSetting]; |
---|
279 | } |
---|
280 | |
---|
281 | if (!canMove) |
---|
282 | { |
---|
283 | fUseIncompleteFolder = NO; |
---|
284 | |
---|
285 | [fDownloadFolder release]; |
---|
286 | fDownloadFolder = fIncompleteFolder; |
---|
287 | fIncompleteFolder = nil; |
---|
288 | } |
---|
289 | |
---|
290 | if (fDateCompleted) |
---|
291 | [fDateCompleted release]; |
---|
292 | fDateCompleted = [[NSDate alloc] init]; |
---|
293 | |
---|
294 | fStat = tr_torrentStat(fHandle); |
---|
295 | [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentFinishedDownloading" object: self]; |
---|
296 | } |
---|
297 | else if (tr_getIncomplete(fHandle)) |
---|
298 | [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentRestartedDownloading" object: self]; |
---|
299 | else; |
---|
300 | |
---|
301 | //check to stop for ratio |
---|
302 | float stopRatio; |
---|
303 | if ([self isSeeding] && (stopRatio = [self actualStopRatio]) != INVALID && [self ratio] >= stopRatio) |
---|
304 | { |
---|
305 | [self stopTransfer]; |
---|
306 | fStat = tr_torrentStat(fHandle); |
---|
307 | |
---|
308 | fFinishedSeeding = YES; |
---|
309 | |
---|
310 | [self setRatioSetting: NSOffState]; |
---|
311 | [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentStoppedForRatio" object: self]; |
---|
312 | } |
---|
313 | |
---|
314 | NSMutableString * progressString = [NSMutableString stringWithString: @""], |
---|
315 | * remainingTimeString = [NSMutableString stringWithString: @""], |
---|
316 | * statusString = [NSMutableString string], |
---|
317 | * shortStatusString = [NSMutableString string]; |
---|
318 | |
---|
319 | if (![self allDownloaded]) |
---|
320 | [progressString appendFormat: NSLocalizedString(@"%@ of %@ (%.2f%%)", "Torrent -> progress string"), |
---|
321 | [NSString stringForFileSize: [self downloadedValid]], |
---|
322 | [NSString stringForFileSize: [self size]], 100.0 * [self progress]]; |
---|
323 | else if (![self isComplete]) |
---|
324 | [progressString appendFormat: NSLocalizedString(@"%@ of %@ (%.2f%%), uploaded %@ (Ratio: %@)", |
---|
325 | "Torrent -> progress string"), |
---|
326 | [NSString stringForFileSize: [self downloadedValid]], [NSString stringForFileSize: [self size]], |
---|
327 | 100.0 * [self progress], [NSString stringForFileSize: [self uploadedTotal]], |
---|
328 | [NSString stringForRatio: [self ratio]]]; |
---|
329 | else |
---|
330 | [progressString appendFormat: NSLocalizedString(@"%@, uploaded %@ (Ratio: %@)", "Torrent -> progress string"), |
---|
331 | [NSString stringForFileSize: [self size]], [NSString stringForFileSize: [self uploadedTotal]], |
---|
332 | [NSString stringForRatio: [self ratio]]]; |
---|
333 | |
---|
334 | BOOL wasChecking = fChecking; |
---|
335 | fChecking = NO; |
---|
336 | switch (fStat->status) |
---|
337 | { |
---|
338 | NSString * tempString; |
---|
339 | |
---|
340 | case TR_STATUS_STOPPED: |
---|
341 | if (fWaitToStart) |
---|
342 | { |
---|
343 | tempString = ![self allDownloaded] |
---|
344 | ? [NSLocalizedString(@"Waiting to download", "Torrent -> status string") stringByAppendingEllipsis] |
---|
345 | : [NSLocalizedString(@"Waiting to seed", "Torrent -> status string") stringByAppendingEllipsis]; |
---|
346 | } |
---|
347 | else if (fFinishedSeeding) |
---|
348 | tempString = NSLocalizedString(@"Seeding complete", "Torrent -> status string"); |
---|
349 | else |
---|
350 | tempString = NSLocalizedString(@"Paused", "Torrent -> status string"); |
---|
351 | |
---|
352 | [statusString setString: tempString]; |
---|
353 | [shortStatusString setString: tempString]; |
---|
354 | |
---|
355 | break; |
---|
356 | |
---|
357 | case TR_STATUS_CHECK_WAIT: |
---|
358 | tempString = [NSLocalizedString(@"Waiting to check existing data", "Torrent -> status string") |
---|
359 | stringByAppendingEllipsis]; |
---|
360 | |
---|
361 | [statusString setString: tempString]; |
---|
362 | [shortStatusString setString: tempString]; |
---|
363 | [remainingTimeString setString: tempString]; |
---|
364 | |
---|
365 | fChecking = YES; |
---|
366 | |
---|
367 | break; |
---|
368 | |
---|
369 | case TR_STATUS_CHECK: |
---|
370 | tempString = [NSLocalizedString(@"Checking existing data", "Torrent -> status string") stringByAppendingEllipsis]; |
---|
371 | |
---|
372 | [statusString setString: tempString]; |
---|
373 | [shortStatusString setString: tempString]; |
---|
374 | [remainingTimeString setString: tempString]; |
---|
375 | |
---|
376 | fChecking = YES; |
---|
377 | |
---|
378 | break; |
---|
379 | |
---|
380 | case TR_STATUS_DOWNLOAD: |
---|
381 | [statusString setString: @""]; |
---|
382 | if ([self totalPeersConnected] != 1) |
---|
383 | [statusString appendFormat: NSLocalizedString(@"Downloading from %d of %d peers", |
---|
384 | "Torrent -> status string"), [self peersUploading], [self totalPeersConnected]]; |
---|
385 | else |
---|
386 | [statusString appendFormat: NSLocalizedString(@"Downloading from %d of 1 peer", |
---|
387 | "Torrent -> status string"), [self peersUploading]]; |
---|
388 | |
---|
389 | int eta = [self eta]; |
---|
390 | if (eta < 0) |
---|
391 | { |
---|
392 | [remainingTimeString setString: NSLocalizedString(@"Unknown", "Torrent -> remaining time")]; |
---|
393 | [progressString appendString: NSLocalizedString(@" - remaining time unknown", "Torrent -> progress string")]; |
---|
394 | } |
---|
395 | else |
---|
396 | { |
---|
397 | if (eta < 60) |
---|
398 | [remainingTimeString appendFormat: NSLocalizedString(@"%d sec", "Torrent -> remaining time"), eta]; |
---|
399 | else if (eta < 3600) //60 * 60 |
---|
400 | [remainingTimeString appendFormat: NSLocalizedString(@"%d min %d sec", "Torrent -> remaining time"), |
---|
401 | eta / 60, eta % 60]; |
---|
402 | else if (eta < 86400) //24 * 60 * 60 |
---|
403 | [remainingTimeString appendFormat: NSLocalizedString(@"%d hr %d min", "Torrent -> remaining time"), |
---|
404 | eta / 3600, (eta / 60) % 60]; |
---|
405 | else |
---|
406 | { |
---|
407 | int days = eta / 86400; |
---|
408 | if (days > 1) |
---|
409 | [remainingTimeString appendFormat: NSLocalizedString(@"%d days %d hr", "Torrent -> remaining time"), |
---|
410 | days, (eta / 3600) % 24]; |
---|
411 | else |
---|
412 | [remainingTimeString appendFormat: NSLocalizedString(@"1 day %d hr", "Torrent -> remaining time"), |
---|
413 | (eta / 3600) % 24]; |
---|
414 | } |
---|
415 | |
---|
416 | [progressString appendFormat: NSLocalizedString(@" - %@ remaining", "Torrent -> progress string"), |
---|
417 | remainingTimeString]; |
---|
418 | } |
---|
419 | |
---|
420 | break; |
---|
421 | |
---|
422 | case TR_STATUS_SEED: |
---|
423 | case TR_STATUS_DONE: |
---|
424 | [statusString setString: @""]; |
---|
425 | if ([self totalPeersConnected] != 1) |
---|
426 | [statusString appendFormat: NSLocalizedString(@"Seeding to %d of %d peers", "Torrent -> status string"), |
---|
427 | [self peersDownloading], [self totalPeersConnected]]; |
---|
428 | else |
---|
429 | [statusString appendFormat: NSLocalizedString(@"Seeding to %d of 1 peer", "Torrent -> status string"), |
---|
430 | [self peersDownloading]]; |
---|
431 | |
---|
432 | break; |
---|
433 | |
---|
434 | case TR_STATUS_STOPPING: |
---|
435 | tempString = [NSLocalizedString(@"Stopping", "Torrent -> status string") stringByAppendingEllipsis]; |
---|
436 | |
---|
437 | [statusString setString: tempString]; |
---|
438 | [shortStatusString setString: tempString]; |
---|
439 | |
---|
440 | break; |
---|
441 | } |
---|
442 | |
---|
443 | //check for error |
---|
444 | BOOL wasError = fError; |
---|
445 | fError = [self isError]; |
---|
446 | |
---|
447 | //check if stalled |
---|
448 | BOOL wasStalled = fStalled; |
---|
449 | fStalled = [self isActive] && [fDefaults boolForKey: @"CheckStalled"] |
---|
450 | && [fDefaults integerForKey: @"StalledMinutes"] < [self stalledMinutes]; |
---|
451 | |
---|
452 | //create strings for error or stalled |
---|
453 | if (fError) |
---|
454 | [statusString setString: [NSLocalizedString(@"Error: ", "Torrent -> status string") |
---|
455 | stringByAppendingString: [self errorMessage]]]; |
---|
456 | else if (fStalled) |
---|
457 | [statusString setString: [NSLocalizedString(@"Stalled, ", "Torrent -> status string") |
---|
458 | stringByAppendingString: statusString]]; |
---|
459 | else; |
---|
460 | |
---|
461 | //update queue for checking (from downloading to seeding), stalled, or error |
---|
462 | if ((wasChecking && !fChecking) || (!wasStalled && fStalled) || (!wasError && fError && [self isActive])) |
---|
463 | [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self]; |
---|
464 | |
---|
465 | if ([self isActive] && ![self isChecking]) |
---|
466 | { |
---|
467 | NSString * stringToAppend = @""; |
---|
468 | if (![self allDownloaded]) |
---|
469 | { |
---|
470 | stringToAppend = [NSString stringWithFormat: NSLocalizedString(@"DL: %@, ", "Torrent -> status string"), |
---|
471 | [NSString stringForSpeed: [self downloadRate]]]; |
---|
472 | [shortStatusString setString: @""]; |
---|
473 | } |
---|
474 | else |
---|
475 | { |
---|
476 | NSString * ratioString = [NSString stringForRatio: [self ratio]]; |
---|
477 | |
---|
478 | [shortStatusString setString: [NSString stringWithFormat: NSLocalizedString(@"Ratio: %@, ", |
---|
479 | "Torrent -> status string"), ratioString]]; |
---|
480 | [remainingTimeString setString: [NSLocalizedString(@"Ratio: ", "Torrent -> status string") |
---|
481 | stringByAppendingString: ratioString]]; |
---|
482 | } |
---|
483 | |
---|
484 | stringToAppend = [stringToAppend stringByAppendingString: [NSLocalizedString(@"UL: ", "Torrent -> status string") |
---|
485 | stringByAppendingString: [NSString stringForSpeed: [self uploadRate]]]]; |
---|
486 | |
---|
487 | [statusString appendFormat: @" - %@", stringToAppend]; |
---|
488 | [shortStatusString appendString: stringToAppend]; |
---|
489 | } |
---|
490 | |
---|
491 | [fProgressString setString: progressString]; |
---|
492 | [fStatusString setString: statusString]; |
---|
493 | [fShortStatusString setString: shortStatusString]; |
---|
494 | [fRemainingTimeString setString: remainingTimeString]; |
---|
495 | } |
---|
496 | |
---|
497 | - (NSDictionary *) infoForCurrentView |
---|
498 | { |
---|
499 | NSMutableDictionary * info = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
---|
500 | [self name], @"Name", |
---|
501 | [NSNumber numberWithFloat: [self progress]], @"Progress", |
---|
502 | [NSNumber numberWithFloat: (float)fStat->left/[self size]], @"Left", |
---|
503 | [NSNumber numberWithBool: [self isActive]], @"Active", |
---|
504 | [NSNumber numberWithBool: [self isSeeding]], @"Seeding", |
---|
505 | [NSNumber numberWithBool: [self isChecking]], @"Checking", |
---|
506 | [NSNumber numberWithBool: fWaitToStart], @"Waiting", |
---|
507 | [NSNumber numberWithBool: [self isError]], @"Error", nil]; |
---|
508 | |
---|
509 | if ([self isSeeding]) |
---|
510 | [info setObject: [NSNumber numberWithFloat: [self progressStopRatio]] forKey: @"ProgressStopRatio"]; |
---|
511 | |
---|
512 | if (![fDefaults boolForKey: @"SmallView"]) |
---|
513 | { |
---|
514 | [info setObject: [self iconFlipped] forKey: @"Icon"]; |
---|
515 | [info setObject: [self progressString] forKey: @"ProgressString"]; |
---|
516 | [info setObject: [self statusString] forKey: @"StatusString"]; |
---|
517 | } |
---|
518 | else |
---|
519 | { |
---|
520 | [info setObject: [self iconSmall] forKey: @"Icon"]; |
---|
521 | [info setObject: [self remainingTimeString] forKey: @"RemainingTimeString"]; |
---|
522 | [info setObject: [self shortStatusString] forKey: @"ShortStatusString"]; |
---|
523 | } |
---|
524 | |
---|
525 | if ([fDefaults boolForKey: @"UseAdvancedBar"]) |
---|
526 | [info setObject: [self advancedBar] forKey: @"AdvancedBar"]; |
---|
527 | |
---|
528 | return info; |
---|
529 | } |
---|
530 | |
---|
531 | - (void) startTransfer |
---|
532 | { |
---|
533 | fWaitToStart = NO; |
---|
534 | fFinishedSeeding = NO; |
---|
535 | |
---|
536 | if (![self isActive] && [self alertForFolderAvailable] && [self alertForRemainingDiskSpace]) |
---|
537 | { |
---|
538 | tr_torrentStart(fHandle); |
---|
539 | [self update]; |
---|
540 | } |
---|
541 | } |
---|
542 | |
---|
543 | - (void) stopTransfer |
---|
544 | { |
---|
545 | fError = NO; |
---|
546 | fWaitToStart = NO; |
---|
547 | |
---|
548 | if ([self isActive]) |
---|
549 | { |
---|
550 | tr_torrentStop(fHandle); |
---|
551 | [self update]; |
---|
552 | |
---|
553 | [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self]; |
---|
554 | } |
---|
555 | } |
---|
556 | |
---|
557 | - (void) sleep |
---|
558 | { |
---|
559 | if ((fResumeOnWake = [self isActive])) |
---|
560 | tr_torrentStop(fHandle); |
---|
561 | } |
---|
562 | |
---|
563 | - (void) wakeUp |
---|
564 | { |
---|
565 | if (fResumeOnWake) |
---|
566 | tr_torrentStart(fHandle); |
---|
567 | } |
---|
568 | |
---|
569 | - (void) announce |
---|
570 | { |
---|
571 | if (![self isActive]) |
---|
572 | return; |
---|
573 | |
---|
574 | tr_manualUpdate(fHandle); |
---|
575 | |
---|
576 | if (fAnnounceDate) |
---|
577 | [fAnnounceDate release]; |
---|
578 | fAnnounceDate = [[NSDate date] retain]; |
---|
579 | } |
---|
580 | |
---|
581 | - (NSDate *) announceDate |
---|
582 | { |
---|
583 | return fAnnounceDate; |
---|
584 | } |
---|
585 | |
---|
586 | - (void) resetCache |
---|
587 | { |
---|
588 | tr_torrentRecheck(fHandle); |
---|
589 | [self update]; |
---|
590 | } |
---|
591 | |
---|
592 | - (float) ratio |
---|
593 | { |
---|
594 | return fStat->ratio; |
---|
595 | } |
---|
596 | |
---|
597 | - (int) ratioSetting |
---|
598 | { |
---|
599 | return fRatioSetting; |
---|
600 | } |
---|
601 | |
---|
602 | - (void) setRatioSetting: (int) setting |
---|
603 | { |
---|
604 | fRatioSetting = setting; |
---|
605 | } |
---|
606 | |
---|
607 | - (float) ratioLimit |
---|
608 | { |
---|
609 | return fRatioLimit; |
---|
610 | } |
---|
611 | |
---|
612 | - (void) setRatioLimit: (float) limit |
---|
613 | { |
---|
614 | if (limit >= 0) |
---|
615 | fRatioLimit = limit; |
---|
616 | } |
---|
617 | |
---|
618 | - (float) actualStopRatio |
---|
619 | { |
---|
620 | if (fRatioSetting == NSOnState) |
---|
621 | return fRatioLimit; |
---|
622 | else if (fRatioSetting == NSMixedState && [fDefaults boolForKey: @"RatioCheck"]) |
---|
623 | return [fDefaults floatForKey: @"RatioLimit"]; |
---|
624 | else |
---|
625 | return INVALID; |
---|
626 | } |
---|
627 | |
---|
628 | - (float) progressStopRatio |
---|
629 | { |
---|
630 | float stopRatio, ratio; |
---|
631 | if ((stopRatio = [self actualStopRatio]) == INVALID || (ratio = [self ratio]) >= stopRatio) |
---|
632 | return 1.0; |
---|
633 | else if (ratio > 0 && stopRatio > 0) |
---|
634 | return ratio / stopRatio; |
---|
635 | else |
---|
636 | return 0; |
---|
637 | } |
---|
638 | |
---|
639 | - (int) checkUpload |
---|
640 | { |
---|
641 | return fCheckUpload; |
---|
642 | } |
---|
643 | |
---|
644 | - (void) setCheckUpload: (int) setting |
---|
645 | { |
---|
646 | fCheckUpload = setting; |
---|
647 | [self updateSpeedSetting]; |
---|
648 | } |
---|
649 | |
---|
650 | - (int) uploadLimit |
---|
651 | { |
---|
652 | return fUploadLimit; |
---|
653 | } |
---|
654 | |
---|
655 | - (void) setUploadLimit: (int) limit |
---|
656 | { |
---|
657 | fUploadLimit = limit; |
---|
658 | [self updateSpeedSetting]; |
---|
659 | } |
---|
660 | |
---|
661 | - (int) checkDownload |
---|
662 | { |
---|
663 | return fCheckDownload; |
---|
664 | } |
---|
665 | |
---|
666 | - (void) setCheckDownload: (int) setting |
---|
667 | { |
---|
668 | fCheckDownload = setting; |
---|
669 | [self updateSpeedSetting]; |
---|
670 | } |
---|
671 | |
---|
672 | - (int) downloadLimit |
---|
673 | { |
---|
674 | return fDownloadLimit; |
---|
675 | } |
---|
676 | |
---|
677 | - (void) setDownloadLimit: (int) limit |
---|
678 | { |
---|
679 | fDownloadLimit = limit; |
---|
680 | [self updateSpeedSetting]; |
---|
681 | } |
---|
682 | |
---|
683 | - (void) updateSpeedSetting |
---|
684 | { |
---|
685 | tr_setUseCustomUpload(fHandle, fCheckUpload != NSMixedState); |
---|
686 | tr_setUploadLimit(fHandle, fCheckUpload == NSOnState ? fUploadLimit : -1); |
---|
687 | |
---|
688 | tr_setUseCustomDownload(fHandle, fCheckDownload != NSMixedState); |
---|
689 | tr_setDownloadLimit(fHandle, fCheckDownload == NSOnState ? fDownloadLimit : -1); |
---|
690 | } |
---|
691 | |
---|
692 | - (void) setWaitToStart: (BOOL) wait |
---|
693 | { |
---|
694 | fWaitToStart = wait; |
---|
695 | } |
---|
696 | |
---|
697 | - (BOOL) waitingToStart |
---|
698 | { |
---|
699 | return fWaitToStart; |
---|
700 | } |
---|
701 | |
---|
702 | - (void) revealData |
---|
703 | { |
---|
704 | [[NSWorkspace sharedWorkspace] selectFile: [self dataLocation] inFileViewerRootedAtPath: nil]; |
---|
705 | } |
---|
706 | |
---|
707 | - (void) revealPublicTorrent |
---|
708 | { |
---|
709 | if (fPublicTorrent) |
---|
710 | [[NSWorkspace sharedWorkspace] selectFile: fPublicTorrentLocation inFileViewerRootedAtPath: nil]; |
---|
711 | } |
---|
712 | |
---|
713 | - (void) trashData |
---|
714 | { |
---|
715 | [self trashFile: [self dataLocation]]; |
---|
716 | } |
---|
717 | |
---|
718 | - (void) trashTorrent |
---|
719 | { |
---|
720 | if (fPublicTorrent) |
---|
721 | [self trashFile: [self publicTorrentLocation]]; |
---|
722 | } |
---|
723 | |
---|
724 | - (void) moveTorrentDataFileTo: (NSString *) folder |
---|
725 | { |
---|
726 | NSString * oldFolder = [self downloadFolder]; |
---|
727 | if (![oldFolder isEqualToString: folder] || ![fDownloadFolder isEqualToString: folder]) |
---|
728 | { |
---|
729 | //check if moving inside itself |
---|
730 | NSArray * oldComponents = [oldFolder pathComponents], |
---|
731 | * newComponents = [folder pathComponents]; |
---|
732 | int count; |
---|
733 | |
---|
734 | if ((count = [oldComponents count]) < [newComponents count] |
---|
735 | && [[newComponents objectAtIndex: count] isEqualToString: [self name]] |
---|
736 | && [oldComponents isEqualToArray: |
---|
737 | [newComponents objectsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, count)]]]) |
---|
738 | { |
---|
739 | NSAlert * alert = [[NSAlert alloc] init]; |
---|
740 | [alert setMessageText: NSLocalizedString(@"A folder cannot be moved to inside itself.", |
---|
741 | "Move inside itself alert -> title")]; |
---|
742 | [alert setInformativeText: [NSString stringWithFormat: |
---|
743 | NSLocalizedString(@"The move operation of \"%@\" cannot be done.", |
---|
744 | "Move inside itself alert -> message"), [self name]]]; |
---|
745 | [alert addButtonWithTitle: NSLocalizedString(@"OK", "Move inside itself alert -> button")]; |
---|
746 | |
---|
747 | [alert runModal]; |
---|
748 | [alert release]; |
---|
749 | |
---|
750 | return; |
---|
751 | } |
---|
752 | |
---|
753 | //pause without actually stopping |
---|
754 | tr_setDownloadLimit(fHandle, 0); |
---|
755 | tr_setUploadLimit(fHandle, 0); |
---|
756 | |
---|
757 | if ([[NSFileManager defaultManager] movePath: [oldFolder stringByAppendingPathComponent: [self name]] |
---|
758 | toPath: [folder stringByAppendingPathComponent: [self name]] handler: nil]) |
---|
759 | { |
---|
760 | //get rid of both incomplete folder and old download folder, even if move failed |
---|
761 | fUseIncompleteFolder = NO; |
---|
762 | if (fIncompleteFolder) |
---|
763 | { |
---|
764 | [fIncompleteFolder release]; |
---|
765 | fIncompleteFolder = nil; |
---|
766 | } |
---|
767 | [self changeDownloadFolder: folder]; |
---|
768 | |
---|
769 | [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateInfoSettings" object: nil]; |
---|
770 | |
---|
771 | [self updateSpeedSetting]; |
---|
772 | } |
---|
773 | else |
---|
774 | { |
---|
775 | [self updateSpeedSetting]; //restart before showing the alert |
---|
776 | |
---|
777 | NSAlert * alert = [[NSAlert alloc] init]; |
---|
778 | [alert setMessageText: NSLocalizedString(@"There was an error moving the data file.", "Move error alert -> title")]; |
---|
779 | [alert setInformativeText: [NSString stringWithFormat: |
---|
780 | NSLocalizedString(@"The move operation of \"%@\" cannot be done.", |
---|
781 | "Move error alert -> message"), [self name]]]; |
---|
782 | [alert addButtonWithTitle: NSLocalizedString(@"OK", "Move error alert -> button")]; |
---|
783 | |
---|
784 | [alert runModal]; |
---|
785 | [alert release]; |
---|
786 | } |
---|
787 | } |
---|
788 | } |
---|
789 | |
---|
790 | - (void) copyTorrentFileTo: (NSString *) path |
---|
791 | { |
---|
792 | [[NSFileManager defaultManager] copyPath: [self torrentLocation] toPath: path handler: nil]; |
---|
793 | } |
---|
794 | |
---|
795 | - (BOOL) alertForRemainingDiskSpace |
---|
796 | { |
---|
797 | if ([self allDownloaded] || ![fDefaults boolForKey: @"WarningRemainingSpace"]) |
---|
798 | return YES; |
---|
799 | |
---|
800 | NSString * volumeName; |
---|
801 | if ((volumeName = [[[NSFileManager defaultManager] componentsToDisplayForPath: [self downloadFolder]] objectAtIndex: 0])) |
---|
802 | { |
---|
803 | NSDictionary * fsAttributes = [[NSFileManager defaultManager] fileSystemAttributesAtPath: [self downloadFolder]]; |
---|
804 | uint64_t remainingSpace = [[fsAttributes objectForKey: NSFileSystemFreeSize] unsignedLongLongValue]; |
---|
805 | |
---|
806 | if (remainingSpace <= fStat->left) |
---|
807 | { |
---|
808 | NSAlert * alert = [[NSAlert alloc] init]; |
---|
809 | [alert setMessageText: [NSString stringWithFormat: |
---|
810 | NSLocalizedString(@"Not enough remaining disk space to download \"%@\" completely.", |
---|
811 | "Torrent file disk space alert -> title"), [self name]]]; |
---|
812 | [alert setInformativeText: [NSString stringWithFormat: |
---|
813 | NSLocalizedString(@"The transfer will be paused. Clear up space on \"%@\" to continue.", |
---|
814 | "Torrent file disk space alert -> message"), volumeName]]; |
---|
815 | [alert addButtonWithTitle: NSLocalizedString(@"OK", "Torrent file disk space alert -> button")]; |
---|
816 | [alert addButtonWithTitle: NSLocalizedString(@"Download Anyway", "Torrent file disk space alert -> button")]; |
---|
817 | [alert addButtonWithTitle: NSLocalizedString(@"Always Download", "Torrent file disk space alert -> button")]; |
---|
818 | |
---|
819 | int result = [alert runModal]; |
---|
820 | [alert release]; |
---|
821 | |
---|
822 | if (result == NSAlertThirdButtonReturn) |
---|
823 | [fDefaults setBool: NO forKey: @"WarningRemainingSpace"]; |
---|
824 | |
---|
825 | return result != NSAlertFirstButtonReturn; |
---|
826 | } |
---|
827 | } |
---|
828 | return YES; |
---|
829 | } |
---|
830 | |
---|
831 | - (BOOL) alertForFolderAvailable |
---|
832 | { |
---|
833 | if (access(tr_torrentGetFolder(fHandle), 0)) |
---|
834 | { |
---|
835 | NSAlert * alert = [[NSAlert alloc] init]; |
---|
836 | [alert setMessageText: [NSString stringWithFormat: |
---|
837 | NSLocalizedString(@"The folder for downloading \"%@\" cannot be found.", |
---|
838 | "Folder cannot be found alert -> title"), [self name]]]; |
---|
839 | [alert setInformativeText: [NSString stringWithFormat: |
---|
840 | NSLocalizedString(@"\"%@\" cannot be found. The transfer will be paused.", |
---|
841 | "Folder cannot be found alert -> message"), [self downloadFolder]]]; |
---|
842 | [alert addButtonWithTitle: NSLocalizedString(@"OK", "Folder cannot be found alert -> button")]; |
---|
843 | [alert addButtonWithTitle: [NSLocalizedString(@"Choose New Location", |
---|
844 | "Folder cannot be found alert -> location button") stringByAppendingEllipsis]]; |
---|
845 | |
---|
846 | if ([alert runModal] != NSAlertFirstButtonReturn) |
---|
847 | { |
---|
848 | NSOpenPanel * panel = [NSOpenPanel openPanel]; |
---|
849 | |
---|
850 | [panel setPrompt: NSLocalizedString(@"Select", "Folder cannot be found alert -> prompt")]; |
---|
851 | [panel setAllowsMultipleSelection: NO]; |
---|
852 | [panel setCanChooseFiles: NO]; |
---|
853 | [panel setCanChooseDirectories: YES]; |
---|
854 | [panel setCanCreateDirectories: YES]; |
---|
855 | |
---|
856 | [panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the download folder for \"%@\"", |
---|
857 | "Folder cannot be found alert -> select destination folder"), [self name]]]; |
---|
858 | |
---|
859 | [[NSNotificationCenter defaultCenter] postNotificationName: @"MakeWindowKey" object: nil]; |
---|
860 | [panel beginSheetForDirectory: nil file: nil types: nil modalForWindow: [NSApp keyWindow] modalDelegate: self |
---|
861 | didEndSelector: @selector(destinationChoiceClosed:returnCode:contextInfo:) contextInfo: nil]; |
---|
862 | } |
---|
863 | |
---|
864 | [alert release]; |
---|
865 | |
---|
866 | return NO; |
---|
867 | } |
---|
868 | |
---|
869 | return YES; |
---|
870 | } |
---|
871 | |
---|
872 | - (void) destinationChoiceClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) context |
---|
873 | { |
---|
874 | if (code != NSOKButton) |
---|
875 | return; |
---|
876 | |
---|
877 | NSString * folder = [[openPanel filenames] objectAtIndex: 0]; |
---|
878 | if (fUseIncompleteFolder) |
---|
879 | [self changeDownloadFolder: folder]; |
---|
880 | else |
---|
881 | [self changeDownloadFolder: folder]; |
---|
882 | |
---|
883 | [self startTransfer]; |
---|
884 | [self update]; |
---|
885 | |
---|
886 | [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateInfoSettings" object: nil]; |
---|
887 | } |
---|
888 | |
---|
889 | - (BOOL) alertForMoveFolderAvailable |
---|
890 | { |
---|
891 | if (access([fDownloadFolder UTF8String], 0)) |
---|
892 | { |
---|
893 | NSAlert * alert = [[NSAlert alloc] init]; |
---|
894 | [alert setMessageText: [NSString stringWithFormat: |
---|
895 | NSLocalizedString(@"The folder for moving the completed \"%@\" cannot be found.", |
---|
896 | "Move folder cannot be found alert -> title"), [self name]]]; |
---|
897 | [alert setInformativeText: [NSString stringWithFormat: |
---|
898 | NSLocalizedString(@"\"%@\" cannot be found. The file will remain in its current location.", |
---|
899 | "Move folder cannot be found alert -> message"), fDownloadFolder]]; |
---|
900 | [alert addButtonWithTitle: NSLocalizedString(@"OK", "Move folder cannot be found alert -> button")]; |
---|
901 | |
---|
902 | [alert runModal]; |
---|
903 | [alert release]; |
---|
904 | |
---|
905 | return NO; |
---|
906 | } |
---|
907 | |
---|
908 | return YES; |
---|
909 | } |
---|
910 | |
---|
911 | - (NSImage *) icon |
---|
912 | { |
---|
913 | return fIcon; |
---|
914 | } |
---|
915 | |
---|
916 | - (NSImage *) iconFlipped |
---|
917 | { |
---|
918 | if (!fIconFlipped) |
---|
919 | { |
---|
920 | fIconFlipped = [fIcon copy]; |
---|
921 | [fIconFlipped setFlipped: YES]; |
---|
922 | } |
---|
923 | return fIconFlipped; |
---|
924 | } |
---|
925 | |
---|
926 | - (NSImage *) iconSmall |
---|
927 | { |
---|
928 | if (!fIconSmall) |
---|
929 | { |
---|
930 | fIconSmall = [fIcon copy]; |
---|
931 | [fIconSmall setFlipped: YES]; |
---|
932 | [fIconSmall setScalesWhenResized: YES]; |
---|
933 | [fIconSmall setSize: NSMakeSize(16.0, 16.0)]; |
---|
934 | } |
---|
935 | return fIconSmall; |
---|
936 | } |
---|
937 | |
---|
938 | - (NSString *) name |
---|
939 | { |
---|
940 | return [NSString stringWithUTF8String: fInfo->name]; |
---|
941 | } |
---|
942 | |
---|
943 | - (uint64_t) size |
---|
944 | { |
---|
945 | return fInfo->totalSize; |
---|
946 | } |
---|
947 | |
---|
948 | - (NSString *) trackerAddress |
---|
949 | { |
---|
950 | return [NSString stringWithFormat: @"http://%s:%d", fStat->tracker->address, fStat->tracker->port]; |
---|
951 | } |
---|
952 | |
---|
953 | - (NSString *) trackerAddressAnnounce |
---|
954 | { |
---|
955 | return [NSString stringWithUTF8String: fStat->tracker->announce]; |
---|
956 | } |
---|
957 | |
---|
958 | - (NSString *) comment |
---|
959 | { |
---|
960 | return [NSString stringWithUTF8String: fInfo->comment]; |
---|
961 | } |
---|
962 | |
---|
963 | - (NSString *) creator |
---|
964 | { |
---|
965 | return [NSString stringWithUTF8String: fInfo->creator]; |
---|
966 | } |
---|
967 | |
---|
968 | - (NSDate *) dateCreated |
---|
969 | { |
---|
970 | int date = fInfo->dateCreated; |
---|
971 | return date > 0 ? [NSDate dateWithTimeIntervalSince1970: date] : nil; |
---|
972 | } |
---|
973 | |
---|
974 | - (int) pieceSize |
---|
975 | { |
---|
976 | return fInfo->pieceSize; |
---|
977 | } |
---|
978 | |
---|
979 | - (int) pieceCount |
---|
980 | { |
---|
981 | return fInfo->pieceCount; |
---|
982 | } |
---|
983 | |
---|
984 | - (NSString *) hashString |
---|
985 | { |
---|
986 | return [NSString stringWithUTF8String: fInfo->hashString]; |
---|
987 | } |
---|
988 | |
---|
989 | - (BOOL) privateTorrent |
---|
990 | { |
---|
991 | return TR_FLAG_PRIVATE & fInfo->flags; |
---|
992 | } |
---|
993 | |
---|
994 | - (NSString *) torrentLocation |
---|
995 | { |
---|
996 | return [NSString stringWithUTF8String: fInfo->torrent]; |
---|
997 | } |
---|
998 | |
---|
999 | - (NSString *) publicTorrentLocation |
---|
1000 | { |
---|
1001 | return fPublicTorrentLocation; |
---|
1002 | } |
---|
1003 | |
---|
1004 | - (NSString *) dataLocation |
---|
1005 | { |
---|
1006 | return [[self downloadFolder] stringByAppendingPathComponent: [self name]]; |
---|
1007 | } |
---|
1008 | |
---|
1009 | - (BOOL) publicTorrent |
---|
1010 | { |
---|
1011 | return fPublicTorrent; |
---|
1012 | } |
---|
1013 | |
---|
1014 | - (NSString *) stateString |
---|
1015 | { |
---|
1016 | switch( fStat->status ) |
---|
1017 | { |
---|
1018 | case TR_STATUS_STOPPED: |
---|
1019 | return NSLocalizedString(@"Paused", "Torrent -> status string"); |
---|
1020 | break; |
---|
1021 | |
---|
1022 | case TR_STATUS_CHECK: |
---|
1023 | return [NSLocalizedString(@"Checking existing data", "Torrent -> status string") stringByAppendingEllipsis]; |
---|
1024 | break; |
---|
1025 | |
---|
1026 | case TR_STATUS_DOWNLOAD: |
---|
1027 | return NSLocalizedString(@"Downloading", "Torrent -> status string"); |
---|
1028 | break; |
---|
1029 | |
---|
1030 | case TR_STATUS_SEED: |
---|
1031 | case TR_STATUS_DONE: |
---|
1032 | return NSLocalizedString(@"Seeding", "Torrent -> status string"); |
---|
1033 | break; |
---|
1034 | |
---|
1035 | case TR_STATUS_STOPPING: |
---|
1036 | return [NSLocalizedString(@"Stopping", "Torrent -> status string") stringByAppendingEllipsis]; |
---|
1037 | break; |
---|
1038 | |
---|
1039 | default: |
---|
1040 | return NSLocalizedString(@"N/A", "Torrent -> status string"); |
---|
1041 | } |
---|
1042 | } |
---|
1043 | |
---|
1044 | - (float) progress |
---|
1045 | { |
---|
1046 | return fStat->percentComplete; |
---|
1047 | } |
---|
1048 | |
---|
1049 | - (float) progressDone |
---|
1050 | { |
---|
1051 | return fStat->percentDone; |
---|
1052 | } |
---|
1053 | |
---|
1054 | - (int) eta |
---|
1055 | { |
---|
1056 | return fStat->eta; |
---|
1057 | } |
---|
1058 | |
---|
1059 | - (BOOL) isActive |
---|
1060 | { |
---|
1061 | return fStat->status & TR_STATUS_ACTIVE; |
---|
1062 | } |
---|
1063 | |
---|
1064 | - (BOOL) isSeeding |
---|
1065 | { |
---|
1066 | return fStat->status == TR_STATUS_SEED || fStat->status == TR_STATUS_DONE; |
---|
1067 | } |
---|
1068 | |
---|
1069 | - (BOOL) isPaused |
---|
1070 | { |
---|
1071 | return fStat->status == TR_STATUS_STOPPED; |
---|
1072 | } |
---|
1073 | |
---|
1074 | - (BOOL) isChecking |
---|
1075 | { |
---|
1076 | return fStat->status == TR_STATUS_CHECK || fStat->status == TR_STATUS_CHECK_WAIT; |
---|
1077 | } |
---|
1078 | |
---|
1079 | - (BOOL) allDownloaded |
---|
1080 | { |
---|
1081 | return fStat->cpStatus != TR_CP_INCOMPLETE; |
---|
1082 | } |
---|
1083 | |
---|
1084 | - (BOOL) isComplete |
---|
1085 | { |
---|
1086 | return fStat->cpStatus == TR_CP_COMPLETE; |
---|
1087 | } |
---|
1088 | |
---|
1089 | - (BOOL) isError |
---|
1090 | { |
---|
1091 | return fStat->error != 0; |
---|
1092 | } |
---|
1093 | |
---|
1094 | - (NSString *) errorMessage |
---|
1095 | { |
---|
1096 | if (![self isError]) |
---|
1097 | return @""; |
---|
1098 | |
---|
1099 | NSString * error; |
---|
1100 | if (!(error = [NSString stringWithUTF8String: fStat->errorString]) |
---|
1101 | && !(error = [NSString stringWithCString: fStat->errorString encoding: NSISOLatin1StringEncoding])) |
---|
1102 | error = NSLocalizedString(@"(unreadable error)", "Torrent -> error string unreadable"); |
---|
1103 | |
---|
1104 | return error; |
---|
1105 | } |
---|
1106 | |
---|
1107 | - (NSArray *) peers |
---|
1108 | { |
---|
1109 | int totalPeers, i; |
---|
1110 | tr_peer_stat_t * peers = tr_torrentPeers(fHandle, &totalPeers); |
---|
1111 | |
---|
1112 | NSMutableArray * peerDics = [NSMutableArray arrayWithCapacity: totalPeers]; |
---|
1113 | NSMutableDictionary * dic; |
---|
1114 | |
---|
1115 | tr_peer_stat_t * peer; |
---|
1116 | for (i = 0; i < totalPeers; i++) |
---|
1117 | { |
---|
1118 | peer = &peers[i]; |
---|
1119 | |
---|
1120 | dic = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
---|
1121 | [NSNumber numberWithBool: peer->isConnected], @"Connected", |
---|
1122 | [NSNumber numberWithInt: peer->from], @"From", |
---|
1123 | [NSString stringWithCString: (char *) peer->addr encoding: NSUTF8StringEncoding], @"IP", |
---|
1124 | [NSNumber numberWithInt: peer->port], @"Port", nil]; |
---|
1125 | |
---|
1126 | if (peer->isConnected) |
---|
1127 | { |
---|
1128 | [dic setObject: [NSNumber numberWithFloat: peer->progress] forKey: @"Progress"]; |
---|
1129 | |
---|
1130 | if (peer->isDownloading) |
---|
1131 | [dic setObject: [NSNumber numberWithFloat: peer->uploadToRate] forKey: @"UL To Rate"]; |
---|
1132 | if (peer->isUploading) |
---|
1133 | [dic setObject: [NSNumber numberWithFloat: peer->downloadFromRate] forKey: @"DL From Rate"]; |
---|
1134 | |
---|
1135 | [dic setObject: [NSString stringWithCString: (char *) peer->client encoding: NSUTF8StringEncoding] forKey: @"Client"]; |
---|
1136 | } |
---|
1137 | else |
---|
1138 | [dic setObject: @"" forKey: @"Client"]; |
---|
1139 | |
---|
1140 | [peerDics addObject: dic]; |
---|
1141 | } |
---|
1142 | |
---|
1143 | tr_torrentPeersFree(peers, totalPeers); |
---|
1144 | |
---|
1145 | return peerDics; |
---|
1146 | } |
---|
1147 | |
---|
1148 | - (NSString *) progressString |
---|
1149 | { |
---|
1150 | return fProgressString; |
---|
1151 | } |
---|
1152 | |
---|
1153 | - (NSString *) statusString |
---|
1154 | { |
---|
1155 | return fStatusString; |
---|
1156 | } |
---|
1157 | |
---|
1158 | - (NSString *) shortStatusString |
---|
1159 | { |
---|
1160 | return fShortStatusString; |
---|
1161 | } |
---|
1162 | |
---|
1163 | - (NSString *) remainingTimeString |
---|
1164 | { |
---|
1165 | return fRemainingTimeString; |
---|
1166 | } |
---|
1167 | |
---|
1168 | - (int) seeders |
---|
1169 | { |
---|
1170 | return fStat->seeders; |
---|
1171 | } |
---|
1172 | |
---|
1173 | - (int) leechers |
---|
1174 | { |
---|
1175 | return fStat->leechers; |
---|
1176 | } |
---|
1177 | |
---|
1178 | - (int) completedFromTracker |
---|
1179 | { |
---|
1180 | return fStat->completedFromTracker; |
---|
1181 | } |
---|
1182 | |
---|
1183 | - (int) totalPeersConnected |
---|
1184 | { |
---|
1185 | return fStat->peersConnected; |
---|
1186 | } |
---|
1187 | |
---|
1188 | - (int) totalPeersTracker |
---|
1189 | { |
---|
1190 | return fStat->peersFrom[TR_PEER_FROM_TRACKER]; |
---|
1191 | } |
---|
1192 | |
---|
1193 | - (int) totalPeersIncoming |
---|
1194 | { |
---|
1195 | return fStat->peersFrom[TR_PEER_FROM_INCOMING]; |
---|
1196 | } |
---|
1197 | |
---|
1198 | - (int) totalPeersCache |
---|
1199 | { |
---|
1200 | return fStat->peersFrom[TR_PEER_FROM_CACHE]; |
---|
1201 | } |
---|
1202 | |
---|
1203 | - (int) totalPeersPex |
---|
1204 | { |
---|
1205 | return fStat->peersFrom[TR_PEER_FROM_PEX]; |
---|
1206 | } |
---|
1207 | |
---|
1208 | //peers uploading to you |
---|
1209 | - (int) peersUploading |
---|
1210 | { |
---|
1211 | return fStat->peersUploading; |
---|
1212 | } |
---|
1213 | |
---|
1214 | //peers downloading from you |
---|
1215 | - (int) peersDownloading |
---|
1216 | { |
---|
1217 | return fStat->peersDownloading; |
---|
1218 | } |
---|
1219 | |
---|
1220 | - (float) downloadRate |
---|
1221 | { |
---|
1222 | return fStat->rateDownload; |
---|
1223 | } |
---|
1224 | |
---|
1225 | - (float) uploadRate |
---|
1226 | { |
---|
1227 | return fStat->rateUpload; |
---|
1228 | } |
---|
1229 | |
---|
1230 | - (uint64_t) downloadedValid |
---|
1231 | { |
---|
1232 | return fStat->downloadedValid; |
---|
1233 | } |
---|
1234 | |
---|
1235 | - (uint64_t) downloadedTotal |
---|
1236 | { |
---|
1237 | return fStat->downloaded; |
---|
1238 | } |
---|
1239 | |
---|
1240 | - (uint64_t) uploadedTotal |
---|
1241 | { |
---|
1242 | return fStat->uploaded; |
---|
1243 | } |
---|
1244 | |
---|
1245 | - (float) swarmSpeed |
---|
1246 | { |
---|
1247 | return fStat->swarmspeed; |
---|
1248 | } |
---|
1249 | |
---|
1250 | - (BOOL) pex |
---|
1251 | { |
---|
1252 | return fPex; |
---|
1253 | } |
---|
1254 | |
---|
1255 | - (void) setPex: (BOOL) setting |
---|
1256 | { |
---|
1257 | if (![self privateTorrent]) |
---|
1258 | { |
---|
1259 | fPex = setting; |
---|
1260 | tr_torrentDisablePex(fHandle, !setting); |
---|
1261 | } |
---|
1262 | } |
---|
1263 | |
---|
1264 | - (NSNumber *) orderValue |
---|
1265 | { |
---|
1266 | return [NSNumber numberWithInt: fOrderValue]; |
---|
1267 | } |
---|
1268 | |
---|
1269 | - (void) setOrderValue: (int) orderValue |
---|
1270 | { |
---|
1271 | fOrderValue = orderValue; |
---|
1272 | } |
---|
1273 | |
---|
1274 | - (NSArray *) fileList |
---|
1275 | { |
---|
1276 | return fFileList; |
---|
1277 | } |
---|
1278 | |
---|
1279 | - (int) fileCount |
---|
1280 | { |
---|
1281 | return fInfo->fileCount; |
---|
1282 | } |
---|
1283 | |
---|
1284 | - (float) fileProgress: (int) index |
---|
1285 | { |
---|
1286 | return tr_torrentFileCompletion(fHandle, index); |
---|
1287 | } |
---|
1288 | |
---|
1289 | - (int) checkForFiles: (NSIndexSet *) indexSet |
---|
1290 | { |
---|
1291 | BOOL onState = NO, offState = NO; |
---|
1292 | int index; |
---|
1293 | for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index]) |
---|
1294 | { |
---|
1295 | if (tr_torrentGetFileDL(fHandle, index) || ![self canChangeDownloadCheckForFiles: [NSIndexSet indexSetWithIndex: index]]) |
---|
1296 | onState = YES; |
---|
1297 | else |
---|
1298 | offState = YES; |
---|
1299 | |
---|
1300 | if (onState == offState) |
---|
1301 | return NSMixedState; |
---|
1302 | } |
---|
1303 | return onState ? NSOnState : NSOffState; |
---|
1304 | } |
---|
1305 | |
---|
1306 | - (BOOL) canChangeDownloadCheckForFiles: (NSIndexSet *) indexSet |
---|
1307 | { |
---|
1308 | if ([self fileCount] <= 1 || [self isComplete]) |
---|
1309 | return NO; |
---|
1310 | |
---|
1311 | int index; |
---|
1312 | for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index]) |
---|
1313 | if (tr_torrentGetFileStatus(fHandle, index) != TR_CP_COMPLETE) |
---|
1314 | return YES; |
---|
1315 | return NO; |
---|
1316 | } |
---|
1317 | |
---|
1318 | - (void) setFileCheckState: (int) state forIndexes: (NSIndexSet *) indexSet |
---|
1319 | { |
---|
1320 | int count = [indexSet count], i = 0, index; |
---|
1321 | int * files = malloc(count * sizeof(int)); |
---|
1322 | for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index]) |
---|
1323 | { |
---|
1324 | files[i] = index; |
---|
1325 | i++; |
---|
1326 | } |
---|
1327 | |
---|
1328 | tr_torrentSetFileDLs(fHandle, files, count, state != NSOffState); |
---|
1329 | free(files); |
---|
1330 | |
---|
1331 | [self update]; |
---|
1332 | if ([self isPaused]) |
---|
1333 | [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self]; //for paused torrents |
---|
1334 | } |
---|
1335 | |
---|
1336 | - (void) setFilePriority: (int) priority forIndexes: (NSIndexSet *) indexSet |
---|
1337 | { |
---|
1338 | int count = [indexSet count], i = 0, index; |
---|
1339 | int * files = malloc(count * sizeof(int)); |
---|
1340 | for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index]) |
---|
1341 | { |
---|
1342 | files[i] = index; |
---|
1343 | i++; |
---|
1344 | } |
---|
1345 | |
---|
1346 | tr_torrentSetFilePriorities(fHandle, files, count, priority); |
---|
1347 | free(files); |
---|
1348 | } |
---|
1349 | |
---|
1350 | - (BOOL) hasFilePriority: (int) priority forIndexes: (NSIndexSet *) indexSet |
---|
1351 | { |
---|
1352 | int index; |
---|
1353 | for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index]) |
---|
1354 | if (priority == tr_torrentGetFilePriority(fHandle, index) |
---|
1355 | && [self canChangeDownloadCheckForFiles: [NSIndexSet indexSetWithIndex: index]]) |
---|
1356 | return YES; |
---|
1357 | return NO; |
---|
1358 | } |
---|
1359 | |
---|
1360 | - (NSSet *) filePrioritiesForIndexes: (NSIndexSet *) indexSet |
---|
1361 | { |
---|
1362 | BOOL low = NO, normal = NO, high = NO; |
---|
1363 | NSMutableSet * priorities = [NSMutableSet setWithCapacity: 3]; |
---|
1364 | |
---|
1365 | int index, priority; |
---|
1366 | for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index]) |
---|
1367 | { |
---|
1368 | if (![self canChangeDownloadCheckForFiles: [NSIndexSet indexSetWithIndex: index]]) |
---|
1369 | continue; |
---|
1370 | |
---|
1371 | priority = tr_torrentGetFilePriority(fHandle, index); |
---|
1372 | if (priority == TR_PRI_LOW) |
---|
1373 | { |
---|
1374 | if (low) |
---|
1375 | continue; |
---|
1376 | low = YES; |
---|
1377 | } |
---|
1378 | else if (priority == TR_PRI_HIGH) |
---|
1379 | { |
---|
1380 | if (high) |
---|
1381 | continue; |
---|
1382 | high = YES; |
---|
1383 | } |
---|
1384 | else |
---|
1385 | { |
---|
1386 | if (normal) |
---|
1387 | continue; |
---|
1388 | normal = YES; |
---|
1389 | } |
---|
1390 | |
---|
1391 | [priorities addObject: [NSNumber numberWithInt: priority]]; |
---|
1392 | if (low && normal && high) |
---|
1393 | break; |
---|
1394 | } |
---|
1395 | return priorities; |
---|
1396 | } |
---|
1397 | |
---|
1398 | - (NSDate *) dateAdded |
---|
1399 | { |
---|
1400 | return fDateAdded; |
---|
1401 | } |
---|
1402 | |
---|
1403 | - (NSDate *) dateCompleted |
---|
1404 | { |
---|
1405 | return fDateCompleted; |
---|
1406 | } |
---|
1407 | |
---|
1408 | - (NSDate *) dateActivity |
---|
1409 | { |
---|
1410 | uint64_t date = fStat->activityDate; |
---|
1411 | return date != 0 ? [NSDate dateWithTimeIntervalSince1970: date / 1000] : fDateActivity; |
---|
1412 | } |
---|
1413 | |
---|
1414 | - (int) stalledMinutes |
---|
1415 | { |
---|
1416 | uint64_t start; |
---|
1417 | if ((start = fStat->startDate) == 0) |
---|
1418 | return -1; |
---|
1419 | |
---|
1420 | NSDate * started = [NSDate dateWithTimeIntervalSince1970: start / 1000], |
---|
1421 | * activity = [self dateActivity]; |
---|
1422 | if (!activity || [started compare: activity] == NSOrderedDescending) |
---|
1423 | return -1 * [started timeIntervalSinceNow] / 60; |
---|
1424 | else |
---|
1425 | return -1 * [activity timeIntervalSinceNow] / 60; |
---|
1426 | } |
---|
1427 | |
---|
1428 | - (BOOL) isStalled |
---|
1429 | { |
---|
1430 | return fStalled; |
---|
1431 | } |
---|
1432 | |
---|
1433 | - (NSNumber *) stateSortKey |
---|
1434 | { |
---|
1435 | if (![self isActive]) |
---|
1436 | return [NSNumber numberWithInt: 0]; |
---|
1437 | else if ([self isSeeding]) |
---|
1438 | return [NSNumber numberWithInt: 1]; |
---|
1439 | else |
---|
1440 | return [NSNumber numberWithInt: 2]; |
---|
1441 | } |
---|
1442 | |
---|
1443 | - (NSNumber *) progressSortKey |
---|
1444 | { |
---|
1445 | float progress; |
---|
1446 | if ((progress = [self progress]) >= 1.0) |
---|
1447 | progress += [self progressStopRatio]; |
---|
1448 | |
---|
1449 | return [NSNumber numberWithFloat: progress]; |
---|
1450 | } |
---|
1451 | |
---|
1452 | - (NSNumber *) ratioSortKey |
---|
1453 | { |
---|
1454 | return [NSNumber numberWithFloat: [self ratio]]; |
---|
1455 | } |
---|
1456 | |
---|
1457 | - (int) torrentID |
---|
1458 | { |
---|
1459 | return fID; |
---|
1460 | } |
---|
1461 | |
---|
1462 | - (const tr_info_t *) torrentInfo |
---|
1463 | { |
---|
1464 | return fInfo; |
---|
1465 | } |
---|
1466 | |
---|
1467 | - (tr_stat_t *) torrentStat |
---|
1468 | { |
---|
1469 | return fStat; |
---|
1470 | } |
---|
1471 | |
---|
1472 | @end |
---|
1473 | |
---|
1474 | @implementation Torrent (Private) |
---|
1475 | |
---|
1476 | //if a hash is given, attempt to load that; otherwise, attempt to open file at path |
---|
1477 | - (id) initWithHash: (NSString *) hashString path: (NSString *) path lib: (tr_handle_t *) lib |
---|
1478 | publicTorrent: (NSNumber *) publicTorrent |
---|
1479 | downloadFolder: (NSString *) downloadFolder |
---|
1480 | useIncompleteFolder: (NSNumber *) useIncompleteFolder incompleteFolder: (NSString *) incompleteFolder |
---|
1481 | dateAdded: (NSDate *) dateAdded dateCompleted: (NSDate *) dateCompleted |
---|
1482 | dateActivity: (NSDate *) dateActivity |
---|
1483 | ratioSetting: (NSNumber *) ratioSetting ratioLimit: (NSNumber *) ratioLimit |
---|
1484 | limitSpeedCustom: (NSNumber *) limitCustom |
---|
1485 | checkUpload: (NSNumber *) checkUpload uploadLimit: (NSNumber *) uploadLimit |
---|
1486 | checkDownload: (NSNumber *) checkDownload downloadLimit: (NSNumber *) downloadLimit |
---|
1487 | pex: (NSNumber *) pex |
---|
1488 | waitToStart: (NSNumber *) waitToStart orderValue: (NSNumber *) orderValue; |
---|
1489 | { |
---|
1490 | if (!(self = [super init])) |
---|
1491 | return nil; |
---|
1492 | |
---|
1493 | static_lastid++; |
---|
1494 | fID = static_lastid; |
---|
1495 | |
---|
1496 | fLib = lib; |
---|
1497 | fDefaults = [NSUserDefaults standardUserDefaults]; |
---|
1498 | |
---|
1499 | fPublicTorrent = path && (publicTorrent ? [publicTorrent boolValue] : ![fDefaults boolForKey: @"DeleteOriginalTorrent"]); |
---|
1500 | if (fPublicTorrent) |
---|
1501 | fPublicTorrentLocation = [path retain]; |
---|
1502 | |
---|
1503 | fDownloadFolder = downloadFolder ? downloadFolder : [fDefaults stringForKey: @"DownloadFolder"]; |
---|
1504 | fDownloadFolder = [[fDownloadFolder stringByExpandingTildeInPath] retain]; |
---|
1505 | |
---|
1506 | fUseIncompleteFolder = useIncompleteFolder ? [useIncompleteFolder boolValue] |
---|
1507 | : [fDefaults boolForKey: @"UseIncompleteDownloadFolder"]; |
---|
1508 | if (fUseIncompleteFolder) |
---|
1509 | { |
---|
1510 | fIncompleteFolder = incompleteFolder ? incompleteFolder : [fDefaults stringForKey: @"IncompleteDownloadFolder"]; |
---|
1511 | fIncompleteFolder = [[fIncompleteFolder stringByExpandingTildeInPath] retain]; |
---|
1512 | } |
---|
1513 | |
---|
1514 | NSString * currentDownloadFolder; |
---|
1515 | tr_info_t info; |
---|
1516 | int error; |
---|
1517 | if (hashString) |
---|
1518 | { |
---|
1519 | if (tr_torrentParseHash(fLib, [hashString UTF8String], NULL, &info) == TR_OK) |
---|
1520 | { |
---|
1521 | currentDownloadFolder = [self shouldUseIncompleteFolderForName: [NSString stringWithUTF8String: info.name]] |
---|
1522 | ? fIncompleteFolder : fDownloadFolder; |
---|
1523 | fHandle = tr_torrentInitSaved(fLib, [hashString UTF8String], [currentDownloadFolder UTF8String], |
---|
1524 | TR_FLAG_SAVE | TR_FLAG_PAUSED, &error); |
---|
1525 | } |
---|
1526 | tr_metainfoFree(&info); |
---|
1527 | } |
---|
1528 | if (!fHandle && path) |
---|
1529 | { |
---|
1530 | if (tr_torrentParse(fLib, [path UTF8String], NULL, &info) == TR_OK) |
---|
1531 | { |
---|
1532 | currentDownloadFolder = [self shouldUseIncompleteFolderForName: [NSString stringWithUTF8String: info.name]] |
---|
1533 | ? fIncompleteFolder : fDownloadFolder; |
---|
1534 | fHandle = tr_torrentInit(fLib, [path UTF8String], [currentDownloadFolder UTF8String], |
---|
1535 | TR_FLAG_SAVE | TR_FLAG_PAUSED, &error); |
---|
1536 | } |
---|
1537 | tr_metainfoFree(&info); |
---|
1538 | } |
---|
1539 | if (!fHandle) |
---|
1540 | { |
---|
1541 | [self release]; |
---|
1542 | return nil; |
---|
1543 | } |
---|
1544 | |
---|
1545 | fInfo = tr_torrentInfo(fHandle); |
---|
1546 | |
---|
1547 | NSNotificationCenter * nc = [NSNotificationCenter defaultCenter]; |
---|
1548 | [nc addObserver: self selector: @selector(updateSpeedSetting:) |
---|
1549 | name: @"UpdateSpeedSetting" object: nil]; |
---|
1550 | |
---|
1551 | fDateAdded = dateAdded ? [dateAdded retain] : [[NSDate alloc] init]; |
---|
1552 | if (dateCompleted) |
---|
1553 | fDateCompleted = [dateCompleted retain]; |
---|
1554 | if (dateActivity) |
---|
1555 | fDateActivity = [dateActivity retain]; |
---|
1556 | |
---|
1557 | fRatioSetting = ratioSetting ? [ratioSetting intValue] : NSMixedState; |
---|
1558 | fRatioLimit = ratioLimit ? [ratioLimit floatValue] : [fDefaults floatForKey: @"RatioLimit"]; |
---|
1559 | fFinishedSeeding = NO; |
---|
1560 | |
---|
1561 | fCheckUpload = checkUpload ? [checkUpload intValue] : NSMixedState; |
---|
1562 | fUploadLimit = uploadLimit ? [uploadLimit intValue] : [fDefaults integerForKey: @"UploadLimit"]; |
---|
1563 | fCheckDownload = checkDownload ? [checkDownload intValue] : NSMixedState; |
---|
1564 | fDownloadLimit = downloadLimit ? [downloadLimit intValue] : [fDefaults integerForKey: @"DownloadLimit"]; |
---|
1565 | [self updateSpeedSetting]; |
---|
1566 | |
---|
1567 | if ([self privateTorrent]) |
---|
1568 | fPex = NO; |
---|
1569 | else |
---|
1570 | fPex = pex ? [pex boolValue] : YES; |
---|
1571 | tr_torrentDisablePex(fHandle, !fPex); |
---|
1572 | |
---|
1573 | fWaitToStart = waitToStart ? [waitToStart boolValue] : [fDefaults boolForKey: @"AutoStartDownload"]; |
---|
1574 | fOrderValue = orderValue ? [orderValue intValue] : tr_torrentCount(fLib) - 1; |
---|
1575 | fError = NO; |
---|
1576 | |
---|
1577 | fIcon = [[[NSWorkspace sharedWorkspace] iconForFileType: fInfo->multifile ? NSFileTypeForHFSTypeCode('fldr') |
---|
1578 | : [[self name] pathExtension]] retain]; |
---|
1579 | |
---|
1580 | fProgressString = [[NSMutableString alloc] initWithCapacity: 50]; |
---|
1581 | fStatusString = [[NSMutableString alloc] initWithCapacity: 75]; |
---|
1582 | fShortStatusString = [[NSMutableString alloc] initWithCapacity: 30]; |
---|
1583 | fRemainingTimeString = [[NSMutableString alloc] initWithCapacity: 30]; |
---|
1584 | |
---|
1585 | [self createFileList]; |
---|
1586 | |
---|
1587 | //set up advanced bar |
---|
1588 | fBitmap = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: nil |
---|
1589 | pixelsWide: MAX_PIECES pixelsHigh: BAR_HEIGHT bitsPerSample: 8 samplesPerPixel: 4 hasAlpha: YES |
---|
1590 | isPlanar: NO colorSpaceName: NSCalibratedRGBColorSpace bytesPerRow: 0 bitsPerPixel: 0]; |
---|
1591 | |
---|
1592 | fPieces = malloc(MAX_PIECES); |
---|
1593 | int i; |
---|
1594 | for (i = 0; i < MAX_PIECES; i++) |
---|
1595 | fPieces[i] = BLANK_PIECE; |
---|
1596 | |
---|
1597 | [self update]; |
---|
1598 | return self; |
---|
1599 | } |
---|
1600 | |
---|
1601 | - (void) createFileList |
---|
1602 | { |
---|
1603 | int count = [self fileCount], i; |
---|
1604 | tr_file_t * file; |
---|
1605 | NSMutableArray * pathComponents; |
---|
1606 | NSString * path; |
---|
1607 | |
---|
1608 | NSMutableArray * fileList = [[NSMutableArray alloc] init], |
---|
1609 | * flatFileList = [[NSMutableArray alloc] initWithCapacity: count]; |
---|
1610 | |
---|
1611 | for (i = 0; i < count; i++) |
---|
1612 | { |
---|
1613 | file = &fInfo->files[i]; |
---|
1614 | |
---|
1615 | pathComponents = [[[NSString stringWithUTF8String: file->name] pathComponents] mutableCopy]; |
---|
1616 | if (fInfo->multifile) |
---|
1617 | { |
---|
1618 | path = [pathComponents objectAtIndex: 0]; |
---|
1619 | [pathComponents removeObjectAtIndex: 0]; |
---|
1620 | } |
---|
1621 | else |
---|
1622 | path = @""; |
---|
1623 | |
---|
1624 | [self insertPath: pathComponents forSiblings: fileList withParent: nil previousPath: path |
---|
1625 | flatList: flatFileList fileSize: file->length index: i]; |
---|
1626 | [pathComponents autorelease]; |
---|
1627 | } |
---|
1628 | |
---|
1629 | fFileList = [[NSArray alloc] initWithArray: fileList]; |
---|
1630 | [fileList release]; |
---|
1631 | fFlatFileList = [[NSArray alloc] initWithArray: flatFileList]; |
---|
1632 | [flatFileList release]; |
---|
1633 | } |
---|
1634 | |
---|
1635 | - (void) insertPath: (NSMutableArray *) components forSiblings: (NSMutableArray *) siblings |
---|
1636 | withParent: (NSMutableDictionary *) parent previousPath: (NSString *) previousPath |
---|
1637 | flatList: (NSMutableArray *) flatList fileSize: (uint64_t) size index: (int) index |
---|
1638 | { |
---|
1639 | NSString * name = [components objectAtIndex: 0]; |
---|
1640 | BOOL isFolder = [components count] > 1; |
---|
1641 | |
---|
1642 | NSMutableDictionary * dict = nil; |
---|
1643 | if (isFolder) |
---|
1644 | { |
---|
1645 | NSEnumerator * enumerator = [siblings objectEnumerator]; |
---|
1646 | while ((dict = [enumerator nextObject])) |
---|
1647 | if ([[dict objectForKey: @"IsFolder"] boolValue] && [[dict objectForKey: @"Name"] isEqualToString: name]) |
---|
1648 | break; |
---|
1649 | } |
---|
1650 | |
---|
1651 | NSString * currentPath = [previousPath stringByAppendingPathComponent: name]; |
---|
1652 | |
---|
1653 | //create new folder or item if it doesn't already exist |
---|
1654 | if (!dict) |
---|
1655 | { |
---|
1656 | dict = [NSMutableDictionary dictionaryWithObjectsAndKeys: name, @"Name", |
---|
1657 | [NSNumber numberWithBool: isFolder], @"IsFolder", currentPath, @"Path", nil]; |
---|
1658 | [siblings addObject: dict]; |
---|
1659 | |
---|
1660 | if (isFolder) |
---|
1661 | { |
---|
1662 | [dict setObject: [NSMutableArray array] forKey: @"Children"]; |
---|
1663 | [dict setObject: [NSMutableIndexSet indexSetWithIndex: index] forKey: @"Indexes"]; |
---|
1664 | } |
---|
1665 | else |
---|
1666 | { |
---|
1667 | [dict setObject: [NSIndexSet indexSetWithIndex: index] forKey: @"Indexes"]; |
---|
1668 | [dict setObject: [NSNumber numberWithUnsignedLongLong: size] forKey: @"Size"]; |
---|
1669 | [dict setObject: [[NSWorkspace sharedWorkspace] iconForFileType: [name pathExtension]] forKey: @"Icon"]; |
---|
1670 | |
---|
1671 | [flatList addObject: dict]; |
---|
1672 | } |
---|
1673 | |
---|
1674 | if (parent) |
---|
1675 | [dict setObject: parent forKey: @"Parent"]; |
---|
1676 | } |
---|
1677 | else |
---|
1678 | [[dict objectForKey: @"Indexes"] addIndex: index]; |
---|
1679 | |
---|
1680 | if (isFolder) |
---|
1681 | { |
---|
1682 | [components removeObjectAtIndex: 0]; |
---|
1683 | [self insertPath: components forSiblings: [dict objectForKey: @"Children"] |
---|
1684 | withParent: dict previousPath: currentPath flatList: flatList fileSize: size index: index]; |
---|
1685 | } |
---|
1686 | } |
---|
1687 | |
---|
1688 | - (BOOL) shouldUseIncompleteFolderForName: (NSString *) name |
---|
1689 | { |
---|
1690 | return fUseIncompleteFolder && |
---|
1691 | ![[NSFileManager defaultManager] fileExistsAtPath: [fDownloadFolder stringByAppendingPathComponent: name]]; |
---|
1692 | } |
---|
1693 | |
---|
1694 | - (void) updateDownloadFolder |
---|
1695 | { |
---|
1696 | NSString * folder = [self shouldUseIncompleteFolderForName: [self name]] ? fIncompleteFolder : fDownloadFolder; |
---|
1697 | tr_torrentSetFolder(fHandle, [folder UTF8String]); |
---|
1698 | } |
---|
1699 | |
---|
1700 | #warning move? |
---|
1701 | - (NSImage *) advancedBar |
---|
1702 | { |
---|
1703 | uint32_t * p; |
---|
1704 | uint8_t * bitmapData = [fBitmap bitmapData]; |
---|
1705 | int bytesPerRow = [fBitmap bytesPerRow]; |
---|
1706 | |
---|
1707 | int pieceCount = [self pieceCount]; |
---|
1708 | int8_t * piecesAvailablity = malloc(pieceCount); |
---|
1709 | [self getAvailability: piecesAvailablity size: pieceCount]; |
---|
1710 | |
---|
1711 | //lines 2 to 14: blue, green, or gray depending on piece availability |
---|
1712 | int i, h, index = 0; |
---|
1713 | float increment = (float)pieceCount / (float)MAX_PIECES, indexValue = 0; |
---|
1714 | uint32_t color; |
---|
1715 | BOOL change; |
---|
1716 | for (i = 0; i < MAX_PIECES; i++) |
---|
1717 | { |
---|
1718 | change = NO; |
---|
1719 | if (piecesAvailablity[index] < 0) |
---|
1720 | { |
---|
1721 | if (fPieces[i] != -1) |
---|
1722 | { |
---|
1723 | color = kBlue; |
---|
1724 | fPieces[i] = -1; |
---|
1725 | change = YES; |
---|
1726 | } |
---|
1727 | } |
---|
1728 | else if (piecesAvailablity[index] == 0) |
---|
1729 | { |
---|
1730 | if (fPieces[i] != 0) |
---|
1731 | { |
---|
1732 | color = kGray; |
---|
1733 | fPieces[i] = 0; |
---|
1734 | change = YES; |
---|
1735 | } |
---|
1736 | } |
---|
1737 | else if (piecesAvailablity[index] <= 4) |
---|
1738 | { |
---|
1739 | if (fPieces[i] != 1) |
---|
1740 | { |
---|
1741 | color = kGreen1; |
---|
1742 | fPieces[i] = 1; |
---|
1743 | change = YES; |
---|
1744 | } |
---|
1745 | } |
---|
1746 | else if (piecesAvailablity[index] <= 8) |
---|
1747 | { |
---|
1748 | if (fPieces[i] != 2) |
---|
1749 | { |
---|
1750 | color = kGreen2; |
---|
1751 | fPieces[i] = 2; |
---|
1752 | change = YES; |
---|
1753 | } |
---|
1754 | } |
---|
1755 | else |
---|
1756 | { |
---|
1757 | if (fPieces[i] != 3) |
---|
1758 | { |
---|
1759 | color = kGreen3; |
---|
1760 | fPieces[i] = 3; |
---|
1761 | change = YES; |
---|
1762 | } |
---|
1763 | } |
---|
1764 | |
---|
1765 | if (change) |
---|
1766 | { |
---|
1767 | //point to pixel (i, 2) and draw "vertically" |
---|
1768 | p = (uint32_t *)(bitmapData + 2 * bytesPerRow) + i; |
---|
1769 | for (h = 2; h < BAR_HEIGHT; h++) |
---|
1770 | { |
---|
1771 | p[0] = color; |
---|
1772 | p = (uint32_t *)((uint8_t *)p + bytesPerRow); |
---|
1773 | } |
---|
1774 | } |
---|
1775 | |
---|
1776 | indexValue += increment; |
---|
1777 | index = (int)indexValue; |
---|
1778 | } |
---|
1779 | |
---|
1780 | //determine percentage finished and available |
---|
1781 | int have = rintf((float)MAX_PIECES * [self progress]), avail; |
---|
1782 | if ([self progress] >= 1.0 || ![self isActive] || [self totalPeersConnected] <= 0) |
---|
1783 | avail = 0; |
---|
1784 | else |
---|
1785 | { |
---|
1786 | float * piecesFinished = malloc(pieceCount * sizeof(float)); |
---|
1787 | [self getAmountFinished: piecesFinished size: pieceCount]; |
---|
1788 | |
---|
1789 | float available = 0; |
---|
1790 | for (i = 0; i < pieceCount; i++) |
---|
1791 | if (piecesAvailablity[i] > 0) |
---|
1792 | available += 1.0 - piecesFinished[i]; |
---|
1793 | |
---|
1794 | avail = rintf((float)MAX_PIECES * available / (float)pieceCount); |
---|
1795 | if (have + avail > MAX_PIECES) //case if both end in .5 and all pieces are available |
---|
1796 | avail--; |
---|
1797 | |
---|
1798 | free(piecesFinished); |
---|
1799 | } |
---|
1800 | |
---|
1801 | free(piecesAvailablity); |
---|
1802 | |
---|
1803 | //first two lines: dark blue to show progression, green to show available |
---|
1804 | p = (uint32_t *)bitmapData; |
---|
1805 | for (i = 0; i < have; i++) |
---|
1806 | { |
---|
1807 | p[i] = kBlue2; |
---|
1808 | p[i + bytesPerRow / 4] = kBlue2; |
---|
1809 | } |
---|
1810 | for (; i < avail + have; i++) |
---|
1811 | { |
---|
1812 | p[i] = kGreen3; |
---|
1813 | p[i + bytesPerRow / 4] = kGreen3; |
---|
1814 | } |
---|
1815 | for (; i < MAX_PIECES; i++) |
---|
1816 | { |
---|
1817 | p[i] = kWhite; |
---|
1818 | p[i + bytesPerRow / 4] = kWhite; |
---|
1819 | } |
---|
1820 | |
---|
1821 | //actually draw image |
---|
1822 | NSImage * bar = [[NSImage alloc] initWithSize: [fBitmap size]]; |
---|
1823 | [bar addRepresentation: fBitmap]; |
---|
1824 | [bar setScalesWhenResized: YES]; |
---|
1825 | |
---|
1826 | return [bar autorelease]; |
---|
1827 | } |
---|
1828 | |
---|
1829 | - (void) trashFile: (NSString *) path |
---|
1830 | { |
---|
1831 | //attempt to move to trash |
---|
1832 | if (![[NSWorkspace sharedWorkspace] performFileOperation: NSWorkspaceRecycleOperation |
---|
1833 | source: [path stringByDeletingLastPathComponent] destination: @"" |
---|
1834 | files: [NSArray arrayWithObject: [path lastPathComponent]] tag: nil]) |
---|
1835 | { |
---|
1836 | //if cannot trash, just delete it (will work if it is on a remote volume) |
---|
1837 | if (![[NSFileManager defaultManager] removeFileAtPath: path handler: nil]) |
---|
1838 | NSLog(@"Could not trash %@", path); |
---|
1839 | } |
---|
1840 | } |
---|
1841 | |
---|
1842 | @end |
---|