mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
Refactor dbus linked devices interface
Export a separate dbus object for each device
This commit is contained in:
parent
1548ce9c79
commit
778adacb80
8 changed files with 157 additions and 38 deletions
|
@ -117,7 +117,7 @@ public interface Manager extends Closeable {
|
||||||
|
|
||||||
List<Device> getLinkedDevices() throws IOException;
|
List<Device> getLinkedDevices() throws IOException;
|
||||||
|
|
||||||
void removeLinkedDevices(int deviceId) throws IOException;
|
void removeLinkedDevices(long deviceId) throws IOException;
|
||||||
|
|
||||||
void addDeviceLink(URI linkUri) throws IOException, InvalidKeyException;
|
void addDeviceLink(URI linkUri) throws IOException, InvalidKeyException;
|
||||||
|
|
||||||
|
|
|
@ -414,7 +414,7 @@ public class ManagerImpl implements Manager {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeLinkedDevices(int deviceId) throws IOException {
|
public void removeLinkedDevices(long deviceId) throws IOException {
|
||||||
dependencies.getAccountManager().removeDevice(deviceId);
|
dependencies.getAccountManager().removeDevice(deviceId);
|
||||||
var devices = dependencies.getAccountManager().getDevices();
|
var devices = dependencies.getAccountManager().getDevices();
|
||||||
account.setMultiDevice(devices.size() > 1);
|
account.setMultiDevice(devices.size() > 1);
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
package org.asamk;
|
package org.asamk;
|
||||||
|
|
||||||
|
import org.freedesktop.dbus.DBusPath;
|
||||||
|
import org.freedesktop.dbus.annotations.DBusProperty;
|
||||||
import org.freedesktop.dbus.exceptions.DBusException;
|
import org.freedesktop.dbus.exceptions.DBusException;
|
||||||
import org.freedesktop.dbus.exceptions.DBusExecutionException;
|
import org.freedesktop.dbus.exceptions.DBusExecutionException;
|
||||||
import org.freedesktop.dbus.interfaces.DBusInterface;
|
import org.freedesktop.dbus.interfaces.DBusInterface;
|
||||||
|
import org.freedesktop.dbus.interfaces.Properties;
|
||||||
import org.freedesktop.dbus.messages.DBusSignal;
|
import org.freedesktop.dbus.messages.DBusSignal;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -97,11 +100,11 @@ public interface Signal extends DBusInterface {
|
||||||
|
|
||||||
void addDevice(String uri) throws Error.InvalidUri;
|
void addDevice(String uri) throws Error.InvalidUri;
|
||||||
|
|
||||||
void removeDevice(int deviceId) throws Error.Failure;
|
DBusPath getDevice(long deviceId);
|
||||||
|
|
||||||
List<String> listDevices() throws Error.Failure;
|
List<DBusPath> listDevices() throws Error.Failure;
|
||||||
|
|
||||||
void updateDeviceName(String deviceName) throws Error.Failure;
|
DBusPath getThisDevice();
|
||||||
|
|
||||||
void updateProfile(
|
void updateProfile(
|
||||||
String givenName,
|
String givenName,
|
||||||
|
@ -255,6 +258,15 @@ public interface Signal extends DBusInterface {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@DBusProperty(name = "Id", type = Integer.class, access = DBusProperty.Access.READ)
|
||||||
|
@DBusProperty(name = "Name", type = String.class)
|
||||||
|
@DBusProperty(name = "Created", type = String.class, access = DBusProperty.Access.READ)
|
||||||
|
@DBusProperty(name = "LastSeen", type = String.class, access = DBusProperty.Access.READ)
|
||||||
|
interface Device extends DBusInterface, Properties {
|
||||||
|
|
||||||
|
void removeDevice() throws Error.Failure;
|
||||||
|
}
|
||||||
|
|
||||||
interface Error {
|
interface Error {
|
||||||
|
|
||||||
class AttachmentInvalid extends DBusExecutionException {
|
class AttachmentInvalid extends DBusExecutionException {
|
||||||
|
|
|
@ -349,7 +349,7 @@ public class App {
|
||||||
((ExtendedDbusCommand) command).handleCommand(ns, ts, dBusConn, outputWriter);
|
((ExtendedDbusCommand) command).handleCommand(ns, ts, dBusConn, outputWriter);
|
||||||
} else if (command instanceof LocalCommand) {
|
} else if (command instanceof LocalCommand) {
|
||||||
try {
|
try {
|
||||||
((LocalCommand) command).handleCommand(ns, new DbusManagerImpl(ts), outputWriter);
|
((LocalCommand) command).handleCommand(ns, new DbusManagerImpl(ts, dBusConn), outputWriter);
|
||||||
} catch (UnsupportedOperationException e) {
|
} catch (UnsupportedOperationException e) {
|
||||||
throw new UserErrorException("Command is not yet implemented via dbus", e);
|
throw new UserErrorException("Command is not yet implemented via dbus", e);
|
||||||
} catch (DBusExecutionException e) {
|
} catch (DBusExecutionException e) {
|
||||||
|
|
|
@ -120,7 +120,10 @@ public class DaemonCommand implements MultiLocalCommand {
|
||||||
private Thread run(
|
private Thread run(
|
||||||
DBusConnection conn, String objectPath, Manager m, OutputWriter outputWriter, boolean ignoreAttachments
|
DBusConnection conn, String objectPath, Manager m, OutputWriter outputWriter, boolean ignoreAttachments
|
||||||
) throws DBusException {
|
) throws DBusException {
|
||||||
conn.exportObject(new DbusSignalImpl(m, objectPath));
|
final var signal = new DbusSignalImpl(m, conn, objectPath);
|
||||||
|
conn.exportObject(signal);
|
||||||
|
final var initThread = new Thread(signal::initObjects);
|
||||||
|
initThread.start();
|
||||||
|
|
||||||
logger.info("Exported dbus object: " + objectPath);
|
logger.info("Exported dbus object: " + objectPath);
|
||||||
|
|
||||||
|
@ -136,6 +139,11 @@ public class DaemonCommand implements MultiLocalCommand {
|
||||||
logger.warn("Receiving messages failed, retrying", e);
|
logger.warn("Receiving messages failed, retrying", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
initThread.join();
|
||||||
|
} catch (InterruptedException ignored) {
|
||||||
|
}
|
||||||
|
signal.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
thread.start();
|
thread.start();
|
||||||
|
|
|
@ -21,7 +21,7 @@ public class RemoveDeviceCommand implements JsonRpcLocalCommand {
|
||||||
public void attachToSubparser(final Subparser subparser) {
|
public void attachToSubparser(final Subparser subparser) {
|
||||||
subparser.help("Remove a linked device.");
|
subparser.help("Remove a linked device.");
|
||||||
subparser.addArgument("-d", "--device-id", "--deviceId")
|
subparser.addArgument("-d", "--device-id", "--deviceId")
|
||||||
.type(int.class)
|
.type(long.class)
|
||||||
.required(true)
|
.required(true)
|
||||||
.help("Specify the device you want to remove. Use listDevices to see the deviceIds.");
|
.help("Specify the device you want to remove. Use listDevices to see the deviceIds.");
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ public class RemoveDeviceCommand implements JsonRpcLocalCommand {
|
||||||
final Namespace ns, final Manager m, final OutputWriter outputWriter
|
final Namespace ns, final Manager m, final OutputWriter outputWriter
|
||||||
) throws CommandException {
|
) throws CommandException {
|
||||||
try {
|
try {
|
||||||
int deviceId = ns.getInt("device-id");
|
final var deviceId = ns.getLong("device-id");
|
||||||
m.removeLinkedDevices(deviceId);
|
m.removeLinkedDevices(deviceId);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new IOErrorException("Error while removing device: " + e.getMessage(), e);
|
throw new IOErrorException("Error while removing device: " + e.getMessage(), e);
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package org.asamk.signal.dbus;
|
package org.asamk.signal.dbus;
|
||||||
|
|
||||||
import org.asamk.Signal;
|
import org.asamk.Signal;
|
||||||
|
import org.asamk.signal.DbusConfig;
|
||||||
import org.asamk.signal.manager.AttachmentInvalidException;
|
import org.asamk.signal.manager.AttachmentInvalidException;
|
||||||
import org.asamk.signal.manager.Manager;
|
import org.asamk.signal.manager.Manager;
|
||||||
import org.asamk.signal.manager.NotMasterDeviceException;
|
import org.asamk.signal.manager.NotMasterDeviceException;
|
||||||
|
@ -25,6 +26,10 @@ import org.asamk.signal.manager.groups.NotAGroupMemberException;
|
||||||
import org.asamk.signal.manager.storage.recipients.Contact;
|
import org.asamk.signal.manager.storage.recipients.Contact;
|
||||||
import org.asamk.signal.manager.storage.recipients.Profile;
|
import org.asamk.signal.manager.storage.recipients.Profile;
|
||||||
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
|
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
|
||||||
|
import org.freedesktop.dbus.DBusPath;
|
||||||
|
import org.freedesktop.dbus.connections.impl.DBusConnection;
|
||||||
|
import org.freedesktop.dbus.exceptions.DBusException;
|
||||||
|
import org.freedesktop.dbus.interfaces.DBusInterface;
|
||||||
import org.whispersystems.libsignal.IdentityKey;
|
import org.whispersystems.libsignal.IdentityKey;
|
||||||
import org.whispersystems.libsignal.InvalidKeyException;
|
import org.whispersystems.libsignal.InvalidKeyException;
|
||||||
import org.whispersystems.libsignal.util.Pair;
|
import org.whispersystems.libsignal.util.Pair;
|
||||||
|
@ -58,9 +63,11 @@ import java.util.stream.Collectors;
|
||||||
public class DbusManagerImpl implements Manager {
|
public class DbusManagerImpl implements Manager {
|
||||||
|
|
||||||
private final Signal signal;
|
private final Signal signal;
|
||||||
|
private final DBusConnection connection;
|
||||||
|
|
||||||
public DbusManagerImpl(final Signal signal) {
|
public DbusManagerImpl(final Signal signal, DBusConnection connection) {
|
||||||
this.signal = signal;
|
this.signal = signal;
|
||||||
|
this.connection = connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -89,7 +96,8 @@ public class DbusManagerImpl implements Manager {
|
||||||
@Override
|
@Override
|
||||||
public void updateAccountAttributes(final String deviceName) throws IOException {
|
public void updateAccountAttributes(final String deviceName) throws IOException {
|
||||||
if (deviceName != null) {
|
if (deviceName != null) {
|
||||||
signal.updateDeviceName(deviceName);
|
final var devicePath = signal.getThisDevice();
|
||||||
|
getRemoteObject(devicePath, Signal.Device.class).Set("org.asamk.Signal.Device", "Name", deviceName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,15 +144,21 @@ public class DbusManagerImpl implements Manager {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Device> getLinkedDevices() throws IOException {
|
public List<Device> getLinkedDevices() throws IOException {
|
||||||
return signal.listDevices()
|
final var thisDevice = signal.getThisDevice();
|
||||||
.stream()
|
return signal.listDevices().stream().map(devicePath -> {
|
||||||
.map(name -> new Device(-1, name, 0, 0, false))
|
final var device = getRemoteObject(devicePath, Signal.Device.class).GetAll("org.asamk.Signal.Device");
|
||||||
.collect(Collectors.toList());
|
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));
|
||||||
|
}).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeLinkedDevices(final int deviceId) throws IOException {
|
public void removeLinkedDevices(final long deviceId) throws IOException {
|
||||||
signal.removeDevice(deviceId);
|
final var devicePath = signal.getDevice(deviceId);
|
||||||
|
getRemoteObject(devicePath, Signal.Device.class).removeDevice();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -494,4 +508,12 @@ public class DbusManagerImpl implements Manager {
|
||||||
private String emptyIfNull(final String string) {
|
private String emptyIfNull(final String string) {
|
||||||
return string == null ? "" : string;
|
return string == null ? "" : string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private <T extends DBusInterface> T getRemoteObject(final DBusPath devicePath, final Class<T> type) {
|
||||||
|
try {
|
||||||
|
return connection.getRemoteObject(DbusConfig.getBusname(), devicePath.getPath(), type);
|
||||||
|
} catch (DBusException e) {
|
||||||
|
throw new AssertionError(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@ import org.asamk.signal.manager.Manager;
|
||||||
import org.asamk.signal.manager.NotMasterDeviceException;
|
import org.asamk.signal.manager.NotMasterDeviceException;
|
||||||
import org.asamk.signal.manager.StickerPackInvalidException;
|
import org.asamk.signal.manager.StickerPackInvalidException;
|
||||||
import org.asamk.signal.manager.UntrustedIdentityException;
|
import org.asamk.signal.manager.UntrustedIdentityException;
|
||||||
import org.asamk.signal.manager.api.Device;
|
|
||||||
import org.asamk.signal.manager.api.Identity;
|
import org.asamk.signal.manager.api.Identity;
|
||||||
import org.asamk.signal.manager.api.Message;
|
import org.asamk.signal.manager.api.Message;
|
||||||
import org.asamk.signal.manager.api.RecipientIdentifier;
|
import org.asamk.signal.manager.api.RecipientIdentifier;
|
||||||
|
@ -21,6 +20,9 @@ import org.asamk.signal.manager.groups.NotAGroupMemberException;
|
||||||
import org.asamk.signal.manager.storage.recipients.Profile;
|
import org.asamk.signal.manager.storage.recipients.Profile;
|
||||||
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
|
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
|
||||||
import org.asamk.signal.util.ErrorUtils;
|
import org.asamk.signal.util.ErrorUtils;
|
||||||
|
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.exceptions.DBusExecutionException;
|
||||||
import org.whispersystems.libsignal.InvalidKeyException;
|
import org.whispersystems.libsignal.InvalidKeyException;
|
||||||
import org.whispersystems.libsignal.util.Pair;
|
import org.whispersystems.libsignal.util.Pair;
|
||||||
|
@ -49,16 +51,24 @@ import java.util.stream.Stream;
|
||||||
public class DbusSignalImpl implements Signal {
|
public class DbusSignalImpl implements Signal {
|
||||||
|
|
||||||
private final Manager m;
|
private final Manager m;
|
||||||
|
private final DBusConnection connection;
|
||||||
private final String objectPath;
|
private final String objectPath;
|
||||||
|
|
||||||
public DbusSignalImpl(final Manager m, final String objectPath) {
|
private DBusPath thisDevice;
|
||||||
|
private final List<DBusPath> devices = new ArrayList<>();
|
||||||
|
|
||||||
|
public DbusSignalImpl(final Manager m, DBusConnection connection, final String objectPath) {
|
||||||
this.m = m;
|
this.m = m;
|
||||||
|
this.connection = connection;
|
||||||
this.objectPath = objectPath;
|
this.objectPath = objectPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public void initObjects() {
|
||||||
public boolean isRemote() {
|
updateDevices();
|
||||||
return false;
|
}
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
unExportDevices();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -85,33 +95,51 @@ public class DbusSignalImpl implements Signal {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void removeDevice(int deviceId) {
|
public DBusPath getDevice(long deviceId) {
|
||||||
try {
|
updateDevices();
|
||||||
m.removeLinkedDevices(deviceId);
|
return new DBusPath(getDeviceObjectPath(objectPath, deviceId));
|
||||||
} catch (IOException e) {
|
|
||||||
throw new Error.Failure(e.getClass().getSimpleName() + ": Error while removing device: " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<String> listDevices() {
|
public List<DBusPath> listDevices() {
|
||||||
List<Device> devices;
|
updateDevices();
|
||||||
|
return this.devices;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateDevices() {
|
||||||
|
List<org.asamk.signal.manager.api.Device> linkedDevices;
|
||||||
try {
|
try {
|
||||||
devices = m.getLinkedDevices();
|
linkedDevices = m.getLinkedDevices();
|
||||||
} catch (IOException | Error.Failure e) {
|
} catch (IOException | Error.Failure e) {
|
||||||
throw new Error.Failure("Failed to get linked devices: " + e.getMessage());
|
throw new Error.Failure("Failed to get linked devices: " + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
return devices.stream().map(d -> d.getName() == null ? "" : d.getName()).collect(Collectors.toList());
|
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
|
@Override
|
||||||
public void updateDeviceName(String deviceName) {
|
public DBusPath getThisDevice() {
|
||||||
try {
|
updateDevices();
|
||||||
m.updateAccountAttributes(deviceName);
|
return thisDevice;
|
||||||
} catch (IOException | Signal.Error.Failure e) {
|
|
||||||
throw new Error.Failure("UpdateAccount error: " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -759,4 +787,53 @@ public class DbusSignalImpl implements Signal {
|
||||||
private String nullIfEmpty(final String name) {
|
private String nullIfEmpty(final String name) {
|
||||||
return name.isEmpty() ? null : name;
|
return name.isEmpty() ? null : name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String getDeviceObjectPath(String basePath, long deviceId) {
|
||||||
|
return basePath + "/Devices/" + deviceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
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<>("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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue