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
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 {
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 {
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(),
}
}
}