atri_plugin/
client.rs

1use crate::contact::friend::Friend;
2use crate::contact::group::Group;
3use crate::loader::get_vtb;
4use atri_ffi::Handle;
5use std::fmt::{Display, Formatter};
6
7pub struct Client(pub(crate) Handle);
8
9impl Client {
10    pub fn id(&self) -> i64 {
11        (get_vtb().client_get_id)(self.0)
12    }
13
14    pub fn nickname(&self) -> String {
15        let rs = (get_vtb().client_get_nickname)(self.0);
16
17        rs.into()
18    }
19
20    pub fn list() -> Vec<Client> {
21        let raw = (get_vtb().client_get_list)();
22
23        raw.into_vec().into_iter().map(Client).collect()
24    }
25
26    pub fn find(id: i64) -> Option<Self> {
27        let handle = (get_vtb().find_client)(id);
28
29        if handle.is_null() {
30            None
31        } else {
32            Some(Self(handle))
33        }
34    }
35
36    pub fn find_group(&self, id: i64) -> Option<Group> {
37        let ma = (get_vtb().client_find_group)(self.0, id);
38
39        if ma.is_null() {
40            None
41        } else {
42            Some(Group(ma))
43        }
44    }
45
46    pub fn find_friend(&self, id: i64) -> Option<Friend> {
47        let ma = (get_vtb().client_find_friend)(self.0, id);
48
49        if ma.is_null() {
50            None
51        } else {
52            Some(Friend(ma))
53        }
54    }
55
56    pub fn groups(&self) -> Vec<Group> {
57        let ma = (get_vtb().client_get_groups)(self.0);
58        ma.into_vec().into_iter().map(Group).collect()
59    }
60
61    pub fn friends(&self) -> Vec<Friend> {
62        let ma = (get_vtb().client_get_friends)(self.0);
63        ma.into_vec().into_iter().map(Friend).collect()
64    }
65}
66
67impl Clone for Client {
68    fn clone(&self) -> Self {
69        Self((get_vtb().client_clone)(self.0))
70    }
71}
72
73impl Drop for Client {
74    fn drop(&mut self) {
75        (get_vtb().client_drop)(self.0);
76    }
77}
78
79unsafe impl Send for Client {}
80unsafe impl Sync for Client {}
81
82impl Display for Client {
83    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
84        write!(f, "Client({})", self.id())
85    }
86}