Skip to content

Commit 505b98b

Browse files
committed
Make signal class handlers overwritable
1 parent c76c7a1 commit 505b98b

File tree

1 file changed

+85
-4
lines changed

1 file changed

+85
-4
lines changed

gio/src/subclass/dbus_proxy.rs

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,103 @@
22

33
#![deny(unsafe_op_in_unsafe_fn)]
44

5-
use glib::{prelude::*, subclass::prelude::*};
5+
use glib::{prelude::*, subclass::prelude::*, translate::*, GString, StrVRef};
66

7-
use crate::subclass::prelude::DBusInterfaceImpl;
8-
use crate::DBusProxy;
7+
use crate::{ffi, subclass::prelude::DBusInterfaceImpl, DBusProxy};
98

109
pub trait DBusProxyImpl:
1110
ObjectImpl + DBusInterfaceImpl + ObjectSubclass<Type: IsA<DBusProxy>>
1211
{
12+
fn g_properties_changed(
13+
&self,
14+
changed_properties: &glib::Variant,
15+
invalidated_properties: &StrVRef,
16+
) {
17+
self.parent_g_properties_changed(changed_properties, invalidated_properties);
18+
}
19+
20+
fn g_signal(&self, sender_name: Option<&str>, signal_name: &str, parameters: &glib::Variant) {
21+
self.parent_g_signal(sender_name, signal_name, parameters);
22+
}
1323
}
1424

15-
pub trait DBusProxyImplExt: DBusProxyImpl {}
25+
pub trait DBusProxyImplExt: DBusProxyImpl {
26+
fn parent_g_properties_changed(
27+
&self,
28+
changed_properties: &glib::Variant,
29+
invalidated_properties: &StrVRef,
30+
) {
31+
unsafe {
32+
let data = Self::type_data();
33+
let parent_class = data.as_ref().parent_class() as *const ffi::GDBusProxyClass;
34+
35+
if let Some(f) = (*parent_class).g_properties_changed {
36+
f(
37+
self.obj().unsafe_cast_ref::<DBusProxy>().to_glib_none().0,
38+
changed_properties.to_glib_none().0,
39+
invalidated_properties.to_glib_none().0,
40+
);
41+
}
42+
}
43+
}
44+
45+
fn parent_g_signal(
46+
&self,
47+
sender_name: Option<&str>,
48+
signal_name: &str,
49+
parameters: &glib::Variant,
50+
) {
51+
unsafe {
52+
let data = Self::type_data();
53+
let parent_class = data.as_ref().parent_class() as *const ffi::GDBusProxyClass;
54+
55+
if let Some(f) = (*parent_class).g_signal {
56+
f(
57+
self.obj().unsafe_cast_ref::<DBusProxy>().to_glib_none().0,
58+
sender_name.to_glib_none().0,
59+
signal_name.to_glib_none().0,
60+
parameters.to_glib_none().0,
61+
);
62+
}
63+
}
64+
}
65+
}
1666

1767
impl<T: DBusProxyImpl> DBusProxyImplExt for T {}
1868

1969
unsafe impl<T: DBusProxyImpl> IsSubclassable<T> for DBusProxy {
2070
fn class_init(class: &mut glib::Class<Self>) {
2171
Self::parent_class_init::<T>(class);
72+
let class = class.as_mut();
73+
class.g_properties_changed = Some(g_properties_changed::<T>);
74+
class.g_signal = Some(g_signal::<T>);
2275
}
2376
}
77+
78+
unsafe extern "C" fn g_properties_changed<T: DBusProxyImpl>(
79+
proxy: *mut ffi::GDBusProxy,
80+
changed_properties: *mut glib::ffi::GVariant,
81+
invalidated_properties: *const *const libc::c_char,
82+
) {
83+
let instance = unsafe { &*(proxy as *mut T::Instance) };
84+
let imp = instance.imp();
85+
86+
let changed_properties = unsafe { from_glib_borrow(changed_properties) };
87+
let invalidated_properties = unsafe { StrVRef::from_glib_borrow(invalidated_properties) };
88+
imp.g_properties_changed(&changed_properties, invalidated_properties);
89+
}
90+
91+
unsafe extern "C" fn g_signal<T: DBusProxyImpl>(
92+
proxy: *mut ffi::GDBusProxy,
93+
sender_name: *const libc::c_char,
94+
signal_name: *const libc::c_char,
95+
parameters: *mut glib::ffi::GVariant,
96+
) {
97+
let instance = unsafe { &*(proxy as *mut T::Instance) };
98+
let imp = instance.imp();
99+
100+
let sender_name = unsafe { Option::<GString>::from_glib_borrow(sender_name) };
101+
let signal_name = unsafe { GString::from_glib_borrow(signal_name) };
102+
let parameters = unsafe { from_glib_borrow(parameters) };
103+
imp.g_signal(sender_name.as_ref().as_deref(), &signal_name, &parameters);
104+
}

0 commit comments

Comments
 (0)