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