mirror of
https://github.com/narrrl/proton-drive-linux.git
synced 2026-11-03 16:41:26 +01:00
feat(gui,core): opt-in check for new releases
Preferences gets a switch that asks GitHub once a day for the latest release and shows a banner over the sidebar when it is newer. Off by default. Packages whose package manager brings updates install the marker share/proton-drive-linux/managed-updates, which hides the switch. Refs #42
This commit is contained in:
1 parent
29b87ece72
commit
e66308498b
25 files changed
+2270
-1569
No files matched your search
@@ -10,6 +10,7 @@ pub(crate) mod pages;
|
||||
pub(crate) mod reload;
|
||||
#[path = "../theme.rs"]
|
||||
pub(crate) mod theme;
|
||||
pub(crate) mod update;
|
||||
pub(crate) mod widgets;
|
||||
|
||||
use compat::*;
|
||||
@@ -33,6 +34,7 @@ use pages::trash::*;
|
||||
use pages::verify::*;
|
||||
use reload::{LoadTicket, Loader, replace_items};
|
||||
use theme::{load_resources, proton_theme_active, set_proton_theme};
|
||||
use update::*;
|
||||
use widgets::compat::*;
|
||||
use widgets::crumb::*;
|
||||
use widgets::details::*;
|
||||
@@ -155,6 +157,7 @@ struct Ui {
|
||||
pub(crate) locations: LocationsState,
|
||||
pub(crate) activity: ActivityState,
|
||||
pub(crate) takeout: TakeoutState,
|
||||
pub(crate) update: UpdateState,
|
||||
}
|
||||
|
||||
impl Ui {
|
||||
@@ -365,7 +368,8 @@ fn build_window(app: &adw::Application) {
|
||||
// Sidebar: the signed-in destinations. Selecting a row swaps the page stack;
|
||||
// `sync_sidebar` pushes the other way when navigation happens elsewhere (e.g.
|
||||
// login lands on Files).
|
||||
let (sidebar_page, sidebar_list) = build_sidebar(&main_widgets.footer);
|
||||
let update = UpdateState::new();
|
||||
let (sidebar_page, sidebar_list) = build_sidebar(&main_widgets.footer, &update.banner);
|
||||
|
||||
// Every page brings its own header bar (see `page_frame`), so the content
|
||||
// side is the bare stack.
|
||||
@@ -432,6 +436,7 @@ fn build_window(app: &adw::Application) {
|
||||
theme_row: main_widgets.theme_row.clone(),
|
||||
online_map_row: main_widgets.online_map_row.clone(),
|
||||
tray_row: main_widgets.tray_row.clone(),
|
||||
update_row: main_widgets.update_row.clone(),
|
||||
language_row: main_widgets.language_row.clone(),
|
||||
settings_suppress: Cell::new(false),
|
||||
budget_source: RefCell::new(None),
|
||||
@@ -654,6 +659,7 @@ fn build_window(app: &adw::Application) {
|
||||
running: Cell::new(false),
|
||||
dry_run: Cell::new(false),
|
||||
},
|
||||
update,
|
||||
});
|
||||
wire_login(&ui);
|
||||
if let Some(e) = keyring_error {
|
||||
@@ -700,6 +706,7 @@ fn build_window(app: &adw::Application) {
|
||||
wire_activity(&ui, &activity_widgets);
|
||||
wire_takeout(&ui, &takeout_widgets);
|
||||
wire_retry(&ui);
|
||||
wire_update(&ui);
|
||||
|
||||
// Lazily load the Files / Photos pages the first time they're shown, so the
|
||||
// network round-trip only happens on demand rather than on every refresh.
|
||||
@@ -871,10 +878,13 @@ const DESTINATIONS: [(&str, &str, &str); 7] = [
|
||||
/// Index of the first row in the sidebar's sync section.
|
||||
const SIDEBAR_SYNC_SECTION: i32 = 6;
|
||||
|
||||
/// The navigation sidebar: the destinations over a footer carrying the sync
|
||||
/// status, the account quota and the account menu. Returns the page (for the
|
||||
/// The navigation sidebar: the destinations under the update banner and over a
|
||||
/// footer carrying the sync status, the account quota and the account menu. Returns the page (for the
|
||||
/// split view) and the list (to drive + reflect the current page).
|
||||
fn build_sidebar(footer: >k4::Box) -> (adw::NavigationPage, gtk4::ListBox) {
|
||||
fn build_sidebar(
|
||||
footer: >k4::Box,
|
||||
update_banner: &adw::Banner,
|
||||
) -> (adw::NavigationPage, gtk4::ListBox) {
|
||||
let list = gtk4::ListBox::new();
|
||||
list.set_selection_mode(gtk4::SelectionMode::Single);
|
||||
list.add_css_class("navigation-sidebar");
|
||||
@@ -927,6 +937,7 @@ fn build_sidebar(footer: >k4::Box) -> (adw::NavigationPage, gtk4::ListBox) {
|
||||
.build();
|
||||
let toolbar = adw::ToolbarView::new();
|
||||
toolbar.add_top_bar(&header);
|
||||
toolbar.add_top_bar(update_banner);
|
||||
toolbar.set_content(Some(&scroll));
|
||||
toolbar.add_bottom_bar(footer);
|
||||
|
||||
|
||||
@@ -52,6 +52,9 @@ pub(crate) struct StatusState {
|
||||
pub(crate) online_map_row: adw::SwitchRow,
|
||||
/// "Show tray icon" toggle. Guarded by [`Self::settings_suppress`].
|
||||
pub(crate) tray_row: adw::SwitchRow,
|
||||
/// "Check for updates" toggle, not shown where a package manager brings
|
||||
/// updates. Guarded by [`Self::settings_suppress`].
|
||||
pub(crate) update_row: adw::SwitchRow,
|
||||
/// Language picker: "System Default", then [`i18n::LANGUAGES`]. Guarded by
|
||||
/// [`Self::settings_suppress`].
|
||||
pub(crate) language_row: adw::ComboRow,
|
||||
@@ -164,6 +167,7 @@ pub(crate) struct MainWidgets {
|
||||
pub(crate) theme_row: adw::SwitchRow,
|
||||
pub(crate) online_map_row: adw::SwitchRow,
|
||||
pub(crate) tray_row: adw::SwitchRow,
|
||||
pub(crate) update_row: adw::SwitchRow,
|
||||
pub(crate) language_row: adw::ComboRow,
|
||||
}
|
||||
|
||||
@@ -250,6 +254,17 @@ pub(crate) fn build_main_page() -> MainWidgets {
|
||||
let language_row = build_language_row();
|
||||
appearance_group.add(&language_row);
|
||||
|
||||
let updates_group = adw::PreferencesGroup::builder()
|
||||
.title(gettext("Updates"))
|
||||
.build();
|
||||
let update_row = adw::SwitchRow::builder()
|
||||
.title(gettext("Check for updates"))
|
||||
.subtitle(gettext(
|
||||
"Ask GitHub once a day whether a new version is out. GitHub learns your IP address and which version you use.",
|
||||
))
|
||||
.build();
|
||||
updates_group.add(&update_row);
|
||||
|
||||
let general = adw::PreferencesPage::builder()
|
||||
.title(gettext("General"))
|
||||
.icon_name("preferences-system-symbolic")
|
||||
@@ -258,6 +273,10 @@ pub(crate) fn build_main_page() -> MainWidgets {
|
||||
general.add(&location_group);
|
||||
general.add(&network_group);
|
||||
general.add(&appearance_group);
|
||||
// A package manager brings new versions, so there is nothing to tell.
|
||||
if !updates_managed_here() {
|
||||
general.add(&updates_group);
|
||||
}
|
||||
|
||||
// ---- Preferences: Storage
|
||||
let storage_group = adw::PreferencesGroup::builder()
|
||||
@@ -442,6 +461,7 @@ pub(crate) fn build_main_page() -> MainWidgets {
|
||||
theme_row,
|
||||
online_map_row,
|
||||
tray_row,
|
||||
update_row,
|
||||
language_row,
|
||||
}
|
||||
}
|
||||
@@ -541,6 +561,7 @@ pub(crate) fn wire_settings(
|
||||
.set_active(config.proton_theme.unwrap_or(false));
|
||||
ui.status.online_map_row.set_active(config.online_map);
|
||||
ui.status.tray_row.set_active(!config.tray_hidden);
|
||||
ui.status.update_row.set_active(config.update_check);
|
||||
ui.status
|
||||
.language_row
|
||||
.set_selected(language_index(config.language.as_deref()));
|
||||
@@ -690,6 +711,26 @@ pub(crate) fn wire_settings(
|
||||
}
|
||||
});
|
||||
|
||||
// Update check: saved, and the first check runs right away if one is due.
|
||||
let ui_update = ui.clone();
|
||||
ui.status.update_row.connect_active_notify(move |row| {
|
||||
if ui_update.status.settings_suppress.get() {
|
||||
return;
|
||||
}
|
||||
let mut config = ui_update.dirs.load_config();
|
||||
config.update_check = row.is_active();
|
||||
if let Err(e) = ui_update.dirs.save_config(&config) {
|
||||
toast_error(
|
||||
&ui_update,
|
||||
&gettext("Couldn't save the update setting"),
|
||||
&e.to_string(),
|
||||
);
|
||||
return;
|
||||
}
|
||||
show_latest(&ui_update);
|
||||
start_update_checks(&ui_update);
|
||||
});
|
||||
|
||||
// Tray icon: the tray reads the same flag at start, so hiding it here also
|
||||
// keeps the login autostart from bringing it back.
|
||||
let ui_tray = ui.clone();
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
//! Tells the user when a newer release is out, if they turned on Preferences →
|
||||
//! Updates. The decisions are [`pdfs_core::update`]'s; this asks GitHub and
|
||||
//! shows the banner.
|
||||
|
||||
use crate::*;
|
||||
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use pdfs_core::update::{
|
||||
RELEASES_LATEST_API, is_newer, latest_version, next_check_in, release_url, updates_managed,
|
||||
};
|
||||
|
||||
/// How long one request to GitHub may take.
|
||||
const FETCH_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
|
||||
/// How long to wait after a failed check, such as one made offline, before
|
||||
/// trying again. Not saved, so a restart tries again straight away.
|
||||
const RETRY_AFTER: Duration = Duration::from_secs(60 * 60);
|
||||
|
||||
pub(crate) struct UpdateState {
|
||||
/// Over the sidebar, so it shows whichever page is open.
|
||||
pub(crate) banner: adw::Banner,
|
||||
/// The check loop is running. Turning the setting off and on again while it
|
||||
/// sleeps must not start a second one.
|
||||
running: Cell<bool>,
|
||||
/// The release page the banner's button opens.
|
||||
url: RefCell<String>,
|
||||
}
|
||||
|
||||
impl UpdateState {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self {
|
||||
banner: adw::Banner::builder()
|
||||
.button_label(gettext("Download"))
|
||||
.build(),
|
||||
running: Cell::new(false),
|
||||
url: RefCell::new(String::new()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a package manager brings new releases to this install. The app then
|
||||
/// never asks, and Preferences does not offer to.
|
||||
pub(crate) fn updates_managed_here() -> bool {
|
||||
std::env::current_exe().is_ok_and(|exe| updates_managed(&exe))
|
||||
}
|
||||
|
||||
/// Wire the banner, show a release an earlier check found, and start checking
|
||||
/// if the user asked for it.
|
||||
pub(crate) fn wire_update(ui: &Rc<Ui>) {
|
||||
let ui_open = ui.clone();
|
||||
ui.update
|
||||
.banner
|
||||
.connect_button_clicked(move |_| open_uri(&ui_open.update.url.borrow()));
|
||||
show_latest(ui);
|
||||
start_update_checks(ui);
|
||||
}
|
||||
|
||||
/// Start the daily check, unless it is off, managed elsewhere, or running.
|
||||
pub(crate) fn start_update_checks(ui: &Rc<Ui>) {
|
||||
if updates_managed_here()
|
||||
|| !ui.dirs.load_config().update_check
|
||||
|| ui.update.running.replace(true)
|
||||
{
|
||||
return;
|
||||
}
|
||||
let ui = ui.clone();
|
||||
glib::spawn_future_local(async move {
|
||||
// Also kept here, so a config that cannot be saved does not make every
|
||||
// pass ask GitHub again.
|
||||
let mut checked = None;
|
||||
loop {
|
||||
let config = ui.dirs.load_config();
|
||||
if !config.update_check {
|
||||
break;
|
||||
}
|
||||
let wait = next_check_in(config.update_last_checked.max(checked), unix_now());
|
||||
if !wait.is_zero() {
|
||||
glib::timeout_future(wait).await;
|
||||
continue;
|
||||
}
|
||||
match fetch_latest().await {
|
||||
Ok(latest) => {
|
||||
checked = Some(unix_now());
|
||||
let mut config = ui.dirs.load_config();
|
||||
config.update_last_checked = checked;
|
||||
config.update_latest = latest;
|
||||
if let Err(e) = ui.dirs.save_config(&config) {
|
||||
tracing::warn!("update check: cannot save the result: {e}");
|
||||
}
|
||||
show_latest(&ui);
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!("update check: {e}");
|
||||
glib::timeout_future(RETRY_AFTER).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
ui.update.running.set(false);
|
||||
});
|
||||
}
|
||||
|
||||
/// Reveal the banner if the last check found a release newer than this one,
|
||||
/// and hide it otherwise.
|
||||
pub(crate) fn show_latest(ui: &Ui) {
|
||||
let config = ui.dirs.load_config();
|
||||
let newer = config
|
||||
.update_latest
|
||||
.filter(|latest| config.update_check && is_newer(latest, env!("CARGO_PKG_VERSION")));
|
||||
match newer {
|
||||
Some(version) => {
|
||||
ui.update.banner.set_title(&gettext_f(
|
||||
"Proton Drive {version} is available",
|
||||
&[("version", &version)],
|
||||
));
|
||||
*ui.update.url.borrow_mut() = release_url(&version);
|
||||
ui.update.banner.set_revealed(!updates_managed_here());
|
||||
}
|
||||
None => ui.update.banner.set_revealed(false),
|
||||
}
|
||||
}
|
||||
|
||||
fn unix_now() -> i64 {
|
||||
SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map_or(0, |d| d.as_secs() as i64)
|
||||
}
|
||||
|
||||
/// The latest release's version, asked on a worker thread so the window never
|
||||
/// waits on GitHub. `Ok(None)` when the answer names no `X.Y.Z` release.
|
||||
async fn fetch_latest() -> Result<Option<String>, String> {
|
||||
let (tx, rx) = async_channel::bounded(1);
|
||||
std::thread::spawn(move || {
|
||||
let _ = tx.send_blocking(fetch_latest_blocking());
|
||||
});
|
||||
rx.recv().await.map_err(|e| e.to_string())?
|
||||
}
|
||||
|
||||
fn fetch_latest_blocking() -> Result<Option<String>, String> {
|
||||
let runtime = tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.build()
|
||||
.map_err(|e| format!("no runtime: {e}"))?;
|
||||
let client = reqwest::Client::builder()
|
||||
.user_agent(concat!(
|
||||
"proton-drive-linux/",
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
" (",
|
||||
env!("CARGO_PKG_REPOSITORY"),
|
||||
")"
|
||||
))
|
||||
.timeout(FETCH_TIMEOUT)
|
||||
.build()
|
||||
.map_err(|e| format!("no HTTP client: {e}"))?;
|
||||
runtime.block_on(async {
|
||||
let reply = client
|
||||
.get(RELEASES_LATEST_API)
|
||||
.header("Accept", "application/vnd.github+json")
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
if !reply.status().is_success() {
|
||||
return Err(format!("GitHub answered {}", reply.status()));
|
||||
}
|
||||
let body = reply.bytes().await.map_err(|e| e.to_string())?;
|
||||
Ok(latest_version(&body))
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user