Search in sources :

Example 81 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class Roster method getPresences.

/**
     * Returns a List of Presence objects for all of a user's current presences
     * or an unavailable presence if the user is unavailable (offline) or if no presence
     * information is available, such as when you are not subscribed to the user's presence
     * updates.
     *
     * @param jid an XMPP ID, e.g. jdoe@example.com.
     * @return a List of Presence objects for all the user's current presences,
     *         or an unavailable presence if the user is offline or if no presence information
     *         is available.
     */
public List<Presence> getPresences(BareJid jid) {
    List<Presence> res;
    Map<Resourcepart, Presence> userPresences = getPresencesInternal(jid);
    if (userPresences == null) {
        Presence presence = new Presence(Presence.Type.unavailable);
        presence.setFrom(jid);
        res = Arrays.asList(presence);
    } else {
        List<Presence> answer = new ArrayList<Presence>();
        // Used in case no available presence is found
        Presence unavailable = null;
        for (Presence presence : userPresences.values()) {
            if (presence.isAvailable()) {
                answer.add(presence.clone());
            } else {
                unavailable = presence;
            }
        }
        if (!answer.isEmpty()) {
            res = answer;
        } else if (unavailable != null) {
            res = Arrays.asList(unavailable.clone());
        } else {
            Presence presence = new Presence(Presence.Type.unavailable);
            presence.setFrom(jid);
            res = Arrays.asList(presence);
        }
    }
    return res;
}
Also used : ArrayList(java.util.ArrayList) Presence(org.jivesoftware.smack.packet.Presence) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Example 82 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class RosterUtil method ensureSubscribedTo.

public static void ensureSubscribedTo(final XMPPConnection connectionOne, final XMPPConnection connectionTwo, final Date deadline) throws NotLoggedInException, NotConnectedException, InterruptedException, TimeoutException {
    final Roster rosterOne = Roster.getInstanceFor(connectionOne);
    final BareJid jidTwo = connectionTwo.getUser().asBareJid();
    if (rosterOne.iAmSubscribedTo(jidTwo))
        return;
    final BareJid jidOne = connectionOne.getUser().asBareJid();
    final SubscribeListener subscribeListener = new SubscribeListener() {

        @Override
        public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest) {
            if (from.equals(jidOne)) {
                return SubscribeAnswer.Approve;
            }
            return null;
        }
    };
    final Roster rosterTwo = Roster.getInstanceFor(connectionTwo);
    rosterTwo.addSubscribeListener(subscribeListener);
    try {
        rosterOne.sendSubscriptionRequest(jidTwo);
        waitUntilOtherEntityIsSubscribed(rosterTwo, jidOne, deadline);
    } finally {
        rosterTwo.removeSubscribeListener(subscribeListener);
    }
}
Also used : Jid(org.jxmpp.jid.Jid) BareJid(org.jxmpp.jid.BareJid) BareJid(org.jxmpp.jid.BareJid) Presence(org.jivesoftware.smack.packet.Presence)

Example 83 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class Workgroup method isAvailable.

/**
     * Returns true if the workgroup is available for receiving new requests. The workgroup will be
     * available only when agents are available for this workgroup.
     *
     * @return true if the workgroup is available for receiving new requests.
     * @throws XMPPErrorException 
     * @throws NoResponseException 
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public boolean isAvailable() throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    Presence directedPresence = new Presence(Presence.Type.available);
    directedPresence.setTo(workgroupJID);
    StanzaFilter typeFilter = new StanzaTypeFilter(Presence.class);
    StanzaFilter fromFilter = FromMatchesFilter.create(workgroupJID);
    StanzaCollector collector = connection.createStanzaCollectorAndSend(new AndFilter(fromFilter, typeFilter), directedPresence);
    Presence response = (Presence) collector.nextResultOrThrow();
    return Presence.Type.available == response.getType();
}
Also used : StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) Presence(org.jivesoftware.smack.packet.Presence) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 84 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class AgentRoster method getPresence.

/**
     * Returns the presence info for a particular agent, or <tt>null</tt> if the agent
     * is unavailable (offline) or if no presence information is available.<p>
     *
     * @param user a fully qualified xmpp JID. The address could be in any valid format (e.g.
     *             "domain/resource", "user@domain" or "user@domain/resource").
     * @return the agent's current presence, or <tt>null</tt> if the agent is unavailable
     *         or if no presence information is available..
     */
public Presence getPresence(Jid user) {
    Jid key = getPresenceMapKey(user);
    Map<Resourcepart, Presence> userPresences = presenceMap.get(key);
    if (userPresences == null) {
        Presence presence = new Presence(Presence.Type.unavailable);
        presence.setFrom(user);
        return presence;
    } else {
        // Find the resource with the highest priority
        // Might be changed to use the resource with the highest availability instead.
        Iterator<Resourcepart> it = userPresences.keySet().iterator();
        Presence p;
        Presence presence = null;
        while (it.hasNext()) {
            p = userPresences.get(it.next());
            if (presence == null) {
                presence = p;
            } else {
                if (p.getPriority() > presence.getPriority()) {
                    presence = p;
                }
            }
        }
        if (presence == null) {
            presence = new Presence(Presence.Type.unavailable);
            presence.setFrom(user);
            return presence;
        } else {
            return presence;
        }
    }
}
Also used : Jid(org.jxmpp.jid.Jid) EntityFullJid(org.jxmpp.jid.EntityFullJid) Presence(org.jivesoftware.smack.packet.Presence) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Example 85 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class AgentRoster method addListener.

/**
     * Adds a listener to this roster. The listener will be fired anytime one or more
     * changes to the roster are pushed from the server.
     *
     * @param listener an agent roster listener.
     */
public void addListener(AgentRosterListener listener) {
    synchronized (listeners) {
        if (!listeners.contains(listener)) {
            listeners.add(listener);
            // Fire events for the existing entries and presences in the roster
            for (Iterator<String> it = getAgents().iterator(); it.hasNext(); ) {
                String jid = it.next();
                // but possible)
                if (entries.contains(jid)) {
                    // Fire the agent added event
                    listener.agentAdded(jid);
                    Jid j;
                    try {
                        j = JidCreate.from(jid);
                    } catch (XmppStringprepException e) {
                        throw new IllegalStateException(e);
                    }
                    Map<Resourcepart, Presence> userPresences = presenceMap.get(j);
                    if (userPresences != null) {
                        Iterator<Presence> presences = userPresences.values().iterator();
                        while (presences.hasNext()) {
                            // Fire the presence changed event
                            listener.presenceChanged(presences.next());
                        }
                    }
                }
            }
        }
    }
}
Also used : Jid(org.jxmpp.jid.Jid) EntityFullJid(org.jxmpp.jid.EntityFullJid) Presence(org.jivesoftware.smack.packet.Presence) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Aggregations

Presence (org.jivesoftware.smack.packet.Presence)103 Message (org.jivesoftware.smack.packet.Message)21 Resourcepart (org.jxmpp.jid.parts.Resourcepart)12 Jid (org.jxmpp.jid.Jid)11 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)10 AccountItem (com.xabber.android.data.account.AccountItem)9 UserJid (com.xabber.android.data.entity.UserJid)9 AccountJid (com.xabber.android.data.entity.AccountJid)7 StanzaCollector (org.jivesoftware.smack.StanzaCollector)7 AndFilter (org.jivesoftware.smack.filter.AndFilter)7 UserPresence (jetbrains.communicator.core.users.UserPresence)6 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)6 BareJid (org.jxmpp.jid.BareJid)6 ClientInfo (com.xabber.android.data.extension.capability.ClientInfo)5 ArrayList (java.util.ArrayList)5 Date (java.util.Date)5 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)5 MUCInitialPresence (org.jivesoftware.smackx.muc.packet.MUCInitialPresence)5 Test (org.junit.Test)5 EntityFullJid (org.jxmpp.jid.EntityFullJid)5