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
use crate::{c, to_cstring, ChannelRef, Context};
use std::ptr;

impl Context {
    /// Gets the current channel context.
    pub fn get_current_channel(&self) -> ChannelRef {
        ChannelRef {
            ph: self.handle,
            handle: unsafe { c!(hexchat_get_context, self.handle) },
        }
    }
    /// Gets the channel that's currently focused in the HexChat window. Returns the `ChannelRef` if
    /// found, or `None` if none exists.
    pub fn get_focused_channel(&self) -> Option<ChannelRef> {
        let handle = unsafe { c!(hexchat_find_context, self.handle, ptr::null(), ptr::null()) };
        if handle.is_null() {
            None
        } else {
            Some(ChannelRef {
                ph: self.handle,
                handle,
            })
        }
    }
    /// Gets the frontmost channel in a particular server. Returns the `ChannelRef` if found, or
    /// `None` if none exists.
    pub fn get_focused_channel_in_server(&self, server_name: &str) -> Option<ChannelRef> {
        let server_name = to_cstring(server_name);
        let handle = unsafe {
            c!(
                hexchat_find_context,
                self.handle,
                server_name.as_ptr(),
                ptr::null()
            )
        };
        if handle.is_null() {
            None
        } else {
            Some(ChannelRef {
                ph: self.handle,
                handle,
            })
        }
    }
    /// Gets the first channel with the specified name in any server. Returns the `ChannelRef` if
    /// found, or `None` if none exists.
    pub fn get_first_channel(&self, channel_name: &str) -> Option<ChannelRef> {
        let channel_name = to_cstring(channel_name);
        let handle = unsafe {
            c!(
                hexchat_find_context,
                self.handle,
                ptr::null(),
                channel_name.as_ptr()
            )
        };
        if handle.is_null() {
            None
        } else {
            Some(ChannelRef {
                ph: self.handle,
                handle,
            })
        }
    }
    /// Gets the first channel with the specified name in the specified server. Returns the
    /// `ChannelRef` if found, or `None` if none exists.
    pub fn get_channel(&self, server_name: &str, channel_name: &str) -> Option<ChannelRef> {
        let channel_name = to_cstring(channel_name);
        let server_name = to_cstring(server_name);
        let handle = unsafe {
            c!(
                hexchat_find_context,
                self.handle,
                server_name.as_ptr(),
                channel_name.as_ptr()
            )
        };
        if handle.is_null() {
            None
        } else {
            Some(ChannelRef {
                ph: self.handle,
                handle,
            })
        }
    }
}