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 | |
---|
26 | @implementation Torrent |
---|
27 | |
---|
28 | - (id) initWithPath: (NSString *) path lib: (tr_handle_t *) lib |
---|
29 | { |
---|
30 | fLib = lib; |
---|
31 | |
---|
32 | int error; |
---|
33 | fHandle = tr_torrentInit( fLib, [path UTF8String], &error ); |
---|
34 | if( !fHandle ) |
---|
35 | { |
---|
36 | [self release]; |
---|
37 | return nil; |
---|
38 | } |
---|
39 | |
---|
40 | fInfo = tr_torrentInfo( fHandle ); |
---|
41 | |
---|
42 | NSString * fileType = ( fInfo->fileCount > 1 ) ? |
---|
43 | NSFileTypeForHFSTypeCode('fldr') : [[self name] pathExtension]; |
---|
44 | fIcon = [[NSWorkspace sharedWorkspace] iconForFileType: fileType]; |
---|
45 | [fIcon setFlipped: YES]; |
---|
46 | [fIcon retain]; |
---|
47 | |
---|
48 | fStatusString = [[NSMutableString alloc] initWithCapacity: 50]; |
---|
49 | fInfoString = [[NSMutableString alloc] initWithCapacity: 50]; |
---|
50 | fDownloadString = [[NSMutableString alloc] initWithCapacity: 10]; |
---|
51 | fUploadString = [[NSMutableString alloc] initWithCapacity: 10]; |
---|
52 | |
---|
53 | [self update]; |
---|
54 | return self; |
---|
55 | } |
---|
56 | |
---|
57 | - (void) dealloc |
---|
58 | { |
---|
59 | if( fHandle ) |
---|
60 | { |
---|
61 | tr_torrentClose( fLib, fHandle ); |
---|
62 | [fIcon release]; |
---|
63 | [fStatusString release]; |
---|
64 | [fInfoString release]; |
---|
65 | [fDownloadString release]; |
---|
66 | [fUploadString release]; |
---|
67 | } |
---|
68 | [super dealloc]; |
---|
69 | } |
---|
70 | |
---|
71 | - (void) setFolder: (NSString *) path |
---|
72 | { |
---|
73 | tr_torrentSetFolder( fHandle, [path UTF8String] ); |
---|
74 | } |
---|
75 | |
---|
76 | - (NSString *) getFolder |
---|
77 | { |
---|
78 | return [NSString stringWithUTF8String: tr_torrentGetFolder( fHandle )]; |
---|
79 | } |
---|
80 | |
---|
81 | - (void) getAvailability: (int8_t *) tab size: (int) size |
---|
82 | { |
---|
83 | tr_torrentAvailability( fHandle, tab, size ); |
---|
84 | } |
---|
85 | |
---|
86 | - (void) update |
---|
87 | { |
---|
88 | fStat = tr_torrentStat( fHandle ); |
---|
89 | |
---|
90 | [fStatusString setString: @""]; |
---|
91 | [fInfoString setString: @""]; |
---|
92 | |
---|
93 | switch( fStat->status ) |
---|
94 | { |
---|
95 | case TR_STATUS_PAUSE: |
---|
96 | [fStatusString appendFormat: @"Paused (%.2f %%)", |
---|
97 | 100 * fStat->progress]; |
---|
98 | break; |
---|
99 | |
---|
100 | case TR_STATUS_CHECK: |
---|
101 | [fStatusString appendFormat: |
---|
102 | @"Checking existing files (%.2f %%)", |
---|
103 | 100 * fStat->progress]; |
---|
104 | break; |
---|
105 | |
---|
106 | case TR_STATUS_DOWNLOAD: |
---|
107 | if( fStat->eta < 0 ) |
---|
108 | { |
---|
109 | [fStatusString appendFormat: |
---|
110 | @"Finishing in --:--:-- (%.2f %%)", |
---|
111 | 100 * fStat->progress]; |
---|
112 | } |
---|
113 | else |
---|
114 | { |
---|
115 | [fStatusString appendFormat: |
---|
116 | @"Finishing in %02d:%02d:%02d (%.2f %%)", |
---|
117 | fStat->eta / 3600, ( fStat->eta / 60 ) % 60, |
---|
118 | fStat->eta % 60, 100 * fStat->progress]; |
---|
119 | } |
---|
120 | [fInfoString appendFormat: |
---|
121 | @"Downloading from %d of %d peer%s", |
---|
122 | fStat->peersUploading, fStat->peersTotal, |
---|
123 | ( fStat->peersTotal == 1 ) ? "" : "s"]; |
---|
124 | break; |
---|
125 | |
---|
126 | case TR_STATUS_SEED: |
---|
127 | [fStatusString appendFormat: |
---|
128 | @"Seeding, uploading to %d of %d peer%s", |
---|
129 | fStat->peersDownloading, fStat->peersTotal, |
---|
130 | ( fStat->peersTotal == 1 ) ? "" : "s"]; |
---|
131 | break; |
---|
132 | |
---|
133 | case TR_STATUS_STOPPING: |
---|
134 | [fStatusString setString: @"Stopping..."]; |
---|
135 | break; |
---|
136 | } |
---|
137 | |
---|
138 | #if 0 |
---|
139 | if( ( stat->status & ( TR_STATUS_DOWNLOAD | TR_STATUS_SEED ) ) && |
---|
140 | ( stat->status & TR_TRACKER_ERROR ) ) |
---|
141 | { |
---|
142 | fPeersString = [NSString stringWithFormat: @"%@%@", |
---|
143 | @"Error: ", [NSString stringWithUTF8String: stat->error]]; |
---|
144 | } |
---|
145 | #endif |
---|
146 | |
---|
147 | [fUploadString setString: @""]; |
---|
148 | if( fStat->progress == 1.0 ) |
---|
149 | { |
---|
150 | [fDownloadString setString: @"Ratio: "]; |
---|
151 | [fDownloadString appendString: [NSString stringForRatio: |
---|
152 | fStat->downloaded upload: fStat->uploaded]]; |
---|
153 | } |
---|
154 | else |
---|
155 | { |
---|
156 | [fDownloadString setString: @"DL: "]; |
---|
157 | [fDownloadString appendString: [NSString stringForSpeed: |
---|
158 | fStat->rateDownload]]; |
---|
159 | } |
---|
160 | [fUploadString setString: @"UL: "]; |
---|
161 | [fUploadString appendString: [NSString stringForSpeed: |
---|
162 | fStat->rateUpload]]; |
---|
163 | } |
---|
164 | |
---|
165 | - (void) start |
---|
166 | { |
---|
167 | if( fStat->status & TR_STATUS_INACTIVE ) |
---|
168 | { |
---|
169 | tr_torrentStart( fHandle ); |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | - (void) stop |
---|
174 | { |
---|
175 | if( fStat->status & TR_STATUS_ACTIVE ) |
---|
176 | { |
---|
177 | tr_torrentStop( fHandle ); |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | - (void) sleep |
---|
182 | { |
---|
183 | if( fStat->status & TR_STATUS_ACTIVE ) |
---|
184 | { |
---|
185 | [self stop]; |
---|
186 | fResumeOnWake = YES; |
---|
187 | } |
---|
188 | else |
---|
189 | { |
---|
190 | fResumeOnWake = NO; |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | - (void) wakeUp |
---|
195 | { |
---|
196 | if( fResumeOnWake ) |
---|
197 | { |
---|
198 | [self start]; |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | - (NSImage *) icon |
---|
203 | { |
---|
204 | return fIcon; |
---|
205 | } |
---|
206 | |
---|
207 | - (NSString *) path |
---|
208 | { |
---|
209 | return [NSString stringWithUTF8String: fInfo->torrent]; |
---|
210 | } |
---|
211 | |
---|
212 | - (NSString *) name |
---|
213 | { |
---|
214 | return [NSString stringWithUTF8String: fInfo->name]; |
---|
215 | } |
---|
216 | |
---|
217 | - (uint64_t) size |
---|
218 | { |
---|
219 | return fInfo->totalSize; |
---|
220 | } |
---|
221 | |
---|
222 | - (float) progress |
---|
223 | { |
---|
224 | return fStat->progress; |
---|
225 | } |
---|
226 | |
---|
227 | - (BOOL) isActive |
---|
228 | { |
---|
229 | return ( fStat->status & TR_STATUS_ACTIVE ); |
---|
230 | } |
---|
231 | |
---|
232 | - (BOOL) isSeeding |
---|
233 | { |
---|
234 | return ( fStat->status == TR_STATUS_SEED ); |
---|
235 | } |
---|
236 | |
---|
237 | - (BOOL) isPaused |
---|
238 | { |
---|
239 | return ( fStat->status == TR_STATUS_PAUSE ); |
---|
240 | } |
---|
241 | |
---|
242 | - (BOOL) justFinished |
---|
243 | { |
---|
244 | return tr_getFinished( fHandle ); |
---|
245 | } |
---|
246 | |
---|
247 | - (NSString *) statusString |
---|
248 | { |
---|
249 | return fStatusString; |
---|
250 | } |
---|
251 | |
---|
252 | - (NSString *) infoString |
---|
253 | { |
---|
254 | return fInfoString; |
---|
255 | } |
---|
256 | |
---|
257 | - (NSString *) downloadString |
---|
258 | { |
---|
259 | return fDownloadString; |
---|
260 | } |
---|
261 | |
---|
262 | - (NSString *) uploadString |
---|
263 | { |
---|
264 | return fUploadString; |
---|
265 | } |
---|
266 | |
---|
267 | @end |
---|