gio/auto/
charset_converter.rs1use gio_sys;
6use glib::object::Cast;
7use glib::object::IsA;
8use glib::signal::connect_raw;
9use glib::signal::SignalHandlerId;
10use glib::translate::*;
11use glib::GString;
12use glib::StaticType;
13use glib::ToValue;
14use glib::Value;
15use glib_sys;
16use gobject_sys;
17use std::boxed::Box as Box_;
18use std::fmt;
19use std::mem::transmute;
20use std::ptr;
21use Converter;
22use Error;
23
24glib_wrapper! {
25 pub struct CharsetConverter(Object<gio_sys::GCharsetConverter, gio_sys::GCharsetConverterClass, CharsetConverterClass>) @implements Converter;
26
27 match fn {
28 get_type => || gio_sys::g_charset_converter_get_type(),
29 }
30}
31
32impl CharsetConverter {
33 pub fn new(to_charset: &str, from_charset: &str) -> Result<CharsetConverter, Error> {
34 unsafe {
35 let mut error = ptr::null_mut();
36 let ret = gio_sys::g_charset_converter_new(
37 to_charset.to_glib_none().0,
38 from_charset.to_glib_none().0,
39 &mut error,
40 );
41 if error.is_null() {
42 Ok(from_glib_full(ret))
43 } else {
44 Err(from_glib_full(error))
45 }
46 }
47 }
48}
49
50pub struct CharsetConverterBuilder {
51 from_charset: Option<String>,
52 to_charset: Option<String>,
53 use_fallback: Option<bool>,
54}
55
56impl CharsetConverterBuilder {
57 pub fn new() -> Self {
58 Self {
59 from_charset: None,
60 to_charset: None,
61 use_fallback: None,
62 }
63 }
64
65 pub fn build(self) -> CharsetConverter {
66 let mut properties: Vec<(&str, &dyn ToValue)> = vec![];
67 if let Some(ref from_charset) = self.from_charset {
68 properties.push(("from-charset", from_charset));
69 }
70 if let Some(ref to_charset) = self.to_charset {
71 properties.push(("to-charset", to_charset));
72 }
73 if let Some(ref use_fallback) = self.use_fallback {
74 properties.push(("use-fallback", use_fallback));
75 }
76 glib::Object::new(CharsetConverter::static_type(), &properties)
77 .expect("object new")
78 .downcast()
79 .expect("downcast")
80 }
81
82 pub fn from_charset(mut self, from_charset: &str) -> Self {
83 self.from_charset = Some(from_charset.to_string());
84 self
85 }
86
87 pub fn to_charset(mut self, to_charset: &str) -> Self {
88 self.to_charset = Some(to_charset.to_string());
89 self
90 }
91
92 pub fn use_fallback(mut self, use_fallback: bool) -> Self {
93 self.use_fallback = Some(use_fallback);
94 self
95 }
96}
97
98pub const NONE_CHARSET_CONVERTER: Option<&CharsetConverter> = None;
99
100pub trait CharsetConverterExt: 'static {
101 fn get_num_fallbacks(&self) -> u32;
102
103 fn get_use_fallback(&self) -> bool;
104
105 fn set_use_fallback(&self, use_fallback: bool);
106
107 fn get_property_from_charset(&self) -> Option<GString>;
108
109 fn get_property_to_charset(&self) -> Option<GString>;
110
111 fn connect_property_use_fallback_notify<F: Fn(&Self) + 'static>(&self, f: F)
112 -> SignalHandlerId;
113}
114
115impl<O: IsA<CharsetConverter>> CharsetConverterExt for O {
116 fn get_num_fallbacks(&self) -> u32 {
117 unsafe { gio_sys::g_charset_converter_get_num_fallbacks(self.as_ref().to_glib_none().0) }
118 }
119
120 fn get_use_fallback(&self) -> bool {
121 unsafe {
122 from_glib(gio_sys::g_charset_converter_get_use_fallback(
123 self.as_ref().to_glib_none().0,
124 ))
125 }
126 }
127
128 fn set_use_fallback(&self, use_fallback: bool) {
129 unsafe {
130 gio_sys::g_charset_converter_set_use_fallback(
131 self.as_ref().to_glib_none().0,
132 use_fallback.to_glib(),
133 );
134 }
135 }
136
137 fn get_property_from_charset(&self) -> Option<GString> {
138 unsafe {
139 let mut value = Value::from_type(<GString as StaticType>::static_type());
140 gobject_sys::g_object_get_property(
141 self.to_glib_none().0 as *mut gobject_sys::GObject,
142 b"from-charset\0".as_ptr() as *const _,
143 value.to_glib_none_mut().0,
144 );
145 value.get()
146 }
147 }
148
149 fn get_property_to_charset(&self) -> Option<GString> {
150 unsafe {
151 let mut value = Value::from_type(<GString as StaticType>::static_type());
152 gobject_sys::g_object_get_property(
153 self.to_glib_none().0 as *mut gobject_sys::GObject,
154 b"to-charset\0".as_ptr() as *const _,
155 value.to_glib_none_mut().0,
156 );
157 value.get()
158 }
159 }
160
161 fn connect_property_use_fallback_notify<F: Fn(&Self) + 'static>(
162 &self,
163 f: F,
164 ) -> SignalHandlerId {
165 unsafe extern "C" fn notify_use_fallback_trampoline<P, F: Fn(&P) + 'static>(
166 this: *mut gio_sys::GCharsetConverter,
167 _param_spec: glib_sys::gpointer,
168 f: glib_sys::gpointer,
169 ) where
170 P: IsA<CharsetConverter>,
171 {
172 let f: &F = &*(f as *const F);
173 f(&CharsetConverter::from_glib_borrow(this).unsafe_cast())
174 }
175 unsafe {
176 let f: Box_<F> = Box_::new(f);
177 connect_raw(
178 self.as_ptr() as *mut _,
179 b"notify::use-fallback\0".as_ptr() as *const _,
180 Some(transmute(
181 notify_use_fallback_trampoline::<Self, F> as usize,
182 )),
183 Box_::into_raw(f),
184 )
185 }
186 }
187}
188
189impl fmt::Display for CharsetConverter {
190 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
191 write!(f, "CharsetConverter")
192 }
193}