1use gio_sys;
6use glib;
7use glib::object::Cast;
8use glib::object::IsA;
9use glib::signal::connect_raw;
10use glib::signal::SignalHandlerId;
11use glib::translate::*;
12use glib::GString;
13use glib::StaticType;
14use glib::Value;
15use glib_sys;
16use gobject_sys;
17use libc;
18use signal::Inhibit;
19use std::boxed::Box as Box_;
20use std::fmt;
21use std::mem::transmute;
22use Action;
23use SettingsBackend;
24use SettingsBindFlags;
25use SettingsSchema;
26
27glib_wrapper! {
28 pub struct Settings(Object<gio_sys::GSettings, gio_sys::GSettingsClass, SettingsClass>);
29
30 match fn {
31 get_type => || gio_sys::g_settings_get_type(),
32 }
33}
34
35impl Settings {
36 pub fn new(schema_id: &str) -> Settings {
37 unsafe { from_glib_full(gio_sys::g_settings_new(schema_id.to_glib_none().0)) }
38 }
39
40 pub fn new_full<P: IsA<SettingsBackend>>(
41 schema: &SettingsSchema,
42 backend: Option<&P>,
43 path: Option<&str>,
44 ) -> Settings {
45 unsafe {
46 from_glib_full(gio_sys::g_settings_new_full(
47 schema.to_glib_none().0,
48 backend.map(|p| p.as_ref()).to_glib_none().0,
49 path.to_glib_none().0,
50 ))
51 }
52 }
53
54 pub fn new_with_backend<P: IsA<SettingsBackend>>(schema_id: &str, backend: &P) -> Settings {
55 unsafe {
56 from_glib_full(gio_sys::g_settings_new_with_backend(
57 schema_id.to_glib_none().0,
58 backend.as_ref().to_glib_none().0,
59 ))
60 }
61 }
62
63 pub fn new_with_backend_and_path<P: IsA<SettingsBackend>>(
64 schema_id: &str,
65 backend: &P,
66 path: &str,
67 ) -> Settings {
68 unsafe {
69 from_glib_full(gio_sys::g_settings_new_with_backend_and_path(
70 schema_id.to_glib_none().0,
71 backend.as_ref().to_glib_none().0,
72 path.to_glib_none().0,
73 ))
74 }
75 }
76
77 pub fn new_with_path(schema_id: &str, path: &str) -> Settings {
78 unsafe {
79 from_glib_full(gio_sys::g_settings_new_with_path(
80 schema_id.to_glib_none().0,
81 path.to_glib_none().0,
82 ))
83 }
84 }
85
86 pub fn sync() {
87 unsafe {
88 gio_sys::g_settings_sync();
89 }
90 }
91
92 pub fn unbind<P: IsA<glib::Object>>(object: &P, property: &str) {
93 unsafe {
94 gio_sys::g_settings_unbind(object.as_ref().to_glib_none().0, property.to_glib_none().0);
95 }
96 }
97}
98
99pub const NONE_SETTINGS: Option<&Settings> = None;
100
101pub trait SettingsExt: 'static {
102 fn apply(&self);
103
104 fn bind<P: IsA<glib::Object>>(
105 &self,
106 key: &str,
107 object: &P,
108 property: &str,
109 flags: SettingsBindFlags,
110 );
111
112 fn bind_writable<P: IsA<glib::Object>>(
115 &self,
116 key: &str,
117 object: &P,
118 property: &str,
119 inverted: bool,
120 );
121
122 fn create_action(&self, key: &str) -> Option<Action>;
123
124 fn delay(&self);
125
126 fn get_boolean(&self, key: &str) -> bool;
129
130 fn get_child(&self, name: &str) -> Option<Settings>;
131
132 fn get_default_value(&self, key: &str) -> Option<glib::Variant>;
133
134 fn get_double(&self, key: &str) -> f64;
135
136 fn get_enum(&self, key: &str) -> i32;
137
138 fn get_flags(&self, key: &str) -> u32;
139
140 fn get_has_unapplied(&self) -> bool;
141
142 fn get_int(&self, key: &str) -> i32;
143
144 #[cfg(any(feature = "v2_50", feature = "dox"))]
145 fn get_int64(&self, key: &str) -> i64;
146
147 fn get_string(&self, key: &str) -> Option<GString>;
150
151 fn get_strv(&self, key: &str) -> Vec<GString>;
152
153 fn get_uint(&self, key: &str) -> u32;
154
155 #[cfg(any(feature = "v2_50", feature = "dox"))]
156 fn get_uint64(&self, key: &str) -> u64;
157
158 fn get_user_value(&self, key: &str) -> Option<glib::Variant>;
159
160 fn get_value(&self, key: &str) -> Option<glib::Variant>;
161
162 fn is_writable(&self, name: &str) -> bool;
163
164 fn list_children(&self) -> Vec<GString>;
165
166 fn list_keys(&self) -> Vec<GString>;
167
168 fn reset(&self, key: &str);
169
170 fn revert(&self);
171
172 fn set_boolean(&self, key: &str, value: bool) -> bool;
175
176 fn set_double(&self, key: &str, value: f64) -> bool;
177
178 fn set_enum(&self, key: &str, value: i32) -> bool;
179
180 fn set_flags(&self, key: &str, value: u32) -> bool;
181
182 fn set_int(&self, key: &str, value: i32) -> bool;
183
184 #[cfg(any(feature = "v2_50", feature = "dox"))]
185 fn set_int64(&self, key: &str, value: i64) -> bool;
186
187 fn set_string(&self, key: &str, value: &str) -> bool;
188
189 fn set_strv(&self, key: &str, value: &[&str]) -> bool;
190
191 fn set_uint(&self, key: &str, value: u32) -> bool;
192
193 #[cfg(any(feature = "v2_50", feature = "dox"))]
194 fn set_uint64(&self, key: &str, value: u64) -> bool;
195
196 fn set_value(&self, key: &str, value: &glib::Variant) -> bool;
197
198 fn get_property_backend(&self) -> Option<SettingsBackend>;
199
200 fn get_property_delay_apply(&self) -> bool;
201
202 fn get_property_path(&self) -> Option<GString>;
203
204 fn get_property_schema_id(&self) -> Option<GString>;
205
206 fn get_property_settings_schema(&self) -> Option<SettingsSchema>;
207
208 fn connect_changed<F: Fn(&Self, &str) + 'static>(&self, f: F) -> SignalHandlerId;
211
212 fn connect_writable_change_event<F: Fn(&Self, u32) -> Inhibit + 'static>(
213 &self,
214 f: F,
215 ) -> SignalHandlerId;
216
217 fn connect_writable_changed<F: Fn(&Self, &str) + 'static>(&self, f: F) -> SignalHandlerId;
218
219 fn connect_property_delay_apply_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
220
221 fn connect_property_has_unapplied_notify<F: Fn(&Self) + 'static>(
222 &self,
223 f: F,
224 ) -> SignalHandlerId;
225}
226
227impl<O: IsA<Settings>> SettingsExt for O {
228 fn apply(&self) {
229 unsafe {
230 gio_sys::g_settings_apply(self.as_ref().to_glib_none().0);
231 }
232 }
233
234 fn bind<P: IsA<glib::Object>>(
235 &self,
236 key: &str,
237 object: &P,
238 property: &str,
239 flags: SettingsBindFlags,
240 ) {
241 unsafe {
242 gio_sys::g_settings_bind(
243 self.as_ref().to_glib_none().0,
244 key.to_glib_none().0,
245 object.as_ref().to_glib_none().0,
246 property.to_glib_none().0,
247 flags.to_glib(),
248 );
249 }
250 }
251
252 fn bind_writable<P: IsA<glib::Object>>(
257 &self,
258 key: &str,
259 object: &P,
260 property: &str,
261 inverted: bool,
262 ) {
263 unsafe {
264 gio_sys::g_settings_bind_writable(
265 self.as_ref().to_glib_none().0,
266 key.to_glib_none().0,
267 object.as_ref().to_glib_none().0,
268 property.to_glib_none().0,
269 inverted.to_glib(),
270 );
271 }
272 }
273
274 fn create_action(&self, key: &str) -> Option<Action> {
275 unsafe {
276 from_glib_full(gio_sys::g_settings_create_action(
277 self.as_ref().to_glib_none().0,
278 key.to_glib_none().0,
279 ))
280 }
281 }
282
283 fn delay(&self) {
284 unsafe {
285 gio_sys::g_settings_delay(self.as_ref().to_glib_none().0);
286 }
287 }
288
289 fn get_boolean(&self, key: &str) -> bool {
294 unsafe {
295 from_glib(gio_sys::g_settings_get_boolean(
296 self.as_ref().to_glib_none().0,
297 key.to_glib_none().0,
298 ))
299 }
300 }
301
302 fn get_child(&self, name: &str) -> Option<Settings> {
303 unsafe {
304 from_glib_full(gio_sys::g_settings_get_child(
305 self.as_ref().to_glib_none().0,
306 name.to_glib_none().0,
307 ))
308 }
309 }
310
311 fn get_default_value(&self, key: &str) -> Option<glib::Variant> {
312 unsafe {
313 from_glib_full(gio_sys::g_settings_get_default_value(
314 self.as_ref().to_glib_none().0,
315 key.to_glib_none().0,
316 ))
317 }
318 }
319
320 fn get_double(&self, key: &str) -> f64 {
321 unsafe {
322 gio_sys::g_settings_get_double(self.as_ref().to_glib_none().0, key.to_glib_none().0)
323 }
324 }
325
326 fn get_enum(&self, key: &str) -> i32 {
327 unsafe {
328 gio_sys::g_settings_get_enum(self.as_ref().to_glib_none().0, key.to_glib_none().0)
329 }
330 }
331
332 fn get_flags(&self, key: &str) -> u32 {
333 unsafe {
334 gio_sys::g_settings_get_flags(self.as_ref().to_glib_none().0, key.to_glib_none().0)
335 }
336 }
337
338 fn get_has_unapplied(&self) -> bool {
339 unsafe {
340 from_glib(gio_sys::g_settings_get_has_unapplied(
341 self.as_ref().to_glib_none().0,
342 ))
343 }
344 }
345
346 fn get_int(&self, key: &str) -> i32 {
347 unsafe { gio_sys::g_settings_get_int(self.as_ref().to_glib_none().0, key.to_glib_none().0) }
348 }
349
350 #[cfg(any(feature = "v2_50", feature = "dox"))]
351 fn get_int64(&self, key: &str) -> i64 {
352 unsafe {
353 gio_sys::g_settings_get_int64(self.as_ref().to_glib_none().0, key.to_glib_none().0)
354 }
355 }
356
357 fn get_string(&self, key: &str) -> Option<GString> {
362 unsafe {
363 from_glib_full(gio_sys::g_settings_get_string(
364 self.as_ref().to_glib_none().0,
365 key.to_glib_none().0,
366 ))
367 }
368 }
369
370 fn get_strv(&self, key: &str) -> Vec<GString> {
371 unsafe {
372 FromGlibPtrContainer::from_glib_full(gio_sys::g_settings_get_strv(
373 self.as_ref().to_glib_none().0,
374 key.to_glib_none().0,
375 ))
376 }
377 }
378
379 fn get_uint(&self, key: &str) -> u32 {
380 unsafe {
381 gio_sys::g_settings_get_uint(self.as_ref().to_glib_none().0, key.to_glib_none().0)
382 }
383 }
384
385 #[cfg(any(feature = "v2_50", feature = "dox"))]
386 fn get_uint64(&self, key: &str) -> u64 {
387 unsafe {
388 gio_sys::g_settings_get_uint64(self.as_ref().to_glib_none().0, key.to_glib_none().0)
389 }
390 }
391
392 fn get_user_value(&self, key: &str) -> Option<glib::Variant> {
393 unsafe {
394 from_glib_full(gio_sys::g_settings_get_user_value(
395 self.as_ref().to_glib_none().0,
396 key.to_glib_none().0,
397 ))
398 }
399 }
400
401 fn get_value(&self, key: &str) -> Option<glib::Variant> {
402 unsafe {
403 from_glib_full(gio_sys::g_settings_get_value(
404 self.as_ref().to_glib_none().0,
405 key.to_glib_none().0,
406 ))
407 }
408 }
409
410 fn is_writable(&self, name: &str) -> bool {
411 unsafe {
412 from_glib(gio_sys::g_settings_is_writable(
413 self.as_ref().to_glib_none().0,
414 name.to_glib_none().0,
415 ))
416 }
417 }
418
419 fn list_children(&self) -> Vec<GString> {
420 unsafe {
421 FromGlibPtrContainer::from_glib_full(gio_sys::g_settings_list_children(
422 self.as_ref().to_glib_none().0,
423 ))
424 }
425 }
426
427 fn list_keys(&self) -> Vec<GString> {
428 unsafe {
429 FromGlibPtrContainer::from_glib_full(gio_sys::g_settings_list_keys(
430 self.as_ref().to_glib_none().0,
431 ))
432 }
433 }
434
435 fn reset(&self, key: &str) {
436 unsafe {
437 gio_sys::g_settings_reset(self.as_ref().to_glib_none().0, key.to_glib_none().0);
438 }
439 }
440
441 fn revert(&self) {
442 unsafe {
443 gio_sys::g_settings_revert(self.as_ref().to_glib_none().0);
444 }
445 }
446
447 fn set_boolean(&self, key: &str, value: bool) -> bool {
452 unsafe {
453 from_glib(gio_sys::g_settings_set_boolean(
454 self.as_ref().to_glib_none().0,
455 key.to_glib_none().0,
456 value.to_glib(),
457 ))
458 }
459 }
460
461 fn set_double(&self, key: &str, value: f64) -> bool {
462 unsafe {
463 from_glib(gio_sys::g_settings_set_double(
464 self.as_ref().to_glib_none().0,
465 key.to_glib_none().0,
466 value,
467 ))
468 }
469 }
470
471 fn set_enum(&self, key: &str, value: i32) -> bool {
472 unsafe {
473 from_glib(gio_sys::g_settings_set_enum(
474 self.as_ref().to_glib_none().0,
475 key.to_glib_none().0,
476 value,
477 ))
478 }
479 }
480
481 fn set_flags(&self, key: &str, value: u32) -> bool {
482 unsafe {
483 from_glib(gio_sys::g_settings_set_flags(
484 self.as_ref().to_glib_none().0,
485 key.to_glib_none().0,
486 value,
487 ))
488 }
489 }
490
491 fn set_int(&self, key: &str, value: i32) -> bool {
492 unsafe {
493 from_glib(gio_sys::g_settings_set_int(
494 self.as_ref().to_glib_none().0,
495 key.to_glib_none().0,
496 value,
497 ))
498 }
499 }
500
501 #[cfg(any(feature = "v2_50", feature = "dox"))]
502 fn set_int64(&self, key: &str, value: i64) -> bool {
503 unsafe {
504 from_glib(gio_sys::g_settings_set_int64(
505 self.as_ref().to_glib_none().0,
506 key.to_glib_none().0,
507 value,
508 ))
509 }
510 }
511
512 fn set_string(&self, key: &str, value: &str) -> bool {
513 unsafe {
514 from_glib(gio_sys::g_settings_set_string(
515 self.as_ref().to_glib_none().0,
516 key.to_glib_none().0,
517 value.to_glib_none().0,
518 ))
519 }
520 }
521
522 fn set_strv(&self, key: &str, value: &[&str]) -> bool {
523 unsafe {
524 from_glib(gio_sys::g_settings_set_strv(
525 self.as_ref().to_glib_none().0,
526 key.to_glib_none().0,
527 value.to_glib_none().0,
528 ))
529 }
530 }
531
532 fn set_uint(&self, key: &str, value: u32) -> bool {
533 unsafe {
534 from_glib(gio_sys::g_settings_set_uint(
535 self.as_ref().to_glib_none().0,
536 key.to_glib_none().0,
537 value,
538 ))
539 }
540 }
541
542 #[cfg(any(feature = "v2_50", feature = "dox"))]
543 fn set_uint64(&self, key: &str, value: u64) -> bool {
544 unsafe {
545 from_glib(gio_sys::g_settings_set_uint64(
546 self.as_ref().to_glib_none().0,
547 key.to_glib_none().0,
548 value,
549 ))
550 }
551 }
552
553 fn set_value(&self, key: &str, value: &glib::Variant) -> bool {
554 unsafe {
555 from_glib(gio_sys::g_settings_set_value(
556 self.as_ref().to_glib_none().0,
557 key.to_glib_none().0,
558 value.to_glib_none().0,
559 ))
560 }
561 }
562
563 fn get_property_backend(&self) -> Option<SettingsBackend> {
564 unsafe {
565 let mut value = Value::from_type(<SettingsBackend as StaticType>::static_type());
566 gobject_sys::g_object_get_property(
567 self.to_glib_none().0 as *mut gobject_sys::GObject,
568 b"backend\0".as_ptr() as *const _,
569 value.to_glib_none_mut().0,
570 );
571 value.get()
572 }
573 }
574
575 fn get_property_delay_apply(&self) -> bool {
576 unsafe {
577 let mut value = Value::from_type(<bool as StaticType>::static_type());
578 gobject_sys::g_object_get_property(
579 self.to_glib_none().0 as *mut gobject_sys::GObject,
580 b"delay-apply\0".as_ptr() as *const _,
581 value.to_glib_none_mut().0,
582 );
583 value.get().unwrap()
584 }
585 }
586
587 fn get_property_path(&self) -> Option<GString> {
588 unsafe {
589 let mut value = Value::from_type(<GString as StaticType>::static_type());
590 gobject_sys::g_object_get_property(
591 self.to_glib_none().0 as *mut gobject_sys::GObject,
592 b"path\0".as_ptr() as *const _,
593 value.to_glib_none_mut().0,
594 );
595 value.get()
596 }
597 }
598
599 fn get_property_schema_id(&self) -> Option<GString> {
600 unsafe {
601 let mut value = Value::from_type(<GString as StaticType>::static_type());
602 gobject_sys::g_object_get_property(
603 self.to_glib_none().0 as *mut gobject_sys::GObject,
604 b"schema-id\0".as_ptr() as *const _,
605 value.to_glib_none_mut().0,
606 );
607 value.get()
608 }
609 }
610
611 fn get_property_settings_schema(&self) -> Option<SettingsSchema> {
612 unsafe {
613 let mut value = Value::from_type(<SettingsSchema as StaticType>::static_type());
614 gobject_sys::g_object_get_property(
615 self.to_glib_none().0 as *mut gobject_sys::GObject,
616 b"settings-schema\0".as_ptr() as *const _,
617 value.to_glib_none_mut().0,
618 );
619 value.get()
620 }
621 }
622
623 fn connect_changed<F: Fn(&Self, &str) + 'static>(&self, f: F) -> SignalHandlerId {
628 unsafe extern "C" fn changed_trampoline<P, F: Fn(&P, &str) + 'static>(
629 this: *mut gio_sys::GSettings,
630 key: *mut libc::c_char,
631 f: glib_sys::gpointer,
632 ) where
633 P: IsA<Settings>,
634 {
635 let f: &F = &*(f as *const F);
636 f(
637 &Settings::from_glib_borrow(this).unsafe_cast(),
638 &GString::from_glib_borrow(key),
639 )
640 }
641 unsafe {
642 let f: Box_<F> = Box_::new(f);
643 connect_raw(
644 self.as_ptr() as *mut _,
645 b"changed\0".as_ptr() as *const _,
646 Some(transmute(changed_trampoline::<Self, F> as usize)),
647 Box_::into_raw(f),
648 )
649 }
650 }
651
652 fn connect_writable_change_event<F: Fn(&Self, u32) -> Inhibit + 'static>(
653 &self,
654 f: F,
655 ) -> SignalHandlerId {
656 unsafe extern "C" fn writable_change_event_trampoline<
657 P,
658 F: Fn(&P, u32) -> Inhibit + 'static,
659 >(
660 this: *mut gio_sys::GSettings,
661 key: libc::c_uint,
662 f: glib_sys::gpointer,
663 ) -> glib_sys::gboolean
664 where
665 P: IsA<Settings>,
666 {
667 let f: &F = &*(f as *const F);
668 f(&Settings::from_glib_borrow(this).unsafe_cast(), key).to_glib()
669 }
670 unsafe {
671 let f: Box_<F> = Box_::new(f);
672 connect_raw(
673 self.as_ptr() as *mut _,
674 b"writable-change-event\0".as_ptr() as *const _,
675 Some(transmute(
676 writable_change_event_trampoline::<Self, F> as usize,
677 )),
678 Box_::into_raw(f),
679 )
680 }
681 }
682
683 fn connect_writable_changed<F: Fn(&Self, &str) + 'static>(&self, f: F) -> SignalHandlerId {
684 unsafe extern "C" fn writable_changed_trampoline<P, F: Fn(&P, &str) + 'static>(
685 this: *mut gio_sys::GSettings,
686 key: *mut libc::c_char,
687 f: glib_sys::gpointer,
688 ) where
689 P: IsA<Settings>,
690 {
691 let f: &F = &*(f as *const F);
692 f(
693 &Settings::from_glib_borrow(this).unsafe_cast(),
694 &GString::from_glib_borrow(key),
695 )
696 }
697 unsafe {
698 let f: Box_<F> = Box_::new(f);
699 connect_raw(
700 self.as_ptr() as *mut _,
701 b"writable-changed\0".as_ptr() as *const _,
702 Some(transmute(writable_changed_trampoline::<Self, F> as usize)),
703 Box_::into_raw(f),
704 )
705 }
706 }
707
708 fn connect_property_delay_apply_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
709 unsafe extern "C" fn notify_delay_apply_trampoline<P, F: Fn(&P) + 'static>(
710 this: *mut gio_sys::GSettings,
711 _param_spec: glib_sys::gpointer,
712 f: glib_sys::gpointer,
713 ) where
714 P: IsA<Settings>,
715 {
716 let f: &F = &*(f as *const F);
717 f(&Settings::from_glib_borrow(this).unsafe_cast())
718 }
719 unsafe {
720 let f: Box_<F> = Box_::new(f);
721 connect_raw(
722 self.as_ptr() as *mut _,
723 b"notify::delay-apply\0".as_ptr() as *const _,
724 Some(transmute(notify_delay_apply_trampoline::<Self, F> as usize)),
725 Box_::into_raw(f),
726 )
727 }
728 }
729
730 fn connect_property_has_unapplied_notify<F: Fn(&Self) + 'static>(
731 &self,
732 f: F,
733 ) -> SignalHandlerId {
734 unsafe extern "C" fn notify_has_unapplied_trampoline<P, F: Fn(&P) + 'static>(
735 this: *mut gio_sys::GSettings,
736 _param_spec: glib_sys::gpointer,
737 f: glib_sys::gpointer,
738 ) where
739 P: IsA<Settings>,
740 {
741 let f: &F = &*(f as *const F);
742 f(&Settings::from_glib_borrow(this).unsafe_cast())
743 }
744 unsafe {
745 let f: Box_<F> = Box_::new(f);
746 connect_raw(
747 self.as_ptr() as *mut _,
748 b"notify::has-unapplied\0".as_ptr() as *const _,
749 Some(transmute(
750 notify_has_unapplied_trampoline::<Self, F> as usize,
751 )),
752 Box_::into_raw(f),
753 )
754 }
755 }
756}
757
758impl fmt::Display for Settings {
759 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
760 write!(f, "Settings")
761 }
762}