001/**
002 *
003 * Copyright 2019 Aditya Borikar, 2020 Florian Schmaus.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.usertune;
018
019import java.util.Map;
020import java.util.WeakHashMap;
021
022import org.jivesoftware.smack.Manager;
023import org.jivesoftware.smack.SmackException.NoResponseException;
024import org.jivesoftware.smack.SmackException.NotConnectedException;
025import org.jivesoftware.smack.SmackException.NotLoggedInException;
026import org.jivesoftware.smack.XMPPConnection;
027import org.jivesoftware.smack.XMPPException.XMPPErrorException;
028import org.jivesoftware.smack.packet.Message;
029
030import org.jivesoftware.smackx.pep.PepEventListener;
031import org.jivesoftware.smackx.pep.PepManager;
032import org.jivesoftware.smackx.pubsub.PayloadItem;
033import org.jivesoftware.smackx.pubsub.PubSubException.NotALeafNodeException;
034import org.jivesoftware.smackx.usertune.element.UserTuneElement;
035
036/**
037 * Entry point for Smacks API for XEP-0118: User Tune.
038 * <br>
039 * To publish a UserTune, please use {@link #publishUserTune(UserTuneElement)} method. This will publish the node.
040 * <br>
041 * To stop publishing a UserTune, please use {@link #clearUserTune()} method. This will send a disabling publish signal.
042 * <br>
043 * To add a UserTune listener in order to remain updated with other users UserTune, use {@link #addUserTuneListener(PepEventListener)} method.
044 * <br>
045 * To link a UserTuneElement with {@link Message}, use 'message.addExtension(userTuneElement)'.
046 * <br>
047 * An example to illustrate is provided inside UserTuneElementTest inside the test package.
048 * <br>
049 * @see <a href="https://xmpp.org/extensions/xep-0118.html">
050 *     XEP-0118: User Tune</a>
051 */
052public final class UserTuneManager extends Manager {
053
054    public static final String USERTUNE_NODE = "http://jabber.org/protocol/tune";
055
056    private static final Map<XMPPConnection, UserTuneManager> INSTANCES = new WeakHashMap<>();
057
058    private final PepManager pepManager;
059
060    public static synchronized UserTuneManager getInstanceFor(XMPPConnection connection) throws NotLoggedInException {
061        UserTuneManager manager = INSTANCES.get(connection);
062        if (manager == null) {
063            manager = new UserTuneManager(connection);
064            INSTANCES.put(connection, manager);
065        }
066        return manager;
067    }
068
069    private UserTuneManager(XMPPConnection connection) {
070        super(connection);
071        pepManager = PepManager.getInstanceFor(connection);
072    }
073
074    public void clearUserTune() throws NotLoggedInException, NotALeafNodeException, NoResponseException, NotConnectedException, XMPPErrorException, InterruptedException {
075        publishUserTune(UserTuneElement.EMPTY_USER_TUNE);
076    }
077
078    public void publishUserTune(UserTuneElement userTuneElement) throws NotLoggedInException, NotALeafNodeException, NoResponseException, NotConnectedException, XMPPErrorException, InterruptedException {
079        // TODO: To prevent a large number of updates when a user is skipping through tracks, an implementation SHOULD wait several seconds before publishing new tune information.
080        pepManager.publish(USERTUNE_NODE, new PayloadItem<>(userTuneElement));
081    }
082
083    public boolean addUserTuneListener(PepEventListener<UserTuneElement> listener) {
084        return pepManager.addPepEventListener(USERTUNE_NODE, UserTuneElement.class, listener);
085    }
086
087    public boolean removeUserTuneListener(PepEventListener<UserTuneElement> listener) {
088        return pepManager.removePepEventListener(listener);
089    }
090}