+ private static Set<RecipientIdentifier.Single> getSingleRecipientIdentifiers(
+ final Collection<String> recipientStrings, final String localNumber
+ ) throws DBusExecutionException {
+ final var identifiers = new HashSet<RecipientIdentifier.Single>();
+ for (var recipientString : recipientStrings) {
+ identifiers.add(getSingleRecipientIdentifier(recipientString, localNumber));
+ }
+ return identifiers;
+ }
+
+ private static RecipientIdentifier.Single getSingleRecipientIdentifier(
+ final String recipientString, final String localNumber
+ ) throws DBusExecutionException {
+ try {
+ return RecipientIdentifier.Single.fromString(recipientString, localNumber);
+ } catch (InvalidNumberException e) {
+ throw new Error.InvalidNumber(e.getMessage());
+ }
+ }
+
+ private static GroupId getGroupId(byte[] groupId) throws DBusExecutionException {
+ try {
+ return GroupId.unknownVersion(groupId);
+ } catch (Throwable e) {
+ throw new Error.InvalidGroupId("Invalid group id: " + e.getMessage());
+ }
+ }
+
+ private byte[] nullIfEmpty(final byte[] array) {
+ return array.length == 0 ? null : array;
+ }
+
+ private String nullIfEmpty(final String name) {
+ 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.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Device",
+ List.of(new DbusProperty<>("Id", device::getId),
+ new DbusProperty<>("Name", () -> emptyIfNull(device.getName()), this::setDeviceName),
+ new DbusProperty<>("Created", device::getCreated),
+ new DbusProperty<>("LastSeen", device::getLastSeen))));
+ this.device = device;
+ }
+
+ @Override
+ public String getObjectPath() {
+ return getDeviceObjectPath(objectPath, device.getId());
+ }
+
+ @Override
+ public void removeDevice() throws Error.Failure {
+ try {
+ m.removeLinkedDevices(device.getId());
+ updateDevices();
+ } catch (IOException e) {
+ throw new Error.Failure(e.getMessage());
+ }
+ }
+
+ private void setDeviceName(String name) {
+ if (!device.isThisDevice()) {
+ throw new Error.Failure("Only the name of this device can be changed");
+ }
+ try {
+ m.updateAccountAttributes(name);
+ // update device list
+ updateDevices();
+ } catch (IOException e) {
+ throw new Error.Failure(e.getMessage());
+ }
+ }
+ }