Java Code Examples for org.jivesoftware.smack.packet.Presence#Mode

The following examples show how to use org.jivesoftware.smack.packet.Presence#Mode . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SendPresence.java    From jmeter-bzm-plugins with Apache License 2.0 6 votes vote down vote up
@Override
public SampleResult perform(JMeterXMPPSampler sampler, SampleResult res) throws Exception {
    Presence.Type typeVal = Presence.Type.valueOf(sampler.getPropertyAsString(TYPE, Presence.Type.available.toString()));
    Presence.Mode modeVal = Presence.Mode.valueOf(sampler.getPropertyAsString(MODE, Presence.Mode.available.toString()));

    Presence presence = new Presence(typeVal);
    presence.setMode(modeVal);

    String to = sampler.getPropertyAsString(RECIPIENT);
    if (!to.isEmpty()) {
        presence.setTo(to);
    }

    String text = sampler.getPropertyAsString(STATUS_TEXT);
    if (!text.isEmpty()) {
        presence.setStatus(text);
    }

    sampler.getXMPPConnection().sendPacket(presence);
    res.setSamplerData(presence.toXML().toString());
    return res;
}
 
Example 2
Source File: Workpane.java    From Spark with Apache License 2.0 6 votes vote down vote up
public void presenceChanged(Presence presence) {
    String status = presence.getStatus();
    if (status == null) {
        status = "";
    }

    try {
        if (FastpathPlugin.getAgentSession().isOnline()) {
            Presence.Mode mode = presence.getMode();
            if (status == null) {
                status = "";
            }
            if (mode == null) {
                mode = Presence.Mode.available;
            }
            FastpathPlugin.getAgentSession().setStatus(presence.getMode(), status);
        }
    }
    catch (XMPPException | SmackException | InterruptedException e) {
        Log.error(e);
    }
}
 
Example 3
Source File: MultiUserChat.java    From Smack with Apache License 2.0 6 votes vote down vote up
/**
 * Changes the occupant's availability status within the room. The presence type
 * will remain available but with a new status that describes the presence update and
 * a new presence mode (e.g. Extended away).
 *
 * @param status a text message describing the presence update.
 * @param mode the mode type for the presence update.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws MucNotJoinedException if not joined to the Multi-User Chat.
 */
public void changeAvailabilityStatus(String status, Presence.Mode mode) throws NotConnectedException, InterruptedException, MucNotJoinedException {
    final EntityFullJid myRoomJid = this.myRoomJid;
    if (myRoomJid == null) {
        throw new MucNotJoinedException(this);
    }

    // We change the availability status by sending a presence packet to the room with the
    // new presence status and mode
    Presence joinPresence = connection.getStanzaFactory().buildPresenceStanza()
            .to(myRoomJid)
            .ofType(Presence.Type.available)
            .setStatus(status)
            .setMode(mode)
            .build();

    // Send join packet.
    connection.sendStanza(joinPresence);
}
 
Example 4
Source File: SparkTabHandler.java    From Spark with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the SparkTab to show it is in a stale state.
 *
 * @param tab      the SparkTab.
 * @param chatRoom the ChatRoom of the SparkTab.
 */
protected void decorateStaleTab(SparkTab tab, ChatRoom chatRoom) {
    tab.setTitleColor(Color.gray);
    tab.setTabFont(tab.getDefaultFont());

    EntityBareJid jid = ((ChatRoomImpl)chatRoom).getParticipantJID();
    Presence presence = PresenceManager.getPresence(jid);

    if (!presence.isAvailable()) {
        tab.setIcon(SparkRes.getImageIcon(SparkRes.IM_UNAVAILABLE_STALE_IMAGE));
    }
    else {
        Presence.Mode mode = presence.getMode();
        if (mode == Presence.Mode.available || mode == null) {
            tab.setIcon(SparkRes.getImageIcon(SparkRes.IM_AVAILABLE_STALE_IMAGE));
        }
        else if (mode == Presence.Mode.away) {
            tab.setIcon(SparkRes.getImageIcon(SparkRes.IM_AWAY_STALE_IMAGE));
        }
        else if (mode == Presence.Mode.chat) {
            tab.setIcon(SparkRes.getImageIcon(SparkRes.IM_FREE_CHAT_STALE_IMAGE));
        }
        else if (mode == Presence.Mode.dnd) {
            tab.setIcon(SparkRes.getImageIcon(SparkRes.IM_DND_STALE_IMAGE));
        }
        else if (mode == Presence.Mode.xa) {
            tab.setIcon(SparkRes.getImageIcon(SparkRes.IM_DND_STALE_IMAGE));
        }
    }

    tab.validateTab();
}
 
Example 5
Source File: PresenceManager.java    From Spark with Apache License 2.0 5 votes vote down vote up
public static boolean isOnPhone(Presence presence) {
	Presence.Mode presenceMode = presence.getMode();
	 if (presenceMode == null) {
    	 presenceMode = Presence.Mode.available;
    }
	if (presence.getStatus() != null && 
		presence.getStatus().contains(Res.getString("status.on.phone")) && 
		presenceMode.equals(Presence.Mode.away)) {
		return true;
	}
	return false;
}
 
Example 6
Source File: AgentSession.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the agent's current status with the workgroup. The presence mode affects how offers
 * are routed to the agent. The possible presence modes with their meanings are as follows:<ul>
 *
 * <li>Presence.Mode.AVAILABLE -- (Default) the agent is available for more chats
 * (equivalent to Presence.Mode.CHAT).
 * <li>Presence.Mode.DO_NOT_DISTURB -- the agent is busy and should not be disturbed.
 * However, special case, or extreme urgency chats may still be offered to the agent.
 * <li>Presence.Mode.AWAY -- the agent is not available and should not
 * have a chat routed to them (equivalent to Presence.Mode.EXTENDED_AWAY).</ul>
 *
 * @param presenceMode the presence mode of the agent.
 * @param status       sets the status message of the presence update.
 * @throws XMPPErrorException if there was an XMPP error returned.
 * @throws NoResponseException if there was no response from the remote entity.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws IllegalStateException if the agent is not online with the workgroup.
 */
public void setStatus(Presence.Mode presenceMode, String status) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    if (!online) {
        throw new IllegalStateException("Cannot set status when the agent is not online.");
    }

    if (presenceMode == null) {
        presenceMode = Presence.Mode.available;
    }
    this.presenceMode = presenceMode;

    PresenceBuilder presenceBuilder = connection.getStanzaFactory().buildPresenceStanza()
            .ofType(Presence.Type.available)
            .setMode(presenceMode)
            .to(getWorkgroupJID());

    if (status != null) {
        presenceBuilder.setStatus(status);
    }

    Presence presence = presenceBuilder.build();
    presence.addExtension(new MetaData(this.metaData));

    StanzaCollector collector = this.connection.createStanzaCollectorAndSend(new AndFilter(new StanzaTypeFilter(Presence.class),
            FromMatchesFilter.create(workgroupJID)), presence);

    collector.nextResultOrThrow();
}
 
Example 7
Source File: AgentSession.java    From Smack with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the agent's current status with the workgroup. The presence mode affects how offers
 * are routed to the agent. The possible presence modes with their meanings are as follows:<ul>
 *
 * <li>Presence.Mode.AVAILABLE -- (Default) the agent is available for more chats
 * (equivalent to Presence.Mode.CHAT).
 * <li>Presence.Mode.DO_NOT_DISTURB -- the agent is busy and should not be disturbed.
 * However, special case, or extreme urgency chats may still be offered to the agent.
 * <li>Presence.Mode.AWAY -- the agent is not available and should not
 * have a chat routed to them (equivalent to Presence.Mode.EXTENDED_AWAY).</ul>
 *
 * The max chats value is the maximum number of chats the agent is willing to have routed to
 * them at once. Some servers may be configured to only accept max chat values in a certain
 * range; for example, between two and five. In that case, the maxChats value the agent sends
 * may be adjusted by the server to a value within that range.
 *
 * @param presenceMode the presence mode of the agent.
 * @param maxChats     the maximum number of chats the agent is willing to accept.
 * @param status       sets the status message of the presence update.
 * @throws XMPPErrorException if there was an XMPP error returned.
 * @throws NoResponseException if there was no response from the remote entity.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws IllegalStateException if the agent is not online with the workgroup.
 */
public void setStatus(Presence.Mode presenceMode, int maxChats, String status)
                throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    if (!online) {
        throw new IllegalStateException("Cannot set status when the agent is not online.");
    }

    if (presenceMode == null) {
        presenceMode = Presence.Mode.available;
    }
    this.presenceMode = presenceMode;
    this.maxChats = maxChats;

    PresenceBuilder presenceBuilder = connection.getStanzaFactory().buildPresenceStanza()
            .ofType(Presence.Type.available)
            .setMode(presenceMode)
            .to(workgroupJID)
            .setStatus(status)
            ;

    // Send information about max chats and current chats as a packet extension.
    StandardExtensionElement.Builder builder = StandardExtensionElement.builder(AgentStatus.ELEMENT_NAME,
            AgentStatus.NAMESPACE);
    builder.addElement("max_chats", Integer.toString(maxChats));
    presenceBuilder.addExtension(builder.build());
    presenceBuilder.addExtension(new MetaData(this.metaData));

    Presence presence = presenceBuilder.build();
    StanzaCollector collector = this.connection.createStanzaCollectorAndSend(new AndFilter(
                    new StanzaTypeFilter(Presence.class),
                    FromMatchesFilter.create(workgroupJID)), presence);

    collector.nextResultOrThrow();
}
 
Example 8
Source File: Friend.java    From League-of-Legends-XMPP-Chat-Library with MIT License 5 votes vote down vote up
/**
 * Returns the current ChatMode of this friend (e.g. away, busy, available).
 * 
 * @see ChatMode
 * @return ChatMode of this friend
 */
public ChatMode getChatMode() {
	final Presence.Mode mode = con.getRoster().getPresence(getUserId())
			.getMode();
	for (final ChatMode c : ChatMode.values()) {
		if (c.mode == mode) {
			return c;
		}
	}
	return null;
}
 
Example 9
Source File: InstantMessagingClient.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * change jabber status. Example: sendPresencePacket(Presence.Type.AVAILABLE, "at meeting...", 1, Presence.Mode.AWAY);
 * 
 * @param type
 * @param status
 * @param priority
 * @param mode
 */
public void sendPresence(final Presence.Type type, String status, final int priority, final Presence.Mode mode) {
    // get rid of "&" because they break xml packages!
    if (status != null) {
        status = status.replaceAll("&", "&amp;");
    }
    if (connection == null || !connection.isConnected()) {
        return;
    }
    if (collaborationDisabled) {
        return;
    }

    setStatus(mode);
    final Presence presence = new Presence(type);
    if (status == null) {
        if (mode == Presence.Mode.available) {
            status = InstantMessagingConstants.PRESENCE_MODE_AVAILABLE;
        } else if (mode == Presence.Mode.away) {
            status = InstantMessagingConstants.PRESENCE_MODE_AWAY;
        } else if (mode == Presence.Mode.chat) {
            status = InstantMessagingConstants.PRESENCE_MODE_CHAT;
        } else if (mode == Presence.Mode.dnd) {
            status = InstantMessagingConstants.PRESENCE_MODE_DND;
        } else if (mode == Presence.Mode.xa) {
            status = InstantMessagingConstants.PRESENCE_MODE_XAWAY;
        }
        presence.setStatus(status);
    } else {
        presence.setStatus(status);
    }
    setStatusMsg(presence.getStatus());
    // setting prio when type == unavailable causes error on IM server
    if (presence.getType() == Presence.Type.available) {
        presence.setPriority(priority);
    }
    if (mode != null) {
        presence.setMode(mode);
    }
    try {
        connection.sendPacket(presence);
    } catch (final RuntimeException ex) {
        log.warn("Error while trying to send Instant Messaging packet for user: " + userInfo.getUsername() + " .Errormessage: ", ex);
    }
}
 
Example 10
Source File: PresenceManager.java    From Spark with Apache License 2.0 4 votes vote down vote up
/**
    * Returns the icon associated with a users presence.
    *
    * @param presence the users presence.
    * @return the icon associated with it.
    */
   public static Icon getIconFromPresence(Presence presence) {
if (isInvisible(presence)) {
           return SparkRes.getImageIcon(SparkRes.CLEAR_BALL_ICON);
       }

       // Handle offline presence
       if (!presence.isAvailable()) {
           return SparkRes.getImageIcon(SparkRes.CLEAR_BALL_ICON);
       }

       Presence.Mode presenceMode = presence.getMode();
       if (presenceMode == null) {
           presenceMode = Presence.Mode.available;
       }

       Icon icon = null;

       if (presenceMode.equals(Presence.Mode.available)) {
           icon = SparkRes.getImageIcon(SparkRes.GREEN_BALL);
       }
       else if (presenceMode.equals(Presence.Mode.chat)) {
           icon = SparkRes.getImageIcon(SparkRes.FREE_TO_CHAT_IMAGE);
       }
       else if (isOnPhone(presence)) {
           icon = SparkRes.getImageIcon(SparkRes.ON_PHONE_IMAGE);
       }
       else if (presenceMode.equals(Presence.Mode.away)) {
           icon = SparkRes.getImageIcon(SparkRes.IM_AWAY);
       }
       else if (presenceMode.equals(Presence.Mode.dnd)) {
           icon = SparkRes.getImageIcon(SparkRes.IM_DND);
       }
       else if (presenceMode.equals(Presence.Mode.xa)) {
           icon = SparkRes.getImageIcon(SparkRes.IM_AWAY);
       }

       // Check For ContactItem handlers
       Icon handlerIcon = SparkManager.getChatManager().getTabIconForContactHandler(presence);
       if (handlerIcon != null) {
           icon = handlerIcon;
       }


       return icon;
   }
 
Example 11
Source File: InstantMessagingClient.java    From olat with Apache License 2.0 4 votes vote down vote up
public Presence.Mode getPresenceMode() {
    return presenceMode;
}
 
Example 12
Source File: InstantMessagingClient.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * change jabber status. Example: sendPresencePacket(Presence.Type.AVAILABLE, "at meeting...", 1, Presence.Mode.AWAY);
 * 
 * @param type
 * @param status
 * @param priority
 * @param mode
 */
public void sendPresence(final Presence.Type type, String status, final int priority, final Presence.Mode mode) {
    // get rid of "&" because they break xml packages!
    if (status != null) {
        status = status.replaceAll("&", "&amp;");
    }
    if (connection == null || !connection.isConnected()) {
        return;
    }
    if (collaborationDisabled) {
        return;
    }

    setStatus(mode);
    final Presence presence = new Presence(type);
    if (status == null) {
        if (mode == Presence.Mode.available) {
            status = InstantMessagingConstants.PRESENCE_MODE_AVAILABLE;
        } else if (mode == Presence.Mode.away) {
            status = InstantMessagingConstants.PRESENCE_MODE_AWAY;
        } else if (mode == Presence.Mode.chat) {
            status = InstantMessagingConstants.PRESENCE_MODE_CHAT;
        } else if (mode == Presence.Mode.dnd) {
            status = InstantMessagingConstants.PRESENCE_MODE_DND;
        } else if (mode == Presence.Mode.xa) {
            status = InstantMessagingConstants.PRESENCE_MODE_XAWAY;
        }
        presence.setStatus(status);
    } else {
        presence.setStatus(status);
    }
    setStatusMsg(presence.getStatus());
    // setting prio when type == unavailable causes error on IM server
    if (presence.getType() == Presence.Type.available) {
        presence.setPriority(priority);
    }
    if (mode != null) {
        presence.setMode(mode);
    }
    try {
        connection.sendPacket(presence);
    } catch (final RuntimeException ex) {
        log.warn("Error while trying to send Instant Messaging packet for user: " + userInfo.getUsername() + " .Errormessage: ", ex);
    }
}
 
Example 13
Source File: InstantMessagingClient.java    From olat with Apache License 2.0 4 votes vote down vote up
public Presence.Mode getPresenceMode() {
    return presenceMode;
}
 
Example 14
Source File: ClientHelper.java    From olat with Apache License 2.0 2 votes vote down vote up
/**
 * send a presence packet "available" with a certain mode e.g. "away" to all buddies
 * 
 * @param mode
 */
public void sendPresenceAvailable(final Presence.Mode mode) {
    imc.sendPresence(Presence.Type.available, null, 0, mode);
}
 
Example 15
Source File: InstantMessagingClient.java    From olat with Apache License 2.0 2 votes vote down vote up
/**
 * The status to set.
 * 
 * @param status
 */
protected void setStatus(final Presence.Mode status) {
    this.presenceMode = status;
}
 
Example 16
Source File: AgentSession.java    From Smack with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the agent's current presence mode.
 *
 * @return the agent's current presence mode.
 */
public Presence.Mode getPresenceMode() {
    return presenceMode;
}
 
Example 17
Source File: AgentSession.java    From Smack with Apache License 2.0 2 votes vote down vote up
/**
 * Sets the agent's current status with the workgroup. The presence mode affects
 * how offers are routed to the agent. The possible presence modes with their
 * meanings are as follows:<ul>
 *
 * <li>Presence.Mode.AVAILABLE -- (Default) the agent is available for more chats
 * (equivalent to Presence.Mode.CHAT).
 * <li>Presence.Mode.DO_NOT_DISTURB -- the agent is busy and should not be disturbed.
 * However, special case, or extreme urgency chats may still be offered to the agent.
 * <li>Presence.Mode.AWAY -- the agent is not available and should not
 * have a chat routed to them (equivalent to Presence.Mode.EXTENDED_AWAY).</ul>
 *
 * The max chats value is the maximum number of chats the agent is willing to have
 * routed to them at once. Some servers may be configured to only accept max chat
 * values in a certain range; for example, between two and five. In that case, the
 * maxChats value the agent sends may be adjusted by the server to a value within that
 * range.
 *
 * @param presenceMode the presence mode of the agent.
 * @param maxChats     the maximum number of chats the agent is willing to accept.
 * @throws XMPPException         if an error occurs setting the agent status.
 * @throws SmackException if Smack detected an exceptional situation.
 * @throws InterruptedException if the calling thread was interrupted.
 * @throws IllegalStateException if the agent is not online with the workgroup.
 */
public void setStatus(Presence.Mode presenceMode, int maxChats) throws XMPPException, SmackException, InterruptedException {
    setStatus(presenceMode, maxChats, null);
}
 
Example 18
Source File: InstantMessagingClient.java    From olat with Apache License 2.0 2 votes vote down vote up
/**
 * send a presence packet "available" with a certain mode e.g. "away" to all buddies
 * 
 * @param mode
 */
public void sendPresenceAvailable(final Presence.Mode mode) {
    sendPresence(Presence.Type.available, null, 0, mode);
}
 
Example 19
Source File: ClientHelper.java    From olat with Apache License 2.0 2 votes vote down vote up
/**
 * send a presence packet "available" with a certain mode e.g. "away" to all buddies
 * 
 * @param mode
 */
public void sendPresenceAvailable(final Presence.Mode mode) {
    imc.sendPresence(Presence.Type.available, null, 0, mode);
}
 
Example 20
Source File: InstantMessagingClient.java    From olat with Apache License 2.0 2 votes vote down vote up
/**
 * send a presence packet "available" with a certain mode e.g. "away" to all buddies
 * 
 * @param mode
 */
public void sendPresenceAvailable(final Presence.Mode mode) {
    sendPresence(Presence.Type.available, null, 0, mode);
}