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
use appstream::Component;
use flatpak::prelude::*;
use flatpak::{Ref, RefKind, Remote};
use gtk::gio::Cancellable;
use gtk::prelude::*;
use xb::prelude::*;
use crate::shared::flatpak::dry_run::DryRunPackage;
pub fn set_dry_run_package_appstream(package: &mut DryRunPackage, ref_str: &str, remote: &Remote) {
let ref_ = Ref::parse(ref_str).unwrap();
let mut kind = if ref_.kind() == RefKind::App {
"app"
} else {
"runtime"
};
let mut name = ref_.name().unwrap().to_string();
let arch = ref_.arch().unwrap().to_string();
let branch = ref_.branch().unwrap().to_string();
if name.ends_with(".Locale") || name.ends_with(".Debug") || name.ends_with(".Sources") {
name = name.trim_end_matches(".Locale").into();
name = name.trim_end_matches(".Debug").into();
name = name.trim_end_matches(".Sources").into();
kind = "app";
}
let appstream_dir = remote.appstream_dir(Some(&arch)).unwrap();
let appstream_file = appstream_dir.child("appstream.xml");
debug!("Load appstream file {:?}", appstream_file.path().unwrap());
let source = xb::BuilderSource::new();
let res = source.load_file(
&appstream_file,
xb::BuilderSourceFlags::NONE,
Cancellable::NONE,
);
if let Err(err) = res {
error!("Unable to load appstream file: {}", err.to_string());
return;
}
add_source_fixups(&source);
let builder = xb::Builder::new();
builder.import_source(&source);
let silo = builder
.compile(xb::BuilderCompileFlags::NONE, Cancellable::NONE)
.unwrap();
let ref_ = format!("{}/{}/{}/{}", kind, name, arch, branch);
let ref_ = ref_.replace('/', r"\/");
let xpath = format!("components/component/bundle[text()='{ref_}']/..");
if let Ok(node) = silo.query_first(&xpath) {
let xml = node.export(xb::NodeExportFlags::NONE).unwrap().to_string();
let element = xmltree::Element::parse(xml.as_bytes()).unwrap();
if let Ok(component) = Component::try_from(&element) {
package.appstream_component = Some(serde_json::to_string(&component).unwrap()).into();
} else {
warn!("Couldn't find appstream component for {name}");
}
}
let icon_file = appstream_dir.child(format!("icons/128x128/{}.png", name));
if let Ok((bytes, _)) = icon_file.load_bytes(Cancellable::NONE) {
package.icon = Some(bytes.to_vec()).into();
}
}
fn add_source_fixups(source: &xb::BuilderSource) {
let fixup = xb::BuilderFixup::new("FixIdDesktopSuffix", |_, node, _| {
if node.element().is_some() {
let id = node.child("id", None);
let bundle = node.child("bundle", None);
if id.is_none() || bundle.is_none() {
return true;
}
let id = id.unwrap();
let bundle = bundle.unwrap();
let bundle_txt = bundle.text();
let split: Vec<&str> = bundle_txt.split('/').collect();
if split.len() != 4 {
return true;
}
let component_id = id.text();
let correct_id = split[1];
if component_id != correct_id {
debug!("Fix component id: {} -> {}", component_id, correct_id);
id.set_text(Some(correct_id));
let provides = match node.child("provides", None) {
Some(provides) => provides,
None => {
let n = xb::BuilderNode::new("provides");
node.add_child(&n);
n
}
};
if provides.child("id", Some(&component_id)).is_none() {
let id_node = xb::BuilderNode::new("id");
id_node.set_text(Some(&component_id));
provides.add_child(&id_node);
};
}
}
true
});
fixup.set_max_depth(2);
source.add_fixup(&fixup);
let fixup = xb::BuilderFixup::new("TextTokenize", |_, node, _| {
if let Some(element) = node.element() {
let tokens = vec!["id", "keyword", "launchable", "mimetype", "name", "summary"];
if tokens.contains(&element.as_str()) {
node.tokenize_text();
}
}
true
});
fixup.set_max_depth(2);
source.add_fixup(&fixup);
}