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
// Souk - installation.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 async_std::process::Command;
use flatpak::prelude::*;
use flatpak::{Installation, Remote};
use gio::{Cancellable, File, FileMonitor};
use glib::{clone, ParamSpec, Properties};
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{gio, glib};
use once_cell::unsync::OnceCell;

use crate::main::error::Error;
use crate::main::flatpak::installation::{SkRemote, SkRemoteModel};
use crate::main::flatpak::package::{SkPackage, SkPackageModel};
use crate::main::i18n::i18n;
use crate::shared::flatpak::info::{InstallationInfo, PackageInfo, RemoteInfo};

mod imp {
    use super::*;

    #[derive(Debug, Default, Properties)]
    #[properties(wrapper_type = super::SkInstallation)]
    pub struct SkInstallation {
        #[property(get)]
        name: OnceCell<String>,
        #[property(get)]
        title: OnceCell<String>,
        #[property(get)]
        description: OnceCell<String>,
        #[property(get)]
        icon_name: OnceCell<String>,
        #[property(get)]
        remotes: SkRemoteModel,
        #[property(get)]
        packages: SkPackageModel,
        #[property(name = "path", get = Self::path, type = File)]
        #[property(name = "is-user", get, type = bool, member = is_user)]
        #[property(get, set, construct_only)]
        info: OnceCell<InstallationInfo>,

        monitor: OnceCell<FileMonitor>,
    }

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

    impl ObjectImpl for SkInstallation {
        fn properties() -> &'static [ParamSpec] {
            Self::derived_properties()
        }

        fn property(&self, id: usize, pspec: &ParamSpec) -> glib::Value {
            Self::derived_property(self, id, pspec)
        }

        fn set_property(&self, id: usize, value: &glib::Value, pspec: &ParamSpec) {
            Self::derived_set_property(self, id, value, pspec)
        }

        fn constructed(&self) {
            self.parent_constructed();

            let info = self.obj().info();
            let obj = self.obj();

            self.name.set(info.name.clone()).unwrap();

            // Create file monitor to detect changes in the installation (eg. ref
            // in/uninstall, remote changes)
            let f_inst = Installation::from(&info);
            let monitor = f_inst.create_monitor(Cancellable::NONE).unwrap();
            monitor.connect_changed(clone!(@weak obj as this => move |_,_,_,_|{
                debug!("Detected change in Flatpak installation.");
                this.refresh();
            }));
            self.monitor.set(monitor).unwrap();

            // Set a more user friendly installation title, a description and an icon which
            // can be used in UIs.
            if info.name == "default" && !info.is_user {
                // Default system installation
                let title = i18n("System");
                self.title.set(title).unwrap();

                let description = i18n("All users on this computer");
                self.description.set(description).unwrap();

                self.icon_name.set("computer-symbolic".into()).unwrap();
            } else if info.name == "user" && info.is_user {
                // Default user installation
                let title = i18n("User");
                self.title.set(title).unwrap();

                let description = i18n("Only currently logged in user");
                self.description.set(description).unwrap();

                self.icon_name.set("person-symbolic".into()).unwrap();
            } else {
                let title = if let Some(string) = Installation::from(&info).display_name() {
                    string.to_string()
                } else {
                    i18n("Flatpak Installation")
                };

                self.title.set(title).unwrap();
                if info.is_user {
                    self.description.set(i18n("User Installation")).unwrap();
                } else {
                    self.description.set(i18n("System Installation")).unwrap();
                }

                self.icon_name
                    .set("drive-harddisk-symbolic".into())
                    .unwrap();
            }
        }
    }

    impl SkInstallation {
        fn path(&self) -> File {
            File::for_parse_name(&self.obj().info().path)
        }
    }
}

glib::wrapper! {
    pub struct SkInstallation(ObjectSubclass<imp::SkInstallation>);
}

impl SkInstallation {
    pub(super) fn new(info: &InstallationInfo) -> Self {
        glib::Object::builder().property("info", info).build()
    }

    pub fn launch_app(&self, app: &SkPackage) {
        debug!(
            "Launch app from installation \"{}\": {}",
            self.name(),
            app.name()
        );

        let installation = if self.name() == "user" {
            "--user".into()
        } else {
            format!("--installation={}", self.name())
        };

        if let Err(err) = Command::new("flatpak-spawn")
            .arg("--host")
            .arg("flatpak")
            .arg("run")
            .arg(installation)
            .arg(app.name())
            .spawn()
        {
            error!("Unable to launch app: {}", err.to_string());
        }
    }

    pub fn add_remote(&self, remote: &SkRemote) -> Result<(), Error> {
        debug!(
            "Adding remote \"{}\" to installation \"{}\"",
            remote.name(),
            self.name()
        );

        let f_remote: Remote = remote.to_owned().info().try_into()?;

        let f_inst = Installation::from(&self.info());
        f_inst.add_remote(&f_remote, false, Cancellable::NONE)?;

        self.refresh();

        Ok(())
    }

    pub fn refresh(&self) {
        debug!(
            "Refresh Flatpak \"{}\" ({}) installation...",
            self.title(),
            self.name()
        );

        let f_inst = Installation::from(&self.info());

        let f_remotes = f_inst.list_remotes(Cancellable::NONE).unwrap();
        let mut remotes = Vec::new();
        for f_remote in &f_remotes {
            let remote_info = RemoteInfo::from_flatpak(f_remote, &f_inst);
            remotes.push(remote_info);
        }
        self.remotes().set_remotes(remotes);

        let f_packages = f_inst.list_installed_refs(Cancellable::NONE).unwrap();
        let mut packages = Vec::new();
        for f_package in &f_packages {
            if let Ok(f_remote) =
                f_inst.remote_by_name(&f_package.origin().unwrap(), Cancellable::NONE)
            {
                let package_info = PackageInfo::from_flatpak(f_package, &f_remote, &f_inst);
                packages.push(package_info);
            } else {
                // TODO: Maybe we shouldn't ignore it, but add it to some kind of dummy remote?
                // Can happen when you delete a remote during a running Flatpak transaction.
                warn!(
                    "Ignoring package without remote: {}",
                    f_package.format_ref().unwrap_or_default()
                );
            }
        }
        self.packages().set_packages(packages);
    }
}