gio/auto/
charset_converter.rs

1// This file was generated by gir (https://github.com/gtk-rs/gir)
2// from gir-files (https://github.com/gtk-rs/gir-files)
3// DO NOT EDIT
4
5use crate::{ffi, Converter, Initable};
6use glib::{
7    prelude::*,
8    signal::{connect_raw, SignalHandlerId},
9    translate::*,
10};
11use std::boxed::Box as Box_;
12
13glib::wrapper! {
14    #[doc(alias = "GCharsetConverter")]
15    pub struct CharsetConverter(Object<ffi::GCharsetConverter, ffi::GCharsetConverterClass>) @implements Converter, Initable;
16
17    match fn {
18        type_ => || ffi::g_charset_converter_get_type(),
19    }
20}
21
22impl CharsetConverter {
23    #[doc(alias = "g_charset_converter_new")]
24    pub fn new(to_charset: &str, from_charset: &str) -> Result<CharsetConverter, glib::Error> {
25        unsafe {
26            let mut error = std::ptr::null_mut();
27            let ret = ffi::g_charset_converter_new(
28                to_charset.to_glib_none().0,
29                from_charset.to_glib_none().0,
30                &mut error,
31            );
32            if error.is_null() {
33                Ok(from_glib_full(ret))
34            } else {
35                Err(from_glib_full(error))
36            }
37        }
38    }
39
40    // rustdoc-stripper-ignore-next
41    /// Creates a new builder-pattern struct instance to construct [`CharsetConverter`] objects.
42    ///
43    /// This method returns an instance of [`CharsetConverterBuilder`](crate::builders::CharsetConverterBuilder) which can be used to create [`CharsetConverter`] objects.
44    pub fn builder() -> CharsetConverterBuilder {
45        CharsetConverterBuilder::new()
46    }
47
48    #[doc(alias = "g_charset_converter_get_num_fallbacks")]
49    #[doc(alias = "get_num_fallbacks")]
50    pub fn num_fallbacks(&self) -> u32 {
51        unsafe { ffi::g_charset_converter_get_num_fallbacks(self.to_glib_none().0) }
52    }
53
54    #[doc(alias = "g_charset_converter_get_use_fallback")]
55    #[doc(alias = "get_use_fallback")]
56    #[doc(alias = "use-fallback")]
57    pub fn uses_fallback(&self) -> bool {
58        unsafe {
59            from_glib(ffi::g_charset_converter_get_use_fallback(
60                self.to_glib_none().0,
61            ))
62        }
63    }
64
65    #[doc(alias = "g_charset_converter_set_use_fallback")]
66    #[doc(alias = "use-fallback")]
67    pub fn set_use_fallback(&self, use_fallback: bool) {
68        unsafe {
69            ffi::g_charset_converter_set_use_fallback(
70                self.to_glib_none().0,
71                use_fallback.into_glib(),
72            );
73        }
74    }
75
76    #[doc(alias = "from-charset")]
77    pub fn from_charset(&self) -> Option<glib::GString> {
78        ObjectExt::property(self, "from-charset")
79    }
80
81    #[doc(alias = "to-charset")]
82    pub fn to_charset(&self) -> Option<glib::GString> {
83        ObjectExt::property(self, "to-charset")
84    }
85
86    #[doc(alias = "use-fallback")]
87    pub fn connect_use_fallback_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
88        unsafe extern "C" fn notify_use_fallback_trampoline<F: Fn(&CharsetConverter) + 'static>(
89            this: *mut ffi::GCharsetConverter,
90            _param_spec: glib::ffi::gpointer,
91            f: glib::ffi::gpointer,
92        ) {
93            let f: &F = &*(f as *const F);
94            f(&from_glib_borrow(this))
95        }
96        unsafe {
97            let f: Box_<F> = Box_::new(f);
98            connect_raw(
99                self.as_ptr() as *mut _,
100                c"notify::use-fallback".as_ptr() as *const _,
101                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
102                    notify_use_fallback_trampoline::<F> as *const (),
103                )),
104                Box_::into_raw(f),
105            )
106        }
107    }
108}
109
110// rustdoc-stripper-ignore-next
111/// A [builder-pattern] type to construct [`CharsetConverter`] objects.
112///
113/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
114#[must_use = "The builder must be built to be used"]
115pub struct CharsetConverterBuilder {
116    builder: glib::object::ObjectBuilder<'static, CharsetConverter>,
117}
118
119impl CharsetConverterBuilder {
120    fn new() -> Self {
121        Self {
122            builder: glib::object::Object::builder(),
123        }
124    }
125
126    pub fn from_charset(self, from_charset: impl Into<glib::GString>) -> Self {
127        Self {
128            builder: self.builder.property("from-charset", from_charset.into()),
129        }
130    }
131
132    pub fn to_charset(self, to_charset: impl Into<glib::GString>) -> Self {
133        Self {
134            builder: self.builder.property("to-charset", to_charset.into()),
135        }
136    }
137
138    pub fn use_fallback(self, use_fallback: bool) -> Self {
139        Self {
140            builder: self.builder.property("use-fallback", use_fallback),
141        }
142    }
143
144    // rustdoc-stripper-ignore-next
145    /// Build the [`CharsetConverter`].
146    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
147    pub fn build(self) -> CharsetConverter {
148        self.builder.build()
149    }
150}