BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / linux_install.rs

linux_install.rs

linux_install.rs 8.9 KiB Download Raw
1//! Linux desktop integration on start: offer to install the binary into
2//! `~/.local/share/applications/teams` with a menu entry, and fetch the Teams icon
3//! from Microsoft (it is not shipped with the app).
4
5use std::collections::HashMap;
6use std::path::{Path, PathBuf};
7
8use app_base::linux_desktop::{self, Layout};
9use gpui_kit::component::button::{Button, ButtonVariants as _};
10use gpui_kit::component::dialog::{Dialog, DialogFooter};
11use gpui_kit::component::{WindowExt as _, v_flex};
12use gpui_kit::*;
13
14use crate::app_state::AppHandle;
15use crate::appearance::AppIcon;
16use crate::i18n::{tr, tr_args};
17use crate::theme;
18
19const DIALOG_WIDTH: f32 = 460.;
20
21/// Loads an icon a previous run downloaded, before the first window picks its icon.
22pub fn load_icon() {
23 if let Some(layout) = Layout::current() {
24 linux_desktop::load_teams_icon(&layout);
25 }
26}
27
28pub fn start(cx: &mut App) {
29 let (Ok(executable), Some(layout)) = (std::env::current_exe(), Layout::current()) else {
30 return;
31 };
32 if !linux_desktop::has_icon(&layout) {
33 download_icon(layout.clone(), cx);
34 }
35 if linux_desktop::is_installed(&executable, &layout) {
36 // Keeps the menu entry pointing at this file, e.g. after it was deleted by hand.
37 if !layout.desktop_file().is_file()
38 && linux_desktop::write_desktop_entry(&layout, &executable).is_ok()
39 {
40 linux_desktop::refresh_desktop_caches(&layout);
41 }
42 return;
43 }
44 let dont_ask = cx
45 .try_global::<AppHandle>()
46 .is_some_and(|handle| linux_desktop::dont_ask_again(&handle.0.read(cx).store));
47 if linux_desktop::should_offer_install(&executable, &layout, dont_ask) {
48 // After the shell's first frame, so the dialog sits on top of the splash.
49 cx.defer(move |cx| offer_install(executable, layout, cx));
50 }
51}
52
53fn download_icon(layout: Layout, cx: &mut App) {
54 let download = linux_desktop::download_icon(layout, cx);
55 cx.spawn(async move |cx| match download.await {
56 Ok(png) => {
57 linux_desktop::set_teams_icon(png);
58 cx.update(|cx| {
59 // First run: the Teams icon becomes the default unless one was chosen.
60 if let Some(store) = cx
61 .try_global::<AppHandle>()
62 .map(|handle| handle.0.read(cx).store.clone())
63 && !crate::appearance::icon_saved(&store)
64 {
65 crate::active_appearance::set_app_icon(AppIcon::MicrosoftTeams, &store, cx);
66 }
67 crate::views::shell::refresh_app_icon(cx);
68 });
69 }
70 Err(error) => log::warn!("Teams icon download failed: {error}"),
71 })
72 .detach();
73}
74
75fn offer_install(executable: PathBuf, layout: Layout, cx: &mut App) {
76 let Some(window) = crate::main_window::current(cx) else {
77 return;
78 };
79 let folder = layout.install_dir();
80 let mut args = HashMap::new();
81 args.insert("folder".to_owned(), display_home(&folder));
82 let body = tr_args("install.body", &args);
83 window
84 .update(cx, move |_, window, cx| {
85 window.open_dialog(cx, move |dialog: Dialog, _, _| {
86 let (install_from, install_layout) = (executable.clone(), layout.clone());
87 let footer = DialogFooter::new()
88 .child(
89 Button::new("install-dont-ask")
90 .label(tr("install.dont_ask"))
91 .on_click(|_, window, cx| {
92 if let Some(handle) = cx.try_global::<AppHandle>() {
93 linux_desktop::set_dont_ask_again(&handle.0.read(cx).store);
94 }
95 window.close_dialog(cx);
96 }),
97 )
98 .child(
99 Button::new("install-not-now")
100 .label(tr("install.not_now"))
101 .on_click(|_, window, cx| window.close_dialog(cx)),
102 )
103 .child(
104 Button::new("install-move")
105 .primary()
106 .label(tr("install.move"))
107 .on_click(move |_, window, cx| {
108 window.close_dialog(cx);
109 install(&install_from, &install_layout, cx);
110 }),
111 );
112 dialog
113 .title(tr("install.title"))
114 .w(px(DIALOG_WIDTH))
115 .child(
116 v_flex()
117 .gap(px(8.))
118 .text_color(theme::text())
119 .child(body.clone()),
120 )
121 .footer(footer)
122 });
123 })
124 .ok();
125}
126
127fn install(executable: &Path, layout: &Layout, cx: &mut App) {
128 match linux_desktop::install_binary(executable, layout) {
129 Ok(installed) => relaunch(&installed, cx),
130 Err(error) => {
131 log::warn!("install failed: {error}");
132 notice(failure("install.failed", &error.to_string()), cx);
133 }
134 }
135}
136
137fn failure(key: &str, detail: &str) -> String {
138 let mut args = HashMap::new();
139 args.insert("detail".to_owned(), detail.to_owned());
140 tr_args(key, &args)
141}
142
143fn notice(text: String, cx: &mut App) {
144 if let Some(handle) = cx.try_global::<AppHandle>() {
145 handle
146 .0
147 .clone()
148 .update(cx, |state, cx| state.raise_notice(text, None, cx));
149 }
150}
151
152/// Where this process stands relative to `~/.local/share/applications/teams`.
153#[derive(Debug, Clone, Copy, PartialEq, Eq)]
154pub enum Installation {
155 /// This process is the installed copy.
156 Running,
157 /// Another binary is installed; this one runs from elsewhere.
158 OtherCopy,
159 NotInstalled,
160}
161
162pub fn installation() -> Installation {
163 let (Ok(executable), Some(layout)) = (std::env::current_exe(), Layout::current()) else {
164 return Installation::NotInstalled;
165 };
166 if linux_desktop::is_installed(&executable, &layout) {
167 Installation::Running
168 } else if layout.installed_binary().is_file() {
169 Installation::OtherCopy
170 } else {
171 Installation::NotInstalled
172 }
173}
174
175pub fn install_folder() -> String {
176 Layout::current()
177 .map(|layout| display_home(&layout.install_dir()))
178 .unwrap_or_default()
179}
180
181/// Settings > General: installs (or replaces the installed copy with) this binary.
182pub fn install_now(cx: &mut App) {
183 let (Ok(executable), Some(layout)) = (std::env::current_exe(), Layout::current()) else {
184 return;
185 };
186 if let Some(handle) = cx.try_global::<AppHandle>() {
187 linux_desktop::ask_again(&handle.0.read(cx).store);
188 }
189 install(&executable, &layout, cx);
190}
191
192/// Settings > General: removes the menu entry and the installed binary.
193pub fn remove(cx: &mut App) {
194 let Some(layout) = Layout::current() else {
195 return;
196 };
197 match linux_desktop::uninstall(&layout) {
198 Ok(()) => notice(tr("install.removed"), cx),
199 Err(error) => notice(failure("install.remove_failed", &error.to_string()), cx),
200 }
201 cx.refresh_windows();
202}
203
204pub fn asks_on_start(cx: &App) -> bool {
205 cx.try_global::<AppHandle>()
206 .is_some_and(|handle| !linux_desktop::dont_ask_again(&handle.0.read(cx).store))
207}
208
209pub fn set_asks_on_start(ask: bool, cx: &mut App) {
210 if let Some(handle) = cx.try_global::<AppHandle>() {
211 let store = &handle.0.read(cx).store;
212 if ask {
213 linux_desktop::ask_again(store);
214 } else {
215 linux_desktop::set_dont_ask_again(store);
216 }
217 }
218 cx.refresh_windows();
219}
220
221/// The release this build counts as: its tag in CI release builds, the workspace
222/// version (kept equal to the latest release) in a local build.
223pub fn running_release() -> &'static str {
224 app_base::version::release_tag().unwrap_or(concat!("v", env!("CARGO_PKG_VERSION")))
225}
226
227/// The release to update from, when this process is the installed copy.
228pub fn updatable_release() -> Option<&'static str> {
229 crate::updater::self_update_allowed().then(running_release)
230}
231
232/// Starts the installed copy and quits; the new process waits for this one to let go
233/// of the single-instance socket.
234fn relaunch(installed: &Path, cx: &mut App) {
235 let started = std::process::Command::new(installed)
236 .env(crate::updater::RELAUNCH_ENV, std::process::id().to_string())
237 .spawn();
238 match started {
239 Ok(_) => cx.quit(),
240 Err(error) => log::warn!("could not start {}: {error}", installed.display()),
241 }
242}
243
244fn display_home(path: &Path) -> String {
245 match directories::BaseDirs::new().and_then(|base| {
246 path.strip_prefix(base.home_dir())
247 .ok()
248 .map(Path::to_path_buf)
249 }) {
250 Some(relative) => format!("~/{}", relative.display()),
251 None => path.display().to_string(),
252 }
253}