Skip to content

Commit d6a00ef

Browse files
committed
Less DBus calls
1 parent bb0ed37 commit d6a00ef

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

applets/batt/battery.vala

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,18 @@ private class DeviceData
1717
internal ServerItem item;
1818
internal UPower.DeviceState prev_state;
1919
internal UPower.DeviceWarningLevel prev_level;
20-
public DeviceData(ServerItem item)
20+
internal UPower.Device dev;
21+
internal string path;
22+
public DeviceData (string path)
2123
{
24+
this.path = path;
25+
try {
26+
dev = Bus.get_proxy_sync(BusType.SYSTEM,BatteryIconExporter.UPOWER_NAME,path);
27+
} catch (Error e){stderr.printf("%s\n",e.message);}
28+
}
29+
public DeviceData.full(string path,ServerItem item)
30+
{
31+
this(path);
2232
this.item = item;
2333
}
2434
}
@@ -27,7 +37,7 @@ private class DeviceData
2737
public class BatteryIconExporter : ItemExporter
2838
{
2939
private static const string UPOWER_PATH = "/org/freedesktop/UPower";
30-
private static const string UPOWER_NAME = "org.freedesktop.UPower";
40+
internal static const string UPOWER_NAME = "org.freedesktop.UPower";
3141
private static const string NOTIFY = "use-notifications";
3242
private static const string PERCENT = "show-percentage";
3343
private static const string TIME = "show-time-remaining";
@@ -135,22 +145,19 @@ public class BatteryIconExporter : ItemExporter
135145
else
136146
x_ayatana_new_label("","");
137147
}
138-
private ServerItem? create_device_item(ObjectPath devs)
148+
private ServerItem? create_device_item(DeviceData data)
139149
{
140-
UPower.Device? dev = null;
141-
try {
142-
dev = Bus.get_proxy_sync(BusType.SYSTEM,UPOWER_NAME,devs);
143-
} catch (Error e){return null;}
150+
var dev = data.dev;
144151
if (dev.device_type == UPower.DeviceType.LINE_POWER)
145152
return null;
146153
var item = new ServerItem();
147154
item.set_variant_property("label",new Variant.string("%s%s (%s) - %0.0lf%%".printf(dev.vendor,dev.model,dev.device_type.to_string(),dev.percentage)));
148155
item.set_variant_property("icon-name",new Variant.string(dev.icon_name));
149156
item.set_variant_property("toggle-type",new Variant.string("radio"));
150-
if (devs == display_device_path)
157+
if (data.path == display_device_path)
151158
item.set_variant_property("toogle-state",new Variant.int32(1));
152159
item.activated.connect(()=>{
153-
settings.set(PATH,"o",devs);
160+
settings.set(PATH,"o",data.path);
154161
});
155162
return item;
156163
}
@@ -161,10 +168,11 @@ public class BatteryIconExporter : ItemExporter
161168
dbusmenu.prepend_item(sep);
162169
foreach (var devs in devices)
163170
{
164-
var item = create_device_item(devs);
171+
var data = new DeviceData(devs);
172+
var item = create_device_item(data);
173+
data.item = item;
165174
if (item == null)
166175
continue;
167-
var data = new DeviceData(item);
168176
devices_table.insert(devs,(owned)data);
169177
dbusmenu.prepend_item(item);
170178
}
@@ -181,8 +189,9 @@ public class BatteryIconExporter : ItemExporter
181189
}
182190
private void device_added_cb(ObjectPath devs)
183191
{
184-
var item = create_device_item(devs);
185-
var data = new DeviceData(item);
192+
var data = new DeviceData(devs);
193+
var item = create_device_item(data);
194+
data.item = item;
186195
devices_table.insert(devs,(owned)data);
187196
dbusmenu.prepend_item(item);
188197
dbusmenu.layout_updated(layout_revision++,0);
@@ -210,7 +219,8 @@ public class BatteryIconExporter : ItemExporter
210219
{
211220
try {
212221
unowned ServerItem item = data.item;
213-
UPower.Device dev = Bus.get_proxy_sync(BusType.SYSTEM,UPOWER_NAME,path);
222+
unowned UPower.Device dev = data.dev;
223+
dev.refresh();
214224
if (dev.device_type == UPower.DeviceType.LINE_POWER)
215225
continue;
216226
item.set_variant_property("label",new Variant.string("%s%s (%s) - %0.0lf%%".printf(dev.vendor,dev.model,dev.device_type.to_string(),dev.percentage)));

0 commit comments

Comments
 (0)