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
// Souk - task_step.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 flatpak::prelude::*;
use flatpak::{Transaction, TransactionOperation, TransactionProgress};
use gtk::gio;
use serde::{Deserialize, Serialize};
use zbus::zvariant::{Optional, Type};

use super::TaskStatus;
use crate::shared::flatpak::info::{PackageInfo, RemoteInfo};
use crate::shared::flatpak::FlatpakOperationKind;

#[derive(Deserialize, Serialize, Type, PartialEq, Debug, Clone)]
pub struct TaskProgress {
    /// A task can consist of several steps. The index indicates for which step
    /// this progress information is.
    pub index: u32,
    pub operation_kind: FlatpakOperationKind,
    pub status: TaskStatus,
    pub progress: i32,
    pub download_rate: u64,

    pub package: Optional<PackageInfo>,
}

impl TaskProgress {
    pub fn new_flatpak(
        transaction: &Transaction,
        operation: &TransactionOperation,
        op_progress: Option<&TransactionProgress>,
        is_done: bool,
    ) -> Self {
        let index: u32 = transaction
            .operations()
            .iter()
            .position(|o| o == operation)
            .unwrap()
            .try_into()
            .unwrap();

        let (progress, download_rate, status) = if let Some(op_progress) = op_progress {
            // Calculate download-rate in bytes per second
            let start_time = op_progress.start_time();
            let elapsed_time = (gtk::glib::monotonic_time() as u64 - start_time) as f64 / 1000000.0;
            let bytes_second = (op_progress.bytes_transferred() as f64 / elapsed_time) as u64;

            (
                op_progress.progress(),
                bytes_second,
                operation.operation_type().into(),
            )
        } else if is_done {
            (100, 0, TaskStatus::Done)
        } else {
            (0, 0, TaskStatus::Pending)
        };

        let ref_ = operation.get_ref().unwrap();

        let installation = transaction.installation().unwrap();
        let remote_name = operation.remote().unwrap();
        let remote = installation
            .remote_by_name(&remote_name, gio::Cancellable::NONE)
            .unwrap();
        let remote_info = RemoteInfo::from_flatpak(&remote, &installation);

        let package = PackageInfo::new(ref_.to_string(), remote_info);
        let operation_kind = operation.operation_type().into();

        Self {
            index,
            operation_kind,
            status,
            progress,
            download_rate,
            package: Some(package).into(),
        }
    }
}

impl Default for TaskProgress {
    fn default() -> Self {
        Self {
            index: u32::default(),
            operation_kind: FlatpakOperationKind::default(),
            status: TaskStatus::default(),
            progress: i32::default(),
            download_rate: u64::default(),
            package: None.into(),
        }
    }
}