@@ -22,6 +22,7 @@ UPower::UPower(const std::string& id, const Json::Value& config)
22
22
showAltText(false ) {
23
23
box_.pack_start (icon_);
24
24
box_.pack_start (label_);
25
+ box_.set_name (name_);
25
26
event_box_.add (box_);
26
27
27
28
// Icon Size
@@ -67,6 +68,14 @@ UPower::UPower(const std::string& id, const Json::Value& config)
67
68
box_.signal_query_tooltip ().connect (sigc::mem_fun (*this , &UPower::show_tooltip_callback));
68
69
}
69
70
71
+ upowerWatcher_id = g_bus_watch_name (G_BUS_TYPE_SYSTEM,
72
+ " org.freedesktop.UPower" ,
73
+ G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
74
+ upowerAppear,
75
+ upowerDisappear,
76
+ this ,
77
+ NULL );
78
+
70
79
GError* error = NULL ;
71
80
client = up_client_new_full (NULL , &error);
72
81
if (client == NULL ) {
@@ -105,6 +114,7 @@ UPower::~UPower() {
105
114
g_dbus_connection_signal_unsubscribe (login1_connection, login1_id);
106
115
login1_id = 0 ;
107
116
}
117
+ g_bus_unwatch_name (upowerWatcher_id);
108
118
removeDevices ();
109
119
}
110
120
@@ -141,6 +151,17 @@ void UPower::prepareForSleep_cb(GDBusConnection* system_bus, const gchar* sender
141
151
}
142
152
}
143
153
}
154
+ void UPower::upowerAppear (GDBusConnection* conn, const gchar* name, const gchar* name_owner,
155
+ gpointer data) {
156
+ UPower* up = static_cast <UPower*>(data);
157
+ up->upowerRunning = true ;
158
+ up->event_box_ .set_visible (true );
159
+ }
160
+ void UPower::upowerDisappear (GDBusConnection* conn, const gchar* name, gpointer data) {
161
+ UPower* up = static_cast <UPower*>(data);
162
+ up->upowerRunning = false ;
163
+ up->event_box_ .set_visible (false );
164
+ }
144
165
145
166
void UPower::removeDevice (const gchar* objectPath) {
146
167
std::lock_guard<std::mutex> guard (m_Mutex);
@@ -226,11 +247,13 @@ const std::string UPower::getDeviceStatus(UpDeviceState& state) {
226
247
case UP_DEVICE_STATE_CHARGING:
227
248
case UP_DEVICE_STATE_PENDING_CHARGE:
228
249
return " charging" ;
229
- case UP_DEVICE_STATE_EMPTY:
230
- case UP_DEVICE_STATE_FULLY_CHARGED:
231
250
case UP_DEVICE_STATE_DISCHARGING:
232
251
case UP_DEVICE_STATE_PENDING_DISCHARGE:
233
252
return " discharging" ;
253
+ case UP_DEVICE_STATE_FULLY_CHARGED:
254
+ return " full" ;
255
+ case UP_DEVICE_STATE_EMPTY:
256
+ return " empty" ;
234
257
default :
235
258
return " unknown-status" ;
236
259
}
@@ -258,6 +281,9 @@ std::string UPower::timeToString(gint64 time) {
258
281
auto UPower::update () -> void {
259
282
std::lock_guard<std::mutex> guard (m_Mutex);
260
283
284
+ // Don't update widget if the UPower service isn't running
285
+ if (!upowerRunning) return ;
286
+
261
287
UpDeviceKind kind;
262
288
UpDeviceState state;
263
289
double percentage;
@@ -344,7 +370,7 @@ auto UPower::update() -> void {
344
370
label_.set_markup (onlySpaces ? " " : label_format);
345
371
346
372
// Set icon
347
- if (!Gtk::IconTheme::get_default ()->has_icon (icon_name)) {
373
+ if (icon_name == NULL || !Gtk::IconTheme::get_default ()->has_icon (icon_name)) {
348
374
icon_name = (char *)" battery-missing-symbolic" ;
349
375
}
350
376
icon_.set_from_icon_name (icon_name, Gtk::ICON_SIZE_INVALID);
0 commit comments