source: trunk/macosx/Torrent.m @ 244

Last change on this file since 244 was 244, checked in by titer, 16 years ago

Saves in history the date at which the torrent was loaded

File size: 8.9 KB
Line 
1/******************************************************************************
2 * Copyright (c) 2005-2006 Transmission authors and contributors
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 *****************************************************************************/
22
23#import "Torrent.h"
24#import "StringAdditions.h"
25#import "Utils.h"
26
27@interface Torrent (Private)
28
29- (void) trashPath: (NSString *) path;
30
31@end
32
33
34@implementation Torrent
35
36- (id) initWithPath: (NSString *) path lib: (tr_handle_t *) lib date: (NSDate *) date
37{
38    fLib = lib;
39
40    int error;
41    fHandle = tr_torrentInit( fLib, [path UTF8String], &error );
42    if( !fHandle )
43    {
44        [self release];
45        return nil;
46    }
47
48    fDate = [date retain];
49    fInfo = tr_torrentInfo( fHandle );
50
51    NSString * fileType = ( fInfo->fileCount > 1 ) ?
52        NSFileTypeForHFSTypeCode('fldr') : [[self name] pathExtension];
53    fIcon = [[NSWorkspace sharedWorkspace] iconForFileType: fileType];
54    [fIcon setFlipped: YES];
55    [fIcon retain];
56    fIconNonFlipped = [[NSWorkspace sharedWorkspace] iconForFileType: fileType];
57    [fIconNonFlipped retain];
58
59    fStatusString   = [[NSMutableString alloc] initWithCapacity: 50];
60    fInfoString     = [[NSMutableString alloc] initWithCapacity: 50];
61    fDownloadString = [[NSMutableString alloc] initWithCapacity: 10];
62    fUploadString   = [[NSMutableString alloc] initWithCapacity: 10];
63
64    [self update];
65    return self;
66}
67
68- (id) initWithPath: (NSString *) path lib: (tr_handle_t *) lib
69{
70    return [self initWithPath: path lib: lib date: [NSDate date]];
71}
72
73- (void) dealloc
74{
75    if( fHandle )
76    {
77        tr_torrentClose( fLib, fHandle );
78        [fDate release];
79        [fIcon release];
80        [fIconNonFlipped release];
81        [fStatusString release];
82        [fInfoString release];
83        [fDownloadString release];
84        [fUploadString release];
85    }
86    [super dealloc];
87}
88
89- (void) setFolder: (NSString *) path
90{
91    tr_torrentSetFolder( fHandle, [path UTF8String] );
92}
93
94- (NSString *) getFolder
95{
96    return [NSString stringWithUTF8String: tr_torrentGetFolder( fHandle )];
97}
98
99- (void) getAvailability: (int8_t *) tab size: (int) size
100{
101    tr_torrentAvailability( fHandle, tab, size );
102}
103
104- (void) update
105{
106    fStat = tr_torrentStat( fHandle );
107
108    [fStatusString setString: @""];
109    [fInfoString   setString: @""];
110
111    switch( fStat->status )
112    {
113        case TR_STATUS_PAUSE:
114            [fStatusString appendFormat: @"Paused (%.2f %%)",
115                100 * fStat->progress];
116            break;
117
118        case TR_STATUS_CHECK:
119            [fStatusString appendFormat:
120                @"Checking existing files (%.2f %%)",
121                100 * fStat->progress];
122            break;
123
124        case TR_STATUS_DOWNLOAD:
125            if( fStat->eta < 0 )
126            {
127                [fStatusString appendFormat:
128                    @"Finishing in --:--:-- (%.2f %%)",
129                    100 * fStat->progress];
130            }
131            else
132            {
133                [fStatusString appendFormat:
134                    @"Finishing in %02d:%02d:%02d (%.2f %%)",
135                    fStat->eta / 3600, ( fStat->eta / 60 ) % 60,
136                    fStat->eta % 60, 100 * fStat->progress];
137            }
138            [fInfoString appendFormat:
139                @"Downloading from %d of %d peer%s",
140                fStat->peersUploading, fStat->peersTotal,
141                ( fStat->peersTotal == 1 ) ? "" : "s"];
142            break;
143
144        case TR_STATUS_SEED:
145            [fStatusString appendFormat:
146                @"Seeding, uploading to %d of %d peer%s",
147                fStat->peersDownloading, fStat->peersTotal,
148                ( fStat->peersTotal == 1 ) ? "" : "s"];
149            break;
150
151        case TR_STATUS_STOPPING:
152            [fStatusString setString: [@"Stopping"
153                stringByAppendingString: NS_ELLIPSIS]];
154            break;
155    }
156
157    if( fStat->error & TR_ETRACKER )
158    {
159        [fInfoString setString: [@"Error: " stringByAppendingString:
160            [NSString stringWithUTF8String: fStat->trackerError]]];
161    }
162
163    if( fStat->progress == 1.0 )
164    {
165        [fDownloadString setString: [@"Ratio: " stringByAppendingString:
166            [NSString stringForRatio: fStat->downloaded
167            upload: fStat->uploaded]]];
168    }
169    else
170    {
171        [fDownloadString setString: [@"DL: " stringByAppendingString:
172            [NSString stringForSpeed: fStat->rateDownload]]];
173    }
174    [fUploadString setString: [@"UL: " stringByAppendingString:
175        [NSString stringForSpeed: fStat->rateUpload]]];
176}
177
178- (void) start
179{
180    if( fStat->status & TR_STATUS_INACTIVE )
181    {
182        tr_torrentStart( fHandle );
183    }
184}
185
186- (void) stop
187{
188    if( fStat->status & TR_STATUS_ACTIVE )
189    {
190        tr_torrentStop( fHandle );
191    }
192}
193
194- (void) sleep
195{
196    if( ( fResumeOnWake = ( fStat->status & TR_STATUS_ACTIVE ) ) )
197    {
198        [self stop];
199    }
200}
201
202- (void) wakeUp
203{
204    if( fResumeOnWake )
205    {
206        [self start];
207    }
208}
209
210- (void) reveal
211{
212    [[NSWorkspace sharedWorkspace] selectFile: [[self getFolder]
213        stringByAppendingPathComponent: [self name]]
214        inFileViewerRootedAtPath: nil];
215}
216
217- (void) trashTorrent
218{
219    [self trashPath: [self path]];
220}
221
222- (void) trashData
223{
224    [self trashPath: [[self getFolder]
225        stringByAppendingPathComponent: [self name]]];
226}
227
228- (NSImage *) icon
229{
230    return fIcon;
231}
232
233- (NSImage *) iconNonFlipped
234{
235    return fIconNonFlipped;
236}
237
238- (NSString *) path
239{
240    return [NSString stringWithUTF8String: fInfo->torrent];
241}
242
243- (NSString *) name
244{
245    return [NSString stringWithUTF8String: fInfo->name];
246}
247
248- (uint64_t) size
249{
250    return fInfo->totalSize;
251}
252
253- (NSString *) tracker
254{
255    return [NSString stringWithFormat: @"%s:%d",
256            fInfo->trackerAddress, fInfo->trackerPort];
257}
258
259- (NSString *) announce
260{
261    return [NSString stringWithUTF8String: fInfo->trackerAnnounce];
262}
263
264- (int) pieceSize
265{
266    return fInfo->pieceSize;
267}
268
269- (int) pieceCount
270{
271    return fInfo->pieceCount;
272}
273
274- (NSString *) hash1
275{
276    NSMutableString * string = [NSMutableString
277        stringWithCapacity: SHA_DIGEST_LENGTH];
278    int i;
279    for( i = 0; i < SHA_DIGEST_LENGTH / 2; i++ )
280    {
281        [string appendFormat: @"%02x", fInfo->hash[i]];
282    }
283    return string;
284}
285- (NSString *) hash2
286{
287    NSMutableString * string = [NSMutableString
288        stringWithCapacity: SHA_DIGEST_LENGTH];
289    int i;
290    for( i = SHA_DIGEST_LENGTH / 2; i < SHA_DIGEST_LENGTH; i++ )
291    {
292        [string appendFormat: @"%02x", fInfo->hash[i]];
293    }
294    return string;
295}
296
297- (float) progress
298{
299    return fStat->progress;
300}
301
302- (BOOL) isActive
303{
304    return ( fStat->status & TR_STATUS_ACTIVE );
305}
306
307- (BOOL) isSeeding
308{
309    return ( fStat->status == TR_STATUS_SEED );
310}
311
312- (BOOL) isPaused
313{
314    return ( fStat->status == TR_STATUS_PAUSE );
315}
316
317- (BOOL) justFinished
318{
319    return tr_getFinished( fHandle );
320}
321
322- (NSString *) statusString
323{
324    return fStatusString;
325}
326
327- (NSString *) infoString
328{
329    return fInfoString;
330}
331
332- (NSString *) downloadString
333{
334    return fDownloadString;
335}
336
337- (NSString *) uploadString
338{
339    return fUploadString;
340}
341
342- (int) seeders
343{
344    return fStat->seeders;
345}
346
347- (int) leechers
348{
349    return fStat->leechers;
350}
351
352- (uint64_t) downloaded
353{
354    return fStat->downloaded;
355}
356
357- (uint64_t) uploaded
358{
359    return fStat->uploaded;
360}
361
362- (NSDate *) date
363{
364    return fDate;
365}
366
367@end
368
369
370@implementation Torrent (Private)
371
372- (void) trashPath: (NSString *) path
373{
374    if( ![[NSWorkspace sharedWorkspace] performFileOperation:
375           NSWorkspaceRecycleOperation source:
376           [path stringByDeletingLastPathComponent]
377           destination: @""
378           files: [NSArray arrayWithObject: [path lastPathComponent]]
379           tag: nil] )
380    {
381        /* We can't move it to the trash, let's try just to delete it
382           (will work if it is on a remote volume) */
383        if( ![[NSFileManager defaultManager]
384                removeFileAtPath: path handler: nil] )
385        {
386            NSLog( [@"Could not trash " stringByAppendingString: path] );
387        }
388    }
389}
390
391@end
Note: See TracBrowser for help on using the repository browser.