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
// Copyright 2019, The Gtk-rs Project Developers.
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use gio_sys;
use glib_sys;

use glib::prelude::*;
use glib::subclass::prelude::*;
use glib::translate::*;

use glib::Error;

use Cancellable;
use IOStream;
use IOStreamClass;

use std::mem;
use std::ptr;

pub trait IOStreamImpl: IOStreamImplExt + Send + 'static {
    fn get_input_stream(&self, stream: &IOStream) -> crate::InputStream {
        self.parent_get_input_stream(stream)
    }

    fn get_output_stream(&self, stream: &IOStream) -> crate::OutputStream {
        self.parent_get_output_stream(stream)
    }

    fn close(&self, stream: &IOStream, cancellable: Option<&Cancellable>) -> Result<(), Error> {
        self.parent_close(stream, cancellable)
    }
}

pub trait IOStreamImplExt {
    fn parent_get_input_stream(&self, stream: &IOStream) -> crate::InputStream;

    fn parent_get_output_stream(&self, stream: &IOStream) -> crate::OutputStream;

    fn parent_close(
        &self,
        stream: &IOStream,
        cancellable: Option<&Cancellable>,
    ) -> Result<(), Error>;
}

impl<T: IOStreamImpl + ObjectImpl> IOStreamImplExt for T {
    fn parent_get_input_stream(&self, stream: &IOStream) -> crate::InputStream {
        unsafe {
            let data = self.get_type_data();
            let parent_class = data.as_ref().get_parent_class() as *mut gio_sys::GIOStreamClass;
            let f = (*parent_class)
                .get_input_stream
                .expect("No parent class implementation for \"get_input_stream\"");
            from_glib_none(f(stream.to_glib_none().0))
        }
    }

    fn parent_get_output_stream(&self, stream: &IOStream) -> crate::OutputStream {
        unsafe {
            let data = self.get_type_data();
            let parent_class = data.as_ref().get_parent_class() as *mut gio_sys::GIOStreamClass;
            let f = (*parent_class)
                .get_output_stream
                .expect("No parent class implementation for \"get_output_stream\"");
            from_glib_none(f(stream.to_glib_none().0))
        }
    }

    fn parent_close(
        &self,
        stream: &IOStream,
        cancellable: Option<&Cancellable>,
    ) -> Result<(), Error> {
        unsafe {
            let data = self.get_type_data();
            let parent_class = data.as_ref().get_parent_class() as *mut gio_sys::GIOStreamClass;
            let mut err = ptr::null_mut();
            if let Some(f) = (*parent_class).close_fn {
                if from_glib(f(
                    stream.to_glib_none().0,
                    cancellable.to_glib_none().0,
                    &mut err,
                )) {
                    Ok(())
                } else {
                    Err(from_glib_full(err))
                }
            } else {
                Ok(())
            }
        }
    }
}

unsafe impl<T: ObjectSubclass + IOStreamImpl> IsSubclassable<T> for IOStreamClass {
    fn override_vfuncs(&mut self) {
        <glib::ObjectClass as IsSubclassable<T>>::override_vfuncs(self);
        unsafe {
            let klass = &mut *(self as *mut Self as *mut gio_sys::GIOStreamClass);
            klass.get_input_stream = Some(stream_get_input_stream::<T>);
            klass.get_output_stream = Some(stream_get_output_stream::<T>);
            klass.close_fn = Some(stream_close::<T>);
        }
    }
}

lazy_static! {
    pub static ref OUTPUT_STREAM_QUARK: glib::Quark =
        glib::Quark::from_string("gtk-rs-subclass-output-stream");
    pub static ref INPUT_STREAM_QUARK: glib::Quark =
        glib::Quark::from_string("gtk-rs-subclass-input-stream");
}

unsafe extern "C" fn stream_get_input_stream<T: ObjectSubclass>(
    ptr: *mut gio_sys::GIOStream,
) -> *mut gio_sys::GInputStream
where
    T: IOStreamImpl,
{
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.get_impl();
    let wrap: IOStream = from_glib_borrow(ptr);

    let ret = imp.get_input_stream(&wrap);

    // Ensure that a) the stream stays alive as long as the IO stream instance and
    // b) that the same stream is returned every time. This is a requirement by the
    // IO stream API.
    let old_ptr = gobject_sys::g_object_get_qdata(ptr as *mut _, INPUT_STREAM_QUARK.to_glib());
    if !old_ptr.is_null() {
        assert_eq!(
            old_ptr as *mut _,
            ret.as_ptr(),
            "Did not return same input stream again"
        );
    }

    unsafe extern "C" fn unref(ptr: glib_sys::gpointer) {
        gobject_sys::g_object_unref(ptr as *mut _);
    }
    gobject_sys::g_object_set_qdata_full(
        ptr as *mut _,
        INPUT_STREAM_QUARK.to_glib(),
        ret.as_ptr() as *mut _,
        Some(unref),
    );

    ret.to_glib_none().0
}

unsafe extern "C" fn stream_get_output_stream<T: ObjectSubclass>(
    ptr: *mut gio_sys::GIOStream,
) -> *mut gio_sys::GOutputStream
where
    T: IOStreamImpl,
{
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.get_impl();
    let wrap: IOStream = from_glib_borrow(ptr);

    let ret = imp.get_output_stream(&wrap);

    // Ensure that a) the stream stays alive as long as the IO stream instance and
    // b) that the same stream is returned every time. This is a requirement by the
    // IO stream API.
    let old_ptr = gobject_sys::g_object_get_qdata(ptr as *mut _, OUTPUT_STREAM_QUARK.to_glib());
    if !old_ptr.is_null() {
        assert_eq!(
            old_ptr as *mut _,
            ret.as_ptr(),
            "Did not return same output stream again"
        );
    }

    unsafe extern "C" fn unref(ptr: glib_sys::gpointer) {
        gobject_sys::g_object_unref(ptr as *mut _);
    }
    gobject_sys::g_object_set_qdata_full(
        ptr as *mut _,
        OUTPUT_STREAM_QUARK.to_glib(),
        ret.as_ptr() as *mut _,
        Some(unref),
    );

    ret.to_glib_none().0
}

unsafe extern "C" fn stream_close<T: ObjectSubclass>(
    ptr: *mut gio_sys::GIOStream,
    cancellable: *mut gio_sys::GCancellable,
    err: *mut *mut glib_sys::GError,
) -> glib_sys::gboolean
where
    T: IOStreamImpl,
{
    let instance = &*(ptr as *mut T::Instance);
    let imp = instance.get_impl();
    let wrap: IOStream = from_glib_borrow(ptr);

    match imp.close(
        &wrap,
        Option::<Cancellable>::from_glib_borrow(cancellable).as_ref(),
    ) {
        Ok(_) => glib_sys::GTRUE,
        Err(mut e) => {
            *err = e.to_glib_none_mut().0;
            mem::forget(e);
            glib_sys::GFALSE
        }
    }
}