1 | /****************************************************************************** |
---|
2 | * $Id: Torrent.m 6047 2008-06-05 05:00:24Z livings124 $ |
---|
3 | * |
---|
4 | * Copyright (c) 2006-2008 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 "GroupsController.h" |
---|
27 | #import "FileListNode.h" |
---|
28 | #import "NSApplicationAdditions.h" |
---|
29 | #import "NSStringAdditions.h" |
---|
30 | #include "utils.h" //tr_httpIsValidURL |
---|
31 | |
---|
32 | @interface Torrent (Private) |
---|
33 | |
---|
34 | - (id) initWithHash: (NSString *) hashString path: (NSString *) path torrentStruct: (tr_torrent *) torrentStruct lib: (tr_handle *) lib |
---|
35 | publicTorrent: (NSNumber *) publicTorrent |
---|
36 | downloadFolder: (NSString *) downloadFolder |
---|
37 | useIncompleteFolder: (NSNumber *) useIncompleteFolder incompleteFolder: (NSString *) incompleteFolder |
---|
38 | ratioSetting: (NSNumber *) ratioSetting ratioLimit: (NSNumber *) ratioLimit |
---|
39 | waitToStart: (NSNumber *) waitToStart |
---|
40 | orderValue: (NSNumber *) orderValue groupValue: (NSNumber *) groupValue addedTrackers: (NSNumber *) addedTrackers; |
---|
41 | |
---|
42 | - (BOOL) shouldUseIncompleteFolderForName: (NSString *) name; |
---|
43 | - (void) updateDownloadFolder; |
---|
44 | |
---|
45 | - (void) createFileList; |
---|
46 | - (void) insertPath: (NSMutableArray *) components forParent: (FileListNode *) parent fileSize: (uint64_t) size index: (int) index; |
---|
47 | |
---|
48 | - (void) completenessChange: (NSNumber *) status; |
---|
49 | |
---|
50 | - (void) quickPause; |
---|
51 | - (void) endQuickPause; |
---|
52 | |
---|
53 | - (NSString *) etaString: (int) eta; |
---|
54 | |
---|
55 | - (void) updateAllTrackers: (NSMutableArray *) trackers; |
---|
56 | |
---|
57 | - (void) trashFile: (NSString *) path; |
---|
58 | |
---|
59 | - (void) setTimeMachineExclude: (BOOL) exclude forPath: (NSString *) path; |
---|
60 | |
---|
61 | @end |
---|
62 | |
---|
63 | void completenessChangeCallback(tr_torrent * torrent, cp_status_t status, void * torrentData) |
---|
64 | { |
---|
65 | [(Torrent *)torrentData performSelectorOnMainThread: @selector(completenessChange:) |
---|
66 | withObject: [[NSNumber alloc] initWithInt: status] waitUntilDone: NO]; |
---|
67 | } |
---|
68 | |
---|
69 | @implementation Torrent |
---|
70 | |
---|
71 | - (id) initWithPath: (NSString *) path location: (NSString *) location deleteTorrentFile: (torrentFileState) torrentDelete |
---|
72 | lib: (tr_handle *) lib |
---|
73 | { |
---|
74 | self = [self initWithHash: nil path: path torrentStruct: NULL lib: lib |
---|
75 | publicTorrent: torrentDelete != TORRENT_FILE_DEFAULT |
---|
76 | ? [NSNumber numberWithBool: torrentDelete == TORRENT_FILE_SAVE] : nil |
---|
77 | downloadFolder: location |
---|
78 | useIncompleteFolder: nil incompleteFolder: nil |
---|
79 | ratioSetting: nil ratioLimit: nil |
---|
80 | waitToStart: nil orderValue: nil groupValue: nil addedTrackers: nil]; |
---|
81 | |
---|
82 | if (self) |
---|
83 | { |
---|
84 | if (!fPublicTorrent) |
---|
85 | [self trashFile: path]; |
---|
86 | } |
---|
87 | return self; |
---|
88 | } |
---|
89 | |
---|
90 | - (id) initWithTorrentStruct: (tr_torrent *) torrentStruct location: (NSString *) location lib: (tr_handle *) lib |
---|
91 | { |
---|
92 | self = [self initWithHash: nil path: nil torrentStruct: torrentStruct lib: lib |
---|
93 | publicTorrent: [NSNumber numberWithBool: NO] |
---|
94 | downloadFolder: location |
---|
95 | useIncompleteFolder: nil incompleteFolder: nil |
---|
96 | ratioSetting: nil ratioLimit: nil |
---|
97 | waitToStart: nil orderValue: nil groupValue: nil addedTrackers: nil]; |
---|
98 | |
---|
99 | return self; |
---|
100 | } |
---|
101 | |
---|
102 | - (id) initWithHistory: (NSDictionary *) history lib: (tr_handle *) lib |
---|
103 | { |
---|
104 | self = [self initWithHash: [history objectForKey: @"TorrentHash"] |
---|
105 | path: [history objectForKey: @"TorrentPath"] torrentStruct: NULL lib: lib |
---|
106 | publicTorrent: [history objectForKey: @"PublicCopy"] |
---|
107 | downloadFolder: [history objectForKey: @"DownloadFolder"] |
---|
108 | useIncompleteFolder: [history objectForKey: @"UseIncompleteFolder"] |
---|
109 | incompleteFolder: [history objectForKey: @"IncompleteFolder"] |
---|
110 | ratioSetting: [history objectForKey: @"RatioSetting"] |
---|
111 | ratioLimit: [history objectForKey: @"RatioLimit"] |
---|
112 | waitToStart: [history objectForKey: @"WaitToStart"] |
---|
113 | orderValue: [history objectForKey: @"OrderValue"] |
---|
114 | groupValue: [history objectForKey: @"GroupValue"] |
---|
115 | addedTrackers: [history objectForKey: @"AddedTrackers"]]; |
---|
116 | |
---|
117 | if (self) |
---|
118 | { |
---|
119 | //start transfer |
---|
120 | NSNumber * active; |
---|
121 | if ((active = [history objectForKey: @"Active"]) && [active boolValue]) |
---|
122 | { |
---|
123 | fStat = tr_torrentStat(fHandle); |
---|
124 | [self startTransfer]; |
---|
125 | } |
---|
126 | |
---|
127 | #warning remove after 1.3 (from libT as well) |
---|
128 | //get old added, activity, and done dates |
---|
129 | NSDate * date; |
---|
130 | if ((date = [history objectForKey: @"Date"])) |
---|
131 | tr_torrentSetAddedDate(fHandle, [date timeIntervalSince1970]); |
---|
132 | if ((date = [history objectForKey: @"DateActivity"])) |
---|
133 | tr_torrentSetActivityDate(fHandle, [date timeIntervalSince1970]); |
---|
134 | if ((date = [history objectForKey: @"DateCompleted"])) |
---|
135 | tr_torrentSetDoneDate(fHandle, [date timeIntervalSince1970]); |
---|
136 | } |
---|
137 | return self; |
---|
138 | } |
---|
139 | |
---|
140 | - (NSDictionary *) history |
---|
141 | { |
---|
142 | NSMutableDictionary * history = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
---|
143 | [NSNumber numberWithBool: fPublicTorrent], @"PublicCopy", |
---|
144 | [self hashString], @"TorrentHash", |
---|
145 | fDownloadFolder, @"DownloadFolder", |
---|
146 | [NSNumber numberWithBool: fUseIncompleteFolder], @"UseIncompleteFolder", |
---|
147 | [NSNumber numberWithBool: [self isActive]], @"Active", |
---|
148 | [NSNumber numberWithInt: fRatioSetting], @"RatioSetting", |
---|
149 | [NSNumber numberWithFloat: fRatioLimit], @"RatioLimit", |
---|
150 | [NSNumber numberWithBool: fWaitToStart], @"WaitToStart", |
---|
151 | [NSNumber numberWithInt: fOrderValue], @"OrderValue", |
---|
152 | [NSNumber numberWithInt: fGroupValue], @"GroupValue", |
---|
153 | [NSNumber numberWithBool: fAddedTrackers], @"AddedTrackers", nil]; |
---|
154 | |
---|
155 | if (fIncompleteFolder) |
---|
156 | [history setObject: fIncompleteFolder forKey: @"IncompleteFolder"]; |
---|
157 | |
---|
158 | if (fPublicTorrent) |
---|
159 | [history setObject: [self publicTorrentLocation] forKey: @"TorrentPath"]; |
---|
160 | |
---|
161 | return history; |
---|
162 | } |
---|
163 | |
---|
164 | - (void) dealloc |
---|
165 | { |
---|
166 | [[NSNotificationCenter defaultCenter] removeObserver: self]; |
---|
167 | |
---|
168 | if (fFileStat) |
---|
169 | tr_torrentFilesFree(fFileStat, [self fileCount]); |
---|
170 | |
---|
171 | if (fPreviousFinishedPieces != NULL) |
---|
172 | free(fPreviousFinishedPieces); |
---|
173 | [fFinishedPiecesDate release]; |
---|
174 | |
---|
175 | [fNameString release]; |
---|
176 | [fHashString release]; |
---|
177 | |
---|
178 | [fDownloadFolder release]; |
---|
179 | [fIncompleteFolder release]; |
---|
180 | |
---|
181 | [fPublicTorrentLocation release]; |
---|
182 | |
---|
183 | [fIcon release]; |
---|
184 | |
---|
185 | [fFileList release]; |
---|
186 | |
---|
187 | [fQuickPauseDict release]; |
---|
188 | |
---|
189 | [super dealloc]; |
---|
190 | } |
---|
191 | |
---|
192 | - (NSString *) description |
---|
193 | { |
---|
194 | return [@"Torrent: " stringByAppendingString: [self name]]; |
---|
195 | } |
---|
196 | |
---|
197 | - (void) closeRemoveTorrentInterface |
---|
198 | { |
---|
199 | //allow the file to be index by Time Machine |
---|
200 | [self setTimeMachineExclude: NO forPath: [[self downloadFolder] stringByAppendingPathComponent: [self name]]]; |
---|
201 | } |
---|
202 | |
---|
203 | - (void) closeRemoveTorrent |
---|
204 | { |
---|
205 | [self closeRemoveTorrentInterface]; |
---|
206 | |
---|
207 | tr_torrentRemove(fHandle); |
---|
208 | } |
---|
209 | |
---|
210 | - (void) changeIncompleteDownloadFolder: (NSString *) folder |
---|
211 | { |
---|
212 | fUseIncompleteFolder = folder != nil; |
---|
213 | |
---|
214 | [fIncompleteFolder release]; |
---|
215 | fIncompleteFolder = fUseIncompleteFolder ? [folder retain] : nil; |
---|
216 | |
---|
217 | [self updateDownloadFolder]; |
---|
218 | } |
---|
219 | |
---|
220 | - (void) changeDownloadFolder: (NSString *) folder |
---|
221 | { |
---|
222 | [fDownloadFolder release]; |
---|
223 | fDownloadFolder = [folder retain]; |
---|
224 | |
---|
225 | [self updateDownloadFolder]; |
---|
226 | } |
---|
227 | |
---|
228 | - (NSString *) downloadFolder |
---|
229 | { |
---|
230 | return [NSString stringWithUTF8String: tr_torrentGetDownloadDir(fHandle)]; |
---|
231 | } |
---|
232 | |
---|
233 | - (void) getAvailability: (int8_t *) tab size: (int) size |
---|
234 | { |
---|
235 | tr_torrentAvailability(fHandle, tab, size); |
---|
236 | } |
---|
237 | |
---|
238 | - (void) getAmountFinished: (float *) tab size: (int) size |
---|
239 | { |
---|
240 | tr_torrentAmountFinished(fHandle, tab, size); |
---|
241 | } |
---|
242 | |
---|
243 | - (float *) getPreviousAmountFinished |
---|
244 | { |
---|
245 | if (fFinishedPiecesDate && [fFinishedPiecesDate timeIntervalSinceNow] > -2.0) |
---|
246 | return fPreviousFinishedPieces; |
---|
247 | else |
---|
248 | return NULL; |
---|
249 | } |
---|
250 | |
---|
251 | -(void) setPreviousAmountFinished: (float *) tab |
---|
252 | { |
---|
253 | if (fPreviousFinishedPieces != NULL) |
---|
254 | free(fPreviousFinishedPieces); |
---|
255 | fPreviousFinishedPieces = tab; |
---|
256 | |
---|
257 | [fFinishedPiecesDate release]; |
---|
258 | fFinishedPiecesDate = tab != NULL ? [[NSDate alloc] init] : nil; |
---|
259 | } |
---|
260 | |
---|
261 | #warning when queue and seeding options are folded into libt, no need to call this on all torrents - use tr_torrentGetStatus |
---|
262 | - (void) update |
---|
263 | { |
---|
264 | //get previous status values before update |
---|
265 | BOOL wasChecking = NO, wasError = NO, wasStalled = NO; |
---|
266 | if (fStat != NULL) |
---|
267 | { |
---|
268 | wasChecking = [self isChecking]; |
---|
269 | wasError = [self isError]; |
---|
270 | wasStalled = fStalled; |
---|
271 | } |
---|
272 | |
---|
273 | fStat = tr_torrentStat(fHandle); |
---|
274 | |
---|
275 | //check to stop for ratio |
---|
276 | float stopRatio; |
---|
277 | if ([self isSeeding] && (stopRatio = [self actualStopRatio]) != INVALID && [self ratio] >= stopRatio) |
---|
278 | { |
---|
279 | [self setRatioSetting: NSOffState]; |
---|
280 | [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentStoppedForRatio" object: self]; |
---|
281 | |
---|
282 | [self stopTransfer]; |
---|
283 | fStat = tr_torrentStat(fHandle); |
---|
284 | |
---|
285 | fFinishedSeeding = YES; |
---|
286 | } |
---|
287 | |
---|
288 | //check if stalled (stored because based on time and needs to check if it was previously stalled) |
---|
289 | fStalled = [self isActive] && [fDefaults boolForKey: @"CheckStalled"] |
---|
290 | && [self stalledMinutes] > [fDefaults integerForKey: @"StalledMinutes"]; |
---|
291 | |
---|
292 | //update queue for checking (from downloading to seeding), stalled, or error |
---|
293 | if ((wasChecking && ![self isChecking]) || (wasStalled != fStalled) || (!wasError && [self isError] && [self isActive])) |
---|
294 | [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self]; |
---|
295 | } |
---|
296 | |
---|
297 | - (void) startTransfer |
---|
298 | { |
---|
299 | fWaitToStart = NO; |
---|
300 | fFinishedSeeding = NO; |
---|
301 | |
---|
302 | if (![self isActive] && [self alertForFolderAvailable] && [self alertForRemainingDiskSpace]) |
---|
303 | { |
---|
304 | tr_torrentStart(fHandle); |
---|
305 | [self update]; |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | - (void) stopTransfer |
---|
310 | { |
---|
311 | fWaitToStart = NO; |
---|
312 | |
---|
313 | if ([self isActive]) |
---|
314 | { |
---|
315 | tr_torrentStop(fHandle); |
---|
316 | [self update]; |
---|
317 | |
---|
318 | [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateQueue" object: self]; |
---|
319 | } |
---|
320 | } |
---|
321 | |
---|
322 | - (void) sleep |
---|
323 | { |
---|
324 | if ((fResumeOnWake = [self isActive])) |
---|
325 | tr_torrentStop(fHandle); |
---|
326 | } |
---|
327 | |
---|
328 | - (void) wakeUp |
---|
329 | { |
---|
330 | if (fResumeOnWake) |
---|
331 | tr_torrentStart(fHandle); |
---|
332 | } |
---|
333 | |
---|
334 | - (void) manualAnnounce |
---|
335 | { |
---|
336 | tr_torrentManualUpdate(fHandle); |
---|
337 | } |
---|
338 | |
---|
339 | - (BOOL) canManualAnnounce |
---|
340 | { |
---|
341 | return tr_torrentCanManualUpdate(fHandle); |
---|
342 | } |
---|
343 | |
---|
344 | - (void) resetCache |
---|
345 | { |
---|
346 | tr_torrentVerify(fHandle); |
---|
347 | [self update]; |
---|
348 | } |
---|
349 | |
---|
350 | - (float) ratio |
---|
351 | { |
---|
352 | return fStat->ratio; |
---|
353 | } |
---|
354 | |
---|
355 | - (int) ratioSetting |
---|
356 | { |
---|
357 | return fRatioSetting; |
---|
358 | } |
---|
359 | |
---|
360 | - (void) setRatioSetting: (int) setting |
---|
361 | { |
---|
362 | fRatioSetting = setting; |
---|
363 | } |
---|
364 | |
---|
365 | - (float) ratioLimit |
---|
366 | { |
---|
367 | return fRatioLimit; |
---|
368 | } |
---|
369 | |
---|
370 | - (void) setRatioLimit: (float) limit |
---|
371 | { |
---|
372 | if (limit >= 0) |
---|
373 | fRatioLimit = limit; |
---|
374 | } |
---|
375 | |
---|
376 | - (float) actualStopRatio |
---|
377 | { |
---|
378 | if (fRatioSetting == NSOnState) |
---|
379 | return fRatioLimit; |
---|
380 | else if (fRatioSetting == NSMixedState && [fDefaults boolForKey: @"RatioCheck"]) |
---|
381 | return [fDefaults floatForKey: @"RatioLimit"]; |
---|
382 | else |
---|
383 | return INVALID; |
---|
384 | } |
---|
385 | |
---|
386 | - (float) progressStopRatio |
---|
387 | { |
---|
388 | float stopRatio, ratio; |
---|
389 | if ((stopRatio = [self actualStopRatio]) == INVALID || (ratio = [self ratio]) >= stopRatio) |
---|
390 | return 1.0; |
---|
391 | else if (stopRatio > 0) |
---|
392 | return ratio / stopRatio; |
---|
393 | else |
---|
394 | return 0; |
---|
395 | } |
---|
396 | |
---|
397 | - (tr_speedlimit) speedMode: (BOOL) upload |
---|
398 | { |
---|
399 | return tr_torrentGetSpeedMode(fHandle, upload ? TR_UP : TR_DOWN); |
---|
400 | } |
---|
401 | |
---|
402 | - (void) setSpeedMode: (tr_speedlimit) mode upload: (BOOL) upload |
---|
403 | { |
---|
404 | tr_torrentSetSpeedMode(fHandle, upload ? TR_UP : TR_DOWN, mode); |
---|
405 | } |
---|
406 | |
---|
407 | - (int) speedLimit: (BOOL) upload |
---|
408 | { |
---|
409 | return tr_torrentGetSpeedLimit(fHandle, upload ? TR_UP : TR_DOWN); |
---|
410 | } |
---|
411 | |
---|
412 | - (void) setSpeedLimit: (int) limit upload: (BOOL) upload |
---|
413 | { |
---|
414 | tr_torrentSetSpeedLimit(fHandle, upload ? TR_UP : TR_DOWN, limit); |
---|
415 | } |
---|
416 | |
---|
417 | - (void) setMaxPeerConnect: (uint16_t) count |
---|
418 | { |
---|
419 | if (count > 0) |
---|
420 | tr_torrentSetPeerLimit(fHandle, count); |
---|
421 | } |
---|
422 | |
---|
423 | - (uint16_t) maxPeerConnect |
---|
424 | { |
---|
425 | return tr_torrentGetPeerLimit(fHandle); |
---|
426 | } |
---|
427 | |
---|
428 | - (void) setWaitToStart: (BOOL) wait |
---|
429 | { |
---|
430 | fWaitToStart = wait; |
---|
431 | } |
---|
432 | |
---|
433 | - (BOOL) waitingToStart |
---|
434 | { |
---|
435 | return fWaitToStart; |
---|
436 | } |
---|
437 | |
---|
438 | - (void) revealData |
---|
439 | { |
---|
440 | [[NSWorkspace sharedWorkspace] selectFile: [self dataLocation] inFileViewerRootedAtPath: nil]; |
---|
441 | } |
---|
442 | |
---|
443 | - (void) revealPublicTorrent |
---|
444 | { |
---|
445 | if (fPublicTorrent) |
---|
446 | [[NSWorkspace sharedWorkspace] selectFile: fPublicTorrentLocation inFileViewerRootedAtPath: nil]; |
---|
447 | } |
---|
448 | |
---|
449 | - (void) trashData |
---|
450 | { |
---|
451 | [self trashFile: [self dataLocation]]; |
---|
452 | } |
---|
453 | |
---|
454 | - (void) trashTorrent |
---|
455 | { |
---|
456 | if (fPublicTorrent) |
---|
457 | { |
---|
458 | [self trashFile: fPublicTorrentLocation]; |
---|
459 | [fPublicTorrentLocation release]; |
---|
460 | fPublicTorrentLocation = nil; |
---|
461 | |
---|
462 | fPublicTorrent = NO; |
---|
463 | } |
---|
464 | } |
---|
465 | |
---|
466 | - (void) moveTorrentDataFileTo: (NSString *) folder |
---|
467 | { |
---|
468 | NSString * oldFolder = [self downloadFolder]; |
---|
469 | if (![oldFolder isEqualToString: folder] || ![fDownloadFolder isEqualToString: folder]) |
---|
470 | { |
---|
471 | //check if moving inside itself |
---|
472 | NSArray * oldComponents = [oldFolder pathComponents], |
---|
473 | * newComponents = [folder pathComponents]; |
---|
474 | int count; |
---|
475 | |
---|
476 | if ((count = [oldComponents count]) < [newComponents count] |
---|
477 | && [[newComponents objectAtIndex: count] isEqualToString: [self name]] |
---|
478 | && [oldComponents isEqualToArray: |
---|
479 | [newComponents objectsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, count)]]]) |
---|
480 | { |
---|
481 | NSAlert * alert = [[NSAlert alloc] init]; |
---|
482 | [alert setMessageText: NSLocalizedString(@"A folder cannot be moved to inside itself.", |
---|
483 | "Move inside itself alert -> title")]; |
---|
484 | [alert setInformativeText: [NSString stringWithFormat: |
---|
485 | NSLocalizedString(@"The move operation of \"%@\" cannot be done.", |
---|
486 | "Move inside itself alert -> message"), [self name]]]; |
---|
487 | [alert addButtonWithTitle: NSLocalizedString(@"OK", "Move inside itself alert -> button")]; |
---|
488 | |
---|
489 | [alert runModal]; |
---|
490 | [alert release]; |
---|
491 | |
---|
492 | return; |
---|
493 | } |
---|
494 | |
---|
495 | [self quickPause]; |
---|
496 | |
---|
497 | //allow if file can be moved or does not exist |
---|
498 | if ([[NSFileManager defaultManager] movePath: [oldFolder stringByAppendingPathComponent: [self name]] |
---|
499 | toPath: [folder stringByAppendingPathComponent: [self name]] handler: nil] |
---|
500 | || ![[NSFileManager defaultManager] fileExistsAtPath: [oldFolder stringByAppendingPathComponent: [self name]]]) |
---|
501 | { |
---|
502 | //get rid of both incomplete folder and old download folder, even if move failed |
---|
503 | fUseIncompleteFolder = NO; |
---|
504 | if (fIncompleteFolder) |
---|
505 | { |
---|
506 | [fIncompleteFolder release]; |
---|
507 | fIncompleteFolder = nil; |
---|
508 | } |
---|
509 | [self changeDownloadFolder: folder]; |
---|
510 | |
---|
511 | [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateStats" object: nil]; |
---|
512 | |
---|
513 | [self endQuickPause]; |
---|
514 | } |
---|
515 | else |
---|
516 | { |
---|
517 | [self endQuickPause]; |
---|
518 | |
---|
519 | NSAlert * alert = [[NSAlert alloc] init]; |
---|
520 | [alert setMessageText: NSLocalizedString(@"There was an error moving the data file.", "Move error alert -> title")]; |
---|
521 | [alert setInformativeText: [NSString stringWithFormat: |
---|
522 | NSLocalizedString(@"The move operation of \"%@\" cannot be done.", |
---|
523 | "Move error alert -> message"), [self name]]]; |
---|
524 | [alert addButtonWithTitle: NSLocalizedString(@"OK", "Move error alert -> button")]; |
---|
525 | |
---|
526 | [alert runModal]; |
---|
527 | [alert release]; |
---|
528 | } |
---|
529 | } |
---|
530 | } |
---|
531 | |
---|
532 | - (void) copyTorrentFileTo: (NSString *) path |
---|
533 | { |
---|
534 | [[NSFileManager defaultManager] copyPath: [self torrentLocation] toPath: path handler: nil]; |
---|
535 | } |
---|
536 | |
---|
537 | - (BOOL) alertForRemainingDiskSpace |
---|
538 | { |
---|
539 | if ([self allDownloaded] || ![fDefaults boolForKey: @"WarningRemainingSpace"]) |
---|
540 | return YES; |
---|
541 | |
---|
542 | NSFileManager * fileManager = [NSFileManager defaultManager]; |
---|
543 | NSString * downloadFolder = [self downloadFolder]; |
---|
544 | |
---|
545 | NSString * volumeName; |
---|
546 | if ((volumeName = [[fileManager componentsToDisplayForPath: downloadFolder] objectAtIndex: 0])) |
---|
547 | { |
---|
548 | BOOL onLeopard = [NSApp isOnLeopardOrBetter]; |
---|
549 | |
---|
550 | NSDictionary * systemAttributes = onLeopard ? [fileManager attributesOfFileSystemForPath: downloadFolder error: NULL] |
---|
551 | : [fileManager fileSystemAttributesAtPath: downloadFolder]; |
---|
552 | uint64_t remainingSpace = [[systemAttributes objectForKey: NSFileSystemFreeSize] unsignedLongLongValue], neededSpace = 0; |
---|
553 | |
---|
554 | //if the size left is less then remaining space, then there is enough space regardless of preallocation |
---|
555 | if (remainingSpace < [self sizeLeft]) |
---|
556 | { |
---|
557 | [self updateFileStat]; |
---|
558 | |
---|
559 | //determine amount needed |
---|
560 | int i; |
---|
561 | for (i = 0; i < [self fileCount]; i++) |
---|
562 | { |
---|
563 | if (tr_torrentGetFileDL(fHandle, i)) |
---|
564 | { |
---|
565 | tr_file * file = &fInfo->files[i]; |
---|
566 | |
---|
567 | neededSpace += file->length; |
---|
568 | |
---|
569 | NSString * path = [downloadFolder stringByAppendingPathComponent: [NSString stringWithUTF8String: file->name]]; |
---|
570 | NSDictionary * fileAttributes = onLeopard ? [fileManager attributesOfItemAtPath: path error: NULL] |
---|
571 | : [fileManager fileAttributesAtPath: path traverseLink: NO]; |
---|
572 | if (fileAttributes) |
---|
573 | { |
---|
574 | unsigned long long fileSize = [[fileAttributes objectForKey: NSFileSize] unsignedLongLongValue]; |
---|
575 | if (fileSize < neededSpace) |
---|
576 | neededSpace -= fileSize; |
---|
577 | else |
---|
578 | neededSpace = 0; |
---|
579 | } |
---|
580 | } |
---|
581 | } |
---|
582 | |
---|
583 | if (remainingSpace < neededSpace) |
---|
584 | { |
---|
585 | NSAlert * alert = [[NSAlert alloc] init]; |
---|
586 | [alert setMessageText: [NSString stringWithFormat: |
---|
587 | NSLocalizedString(@"Not enough remaining disk space to download \"%@\" completely.", |
---|
588 | "Torrent disk space alert -> title"), [self name]]]; |
---|
589 | [alert setInformativeText: [NSString stringWithFormat: NSLocalizedString(@"The transfer will be paused." |
---|
590 | " Clear up space on %@ or deselect files in the torrent inspector to continue.", |
---|
591 | "Torrent disk space alert -> message"), volumeName]]; |
---|
592 | [alert addButtonWithTitle: NSLocalizedString(@"OK", "Torrent disk space alert -> button")]; |
---|
593 | [alert addButtonWithTitle: NSLocalizedString(@"Download Anyway", "Torrent disk space alert -> button")]; |
---|
594 | |
---|
595 | if (onLeopard) |
---|
596 | { |
---|
597 | [alert setShowsSuppressionButton: YES]; |
---|
598 | [[alert suppressionButton] setTitle: NSLocalizedString(@"Do not check disk space again", |
---|
599 | "Torrent disk space alert -> button")]; |
---|
600 | } |
---|
601 | else |
---|
602 | [alert addButtonWithTitle: NSLocalizedString(@"Always Download", "Torrent disk space alert -> button")]; |
---|
603 | |
---|
604 | NSInteger result = [alert runModal]; |
---|
605 | if ((onLeopard ? [[alert suppressionButton] state] == NSOnState : result == NSAlertThirdButtonReturn)) |
---|
606 | [fDefaults setBool: NO forKey: @"WarningRemainingSpace"]; |
---|
607 | [alert release]; |
---|
608 | |
---|
609 | return result != NSAlertFirstButtonReturn; |
---|
610 | } |
---|
611 | } |
---|
612 | } |
---|
613 | return YES; |
---|
614 | } |
---|
615 | |
---|
616 | - (BOOL) alertForFolderAvailable |
---|
617 | { |
---|
618 | #warning check for change from incomplete to download folder first |
---|
619 | if (access(tr_torrentGetDownloadDir(fHandle), 0)) |
---|
620 | { |
---|
621 | NSAlert * alert = [[NSAlert alloc] init]; |
---|
622 | [alert setMessageText: [NSString stringWithFormat: |
---|
623 | NSLocalizedString(@"The folder for downloading \"%@\" cannot be used.", |
---|
624 | "Folder cannot be used alert -> title"), [self name]]]; |
---|
625 | [alert setInformativeText: [NSString stringWithFormat: |
---|
626 | NSLocalizedString(@"\"%@\" cannot be used. The transfer will be paused.", |
---|
627 | "Folder cannot be used alert -> message"), [self downloadFolder]]]; |
---|
628 | [alert addButtonWithTitle: NSLocalizedString(@"OK", "Folder cannot be used alert -> button")]; |
---|
629 | [alert addButtonWithTitle: [NSLocalizedString(@"Choose New Location", |
---|
630 | "Folder cannot be used alert -> location button") stringByAppendingEllipsis]]; |
---|
631 | |
---|
632 | if ([alert runModal] != NSAlertFirstButtonReturn) |
---|
633 | { |
---|
634 | NSOpenPanel * panel = [NSOpenPanel openPanel]; |
---|
635 | |
---|
636 | [panel setPrompt: NSLocalizedString(@"Select", "Folder cannot be used alert -> prompt")]; |
---|
637 | [panel setAllowsMultipleSelection: NO]; |
---|
638 | [panel setCanChooseFiles: NO]; |
---|
639 | [panel setCanChooseDirectories: YES]; |
---|
640 | [panel setCanCreateDirectories: YES]; |
---|
641 | |
---|
642 | [panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the download folder for \"%@\"", |
---|
643 | "Folder cannot be used alert -> select destination folder"), [self name]]]; |
---|
644 | |
---|
645 | [[NSNotificationCenter defaultCenter] postNotificationName: @"MakeWindowKey" object: nil]; |
---|
646 | [panel beginSheetForDirectory: nil file: nil types: nil modalForWindow: [NSApp keyWindow] modalDelegate: self |
---|
647 | didEndSelector: @selector(destinationChoiceClosed:returnCode:contextInfo:) contextInfo: nil]; |
---|
648 | } |
---|
649 | |
---|
650 | [alert release]; |
---|
651 | |
---|
652 | return NO; |
---|
653 | } |
---|
654 | return YES; |
---|
655 | } |
---|
656 | |
---|
657 | - (void) destinationChoiceClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) context |
---|
658 | { |
---|
659 | if (code != NSOKButton) |
---|
660 | return; |
---|
661 | |
---|
662 | [self changeDownloadFolder: [[openPanel filenames] objectAtIndex: 0]]; |
---|
663 | |
---|
664 | [self startTransfer]; |
---|
665 | [self update]; |
---|
666 | |
---|
667 | [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateStats" object: nil]; |
---|
668 | } |
---|
669 | |
---|
670 | - (BOOL) alertForMoveFolderAvailable |
---|
671 | { |
---|
672 | if (access([fDownloadFolder UTF8String], 0)) |
---|
673 | { |
---|
674 | NSAlert * alert = [[NSAlert alloc] init]; |
---|
675 | [alert setMessageText: [NSString stringWithFormat: |
---|
676 | NSLocalizedString(@"The folder for moving the completed \"%@\" cannot be used.", |
---|
677 | "Move folder cannot be used alert -> title"), [self name]]]; |
---|
678 | [alert setInformativeText: [NSString stringWithFormat: |
---|
679 | NSLocalizedString(@"\"%@\" cannot be used. The file will remain in its current location.", |
---|
680 | "Move folder cannot be used alert -> message"), fDownloadFolder]]; |
---|
681 | [alert addButtonWithTitle: NSLocalizedString(@"OK", "Move folder cannot be used alert -> button")]; |
---|
682 | |
---|
683 | [alert runModal]; |
---|
684 | [alert release]; |
---|
685 | |
---|
686 | return NO; |
---|
687 | } |
---|
688 | |
---|
689 | return YES; |
---|
690 | } |
---|
691 | |
---|
692 | - (NSImage *) icon |
---|
693 | { |
---|
694 | if (!fIcon) |
---|
695 | { |
---|
696 | fIcon = [[[NSWorkspace sharedWorkspace] iconForFileType: [self isFolder] ? NSFileTypeForHFSTypeCode('fldr') |
---|
697 | : [[self name] pathExtension]] retain]; |
---|
698 | [fIcon setFlipped: YES]; |
---|
699 | } |
---|
700 | return fIcon; |
---|
701 | } |
---|
702 | |
---|
703 | - (NSString *) name |
---|
704 | { |
---|
705 | return fNameString; |
---|
706 | } |
---|
707 | |
---|
708 | - (BOOL) isFolder |
---|
709 | { |
---|
710 | return fInfo->isMultifile; |
---|
711 | } |
---|
712 | |
---|
713 | - (uint64_t) size |
---|
714 | { |
---|
715 | return fInfo->totalSize; |
---|
716 | } |
---|
717 | |
---|
718 | - (uint64_t) sizeLeft |
---|
719 | { |
---|
720 | return fStat->leftUntilDone; |
---|
721 | } |
---|
722 | |
---|
723 | - (NSString *) trackerAddressAnnounce |
---|
724 | { |
---|
725 | return fStat->announceURL ? [NSString stringWithUTF8String: fStat->announceURL] : nil; |
---|
726 | } |
---|
727 | |
---|
728 | - (NSDate *) lastAnnounceTime |
---|
729 | { |
---|
730 | int date = fStat->lastAnnounceTime; |
---|
731 | return date > 0 ? [NSDate dateWithTimeIntervalSince1970: date] : nil; |
---|
732 | } |
---|
733 | |
---|
734 | - (int) nextAnnounceTime |
---|
735 | { |
---|
736 | int date = fStat->nextAnnounceTime; |
---|
737 | if (date <= 0) |
---|
738 | return -1; |
---|
739 | |
---|
740 | NSTimeInterval difference = [[NSDate dateWithTimeIntervalSince1970: date] timeIntervalSinceNow]; |
---|
741 | return difference > 0 ? (int)difference : -1; |
---|
742 | } |
---|
743 | |
---|
744 | - (NSString *) announceResponse |
---|
745 | { |
---|
746 | return [NSString stringWithUTF8String: fStat->announceResponse]; |
---|
747 | } |
---|
748 | |
---|
749 | - (NSString *) trackerAddressScrape |
---|
750 | { |
---|
751 | return fStat->scrapeURL ? [NSString stringWithUTF8String: fStat->scrapeURL] : nil; |
---|
752 | } |
---|
753 | |
---|
754 | - (NSDate *) lastScrapeTime |
---|
755 | { |
---|
756 | int date = fStat->lastScrapeTime; |
---|
757 | return date > 0 ? [NSDate dateWithTimeIntervalSince1970: date] : nil; |
---|
758 | } |
---|
759 | |
---|
760 | - (int) nextScrapeTime |
---|
761 | { |
---|
762 | int date = fStat->nextScrapeTime; |
---|
763 | if (date <= 0) |
---|
764 | return -1; |
---|
765 | |
---|
766 | NSTimeInterval difference = [[NSDate dateWithTimeIntervalSince1970: date] timeIntervalSinceNow]; |
---|
767 | return difference > 0 ? (int)difference : -1; |
---|
768 | } |
---|
769 | |
---|
770 | - (NSString *) scrapeResponse |
---|
771 | { |
---|
772 | return [NSString stringWithUTF8String: fStat->scrapeResponse]; |
---|
773 | } |
---|
774 | |
---|
775 | - (NSMutableArray *) allTrackers: (BOOL) separators |
---|
776 | { |
---|
777 | int count = fInfo->trackerCount, capacity = count; |
---|
778 | if (separators) |
---|
779 | capacity += fInfo->trackers[count-1].tier + 1; |
---|
780 | NSMutableArray * allTrackers = [NSMutableArray arrayWithCapacity: capacity]; |
---|
781 | |
---|
782 | int i, tier = -1; |
---|
783 | for (i = 0; i < count; i++) |
---|
784 | { |
---|
785 | if (separators && tier != fInfo->trackers[i].tier) |
---|
786 | { |
---|
787 | tier = fInfo->trackers[i].tier; |
---|
788 | [allTrackers addObject: [NSNumber numberWithInt: fAddedTrackers ? tier : tier + 1]]; |
---|
789 | } |
---|
790 | |
---|
791 | [allTrackers addObject: [NSString stringWithUTF8String: fInfo->trackers[i].announce]]; |
---|
792 | } |
---|
793 | |
---|
794 | return allTrackers; |
---|
795 | } |
---|
796 | |
---|
797 | - (BOOL) updateAllTrackersForAdd: (NSMutableArray *) trackers |
---|
798 | { |
---|
799 | //find added tracker at end of first tier |
---|
800 | int i; |
---|
801 | for (i = 1; i < [trackers count]; i++) |
---|
802 | if ([[trackers objectAtIndex: i] isKindOfClass: [NSNumber class]]) |
---|
803 | break; |
---|
804 | i--; |
---|
805 | |
---|
806 | NSString * tracker = [trackers objectAtIndex: i]; |
---|
807 | if ([tracker rangeOfString: @"://"].location == NSNotFound) |
---|
808 | { |
---|
809 | tracker = [@"http://" stringByAppendingString: tracker]; |
---|
810 | [trackers replaceObjectAtIndex: i withObject: tracker]; |
---|
811 | } |
---|
812 | |
---|
813 | if (!tr_httpIsValidURL([tracker UTF8String])) |
---|
814 | return NO; |
---|
815 | |
---|
816 | [self updateAllTrackers: trackers]; |
---|
817 | |
---|
818 | fAddedTrackers = YES; |
---|
819 | return YES; |
---|
820 | } |
---|
821 | |
---|
822 | - (BOOL) updateAllTrackersForRemove: (NSMutableArray *) trackers |
---|
823 | { |
---|
824 | //check if no user-added groups |
---|
825 | if ([[trackers objectAtIndex: 0] intValue] != 0) |
---|
826 | fAddedTrackers = NO; |
---|
827 | |
---|
828 | [self updateAllTrackers: trackers]; |
---|
829 | |
---|
830 | return YES; |
---|
831 | } |
---|
832 | |
---|
833 | - (BOOL) hasAddedTrackers |
---|
834 | { |
---|
835 | return fAddedTrackers; |
---|
836 | } |
---|
837 | |
---|
838 | - (NSString *) comment |
---|
839 | { |
---|
840 | return [NSString stringWithUTF8String: fInfo->comment]; |
---|
841 | } |
---|
842 | |
---|
843 | - (NSString *) creator |
---|
844 | { |
---|
845 | return [NSString stringWithUTF8String: fInfo->creator]; |
---|
846 | } |
---|
847 | |
---|
848 | - (NSDate *) dateCreated |
---|
849 | { |
---|
850 | int date = fInfo->dateCreated; |
---|
851 | return date > 0 ? [NSDate dateWithTimeIntervalSince1970: date] : nil; |
---|
852 | } |
---|
853 | |
---|
854 | - (int) pieceSize |
---|
855 | { |
---|
856 | return fInfo->pieceSize; |
---|
857 | } |
---|
858 | |
---|
859 | - (int) pieceCount |
---|
860 | { |
---|
861 | return fInfo->pieceCount; |
---|
862 | } |
---|
863 | |
---|
864 | - (NSString *) hashString |
---|
865 | { |
---|
866 | return fHashString; |
---|
867 | } |
---|
868 | |
---|
869 | - (BOOL) privateTorrent |
---|
870 | { |
---|
871 | return fInfo->isPrivate; |
---|
872 | } |
---|
873 | |
---|
874 | - (NSString *) torrentLocation |
---|
875 | { |
---|
876 | return [NSString stringWithUTF8String: fInfo->torrent]; |
---|
877 | } |
---|
878 | |
---|
879 | - (NSString *) publicTorrentLocation |
---|
880 | { |
---|
881 | return fPublicTorrentLocation; |
---|
882 | } |
---|
883 | |
---|
884 | - (NSString *) dataLocation |
---|
885 | { |
---|
886 | return [[self downloadFolder] stringByAppendingPathComponent: [self name]]; |
---|
887 | } |
---|
888 | |
---|
889 | - (BOOL) publicTorrent |
---|
890 | { |
---|
891 | return fPublicTorrent; |
---|
892 | } |
---|
893 | |
---|
894 | - (float) progress |
---|
895 | { |
---|
896 | return fStat->percentComplete; |
---|
897 | } |
---|
898 | |
---|
899 | - (float) progressDone |
---|
900 | { |
---|
901 | return fStat->percentDone; |
---|
902 | } |
---|
903 | |
---|
904 | - (float) progressLeft |
---|
905 | { |
---|
906 | return (float)[self sizeLeft] / [self size]; |
---|
907 | } |
---|
908 | |
---|
909 | - (float) checkingProgress |
---|
910 | { |
---|
911 | return fStat->recheckProgress; |
---|
912 | } |
---|
913 | |
---|
914 | - (int) eta |
---|
915 | { |
---|
916 | return fStat->eta; |
---|
917 | } |
---|
918 | |
---|
919 | - (int) etaRatio |
---|
920 | { |
---|
921 | if (![self isSeeding]) |
---|
922 | return TR_ETA_UNKNOWN; |
---|
923 | |
---|
924 | float uploadRate = [self uploadRate]; |
---|
925 | if (uploadRate < 0.1) |
---|
926 | return TR_ETA_UNKNOWN; |
---|
927 | |
---|
928 | float stopRatio = [self actualStopRatio], ratio = [self ratio]; |
---|
929 | if (stopRatio == INVALID || ratio >= stopRatio) |
---|
930 | return TR_ETA_UNKNOWN; |
---|
931 | |
---|
932 | return (float)MAX([self downloadedTotal], [self haveTotal]) * (stopRatio - ratio) / uploadRate / 1024.0; |
---|
933 | } |
---|
934 | |
---|
935 | - (float) notAvailableDesired |
---|
936 | { |
---|
937 | return 1.0 - (float)fStat->desiredAvailable / [self sizeLeft]; |
---|
938 | } |
---|
939 | |
---|
940 | - (BOOL) isActive |
---|
941 | { |
---|
942 | return fStat->status != TR_STATUS_STOPPED; |
---|
943 | } |
---|
944 | |
---|
945 | - (BOOL) isSeeding |
---|
946 | { |
---|
947 | return fStat->status == TR_STATUS_SEED; |
---|
948 | } |
---|
949 | |
---|
950 | - (BOOL) isChecking |
---|
951 | { |
---|
952 | return fStat->status == TR_STATUS_CHECK || fStat->status == TR_STATUS_CHECK_WAIT; |
---|
953 | } |
---|
954 | |
---|
955 | - (BOOL) isCheckingWaiting |
---|
956 | { |
---|
957 | return fStat->status == TR_STATUS_CHECK_WAIT; |
---|
958 | } |
---|
959 | |
---|
960 | - (BOOL) allDownloaded |
---|
961 | { |
---|
962 | return [self progressDone] >= 1.0; |
---|
963 | } |
---|
964 | |
---|
965 | - (BOOL) isComplete |
---|
966 | { |
---|
967 | return [self progress] >= 1.0; |
---|
968 | } |
---|
969 | |
---|
970 | - (BOOL) isError |
---|
971 | { |
---|
972 | return fStat->error != TR_OK; |
---|
973 | } |
---|
974 | |
---|
975 | - (NSString *) errorMessage |
---|
976 | { |
---|
977 | if (![self isError]) |
---|
978 | return @""; |
---|
979 | |
---|
980 | NSString * error; |
---|
981 | if (!(error = [NSString stringWithUTF8String: fStat->errorString]) |
---|
982 | && !(error = [NSString stringWithCString: fStat->errorString encoding: NSISOLatin1StringEncoding])) |
---|
983 | error = [NSString stringWithFormat: @"(%@)", NSLocalizedString(@"unreadable error", "Torrent -> error string unreadable")]; |
---|
984 | |
---|
985 | return error; |
---|
986 | } |
---|
987 | |
---|
988 | - (NSArray *) peers |
---|
989 | { |
---|
990 | int totalPeers, i; |
---|
991 | tr_peer_stat * peers = tr_torrentPeers(fHandle, &totalPeers); |
---|
992 | |
---|
993 | NSMutableArray * peerDicts = [NSMutableArray arrayWithCapacity: totalPeers]; |
---|
994 | |
---|
995 | for (i = 0; i < totalPeers; i++) |
---|
996 | { |
---|
997 | tr_peer_stat * peer = &peers[i]; |
---|
998 | NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithCapacity: 9]; |
---|
999 | |
---|
1000 | [dict setObject: [NSNumber numberWithInt: peer->from] forKey: @"From"]; |
---|
1001 | [dict setObject: [NSString stringWithUTF8String: peer->addr] forKey: @"IP"]; |
---|
1002 | [dict setObject: [NSNumber numberWithInt: peer->port] forKey: @"Port"]; |
---|
1003 | [dict setObject: [NSNumber numberWithFloat: peer->progress] forKey: @"Progress"]; |
---|
1004 | [dict setObject: [NSNumber numberWithBool: peer->isEncrypted] forKey: @"Encryption"]; |
---|
1005 | [dict setObject: [NSString stringWithUTF8String: peer->client] forKey: @"Client"]; |
---|
1006 | [dict setObject: [NSString stringWithUTF8String: peer->flagStr] forKey: @"Flags"]; |
---|
1007 | |
---|
1008 | if (peer->isUploadingTo) |
---|
1009 | [dict setObject: [NSNumber numberWithFloat: peer->uploadToRate] forKey: @"UL To Rate"]; |
---|
1010 | if (peer->isDownloadingFrom) |
---|
1011 | [dict setObject: [NSNumber numberWithFloat: peer->downloadFromRate] forKey: @"DL From Rate"]; |
---|
1012 | |
---|
1013 | [peerDicts addObject: dict]; |
---|
1014 | } |
---|
1015 | |
---|
1016 | tr_torrentPeersFree(peers, totalPeers); |
---|
1017 | |
---|
1018 | return peerDicts; |
---|
1019 | } |
---|
1020 | |
---|
1021 | - (NSString *) progressString |
---|
1022 | { |
---|
1023 | NSString * string; |
---|
1024 | |
---|
1025 | if (![self allDownloaded]) |
---|
1026 | { |
---|
1027 | float progress; |
---|
1028 | if ([self isFolder] && [fDefaults boolForKey: @"DisplayStatusProgressSelected"]) |
---|
1029 | { |
---|
1030 | string = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@ selected", "Torrent -> progress string"), |
---|
1031 | [NSString stringForFileSize: [self haveTotal]], [NSString stringForFileSize: [self totalSizeSelected]]]; |
---|
1032 | progress = 100.0 * [self progressDone]; |
---|
1033 | } |
---|
1034 | else |
---|
1035 | { |
---|
1036 | string = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@", "Torrent -> progress string"), |
---|
1037 | [NSString stringForFileSize: [self haveTotal]], [NSString stringForFileSize: [self size]]]; |
---|
1038 | progress = 100.0 * [self progress]; |
---|
1039 | } |
---|
1040 | |
---|
1041 | string = [NSString localizedStringWithFormat: @"%@ (%.2f%%)", string, progress]; |
---|
1042 | } |
---|
1043 | else |
---|
1044 | { |
---|
1045 | NSString * downloadString; |
---|
1046 | if (![self isComplete]) //only multifile possible |
---|
1047 | { |
---|
1048 | if ([fDefaults boolForKey: @"DisplayStatusProgressSelected"]) |
---|
1049 | downloadString = [NSString stringWithFormat: NSLocalizedString(@"%@ selected", "Torrent -> progress string"), |
---|
1050 | [NSString stringForFileSize: [self haveTotal]]]; |
---|
1051 | else |
---|
1052 | { |
---|
1053 | downloadString = [NSString stringWithFormat: NSLocalizedString(@"%@ of %@", "Torrent -> progress string"), |
---|
1054 | [NSString stringForFileSize: [self haveTotal]], [NSString stringForFileSize: [self size]]]; |
---|
1055 | |
---|
1056 | downloadString = [NSString localizedStringWithFormat: @"%@ (%.2f%%)", downloadString, 100.0 * [self progress]]; |
---|
1057 | } |
---|
1058 | } |
---|
1059 | else |
---|
1060 | downloadString = [NSString stringForFileSize: [self size]]; |
---|
1061 | |
---|
1062 | NSString * uploadString = [NSString stringWithFormat: NSLocalizedString(@"uploaded %@ (Ratio: %@)", |
---|
1063 | "Torrent -> progress string"), [NSString stringForFileSize: [self uploadedTotal]], |
---|
1064 | [NSString stringForRatio: [self ratio]]]; |
---|
1065 | |
---|
1066 | string = [downloadString stringByAppendingFormat: @", %@", uploadString]; |
---|
1067 | } |
---|
1068 | |
---|
1069 | //add time when downloading |
---|
1070 | if (fStat->status == TR_STATUS_DOWNLOAD || ([self isSeeding] |
---|
1071 | && (fRatioSetting == NSOnState || (fRatioSetting == NSMixedState && [fDefaults boolForKey: @"RatioCheck"])))) |
---|
1072 | { |
---|
1073 | int eta = fStat->status == TR_STATUS_DOWNLOAD ? [self eta] : [self etaRatio]; |
---|
1074 | string = [string stringByAppendingFormat: @" - %@", [self etaString: eta]]; |
---|
1075 | } |
---|
1076 | |
---|
1077 | return string; |
---|
1078 | } |
---|
1079 | |
---|
1080 | - (NSString *) statusString |
---|
1081 | { |
---|
1082 | NSString * string; |
---|
1083 | |
---|
1084 | if ([self isError]) |
---|
1085 | { |
---|
1086 | string = NSLocalizedString(@"Error", "Torrent -> status string"); |
---|
1087 | NSString * errorString = [self errorMessage]; |
---|
1088 | if (errorString && ![errorString isEqualToString: @""]) |
---|
1089 | string = [string stringByAppendingFormat: @": %@", errorString]; |
---|
1090 | } |
---|
1091 | else |
---|
1092 | { |
---|
1093 | switch (fStat->status) |
---|
1094 | { |
---|
1095 | case TR_STATUS_STOPPED: |
---|
1096 | if (fWaitToStart) |
---|
1097 | { |
---|
1098 | string = ![self allDownloaded] |
---|
1099 | ? [NSLocalizedString(@"Waiting to download", "Torrent -> status string") stringByAppendingEllipsis] |
---|
1100 | : [NSLocalizedString(@"Waiting to seed", "Torrent -> status string") stringByAppendingEllipsis]; |
---|
1101 | } |
---|
1102 | else if (fFinishedSeeding) |
---|
1103 | string = NSLocalizedString(@"Seeding complete", "Torrent -> status string"); |
---|
1104 | else |
---|
1105 | string = NSLocalizedString(@"Paused", "Torrent -> status string"); |
---|
1106 | break; |
---|
1107 | |
---|
1108 | case TR_STATUS_CHECK_WAIT: |
---|
1109 | string = [NSLocalizedString(@"Waiting to check existing data", "Torrent -> status string") stringByAppendingEllipsis]; |
---|
1110 | break; |
---|
1111 | |
---|
1112 | case TR_STATUS_CHECK: |
---|
1113 | string = [NSString localizedStringWithFormat: NSLocalizedString(@"Checking existing data (%.2f%%)", |
---|
1114 | "Torrent -> status string"), 100.0 * [self checkingProgress]]; |
---|
1115 | break; |
---|
1116 | |
---|
1117 | case TR_STATUS_DOWNLOAD: |
---|
1118 | if ([self totalPeersConnected] != 1) |
---|
1119 | string = [NSString stringWithFormat: NSLocalizedString(@"Downloading from %d of %d peers", |
---|
1120 | "Torrent -> status string"), [self peersSendingToUs], [self totalPeersConnected]]; |
---|
1121 | else |
---|
1122 | string = [NSString stringWithFormat: NSLocalizedString(@"Downloading from %d of 1 peer", |
---|
1123 | "Torrent -> status string"), [self peersSendingToUs]]; |
---|
1124 | break; |
---|
1125 | |
---|
1126 | case TR_STATUS_SEED: |
---|
1127 | if ([self totalPeersConnected] != 1) |
---|
1128 | string = [NSString stringWithFormat: NSLocalizedString(@"Seeding to %d of %d peers", "Torrent -> status string"), |
---|
1129 | [self peersGettingFromUs], [self totalPeersConnected]]; |
---|
1130 | else |
---|
1131 | string = [NSString stringWithFormat: NSLocalizedString(@"Seeding to %d of 1 peer", "Torrent -> status string"), |
---|
1132 | [self peersGettingFromUs]]; |
---|
1133 | } |
---|
1134 | |
---|
1135 | if (fStalled) |
---|
1136 | string = [NSLocalizedString(@"Stalled", "Torrent -> status string") stringByAppendingFormat: @", %@", string]; |
---|
1137 | } |
---|
1138 | |
---|
1139 | //append even if error |
---|
1140 | if ([self isActive] && ![self isChecking]) |
---|
1141 | { |
---|
1142 | if (fStat->status == TR_STATUS_DOWNLOAD) |
---|
1143 | string = [string stringByAppendingFormat: @" - %@: %@, %@: %@", |
---|
1144 | NSLocalizedString(@"DL", "Torrent -> status string"), [NSString stringForSpeed: [self downloadRate]], |
---|
1145 | NSLocalizedString(@"UL", "Torrent -> status string"), [NSString stringForSpeed: [self uploadRate]]]; |
---|
1146 | else |
---|
1147 | string = [string stringByAppendingFormat: @" - %@: %@", |
---|
1148 | NSLocalizedString(@"UL", "Torrent -> status string"), [NSString stringForSpeed: [self uploadRate]]]; |
---|
1149 | } |
---|
1150 | |
---|
1151 | return string; |
---|
1152 | } |
---|
1153 | |
---|
1154 | - (NSString *) shortStatusString |
---|
1155 | { |
---|
1156 | NSString * string; |
---|
1157 | |
---|
1158 | switch (fStat->status) |
---|
1159 | { |
---|
1160 | case TR_STATUS_STOPPED: |
---|
1161 | if (fWaitToStart) |
---|
1162 | { |
---|
1163 | string = ![self allDownloaded] |
---|
1164 | ? [NSLocalizedString(@"Waiting to download", "Torrent -> status string") stringByAppendingEllipsis] |
---|
1165 | : [NSLocalizedString(@"Waiting to seed", "Torrent -> status string") stringByAppendingEllipsis]; |
---|
1166 | } |
---|
1167 | else if (fFinishedSeeding) |
---|
1168 | string = NSLocalizedString(@"Seeding complete", "Torrent -> status string"); |
---|
1169 | else |
---|
1170 | string = NSLocalizedString(@"Paused", "Torrent -> status string"); |
---|
1171 | break; |
---|
1172 | |
---|
1173 | case TR_STATUS_CHECK_WAIT: |
---|
1174 | string = [NSLocalizedString(@"Waiting to check existing data", "Torrent -> status string") stringByAppendingEllipsis]; |
---|
1175 | break; |
---|
1176 | |
---|
1177 | case TR_STATUS_CHECK: |
---|
1178 | string = [NSString localizedStringWithFormat: NSLocalizedString(@"Checking existing data (%.2f%%)", |
---|
1179 | "Torrent -> status string"), 100.0 * [self checkingProgress]]; |
---|
1180 | break; |
---|
1181 | |
---|
1182 | case TR_STATUS_DOWNLOAD: |
---|
1183 | string = [NSString stringWithFormat: @"%@: %@, %@: %@", |
---|
1184 | NSLocalizedString(@"DL", "Torrent -> status string"), [NSString stringForSpeed: [self downloadRate]], |
---|
1185 | NSLocalizedString(@"UL", "Torrent -> status string"), [NSString stringForSpeed: [self uploadRate]]]; |
---|
1186 | break; |
---|
1187 | |
---|
1188 | case TR_STATUS_SEED: |
---|
1189 | string = [NSString stringWithFormat: @"%@: %@, %@: %@", |
---|
1190 | NSLocalizedString(@"Ratio", "Torrent -> status string"), [NSString stringForRatio: [self ratio]], |
---|
1191 | NSLocalizedString(@"UL", "Torrent -> status string"), [NSString stringForSpeed: [self uploadRate]]]; |
---|
1192 | } |
---|
1193 | |
---|
1194 | return string; |
---|
1195 | } |
---|
1196 | |
---|
1197 | - (NSString *) remainingTimeString |
---|
1198 | { |
---|
1199 | if (![self isActive] || ([self isSeeding] |
---|
1200 | && !(fRatioSetting == NSOnState || (fRatioSetting == NSMixedState && [fDefaults boolForKey: @"RatioCheck"])))) |
---|
1201 | return [self shortStatusString]; |
---|
1202 | |
---|
1203 | return [self etaString: [self isSeeding] ? [self etaRatio] : [self eta]]; |
---|
1204 | } |
---|
1205 | |
---|
1206 | - (NSString *) stateString |
---|
1207 | { |
---|
1208 | switch (fStat->status) |
---|
1209 | { |
---|
1210 | case TR_STATUS_STOPPED: |
---|
1211 | return NSLocalizedString(@"Paused", "Torrent -> status string"); |
---|
1212 | |
---|
1213 | case TR_STATUS_CHECK: |
---|
1214 | return [NSString localizedStringWithFormat: NSLocalizedString(@"Checking existing data (%.2f%%)", |
---|
1215 | "Torrent -> status string"), 100.0 * [self checkingProgress]]; |
---|
1216 | |
---|
1217 | case TR_STATUS_CHECK_WAIT: |
---|
1218 | return [NSLocalizedString(@"Waiting to check existing data", "Torrent -> status string") stringByAppendingEllipsis]; |
---|
1219 | |
---|
1220 | case TR_STATUS_DOWNLOAD: |
---|
1221 | return NSLocalizedString(@"Downloading", "Torrent -> status string"); |
---|
1222 | |
---|
1223 | case TR_STATUS_SEED: |
---|
1224 | return NSLocalizedString(@"Seeding", "Torrent -> status string"); |
---|
1225 | } |
---|
1226 | } |
---|
1227 | |
---|
1228 | - (int) seeders |
---|
1229 | { |
---|
1230 | return fStat->seeders; |
---|
1231 | } |
---|
1232 | |
---|
1233 | - (int) leechers |
---|
1234 | { |
---|
1235 | return fStat->leechers; |
---|
1236 | } |
---|
1237 | |
---|
1238 | - (int) completedFromTracker |
---|
1239 | { |
---|
1240 | return fStat->completedFromTracker; |
---|
1241 | } |
---|
1242 | |
---|
1243 | - (int) totalPeersConnected |
---|
1244 | { |
---|
1245 | return fStat->peersConnected; |
---|
1246 | } |
---|
1247 | |
---|
1248 | - (int) totalPeersTracker |
---|
1249 | { |
---|
1250 | return fStat->peersFrom[TR_PEER_FROM_TRACKER]; |
---|
1251 | } |
---|
1252 | |
---|
1253 | - (int) totalPeersIncoming |
---|
1254 | { |
---|
1255 | return fStat->peersFrom[TR_PEER_FROM_INCOMING]; |
---|
1256 | } |
---|
1257 | |
---|
1258 | - (int) totalPeersCache |
---|
1259 | { |
---|
1260 | return fStat->peersFrom[TR_PEER_FROM_CACHE]; |
---|
1261 | } |
---|
1262 | |
---|
1263 | - (int) totalPeersPex |
---|
1264 | { |
---|
1265 | return fStat->peersFrom[TR_PEER_FROM_PEX]; |
---|
1266 | } |
---|
1267 | |
---|
1268 | - (int) totalPeersKnown |
---|
1269 | { |
---|
1270 | return fStat->peersKnown; |
---|
1271 | } |
---|
1272 | |
---|
1273 | - (int) peersSendingToUs |
---|
1274 | { |
---|
1275 | return fStat->peersSendingToUs; |
---|
1276 | } |
---|
1277 | |
---|
1278 | - (int) peersGettingFromUs |
---|
1279 | { |
---|
1280 | return fStat->peersGettingFromUs; |
---|
1281 | } |
---|
1282 | |
---|
1283 | - (float) downloadRate |
---|
1284 | { |
---|
1285 | return fStat->rateDownload; |
---|
1286 | } |
---|
1287 | |
---|
1288 | - (float) uploadRate |
---|
1289 | { |
---|
1290 | return fStat->rateUpload; |
---|
1291 | } |
---|
1292 | |
---|
1293 | - (float) totalRate |
---|
1294 | { |
---|
1295 | return [self downloadRate] + [self uploadRate]; |
---|
1296 | } |
---|
1297 | |
---|
1298 | - (uint64_t) haveVerified |
---|
1299 | { |
---|
1300 | return fStat->haveValid; |
---|
1301 | } |
---|
1302 | |
---|
1303 | - (uint64_t) haveTotal |
---|
1304 | { |
---|
1305 | return [self haveVerified] + fStat->haveUnchecked; |
---|
1306 | } |
---|
1307 | |
---|
1308 | - (uint64_t) totalSizeSelected |
---|
1309 | { |
---|
1310 | return fStat->sizeWhenDone; |
---|
1311 | } |
---|
1312 | |
---|
1313 | - (uint64_t) downloadedTotal |
---|
1314 | { |
---|
1315 | return fStat->downloadedEver; |
---|
1316 | } |
---|
1317 | |
---|
1318 | - (uint64_t) uploadedTotal |
---|
1319 | { |
---|
1320 | return fStat->uploadedEver; |
---|
1321 | } |
---|
1322 | |
---|
1323 | - (uint64_t) failedHash |
---|
1324 | { |
---|
1325 | return fStat->corruptEver; |
---|
1326 | } |
---|
1327 | |
---|
1328 | - (float) swarmSpeed |
---|
1329 | { |
---|
1330 | return fStat->swarmSpeed; |
---|
1331 | } |
---|
1332 | |
---|
1333 | - (int) orderValue |
---|
1334 | { |
---|
1335 | return fOrderValue; |
---|
1336 | } |
---|
1337 | |
---|
1338 | - (void) setOrderValue: (int) orderValue |
---|
1339 | { |
---|
1340 | fOrderValue = orderValue; |
---|
1341 | } |
---|
1342 | |
---|
1343 | - (int) groupValue |
---|
1344 | { |
---|
1345 | return fGroupValue; |
---|
1346 | } |
---|
1347 | |
---|
1348 | - (void) setGroupValue: (int) goupValue |
---|
1349 | { |
---|
1350 | fGroupValue = goupValue; |
---|
1351 | } |
---|
1352 | |
---|
1353 | - (int) groupOrderValue |
---|
1354 | { |
---|
1355 | return [[GroupsController groups] rowValueForIndex: fGroupValue]; |
---|
1356 | } |
---|
1357 | |
---|
1358 | - (void) checkGroupValueForRemoval: (NSNotification *) notification |
---|
1359 | { |
---|
1360 | if (fGroupValue != -1 && [[[notification userInfo] objectForKey: @"Indexes"] containsIndex: fGroupValue]) |
---|
1361 | fGroupValue = -1; |
---|
1362 | } |
---|
1363 | |
---|
1364 | - (NSArray *) fileList |
---|
1365 | { |
---|
1366 | return fFileList; |
---|
1367 | } |
---|
1368 | |
---|
1369 | - (int) fileCount |
---|
1370 | { |
---|
1371 | return fInfo->fileCount; |
---|
1372 | } |
---|
1373 | |
---|
1374 | - (void) updateFileStat |
---|
1375 | { |
---|
1376 | if (fFileStat) |
---|
1377 | tr_torrentFilesFree(fFileStat, [self fileCount]); |
---|
1378 | |
---|
1379 | fFileStat = tr_torrentFiles(fHandle, NULL); |
---|
1380 | } |
---|
1381 | |
---|
1382 | - (float) fileProgress: (int) index |
---|
1383 | { |
---|
1384 | if (!fFileStat) |
---|
1385 | [self updateFileStat]; |
---|
1386 | |
---|
1387 | return fFileStat[index].progress; |
---|
1388 | } |
---|
1389 | |
---|
1390 | - (BOOL) canChangeDownloadCheckForFile: (int) index |
---|
1391 | { |
---|
1392 | if (!fFileStat) |
---|
1393 | [self updateFileStat]; |
---|
1394 | |
---|
1395 | return [self fileCount] > 1 && fFileStat[index].progress < 1.0; |
---|
1396 | } |
---|
1397 | |
---|
1398 | - (BOOL) canChangeDownloadCheckForFiles: (NSIndexSet *) indexSet |
---|
1399 | { |
---|
1400 | if ([self fileCount] <= 1 || [self isComplete]) |
---|
1401 | return NO; |
---|
1402 | |
---|
1403 | if (!fFileStat) |
---|
1404 | [self updateFileStat]; |
---|
1405 | |
---|
1406 | int index; |
---|
1407 | for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index]) |
---|
1408 | if (fFileStat[index].progress < 1.0) |
---|
1409 | return YES; |
---|
1410 | return NO; |
---|
1411 | } |
---|
1412 | |
---|
1413 | - (int) checkForFiles: (NSIndexSet *) indexSet |
---|
1414 | { |
---|
1415 | BOOL onState = NO, offState = NO; |
---|
1416 | int index; |
---|
1417 | for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index]) |
---|
1418 | { |
---|
1419 | if (tr_torrentGetFileDL(fHandle, index) || ![self canChangeDownloadCheckForFile: index]) |
---|
1420 | onState = YES; |
---|
1421 | else |
---|
1422 | offState = YES; |
---|
1423 | |
---|
1424 | if (onState && offState) |
---|
1425 | return NSMixedState; |
---|
1426 | } |
---|
1427 | return onState ? NSOnState : NSOffState; |
---|
1428 | } |
---|
1429 | |
---|
1430 | - (void) setFileCheckState: (int) state forIndexes: (NSIndexSet *) indexSet |
---|
1431 | { |
---|
1432 | NSUInteger count = [indexSet count], i = 0, index; |
---|
1433 | tr_file_index_t * files = malloc(count * sizeof(tr_file_index_t)); |
---|
1434 | for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index]) |
---|
1435 | { |
---|
1436 | files[i] = index; |
---|
1437 | i++; |
---|
1438 | } |
---|
1439 | |
---|
1440 | tr_torrentSetFileDLs(fHandle, files, count, state != NSOffState); |
---|
1441 | free(files); |
---|
1442 | |
---|
1443 | [self update]; |
---|
1444 | [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentFileCheckChange" object: self]; |
---|
1445 | } |
---|
1446 | |
---|
1447 | - (void) setFilePriority: (int) priority forIndexes: (NSIndexSet *) indexSet |
---|
1448 | { |
---|
1449 | NSUInteger count = [indexSet count], i = 0, index; |
---|
1450 | tr_file_index_t * files = malloc(count * sizeof(tr_file_index_t)); |
---|
1451 | for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index]) |
---|
1452 | { |
---|
1453 | files[i] = index; |
---|
1454 | i++; |
---|
1455 | } |
---|
1456 | |
---|
1457 | tr_torrentSetFilePriorities(fHandle, files, count, priority); |
---|
1458 | free(files); |
---|
1459 | } |
---|
1460 | |
---|
1461 | - (BOOL) hasFilePriority: (int) priority forIndexes: (NSIndexSet *) indexSet |
---|
1462 | { |
---|
1463 | int index; |
---|
1464 | for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index]) |
---|
1465 | if (priority == tr_torrentGetFilePriority(fHandle, index) && [self canChangeDownloadCheckForFile: index]) |
---|
1466 | return YES; |
---|
1467 | return NO; |
---|
1468 | } |
---|
1469 | |
---|
1470 | - (NSSet *) filePrioritiesForIndexes: (NSIndexSet *) indexSet |
---|
1471 | { |
---|
1472 | BOOL low = NO, normal = NO, high = NO; |
---|
1473 | NSMutableSet * priorities = [NSMutableSet setWithCapacity: 3]; |
---|
1474 | |
---|
1475 | int index, priority; |
---|
1476 | for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex: index]) |
---|
1477 | { |
---|
1478 | if (![self canChangeDownloadCheckForFile: index]) |
---|
1479 | continue; |
---|
1480 | |
---|
1481 | priority = tr_torrentGetFilePriority(fHandle, index); |
---|
1482 | if (priority == TR_PRI_LOW) |
---|
1483 | { |
---|
1484 | if (low) |
---|
1485 | continue; |
---|
1486 | low = YES; |
---|
1487 | } |
---|
1488 | else if (priority == TR_PRI_HIGH) |
---|
1489 | { |
---|
1490 | if (high) |
---|
1491 | continue; |
---|
1492 | high = YES; |
---|
1493 | } |
---|
1494 | else |
---|
1495 | { |
---|
1496 | if (normal) |
---|
1497 | continue; |
---|
1498 | normal = YES; |
---|
1499 | } |
---|
1500 | |
---|
1501 | [priorities addObject: [NSNumber numberWithInt: priority]]; |
---|
1502 | if (low && normal && high) |
---|
1503 | break; |
---|
1504 | } |
---|
1505 | return priorities; |
---|
1506 | } |
---|
1507 | |
---|
1508 | - (NSDate *) dateAdded |
---|
1509 | { |
---|
1510 | time_t date = fStat->addedDate; |
---|
1511 | return [NSDate dateWithTimeIntervalSince1970: date]; |
---|
1512 | } |
---|
1513 | |
---|
1514 | - (NSDate *) dateCompleted |
---|
1515 | { |
---|
1516 | time_t date = fStat->doneDate; |
---|
1517 | return date != 0 ? [NSDate dateWithTimeIntervalSince1970: date] : nil; |
---|
1518 | } |
---|
1519 | |
---|
1520 | - (NSDate *) dateActivity |
---|
1521 | { |
---|
1522 | time_t date = fStat->activityDate; |
---|
1523 | return date != 0 ? [NSDate dateWithTimeIntervalSince1970: date] : nil; |
---|
1524 | } |
---|
1525 | |
---|
1526 | - (NSDate *) dateActivityOrAdd |
---|
1527 | { |
---|
1528 | NSDate * date = [self dateActivity]; |
---|
1529 | return date ? date : [self dateAdded]; |
---|
1530 | } |
---|
1531 | |
---|
1532 | - (int) stalledMinutes |
---|
1533 | { |
---|
1534 | time_t start = fStat->startDate; |
---|
1535 | if (start == 0) |
---|
1536 | return -1; |
---|
1537 | |
---|
1538 | NSDate * started = [NSDate dateWithTimeIntervalSince1970: start], |
---|
1539 | * activity = [self dateActivity]; |
---|
1540 | |
---|
1541 | NSDate * laterDate = activity ? [started laterDate: activity] : started; |
---|
1542 | return -1 * [laterDate timeIntervalSinceNow] / 60; |
---|
1543 | } |
---|
1544 | |
---|
1545 | - (BOOL) isStalled |
---|
1546 | { |
---|
1547 | return fStalled; |
---|
1548 | } |
---|
1549 | |
---|
1550 | - (NSNumber *) stateSortKey |
---|
1551 | { |
---|
1552 | if (![self isActive]) |
---|
1553 | return [NSNumber numberWithInt: 0]; |
---|
1554 | else if ([self isSeeding]) |
---|
1555 | return [NSNumber numberWithInt: 1]; |
---|
1556 | else |
---|
1557 | return [NSNumber numberWithInt: 2]; |
---|
1558 | } |
---|
1559 | |
---|
1560 | - (tr_torrent *) torrentStruct |
---|
1561 | { |
---|
1562 | return fHandle; |
---|
1563 | } |
---|
1564 | |
---|
1565 | @end |
---|
1566 | |
---|
1567 | @implementation Torrent (Private) |
---|
1568 | |
---|
1569 | //if a hash is given, attempt to load that; otherwise, attempt to open file at path |
---|
1570 | - (id) initWithHash: (NSString *) hashString path: (NSString *) path torrentStruct: (tr_torrent *) torrentStruct lib: (tr_handle *) lib |
---|
1571 | publicTorrent: (NSNumber *) publicTorrent |
---|
1572 | downloadFolder: (NSString *) downloadFolder |
---|
1573 | useIncompleteFolder: (NSNumber *) useIncompleteFolder incompleteFolder: (NSString *) incompleteFolder |
---|
1574 | ratioSetting: (NSNumber *) ratioSetting ratioLimit: (NSNumber *) ratioLimit |
---|
1575 | waitToStart: (NSNumber *) waitToStart |
---|
1576 | orderValue: (NSNumber *) orderValue groupValue: (NSNumber *) groupValue addedTrackers: (NSNumber *) addedTrackers |
---|
1577 | { |
---|
1578 | if (!(self = [super init])) |
---|
1579 | return nil; |
---|
1580 | |
---|
1581 | fDefaults = [NSUserDefaults standardUserDefaults]; |
---|
1582 | |
---|
1583 | fPublicTorrent = path && (publicTorrent ? [publicTorrent boolValue] : ![fDefaults boolForKey: @"DeleteOriginalTorrent"]); |
---|
1584 | if (fPublicTorrent) |
---|
1585 | fPublicTorrentLocation = [path retain]; |
---|
1586 | |
---|
1587 | fDownloadFolder = downloadFolder ? downloadFolder : [fDefaults stringForKey: @"DownloadFolder"]; |
---|
1588 | fDownloadFolder = [[fDownloadFolder stringByExpandingTildeInPath] retain]; |
---|
1589 | |
---|
1590 | fUseIncompleteFolder = useIncompleteFolder ? [useIncompleteFolder boolValue] |
---|
1591 | : [fDefaults boolForKey: @"UseIncompleteDownloadFolder"]; |
---|
1592 | if (fUseIncompleteFolder) |
---|
1593 | { |
---|
1594 | fIncompleteFolder = incompleteFolder ? incompleteFolder : [fDefaults stringForKey: @"IncompleteDownloadFolder"]; |
---|
1595 | fIncompleteFolder = [[fIncompleteFolder stringByExpandingTildeInPath] retain]; |
---|
1596 | } |
---|
1597 | |
---|
1598 | if (torrentStruct) |
---|
1599 | { |
---|
1600 | fHandle = torrentStruct; |
---|
1601 | fInfo = tr_torrentInfo(fHandle); |
---|
1602 | |
---|
1603 | NSString * currentDownloadFolder = [self shouldUseIncompleteFolderForName: [NSString stringWithUTF8String: fInfo->name]] |
---|
1604 | ? fIncompleteFolder : fDownloadFolder; |
---|
1605 | tr_torrentSetDownloadDir(fHandle, [currentDownloadFolder UTF8String]); |
---|
1606 | } |
---|
1607 | else |
---|
1608 | { |
---|
1609 | //set libtransmission settings for initialization |
---|
1610 | tr_ctor * ctor = tr_ctorNew(lib); |
---|
1611 | tr_ctorSetPaused(ctor, TR_FORCE, YES); |
---|
1612 | tr_ctorSetPeerLimit(ctor, TR_FALLBACK, [fDefaults integerForKey: @"PeersTorrent"]); |
---|
1613 | |
---|
1614 | tr_info info; |
---|
1615 | if (hashString) |
---|
1616 | { |
---|
1617 | tr_ctorSetMetainfoFromHash(ctor, [hashString UTF8String]); |
---|
1618 | if (tr_torrentParse(lib, ctor, &info) == TR_OK) |
---|
1619 | { |
---|
1620 | NSString * currentDownloadFolder = [self shouldUseIncompleteFolderForName: [NSString stringWithUTF8String: info.name]] |
---|
1621 | ? fIncompleteFolder : fDownloadFolder; |
---|
1622 | tr_ctorSetDownloadDir(ctor, TR_FORCE, [currentDownloadFolder UTF8String]); |
---|
1623 | |
---|
1624 | fHandle = tr_torrentNew(lib, ctor, NULL); |
---|
1625 | } |
---|
1626 | tr_metainfoFree(&info); |
---|
1627 | } |
---|
1628 | if (!fHandle && path) |
---|
1629 | { |
---|
1630 | tr_ctorSetMetainfoFromFile(ctor, [path UTF8String]); |
---|
1631 | if (tr_torrentParse(lib, ctor, &info) == TR_OK) |
---|
1632 | { |
---|
1633 | NSString * currentDownloadFolder = [self shouldUseIncompleteFolderForName: [NSString stringWithUTF8String: info.name]] |
---|
1634 | ? fIncompleteFolder : fDownloadFolder; |
---|
1635 | tr_ctorSetDownloadDir(ctor, TR_FORCE, [currentDownloadFolder UTF8String]); |
---|
1636 | |
---|
1637 | fHandle = tr_torrentNew(lib, ctor, NULL); |
---|
1638 | } |
---|
1639 | tr_metainfoFree(&info); |
---|
1640 | } |
---|
1641 | |
---|
1642 | tr_ctorFree(ctor); |
---|
1643 | |
---|
1644 | if (!fHandle) |
---|
1645 | { |
---|
1646 | [self release]; |
---|
1647 | return nil; |
---|
1648 | } |
---|
1649 | |
---|
1650 | fInfo = tr_torrentInfo(fHandle); |
---|
1651 | } |
---|
1652 | |
---|
1653 | tr_torrentSetStatusCallback(fHandle, completenessChangeCallback, self); |
---|
1654 | |
---|
1655 | fNameString = [[NSString alloc] initWithUTF8String: fInfo->name]; |
---|
1656 | fHashString = [[NSString alloc] initWithUTF8String: fInfo->hashString]; |
---|
1657 | |
---|
1658 | fRatioSetting = ratioSetting ? [ratioSetting intValue] : NSMixedState; |
---|
1659 | fRatioLimit = ratioLimit ? [ratioLimit floatValue] : [fDefaults floatForKey: @"RatioLimit"]; |
---|
1660 | fFinishedSeeding = NO; |
---|
1661 | |
---|
1662 | fWaitToStart = waitToStart && [waitToStart boolValue]; |
---|
1663 | fResumeOnWake = NO; |
---|
1664 | |
---|
1665 | fOrderValue = orderValue ? [orderValue intValue] : tr_sessionCountTorrents(lib) - 1; |
---|
1666 | fGroupValue = groupValue ? [groupValue intValue] : -1; |
---|
1667 | |
---|
1668 | fAddedTrackers = addedTrackers ? [addedTrackers boolValue] : NO; |
---|
1669 | |
---|
1670 | [self createFileList]; |
---|
1671 | |
---|
1672 | [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(checkGroupValueForRemoval:) |
---|
1673 | name: @"GroupValueRemoved" object: nil]; |
---|
1674 | |
---|
1675 | [self update]; |
---|
1676 | |
---|
1677 | //mark incomplete files to be ignored by Time Machine |
---|
1678 | [self setTimeMachineExclude: ![self allDownloaded] forPath: [[self downloadFolder] stringByAppendingPathComponent: [self name]]]; |
---|
1679 | |
---|
1680 | return self; |
---|
1681 | } |
---|
1682 | |
---|
1683 | - (void) createFileList |
---|
1684 | { |
---|
1685 | if ([self isFolder]) |
---|
1686 | { |
---|
1687 | int count = [self fileCount], i; |
---|
1688 | NSMutableArray * fileList = [[NSMutableArray alloc] initWithCapacity: count]; |
---|
1689 | |
---|
1690 | for (i = 0; i < count; i++) |
---|
1691 | { |
---|
1692 | tr_file * file = &fInfo->files[i]; |
---|
1693 | |
---|
1694 | NSMutableArray * pathComponents = [[[NSString stringWithUTF8String: file->name] pathComponents] mutableCopy]; |
---|
1695 | NSString * path = [pathComponents objectAtIndex: 0]; |
---|
1696 | NSString * name = [pathComponents objectAtIndex: 1]; |
---|
1697 | [pathComponents removeObjectsAtIndexes: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, 2)]]; |
---|
1698 | |
---|
1699 | if ([pathComponents count] > 0) |
---|
1700 | { |
---|
1701 | //determine if folder node already exists |
---|
1702 | NSEnumerator * enumerator = [fileList objectEnumerator]; |
---|
1703 | FileListNode * node; |
---|
1704 | while ((node = [enumerator nextObject])) |
---|
1705 | if ([[node name] isEqualToString: name] && [node isFolder]) |
---|
1706 | break; |
---|
1707 | |
---|
1708 | if (!node) |
---|
1709 | { |
---|
1710 | node = [[FileListNode alloc] initWithFolderName: name path: path]; |
---|
1711 | [fileList addObject: node]; |
---|
1712 | [node release]; |
---|
1713 | } |
---|
1714 | |
---|
1715 | [node insertIndex: i withSize: file->length]; |
---|
1716 | [self insertPath: pathComponents forParent: node fileSize: file->length index: i]; |
---|
1717 | } |
---|
1718 | else |
---|
1719 | { |
---|
1720 | FileListNode * node = [[FileListNode alloc] initWithFileName: name path: path size: file->length index: i]; |
---|
1721 | [fileList addObject: node]; |
---|
1722 | [node release]; |
---|
1723 | } |
---|
1724 | |
---|
1725 | [pathComponents release]; |
---|
1726 | } |
---|
1727 | |
---|
1728 | fFileList = [[NSArray alloc] initWithArray: fileList]; |
---|
1729 | [fileList release]; |
---|
1730 | } |
---|
1731 | else |
---|
1732 | { |
---|
1733 | FileListNode * node = [[FileListNode alloc] initWithFileName: [self name] path: @"" size: [self size] index: 0]; |
---|
1734 | fFileList = [[NSArray arrayWithObject: node] retain]; |
---|
1735 | [node release]; |
---|
1736 | } |
---|
1737 | } |
---|
1738 | |
---|
1739 | - (void) insertPath: (NSMutableArray *) components forParent: (FileListNode *) parent fileSize: (uint64_t) size index: (int) index |
---|
1740 | { |
---|
1741 | NSString * name = [components objectAtIndex: 0]; |
---|
1742 | BOOL isFolder = [components count] > 1; |
---|
1743 | |
---|
1744 | FileListNode * node = nil; |
---|
1745 | if (isFolder) |
---|
1746 | { |
---|
1747 | NSEnumerator * enumerator = [[parent children] objectEnumerator]; |
---|
1748 | while ((node = [enumerator nextObject])) |
---|
1749 | if ([[node name] isEqualToString: name] && [node isFolder]) |
---|
1750 | break; |
---|
1751 | } |
---|
1752 | |
---|
1753 | //create new folder or file if it doesn't already exist |
---|
1754 | if (!node) |
---|
1755 | { |
---|
1756 | if (isFolder) |
---|
1757 | node = [[FileListNode alloc] initWithFolderName: name path: [parent fullPath]]; |
---|
1758 | else |
---|
1759 | node = [[FileListNode alloc] initWithFileName: name path: [parent fullPath] size: size index: index]; |
---|
1760 | |
---|
1761 | [parent insertChild: node]; |
---|
1762 | } |
---|
1763 | |
---|
1764 | if (isFolder) |
---|
1765 | { |
---|
1766 | [node insertIndex: index withSize: size]; |
---|
1767 | |
---|
1768 | [components removeObjectAtIndex: 0]; |
---|
1769 | [self insertPath: components forParent: node fileSize: size index: index]; |
---|
1770 | } |
---|
1771 | } |
---|
1772 | |
---|
1773 | - (BOOL) shouldUseIncompleteFolderForName: (NSString *) name |
---|
1774 | { |
---|
1775 | return fUseIncompleteFolder && |
---|
1776 | ![[NSFileManager defaultManager] fileExistsAtPath: [fDownloadFolder stringByAppendingPathComponent: name]]; |
---|
1777 | } |
---|
1778 | |
---|
1779 | - (void) updateDownloadFolder |
---|
1780 | { |
---|
1781 | //remove old Time Machine location |
---|
1782 | [self setTimeMachineExclude: NO forPath: [[self downloadFolder] stringByAppendingPathComponent: [self name]]]; |
---|
1783 | |
---|
1784 | NSString * folder = [self shouldUseIncompleteFolderForName: [self name]] ? fIncompleteFolder : fDownloadFolder; |
---|
1785 | tr_torrentSetDownloadDir(fHandle, [folder UTF8String]); |
---|
1786 | |
---|
1787 | [self setTimeMachineExclude: ![self allDownloaded] forPath: [folder stringByAppendingPathComponent: [self name]]]; |
---|
1788 | } |
---|
1789 | |
---|
1790 | //status has been retained |
---|
1791 | - (void) completenessChange: (NSNumber *) status |
---|
1792 | { |
---|
1793 | [self update]; |
---|
1794 | |
---|
1795 | BOOL canMove; |
---|
1796 | switch ([status intValue]) |
---|
1797 | { |
---|
1798 | case TR_CP_DONE: |
---|
1799 | case TR_CP_COMPLETE: |
---|
1800 | canMove = YES; |
---|
1801 | |
---|
1802 | //move file from incomplete folder to download folder |
---|
1803 | if (fUseIncompleteFolder && ![[self downloadFolder] isEqualToString: fDownloadFolder] |
---|
1804 | && (canMove = [self alertForMoveFolderAvailable])) |
---|
1805 | { |
---|
1806 | [self quickPause]; |
---|
1807 | |
---|
1808 | if ([[NSFileManager defaultManager] movePath: [[self downloadFolder] stringByAppendingPathComponent: [self name]] |
---|
1809 | toPath: [fDownloadFolder stringByAppendingPathComponent: [self name]] handler: nil]) |
---|
1810 | [self updateDownloadFolder]; |
---|
1811 | else |
---|
1812 | canMove = NO; |
---|
1813 | |
---|
1814 | [self endQuickPause]; |
---|
1815 | } |
---|
1816 | |
---|
1817 | if (!canMove) |
---|
1818 | { |
---|
1819 | fUseIncompleteFolder = NO; |
---|
1820 | |
---|
1821 | [fDownloadFolder release]; |
---|
1822 | fDownloadFolder = fIncompleteFolder; |
---|
1823 | fIncompleteFolder = nil; |
---|
1824 | } |
---|
1825 | |
---|
1826 | //allow to be backed up by Time Machine |
---|
1827 | [self setTimeMachineExclude: NO forPath: [[self downloadFolder] stringByAppendingPathComponent: [self name]]]; |
---|
1828 | |
---|
1829 | fStat = tr_torrentStat(fHandle); |
---|
1830 | [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentFinishedDownloading" object: self]; |
---|
1831 | break; |
---|
1832 | |
---|
1833 | case TR_CP_INCOMPLETE: |
---|
1834 | //do not allow to be backed up by Time Machine |
---|
1835 | [self setTimeMachineExclude: YES forPath: [[self downloadFolder] stringByAppendingPathComponent: [self name]]]; |
---|
1836 | |
---|
1837 | [[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentRestartedDownloading" object: self]; |
---|
1838 | break; |
---|
1839 | } |
---|
1840 | [status release]; |
---|
1841 | } |
---|
1842 | |
---|
1843 | - (void) quickPause |
---|
1844 | { |
---|
1845 | if (fQuickPauseDict) |
---|
1846 | return; |
---|
1847 | |
---|
1848 | fQuickPauseDict = [[NSDictionary alloc] initWithObjectsAndKeys: |
---|
1849 | [NSNumber numberWithInt: [self speedMode: YES]], @"UploadSpeedMode", |
---|
1850 | [NSNumber numberWithInt: [self speedLimit: YES]], @"UploadSpeedLimit", |
---|
1851 | [NSNumber numberWithInt: [self speedMode: NO]], @"DownloadSpeedMode", |
---|
1852 | [NSNumber numberWithInt: [self speedLimit: NO]], @"DownloadSpeedLimit", nil]; |
---|
1853 | |
---|
1854 | [self setSpeedMode: TR_SPEEDLIMIT_SINGLE upload: YES]; |
---|
1855 | [self setSpeedLimit: 0 upload: YES]; |
---|
1856 | [self setSpeedMode: TR_SPEEDLIMIT_SINGLE upload: NO]; |
---|
1857 | [self setSpeedLimit: 0 upload: NO]; |
---|
1858 | } |
---|
1859 | |
---|
1860 | - (void) endQuickPause |
---|
1861 | { |
---|
1862 | if (!fQuickPauseDict) |
---|
1863 | return; |
---|
1864 | |
---|
1865 | [self setSpeedMode: [[fQuickPauseDict objectForKey: @"UploadSpeedMode"] intValue] upload: YES]; |
---|
1866 | [self setSpeedLimit: [[fQuickPauseDict objectForKey: @"UploadSpeedLimit"] intValue] upload: YES]; |
---|
1867 | [self setSpeedMode: [[fQuickPauseDict objectForKey: @"DownloadSpeedMode"] intValue] upload: NO]; |
---|
1868 | [self setSpeedLimit: [[fQuickPauseDict objectForKey: @"DownloadSpeedLimit"] intValue] upload: NO]; |
---|
1869 | |
---|
1870 | [fQuickPauseDict release]; |
---|
1871 | fQuickPauseDict = nil; |
---|
1872 | } |
---|
1873 | |
---|
1874 | - (NSString *) etaString: (int) eta |
---|
1875 | { |
---|
1876 | switch (eta) |
---|
1877 | { |
---|
1878 | case TR_ETA_NOT_AVAIL: |
---|
1879 | return NSLocalizedString(@"data not fully available", "Torrent -> eta string"); |
---|
1880 | case TR_ETA_UNKNOWN: |
---|
1881 | return NSLocalizedString(@"remaining time unknown", "Torrent -> eta string"); |
---|
1882 | default: |
---|
1883 | return [NSString stringWithFormat: NSLocalizedString(@"%@ remaining", "Torrent -> eta string"), |
---|
1884 | [NSString timeString: eta showSeconds: YES maxDigits: 2]]; |
---|
1885 | } |
---|
1886 | } |
---|
1887 | |
---|
1888 | - (void) updateAllTrackers: (NSMutableArray *) trackers |
---|
1889 | { |
---|
1890 | //get count |
---|
1891 | int count = 0; |
---|
1892 | NSEnumerator * enumerator = [trackers objectEnumerator]; |
---|
1893 | id object; |
---|
1894 | while ((object = [enumerator nextObject])) |
---|
1895 | if (![object isKindOfClass: [NSNumber class]]) |
---|
1896 | count++; |
---|
1897 | |
---|
1898 | //recreate the tracker structure |
---|
1899 | tr_tracker_info * trackerStructs = tr_new(tr_tracker_info, count); |
---|
1900 | int tier = 0; |
---|
1901 | int i = 0; |
---|
1902 | enumerator = [trackers objectEnumerator]; |
---|
1903 | while ((object = [enumerator nextObject])) |
---|
1904 | { |
---|
1905 | if (![object isKindOfClass: [NSNumber class]]) |
---|
1906 | { |
---|
1907 | trackerStructs[i].tier = tier; |
---|
1908 | trackerStructs[i].announce = (char *)[object UTF8String]; |
---|
1909 | i++; |
---|
1910 | } |
---|
1911 | else |
---|
1912 | tier++; |
---|
1913 | } |
---|
1914 | |
---|
1915 | tr_torrentSetAnnounceList(fHandle, trackerStructs, count); |
---|
1916 | tr_free(trackerStructs); |
---|
1917 | } |
---|
1918 | |
---|
1919 | - (void) trashFile: (NSString *) path |
---|
1920 | { |
---|
1921 | //attempt to move to trash |
---|
1922 | if (![[NSWorkspace sharedWorkspace] performFileOperation: NSWorkspaceRecycleOperation |
---|
1923 | source: [path stringByDeletingLastPathComponent] destination: @"" |
---|
1924 | files: [NSArray arrayWithObject: [path lastPathComponent]] tag: nil]) |
---|
1925 | { |
---|
1926 | //if cannot trash, just delete it (will work if it's on a remote volume) |
---|
1927 | if ([NSApp isOnLeopardOrBetter]) |
---|
1928 | { |
---|
1929 | NSError * error; |
---|
1930 | if (![[NSFileManager defaultManager] removeItemAtPath: path error: &error]) |
---|
1931 | NSLog(@"Could not trash %@: %@", path, [error localizedDescription]); |
---|
1932 | } |
---|
1933 | else |
---|
1934 | { |
---|
1935 | if (![[NSFileManager defaultManager] removeFileAtPath: path handler: nil]) |
---|
1936 | NSLog(@"Could not trash %@", path); |
---|
1937 | } |
---|
1938 | } |
---|
1939 | } |
---|
1940 | |
---|
1941 | - (void) setTimeMachineExclude: (BOOL) exclude forPath: (NSString *) path |
---|
1942 | { |
---|
1943 | if ([NSApp isOnLeopardOrBetter]) |
---|
1944 | CSBackupSetItemExcluded((CFURLRef)[NSURL fileURLWithPath: path], exclude, true); |
---|
1945 | } |
---|
1946 | |
---|
1947 | @end |
---|