gio/auto/
socket_service.rs1use gio_sys;
6use glib;
7use glib::object::Cast;
8use glib::object::IsA;
9use glib::signal::connect_raw;
10use glib::signal::SignalHandlerId;
11use glib::translate::*;
12#[cfg(any(feature = "v2_46", feature = "dox"))]
13use glib::StaticType;
14#[cfg(any(feature = "v2_46", feature = "dox"))]
15use glib::Value;
16use glib_sys;
17use gobject_sys;
18use std::boxed::Box as Box_;
19use std::fmt;
20use std::mem::transmute;
21use SocketConnection;
22use SocketListener;
23
24glib_wrapper! {
25 pub struct SocketService(Object<gio_sys::GSocketService, gio_sys::GSocketServiceClass, SocketServiceClass>) @extends SocketListener;
26
27 match fn {
28 get_type => || gio_sys::g_socket_service_get_type(),
29 }
30}
31
32impl SocketService {
33 pub fn new() -> SocketService {
34 unsafe { from_glib_full(gio_sys::g_socket_service_new()) }
35 }
36}
37
38impl Default for SocketService {
39 fn default() -> Self {
40 Self::new()
41 }
42}
43
44pub const NONE_SOCKET_SERVICE: Option<&SocketService> = None;
45
46pub trait SocketServiceExt: 'static {
47 fn is_active(&self) -> bool;
48
49 fn start(&self);
50
51 fn stop(&self);
52
53 #[cfg(any(feature = "v2_46", feature = "dox"))]
54 fn get_property_active(&self) -> bool;
55
56 #[cfg(any(feature = "v2_46", feature = "dox"))]
57 fn set_property_active(&self, active: bool);
58
59 fn connect_incoming<F: Fn(&Self, &SocketConnection, Option<&glib::Object>) -> bool + 'static>(
60 &self,
61 f: F,
62 ) -> SignalHandlerId;
63
64 #[cfg(any(feature = "v2_46", feature = "dox"))]
65 fn connect_property_active_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
66}
67
68impl<O: IsA<SocketService>> SocketServiceExt for O {
69 fn is_active(&self) -> bool {
70 unsafe {
71 from_glib(gio_sys::g_socket_service_is_active(
72 self.as_ref().to_glib_none().0,
73 ))
74 }
75 }
76
77 fn start(&self) {
78 unsafe {
79 gio_sys::g_socket_service_start(self.as_ref().to_glib_none().0);
80 }
81 }
82
83 fn stop(&self) {
84 unsafe {
85 gio_sys::g_socket_service_stop(self.as_ref().to_glib_none().0);
86 }
87 }
88
89 #[cfg(any(feature = "v2_46", feature = "dox"))]
90 fn get_property_active(&self) -> bool {
91 unsafe {
92 let mut value = Value::from_type(<bool as StaticType>::static_type());
93 gobject_sys::g_object_get_property(
94 self.to_glib_none().0 as *mut gobject_sys::GObject,
95 b"active\0".as_ptr() as *const _,
96 value.to_glib_none_mut().0,
97 );
98 value.get().unwrap()
99 }
100 }
101
102 #[cfg(any(feature = "v2_46", feature = "dox"))]
103 fn set_property_active(&self, active: bool) {
104 unsafe {
105 gobject_sys::g_object_set_property(
106 self.to_glib_none().0 as *mut gobject_sys::GObject,
107 b"active\0".as_ptr() as *const _,
108 Value::from(&active).to_glib_none().0,
109 );
110 }
111 }
112
113 fn connect_incoming<
114 F: Fn(&Self, &SocketConnection, Option<&glib::Object>) -> bool + 'static,
115 >(
116 &self,
117 f: F,
118 ) -> SignalHandlerId {
119 unsafe extern "C" fn incoming_trampoline<
120 P,
121 F: Fn(&P, &SocketConnection, Option<&glib::Object>) -> bool + 'static,
122 >(
123 this: *mut gio_sys::GSocketService,
124 connection: *mut gio_sys::GSocketConnection,
125 source_object: *mut gobject_sys::GObject,
126 f: glib_sys::gpointer,
127 ) -> glib_sys::gboolean
128 where
129 P: IsA<SocketService>,
130 {
131 let f: &F = &*(f as *const F);
132 f(
133 &SocketService::from_glib_borrow(this).unsafe_cast(),
134 &from_glib_borrow(connection),
135 Option::<glib::Object>::from_glib_borrow(source_object).as_ref(),
136 )
137 .to_glib()
138 }
139 unsafe {
140 let f: Box_<F> = Box_::new(f);
141 connect_raw(
142 self.as_ptr() as *mut _,
143 b"incoming\0".as_ptr() as *const _,
144 Some(transmute(incoming_trampoline::<Self, F> as usize)),
145 Box_::into_raw(f),
146 )
147 }
148 }
149
150 #[cfg(any(feature = "v2_46", feature = "dox"))]
151 fn connect_property_active_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
152 unsafe extern "C" fn notify_active_trampoline<P, F: Fn(&P) + 'static>(
153 this: *mut gio_sys::GSocketService,
154 _param_spec: glib_sys::gpointer,
155 f: glib_sys::gpointer,
156 ) where
157 P: IsA<SocketService>,
158 {
159 let f: &F = &*(f as *const F);
160 f(&SocketService::from_glib_borrow(this).unsafe_cast())
161 }
162 unsafe {
163 let f: Box_<F> = Box_::new(f);
164 connect_raw(
165 self.as_ptr() as *mut _,
166 b"notify::active\0".as_ptr() as *const _,
167 Some(transmute(notify_active_trampoline::<Self, F> as usize)),
168 Box_::into_raw(f),
169 )
170 }
171 }
172}
173
174impl fmt::Display for SocketService {
175 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
176 write!(f, "SocketService")
177 }
178}