2008-11-01 17:32:06 -04:00
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
package com.fsck.k9.mail;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.HashSet;
|
2010-05-21 11:34:29 -04:00
|
|
|
import java.util.Set;
|
2011-03-27 11:06:08 -04:00
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
2010-05-01 16:06:52 -04:00
|
|
|
import com.fsck.k9.activity.MessageReference;
|
2011-03-27 11:06:08 -04:00
|
|
|
import com.fsck.k9.mail.filter.CountingOutputStream;
|
|
|
|
import com.fsck.k9.mail.filter.EOLConvertingOutputStream;
|
|
|
|
|
2010-11-13 16:40:56 -05:00
|
|
|
import com.fsck.k9.mail.store.UnavailableStorageException;
|
2011-03-27 11:06:08 -04:00
|
|
|
import com.fsck.k9.K9;
|
|
|
|
|
2010-05-01 16:06:52 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public abstract class Message implements Part, Body {
|
2010-08-07 11:10:07 -04:00
|
|
|
private static final Flag[] EMPTY_FLAG_ARRAY = new Flag[0];
|
|
|
|
|
2010-05-01 16:06:52 -04:00
|
|
|
private MessageReference mReference = null;
|
2010-05-11 22:51:59 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public enum RecipientType {
|
2008-11-01 17:32:06 -04:00
|
|
|
TO, CC, BCC,
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String mUid;
|
|
|
|
|
|
|
|
protected HashSet<Flag> mFlags = new HashSet<Flag>();
|
|
|
|
|
|
|
|
protected Date mInternalDate;
|
|
|
|
|
|
|
|
protected Folder mFolder;
|
2010-07-06 06:29:26 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public boolean olderThan(Date earliestDate) {
|
|
|
|
if (earliestDate == null) {
|
2010-05-30 17:20:47 -04:00
|
|
|
return false;
|
|
|
|
}
|
2010-07-13 17:16:49 -04:00
|
|
|
Date myDate = getSentDate();
|
2011-02-06 17:09:48 -05:00
|
|
|
if (myDate == null) {
|
2010-07-13 17:16:49 -04:00
|
|
|
myDate = getInternalDate();
|
2010-05-30 17:20:47 -04:00
|
|
|
}
|
2011-02-06 17:09:48 -05:00
|
|
|
if (myDate != null) {
|
2010-05-30 17:20:47 -04:00
|
|
|
return myDate.before(earliestDate);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-03 09:46:55 -04:00
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (o == null || !(o instanceof Message)) {
|
2010-05-01 16:06:52 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Message other = (Message)o;
|
|
|
|
return (mFolder.getName().equals(other.getFolder().getName())
|
|
|
|
&& mFolder.getAccount().getUuid().equals(other.getFolder().getAccount().getUuid())
|
2010-05-11 22:51:59 -04:00
|
|
|
&& mUid.equals(other.getUid()));
|
2010-05-01 16:06:52 -04:00
|
|
|
}
|
2010-05-11 22:51:59 -04:00
|
|
|
|
2010-05-03 09:46:55 -04:00
|
|
|
@Override
|
2011-02-06 17:09:48 -05:00
|
|
|
public int hashCode() {
|
2010-05-03 09:46:55 -04:00
|
|
|
final int MULTIPLIER = 31;
|
|
|
|
|
|
|
|
int result = 1;
|
|
|
|
result = MULTIPLIER * result + mFolder.getName().hashCode();
|
|
|
|
result = MULTIPLIER * result + mFolder.getAccount().getUuid().hashCode();
|
|
|
|
result = MULTIPLIER * result + mUid.hashCode();
|
|
|
|
return result;
|
|
|
|
}
|
2010-05-11 22:51:59 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public String getUid() {
|
2008-11-01 17:32:06 -04:00
|
|
|
return mUid;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void setUid(String uid) {
|
2010-05-01 16:06:52 -04:00
|
|
|
mReference = null;
|
2008-11-01 17:32:06 -04:00
|
|
|
this.mUid = uid;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public Folder getFolder() {
|
2008-11-01 17:32:06 -04:00
|
|
|
return mFolder;
|
|
|
|
}
|
|
|
|
|
2010-11-30 22:02:13 -05:00
|
|
|
public abstract String getSubject();
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
public abstract void setSubject(String subject) throws MessagingException;
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public Date getInternalDate() {
|
2008-11-01 17:32:06 -04:00
|
|
|
return mInternalDate;
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void setInternalDate(Date internalDate) {
|
2008-11-01 17:32:06 -04:00
|
|
|
this.mInternalDate = internalDate;
|
|
|
|
}
|
|
|
|
|
2010-05-30 17:20:47 -04:00
|
|
|
public abstract Date getSentDate();
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
public abstract void setSentDate(Date sentDate) throws MessagingException;
|
|
|
|
|
|
|
|
public abstract Address[] getRecipients(RecipientType type) throws MessagingException;
|
|
|
|
|
|
|
|
public abstract void setRecipients(RecipientType type, Address[] addresses)
|
2009-11-24 19:40:29 -05:00
|
|
|
throws MessagingException;
|
2008-11-01 17:32:06 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public void setRecipient(RecipientType type, Address address) throws MessagingException {
|
|
|
|
setRecipients(type, new Address[] {
|
2009-12-06 19:56:06 -05:00
|
|
|
address
|
|
|
|
});
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2010-11-30 22:02:13 -05:00
|
|
|
public abstract Address[] getFrom();
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
public abstract void setFrom(Address from) throws MessagingException;
|
|
|
|
|
2010-11-30 22:02:13 -05:00
|
|
|
public abstract Address[] getReplyTo();
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
public abstract void setReplyTo(Address[] from) throws MessagingException;
|
|
|
|
|
2009-11-17 16:13:29 -05:00
|
|
|
public abstract String getMessageId() throws MessagingException;
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-11-17 16:13:29 -05:00
|
|
|
public abstract void setInReplyTo(String inReplyTo) throws MessagingException;
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-11-17 16:13:29 -05:00
|
|
|
public abstract String[] getReferences() throws MessagingException;
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-11-17 16:13:29 -05:00
|
|
|
public abstract void setReferences(String references) throws MessagingException;
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2010-11-30 22:02:13 -05:00
|
|
|
public abstract Body getBody();
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
public abstract String getContentType() throws MessagingException;
|
|
|
|
|
|
|
|
public abstract void addHeader(String name, String value) throws MessagingException;
|
|
|
|
|
|
|
|
public abstract void setHeader(String name, String value) throws MessagingException;
|
|
|
|
|
|
|
|
public abstract String[] getHeader(String name) throws MessagingException;
|
|
|
|
|
2010-11-13 16:40:56 -05:00
|
|
|
public abstract Set<String> getHeaderNames() throws UnavailableStorageException;
|
2010-05-21 11:34:29 -04:00
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
public abstract void removeHeader(String name) throws MessagingException;
|
|
|
|
|
|
|
|
public abstract void setBody(Body body) throws MessagingException;
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public boolean isMimeType(String mimeType) throws MessagingException {
|
2008-11-01 17:32:06 -04:00
|
|
|
return getContentType().startsWith(mimeType);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2010-11-30 22:07:28 -05:00
|
|
|
public void delete(String trashFolderName) throws MessagingException {}
|
2008-11-01 17:32:06 -04:00
|
|
|
|
|
|
|
/*
|
2009-11-24 19:40:29 -05:00
|
|
|
* TODO Refactor Flags at some point to be able to store user defined flags.
|
2008-11-01 17:32:06 -04:00
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public Flag[] getFlags() {
|
2010-08-07 11:10:07 -04:00
|
|
|
return mFlags.toArray(EMPTY_FLAG_ARRAY);
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|
|
|
|
|
2010-09-12 02:11:08 -04:00
|
|
|
/**
|
|
|
|
* @param flag
|
|
|
|
* Flag to set. Never <code>null</code>.
|
|
|
|
* @param set
|
|
|
|
* If <code>true</code>, the flag is added. If <code>false</code>
|
|
|
|
* , the flag is removed.
|
|
|
|
* @throws MessagingException
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public void setFlag(Flag flag, boolean set) throws MessagingException {
|
|
|
|
if (set) {
|
2008-11-01 17:32:06 -04:00
|
|
|
mFlags.add(flag);
|
2011-02-06 17:09:48 -05:00
|
|
|
} else {
|
2008-11-01 17:32:06 -04:00
|
|
|
mFlags.remove(flag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method calls setFlag(Flag, boolean)
|
|
|
|
* @param flags
|
|
|
|
* @param set
|
|
|
|
*/
|
2011-02-06 17:09:48 -05:00
|
|
|
public void setFlags(Flag[] flags, boolean set) throws MessagingException {
|
|
|
|
for (Flag flag : flags) {
|
2008-11-01 17:32:06 -04:00
|
|
|
setFlag(flag, set);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public boolean isSet(Flag flag) {
|
2008-11-01 17:32:06 -04:00
|
|
|
return mFlags.contains(flag);
|
|
|
|
}
|
|
|
|
|
2010-11-12 16:38:02 -05:00
|
|
|
|
|
|
|
public void destroy() throws MessagingException {}
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
public abstract void saveChanges() throws MessagingException;
|
2010-02-04 18:37:50 -05:00
|
|
|
|
2010-11-13 16:40:56 -05:00
|
|
|
public abstract void setEncoding(String encoding) throws UnavailableStorageException;
|
2010-05-11 22:51:59 -04:00
|
|
|
|
2011-01-04 08:25:59 -05:00
|
|
|
public abstract void setCharset(String charset) throws MessagingException;
|
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public MessageReference makeMessageReference() {
|
|
|
|
if (mReference == null) {
|
2010-05-01 16:06:52 -04:00
|
|
|
mReference = new MessageReference();
|
|
|
|
mReference.accountUuid = getFolder().getAccount().getUuid();
|
|
|
|
mReference.folderName = getFolder().getName();
|
|
|
|
mReference.uid = mUid;
|
|
|
|
}
|
|
|
|
return mReference;
|
|
|
|
}
|
2010-05-11 22:51:59 -04:00
|
|
|
|
2011-02-06 17:09:48 -05:00
|
|
|
public boolean equalsReference(MessageReference ref) {
|
2010-05-01 16:06:52 -04:00
|
|
|
MessageReference tmpReference = makeMessageReference();
|
|
|
|
return tmpReference.equals(ref);
|
|
|
|
}
|
2010-11-28 15:28:42 -05:00
|
|
|
|
2011-03-27 11:06:08 -04:00
|
|
|
public long calculateSize() {
|
|
|
|
try {
|
|
|
|
|
|
|
|
CountingOutputStream out = new CountingOutputStream();
|
|
|
|
EOLConvertingOutputStream eolOut = new EOLConvertingOutputStream(out);
|
|
|
|
writeTo(eolOut);
|
|
|
|
eolOut.flush();
|
|
|
|
return out.getCount();
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.e(K9.LOG_TAG, "Failed to calculate a message size: " + e);
|
|
|
|
} catch (MessagingException e) {
|
|
|
|
Log.e(K9.LOG_TAG, "Failed to calculate a message size: " + e);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-11-01 17:32:06 -04:00
|
|
|
}
|