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 derivative::Derivative;
use flatpak::prelude::*;
use flatpak::{Installation, Remote};
use gtk::glib;
use gtk::glib::Error;
use serde::{Deserialize, Serialize};
use zbus::zvariant::{Optional, Type};
use super::InstallationInfo;
use crate::shared::WorkerError;
#[derive(Derivative, Deserialize, Serialize, Hash, Type, Clone, Eq, PartialEq, glib::Boxed)]
#[boxed_type(name = "RemoteInfo", nullable)]
#[derivative(Debug)]
pub struct RemoteInfo {
pub name: String,
pub repository_url: String,
pub installation: Optional<InstallationInfo>,
#[derivative(Debug = "ignore")]
repo_bytes: Optional<Vec<u8>>,
}
impl RemoteInfo {
pub fn new(
name: String,
repository_url: String,
installation: Option<InstallationInfo>,
) -> Self {
Self {
name,
repository_url,
installation: installation.into(),
..Default::default()
}
}
pub fn from_flatpak(remote: &Remote, installation: &Installation) -> Self {
Self::new(
remote.name().unwrap().into(),
remote.url().unwrap_or_default().into(),
Some(installation.into()),
)
}
pub fn from_repo_file(name: &str, bytes: Vec<u8>) -> Result<Self, WorkerError> {
let g_bytes = glib::Bytes::from(&bytes);
let remote = Remote::from_file(name, &g_bytes)?;
Ok(Self {
name: remote.name().unwrap().into(),
repository_url: remote.url().unwrap_or_default().into(),
repo_bytes: Some(bytes).into(),
..Default::default()
})
}
pub fn set_repo_bytes(&mut self, bytes: Vec<u8>) {
self.repo_bytes = Some(bytes.to_vec()).into();
}
}
impl Default for RemoteInfo {
fn default() -> Self {
Self {
name: String::default(),
repository_url: String::default(),
installation: None.into(),
repo_bytes: None.into(),
}
}
}
impl TryInto<Remote> for RemoteInfo {
type Error = Error;
fn try_into(self) -> Result<Remote, Self::Error> {
if let Some(bytes) = self.repo_bytes.as_ref() {
let g_bytes = glib::Bytes::from(bytes);
let r = Remote::from_file(&self.name, &g_bytes)?;
r.set_gpg_verify(true);
Ok(r)
} else {
Err(Error::new(
flatpak::Error::RemoteNotFound,
"Unable to create Flatpak remote object, no repodata available.",
))
}
}
}