X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/2ab42ca5471e8fc1e1a31cde954e19564178f114..82bb4f22f07bae40ff42570d0ada81125adf4392:/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java diff --git a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java index 58bd1a97..9f6f1340 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java @@ -2,7 +2,6 @@ package org.asamk.signal.dbus; import org.asamk.Signal; import org.asamk.signal.BaseConfig; -import org.asamk.signal.commands.exceptions.IOErrorException; import org.asamk.signal.manager.AttachmentInvalidException; import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.NotMasterDeviceException; @@ -33,7 +32,10 @@ import org.freedesktop.dbus.DBusPath; import org.freedesktop.dbus.connections.impl.DBusConnection; import org.freedesktop.dbus.exceptions.DBusException; import org.freedesktop.dbus.exceptions.DBusExecutionException; +import org.freedesktop.dbus.interfaces.DBusInterface; import org.freedesktop.dbus.types.Variant; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.File; import java.io.IOException; @@ -63,6 +65,8 @@ public class DbusSignalImpl implements Signal { private final List devices = new ArrayList<>(); private final List groups = new ArrayList<>(); + private final static Logger logger = LoggerFactory.getLogger(DbusSignalImpl.class); + public DbusSignalImpl(final Manager m, DBusConnection connection, final String objectPath) { this.m = m; this.connection = connection; @@ -72,11 +76,13 @@ public class DbusSignalImpl implements Signal { public void initObjects() { updateDevices(); updateGroups(); + updateConfiguration(); } public void close() { unExportDevices(); unExportGroups(); + unExportConfiguration(); } @Override @@ -90,13 +96,13 @@ public class DbusSignalImpl implements Signal { } @Override - public void submitRateLimitChallenge(String challenge, String captchaString) throws IOErrorException { + public void submitRateLimitChallenge(String challenge, String captchaString) { final var captcha = captchaString == null ? null : captchaString.replace("signalcaptcha://", ""); try { m.submitRateLimitRecaptchaChallenge(challenge, captcha); } catch (IOException e) { - throw new IOErrorException("Submit challenge error: " + e.getMessage(), e); + throw new Error.Failure("Submit challenge error: " + e.getMessage()); } } @@ -834,11 +840,7 @@ public class DbusSignalImpl implements Signal { linkedDevices.forEach(d -> { final var object = new DbusSignalDeviceImpl(d); final var deviceObjectPath = object.getObjectPath(); - try { - connection.exportObject(object); - } catch (DBusException e) { - e.printStackTrace(); - } + exportObject(object); if (d.isThisDevice()) { thisDevice = new DBusPath(deviceObjectPath); } @@ -870,11 +872,7 @@ public class DbusSignalImpl implements Signal { groups.forEach(g -> { final var object = new DbusSignalGroupImpl(g.groupId()); - try { - connection.exportObject(object); - } catch (DBusException e) { - e.printStackTrace(); - } + exportObject(object); this.groups.add(new StructGroup(new DBusPath(object.getObjectPath()), g.groupId().serialize(), emptyIfNull(g.title()))); @@ -886,6 +884,30 @@ public class DbusSignalImpl implements Signal { this.groups.clear(); } + private static String getConfigurationObjectPath(String basePath) { + return basePath + "/Configuration"; + } + + private void updateConfiguration() { + unExportConfiguration(); + final var object = new DbusSignalConfigurationImpl(); + exportObject(object); + } + + private void unExportConfiguration() { + final var objectPath = getConfigurationObjectPath(this.objectPath); + connection.unExportObject(objectPath); + } + + private void exportObject(final DBusInterface object) { + try { + connection.exportObject(object); + logger.debug("Exported dbus object: " + object.getObjectPath()); + } catch (DBusException e) { + e.printStackTrace(); + } + } + public class DbusSignalDeviceImpl extends DbusProperties implements Signal.Device { private final org.asamk.signal.manager.api.Device device; @@ -928,6 +950,78 @@ public class DbusSignalImpl implements Signal { } } + public class DbusSignalConfigurationImpl extends DbusProperties implements Signal.Configuration { + + public DbusSignalConfigurationImpl( + ) { + super.addPropertiesHandler(new DbusInterfacePropertiesHandler("org.asamk.Signal.Configuration", + List.of(new DbusProperty<>("ReadReceipts", this::getReadReceipts, this::setReadReceipts), + new DbusProperty<>("UnidentifiedDeliveryIndicators", + this::getUnidentifiedDeliveryIndicators, + this::setUnidentifiedDeliveryIndicators), + new DbusProperty<>("TypingIndicators", + this::getTypingIndicators, + this::setTypingIndicators), + new DbusProperty<>("LinkPreviews", this::getLinkPreviews, this::setLinkPreviews)))); + + } + + @Override + public String getObjectPath() { + return getConfigurationObjectPath(objectPath); + } + + public void setReadReceipts(Boolean readReceipts) { + setConfiguration(readReceipts, null, null, null); + } + + public void setUnidentifiedDeliveryIndicators(Boolean unidentifiedDeliveryIndicators) { + setConfiguration(null, unidentifiedDeliveryIndicators, null, null); + } + + public void setTypingIndicators(Boolean typingIndicators) { + setConfiguration(null, null, typingIndicators, null); + } + + public void setLinkPreviews(Boolean linkPreviews) { + setConfiguration(null, null, null, linkPreviews); + } + + private void setConfiguration( + Boolean readReceipts, + Boolean unidentifiedDeliveryIndicators, + Boolean typingIndicators, + Boolean linkPreviews + ) { + try { + m.updateConfiguration(new org.asamk.signal.manager.api.Configuration(Optional.ofNullable(readReceipts), + Optional.ofNullable(unidentifiedDeliveryIndicators), + Optional.ofNullable(typingIndicators), + Optional.ofNullable(linkPreviews))); + } catch (IOException e) { + throw new Error.Failure("UpdateAccount error: " + e.getMessage()); + } catch (NotMasterDeviceException e) { + throw new Error.Failure("This command doesn't work on linked devices."); + } + } + + private boolean getReadReceipts() { + return m.getConfiguration().readReceipts().orElse(false); + } + + private boolean getUnidentifiedDeliveryIndicators() { + return m.getConfiguration().unidentifiedDeliveryIndicators().orElse(false); + } + + private boolean getTypingIndicators() { + return m.getConfiguration().typingIndicators().orElse(false); + } + + private boolean getLinkPreviews() { + return m.getConfiguration().linkPreviews().orElse(false); + } + } + public class DbusSignalGroupImpl extends DbusProperties implements Signal.Group { private final GroupId groupId;