📄 messageheader.java
字号:
* recipient
* @param defaultRecipient the recipient to be added if no recipient is yet
* specified.
*/
public void ensureRecipient(String defaultRecipient) {
if (recipients.startsWith("+")) {
// TODO (Betlista): what is the difference between this call and
// addRecipient(defaultRecipient)
// I know, the result is different, there is missing " *" at the end
// but is that important ?
recipients = defaultRecipient;
}
}
/**
* Marks this message as read.
*/
public void markAsReplied() {
sendStatus = REPLIED;
}
/**
* Marks this message as read.
*/
public void markAsDeleted() {
deleted = true;
}
/**
* Was this message deleted?
* @return true if this message was deleted.
*/
public boolean wasDeleted() {
return deleted;
}
/**
* Marks this message as read.
*/
public void markAsFlagged() {
flagged = true;
}
/**
* Saves the header of the message and of all bodyparts to the RMS database.
* Does not save the content of the message.
* If the status of the message is header.DBStatus == MessageHeader.STORED
* saves the header to existing record in the database (just updates it)
* @return the record ID under which the header is saved
* @throws mujmail.MyException
*/
public int saveHeader() throws MyException {
return box.getMailDB().saveHeader(this);
}
/**
* Gets the message id without special characters.
* @return message id without special characters
*/
public String getMessageIDWithoutSpecCharacters() {
String tempID = messageID.replace('/', '1');
tempID = tempID.replace('\\', '2');
tempID = tempID.replace('.', '3');
tempID = tempID.replace(MessageHeader.MSG_ID_SEPARATOR, '4');
return tempID;
}
/** ...and then start adding body parts headers to it
*
* The method adds a <code>BodyPart</code> to the message header to cache info about each mail part, but this method doesn't store it in the <code>RecordStore</code>.
* Its called only when the real content is stored and so we have a proper functioning recordID
* Note: Storing the real content is done in the <code>saveBodyPart</code> method in <code>MailDb</code> class.
* @see #MailDB
*/
public void addBodyPart(BodyPart bpHeader) {
bodyParts.addElement(bpHeader);
}
/* Add attachment file info to Vector (in <code>MessageHeader</code>)
*
* The method adds an <code>AttachmentPart</code> to the message header to store the attachment file info.
* Only the file url, name, and size are stored.
* @param apHeader <code>AttachmentPart</code> with attachment file info
*/
// public void addAttachFilePart(AttachmentPart apHeader) {
// attachFileParts.addElement(apHeader);
// }
public BodyPart getBodyPart(int index) {
return (BodyPart) bodyParts.elementAt(index);
}
/**
* Removes all body parts.
*/
public void deleteAllBodyParts() {
bodyParts.removeAllElements();
}
/**
* Replaces the body of this message
* @param bodyPart
*/
public void replaceBody(BodyPart newBody) {
bodyParts.setElementAt(newBody, 0);
}
/**
* Replaces the attachment at given index with new attachment.
* @param newAttachment the attachment which replaces the old attachment
* @param i the index of attachment to be replaced
*/
public void replaceAttachment(BodyPart newAttachment, int i) {
bodyParts.setElementAt(newAttachment, i+1);
}
/**
* Replaces the body part at given index with new body part.
* @param newBodyPart the body part which replaces the old body part
* @param i the index of the body part to be replaced
*/
public void replaceBodyPart(BodyPart newBodyPart, int i) {
bodyParts.setElementAt(newBodyPart, i);
}
//#ifdef MUJMAIL_SEARCH
/**
* Sets the result of searching to this message.
* @param searchResult the result of searching.
*/
public void setSearchResult(MessageSearchResult searchResult) {
this.searchResult = searchResult;
}
//#endif
//#ifdef MUJMAIL_SEARCH
/**
* Gets the result of searching in this message.
* @return the result of searching in this message.
* if this message have not been searched yet, alwayes returns
* {@link mujmail.search.MessageSearchResult.NO_MATCH}.
*/
public MessageSearchResult getSearchResult() {
return searchResult;
}
//#endif
/**
* Insert the bodypart to the list of body parts. Each body part with an index
* greater or equal is shifted upward to have an index one greater than the
* value it had previously.
* @param bodyPart
* @param i
*/
public void insertBodyPartAt(BodyPart bodyPart, int i) {
bodyParts.insertElementAt(bodyPart, i);
}
/**
* Deletes this message from database and the box to which the message belongs.
* @param reportBox the box to which should be reported warning messages.
* @param trashMode describes the storing of deleted mail to trash
*/
public void deleteFromDBAndBox(TheBox reportBox, Trash.TrashModes trashMode) {
deleted = true;
MujMail.mujmail.getTrash().storeToTrash(this, trashMode);
if ( getMailDB() != null) {
// Delete from DB, here comes only persistent folders
getMailDB().deleteMail(this, (PersistentBox)reportBox);
}
MujMail.mujmail.getMailDBManager().removeMessage(this); // Remove from Boxes
}
/**
* Ads the body to this email. Note that the body should not be already
* inserted.
* @param body the body part representing the body of the mail
*/
public void addBody(BodyPart body) {
if (bodyParts.size() == 0) bodyParts.addElement(body);
else insertBodyPartAt(body, 0);
}
/*
* Get the attachment file info (path, name, and size) from <code>MessageHeader</code>
* @param index Element in <code>AttachmentPart</code> Vector (in <code>MessageHeader</code>
* @return <code>AttachmentPart</code> with attachment file path, name, and size
*/
// public AttachmentPart getAttachmentPart(byte index) {
// if (!attachFileParts.isEmpty()) {
// return (AttachmentPart)attachFileParts.elementAt(index);
// } else {
// return (AttachmentPart)null;
// }
// }
public String getBodyPartContent(byte index) {
try {
BodyPart bp = (BodyPart) bodyParts.elementAt(index);
return bp.getStorage().getContent();
} catch (Throwable ex) {
ex.printStackTrace();
System.out.println("exception in get body part content");
return "This bodypart was not yet downloaded or deleted. To see it, redownload it.";
}
}
/**
* Gets converted bodypart content.
* @param index ID of bodypart of the email
* @return content as String
*/
public String getConvertedBodyPartContent(byte index) {
BodyPart bp = (BodyPart) bodyParts.elementAt(index);
try {
bp.switchToConvertedContent();
if (DEBUG) System.out.println("DEBUG MessageHeader.getConvertedBodyPartContent Bodypart chosen="+(index+1));
String extension = bp.getHeader().getExtension();
if (!("pdf".equals(extension) || "jpg".equals(extension))) {
MujMail.mujmail.alert.setAlert(Lang.get(Lang.ALRT_MF_UNSUPPORTED_CONVERSION), AlertType.ERROR);
return "";
}
if (bp.getStorage() == null) {
/*** Download converted bodypart ***/
//TODO: Check if this message comes from mujMail server account
if (DEBUG) System.out.println("DEBUG MessageHeader.getConvertedBodyPartContent - Retrieving converted body");
if (!(getBox() instanceof InBox)) throw new MyException(0, "Only inbox can retrive converted bodies");
((MailAccount)MujMail.mujmail.getMailAccounts().get(this.accountID)).getProtocol().
getConvertedBody(this, index, (InBox)getBox());
if (DEBUG) System.out.println("DEBUG MessageHeader.getConvertedBodyPartContent - Converted body retrieved");
/*** Save the bodypart ***/
}
while (bp.getStorage() == null) {
bp = (BodyPart)bodyParts.elementAt(index);
synchronized (bp) {
if (DEBUG) {
System.out.println("DEBUG MessageHeader.getConvertedBodyPartContent - Waiting while converted body retrieved");
}
bp.wait();
}
if (DEBUG) {
System.out.println("DEBUG MessageHeader.getConvertedBodyPartContent - Still waiting ...");
}
}
if (DEBUG) System.out.println("DEBUG MessageHeader.getConvertedBodyPartContent - Finished waiting, storage instance = " + bp.getStorage());
bp.switchToConvertedContent();
return bp.getStorage().getContent();
} catch (Throwable ex) {
ex.printStackTrace();
System.out.println("exception in get body part content");
return "";
} finally {
bp.switchToNotConvertedContent();
}
}
public byte[] getBodyPartContentRaw(byte index) {
try {
return ((BodyPart) bodyParts.elementAt(index)).getStorage().getContentRaw();
} catch (Exception ex) {
return new byte[0];
}
}
public byte getBodyPartCount() {
return (byte) bodyParts.size(); //byte because we never have more bodyparts than 127
}
/**
* Removes body part at specified index.
* @param i
*/
public void removeBodyPart(int i) { // TODO (Betlista): this (int) is inconsistent with getBodyPartContent(byte index)
bodyParts.removeElementAt(i);
}
/**
* Gets the body of the mail with this header.
* @return the body of the mail with this header
*/
public BodyPart getBody() {
return (BodyPart) bodyParts.elementAt(0);
}
/**
* Gets the number of attachments.
* @return the number of attachments
*/
public int getAttachementCount() {
return bodyParts.size()-1;
}
//#ifdef MUJMAIL_FS
/**
* Removes all filesystem attachments (not others!).
*/
public void removeFSAttachments() {
Vector toRemove = new Vector();
for (int i = 0; i < getAttachementCount(); i++) {
BodyPart bp = getAttachement(i);
if (bp.getStorage().getStorageType() == ContentStorage.StorageTypes.FS_STORAGE) {
toRemove.addElement(bp);
}
}
for (int i = 0; i < toRemove.size(); i++) {
bodyParts.removeElement(toRemove.elementAt(i));
}
}
//#endif
/**
* Removes the attachment with index i (the attachment got by calling
* getAttachement(i)).
* @param i
*/
private void removeAttachment(int i) {
bodyParts.removeElementAt(i);
}
/**
* Gets recipients of the message (field "To").
* @return the recipients of the message.
*/
public String getRecipients() {
return recipients;
}
/**
* Gets the sender of the message (field "From").
* @return the sender of the message.
*/
public String getSender() {
return from;
}
/**
* Gets the subject of the message.
* @return the subject of the message
*/
public String getSubject() {
return subject;
}
/**
* Gets the i-th attachment
* @param i the number of attachment to get
* @return the i-th attachment
*/
public BodyPart getAttachement(int i) {
return (BodyPart) bodyParts.elementAt(i+1);
}
/**
* Returns true if this message is plain.
* @return true if this message is plain.
*/
public boolean isPlain() {
return messageFormat == FRT_PLAIN;
}
public byte getBpEncoding(byte i) {
return ((BodyPart) bodyParts.elementAt(i)).getHeader().getEncoding();
}
public byte getBpType(byte i) {
return ((BodyPart) bodyParts.elementAt(i)).getHeader().getBodyPartContentType();
}
public String getBpName(byte i) {
return ((BodyPart) bodyParts.elementAt(i)).getHeader().getName();
}
public String getBpExtension(byte i) {
String name = ((BodyPart) bodyParts.elementAt(i)).getHeader().getName();
byte j = (byte) name.lastIndexOf('.');
return (j == -1) ? name : name.substring(j + 1);
}
public byte getBpCharSet(byte i) {
return ((BodyPart) bodyParts.elementAt(i)).getHeader().getCharSet();
}
public long getBpSize(byte i) {
return ((BodyPart) bodyParts.elementAt(i)).getStorage().getSize();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -