1use std::mem;
2
3use cxx::UniquePtr;
4
5use crate::{
6 add_torrent_params::{AddTorrentParamsIntoPtr, AddTorrentParamsRef},
7 alerts::Alert,
8 ffi::ffi,
9 settings_pack::SettingsPack,
10 torrent_handle::{StatusFlags, TorrentHandle},
11};
12
13pub struct LtSession {
14 inner: UniquePtr<ffi::session>,
15 alerts: Vec<Alert>,
16}
17
18impl LtSession {
19 pub fn new() -> LtSession {
20 LtSession {
21 inner: ffi::lt_create_session(),
22 alerts: Vec::new(),
23 }
24 }
25
26 pub fn new_with_settings(settings: &SettingsPack) -> LtSession {
27 LtSession {
28 inner: ffi::lt_create_session_with_settings(settings.inner()),
29 alerts: Vec::new(),
30 }
31 }
32
33 pub fn add_torrent<'a>(&'a mut self, _params: &AddTorrentParamsRef) -> TorrentHandle {
34 unimplemented!()
35 }
37
38 pub fn async_add_torrent<T: AddTorrentParamsIntoPtr>(&mut self, params: &T) {
39 unsafe { ffi::lt_session_async_add_torrent(self.inner.pin_mut(), params.as_ptr()) };
40 }
41
42 pub fn pop_alerts(&mut self) {
43 let alerts = ffi::lt_session_pop_alerts(self.inner.pin_mut());
44 self.alerts.clear();
45
46 for alert in alerts {
47 self.alerts.push(alert.into());
48 }
49 }
50
51 pub fn alerts(&self) -> &Vec<Alert> {
52 &self.alerts
53 }
54
55 pub fn post_torrent_updates(&mut self, flags: StatusFlags) {
60 ffi::lt_session_post_torrent_updates(self.inner.pin_mut(), flags.bits());
61 }
62
63 pub unsafe fn take_alerts(&mut self) -> Vec<Alert> {
68 let alerts = mem::replace(&mut self.alerts, Vec::new());
69 alerts
70 }
71}
72
73unsafe impl Send for LtSession {}
75
76