summaryrefslogtreecommitdiff
path: root/qml_plugin/src/qml_interface/network.rs
blob: 7ece121d08ebbe30ffd13028591e80f9a8a7e437 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use cxx_qt::{CxxQtType, Threading};
use cxx_qt_lib::{QString, QVariant};
use std::pin::Pin;
use unwrap_print::PrintableResult;
use zbus::zvariant::NoneValue;

use crate::{ExclusiveExecutor, services::network};

#[cxx_qt::bridge]
mod qobject {
    extern "C++" {
        include!("cxx-qt-lib/qvariant.h");
        type QVariant = cxx_qt_lib::QVariant;
    }

    extern "RustQt" {
        #[qobject]
        #[qml_element]
        #[qml_singleton]
        #[qproperty(
            QVariant, result
        )]
        type Network = super::NetworkRs;

        #[qinvokable]
        fn query_primary_device(self: Pin<&mut Network>);
    }

    impl cxx_qt::Threading for Network {}
}

#[derive(Default)]
pub struct NetworkRs {
    result: QVariant,

    executor: ExclusiveExecutor,
}

impl qobject::Network {
    fn query_primary_device(self: Pin<&mut Self>) {
        let qt_thread = self.qt_thread();
        self.rust_mut().executor.spawn(
            async move {
                let result = match network::get().await {
                    Ok(o) => {
                        let r: QString = o.into();
                        QVariant::from(&r)
                    }
                    Err(_) => QVariant::null_value(),
                };
                _ = qt_thread.queue(|mut qo| qo.as_mut().set_result(result)).unwrap_print();
            },
        );
    }
}