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
#![cfg(feature = "threadsafe")]

//! A thread-safe version of `Context`. The methods of these objects will
//! execute on the main thread of Hexchat. Invoking them is virutally the same
//! as with `Context` objects.

use std::sync::Arc;
use std::fmt;
use std::sync::RwLock;

use send_wrapper::SendWrapper;

use crate::HexchatError;
use crate::context::*;
use crate::thread_facilities::*;
use crate::threadsafe_list_iterator::*;

use HexchatError::*;

const DROPPED_ERR: &str = "Context dropped from threadsafe context.";

/// A thread-safe version of `Context`. Its methods automatically execute on
/// the Hexchat main thread. The full set of methods of `Context` aren't
/// fully implemented for this struct because some can't be trusted to produce
/// predictable results from other threads. For instance `.set()` from a thread
/// would only cause Hexchat to momentarily set its context, but Hexchat's
/// context could change again at any moment while the other thread is
/// executing.
///
///
#[derive(Clone)]
pub struct ThreadSafeContext {
    ctx : Arc<RwLock<Option<SendWrapper<Context>>>>,
}

unsafe impl Send for ThreadSafeContext {}
unsafe impl Sync for ThreadSafeContext {}

impl ThreadSafeContext {
    /// Creates a new `ThreadSafeContext` object, which wraps a `Context` object
    /// internally. Only to be called from the main thread internally.
    /// 
    pub (crate)
    fn new(ctx: Context) -> Self {
        Self { ctx: Arc::new(RwLock::new(Some(SendWrapper::new(ctx)))) }
    }

    /// Gets the user's current `Context` wrapped in a `ThreadSafeContext` 
    /// object.
    ///
    pub fn get() -> Result<Self, HexchatError> {
        main_thread(|_| Context::get().map(Self::new)).get()
        .and_then(|r| r.ok_or_else(|| ContextAcquisitionFailed("?, ?".into())))
    }

    /// Gets a `ThreadSafeContext` object associated with the given channel.
    /// # Arguments
    /// * `network` - The network of the channel to get the context for.
    /// * `channel` - The channel to get the context of.
    /// # Returns
    /// * `Ok(ThreadSafeContext)` on success, and `HexchatError` on failure.
    ///
    pub fn find(network: &str, channel: &str) -> Result<Self, HexchatError> {
        let data = (network.to_string(), channel.to_string());
        main_thread(move |_| Context::find(&data.0, &data.1).map(Self::new))
        .get()
        .and_then(|r| r.ok_or_else(|| {
                    let msg = format!("{}, {}", network, channel);
                    ContextAcquisitionFailed(msg) 
                }))
    }

    /// Prints the message to the `ThreadSafeContext` object's Hexchat context.
    /// This is how messages can be printed to Hexchat windows apart from the
    /// currently active one.
    ///
    pub fn print(&self, message: &str) -> Result<(), HexchatError> {
        let message = message.to_string();
        let me = self.clone();
        main_thread(move |_| {
            me.ctx.read().unwrap().as_ref()
                  .ok_or_else(|| ContextDropped(DROPPED_ERR.into()))?
                  .print(&message)
        }).get().and_then(|r| r)
    }

    /// Prints without waiting for asynchronous completion. This will print
    /// faster than `.print()` because it just stacks up print requests in the
    /// timer queue and moves on without blocking. The downside is errors
    /// will not be checked. Error messages will, however, still be printed
    /// if any occur.
    ///
    pub fn aprint(&self, message: &str) {
        let message = message.to_string();
        let me = self.clone();
        main_thread(move |hc| {
            if let Err(err)
                = me.ctx.read().unwrap().as_ref().unwrap().print(&message) {
                hc.print(&format!("\x0313Context.aprint() failed to acquire \
                                  context: {}", err));
                hc.print(&format!("\x0313{}", message));
            }
        });
    }

    /// Issues a command in the context held by the `ThreadSafeContext` object.
    ///
    pub fn command(&self, command: &str) -> Result<(), HexchatError> {
        let command = command.to_string();
        let me = self.clone();
        main_thread(move |_| {
            me.ctx.read().unwrap().as_ref()
                  .ok_or_else(|| ContextDropped(DROPPED_ERR.into()))?
                  .command(&command)
        }).get().and_then(|r| r)
    }

    /// Gets information from the channel/window that the `ThreadSafeContext`
    /// object holds an internal pointer to.
    ///
    pub fn get_info(&self, info: &str) -> Result<String, HexchatError> {
        let info = info.to_string();
        let me = self.clone();
        main_thread(move |_| {
            me.ctx.read().unwrap().as_ref()
                  .ok_or_else(|| ContextDropped(DROPPED_ERR.into()))?
                  .get_info(&info)
            }).get().and_then(|r| r)
    }

    /// Issues a print event to the context held by the `ThreadSafeContext`
    /// object.
    ///
    pub fn emit_print(&self, event_name: &str, var_args: &[&str])
        -> Result<(), HexchatError>
    {
        let var_args: Vec<String> = var_args.iter()
                                            .map(|s| s.to_string())
                                            .collect();
        let data = (event_name.to_string(), var_args);
        let me = self.clone();
        main_thread(move |_| {
            let var_args: Vec<&str> = data.1.iter()
                                            .map(|s| s.as_str())
                                            .collect();
            me.ctx.read().unwrap().as_ref()
                  .ok_or_else(|| ContextDropped(DROPPED_ERR.into()))?
                  .emit_print(&data.0, var_args.as_slice())
        }).get().and_then(|r| r)
    }

    /// Gets a `ThreadSafeListIterator` from the context.  If the list doesn't 
    /// exist, or a problem occurs, an error will be returned.
    ///
    pub fn list_get(&self,
                    name: &str)
        -> Result<ThreadSafeListIterator, HexchatError>
    {
        let name = name.to_string();
        let me = self.clone();
        main_thread(move |_| {
            if let Some(ctx) = me.ctx.read().unwrap().as_ref() {
                match ctx.list_get(&name) {
                    Ok(list) => {
                        Ok(ThreadSafeListIterator::create(list))
                    },
                    Err(err) => Err(err),
                }
            } else {
                Err(ContextDropped(DROPPED_ERR.into()))
            }
        }).get().and_then(|r| r)
    }
    /// Returns the network name associated with the context.
    /// 
    pub fn network(&self) -> Result<String, HexchatError> {
        let me = self.clone();
        main_thread(move |_| {
            if let Some(ctx) = me.ctx.read().unwrap().as_ref() {
                Ok(ctx.network())
            } else {
                Err(ContextDropped(DROPPED_ERR.into()))
            }
        }).get().and_then(|r| r)
    }

    /// Returns the channel name associated with the context.
    /// 
    pub fn channel(&self) -> Result<String, HexchatError> {
        let me = self.clone();
        main_thread(move |_| {
            if let Some(ctx) = me.ctx.read().unwrap().as_ref() {
                Ok(ctx.channel())
            } else {
                Err(ContextDropped(DROPPED_ERR.into()))
            }
        }).get().and_then(|r| r)
    }
}

impl fmt::Display for ThreadSafeContext {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self.ctx)
    }
}

impl Drop for ThreadSafeContext {
    fn drop(&mut self) {
        if Arc::strong_count(&self.ctx) <= 1
            && self.ctx.read().unwrap().is_some() {
            let me = self.clone();
            main_thread(move |_| {
                me.ctx.write().unwrap().take();
            });
        }
    }
}

impl fmt::Debug for ThreadSafeContext {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // A bit overkill, but fixes problems with users trying to debug print
        // the object from other threads.
        let me = self.clone();
        let s = main_thread(move |_| {
            if let Ok(guard) = me.ctx.read() {
                if let Some(ctx) =  guard.as_ref() {
                    format!("Context({:?}, {:?})", ctx.network(), ctx.channel())
                } else {
                    "Context(Error getting info)".to_string()
                }
            } else {
                "Context(Error getting info)".to_string()
            }
        }).get().unwrap();
        write!(f, "{}", s)
    }
}