From cb934ff6dc548fa66e4ae62359a83cd3187d4888 Mon Sep 17 00:00:00 2001 From: Ayush Singh Date: Tue, 14 Dec 2021 18:15:49 +0530 Subject: [PATCH] Added KLocalizedString Wrapper. Only added applicationDomain related methods. --- src/klocalizedstring.rs | 32 ++++++++++++++++++++++++++++++++ src/lib.rs | 7 ++++++- tests/klocalizedcontext_tests.rs | 2 +- tests/klocalizedstring_tests.rs | 14 ++++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 src/klocalizedstring.rs create mode 100644 tests/klocalizedstring_tests.rs diff --git a/src/klocalizedstring.rs b/src/klocalizedstring.rs new file mode 100644 index 0000000..d0e7872 --- /dev/null +++ b/src/klocalizedstring.rs @@ -0,0 +1,32 @@ +use cpp::{cpp, cpp_class}; +use qmetaobject::QByteArray; +use std::ffi::CStr; + +cpp! {{ + #include + #include +}} + +cpp_class!( + /// Wrapper around [`KLocalizedString`][class] class + /// + /// [class]: https://api.kde.org/frameworks/ki18n/html/classKLocalizedString.html + pub unsafe struct KLocalizedString as "KLocalizedString" +); + +impl KLocalizedString { + /// Set Application Domain + pub fn set_application_domain(domain: &CStr) { + let domain_ptr = domain.as_ptr(); + cpp!(unsafe [domain_ptr as "char*"] { + KLocalizedString::setApplicationDomain(domain_ptr); + }); + } + + /// Get Application Domain + pub fn application_domain() -> QByteArray { + cpp!(unsafe [] -> QByteArray as "QByteArray" { + return KLocalizedString::applicationDomain(); + }) + } +} diff --git a/src/lib.rs b/src/lib.rs index fab00b0..46a5d3c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,4 +46,9 @@ //! ``` pub mod klocalizedcontext; -pub use klocalizedcontext::KLocalizedContext; +pub mod klocalizedstring; + +pub mod prelude { + pub use crate::klocalizedcontext::KLocalizedContext; + pub use crate::klocalizedstring::KLocalizedString; +} diff --git a/tests/klocalizedcontext_tests.rs b/tests/klocalizedcontext_tests.rs index 7c4ebec..0ff2cfc 100644 --- a/tests/klocalizedcontext_tests.rs +++ b/tests/klocalizedcontext_tests.rs @@ -1,4 +1,4 @@ -use ki18n_rs::KLocalizedContext; +use ki18n_rs::klocalizedcontext::KLocalizedContext; use qmetaobject::prelude::*; mod common; diff --git a/tests/klocalizedstring_tests.rs b/tests/klocalizedstring_tests.rs new file mode 100644 index 0000000..38d29dc --- /dev/null +++ b/tests/klocalizedstring_tests.rs @@ -0,0 +1,14 @@ +use ki18n_rs::klocalizedstring::KLocalizedString; +use std::ffi::CString; + +#[test] +fn application_domain() { + const APPLICATION_DOMAIN: &str = "KI18n"; + let domain = CString::new(APPLICATION_DOMAIN).unwrap(); + + KLocalizedString::set_application_domain(&domain); + let domain = KLocalizedString::application_domain(); + println!("{:#?}", domain); + + assert_eq!(domain.to_str().unwrap(), "KI18n"); +}