]> nmode's Git Repositories - signal-cli/commitdiff
Return struct instead of object path directly for dbus list devices
authorAsamK <asamk@gmx.de>
Thu, 7 Oct 2021 18:51:33 +0000 (20:51 +0200)
committerAsamK <asamk@gmx.de>
Thu, 7 Oct 2021 18:56:29 +0000 (20:56 +0200)
src/main/java/org/asamk/Signal.java
src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java
src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java

index 1d8d04a3fe4505cfd9bc26f4c64c7372ebfba35c..2f81c196e714950dff561c48586c7e64578d925b 100644 (file)
@@ -3,7 +3,9 @@ package org.asamk;
 import org.asamk.signal.commands.exceptions.IOErrorException;
 
 import org.freedesktop.dbus.DBusPath;
+import org.freedesktop.dbus.Struct;
 import org.freedesktop.dbus.annotations.DBusProperty;
+import org.freedesktop.dbus.annotations.Position;
 import org.freedesktop.dbus.exceptions.DBusException;
 import org.freedesktop.dbus.exceptions.DBusExecutionException;
 import org.freedesktop.dbus.interfaces.DBusInterface;
@@ -104,7 +106,7 @@ public interface Signal extends DBusInterface {
 
     DBusPath getDevice(long deviceId);
 
-    List<DBusPath> listDevices() throws Error.Failure;
+    List<StructDevice> listDevices() throws Error.Failure;
 
     DBusPath getThisDevice();
 
@@ -262,6 +264,36 @@ public interface Signal extends DBusInterface {
         }
     }
 
+    class StructDevice extends Struct {
+
+        @Position(0)
+        DBusPath objectPath;
+
+        @Position(1)
+        Long id;
+
+        @Position(2)
+        String name;
+
+        public StructDevice(final DBusPath objectPath, final Long id, final String name) {
+            this.objectPath = objectPath;
+            this.id = id;
+            this.name = name;
+        }
+
+        public DBusPath getObjectPath() {
+            return objectPath;
+        }
+
+        public Long getId() {
+            return id;
+        }
+
+        public String getName() {
+            return name;
+        }
+    }
+
     @DBusProperty(name = "Id", type = Long.class, access = DBusProperty.Access.READ)
     @DBusProperty(name = "Name", type = String.class)
     @DBusProperty(name = "Created", type = String.class, access = DBusProperty.Access.READ)
index 3124a5b05ed8ee655418a466bab56c44c5edc83d..53148c01c54199f9c375f2f2099553c07b086c9d 100644 (file)
@@ -145,13 +145,14 @@ public class DbusManagerImpl implements Manager {
     @Override
     public List<Device> getLinkedDevices() throws IOException {
         final var thisDevice = signal.getThisDevice();
-        return signal.listDevices().stream().map(devicePath -> {
-            final var device = getRemoteObject(devicePath, Signal.Device.class).GetAll("org.asamk.Signal.Device");
+        return signal.listDevices().stream().map(d -> {
+            final var device = getRemoteObject(d.getObjectPath(),
+                    Signal.Device.class).GetAll("org.asamk.Signal.Device");
             return new Device((long) device.get("Id").getValue(),
                     (String) device.get("Name").getValue(),
                     (long) device.get("Created").getValue(),
                     (long) device.get("LastSeen").getValue(),
-                    thisDevice.equals(devicePath));
+                    thisDevice.equals(d.getObjectPath()));
         }).collect(Collectors.toList());
     }
 
index 698ce7c1d2c227d3aad1a8eb39d50d3e1ce70422..d0e33a40e66ba2abc03d7ae61e5ae8883663ea72 100644 (file)
@@ -57,7 +57,7 @@ public class DbusSignalImpl implements Signal {
     private final String objectPath;
 
     private DBusPath thisDevice;
-    private final List<DBusPath> devices = new ArrayList<>();
+    private final List<StructDevice> devices = new ArrayList<>();
 
     public DbusSignalImpl(final Manager m, DBusConnection connection, final String objectPath) {
         this.m = m;
@@ -115,41 +115,11 @@ public class DbusSignalImpl implements Signal {
     }
 
     @Override
-    public List<DBusPath> listDevices() {
+    public List<StructDevice> listDevices() {
         updateDevices();
         return this.devices;
     }
 
-    private void updateDevices() {
-        List<org.asamk.signal.manager.api.Device> linkedDevices;
-        try {
-            linkedDevices = m.getLinkedDevices();
-        } catch (IOException | Error.Failure e) {
-            throw new Error.Failure("Failed to get linked devices: " + e.getMessage());
-        }
-
-        unExportDevices();
-
-        linkedDevices.forEach(d -> {
-            final var object = new DbusSignalDeviceImpl(d);
-            final var deviceObjectPath = object.getObjectPath();
-            try {
-                connection.exportObject(object);
-            } catch (DBusException e) {
-                e.printStackTrace();
-            }
-            if (d.isThisDevice()) {
-                thisDevice = new DBusPath(deviceObjectPath);
-            }
-            this.devices.add(new DBusPath(deviceObjectPath));
-        });
-    }
-
-    private void unExportDevices() {
-        this.devices.stream().map(DBusPath::getPath).forEach(connection::unExportObject);
-        this.devices.clear();
-    }
-
     @Override
     public DBusPath getThisDevice() {
         updateDevices();
@@ -802,21 +772,55 @@ public class DbusSignalImpl implements Signal {
         return name.isEmpty() ? null : name;
     }
 
+    private String emptyIfNull(final String string) {
+        return string == null ? "" : string;
+    }
+
     private static String getDeviceObjectPath(String basePath, long deviceId) {
         return basePath + "/Devices/" + deviceId;
     }
 
+    private void updateDevices() {
+        List<org.asamk.signal.manager.api.Device> linkedDevices;
+        try {
+            linkedDevices = m.getLinkedDevices();
+        } catch (IOException e) {
+            throw new Error.Failure("Failed to get linked devices: " + e.getMessage());
+        }
+
+        unExportDevices();
+
+        linkedDevices.forEach(d -> {
+            final var object = new DbusSignalDeviceImpl(d);
+            final var deviceObjectPath = object.getObjectPath();
+            try {
+                connection.exportObject(object);
+            } catch (DBusException e) {
+                e.printStackTrace();
+            }
+            if (d.isThisDevice()) {
+                thisDevice = new DBusPath(deviceObjectPath);
+            }
+            this.devices.add(new StructDevice(new DBusPath(deviceObjectPath), d.getId(), emptyIfNull(d.getName())));
+        });
+    }
+
+    private void unExportDevices() {
+        this.devices.stream()
+                .map(StructDevice::getObjectPath)
+                .map(DBusPath::getPath)
+                .forEach(connection::unExportObject);
+        this.devices.clear();
+    }
+
     public class DbusSignalDeviceImpl extends DbusProperties implements Signal.Device {
 
         private final org.asamk.signal.manager.api.Device device;
 
         public DbusSignalDeviceImpl(final org.asamk.signal.manager.api.Device device) {
-            super();
             super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Device",
                     List.of(new DbusProperty<>("Id", device::getId),
-                            new DbusProperty<>("Name",
-                                    () -> device.getName() == null ? "" : device.getName(),
-                                    this::setDeviceName),
+                            new DbusProperty<>("Name", () -> emptyIfNull(device.getName()), this::setDeviceName),
                             new DbusProperty<>("Created", device::getCreated),
                             new DbusProperty<>("LastSeen", device::getLastSeen))));
             this.device = device;