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
// Souk - task.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 gtk::glib;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use zbus::zvariant::{Optional, Type};

use crate::shared::task::{AppstreamTask, FlatpakTask};

#[derive(Deserialize, Serialize, Type, Eq, PartialEq, Debug, Clone, Hash, glib::Boxed)]
#[boxed_type(name = "Task", nullable)]
pub struct Task {
    /// Each task has a unique UUID that can be used for identification. This is
    /// required, for example, if a running task should be cancelled.
    pub uuid: String,
    /// Whether the task can be cancelled
    pub cancellable: bool,

    // This should have been an enum, unfortunately not supported by zbus / dbus
    flatpak_task: Optional<FlatpakTask>,
    appstream_task: Optional<AppstreamTask>,
}

impl Task {
    pub fn new_flatpak(task: FlatpakTask, cancellable: bool) -> Self {
        let uuid = Uuid::new_v4().to_string();
        Self {
            uuid,
            cancellable,
            flatpak_task: Some(task).into(),
            appstream_task: None.into(),
        }
    }

    pub fn new_appstream(task: AppstreamTask, cancellable: bool) -> Self {
        let uuid = Uuid::new_v4().to_string();
        Self {
            uuid,
            cancellable,
            flatpak_task: None.into(),
            appstream_task: Some(task).into(),
        }
    }

    /// Returns [FlatpakTask] if this is a Flatpak task.
    pub fn flatpak_task(&self) -> Option<FlatpakTask> {
        self.flatpak_task.clone().into()
    }

    /// Returns [AppstreamTask] if this is a Flatpak task
    pub fn appstream_task(&self) -> Option<AppstreamTask> {
        self.appstream_task.clone().into()
    }
}