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
// Souk - remote_info.rs
// Copyright (C) 2022-2023  Felix Häcker <haeckerfelix@gnome.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

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);

        // Verify that bytes can be read into a Flatpak `Remote` object
        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.",
            ))
        }
    }
}