diff --git a/build/resources/META-INF/smack.providers b/build/resources/META-INF/smack.providers index 5efdca4d3..e336fc42d 100644 --- a/build/resources/META-INF/smack.providers +++ b/build/resources/META-INF/smack.providers @@ -44,4 +44,18 @@ org.jivesoftware.smackx.GroupChatInvitation$Provider + + + query + http://jabber.org/protocol/disco#items + org.jivesoftware.smackx.provider.DiscoverItemsProvider + + + + + query + http://jabber.org/protocol/disco#info + org.jivesoftware.smackx.provider.DiscoverInfoProvider + + \ No newline at end of file diff --git a/source/org/jivesoftware/smackx/XHTMLManager.java b/source/org/jivesoftware/smackx/XHTMLManager.java index eee45c568..8654c081a 100644 --- a/source/org/jivesoftware/smackx/XHTMLManager.java +++ b/source/org/jivesoftware/smackx/XHTMLManager.java @@ -54,7 +54,10 @@ package org.jivesoftware.smackx; import java.util.Iterator; +import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.packet.Message; +import org.jivesoftware.smackx.packet.DiscoverInfo; import org.jivesoftware.smackx.packet.XHTMLExtension; /** @@ -66,6 +69,8 @@ import org.jivesoftware.smackx.packet.XHTMLExtension; */ public class XHTMLManager { + private final static String namespace = "http://jabber.org/protocol/xhtml-im"; + /** * Returns an Iterator for the XHTML bodies in the message. Returns null if * the message does not contain an XHTML extension. @@ -74,8 +79,7 @@ public class XHTMLManager { * @return an Iterator for the bodies in the message or null if none. */ public static Iterator getBodies(Message message) { - XHTMLExtension xhtmlExtension = - (XHTMLExtension) message.getExtension("html", "http://jabber.org/protocol/xhtml-im"); + XHTMLExtension xhtmlExtension = (XHTMLExtension) message.getExtension("html", namespace); if (xhtmlExtension != null) return xhtmlExtension.getBodies(); else @@ -89,8 +93,7 @@ public class XHTMLManager { * @param body the string to add as an XHTML body to the message */ public static void addBody(Message message, String body) { - XHTMLExtension xhtmlExtension = - (XHTMLExtension) message.getExtension("html", "http://jabber.org/protocol/xhtml-im"); + XHTMLExtension xhtmlExtension = (XHTMLExtension) message.getExtension("html", namespace); if (xhtmlExtension == null) { // Create an XHTMLExtension and add it to the message xhtmlExtension = new XHTMLExtension(); @@ -107,7 +110,54 @@ public class XHTMLManager { * @return a boolean indicating whether the message is an XHTML message */ public static boolean isXHTMLMessage(Message message) { - return message.getExtension("html", "http://jabber.org/protocol/xhtml-im") != null; + return message.getExtension("html", namespace) != null; } + /** + * Enables or disables the XHTML support on a given connection.

+ * + * Before starting to send XHTML messages to a user, check that the user can handle XHTML + * messages. Enable the XHTML support to indicate that this client handles XHTML messages. + * + * @param connection the connection where the service will be enabled or disabled + * @param enabled indicates if the service will be enabled or disabled + */ + public synchronized static void setServiceEnabled(XMPPConnection connection, boolean enabled) { + if (isServiceEnabled(connection) == enabled) + return; + + if (enabled) { + ServiceDiscoveryManager.getInstanceFor(connection).addFeature(namespace); + } else { + ServiceDiscoveryManager.getInstanceFor(connection).removeFeature(namespace); + } + } + + /** + * Returns true if the XHTML support is enabled for the given connection. + * + * @param connection the connection to look for XHTML support + * @return a boolean indicating if the XHTML support is enabled for the given connection + */ + public static boolean isServiceEnabled(XMPPConnection connection) { + return ServiceDiscoveryManager.getInstanceFor(connection).includesFeature(namespace); + } + + /** + * Returns true if the specified user handles XHTML messages. + * + * @param connection the connection to use to perform the service discovery + * @param userID the user to check. A fully qualified xmpp ID, e.g. jdoe@example.com + * @return a boolean indicating whether the specified user handles XHTML messages + */ + public static boolean isServiceEnabled(XMPPConnection connection, String userID) { + try { + DiscoverInfo result = + ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(userID); + return result.containsFeature(namespace); + } catch (XMPPException e) { + e.printStackTrace(); + return false; + } + } }