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