📄 vcardmanager.java.svn-base
字号:
*/
public VCard getVCard(String jid) {
return getVCard(jid, true);
}
/**
* Loads the vCard from memory. If no vCard is found in memory,
* will add it to a loading queue for future loading. Users of this method
* should only use it if the correct vCard is not important the first time around.
*
* @param jid the users jid.
* @return the users VCard or an empty VCard.
*/
public VCard getVCardFromMemory(String jid) {
// Check in memory first.
if (vcards.containsKey(jid)) {
return vcards.get(jid);
}
// if not in memory
VCard vcard = loadFromFileSystem(jid);
if (vcard == null) {
addToQueue(jid);
// Create temp vcard.
vcard = new VCard();
vcard.setJabberId(jid);
}
return vcard;
}
/**
* Returns the VCard.
*
* @param jid the users jid.
* @param useCache true to check in cache, otherwise false will do a new network vcard operation.
* @return the VCard.
*/
public VCard getVCard(String jid, boolean useCache) {
jid = StringUtils.parseBareAddress(jid);
if (!vcards.containsKey(jid) || !useCache) {
VCard vcard = new VCard();
try {
// Check File system first
VCard localVCard = loadFromFileSystem(jid);
if (localVCard != null) {
localVCard.setJabberId(jid);
vcards.put(jid, localVCard);
return localVCard;
}
// Otherwise retrieve vCard from server and persist back out.
vcard.load(SparkManager.getConnection(), jid);
vcard.setJabberId(jid);
vcards.put(jid, vcard);
}
catch (XMPPException e) {
vcard.setJabberId(jid);
//Log.warning("Unable to load vcard for " + jid, e);
vcard.setError(new XMPPError(409));
vcards.put(jid, vcard);
}
// Persist XML
persistVCard(jid, vcard);
}
return vcards.get(jid);
}
/**
* Forces a reload of a <code>VCard</code>.
*
* @param jid the jid of the user.
* @return the new VCard.
*/
public VCard reloadVCard(String jid) {
jid = StringUtils.parseBareAddress(jid);
VCard vcard = new VCard();
try {
vcard.load(SparkManager.getConnection(), jid);
vcard.setJabberId(jid);
vcards.put(jid, vcard);
}
catch (XMPPException e) {
vcard.setError(new XMPPError(409));
vcards.put(jid, vcard);
}
// Persist XML
persistVCard(jid, vcard);
return vcards.get(jid);
}
/**
* Adds a new vCard to the cache.
*
* @param jid the jid of the user.
* @param vcard the users vcard to cache.
*/
public void addVCard(String jid, VCard vcard) {
vcard.setJabberId(jid);
vcards.put(jid, vcard);
}
/**
* Scales an image to the preferred avatar size.
*
* @param icon the icon to scale.
* @return the scaled version of the image.
*/
public static ImageIcon scale(ImageIcon icon) {
Image avatarImage = icon.getImage();
if (icon.getIconHeight() > 64 || icon.getIconWidth() > 64) {
avatarImage = avatarImage.getScaledInstance(-1, 64, Image.SCALE_SMOOTH);
}
return new ImageIcon(avatarImage);
}
/**
* Returns the URL of the avatar image associated with the users JID.
*
* @param jid the jid of the user.
* @return the URL of the image. If not image is found, a default avatar is returned.
*/
public URL getAvatar(String jid) {
// Handle own avatar file.
if (jid != null && StringUtils.parseBareAddress(SparkManager.getSessionManager().getJID()).equals(StringUtils.parseBareAddress(jid))) {
if (imageFile.exists()) {
try {
return imageFile.toURL();
}
catch (MalformedURLException e) {
Log.error(e);
}
}
else {
return SparkRes.getURL(SparkRes.DUMMY_CONTACT_IMAGE);
}
}
// Handle other users JID
ContactItem item = SparkManager.getWorkspace().getContactList().getContactItemByJID(jid);
URL avatarURL = null;
if (item != null) {
try {
avatarURL = item.getAvatarURL();
}
catch (MalformedURLException e) {
Log.error(e);
}
}
if (avatarURL == null) {
return SparkRes.getURL(SparkRes.DUMMY_CONTACT_IMAGE);
}
return avatarURL;
}
/**
* Searches all vCards for a specified phone number.
*
* @param phoneNumber the phoneNumber.
* @return the vCard which contains the phone number.
*/
public VCard searchPhoneNumber(String phoneNumber) {
for (VCard vcard : vcards.values()) {
String homePhone = getNumbersFromPhone(vcard.getPhoneHome("VOICE"));
String workPhone = getNumbersFromPhone(vcard.getPhoneWork("VOICE"));
String cellPhone = getNumbersFromPhone(vcard.getPhoneWork("CELL"));
String query = getNumbersFromPhone(phoneNumber);
if ((homePhone != null && homePhone.contains(query)) ||
(workPhone != null && workPhone.contains(query)) ||
(cellPhone != null && cellPhone.contains(query))) {
return vcard;
}
}
return null;
}
/**
* Parses out the numbers only from a phone number.
*
* @param number the full phone number.
* @return the phone number only (5551212)
*/
public static String getNumbersFromPhone(String number) {
if (number == null) {
return null;
}
number = number.replace("-", "");
number = number.replace("(", "");
number = number.replace(")", "");
number = number.replace(" ", "");
if (number.startsWith("1")) {
number = number.substring(1);
}
return number;
}
/**
* Sets the personal vcard of the user.
*
* @param vcard the users vCard.
*/
public void setPersonalVCard(VCard vcard) {
this.personalVCard = vcard;
}
public URL getAvatarURL(String jid) {
VCard vcard = getVCard(jid, true);
if (vcard != null) {
String hash = vcard.getAvatarHash();
if (!ModelUtil.hasLength(hash)) {
return null;
}
final File avatarFile = new File(contactsDir, hash);
try {
return avatarFile.toURL();
}
catch (MalformedURLException e) {
Log.error(e);
}
}
return null;
}
/**
* Persist vCard information out for caching.
*
* @param jid the users jid.
* @param vcard the users vcard.
*/
private void persistVCard(String jid, VCard vcard) {
String fileName = Base64.encodeBytes(jid.getBytes());
byte[] bytes = vcard.getAvatar();
if (bytes != null) {
vcard.setAvatar(bytes);
try {
String hash = vcard.getAvatarHash();
final File avatarFile = new File(contactsDir, hash);
ImageIcon icon = new ImageIcon(bytes);
icon = VCardManager.scale(icon);
if (icon != null && icon.getIconWidth() != -1) {
BufferedImage image = GraphicUtils.convert(icon.getImage());
if (image == null) {
Log.warning("Unable to write out avatar for " + jid);
}
else {
ImageIO.write(image, "PNG", avatarFile);
}
}
}
catch (Exception e) {
Log.error("Unable to update avatar in Contact Item.", e);
}
}
// Set timestamp
vcard.setField("timestamp", Long.toString(System.currentTimeMillis()));
final String xml = vcard.toString();
File vcardFile = new File(vcardStorageDirectory, fileName);
// write xml to file
try {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(vcardFile), "UTF-8"));
out.write(xml);
out.close();
}
catch (IOException e) {
Log.error(e);
}
}
/**
* Attempts to load
*
* @param jid the jid of the user.
* @return the VCard if found, otherwise null.
*/
private VCard loadFromFileSystem(String jid) {
// Unescape JID
String fileName = Base64.encodeBytes(jid.getBytes());
final File vcardFile = new File(vcardStorageDirectory, fileName);
if (!vcardFile.exists()) {
return null;
}
try {
// Otherwise load from file system.
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(vcardFile), "UTF-8"));
VCardProvider provider = new VCardProvider();
parser.setInput(in);
VCard vcard = (VCard)provider.parseIQ(parser);
// Check to see if the file is older 10 minutes. If so, reload.
String timestamp = vcard.getField("timestamp");
if (timestamp != null) {
long time = Long.parseLong(timestamp);
long now = System.currentTimeMillis();
long hour = (1000 * 60) * 60;
if (now - time > hour) {
addToQueue(jid);
}
}
vcard.setJabberId(jid);
vcards.put(jid, vcard);
return vcard;
}
catch (Exception e) {
Log.warning("Unable to load vCard for " + jid, e);
}
return null;
}
/**
* Add <code>VCardListener</code>.
*
* @param listener the listener to add.
*/
public void addVCardListener(VCardListener listener) {
listeners.add(listener);
}
/**
* Remove <code>VCardListener</code>.
*
* @param listener the listener to remove.
*/
public void removeVCardListener(VCardListener listener) {
listeners.remove(listener);
}
/**
* Notify all <code>VCardListener</code> implementations.
*/
protected void notifyVCardListeners() {
for (VCardListener listener : listeners) {
listener.vcardChanged(personalVCard);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -