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