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
// Souk - task_status.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 std::fmt;

use glib::Enum;
use gtk::glib;

use crate::main::i18n::i18n;
use crate::shared::task::TaskStatus;

#[derive(Copy, Debug, Clone, Eq, PartialEq, Enum)]
#[repr(u32)]
#[enum_type(name = "SkTaskStatus")]
#[derive(Default)]
pub enum SkTaskStatus {
    #[default]
    None,
    Pending,
    Preparing,
    Installing,
    InstallingBundle,
    Uninstalling,
    Updating,
    Done,
    Cancelled,
    Error,
}

impl SkTaskStatus {
    pub fn is_completed(&self) -> bool {
        self == &Self::Done || self == &Self::Cancelled || self == &Self::Error
    }

    pub fn has_no_detailed_progress(&self) -> bool {
        self == &Self::InstallingBundle
    }
}

impl From<TaskStatus> for SkTaskStatus {
    fn from(status: TaskStatus) -> Self {
        match status {
            TaskStatus::Pending => Self::Pending,
            TaskStatus::Installing => Self::Installing,
            TaskStatus::InstallingBundle => Self::InstallingBundle,
            TaskStatus::Uninstalling => Self::Uninstalling,
            TaskStatus::Updating => Self::Updating,
            TaskStatus::Done => Self::Done,
            TaskStatus::None => Self::None,
        }
    }
}

impl fmt::Display for SkTaskStatus {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let text = match self {
            Self::None => i18n("Unknown"),
            Self::Pending => i18n("Pending…"),
            Self::Preparing => i18n("Preparing…"),
            Self::Installing => i18n("Installing…"),
            Self::InstallingBundle => i18n("Installing Bundle…"),
            Self::Uninstalling => i18n("Uninstalling…"),
            Self::Updating => i18n("Updating…"),
            Self::Done => i18n("Done"),
            Self::Cancelled => i18n("Cancelled"),
            Self::Error => i18n("Error"),
        };

        write!(f, "{text}")
    }
}