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
use crate::minecraft::screenshots::Screenshot;
use gio::subclass::prelude::*;
use glib::{ObjectExt, ParamFlags, ParamSpec, ParamSpecString, ToValue};
use once_cell::sync::Lazy;
use std::cell::RefCell;
use std::path::PathBuf;

pub const NAME: &str = "name";
pub const PATH: &str = "path";

mod imp {

    use super::*;

    #[derive(Debug, Default)]
    pub struct GScreenshot {
        pub name: RefCell<String>,
        pub path: RefCell<String>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for GScreenshot {
        const NAME: &'static str = "GScreenshot";
        type Type = super::GScreenshot;
        type ParentType = glib::Object;
    }

    impl ObjectImpl for GScreenshot {
        fn properties() -> &'static [glib::ParamSpec] {
            static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| {
                vec![
                    ParamSpecString::new(NAME, "Name", "Name", None, ParamFlags::READWRITE),
                    ParamSpecString::new(PATH, "Path", "Path", None, ParamFlags::READWRITE),
                ]
            });

            PROPERTIES.as_ref()
        }

        fn set_property(
            &self,
            _obj: &Self::Type,
            _id: usize,
            value: &glib::Value,
            pspec: &glib::ParamSpec,
        ) {
            match pspec.name() {
                NAME => *self.name.borrow_mut() = value.get().unwrap(),
                PATH => *self.path.borrow_mut() = value.get().unwrap(),
                prop => unimplemented!("Property {prop} not a member of GScreenshot"),
            }
        }

        fn property(&self, _obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
            match pspec.name() {
                NAME => self.name.borrow().to_value(),
                PATH => self.path.borrow().to_value(),
                prop => unimplemented!("Property {prop} not a member of GScreenshot"),
            }
        }
    }
}

glib::wrapper! {
    /// Glib object for [`Screenshot`](crate::minecraft::screenshots::Screenshot).
    pub struct GScreenshot(ObjectSubclass<imp::GScreenshot>);
}

impl GScreenshot {
    /// Set 'name'.
    pub fn set_name(&self, value: String) {
        self.set_property(NAME, value);
    }

    /// Get 'name'.
    pub fn name(&self) -> String {
        self.property(NAME)
    }

    /// Set 'path'.
    pub fn set_path(&self, value: PathBuf) {
        self.set_property(PATH, value.to_string_lossy().to_string());
    }

    /// Get 'path'.
    pub fn path(&self) -> PathBuf {
        PathBuf::from(self.property::<String>(PATH))
    }
}

impl From<Screenshot> for GScreenshot {
    fn from(screenshot: Screenshot) -> Self {
        glib::Object::new(&[
            (NAME, &screenshot.name),
            (PATH, &screenshot.path.to_string_lossy().to_string()),
        ])
        .unwrap()
    }
}

impl From<GScreenshot> for Screenshot {
    fn from(gscreenshot: GScreenshot) -> Self {
        Self {
            name: gscreenshot.name(),
            path: gscreenshot.path(),
        }
    }
}