summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlan Young <consult.awy@gmail.com>2015-07-06 08:14:13 (GMT)
committerKevin Smith <git@kismith.co.uk>2015-08-13 08:05:42 (GMT)
commit9bf162922691c1f45813da6a21f2e274bfad3114 (patch)
tree2d7c49abc72ede17701c3300140718b1c3e81bcc
parentaf3bb03053b9d83f4d38b31d66b292792206a327 (diff)
downloadstroke-9bf162922691c1f45813da6a21f2e274bfad3114.zip
stroke-9bf162922691c1f45813da6a21f2e274bfad3114.tar.bz2
VCard.getPhoto() to return null for absent photo.
Most of the time when VCard.getPhoto() is called it is only used to determine presence or absence of a photo. Thus, it makes no sense to allocate an empty ByteArray() just for this purpose. Return null instead, if absent. Change-Id: I3bae02ffbece5589c74c950bbb4c0db6c0376c76
-rw-r--r--README.md (renamed from README)8
-rwxr-xr-xsrc/com/isode/stroke/avatars/VCardAvatarManager.java10
-rwxr-xr-xsrc/com/isode/stroke/avatars/VCardUpdateAvatarManager.java9
-rw-r--r--src/com/isode/stroke/elements/VCard.java17
-rw-r--r--src/com/isode/stroke/serializer/payloadserializers/VCardSerializer.java4
-rw-r--r--src/com/isode/stroke/vcards/VCardStorage.java14
6 files changed, 39 insertions, 23 deletions
diff --git a/README b/README.md
index 0574db7..ee28436 100644
--- a/README
+++ b/README.md
@@ -1,4 +1,4 @@
-Stroke
+# Stroke
Stroke is a port of the C++ Swift library ( http://swift.im/swiften/ )
The source is available from the Git repository at http://swift.im/git/stroke/
@@ -22,3 +22,9 @@ The included Makefile should, on Unixes with make/curl installed, grab the depen
For development:
If you want to commit changes to Stroke, first run `make .git/hooks/commit-msg` to download a script that will generate change-ids needed by our review system.
+
+## Differences from Swiften
+
+Stroke tries to be a clean and accurate port of Swiften, in order to facilitate mirroring changes. Sometimes differences are either necessary or desirable.
+
+* `VCard.getPhoto()` returns null instead of an empty `ByteArray` when there is no photo.
diff --git a/src/com/isode/stroke/avatars/VCardAvatarManager.java b/src/com/isode/stroke/avatars/VCardAvatarManager.java
index 83c8c64..a924676 100755
--- a/src/com/isode/stroke/avatars/VCardAvatarManager.java
+++ b/src/com/isode/stroke/avatars/VCardAvatarManager.java
@@ -17,6 +17,7 @@ import com.isode.stroke.elements.VCard;
import com.isode.stroke.crypto.CryptoProvider;
import com.isode.stroke.stringcodecs.Hexify;
import com.isode.stroke.avatars.AvatarStorage;
+import com.isode.stroke.base.ByteArray;
import com.isode.stroke.muc.MUCRegistry;
import com.isode.stroke.vcards.VCardManager;
import com.isode.stroke.signals.Slot2;
@@ -62,16 +63,17 @@ public class VCardAvatarManager extends AvatarProvider {
String hash = vcardManager_.getPhotoHash(avatarJID);
if(hash.length() != 0) {
if (!avatarStorage_.hasAvatar(hash)) {
- VCard vCard = vcardManager_.getVCard(avatarJID);
- if (vCard != null) {
- String newHash = Hexify.hexify(crypto_.getSHA1Hash(vCard.getPhoto()));
+ final VCard vCard = vcardManager_.getVCard(avatarJID);
+ final ByteArray photo = vCard != null ? vCard.getPhoto() : null;
+ if (photo != null) {
+ String newHash = Hexify.hexify(crypto_.getSHA1Hash(photo));
if (!newHash.equals(hash)) {
// Shouldn't happen, but sometimes seem to. Might be fixed if we
// move to a safer backend.
logger_.warning("Inconsistent vCard photo hash cache");
hash = newHash;
}
- avatarStorage_.addAvatar(hash, vCard.getPhoto());
+ avatarStorage_.addAvatar(hash, photo);
}
else {
// Can happen if the cache is inconsistent.
diff --git a/src/com/isode/stroke/avatars/VCardUpdateAvatarManager.java b/src/com/isode/stroke/avatars/VCardUpdateAvatarManager.java
index 5328fb4..681e94b 100755
--- a/src/com/isode/stroke/avatars/VCardUpdateAvatarManager.java
+++ b/src/com/isode/stroke/avatars/VCardUpdateAvatarManager.java
@@ -24,12 +24,14 @@ import com.isode.stroke.client.StanzaChannel;
import com.isode.stroke.crypto.CryptoProvider;
import com.isode.stroke.stringcodecs.Hexify;
import com.isode.stroke.avatars.AvatarStorage;
+import com.isode.stroke.base.ByteArray;
import com.isode.stroke.muc.MUCRegistry;
import com.isode.stroke.vcards.VCardManager;
import com.isode.stroke.signals.Slot2;
import com.isode.stroke.signals.Slot1;
import java.util.logging.Logger;
+
import com.isode.stroke.signals.SignalConnection;
public class VCardUpdateAvatarManager extends AvatarProvider {
@@ -124,13 +126,14 @@ public class VCardUpdateAvatarManager extends AvatarProvider {
return;
}
- if (vCard.getPhoto().isEmpty()) {
+ final ByteArray photo = vCard.getPhoto();
+ if (photo == null) {
setAvatarHash(from, "");
}
else {
- String hash = Hexify.hexify(crypto_.getSHA1Hash(vCard.getPhoto()));
+ String hash = Hexify.hexify(crypto_.getSHA1Hash(photo));
if (!avatarStorage_.hasAvatar(hash)) {
- avatarStorage_.addAvatar(hash, vCard.getPhoto());
+ avatarStorage_.addAvatar(hash, photo);
}
setAvatarHash(from, hash);
}
diff --git a/src/com/isode/stroke/elements/VCard.java b/src/com/isode/stroke/elements/VCard.java
index 3bd4db2..3e64c7c 100644
--- a/src/com/isode/stroke/elements/VCard.java
+++ b/src/com/isode/stroke/elements/VCard.java
@@ -131,14 +131,17 @@ public class VCard extends Payload implements Serializable {
public void setNickname(final String nick) { nick_ = nick; }
public final String getNickname() { return nick_; }
- public void setPhoto(final ByteArray photo) { photo_ = photo; }
+ /**
+ * Set the photo, but store null if the photo is empty.
+ */
+ public void setPhoto(final ByteArray photo) { photo_ = photo != null && !photo.isEmpty() ? photo : null; }
+ /**
+ * Get the photo.
+ * Unlike Swiften, this will return null for empty photos,
+ * instead of an empty ByteArray.
+ */
public final ByteArray getPhoto() {
- if(this.photo_ != null) {
- return photo_;
- }
- else {
- return new ByteArray();
- }
+ return photo_;
}
public void setPhotoType(final String photoType) { photoType_ = photoType; }
diff --git a/src/com/isode/stroke/serializer/payloadserializers/VCardSerializer.java b/src/com/isode/stroke/serializer/payloadserializers/VCardSerializer.java
index 9d460fa..9630d80 100644
--- a/src/com/isode/stroke/serializer/payloadserializers/VCardSerializer.java
+++ b/src/com/isode/stroke/serializer/payloadserializers/VCardSerializer.java
@@ -74,12 +74,12 @@ public class VCardSerializer extends GenericPayloadSerializer<VCard>{
if (vcard.getNickname() != null && !vcard.getNickname().isEmpty()) {
queryElement.addNode(new XMLElement("NICKNAME", "", vcard.getNickname()));
}
- if (vcard.getPhoto() != null && !vcard.getPhoto().isEmpty() || vcard.getPhotoType() != null && !vcard.getPhotoType().isEmpty()) {
+ if (vcard.getPhoto() != null || vcard.getPhotoType() != null && !vcard.getPhotoType().isEmpty()) {
XMLElement photoElement = new XMLElement("PHOTO");
if (vcard.getPhotoType() != null && !vcard.getPhotoType().isEmpty()) {
photoElement.addNode(new XMLElement("TYPE", "", vcard.getPhotoType()));
}
- if (vcard.getPhoto() != null && !vcard.getPhoto().isEmpty()) {
+ if (vcard.getPhoto() != null) {
photoElement.addNode(new XMLElement("BINVAL", "", Base64.encode(vcard.getPhoto())));
}
queryElement.addNode(photoElement);
diff --git a/src/com/isode/stroke/vcards/VCardStorage.java b/src/com/isode/stroke/vcards/VCardStorage.java
index cb732ea..5ac095c 100644
--- a/src/com/isode/stroke/vcards/VCardStorage.java
+++ b/src/com/isode/stroke/vcards/VCardStorage.java
@@ -4,6 +4,7 @@
*/
package com.isode.stroke.vcards;
+import com.isode.stroke.base.ByteArray;
import com.isode.stroke.crypto.CryptoProvider;
import com.isode.stroke.elements.VCard;
import com.isode.stroke.jid.JID;
@@ -24,12 +25,13 @@ public abstract class VCardStorage {
public void delete() {};
public String getPhotoHash(final JID jid) {
- VCard vCard = getVCard(jid);
- if (vCard != null && vCard.getPhoto().getSize() != 0) {
- return Hexify.hexify(crypto.getSHA1Hash(vCard.getPhoto()));
- }
- else {
- return "";
+ final VCard vCard = getVCard(jid);
+ if (vCard != null) {
+ final ByteArray photo = vCard.getPhoto();
+ if (photo != null) {
+ return Hexify.hexify(crypto.getSHA1Hash(photo));
+ }
}
+ return "";
}
}