gnome-dbus-api 0.1.6

A simple API to interact with GNOME DBus and Gsettings
Documentation
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#![feature(ascii_char)]

pub mod easy_gnome {
    use std::collections::HashMap;
    use zbus::{dbus_proxy, Result};

    #[dbus_proxy(
        interface = "org.freedesktop.login1.Manager",
        default_service = "org.freedesktop.login1",
        default_path = "/org/freedesktop/login1"
    )]
    trait PowerManagement {
        async fn Suspend(&self, arg: bool) -> Result<()>;
        async fn PowerOff(&self, arg: bool) -> Result<()>;
        async fn Reboot(&self, arg: bool) -> Result<()>;
    }

    // Shell extensions
    #[dbus_proxy(
        interface = "org.gnome.Shell.Extensions",
        default_service = "org.gnome.Shell.Extensions",
        default_path = "/org/gnome/Shell/Extensions"
    )]
    trait Extensions {
        async fn ListExtensions(
            &self,
        ) -> Result<HashMap<String, HashMap<String, zvariant::OwnedValue>>>;
        async fn LaunchExtensionPrefs(&self, uuid: String) -> Result<()>;
        async fn EnableExtension(&self, uuid: String) -> Result<bool>;
        async fn DisableExtension(&self, uuid: String) -> Result<bool>;
        async fn UninstallExtension(&self, uuid: String) -> Result<bool>;
    }

    /// # Extension states
    /// ```xml
    /// <member>1: ENABLED</member>
    /// <member>2: DISABLED</member>
    /// <member>3: ERROR</member>
    /// <member>4: OUT_OF_DATE</member>
    /// <member>5: DOWNLOADING</member>
    /// <member>6: INITIALIZED</member>
    /// <member>99: UNINSTALLED</member>
    /// ```
    /// https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/92d3c6e051958b31151bf9538205a71cab6f70d7/data/dbus-interfaces/org.gnome.Shell.Extensions.xml#L73
    #[derive(Debug)]
    enum ListExtensionState {
        ENABLED = 1,
        DISABLED = 2,
        ERROR = 3,
        OUT_OF_DATE = 4,
        DOWNLOADING = 5,
        INITIALIZED = 6,
        UNINSTALLED = 99,
    }
    #[derive(Debug)]
    struct ListExtension {
        uuid: String,
        name: String,
        description: String,
        state: ListExtensionState,
        version: String,
        url: String,
    }
    impl ExtensionsProxy<'static> {
        async fn launch_extension_prefs(&self, uuid: &str) -> Result<()> {
            let _reply = self
                .LaunchExtensionPrefs(uuid.to_string())
                .await
                .unwrap_or_else(|_| ());
            Ok(())
        }
        async fn list_extensions(&self) -> Vec<ListExtension> {
            let list = self.ListExtensions().await.unwrap();
            let mut list_extension: Vec<ListExtension> = Vec::new();
            for extension in list {
                let uuid = extension.0;
                let name = extension
                    .1
                    .get("name")
                    .unwrap()
                    .to_owned()
                    .try_into()
                    .unwrap();
                let description = extension
                    .1
                    .get("description")
                    .unwrap()
                    .to_owned()
                    .try_into()
                    .unwrap();
                let try_version = extension.1.get("version");
                let version = match try_version {
                    Some(version) => version
                        .to_owned()
                        .try_into()
                        .unwrap_or_else(|_| "".to_string()),
                    None => "".to_string(),
                };
                let state_number: f64 = extension
                    .1
                    .get("state")
                    .unwrap()
                    .to_owned()
                    .try_into()
                    .unwrap();
                let state: ListExtensionState = match state_number {
                    1.0 => ListExtensionState::ENABLED,
                    2.0 => ListExtensionState::DISABLED,
                    3.0 => ListExtensionState::ERROR,
                    4.0 => ListExtensionState::OUT_OF_DATE,
                    5.0 => ListExtensionState::DOWNLOADING,
                    6.0 => ListExtensionState::INITIALIZED,
                    99.0 => ListExtensionState::UNINSTALLED,
                    _ => ListExtensionState::UNINSTALLED,
                };
                let url = extension
                    .1
                    .get("url")
                    .unwrap()
                    .to_owned()
                    .try_into()
                    .unwrap();
                let item = ListExtension {
                    uuid,
                    name,
                    description,
                    version,
                    state,
                    url,
                };
                list_extension.push(item);
            }
            list_extension
        }
    }
    // Shell screenshot
    #[dbus_proxy(
        interface = "org.gnome.Shell.Screenshot",
        default_path = "/org/gnome/Shell/Screenshot"
    )]
    trait Screenshot {
        async fn PickColor(&self) -> Result<HashMap<String, zvariant::OwnedValue>>;
    }
    impl ScreenshotProxy<'static> {
        async fn pick_color(&self) -> (f64, f64, f64) {
            let pick_color = self.PickColor().await.unwrap();
            let value = pick_color.get("color").unwrap();
            let (r, g, b): (f64, f64, f64) = value.to_owned().try_into().unwrap();

            (r, g, b)
        }
    }

    #[dbus_proxy(
        interface = "org.gnome.SettingsDaemon.Power.Screen",
        default_service = "org.gnome.SettingsDaemon.Power",
        default_path = "/org/gnome/SettingsDaemon/Power"
    )]
    trait Screen {
        #[dbus_proxy(property)]
        fn Brightness(&self) -> Result<i32>;
        #[dbus_proxy(property)]
        fn set_Brightness(&self, brightness: i32) -> Result<()>;
        fn StepUp(&self) -> Result<()>;
        fn StepDown(&self) -> Result<()>;
    }

    // Settings color (night light)
    #[dbus_proxy(
        interface = "org.gnome.SettingsDaemon.Color",
        default_service = "org.gnome.SettingsDaemon.Color",
        default_path = "/org/gnome/SettingsDaemon/Color"
    )]
    trait Nightlight {
        #[dbus_proxy(property)]
        fn set_NightLightActive(&self, active: bool) -> Result<()>;
        #[dbus_proxy(property)]
        fn NightLightActive(&self) -> Result<bool>;
        #[dbus_proxy(property)]
        fn Temperature(&self) -> Result<u32>;

        async fn SetTemperature(&self, temperature: u32) -> Result<()>;
    }

    pub mod power {
        use zbus::Connection;

        use crate::handlers::easy_gnome::PowerManagementProxy;

        pub async fn power_off() {
            let connection = Connection::system().await.unwrap();
            let proxy = PowerManagementProxy::new(&connection).await.unwrap();
            proxy.PowerOff(true).await.unwrap();
        }
        pub async fn suspend() {
            let connection = Connection::system().await.unwrap();
            let proxy = PowerManagementProxy::new(&connection).await.unwrap();
            proxy.Suspend(true).await.unwrap();
        }
        pub async fn reboot() {
            let connection = Connection::system().await.unwrap();
            let proxy = PowerManagementProxy::new(&connection).await.unwrap();
            proxy.Reboot(true).await.unwrap();
        }
    }

    pub mod screenshot {
        use zbus::Connection;

        use crate::handlers::easy_gnome::ScreenshotProxy;

        pub async fn pick_color() -> (f64, f64, f64) {
            let connection = Connection::session().await.unwrap();
            let proxy = ScreenshotProxy::new(&connection).await.unwrap();
            proxy.pick_color().await
        }
    }

    pub mod screen {
        use zbus::Connection;

        use crate::handlers::easy_gnome::ScreenProxy;

        pub async fn brightness() -> i32 {
            let connection = Connection::session().await.unwrap();
            let proxy = ScreenProxy::new(&connection).await.unwrap();
            proxy.Brightness().await.unwrap()
        }
        pub async fn set_brightness(brightness: i32) {
            let connection = Connection::session().await.unwrap();
            let proxy = ScreenProxy::new(&connection).await.unwrap();
            proxy.set_Brightness(brightness).await.unwrap();
        }
        pub async fn step_up() {
            let connection = Connection::session().await.unwrap();
            let proxy = ScreenProxy::new(&connection).await.unwrap();
            proxy.StepUp().await.unwrap();
        }
        pub async fn step_down() {
            let connection = Connection::session().await.unwrap();
            let proxy = ScreenProxy::new(&connection).await.unwrap();
            proxy.StepDown().await.unwrap();
        }
    }

    pub mod nightlight {
        use std::process::Command;

        use zbus::Connection;

        use crate::handlers::easy_gnome::NightlightProxy;

        pub async fn nightlight_active() -> bool {
            let connection = Connection::session().await.unwrap();
            let proxy = NightlightProxy::new(&connection).await.unwrap();
            proxy.NightLightActive().await.unwrap()
        }
        pub async fn set_nightlight_active(active: bool) {
            Command::new("gsettings")
                .arg("set")
                .arg("org.gnome.settings-daemon.plugins.color")
                .arg("night-light-enabled")
                .arg(active.to_string())
                .spawn()
                .expect("failed to execute process");
        }
        pub async fn temperature() -> u32 {
            let connection = Connection::session().await.unwrap();
            let proxy = NightlightProxy::new(&connection).await.unwrap();
            proxy.Temperature().await.unwrap()
        }
        pub async fn set_temperature(temperature: u32) {
            let connection = Connection::session().await.unwrap();
            let proxy = NightlightProxy::new(&connection).await.unwrap();
            proxy.SetTemperature(temperature).await.unwrap();
        }
    }

    pub mod apps {

        use std::io::Cursor;

        use gio::glib::{home_dir, GString};
        use gio::prelude::*;
        use gio::AppInfo;
        use gtk::IconTheme;
        use gtk::{prelude::*, IconLookupFlags};
        use image::ImageOutputFormat;

        // #[derive(Debug)]
        pub struct App {
            pub name: GString,
            pub description: Option<GString>,
            pub icon: Option<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>>,
            // pub launch: &'static dyn Fn() -> Result<(), Box<dyn std::error::Error>>,
        }
        impl App {
            pub fn get_name(&self) -> &GString {
                &self.name
            }
            pub fn get_description(&self) -> &Option<GString> {
                &self.description
            }
            pub fn get_icon(&self) -> &Option<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>> {
                &self.icon
            }
            pub fn get_base64_icon(&self) -> Option<String> {
                match &self.icon {
                    Some(icon) => {
                        let mut image_data: Vec<u8> = Vec::new();
                        icon.write_to(&mut Cursor::new(&mut image_data), ImageOutputFormat::Png)
                            .unwrap();
                        let res_base64 = base64::encode(image_data);
                        Some(format!("data:image/png;base64,{}", res_base64))
                    }
                    None => None,
                }
            }
            pub fn launch(&self) -> Result<(), gio::glib::Error> {
                // Find app by name
                let __apps = AppInfo::all();
                __apps
                    .iter()
                    .find(|app| app.name().eq_ignore_ascii_case(&self.name))
                    .unwrap()
                    .launch(&[], None::<&gio::AppLaunchContext>)
            }
        }

        // #[derive(Debug)]
        pub struct Apps {
            pub apps: Vec<App>,
        }

        impl Apps {
            // pub fn set_launch_callback(&self, launch: Box<dyn Fn() -> Result<(), Box<dyn Error>>>) {
            //     self.launch = launch;
            // }
            pub fn get_apps(&self) -> &Vec<App> {
                &self.apps
            }

            pub fn new() -> Apps {
                gtk::init().unwrap();
                const ICON_SIZE: i32 = 128;

                let __apps = AppInfo::all();
                let icon_theme: IconTheme = IconTheme::default().unwrap();
                icon_theme.add_resource_path(
                    format!(
                        "{}/.local/share/icons/hicolor",
                        home_dir().to_str().unwrap()
                    )
                    .as_str(),
                );
                let mut apps: Vec<App> = Vec::new();

                for app in &__apps {
                    let name = app.name();
                    let description = app.description();
                    let icon = app.icon();
                    // let launch: &'static (dyn Fn() -> Result<(), ()> + 'static) = || {
                    //     let launch_context = gio::AppLaunchContext::NONE;
                    //     app.launch_uris(&[], launch_context)?;
                    //     Ok(())
                    // };

                    if icon.is_none() {
                        apps.push(App {
                            name,
                            description,
                            icon: None,
                            // launch: &launch,
                        });
                        continue;
                    }
                    let icon_name = gio::prelude::IconExt::to_string(&icon.unwrap()).unwrap();
                    // // Transform icon name to pixbuf
                    let pixbuf = icon_theme
                        .load_icon(&icon_name, ICON_SIZE, IconLookupFlags::GENERIC_FALLBACK)
                        .unwrap_or(
                            icon_theme
                                .load_icon("info", ICON_SIZE, IconLookupFlags::GENERIC_FALLBACK)
                                .unwrap(),
                        );

                    // Pix buf are cuadruplets of u8 (rgba)
                    let bytes: Vec<u8> = pixbuf.unwrap().read_pixel_bytes().unwrap().to_vec();

                    // Using image library build a png based on cuadruplets (rgba)
                    let png: image::ImageBuffer<image::Rgba<u8>, Vec<u8>> =
                        match image::RgbaImage::from_vec(ICON_SIZE as u32, ICON_SIZE as u32, bytes)
                        {
                            Some(png) => png,
                            None => continue,
                        };

                    apps.push(App {
                        name,
                        description,
                        icon: Some(png),
                        // launch: &launch,
                    });
                }
                Apps { apps }
            }
        }
    }

    pub mod peripherals {
        pub fn set_keyboard_press_delay(delay: u32) -> Result<(), String> {
            crate::dconf::set(
                "org.gnome.desktop.peripherals.keyboard",
                "delay",
                String::from(delay.to_string()).as_str(),
            )
        }
        pub fn get_keyboard_press_delay() -> Result<u32, String> {
            let value: String =
                crate::dconf::get("org.gnome.desktop.peripherals.keyboard", "delay")?;
            Ok(value.parse::<u32>().unwrap())
        }
        pub fn reset_keyboard_press_delay() -> Result<(), String> {
            crate::dconf::reset("org.gnome.desktop.peripherals.keyboard", "delay")
        }
        pub fn set_keyboard_repeat_interval(interval: u32) -> Result<(), String> {
            crate::dconf::set(
                "org.gnome.desktop.peripherals.keyboard",
                "repeat-interval",
                String::from(interval.to_string()).as_str(),
            )
        }
        pub fn get_keyboard_repeat_interval() -> Result<u32, String> {
            let value =
                crate::dconf::get("org.gnome.desktop.peripherals.keyboard", "repeat-interval")?;
            Ok(value.parse::<u32>().unwrap())
        }
        pub fn reset_keyboard_repeat_interval() -> Result<(), String> {
            crate::dconf::reset("org.gnome.desktop.peripherals.keyboard", "repeat-interval")
        }
        pub fn set_mouse_natural_scroll(enabled: bool) -> Result<(), String> {
            crate::dconf::set(
                "org.gnome.desktop.peripherals.mouse",
                "natural-scroll",
                String::from(enabled.to_string()).as_str(),
            )
        }
        pub fn get_mouse_natural_scroll() -> Result<bool, String> {
            let value = crate::dconf::get("org.gnome.desktop.peripherals.mouse", "natural-scroll")?;
            Ok(value.parse::<bool>().unwrap())
        }
        pub fn reset_mouse_natural_scroll() -> Result<(), String> {
            crate::dconf::reset("org.gnome.desktop.peripherals.mouse", "natural-scroll")
        }
        pub fn set_touchpad_tap_to_click(enabled: bool) -> Result<(), String> {
            crate::dconf::set(
                "org.gnome.desktop.peripherals.touchpad",
                "tap-to-click",
                String::from(enabled.to_string()).as_str(),
            )
        }
        pub fn get_touchpad_tap_to_click() -> Result<bool, String> {
            let value =
                crate::dconf::get("org.gnome.desktop.peripherals.touchpad", "tap-to-click")?;
            Ok(value.parse::<bool>().unwrap())
        }
        pub fn reset_touchpad_tap_to_click() -> Result<(), String> {
            crate::dconf::reset("org.gnome.desktop.peripherals.touchpad", "tap-to-click")
        }
        pub fn set_two_finger_scroll(enabled: bool) -> Result<(), String> {
            crate::dconf::set(
                "org.gnome.desktop.peripherals.touchpad",
                "two-finger-scrolling-enabled",
                String::from(enabled.to_string()).as_str(),
            )
        }
        pub fn get_two_finger_scroll() -> Result<bool, String> {
            let value = crate::dconf::get(
                "org.gnome.desktop.peripherals.touchpad",
                "two-finger-scrolling-enabled",
            )?;
            Ok(value.parse::<bool>().unwrap())
        }
        pub fn reset_two_finger_scroll() -> Result<(), String> {
            crate::dconf::reset(
                "org.gnome.desktop.peripherals.touchpad",
                "two-finger-scrolling-enabled",
            )
        }
    }
}