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
use gio::subclass::prelude::*;
use glib::ParamSpecInt64;
use glib::{ObjectExt, ParamFlags, ParamSpec, ParamSpecString, ToValue};
use once_cell::sync::{Lazy, OnceCell};
use std::cell::Cell;
use std::{cell::RefCell, str::FromStr};
use time::OffsetDateTime;
use uuid::Uuid;
use crate::profile::Profile;
pub const UUID: &str = "uuid";
pub const USERNAME: &str = "username";
pub const PROFILE_ID: &str = "profile-id";
pub const PLAYER_NAME: &str = "player-name";
pub const MICROSOFT_REFRESH_TOKEN: &str = "microsoft-refresh-token";
pub const MINECRAFT_TOKEN: &str = "minecraft-token";
pub const MINECRAFT_TOKEN_EXP: &str = "minecraft-token-exp";
mod imp {
use super::*;
#[derive(Debug, Default)]
pub struct GProfile {
pub uuid: OnceCell<String>,
pub username: RefCell<String>,
pub profile_id: RefCell<String>,
pub player_name: RefCell<String>,
pub microsoft_refresh_token: RefCell<String>,
pub minecraft_token: RefCell<String>,
pub minecraft_token_exp: Cell<i64>,
}
#[glib::object_subclass]
impl ObjectSubclass for GProfile {
const NAME: &'static str = "GProfile";
type Type = super::GProfile;
type ParentType = glib::Object;
}
impl ObjectImpl for GProfile {
fn properties() -> &'static [glib::ParamSpec] {
static PROPERTIES: Lazy<Vec<ParamSpec>> = Lazy::new(|| {
vec![
ParamSpecString::new(
UUID,
"UUID",
"UUID",
None,
ParamFlags::READWRITE | ParamFlags::CONSTRUCT_ONLY,
),
ParamSpecString::new(
USERNAME,
"Username",
"Username",
None,
ParamFlags::READWRITE,
),
ParamSpecString::new(
PROFILE_ID,
"Profile ID",
"Profile ID",
None,
ParamFlags::READWRITE,
),
ParamSpecString::new(
PLAYER_NAME,
"Player Name",
"Player Name",
None,
ParamFlags::READWRITE,
),
ParamSpecString::new(
MICROSOFT_REFRESH_TOKEN,
"Microsoft Refresh Token",
"Microsoft Refresh Token",
None,
ParamFlags::READWRITE,
),
ParamSpecString::new(
MINECRAFT_TOKEN,
"Minecraft Token",
"Minecraft Token",
None,
ParamFlags::READWRITE,
),
ParamSpecInt64::new(
MINECRAFT_TOKEN_EXP,
"Minecraft Token Expiration",
"Minecraft Token Expiration",
i64::MIN,
i64::MAX,
0,
ParamFlags::READWRITE,
),
]
});
PROPERTIES.as_ref()
}
fn set_property(
&self,
_obj: &Self::Type,
_id: usize,
value: &glib::Value,
pspec: &glib::ParamSpec,
) {
match pspec.name() {
UUID => self.uuid.set(value.get().unwrap()).unwrap(),
USERNAME => *self.username.borrow_mut() = value.get().unwrap(),
PROFILE_ID => *self.profile_id.borrow_mut() = value.get().unwrap(),
PLAYER_NAME => *self.player_name.borrow_mut() = value.get().unwrap(),
MICROSOFT_REFRESH_TOKEN => {
*self.microsoft_refresh_token.borrow_mut() = value.get().unwrap()
}
MINECRAFT_TOKEN => *self.minecraft_token.borrow_mut() = value.get().unwrap(),
MINECRAFT_TOKEN_EXP => self.minecraft_token_exp.set(value.get().unwrap()),
prop => unimplemented!("Property {prop} not a member of GProfile"),
}
}
fn property(&self, _obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
match pspec.name() {
UUID => self.uuid.get().to_value(),
USERNAME => self.username.borrow().to_value(),
PROFILE_ID => self.profile_id.borrow().to_value(),
PLAYER_NAME => self.player_name.borrow().to_value(),
MICROSOFT_REFRESH_TOKEN => self.microsoft_refresh_token.borrow().to_value(),
MINECRAFT_TOKEN => self.minecraft_token.borrow().to_value(),
MINECRAFT_TOKEN_EXP => self.minecraft_token_exp.get().to_value(),
prop => unimplemented!("Property {prop} not a member of GProfile"),
}
}
}
}
glib::wrapper! {
pub struct GProfile(ObjectSubclass<imp::GProfile>);
}
impl GProfile {
pub fn uuid(&self) -> Uuid {
let uuid: String = self.property(UUID);
Uuid::from_str(&uuid).unwrap()
}
pub fn set_username(&self, value: String) {
self.set_property(USERNAME, value);
}
pub fn username(&self) -> String {
self.property(USERNAME)
}
pub fn set_profile_id(&self, value: String) {
self.set_property(PROFILE_ID, value);
}
pub fn profile_id(&self) -> String {
self.property(PROFILE_ID)
}
pub fn set_player_name(&self, value: String) {
self.set_property(PLAYER_NAME, value);
}
pub fn player_name(&self) -> String {
self.property(PLAYER_NAME)
}
pub fn set_microsoft_refresh_token(&self, value: String) {
self.set_property(MICROSOFT_REFRESH_TOKEN, value);
}
pub fn microsoft_refresh_token(&self) -> String {
self.property(MICROSOFT_REFRESH_TOKEN)
}
pub fn set_minecraft_token(&self, value: String) {
self.set_property(MINECRAFT_TOKEN, value);
}
pub fn minecraft_token(&self) -> String {
self.property(MINECRAFT_TOKEN)
}
pub fn set_minecraft_token_exp(&self, value: OffsetDateTime) {
let exp = value.unix_timestamp();
self.set_property(MINECRAFT_TOKEN_EXP, exp);
}
pub fn minecraft_token_exp(&self) -> OffsetDateTime {
let exp: i64 = self.property(MINECRAFT_TOKEN_EXP);
OffsetDateTime::from_unix_timestamp(exp).unwrap()
}
}
impl From<Profile> for GProfile {
fn from(profile: Profile) -> Self {
glib::Object::new(&[
(UUID, &profile.uuid.to_string()),
(USERNAME, &profile.username),
(PROFILE_ID, &profile.profile_id),
(PLAYER_NAME, &profile.player_name),
(MICROSOFT_REFRESH_TOKEN, &profile.microsoft_refresh_token),
(MINECRAFT_TOKEN, &profile.minecraft_token),
(
MINECRAFT_TOKEN_EXP,
&profile.minecraft_token_exp.unix_timestamp(),
),
])
.unwrap()
}
}
impl From<GProfile> for Profile {
fn from(gprofile: GProfile) -> Self {
Self {
uuid: gprofile.uuid(),
username: gprofile.username(),
profile_id: gprofile.profile_id(),
player_name: gprofile.player_name(),
microsoft_refresh_token: gprofile.microsoft_refresh_token(),
minecraft_token: gprofile.minecraft_token(),
minecraft_token_exp: gprofile.minecraft_token_exp(),
}
}
}
impl Default for GProfile {
fn default() -> Self {
let uuid = Uuid::new_v4();
glib::Object::new(&[(UUID, &uuid.to_string())]).unwrap()
}
}