From 3ff90e931ed14338383c727b27e408e09fdb8a4d Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Wed, 31 Jul 2019 12:17:19 -0600 Subject: [PATCH] fix(gtk): Only show battery message when a battery is detected Closes #23 --- Cargo.lock | 12 ++++++++++++ gtk/Cargo.toml | 1 + gtk/src/dialogs/fwupd.rs | 16 +++++++++++++--- gtk/src/dialogs/system76.rs | 4 ++-- gtk/src/dialogs/update.rs | 19 +++++++++++-------- gtk/src/state.rs | 13 +++++++++++-- 6 files changed, 50 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b68ff27..da1a272 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -593,6 +593,7 @@ dependencies = [ "html2runes 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "shrinkwraprs 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "slotmap 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "upower_dbus 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -2363,6 +2364,16 @@ dependencies = [ "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "upower_dbus" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "dbus 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "url" version = "1.7.2" @@ -2739,6 +2750,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum unreachable 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "1f2ae5ddb18e1c92664717616dd9549dde73f539f01bd7b77c2edb2446bdff91" +"checksum upower_dbus 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b8121b555fe6fd748fb4bb147f7b31b5ea17d006633183447e0d89e19d9aa9b7" "checksum url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a" "checksum utf-8 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9aee9ba280438b56d1ebc5329f2094f0ff457f811eeeff0b278d75aa99db400" "checksum utf8-ranges 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9d50aa7650df78abf942826607c62468ce18d9019673d4a2ebe1865dbb96ffde" diff --git a/gtk/Cargo.toml b/gtk/Cargo.toml index 9c4fee8..ed4de08 100644 --- a/gtk/Cargo.toml +++ b/gtk/Cargo.toml @@ -19,6 +19,7 @@ slotmap = "0.3" html2runes = "1.0" firmware-manager = { path = "../" } gdk = "0.11.0" +upower_dbus = "0.1.0" [target.'cfg(all(not(feature = "fwupd"), feature = "system76"))'.dependencies] firmware-manager = { path = "../", features = [ "system76" ] } diff --git a/gtk/src/dialogs/fwupd.rs b/gtk/src/dialogs/fwupd.rs index 783ee61..0a7b760 100644 --- a/gtk/src/dialogs/fwupd.rs +++ b/gtk/src/dialogs/fwupd.rs @@ -10,7 +10,12 @@ pub(crate) struct FwupdDialogData { pub shared: DialogData, } -pub(crate) fn fwupd_dialog(data: &FwupdDialogData, upgradeable: bool, upgrade_button: bool) { +pub(crate) fn fwupd_dialog( + data: &FwupdDialogData, + upgradeable: bool, + has_battery: bool, + upgrade_button: bool, +) { let &FwupdDialogData { entity, device, releases, shared } = &data; let &DialogData { sender, stack, progress, info } = &shared; @@ -22,8 +27,13 @@ pub(crate) fn fwupd_dialog(data: &FwupdDialogData, upgradeable: bool, upgrade_bu .rev() .map(|release| (release.version.as_ref(), release.description.as_ref())); - let dialog = - FirmwareUpdateDialog::new(latest, log_entries, upgradeable, device.needs_reboot()); + let dialog = FirmwareUpdateDialog::new( + latest, + log_entries, + upgradeable, + device.needs_reboot(), + has_battery, + ); let response = dialog.run(); dialog.destroy(); diff --git a/gtk/src/dialogs/system76.rs b/gtk/src/dialogs/system76.rs index ea1d20a..0e07161 100644 --- a/gtk/src/dialogs/system76.rs +++ b/gtk/src/dialogs/system76.rs @@ -11,7 +11,7 @@ pub(crate) struct System76DialogData { } #[cfg(feature = "system76")] -pub(crate) fn s76_system_dialog(data: &System76DialogData, upgradeable: bool) { +pub(crate) fn s76_system_dialog(data: &System76DialogData, upgradeable: bool, has_battery: bool) { let &System76DialogData { entity, digest, changelog, shared } = &data; let &DialogData { sender, stack, progress, info } = &shared; let &FirmwareInfo { latest, .. } = &info; @@ -20,7 +20,7 @@ pub(crate) fn s76_system_dialog(data: &System76DialogData, upgradeable: bool) { (version.bios.as_ref(), version.description.as_ref().map_or("", |desc| desc.as_ref())) }); - let dialog = FirmwareUpdateDialog::new(latest, log_entries, upgradeable, true); + let dialog = FirmwareUpdateDialog::new(latest, log_entries, upgradeable, true, has_battery); if gtk::ResponseType::Accept == dialog.run() { // Exchange the button for a progress bar. diff --git a/gtk/src/dialogs/update.rs b/gtk/src/dialogs/update.rs index 89e2276..62e54f4 100644 --- a/gtk/src/dialogs/update.rs +++ b/gtk/src/dialogs/update.rs @@ -9,6 +9,7 @@ impl FirmwareUpdateDialog { changelog: I, upgradeable: bool, needs_reboot: bool, + has_battery: bool, ) -> Self { let changelog_entries = crate::changelog::generate_widget(changelog, false); @@ -54,7 +55,7 @@ impl FirmwareUpdateDialog { }; cascade! { - dialog.get_content_area(); + content: dialog.get_content_area(); ..set_orientation(gtk::Orientation::Horizontal); ..set_border_width(12); ..set_spacing(12); @@ -82,13 +83,15 @@ impl FirmwareUpdateDialog { .build(); ..add(&changelog_entries); }); - ..add( - >k::LabelBuilder::new() - .label("If you're on a laptop, plug into power before you begin.") - .use_markup(true) - .xalign(0.0) - .build() - ); + | if has_battery { + content.add( + >k::LabelBuilder::new() + .label("If you're on a laptop, plug into power before you begin.") + .use_markup(true) + .xalign(0.0) + .build() + ); + }; }); }; diff --git a/gtk/src/state.rs b/gtk/src/state.rs index c0a6047..cebd157 100644 --- a/gtk/src/state.rs +++ b/gtk/src/state.rs @@ -16,6 +16,8 @@ pub(crate) struct State { pub(crate) components: Components, /// All devices will be created as an entity here pub(crate) entities: Entities, + /// If this system has a battery. + pub(crate) has_battery: bool, /// Sends events to the progress signal pub(crate) progress_sender: Sender, /// A sender to send firmware requests to the background thread @@ -68,6 +70,9 @@ impl State { Self { entities: Entities::default(), components: Components::default(), + has_battery: upower_dbus::UPower::new(-1) + .map(|upower| upower.on_battery().is_ok()) + .unwrap_or(false), progress_sender, sender, #[cfg(feature = "system76")] @@ -124,6 +129,7 @@ impl State { pub fn fwupd(&mut self, signal: FwupdSignal) { let FwupdSignal { info, device, upgradeable, releases } = signal; let entity = self.entities.create(); + let has_battery = self.has_battery; let widget = if device.needs_reboot() { self.entities.associate_system(entity); @@ -149,7 +155,9 @@ impl State { if upgradeable.get() { let data = Rc::clone(&data); let upgradeable = Rc::clone(&upgradeable); - widget.connect_upgrade_clicked(move || fwupd_dialog(&data, upgradeable.get(), true)); + widget.connect_upgrade_clicked(move || { + fwupd_dialog(&data, upgradeable.get(), has_battery, true) + }); } else { widget.stack.set_visible(false); } @@ -186,6 +194,7 @@ impl State { let entity = self.entities.create(); self.entities.associate_system(entity); let upgradeable = info.current != info.latest; + let has_battery = self.has_battery; let data = Rc::new(System76DialogData { entity, @@ -205,7 +214,7 @@ impl State { let data = Rc::clone(&data); let upgradeable = Rc::clone(&upgradeable); widget.connect_upgrade_clicked(move || { - s76_system_dialog(&data, upgradeable.get()); + s76_system_dialog(&data, upgradeable.get(), has_battery); }); } else { widget.stack.set_visible(false);