📄 bspresenceinfo.java
字号:
package edu.ou.kmi.buddyspace.core;
/*
* BSPresenceBean.java
*
* Project: BuddySpace
* (C) Copyright Knowledge Media Institute 2002
*
*
* Created on 18 July 2002, 15:32
*/
import java.util.*;
import org.jabber.jabberbeans.*;
import org.jabber.jabberbeans.util.*;
//import org.jabber.jabberbeans.Extension.*;
/**
* <code>BSPresenceInfo</code> contains presence information.
* It includes <code>JID</code>, availability, show and status information.
*
* @author Jiri Komzak, Knowledge Media Institute, Open University, United Kingdom
*/
public class BSPresenceInfo {
protected String show = null;
protected String status = null;
protected int priority = 0;
protected boolean prioritySet = false;
protected JID jid = null;
protected boolean available = false;
protected boolean myself = false;
protected boolean lurker = false;
/** no priority flag from jabberbeans */
public final static int NO_PRIORITY = -66666;
/** types */
public final static String TYPE_AVAILABLE = "available";
public final static String TYPE_UNAVAILABLE = "unavailable";
/** presence show constants */
public final static String SHOW_ONLINE = "";
public final static String SHOW_AWAY = "away";
public final static String SHOW_CHAT = "chat";
public final static String SHOW_DND = "dnd";
public final static String SHOW_XA = "xa";
/** constants for friendly show text */
public static final String FRIENDLY_SHOW_ONLINE = "Online";
public static final String FRIENDLY_SHOW_CHAT = "Free for chat";
public static final String FRIENDLY_SHOW_AWAY = "Away";
public static final String FRIENDLY_SHOW_XA = "Extended away";
public static final String FRIENDLY_SHOW_DND = "Do not disturb";
public static final String FRIENDLY_SHOW_OFFLINE = "Offline";
public static final String FRIENDLY_SHOW_LURKER = "Not in roster";
//public static String FRIENDLY_SHOW_MYSELF = "Myself";
/**
* Constructs <code>BSPresenceInfo</code> from <code>Presence</code>
* packet.
*/
public BSPresenceInfo(Presence p) {
if (p == null) return;
jid = p.getFromAddress();
if (jid == null) return;
String type = p.getType();
available = (type == null || type.equals("") || type.equals(TYPE_AVAILABLE));
if (available)
show = p.getStateShow();
status = p.getStatus();
priority = p.getPriority();
prioritySet = (priority != NO_PRIORITY);
}
/**
* Constructor
*/
public BSPresenceInfo(JID jid, boolean available, String show, String status) {
this.jid = jid;
this.available = available;
if (SHOW_ONLINE.equals(show) || SHOW_CHAT.equals(show) ||
SHOW_AWAY.equals(show) || SHOW_XA.equals(show) ||
SHOW_DND.equals(show))
this.show = show;
else
this.show = null;
this.status = status;
prioritySet = false;
}
/** Returns new presence packet representing this. */
public Presence getPresencePacket(JID toAddress)
throws java.lang.InstantiationException {
PresenceBuilder pb = new PresenceBuilder();
if (toAddress != null)
pb.setToAddress(toAddress);
if (available) {
if (prioritySet)
pb.setPriority(priority);
if (show != null)
pb.setStateShow(show);
if (status != null)
pb.setStatus(status);
}
else {
pb.setType(TYPE_UNAVAILABLE);
if (status != null)
pb.setStatus(status);
}
return (Presence) pb.build();
}
/** Returns if buddy is available/on-line */
public boolean isOnline() {
return available;
}
/** Returns show */
public String getShow() {
return show;
}
/** Returns status */
public String getStatus() {
return status;
}
/** Returns friendly form of show for displaying */
public String getFriendlyShow() {
if (lurker && !available)
return FRIENDLY_SHOW_LURKER;
else if (!available)
return FRIENDLY_SHOW_OFFLINE;
else if (show == null || SHOW_ONLINE.equals(show))
return FRIENDLY_SHOW_ONLINE;
else if (SHOW_CHAT.equals(show))
return FRIENDLY_SHOW_CHAT;
else if (SHOW_AWAY.equals(show))
return FRIENDLY_SHOW_AWAY;
else if (SHOW_XA.equals(show))
return FRIENDLY_SHOW_XA;
else if (SHOW_DND.equals(show))
return FRIENDLY_SHOW_DND;
else
return FRIENDLY_SHOW_LURKER;
}
/**
* Sets if the buddy is in roster.
* Typically used to set that the buddy is NOT in roster.
*/
public void setIsInRoster(boolean inRoster) {
lurker = !inRoster;
}
/**
* Returns if the buddy is in roster.
*/
public boolean isInRoster() {
return !lurker;
}
/**
* Sets if the buddy is myself it means the currently logged one.
*/
public void setIsMyself(boolean isMyself) {
myself = isMyself;
}
/**
* Returns if the buddy is myself it means the currently logged one.
*/
public boolean isMyself() {
return myself;
}
/**
* Sets priority level being set by this presence.
*/
public void setPriority(int priority) {
this.priority = priority;
prioritySet = (priority != NO_PRIORITY);
}
/**
* Returns priority level being set by this presence.
*/
public int getPriority() {
return priority;
}
/** Returns JID of the buddy */
public JID getJID() {
return jid;
}
/** Sets JID of the buddy */
public void setJID(JID jid) {
this.jid = jid;
}
/** Returns if this is better (not equal) presence */
public boolean isBetterPresence(BSPresenceInfo pi) {
if (pi == null) return true;
if (!available) return false;
if (available && !pi.isOnline()) return true;
int thisShow = 3;
if (SHOW_XA.equals(show)) thisShow = 0;
else if (SHOW_AWAY.equals(show)) thisShow = 1;
else if (SHOW_DND.equals(show)) thisShow = 2;
else if (SHOW_ONLINE.equals(show)) thisShow = 3;
else if (SHOW_CHAT.equals(show)) thisShow = 4;
int piShow = 3;
String piShowStr = pi.getShow();
if (SHOW_XA.equals(piShowStr)) piShow = 0;
else if (SHOW_AWAY.equals(piShowStr)) piShow = 1;
else if (SHOW_DND.equals(piShowStr)) piShow = 2;
else if (SHOW_ONLINE.equals(piShowStr)) piShow = 3;
else if (SHOW_CHAT.equals(piShowStr)) piShow = 4;
return thisShow > piShow;
}
/** Returns if this has the same show status */
public boolean isEqualShow(BSPresenceInfo pi) {
if (pi == null) return false;
if (available != pi.isOnline()) return false;
if (show == null)
return pi.show == null;
else
return show.equals(pi.show);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -