1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//! The Torrent struct and related components.
use std::ffi;
use std::mem;
use std::path::PathBuf;
use std::ptr::{null, null_mut, NonNull};
use std::sync::{Arc, RwLock};

use serde::ser::{SerializeStruct, Serializer};
use serde::{Deserialize, Serialize};
use transmission_sys;

use super::torrentinfo::TorrentFile;
use super::TorrentBuilder;
use super::TorrentInfo;
use super::TorrentStats;
use crate::error::{Error, ParseInt, TrResult};

/// The priority of a torrent as either:
///
/// - High
/// - Normal
/// - Low
///
/// Priority does not directly affect download speed but
/// instead changes how the torrent will be queued compared to other torrents
#[derive(Debug, Serialize, Deserialize)]
#[repr(i8)]
pub enum Priority {
    Low = transmission_sys::TR_PRI_LOW as i8,
    Normal = transmission_sys::TR_PRI_NORMAL as i8,
    High = transmission_sys::TR_PRI_HIGH as i8,
}

impl From<transmission_sys::_bindgen_ty_2> for Priority {
    fn from(f: transmission_sys::_bindgen_ty_2) -> Self {
        match f {
            transmission_sys::TR_PRI_LOW => Priority::Low,
            transmission_sys::TR_PRI_NORMAL => Priority::Normal,
            transmission_sys::TR_PRI_HIGH => Priority::High,
        }
    }
}

impl From<i8> for Priority {
    fn from(f: i8) -> Self {
        match f {
            x if x < 0 => Priority::Low,
            0 => Priority::Normal,
            x if x < 0 => Priority::High,
            _ => Priority::Normal,
        }
    }
}

/// Representation of a torrent download.
///
/// Can be used to start, stop, or get the information of a torrent.
#[derive(Clone)]
pub struct Torrent {
    tr_torrent: Arc<RwLock<NonNull<transmission_sys::tr_torrent>>>,
}

impl<'a> Torrent {
    /// Create a new torrent from a tr_ctor
    pub(crate) fn from_ctor(ctor: *mut transmission_sys::tr_ctor) -> TrResult<Self> {
        let tor;
        let mut error = 0;
        let mut dupli = 0;
        unsafe {
            tor = transmission_sys::tr_torrentNew(ctor, &mut error, &mut dupli);
        }
        // Match the possible errors from torrentNew
        Error::from(error as ParseInt).to_result().and_then(|_| {
            Ok(Self {
                tr_torrent: Arc::new(RwLock::new(NonNull::new(tor).unwrap())),
            })
        })
    }

    pub(crate) fn from_tr_torrent(tr_torrent: *mut transmission_sys::tr_torrent) -> TrResult<Self> {
        Ok(Self {
            tr_torrent: Arc::new(RwLock::new(NonNull::new(tr_torrent).unwrap())),
        })
    }

    pub fn parse_torrent_file(path: &str) -> TrResult<TorrentInfo> {
        let path = ffi::CString::new(path).unwrap();
        unsafe {
            let ctor = transmission_sys::tr_ctorNew(null());
            let mut info: transmission_sys::tr_info = mem::uninitialized();
            match transmission_sys::tr_ctorSetMetainfoFromFile(ctor, path.as_ptr()) {
                0 => match transmission_sys::tr_torrentParse(ctor, &mut info) {
                    transmission_sys::tr_parse_result::TR_PARSE_OK => Ok(TorrentInfo::from(info)),
                    _ => Err(Error::ParseErr),
                },
                _ => Err(Error::ParseErr),
            }
        }
    }

    /// Alias to `TorrentBuilder::new()`
    pub fn build() -> TorrentBuilder {
        TorrentBuilder::new()
    }

    /// Start or resume the torrent
    pub fn start(&self) {
        let mut tor = self.tr_torrent.write().unwrap();
        unsafe {
            transmission_sys::tr_torrentStart(tor.as_mut());
        }
    }

    /// Stop (pause) the torrent
    pub fn stop(&self) {
        let mut tor = self.tr_torrent.write().unwrap();
        unsafe {
            transmission_sys::tr_torrentStop(tor.as_mut());
        }
    }

    /// Removes a torrent from the downloads
    /// Consumes the Torrent
    pub fn remove(self, with_data: bool) {
        let mut tor = Arc::try_unwrap(self.tr_torrent)
            .unwrap()
            .into_inner()
            .unwrap();
        unsafe {
            transmission_sys::tr_torrentRemove(tor.as_mut(), with_data, None);
        }
    }

    /// Verify the torrent
    // TODO callback function
    pub fn verify(&self) {
        let mut tor = self.tr_torrent.write().unwrap();
        unsafe { transmission_sys::tr_torrentVerify(tor.as_mut(), None, null_mut()) }
    }

    //# The following functions get information about the torrent

    /// This torrent's name
    pub fn name(&self) -> &str {
        let tor = self.tr_torrent.read().unwrap();
        unsafe {
            let c_str = transmission_sys::tr_torrentName(tor.as_ref());
            ffi::CStr::from_ptr(c_str).to_str().unwrap()
        }
    }

    /// The unique ID of the torrent
    pub fn id(&self) -> i32 {
        let tor = self.tr_torrent.read().unwrap();
        unsafe { transmission_sys::tr_torrentId(tor.as_ref()) }
    }

    /// The stats of the torrent as given by Transmission
    ///
    /// These are only available after the torrent has been added to a session
    pub fn stats(&self) -> TorrentStats {
        let mut tor = self.tr_torrent.write().unwrap();
        unsafe { TorrentStats::from(transmission_sys::tr_torrentStatCached(tor.as_mut())) }
    }

    /// The info of the torrent as given by Transmission
    ///
    /// This is available after the torrent has been parsed and does not need to
    /// be added to a session.
    pub fn info(&self) -> TorrentInfo {
        let tor = self.tr_torrent.read().unwrap();
        let info;
        unsafe {
            info = transmission_sys::tr_torrentInfo(tor.as_ref());
        }
        TorrentInfo::from(unsafe { *info })
    }

    /// Set the seed ratio of the torrent
    pub fn set_ratio(&mut self, limit: f64) {
        let mut tor = self.tr_torrent.write().unwrap();
        // Does ratio mode need to be toggled?
        unsafe {
            transmission_sys::tr_torrentSetRatioLimit(tor.as_mut(), limit);
        }
    }

    /// Set the download directory of the torrent
    pub fn set_download_dir(&mut self, download_dir: PathBuf) {
        let mut tor = self.tr_torrent.write().unwrap();
        let d_dir = ffi::CString::new(download_dir.to_str().unwrap()).unwrap();
        unsafe {
            transmission_sys::tr_torrentSetDownloadDir(tor.as_mut(), d_dir.as_ptr());
        }
    }

    /// Set the priority of the torrent
    ///
    /// See `Priority` for more information
    pub fn set_priority(&mut self, priority: Priority) {
        let mut tor = self.tr_torrent.write().unwrap();
        unsafe {
            transmission_sys::tr_torrentSetPriority(tor.as_mut(), priority as i8);
        }
    }

    ///# File Related Functions

    /// Get the index of a file in a torrent
    pub fn get_file_index(&self, file: &TorrentFile) -> Option<usize> {
        self.info()
            .files
            .iter()
            .position(|e| e.name == file.name && e.length == file.length)
    }

    pub fn set_file_download(&mut self, file: TorrentFile, download: bool) {
        self.set_files_download(vec![file], download);
    }

    /// Set whether or not a set of files should be downloaded
    pub fn set_files_download(&mut self, files: Vec<TorrentFile>, download: bool) {
        let ids = files
            .iter()
            .filter_map(|f| self.get_file_index(f).and_then(|e| Some(e as u32)))
            .collect();
        self.set_files_download_by_id(ids, download);
    }

    /// Set whether or not a set of files, by ids, should be downloaded
    pub fn set_files_download_by_id(&mut self, ids: Vec<u32>, download: bool) {
        let mut tor = self.tr_torrent.write().unwrap();
        unsafe {
            transmission_sys::tr_torrentSetFileDLs(
                tor.as_mut(),
                ids.as_ptr(),
                ids.len() as u32,
                download,
            );
        }
    }

    pub fn set_file_priority(&mut self, file: TorrentFile, priority: Priority) {
        self.set_files_priorities(vec![file], priority);
    }

    /// Set the priority of a set of files
    pub fn set_files_priorities(&mut self, files: Vec<TorrentFile>, priority: Priority) {
        let ids = files
            .iter()
            .filter_map(|f| self.get_file_index(f).and_then(|e| Some(e as u32)))
            .collect();
        self.set_files_priorities_by_id(ids, priority);
    }

    /// Set the priority of a set of files, by ids
    pub fn set_files_priorities_by_id(&mut self, ids: Vec<u32>, priority: Priority) {
        let mut tor = self.tr_torrent.write().unwrap();
        unsafe {
            transmission_sys::tr_torrentSetFilePriorities(
                tor.as_mut(),
                ids.as_ptr(),
                ids.len() as u32,
                priority as i8,
            )
        }
    }
}

impl serde::ser::Serialize for Torrent {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut state = serializer.serialize_struct("Torrent", 2)?;
        state.serialize_field("info", &self.info())?;
        state.serialize_field("stats", &self.stats())?;
        state.end()
    }
}

impl AsMut<Torrent> for Torrent {
    fn as_mut(&mut self) -> &mut Self {
        self
    }
}

unsafe impl std::marker::Send for Torrent {}
unsafe impl std::marker::Sync for Torrent {}