Show status when battery query fails.

When there is an issue with getting battery details, panel would not
show any icon nor tooltip to mark that status. This changes adds failing
icon and appropriate tooltip message.
This commit is contained in:
Sanel Zukan 2016-07-13 16:07:02 +02:00
parent 09039b46c9
commit cbcb0e2ce4
1 changed files with 28 additions and 19 deletions

View File

@ -1,7 +1,5 @@
/* /*
* $Id: ede-panel.cpp 3463 2012-12-17 15:49:33Z karijes $ * Copyright (C) 2013-2016 Sanel Zukan
*
* Copyright (C) 2013 Sanel Zukan
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License * modify it under the terms of the GNU General Public License
@ -59,9 +57,10 @@ EDELIB_NS_USING(EDBUS_SYSTEM)
#define UPOWER_INTERFACE "org.freedesktop.UPower.Device" #define UPOWER_INTERFACE "org.freedesktop.UPower.Device"
#define UPOWER_PATH "/org/freedesktop/UPower" #define UPOWER_PATH "/org/freedesktop/UPower"
#define BATTERY_MIN 10 /* minimal time before icon to BATTERY_CAUTION_IMG was changed */ #define BATTERY_MIN 10 /* minimal time before icon to BATTERY_CAUTION_IMG was changed */
#define BATTERY_IMG "battery" #define BATTERY_IMG "battery"
#define BATTERY_CAUTION_IMG "battery-caution" #define BATTERY_CAUTION_IMG "battery-caution"
#define BATTERY_NO_CONNECTION "dialog-warning"
typedef list<EdbusObjectPath> BatteryList; typedef list<EdbusObjectPath> BatteryList;
typedef list<EdbusObjectPath>::iterator BatteryListIt; typedef list<EdbusObjectPath>::iterator BatteryListIt;
@ -83,15 +82,18 @@ private:
public: public:
BatteryMonitor() : Fl_Box(0, 0, 30, 25), bimg(0) { BatteryMonitor() : Fl_Box(0, 0, 30, 25), bimg(0) {
box(FL_FLAT_BOX); box(FL_FLAT_BOX);
scan_and_init(); if(!scan_and_init()) {
set_icon(0, true);
tooltip(_("Failed to query battery status"));
}
} }
EdbusConnection &connection() { return con; } EdbusConnection &connection() { return con; }
void tooltip_printf(const char *fmt, ...); void tooltip_printf(const char *fmt, ...);
void scan_and_init(void); int scan_and_init(void);
int update_icon_and_tooltip(void); int update_icon_and_tooltip(void);
void set_icon(double percentage); void set_icon(double percentage, bool failure = false);
BatteryList &get_batteries(void); BatteryList &get_batteries(void);
}; };
@ -186,19 +188,19 @@ void BatteryMonitor::tooltip_printf(const char *fmt, ...) {
tooltip(tip); tooltip(tip);
} }
void BatteryMonitor::scan_and_init(void) { int BatteryMonitor::scan_and_init(void) {
if(con.connected()) return; if(con.connected()) return 1;
E_RETURN_IF_FAIL(con.connect(EDBUS_SYSTEM)); E_RETURN_VAL_IF_FAIL(con.connect(EDBUS_SYSTEM), 0);
/* get battery devices */ /* get battery devices */
EdbusMessage msg, reply; EdbusMessage msg, reply;
msg.create_method_call(UPOWER_SERVICE, UPOWER_PATH, UPOWER_SERVICE, "EnumerateDevices"); msg.create_method_call(UPOWER_SERVICE, UPOWER_PATH, UPOWER_SERVICE, "EnumerateDevices");
E_RETURN_IF_FAIL(con.send_with_reply_and_block(msg, 1000, reply)); E_RETURN_VAL_IF_FAIL(con.send_with_reply_and_block(msg, 1000, reply), 0);
E_RETURN_IF_FAIL(reply.size() == 1); E_RETURN_VAL_IF_FAIL(reply.size() == 1, 0);
EdbusMessage::const_iterator it = reply.begin(); EdbusMessage::const_iterator it = reply.begin();
E_RETURN_IF_FAIL(it->is_array()); E_RETURN_VAL_IF_FAIL(it->is_array(), 0);
EdbusList arr = it->to_array(); EdbusList arr = it->to_array();
it = arr.begin(); it = arr.begin();
@ -222,6 +224,7 @@ void BatteryMonitor::scan_and_init(void) {
/* ready to receive signals */ /* ready to receive signals */
con.signal_callback(signal_cb, this); con.signal_callback(signal_cb, this);
con.setup_listener_with_fltk(); con.setup_listener_with_fltk();
return 1;
} }
int BatteryMonitor::update_icon_and_tooltip(void) { int BatteryMonitor::update_icon_and_tooltip(void) {
@ -255,16 +258,22 @@ int BatteryMonitor::update_icon_and_tooltip(void) {
#define BATTERY_MIN 10 #define BATTERY_MIN 10
void BatteryMonitor::set_icon(double percentage) { void BatteryMonitor::set_icon(double percentage, bool failure) {
if(E_UNLIKELY(IconLoader::inited() == false)) { if(E_UNLIKELY(IconLoader::inited() == false)) {
char buf[8]; char buf[8];
snprintf(buf, sizeof(buf), "%i%%", (int)percentage); snprintf(buf, sizeof(buf), "%i%%", (int)percentage);
copy_label(buf); copy_label(buf);
return; return;
} }
const char *icon = (percentage >= BATTERY_MIN) ? BATTERY_IMG : BATTERY_CAUTION_IMG; const char *icon;
if(failure)
icon = BATTERY_NO_CONNECTION;
else
icon = (percentage >= BATTERY_MIN) ? BATTERY_IMG : BATTERY_CAUTION_IMG;
/* small check to prevent image loading when not needed */ /* small check to prevent image loading when not needed */
if(icon == bimg) return; if(icon == bimg) return;