From 1fe2aa2dca6f14c9a5427e11c9cb4d3ccbc59d6d Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Sun, 16 Feb 2020 23:30:01 +0100 Subject: [PATCH] Initial commit --- .../smackx/inbox/InboxManager.java | 42 +++++++++ .../smackx/inbox/element/EntryElement.java | 73 ++++++++++++++++ .../smackx/inbox/element/FinElement.java | 81 ++++++++++++++++++ .../smackx/inbox/element/InboxElement.java | 85 +++++++++++++++++++ 4 files changed, 281 insertions(+) create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/inbox/InboxManager.java create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/inbox/element/EntryElement.java create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/inbox/element/FinElement.java create mode 100644 smack-experimental/src/main/java/org/jivesoftware/smackx/inbox/element/InboxElement.java diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/inbox/InboxManager.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/inbox/InboxManager.java new file mode 100644 index 000000000..908ddec90 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/inbox/InboxManager.java @@ -0,0 +1,42 @@ +/** + * + * Copyright 2020 Paul Schaub. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.inbox; + +import java.util.Map; +import java.util.WeakHashMap; + +import org.jivesoftware.smack.Manager; +import org.jivesoftware.smack.XMPPConnection; + +public final class InboxManager extends Manager { + + public static final String NAMESPACE_1 = "urn:xmpp:inbox:1"; + private static final Map INSTANCES = new WeakHashMap<>(); + + private InboxManager(XMPPConnection connection) { + super(connection); + } + + public static InboxManager getInstanceFor(XMPPConnection connection) { + InboxManager inboxManager = INSTANCES.get(connection); + if (inboxManager == null) { + inboxManager = new InboxManager(connection); + INSTANCES.put(connection, inboxManager); + } + return inboxManager; + } +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/inbox/element/EntryElement.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/inbox/element/EntryElement.java new file mode 100644 index 000000000..a978ded6b --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/inbox/element/EntryElement.java @@ -0,0 +1,73 @@ +/** + * + * Copyright 2020 Paul Schaub. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.inbox.element; + +import org.jivesoftware.smack.packet.ExtensionElement; +import org.jivesoftware.smack.packet.XmlEnvironment; +import org.jivesoftware.smack.util.XmlStringBuilder; +import org.jivesoftware.smackx.inbox.InboxManager; + +import org.jxmpp.jid.EntityBareJid; + +public class EntryElement implements ExtensionElement { + + public static final String ELEMENT = "entry"; + public static final String ATTR_UNREAD = "unread"; + public static final String ATTR_JID = "jid"; + public static final String ATTR_ID = "id"; + + private final int unreadCount; + private final EntityBareJid jid; + private final String id; // TODO: Legacy Stanza ID, Origin ID, Stanza ID or MAM ID? + + public EntryElement(int unreadCount, EntityBareJid jid, String id) { + this.unreadCount = unreadCount; + this.jid = jid; + this.id = id; + } + + public int getUnreadCount() { + return unreadCount; + } + + public EntityBareJid getJid() { + return jid; + } + + public String getId() { + return id; + } + + @Override + public String getNamespace() { + return InboxManager.NAMESPACE_1; + } + + @Override + public String getElementName() { + return ELEMENT; + } + + @Override + public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) { + return new XmlStringBuilder(this) + .attribute(ATTR_UNREAD, getUnreadCount()) + .attribute(ATTR_JID, getJid()) + .attribute(ATTR_ID, getId()) + .closeEmptyElement(); + } +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/inbox/element/FinElement.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/inbox/element/FinElement.java new file mode 100644 index 000000000..afa0334c9 --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/inbox/element/FinElement.java @@ -0,0 +1,81 @@ +/** + * + * Copyright 2020 Paul Schaub. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.inbox.element; + +import org.jivesoftware.smack.packet.ExtensionElement; +import org.jivesoftware.smack.packet.XmlEnvironment; +import org.jivesoftware.smack.util.XmlStringBuilder; +import org.jivesoftware.smackx.inbox.InboxManager; +import org.jivesoftware.smackx.rsm.packet.RSMSet; + +public class FinElement implements ExtensionElement { + + public static final String ELEMENT = "fin"; + public static final String ATTR_TOTAL = "total"; + public static final String ATTR_UNREAD = "unread"; + public static final String ATTR_ALL_UNREAD = "all-unread"; + + private final int total; + private final int unread; + private final int allUnread; + + private RSMSet rsmData; // TODO: What is RSM data? RSM set? + + public FinElement(RSMSet rsmData, int total, int unread, int allUnread) { + this.rsmData = rsmData; + this.total = total; + this.unread = unread; + this.allUnread = allUnread; + } + + public RSMSet getRsmData() { + return rsmData; + } + + public int getTotal() { + return total; + } + + public int getUnread() { + return unread; + } + + public int getAllUnread() { + return allUnread; + } + + @Override + public String getNamespace() { + return InboxManager.NAMESPACE_1; + } + + @Override + public String getElementName() { + return ELEMENT; + } + + @Override + public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) { + return new XmlStringBuilder(this) + .attribute(ATTR_TOTAL, getTotal()) + .attribute(ATTR_UNREAD, getUnread()) + .attribute(ATTR_ALL_UNREAD, getAllUnread()) + .rightAngleBracket() + .optAppend(getRsmData()) + .closeElement(this); + } +} diff --git a/smack-experimental/src/main/java/org/jivesoftware/smackx/inbox/element/InboxElement.java b/smack-experimental/src/main/java/org/jivesoftware/smackx/inbox/element/InboxElement.java new file mode 100644 index 000000000..3ab3ee44c --- /dev/null +++ b/smack-experimental/src/main/java/org/jivesoftware/smackx/inbox/element/InboxElement.java @@ -0,0 +1,85 @@ +/** + * + * Copyright 2020 Paul Schaub. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jivesoftware.smackx.inbox.element; + +import org.jivesoftware.smack.packet.ExtensionElement; +import org.jivesoftware.smack.packet.XmlEnvironment; +import org.jivesoftware.smack.util.XmlStringBuilder; +import org.jivesoftware.smackx.inbox.InboxManager; +import org.jivesoftware.smackx.rsm.packet.RSMSet; + +public class InboxElement implements ExtensionElement { + + public static final String ELEMENT = "inbox"; + public static final String ATTR_UNREAD_ONLY = "unread-only"; + public static final String ATTR_MESSAGES = "messages"; + + private final RSMSet filter; + private final boolean unreadOnly; + private final boolean includeMessages; + + public InboxElement() { + this(null); + } + + public InboxElement(RSMSet filter) { + this(filter, false, true); + } + + public InboxElement(RSMSet filter, boolean unreadOnly, boolean includeMessages) { + this.filter = filter; + this.unreadOnly = unreadOnly; + this.includeMessages = includeMessages; + } + + public RSMSet getFilter() { + return filter; + } + + public boolean isUnreadOnly() { + return unreadOnly; + } + + public boolean isIncludeMessages() { + return includeMessages; + } + + @Override + public String getNamespace() { + return InboxManager.NAMESPACE_1; + } + + @Override + public String getElementName() { + return ELEMENT; + } + + @Override + public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) { + XmlStringBuilder xml = new XmlStringBuilder(this) + .optBooleanAttribute(ATTR_UNREAD_ONLY, isUnreadOnly()) + .optBooleanAttributeDefaultTrue(ATTR_MESSAGES, isIncludeMessages()); + + if (getFilter() == null) { + return xml.closeEmptyElement(); + } else { + return xml.rightAngleBracket() + .append(getFilter()) + .closeElement(this); + } + } +}