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
use crate::minecraft::logs::{LogFile, LogFileType};
use gio::subclass::prelude::*;
use glib::{ObjectExt, ParamFlags, ParamSpec, ParamSpecInt64, ParamSpecString, ToValue};
use once_cell::sync::Lazy;
use std::cell::{Cell, RefCell};
use std::path::PathBuf;
use time::OffsetDateTime;

pub const NAME: &str = "name";
pub const PATH: &str = "path";
pub const TYPE: &str = "type";
pub const MODIFIED: &str = "modified";

mod imp {
    use super::*;

    #[derive(Debug, Default)]
    pub struct GLogFile {
        pub name: RefCell<String>,
        pub path: RefCell<String>,
        pub _type: RefCell<String>,
        pub modified: Cell<i64>,
    }

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

    impl ObjectImpl for GLogFile {
        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),
                    ParamSpecString::new(TYPE, "Type", "Type", None, ParamFlags::READWRITE),
                    ParamSpecInt64::new(
                        MODIFIED,
                        "Modified",
                        "Modified",
                        -1,
                        i64::MAX,
                        -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(),
                PATH => *self.path.borrow_mut() = value.get().unwrap(),
                TYPE => *self._type.borrow_mut() = value.get().unwrap(),
                MODIFIED => self.modified.set(value.get().unwrap()),
                prop => unimplemented!("Property {prop} not a member of GLogFile"),
            }
        }

        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(),
                TYPE => self._type.borrow().to_value(),
                MODIFIED => self.modified.get().to_value(),
                prop => unimplemented!("Property {prop} not a member of GLogFile"),
            }
        }
    }
}

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

impl GLogFile {
    /// 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))
    }

    /// Set 'type'.
    pub fn set_type(&self, value: LogFileType) {
        let value = match value {
            LogFileType::Plain => "plain",
            LogFileType::Compressed => "compressed",
        };
        self.set_property(PATH, value);
    }

    /// Get 'type'.
    pub fn _type(&self) -> LogFileType {
        let value = self.property::<String>(PATH);
        match value.as_str() {
            "plain" => LogFileType::Plain,
            "compressed" => LogFileType::Compressed,
            x => panic!("Unknown LogFileType '{x}'"),
        }
    }

    /// Set 'modified'.
    pub fn set_modified(&self, value: Option<OffsetDateTime>) {
        let value = value.map(|v| v.unix_timestamp()).unwrap_or(-1);
        self.set_property(MODIFIED, value);
    }

    /// Get 'modified'.
    /// Panics if an invalid timestamp was saved
    /// (Should not happen when converting from [LogFile](crate::minecraft::logs::LogFile), or using `set_modified()`).
    pub fn modified(&self) -> Option<OffsetDateTime> {
        let value = self.property::<i64>(MODIFIED);
        if value < 0 {
            None
        } else {
            Some(OffsetDateTime::from_unix_timestamp(value).unwrap())
        }
    }
}

impl From<LogFile> for GLogFile {
    fn from(log_file: LogFile) -> Self {
        let _type = match log_file._type {
            LogFileType::Plain => "plain",
            LogFileType::Compressed => "compressed",
        };

        glib::Object::new(&[
            (NAME, &log_file.name),
            (PATH, &log_file.path.to_string_lossy().to_string()),
            (TYPE, &_type),
        ])
        .unwrap()
    }
}

impl From<GLogFile> for LogFile {
    fn from(glog_file: GLogFile) -> Self {
        Self {
            name: glog_file.name(),
            path: glog_file.path(),
            _type: glog_file._type(),
            modified: None,
        }
    }
}