Repository navigation
Expand file tree
/
Copy pathlauncher.rs
More file actions
254 lines (227 loc) · 9.04 KB
/
Copy pathlauncher.rs
File metadata and controls
254 lines (227 loc) · 9.04 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
use crate::{
browser::{Browser, BrowserModel, Chromium, Falkon, Firefox},
common::{self, database_path, desktop_files_location},
pages::editor::Category,
LOCALES,
};
use anyhow::Result;
use freedesktop_desktop_entry::DesktopEntry;
use serde::{Deserialize, Serialize};
use tracing::debug;
use std::{
fs::{self},
io::Read,
};
use tokio::{
fs::{remove_dir_all, remove_file, File},
io::AsyncWriteExt,
};
pub fn webapplauncher_is_valid(icon: &str, name: &str, url: &str) -> bool {
if !common::url_valid(url) || name.is_empty() || icon.is_empty() || url.is_empty() {
return false;
}
true
}
pub fn installed_webapps() -> Vec<WebAppLauncher> {
let mut webapps = Vec::new();
if let Ok(entries) = fs::read_dir(database_path("")) {
for entry in entries {
match entry {
Ok(entry) => {
let file = std::fs::File::open(entry.path());
let mut content = String::new();
if let Ok(mut f) = file {
f.read_to_string(&mut content).unwrap();
if let Ok(mut launcher) = ron::from_str::<WebAppLauncher>(&content) {
launcher.browser = Browser::from_appid(launcher.appid.clone());
webapps.push(launcher);
}
}
}
Err(e) => tracing::error!("Error reading directory: {}", e),
}
}
}
webapps
}
#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct WebAppLauncher {
pub appid: String,
pub codename: String,
pub browser: Browser,
pub name: String,
pub icon: String,
pub category: Category,
pub url: String,
pub custom_parameters: String,
pub isolate_profile: bool,
pub navbar: bool,
pub is_incognito: bool,
}
impl From<DesktopEntry> for WebAppLauncher {
fn from(value: DesktopEntry) -> Self {
let group = value.groups.group("Desktop Entry");
match group {
Some(group) => Self {
appid: group
.entry("X-QWA-Browser-Id")
.unwrap_or_default()
.to_string(),
codename: group
.entry("X-QWA-Codename")
.unwrap_or_default()
.to_string(),
browser: Browser::from_appid(
group
.entry("X-QWA-Browser-Id")
.unwrap_or_default()
.to_string(),
),
name: value.name(&LOCALES).unwrap_or_default().to_string(),
icon: value.icon().unwrap_or_default().to_string(),
category: Category::from(value.categories().unwrap_or_default().concat()),
url: group.entry("X-QWA-Url").unwrap_or_default().to_string(),
custom_parameters: group
.entry("X-QWA-Parameters")
.unwrap_or_default()
.to_string(),
isolate_profile: group
.entry("X-QWA-Isolated")
.unwrap_or_default()
.parse::<bool>()
.unwrap_or_default(),
navbar: group
.entry("X-QWA-Navbar")
.unwrap_or_default()
.parse::<bool>()
.unwrap_or_default(),
is_incognito: group
.entry("X-QWA-Private")
.unwrap_or_default()
.parse::<bool>()
.unwrap_or_default(),
},
None => Self {
appid: String::new(),
codename: String::new(),
browser: Browser::default(),
name: String::new(),
icon: String::new(),
category: Category::default(),
url: String::new(),
custom_parameters: String::new(),
isolate_profile: false,
navbar: false,
is_incognito: false,
},
}
}
}
impl WebAppLauncher {
fn exec_firefox(&self, zen_browser: bool) -> String {
let profile_path = self.browser.profile_path.join(&self.codename);
Firefox::builder(self.browser.exec.clone())
.url(self.url.clone())
.codename(self.codename.clone())
.navbar(self.navbar)
.isolated(self.isolate_profile)
.profile_path(profile_path)
.zen_browser(zen_browser)
.private_mode(self.is_incognito)
.custom_args(self.custom_parameters.clone())
.build()
}
fn exec_chromium(&self, microsoft_edge: bool) -> String {
let profile_dir = self.browser.profile_path.join(&self.codename);
Chromium::builder(self.browser.exec.clone())
.url(self.url.clone())
.codename(self.codename.clone())
.isolated(self.isolate_profile)
.profile_path(profile_dir)
.ms_edge(microsoft_edge)
.private_mode(self.is_incognito)
.custom_args(self.custom_parameters.clone())
.build()
}
fn exec_falkon(&self) -> String {
let profile_dir = self.browser.profile_path.join(&self.codename);
Falkon::builder(self.browser.exec.clone())
.url(self.url.clone())
.codename(self.codename.clone())
.isolated(self.isolate_profile)
.profile_path(profile_dir)
.private_mode(self.is_incognito)
.custom_args(self.custom_parameters.clone())
.build()
}
fn exec_string(&self) -> String {
if let Some(model) = &self.browser.model {
return match model {
BrowserModel::Brave => self.exec_chromium(false),
BrowserModel::Chrome => self.exec_chromium(false),
BrowserModel::Chromium => self.exec_chromium(false),
BrowserModel::Cromite => self.exec_chromium(false),
BrowserModel::Falkon => self.exec_falkon(),
BrowserModel::Firefox => self.exec_firefox(false),
BrowserModel::Floorp => self.exec_firefox(false),
BrowserModel::Librewolf => self.exec_firefox(false),
BrowserModel::MicrosoftEdge => self.exec_chromium(true),
BrowserModel::Vivaldi => self.exec_chromium(false),
BrowserModel::Waterfox => self.exec_firefox(false),
BrowserModel::Zen => self.exec_firefox(true),
};
};
String::new()
}
pub async fn create(&self) -> Result<()> {
debug!("create {:?}", self);
let entry_location = desktop_files_location(&self.codename);
if entry_location.exists() {
let _ = std::fs::remove_file(&entry_location);
}
let mut desktop_entry = String::from("[Desktop Entry]\n");
desktop_entry.push_str(&format!("Name={}\n", self.name));
desktop_entry.push_str("Comment=Quick Web App\n");
desktop_entry.push_str(&format!("Exec={}\n", self.exec_string()));
desktop_entry.push_str(&format!("Icon={}\n", self.icon));
desktop_entry.push_str("Terminal=false\n");
desktop_entry.push_str("Type=Application\n");
desktop_entry.push_str(&format!("Categories={}\n", self.category.as_ref()));
desktop_entry.push_str("MimeType=text/html;text/xml;application/xhtml_xml;\n");
desktop_entry.push_str(&format!(
"StartupWMClass=dev.heppen.webapps.{}\n",
self.codename
));
desktop_entry.push_str("StartupNotify=true\n");
desktop_entry.push_str(&format!("X-QWA-Codename={}\n", self.codename));
desktop_entry.push_str(&format!("X-QWA-Browser-Id={}\n", self.appid));
desktop_entry.push_str(&format!("X-QWA-Url={}\n", self.url));
desktop_entry.push_str(&format!("X-QWA-Navbar={}\n", self.navbar));
desktop_entry.push_str(&format!("X-QWA-Private={}\n", self.is_incognito));
desktop_entry.push_str(&format!("X-QWA-Isolated={}\n", self.isolate_profile));
desktop_entry.push_str(&format!("X-QWA-Parameters={}\n", self.custom_parameters));
if let Ok(mut f) = File::create(entry_location).await {
f.write_all(desktop_entry.as_bytes()).await?;
}
Ok(())
}
pub async fn delete(&self) -> Result<()> {
let profile_path = self.browser.profile_path.join(&self.codename);
remove_file(desktop_files_location(&self.codename)).await?;
remove_dir_all(&profile_path).await?;
remove_file(database_path(&format!("{}.ron", self.codename))).await?;
Ok(())
}
}
//pub async fn launch_webapp(app_id: Arc<String>) -> anyhow::Result<()> {
// let proxy = DynamicLauncherProxy::new().await?;
//
// proxy
// .launch(
// &format!("dev.heppen.webapps.{}.desktop", app_id),
// ashpd::desktop::dynamic_launcher::LaunchOptions::default(),
// )
// .await?;
//
// Ok(())
//}