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
use crate::minecraft::servers::{AcceptTextures, Server};
use gio::subclass::prelude::*;
use glib::{ObjectExt, ParamFlags, ParamSpec, ParamSpecInt, ParamSpecString, ToValue};
use once_cell::sync::Lazy;
use std::cell::{Cell, RefCell};

pub const NAME: &str = "name";
pub const IP: &str = "ip";
pub const ICON: &str = "icon";
pub const ACCEPT_TEXTURES: &str = "accept-textures";

mod imp {

    use super::*;

    #[derive(Debug, Default)]
    pub struct GServer {
        pub name: RefCell<String>,
        pub ip: RefCell<String>,
        pub icon: RefCell<Option<String>>,
        pub accept_textures: Cell<i32>,
    }

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

    impl ObjectImpl for GServer {
        fn properties() -> &'static [glib::ParamSpec] {
            static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| {
                vec![
                    ParamSpecString::new(NAME, "Name", "Name", None, ParamFlags::READWRITE),
                    ParamSpecString::new(IP, "IP", "IP", None, ParamFlags::READWRITE),
                    ParamSpecString::new(ICON, "Icon", "Icon", None, ParamFlags::READWRITE),
                    ParamSpecInt::new(
                        ACCEPT_TEXTURES,
                        "Accept Textures",
                        "Accept Textures",
                        -1,
                        1,
                        -1,
                        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(),
                IP => *self.ip.borrow_mut() = value.get().unwrap(),
                ICON => *self.icon.borrow_mut() = value.get().unwrap(),
                ACCEPT_TEXTURES => self.accept_textures.set(value.get().unwrap()),
                prop => unimplemented!("Property {prop} not a member of GServer"),
            }
        }

        fn property(&self, _obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
            match pspec.name() {
                NAME => self.name.borrow().to_value(),
                IP => self.ip.borrow().to_value(),
                ICON => self.icon.borrow().to_value(),
                ACCEPT_TEXTURES => self.accept_textures.get().to_value(),
                prop => unimplemented!("Property {prop} not a member of GServer"),
            }
        }
    }
}

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

impl GServer {
    /// 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 'ip'.
    pub fn set_ip(&self, value: String) {
        self.set_property(IP, value);
    }

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

    /// Set 'icon'.
    pub fn set_icon(&self, value: Option<String>) {
        self.set_property(ICON, value);
    }

    /// Get 'icon'.
    pub fn icon(&self) -> Option<String> {
        self.property(ICON)
    }

    /// Set 'accept_textures'.
    pub fn set_accept_textures(&self, value: AcceptTextures) {
        self.set_property::<i32>(ACCEPT_TEXTURES, value.into());
    }

    /// Get 'accept_textures'.
    /// Panics if an invalid accept_textures was saved.
    pub fn accept_textures(&self) -> AcceptTextures {
        let value = self.property::<i32>(ACCEPT_TEXTURES);
        AcceptTextures::try_from(value).unwrap()
    }
}

impl From<Server> for GServer {
    fn from(server: Server) -> Self {
        glib::Object::new(&[
            (NAME, &server.name),
            (IP, &server.ip),
            (ICON, &server.icon),
            (ACCEPT_TEXTURES, &i32::from(server.accept_textures)),
        ])
        .unwrap()
    }
}

impl From<GServer> for Server {
    fn from(gserver: GServer) -> Self {
        Self {
            name: gserver.name(),
            ip: gserver.ip(),
            icon: gserver.icon(),
            accept_textures: gserver.accept_textures(),
        }
    }
}