diff options
Diffstat (limited to 'src/main/java/eu/siacs/conversations/entities/Conversation.java')
-rw-r--r-- | src/main/java/eu/siacs/conversations/entities/Conversation.java | 281 |
1 files changed, 226 insertions, 55 deletions
diff --git a/src/main/java/eu/siacs/conversations/entities/Conversation.java b/src/main/java/eu/siacs/conversations/entities/Conversation.java index a7da0bc2..470bd290 100644 --- a/src/main/java/eu/siacs/conversations/entities/Conversation.java +++ b/src/main/java/eu/siacs/conversations/entities/Conversation.java @@ -16,12 +16,15 @@ import org.json.JSONObject; import java.security.interfaces.DSAPublicKey; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.List; +import eu.siacs.conversations.Config; import eu.siacs.conversations.xmpp.jid.InvalidJidException; import eu.siacs.conversations.xmpp.jid.Jid; -public class Conversation extends AbstractEntity { +public class Conversation extends AbstractEntity implements Blockable { public static final String TABLENAME = "conversations"; public static final int STATUS_AVAILABLE = 0; @@ -43,6 +46,7 @@ public class Conversation extends AbstractEntity { public static final String ATTRIBUTE_NEXT_ENCRYPTION = "next_encryption"; public static final String ATTRIBUTE_MUC_PASSWORD = "muc_password"; public static final String ATTRIBUTE_MUTED_TILL = "muted_till"; + public static final String ATTRIBUTE_LAST_MESSAGE_TRANSMITTED = "last_message_transmitted"; private String name; private String contactUuid; @@ -72,6 +76,140 @@ public class Conversation extends AbstractEntity { private Bookmark bookmark; + private boolean messagesLeftOnServer = true; + + public boolean hasMessagesLeftOnServer() { + return messagesLeftOnServer; + } + + public void setHasMessagesLeftOnServer(boolean value) { + this.messagesLeftOnServer = value; + } + + public Message findUnsentMessageWithUuid(String uuid) { + synchronized(this.messages) { + for (final Message message : this.messages) { + final int s = message.getStatus(); + if ((s == Message.STATUS_UNSEND || s == Message.STATUS_WAITING) && message.getUuid().equals(uuid)) { + return message; + } + } + } + return null; + } + + public void findWaitingMessages(OnMessageFound onMessageFound) { + synchronized (this.messages) { + for(Message message : this.messages) { + if (message.getStatus() == Message.STATUS_WAITING) { + onMessageFound.onMessageFound(message); + } + } + } + } + + public void findMessagesWithFiles(OnMessageFound onMessageFound) { + synchronized (this.messages) { + for (Message message : this.messages) { + if ((message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) + && message.getEncryption() != Message.ENCRYPTION_PGP) { + onMessageFound.onMessageFound(message); + } + } + } + } + + public Message findMessageWithFileAndUuid(String uuid) { + synchronized (this.messages) { + for (Message message : this.messages) { + if (message.getType() == Message.TYPE_IMAGE + && message.getEncryption() != Message.ENCRYPTION_PGP + && message.getUuid().equals(uuid)) { + return message; + } + } + } + return null; + } + + public void clearMessages() { + synchronized (this.messages) { + this.messages.clear(); + } + } + + public void trim() { + synchronized (this.messages) { + final int size = messages.size(); + final int maxsize = Config.PAGE_SIZE * Config.MAX_NUM_PAGES; + if (size > maxsize) { + this.messages.subList(0, size - maxsize).clear(); + } + } + } + + public void findUnsentMessagesWithOtrEncryption(OnMessageFound onMessageFound) { + synchronized (this.messages) { + for (Message message : this.messages) { + if ((message.getStatus() == Message.STATUS_UNSEND || message.getStatus() == Message.STATUS_WAITING) + && (message.getEncryption() == Message.ENCRYPTION_OTR)) { + onMessageFound.onMessageFound(message); + } + } + } + } + + public void findUnsentTextMessages(OnMessageFound onMessageFound) { + synchronized (this.messages) { + for (Message message : this.messages) { + if (message.getType() != Message.TYPE_IMAGE + && message.getStatus() == Message.STATUS_UNSEND) { + onMessageFound.onMessageFound(message); + } + } + } + } + + public Message findSentMessageWithUuid(String uuid) { + synchronized (this.messages) { + for (Message message : this.messages) { + if (uuid.equals(message.getUuid()) + || (message.getStatus() >= Message.STATUS_SEND && uuid + .equals(message.getRemoteMsgId()))) { + return message; + } + } + } + return null; + } + + public void populateWithMessages(final List<Message> messages) { + synchronized (this.messages) { + messages.clear(); + messages.addAll(this.messages); + } + } + + @Override + public boolean isBlocked() { + return getContact().isBlocked(); + } + + @Override + public boolean isDomainBlocked() { + return getContact().isDomainBlocked(); + } + + @Override + public Jid getBlockedJid() { + return getContact().getBlockedJid(); + } + + + public interface OnMessageFound { + public void onMessageFound(final Message message); + } + public Conversation(final String name, final Account account, final Jid contactJid, final int mode) { this(java.util.UUID.randomUUID().toString(), name, null, account @@ -98,18 +236,11 @@ public class Conversation extends AbstractEntity { } } - public List<Message> getMessages() { - return messages; - } - public boolean isRead() { - return (this.messages == null) || (this.messages.size() == 0) || this.messages.get(this.messages.size() - 1).isRead(); - } + return (this.messages.size() == 0) || this.messages.get(this.messages.size() - 1).isRead(); + } public void markRead() { - if (this.messages == null) { - return; - } for (int i = this.messages.size() - 1; i >= 0; --i) { if (messages.get(i).isRead()) { break; @@ -119,9 +250,6 @@ public class Conversation extends AbstractEntity { } public Message getLatestMarkableMessage() { - if (this.messages == null) { - return null; - } for (int i = this.messages.size() - 1; i >= 0; --i) { if (this.messages.get(i).getStatus() <= Message.STATUS_RECEIVED && this.messages.get(i).markable) { @@ -130,13 +258,13 @@ public class Conversation extends AbstractEntity { } else { return this.messages.get(i); } - } + } } return null; } public Message getLatestMessage() { - if ((this.messages == null) || (this.messages.size() == 0)) { + if (this.messages.size() == 0) { Message message = new Message(this, "", Message.ENCRYPTION_NONE); message.setTime(getCreated()); return message; @@ -158,7 +286,7 @@ public class Conversation extends AbstractEntity { if (generatedName != null) { return generatedName; } else { - return getContactJid().getLocalpart(); + return getJid().getLocalpart(); } } } else { @@ -166,10 +294,6 @@ public class Conversation extends AbstractEntity { } } - public String getProfilePhotoString() { - return this.getContact().getProfilePhoto(); - } - public String getAccountUuid() { return this.accountUuid; } @@ -182,11 +306,12 @@ public class Conversation extends AbstractEntity { return this.account.getRoster().getContact(this.contactJid); } - public void setAccount(Account account) { + public void setAccount(final Account account) { this.account = account; } - public Jid getContactJid() { + @Override + public Jid getJid() { return this.contactJid; } @@ -213,14 +338,14 @@ public class Conversation extends AbstractEntity { } public static Conversation fromCursor(Cursor cursor) { - Jid jid; - try { - jid = Jid.fromString(cursor.getString(cursor.getColumnIndex(CONTACTJID))); - } catch (final InvalidJidException e) { - // Borked DB.. - jid = null; - } - return new Conversation(cursor.getString(cursor.getColumnIndex(UUID)), + Jid jid; + try { + jid = Jid.fromString(cursor.getString(cursor.getColumnIndex(CONTACTJID))); + } catch (final InvalidJidException e) { + // Borked DB.. + jid = null; + } + return new Conversation(cursor.getString(cursor.getColumnIndex(UUID)), cursor.getString(cursor.getColumnIndex(NAME)), cursor.getString(cursor.getColumnIndex(CONTACT)), cursor.getString(cursor.getColumnIndex(ACCOUNT)), @@ -247,9 +372,9 @@ public class Conversation extends AbstractEntity { if (this.otrSession != null) { return this.otrSession; } else { - final SessionID sessionId = new SessionID(this.getContactJid().toBareJid().toString(), - presence, - "xmpp"); + final SessionID sessionId = new SessionID(this.getJid().toBareJid().toString(), + presence, + "xmpp"); this.otrSession = new SessionImpl(sessionId, getAccount().getOtrEngine()); try { if (sendStart) { @@ -288,7 +413,7 @@ public class Conversation extends AbstractEntity { } catch (OtrException e) { this.resetOtrSession(); } - } + } } public boolean endOtrIfNeeded() { @@ -315,30 +440,29 @@ public class Conversation extends AbstractEntity { return this.otrSession != null; } - public String getOtrFingerprint() { + public synchronized String getOtrFingerprint() { if (this.otrFingerprint == null) { try { - if (getOtrSession() == null) { - return ""; + if (getOtrSession() == null || getOtrSession().getSessionStatus() != SessionStatus.ENCRYPTED) { + return null; } - DSAPublicKey remotePubKey = (DSAPublicKey) getOtrSession() - .getRemotePublicKey(); - StringBuilder builder = new StringBuilder( - new OtrCryptoEngineImpl().getFingerprint(remotePubKey)); - builder.insert(8, " "); - builder.insert(17, " "); - builder.insert(26, " "); - builder.insert(35, " "); - this.otrFingerprint = builder.toString(); - } catch (final OtrCryptoException ignored) { - + DSAPublicKey remotePubKey = (DSAPublicKey) getOtrSession().getRemotePublicKey(); + this.otrFingerprint = getAccount().getOtrEngine().getFingerprint(remotePubKey); + } catch (final OtrCryptoException | UnsupportedOperationException ignored) { + return null; } } return this.otrFingerprint; } - public void verifyOtrFingerprint() { - getContact().addOtrFingerprint(getOtrFingerprint()); + public boolean verifyOtrFingerprint() { + final String fingerprint = getOtrFingerprint(); + if (fingerprint != null) { + getContact().addOtrFingerprint(fingerprint); + return true; + } else { + return false; + } } public boolean isOtrFingerprintVerified() { @@ -450,9 +574,11 @@ public class Conversation extends AbstractEntity { } public boolean hasDuplicateMessage(Message message) { - for (int i = this.getMessages().size() - 1; i >= 0; --i) { - if (this.messages.get(i).equals(message)) { - return true; + synchronized (this.messages) { + for (int i = this.messages.size() - 1; i >= 0; --i) { + if (this.messages.get(i).equals(message)) { + return true; + } } } return false; @@ -460,7 +586,7 @@ public class Conversation extends AbstractEntity { public Message findSentMessageWithBody(String body) { synchronized (this.messages) { - for (int i = this.getMessages().size() - 1; i >= 0; --i) { + for (int i = this.messages.size() - 1; i >= 0; --i) { Message message = this.messages.get(i); if ((message.getStatus() == Message.STATUS_UNSEND || message.getStatus() == Message.STATUS_SEND) && message.getBody() != null && message.getBody().equals(body)) { return message; @@ -470,6 +596,31 @@ public class Conversation extends AbstractEntity { } } + public boolean setLastMessageTransmitted(long value) { + long before = getLastMessageTransmitted(); + if (value - before > 1000) { + this.setAttribute(ATTRIBUTE_LAST_MESSAGE_TRANSMITTED, String.valueOf(value)); + return true; + } else { + return false; + } + } + + public long getLastMessageTransmitted() { + long timestamp = getLongAttribute(ATTRIBUTE_LAST_MESSAGE_TRANSMITTED,0); + if (timestamp == 0) { + synchronized (this.messages) { + for(int i = this.messages.size() - 1; i >= 0; --i) { + Message message = this.messages.get(i); + if (message.getStatus() == Message.STATUS_RECEIVED) { + return message.getTimeSent(); + } + } + } + } + return timestamp; + } + public void setMutedTill(long value) { this.setAttribute(ATTRIBUTE_MUTED_TILL, String.valueOf(value)); } @@ -535,12 +686,32 @@ public class Conversation extends AbstractEntity { } } + public void sort() { + synchronized (this.messages) { + Collections.sort(this.messages, new Comparator<Message>() { + @Override + public int compare(Message left, Message right) { + if (left.getTimeSent() < right.getTimeSent()) { + return -1; + } else if (left.getTimeSent() > right.getTimeSent()) { + return 1; + } else { + return 0; + } + } + }); + for(Message message : this.messages) { + message.untie(); + } + } + } + public class Smp { public static final int STATUS_NONE = 0; public static final int STATUS_CONTACT_REQUESTED = 1; public static final int STATUS_WE_REQUESTED = 2; public static final int STATUS_FAILED = 3; - public static final int STATUS_FINISHED = 4; + public static final int STATUS_VERIFIED = 4; public String secret = null; public String hint = null; |