source: trunk/macosx/Torrent.m @ 235

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

Cosmetic

File size: 8.6 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
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    fInfo = tr_torrentInfo( fHandle );
49
50    NSString * fileType = ( fInfo->fileCount > 1 ) ?
51        NSFileTypeForHFSTypeCode('fldr') : [[self name] pathExtension];
52    fIcon = [[NSWorkspace sharedWorkspace] iconForFileType: fileType];
53    [fIcon setFlipped: YES];
54    [fIcon retain];
55    fIconNonFlipped = [[NSWorkspace sharedWorkspace] iconForFileType: fileType];
56    [fIconNonFlipped retain];
57
58    fStatusString   = [[NSMutableString alloc] initWithCapacity: 50];
59    fInfoString     = [[NSMutableString alloc] initWithCapacity: 50];
60    fDownloadString = [[NSMutableString alloc] initWithCapacity: 10];
61    fUploadString   = [[NSMutableString alloc] initWithCapacity: 10];
62
63    [self update];
64    return self;
65}
66
67- (void) dealloc
68{
69    if( fHandle )
70    {
71        tr_torrentClose( fLib, fHandle );
72        [fIcon release];
73        [fStatusString release];
74        [fInfoString release];
75        [fDownloadString release];
76        [fUploadString release];
77    }
78    [super dealloc];
79}
80
81- (void) setFolder: (NSString *) path
82{
83    tr_torrentSetFolder( fHandle, [path UTF8String] );
84}
85
86- (NSString *) getFolder
87{
88    return [NSString stringWithUTF8String: tr_torrentGetFolder( fHandle )];
89}
90
91- (void) getAvailability: (int8_t *) tab size: (int) size
92{
93    tr_torrentAvailability( fHandle, tab, size );
94}
95
96- (void) update
97{
98    fStat = tr_torrentStat( fHandle );
99
100    [fStatusString setString: @""];
101    [fInfoString   setString: @""];
102
103    switch( fStat->status )
104    {
105        case TR_STATUS_PAUSE:
106            [fStatusString appendFormat: @"Paused (%.2f %%)",
107                100 * fStat->progress];
108            break;
109
110        case TR_STATUS_CHECK:
111            [fStatusString appendFormat:
112                @"Checking existing files (%.2f %%)",
113                100 * fStat->progress];
114            break;
115
116        case TR_STATUS_DOWNLOAD:
117            if( fStat->eta < 0 )
118            {
119                [fStatusString appendFormat:
120                    @"Finishing in --:--:-- (%.2f %%)",
121                    100 * fStat->progress];
122            }
123            else
124            {
125                [fStatusString appendFormat:
126                    @"Finishing in %02d:%02d:%02d (%.2f %%)",
127                    fStat->eta / 3600, ( fStat->eta / 60 ) % 60,
128                    fStat->eta % 60, 100 * fStat->progress];
129            }
130            [fInfoString appendFormat:
131                @"Downloading from %d of %d peer%s",
132                fStat->peersUploading, fStat->peersTotal,
133                ( fStat->peersTotal == 1 ) ? "" : "s"];
134            break;
135
136        case TR_STATUS_SEED:
137            [fStatusString appendFormat:
138                @"Seeding, uploading to %d of %d peer%s",
139                fStat->peersDownloading, fStat->peersTotal,
140                ( fStat->peersTotal == 1 ) ? "" : "s"];
141            break;
142
143        case TR_STATUS_STOPPING:
144            [fStatusString setString: [@"Stopping"
145                stringByAppendingString: NS_ELLIPSIS]];
146            break;
147    }
148
149    if( fStat->error & TR_ETRACKER )
150    {
151        [fInfoString setString: [@"Error: " stringByAppendingString:
152            [NSString stringWithUTF8String: fStat->trackerError]]];
153    }
154
155    if( fStat->progress == 1.0 )
156    {
157        [fDownloadString setString: [@"Ratio: " stringByAppendingString:
158            [NSString stringForRatio: fStat->downloaded
159            upload: fStat->uploaded]]];
160    }
161    else
162    {
163        [fDownloadString setString: [@"DL: " stringByAppendingString:
164            [NSString stringForSpeed: fStat->rateDownload]]];
165    }
166    [fUploadString setString: [@"UL: " stringByAppendingString:
167        [NSString stringForSpeed: fStat->rateUpload]]];
168}
169
170- (void) start
171{
172    if( fStat->status & TR_STATUS_INACTIVE )
173    {
174        tr_torrentStart( fHandle );
175    }
176}
177
178- (void) stop
179{
180    if( fStat->status & TR_STATUS_ACTIVE )
181    {
182        tr_torrentStop( fHandle );
183    }
184}
185
186- (void) sleep
187{
188    if( ( fResumeOnWake = ( fStat->status & TR_STATUS_ACTIVE ) ) )
189    {
190        [self stop];
191    }
192}
193
194- (void) wakeUp
195{
196    if( fResumeOnWake )
197    {
198        [self start];
199    }
200}
201
202- (void) reveal
203{
204    [[NSWorkspace sharedWorkspace] selectFile: [[self getFolder]
205        stringByAppendingPathComponent: [self name]]
206        inFileViewerRootedAtPath: nil];
207}
208
209- (void) trashTorrent
210{
211    [self trashPath: [self path]];
212}
213
214- (void) trashData
215{
216    [self trashPath: [[self getFolder]
217        stringByAppendingPathComponent: [self name]]];
218}
219
220- (NSImage *) icon
221{
222    return fIcon;
223}
224
225- (NSImage *) iconNonFlipped
226{
227    return fIconNonFlipped;
228}
229
230- (NSString *) path
231{
232    return [NSString stringWithUTF8String: fInfo->torrent];
233}
234
235- (NSString *) name
236{
237    return [NSString stringWithUTF8String: fInfo->name];
238}
239
240- (uint64_t) size
241{
242    return fInfo->totalSize;
243}
244
245- (NSString *) tracker
246{
247    return [NSString stringWithFormat: @"%s:%d",
248            fInfo->trackerAddress, fInfo->trackerPort];
249}
250
251- (NSString *) announce
252{
253    return [NSString stringWithUTF8String: fInfo->trackerAnnounce];
254}
255
256- (int) pieceSize
257{
258    return fInfo->pieceSize;
259}
260
261- (int) pieceCount
262{
263    return fInfo->pieceCount;
264}
265
266- (NSString *) hash1
267{
268    NSMutableString * string = [NSMutableString
269        stringWithCapacity: SHA_DIGEST_LENGTH];
270    int i;
271    for( i = 0; i < SHA_DIGEST_LENGTH / 2; i++ )
272    {
273        [string appendFormat: @"%02x", fInfo->hash[i]];
274    }
275    return string;
276}
277- (NSString *) hash2
278{
279    NSMutableString * string = [NSMutableString
280        stringWithCapacity: SHA_DIGEST_LENGTH];
281    int i;
282    for( i = SHA_DIGEST_LENGTH / 2; i < SHA_DIGEST_LENGTH; i++ )
283    {
284        [string appendFormat: @"%02x", fInfo->hash[i]];
285    }
286    return string;
287}
288
289- (float) progress
290{
291    return fStat->progress;
292}
293
294- (BOOL) isActive
295{
296    return ( fStat->status & TR_STATUS_ACTIVE );
297}
298
299- (BOOL) isSeeding
300{
301    return ( fStat->status == TR_STATUS_SEED );
302}
303
304- (BOOL) isPaused
305{
306    return ( fStat->status == TR_STATUS_PAUSE );
307}
308
309- (BOOL) justFinished
310{
311    return tr_getFinished( fHandle );
312}
313
314- (NSString *) statusString
315{
316    return fStatusString;
317}
318
319- (NSString *) infoString
320{
321    return fInfoString;
322}
323
324- (NSString *) downloadString
325{
326    return fDownloadString;
327}
328
329- (NSString *) uploadString
330{
331    return fUploadString;
332}
333
334- (int) seeders
335{
336    return fStat->seeders;
337}
338
339- (int) leechers
340{
341    return fStat->leechers;
342}
343
344- (uint64_t) downloaded
345{
346    return fStat->downloaded;
347}
348
349- (uint64_t) uploaded
350{
351    return fStat->uploaded;
352}
353
354@end
355
356
357@implementation Torrent (Private)
358
359- (void) trashPath: (NSString *) path
360{
361    if( ![[NSWorkspace sharedWorkspace] performFileOperation:
362           NSWorkspaceRecycleOperation source:
363           [path stringByDeletingLastPathComponent]
364           destination: @""
365           files: [NSArray arrayWithObject: [path lastPathComponent]]
366           tag: nil] )
367    {
368        /* We can't move it to the trash, let's try just to delete it
369           (will work if it is on a remote volume) */
370        if( ![[NSFileManager defaultManager]
371                removeFileAtPath: path handler: nil] )
372        {
373            NSLog( [@"Could not trash " stringByAppendingString: path] );
374        }
375    }
376}
377
378@end
Note: See TracBrowser for help on using the repository browser.