+ });
+ }
+
+ private void runDbusMultiAccount(
+ final MultiAccountManager c, final boolean noReceiveOnStart, final boolean isDbusSystem
+ ) throws UnexpectedErrorException {
+ runDbus(isDbusSystem, (connection, objectPath) -> {
+ final var signalControl = new DbusSignalControlImpl(c, objectPath);
+ connection.exportObject(signalControl);
+
+ c.addOnManagerAddedHandler(m -> {
+ final var thread = exportMultiAccountManager(connection, m, noReceiveOnStart);
+ if (thread != null) {
+ try {
+ thread.join();
+ } catch (InterruptedException ignored) {
+ }
+ }
+ });
+ c.addOnManagerRemovedHandler(m -> {
+ final var path = DbusConfig.getObjectPath(m.getSelfNumber());
+ try {
+ final var object = connection.getExportedObject(null, path);
+ if (object instanceof DbusSignalImpl dbusSignal) {
+ dbusSignal.close();
+ }
+ } catch (DBusException ignored) {
+ }
+ connection.unExportObject(path);
+ });
+
+ final var initThreads = c.getAccountNumbers()
+ .stream()
+ .map(c::getManager)
+ .filter(Objects::nonNull)
+ .map(m -> exportMultiAccountManager(connection, m, noReceiveOnStart))
+ .filter(Objects::nonNull)
+ .collect(Collectors.toList());
+
+ for (var t : initThreads) {
+ try {
+ t.join();
+ } catch (InterruptedException ignored) {
+ }
+ }
+ });
+ }
+
+ private void runDbus(
+ final boolean isDbusSystem, DbusRunner dbusRunner
+ ) throws UnexpectedErrorException {
+ DBusConnection.DBusBusType busType;
+ if (isDbusSystem) {
+ busType = DBusConnection.DBusBusType.SYSTEM;
+ } else {
+ busType = DBusConnection.DBusBusType.SESSION;
+ }
+ try {
+ var conn = DBusConnection.getConnection(busType);
+ dbusRunner.run(conn, DbusConfig.getObjectPath());