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