mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-25 21:42:07 +01:00
Allow for offline configuration of CarbonManager
This commit is contained in:
parent
cd96909ac4
commit
ffb8729ba7
1 changed files with 34 additions and 6 deletions
|
@ -1,6 +1,6 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Copyright 2013-2014 Georg Lukas, 2017-2020 Florian Schmaus
|
* Copyright 2013-2014 Georg Lukas, 2017-2020 Florian Schmaus, 2020 Paul Schaub
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -20,6 +20,8 @@ import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
import java.util.concurrent.CopyOnWriteArraySet;
|
import java.util.concurrent.CopyOnWriteArraySet;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.jivesoftware.smack.AsyncButOrdered;
|
import org.jivesoftware.smack.AsyncButOrdered;
|
||||||
import org.jivesoftware.smack.ConnectionCreationListener;
|
import org.jivesoftware.smack.ConnectionCreationListener;
|
||||||
|
@ -71,10 +73,14 @@ import org.jxmpp.jid.EntityFullJid;
|
||||||
*
|
*
|
||||||
* @author Georg Lukas
|
* @author Georg Lukas
|
||||||
* @author Florian Schmaus
|
* @author Florian Schmaus
|
||||||
|
* @author Paul Schaub
|
||||||
*/
|
*/
|
||||||
public final class CarbonManager extends Manager {
|
public final class CarbonManager extends Manager {
|
||||||
|
|
||||||
private static Map<XMPPConnection, CarbonManager> INSTANCES = new WeakHashMap<XMPPConnection, CarbonManager>();
|
private static final Logger LOGGER = Logger.getLogger(CarbonManager.class.getName());
|
||||||
|
private static Map<XMPPConnection, CarbonManager> INSTANCES = new WeakHashMap<>();
|
||||||
|
|
||||||
|
private static boolean ENABLED_BY_DEFAULT = false;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
|
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
|
||||||
|
@ -99,11 +105,22 @@ public final class CarbonManager extends Manager {
|
||||||
private final Set<CarbonCopyReceivedListener> listeners = new CopyOnWriteArraySet<>();
|
private final Set<CarbonCopyReceivedListener> listeners = new CopyOnWriteArraySet<>();
|
||||||
|
|
||||||
private volatile boolean enabled_state = false;
|
private volatile boolean enabled_state = false;
|
||||||
|
private volatile boolean enabledByDefault = ENABLED_BY_DEFAULT;
|
||||||
|
|
||||||
private final StanzaListener carbonsListener;
|
private final StanzaListener carbonsListener;
|
||||||
|
|
||||||
private final AsyncButOrdered<BareJid> carbonsListenerAsyncButOrdered = new AsyncButOrdered<>();
|
private final AsyncButOrdered<BareJid> carbonsListenerAsyncButOrdered = new AsyncButOrdered<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Should Carbons be automatically be enabled once the connection is authenticated?
|
||||||
|
* Default: false
|
||||||
|
*
|
||||||
|
* @param enabledByDefault new default value
|
||||||
|
*/
|
||||||
|
public static void setEnabledByDefault(boolean enabledByDefault) {
|
||||||
|
ENABLED_BY_DEFAULT = enabledByDefault;
|
||||||
|
}
|
||||||
|
|
||||||
private CarbonManager(XMPPConnection connection) {
|
private CarbonManager(XMPPConnection connection) {
|
||||||
super(connection);
|
super(connection);
|
||||||
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||||
|
@ -111,7 +128,7 @@ public final class CarbonManager extends Manager {
|
||||||
|
|
||||||
carbonsListener = new StanzaListener() {
|
carbonsListener = new StanzaListener() {
|
||||||
@Override
|
@Override
|
||||||
public void processStanza(final Stanza stanza) throws NotConnectedException, InterruptedException {
|
public void processStanza(final Stanza stanza) {
|
||||||
final Message wrappingMessage = (Message) stanza;
|
final Message wrappingMessage = (Message) stanza;
|
||||||
final CarbonExtension carbonExtension = CarbonExtension.from(wrappingMessage);
|
final CarbonExtension carbonExtension = CarbonExtension.from(wrappingMessage);
|
||||||
final Direction direction = carbonExtension.getDirection();
|
final Direction direction = carbonExtension.getDirection();
|
||||||
|
@ -145,6 +162,13 @@ public final class CarbonManager extends Manager {
|
||||||
if (!resumed) {
|
if (!resumed) {
|
||||||
// Non-resumed XMPP sessions always start with disabled carbons
|
// Non-resumed XMPP sessions always start with disabled carbons
|
||||||
enabled_state = false;
|
enabled_state = false;
|
||||||
|
try {
|
||||||
|
if (shouldCarbonsBeEnabled() && isSupportedByServer()) {
|
||||||
|
setCarbonsEnabled(true);
|
||||||
|
}
|
||||||
|
} catch (InterruptedException | XMPPErrorException | NotConnectedException | NoResponseException e) {
|
||||||
|
LOGGER.log(Level.WARNING, "Cannot check for Carbon support and / or enable carbons.", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
addCarbonsListener(connection);
|
addCarbonsListener(connection);
|
||||||
}
|
}
|
||||||
|
@ -239,12 +263,10 @@ public final class CarbonManager extends Manager {
|
||||||
* You should first check for support using isSupportedByServer().
|
* You should first check for support using isSupportedByServer().
|
||||||
*
|
*
|
||||||
* @param new_state whether carbons should be enabled or disabled
|
* @param new_state whether carbons should be enabled or disabled
|
||||||
* @throws NotConnectedException if the XMPP connection is not connected.
|
|
||||||
* @throws InterruptedException if the calling thread was interrupted.
|
|
||||||
* @deprecated use {@link #enableCarbonsAsync(ExceptionCallback)} or {@link #disableCarbonsAsync(ExceptionCallback)} instead.
|
* @deprecated use {@link #enableCarbonsAsync(ExceptionCallback)} or {@link #disableCarbonsAsync(ExceptionCallback)} instead.
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public void sendCarbonsEnabled(final boolean new_state) throws NotConnectedException, InterruptedException {
|
public void sendCarbonsEnabled(final boolean new_state) {
|
||||||
sendUseCarbons(new_state, null);
|
sendUseCarbons(new_state, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -281,6 +303,7 @@ public final class CarbonManager extends Manager {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendUseCarbons(final boolean use, ExceptionCallback<Exception> exceptionCallback) {
|
private void sendUseCarbons(final boolean use, ExceptionCallback<Exception> exceptionCallback) {
|
||||||
|
enabledByDefault = use;
|
||||||
IQ setIQ = carbonsEnabledIQ(use);
|
IQ setIQ = carbonsEnabledIQ(use);
|
||||||
|
|
||||||
SmackFuture<IQ, Exception> future = connection().sendIqRequestAsync(setIQ);
|
SmackFuture<IQ, Exception> future = connection().sendIqRequestAsync(setIQ);
|
||||||
|
@ -310,6 +333,7 @@ public final class CarbonManager extends Manager {
|
||||||
*/
|
*/
|
||||||
public synchronized void setCarbonsEnabled(final boolean new_state) throws NoResponseException,
|
public synchronized void setCarbonsEnabled(final boolean new_state) throws NoResponseException,
|
||||||
XMPPErrorException, NotConnectedException, InterruptedException {
|
XMPPErrorException, NotConnectedException, InterruptedException {
|
||||||
|
enabledByDefault = new_state;
|
||||||
if (enabled_state == new_state)
|
if (enabled_state == new_state)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -350,6 +374,10 @@ public final class CarbonManager extends Manager {
|
||||||
return this.enabled_state;
|
return this.enabled_state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean shouldCarbonsBeEnabled() {
|
||||||
|
return enabledByDefault;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mark a message as "private", so it will not be carbon-copied.
|
* Mark a message as "private", so it will not be carbon-copied.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue