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    /// `GCharsetConverter` is an implementation of [`Converter`][crate::Converter] based on
15    /// `GLib::IConv`.
16    ///
17    /// ## Properties
18    ///
19    ///
20    /// #### `from-charset`
21    ///  The character encoding to convert from.
22    ///
23    /// Readable | Writeable | Construct Only
24    ///
25    ///
26    /// #### `to-charset`
27    ///  The character encoding to convert to.
28    ///
29    /// Readable | Writeable | Construct Only
30    ///
31    ///
32    /// #### `use-fallback`
33    ///  Use fallback (of form `\<hexval>`) for invalid bytes.
34    ///
35    /// Readable | Writeable | Construct
36    ///
37    /// # Implements
38    ///
39    /// [`trait@glib::ObjectExt`], [`ConverterExt`][trait@crate::prelude::ConverterExt], [`InitableExt`][trait@crate::prelude::InitableExt], [`ConverterExtManual`][trait@crate::prelude::ConverterExtManual]
40    #[doc(alias = "GCharsetConverter")]
41    pub struct CharsetConverter(Object<ffi::GCharsetConverter, ffi::GCharsetConverterClass>) @implements Converter, Initable;
42
43    match fn {
44        type_ => || ffi::g_charset_converter_get_type(),
45    }
46}
47
48impl CharsetConverter {
49    /// Creates a new #GCharsetConverter.
50    /// ## `to_charset`
51    /// destination charset
52    /// ## `from_charset`
53    /// source charset
54    ///
55    /// # Returns
56    ///
57    /// a new #GCharsetConverter or [`None`] on error.
58    #[doc(alias = "g_charset_converter_new")]
59    pub fn new(to_charset: &str, from_charset: &str) -> Result<CharsetConverter, glib::Error> {
60        unsafe {
61            let mut error = std::ptr::null_mut();
62            let ret = ffi::g_charset_converter_new(
63                to_charset.to_glib_none().0,
64                from_charset.to_glib_none().0,
65                &mut error,
66            );
67            if error.is_null() {
68                Ok(from_glib_full(ret))
69            } else {
70                Err(from_glib_full(error))
71            }
72        }
73    }
74
75    // rustdoc-stripper-ignore-next
76    /// Creates a new builder-pattern struct instance to construct [`CharsetConverter`] objects.
77    ///
78    /// This method returns an instance of [`CharsetConverterBuilder`](crate::builders::CharsetConverterBuilder) which can be used to create [`CharsetConverter`] objects.
79    pub fn builder() -> CharsetConverterBuilder {
80        CharsetConverterBuilder::new()
81    }
82
83    /// Gets the number of fallbacks that @self has applied so far.
84    ///
85    /// # Returns
86    ///
87    /// the number of fallbacks that @self has applied
88    #[doc(alias = "g_charset_converter_get_num_fallbacks")]
89    #[doc(alias = "get_num_fallbacks")]
90    pub fn num_fallbacks(&self) -> u32 {
91        unsafe { ffi::g_charset_converter_get_num_fallbacks(self.to_glib_none().0) }
92    }
93
94    /// Gets the #GCharsetConverter:use-fallback property.
95    ///
96    /// # Returns
97    ///
98    /// [`true`] if fallbacks are used by @self
99    #[doc(alias = "g_charset_converter_get_use_fallback")]
100    #[doc(alias = "get_use_fallback")]
101    #[doc(alias = "use-fallback")]
102    pub fn uses_fallback(&self) -> bool {
103        unsafe {
104            from_glib(ffi::g_charset_converter_get_use_fallback(
105                self.to_glib_none().0,
106            ))
107        }
108    }
109
110    /// Sets the #GCharsetConverter:use-fallback property.
111    /// ## `use_fallback`
112    /// [`true`] to use fallbacks
113    #[doc(alias = "g_charset_converter_set_use_fallback")]
114    #[doc(alias = "use-fallback")]
115    pub fn set_use_fallback(&self, use_fallback: bool) {
116        unsafe {
117            ffi::g_charset_converter_set_use_fallback(
118                self.to_glib_none().0,
119                use_fallback.into_glib(),
120            );
121        }
122    }
123
124    /// The character encoding to convert from.
125    #[doc(alias = "from-charset")]
126    pub fn from_charset(&self) -> Option<glib::GString> {
127        ObjectExt::property(self, "from-charset")
128    }
129
130    /// The character encoding to convert to.
131    #[doc(alias = "to-charset")]
132    pub fn to_charset(&self) -> Option<glib::GString> {
133        ObjectExt::property(self, "to-charset")
134    }
135
136    #[doc(alias = "use-fallback")]
137    pub fn connect_use_fallback_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
138        unsafe extern "C" fn notify_use_fallback_trampoline<F: Fn(&CharsetConverter) + 'static>(
139            this: *mut ffi::GCharsetConverter,
140            _param_spec: glib::ffi::gpointer,
141            f: glib::ffi::gpointer,
142        ) {
143            let f: &F = &*(f as *const F);
144            f(&from_glib_borrow(this))
145        }
146        unsafe {
147            let f: Box_<F> = Box_::new(f);
148            connect_raw(
149                self.as_ptr() as *mut _,
150                b"notify::use-fallback\0".as_ptr() as *const _,
151                Some(std::mem::transmute::<*const (), unsafe extern "C" fn()>(
152                    notify_use_fallback_trampoline::<F> as *const (),
153                )),
154                Box_::into_raw(f),
155            )
156        }
157    }
158}
159
160impl Default for CharsetConverter {
161    fn default() -> Self {
162        glib::object::Object::new::<Self>()
163    }
164}
165
166// rustdoc-stripper-ignore-next
167/// A [builder-pattern] type to construct [`CharsetConverter`] objects.
168///
169/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
170#[must_use = "The builder must be built to be used"]
171pub struct CharsetConverterBuilder {
172    builder: glib::object::ObjectBuilder<'static, CharsetConverter>,
173}
174
175impl CharsetConverterBuilder {
176    fn new() -> Self {
177        Self {
178            builder: glib::object::Object::builder(),
179        }
180    }
181
182    /// The character encoding to convert from.
183    pub fn from_charset(self, from_charset: impl Into<glib::GString>) -> Self {
184        Self {
185            builder: self.builder.property("from-charset", from_charset.into()),
186        }
187    }
188
189    /// The character encoding to convert to.
190    pub fn to_charset(self, to_charset: impl Into<glib::GString>) -> Self {
191        Self {
192            builder: self.builder.property("to-charset", to_charset.into()),
193        }
194    }
195
196    /// Use fallback (of form `\<hexval>`) for invalid bytes.
197    pub fn use_fallback(self, use_fallback: bool) -> Self {
198        Self {
199            builder: self.builder.property("use-fallback", use_fallback),
200        }
201    }
202
203    // rustdoc-stripper-ignore-next
204    /// Build the [`CharsetConverter`].
205    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
206    pub fn build(self) -> CharsetConverter {
207        self.builder.build()
208    }
209}