messagemanager.java
来自「moblie syncml mail javame」· Java 代码 · 共 1,265 行 · 第 1/4 页
JAVA
1,265 行
/*
* Funambol is a mobile platform developed by Funambol, Inc.
* Copyright (C) 2003 - 2007 Funambol, Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by
* the Free Software Foundation with the addition of the following permission
* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
* WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
* WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program; if not, see http://www.gnu.org/licenses or write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA.
*
* You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
* 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License
* version 3, these Appropriate Legal Notices must retain the display of the
* "Powered by Funambol" logo. If the display of the logo is not reasonably
* feasible for technical reasons, the Appropriate Legal Notices must display
* the words "Powered by Funambol".
*/
package com.funambol.mailclient.mm;
import com.funambol.mail.BodyPart;
import com.funambol.mail.MessageFlags;
import com.funambol.mail.Multipart;
import com.funambol.mailclient.syncml.FlagQueue;
import java.io.UnsupportedEncodingException;
import java.util.Hashtable;
import java.util.Date;
import java.util.Vector;
import com.funambol.mailclient.config.ConfigManager;
import com.funambol.mailclient.config.ConfigException;
import com.funambol.mail.Message;
import com.funambol.mail.Folder;
import com.funambol.mail.Store;
import com.funambol.mail.StoreFactory;
import com.funambol.mail.MailException;
import com.funambol.mail.MIMEProcessor;
import com.funambol.mail.MIMEFormatter;
import com.funambol.mailclient.config.MailClientConfig;
import com.funambol.util.Log;
import com.funambol.util.XmlException;
import com.funambol.util.XmlUtil;
import com.funambol.util.ChunkedString;
import com.funambol.util.MailDateFormatter;
import com.funambol.util.QuotedPrintable;
import com.funambol.util.StringUtil;
/**
* This class is used to manage local messages.The MessageManager is responsible
* for adding/updating/deleting messages. All these operations can be performed
* on the client and then propagated to the server, or originated on the server
* and propagated to the client.
* Message (@see Message)is the basic entity to represent an email. Message is
* handled almost completely by the mail API, but there is one information in
* Message which is left to the client to manage. The key is a concept for the
* client and Messages created by the API leave it empty.
* The MessageManager is responsible for creating all the Messages in the
* client. Message should never be created without using the MessageManager. In
* this context the MessageManager acts as a Message factory for the client.
* Any time the MessageManager creates a Message it sets the key to a proper
* value, guaranteeing that no Message is without key. The key is used to
* save/load messages, therefore it is crucial that each Message has this
* information properly set.
* From a message key it is always possible to derive both the message folder
* and the message record id in the underlying store.
* Other parts of the client should not make any assumption on how the LUID is
* formed.
* The MessageManager has three different set of methods to handle messages:
*
* 1) generic methods to operate on the client messages
* 2) methods invoked to create/update/delete messages upon a user action. In
* these cases the UI notifies that something changed and this information
* must be sent to the server on the next synchronization
* 3) methods invoked to create/update/delete messages upon a server request. In
* these cases the information in the system (e.g. RMS) is updated. The
* MessageManager does not call back any method, because notifications to the
* UI are sent via SyncListener by the SyncManager.
*
*/
public class MessageManager {
//---------------------------------------------------------------- Constant
public static final int INBOX = 0 ;
public static final int OUTBOX = 1 ;
public static final int DRAFTS = 2 ;
public static final int SENT = 3 ;
//public static final int TRASH = 4 ; trash is hidden
//-------------------------------------------------------------- Attributes
private Store msgStore = null;
private static MessageManager mm = null;
private Folder[] rootFolders;
//------------------------------------------------------------ Constructors
/**
* Default constructor.
*/
private MessageManager() {
try {
msgStore = StoreFactory.getStore();
} catch (Exception e) {
Log.error(this, "Cannot get store: " + e.toString());
msgStore = null;
}
}
public static MessageManager getInstance() {
if (mm == null) {
mm = new MessageManager();
}
return mm;
}
//---------------------------------------------------------- Public Methods
/**
* Re-initialize the mail the store.
*
* @param reset if true, erase and re-create all the main folders.
*
* @throws MailException in case of error (re)creating folders.
*/
public void initStore(boolean reset) throws MailException {
msgStore.init(reset);
// clear queue of flags if re-initialization is needed
if (reset) {
FlagQueue.clear();
}
}
/**
* Get the list of the top level folders from the store.
*/
public Folder[] getRootFolders() {
if (rootFolders == null) {
computeRootFolders();
}
return rootFolders;
}
////////////////////////////////////////////////////////////////////////////
// Methods invoked by the client to apply changes to messages. The changes
// are originated on the client side. All these methods update the
// information and keep tracks of what happened to notify the server on the
// next synchronization
////////////////////////////////////////////////////////////////////////////
/**
* Delete a message from a folder
* @param msg the message to be deleted
*/
public void deleteMessage(Message msg) throws ConfigException, MailException {
removeFromFlagQueue(msg);
if ((ConfigManager.getConfig().isEnableDeletePropagation())) {
updateMessageFlag(msg, MessageFlags.DELETED, true);
}
msg.getParent().deleteMessage(msg);
}
/**
* Append a message to a folder. The message key is set after this
* operation. The method throws an exception if the underlying folder
* throws an exception.
*
* @param msg the message
* @param folder the folder
*
* @throws MailException if the underlying folder throws an exception
*/
public void appendMessageToFolder(Message msg, Folder folder) throws MailException {
folder.appendMessage(msg);
msg.setKey(makeLuid(msg));
}
/**
* Append a message to a folder. The message key is set after this
* operation. The method throws an exception if the underlying folder
* throws an exception.
*
* @param msg the message
* @param folder the folder id
*
* @throws MailException if the underlying folder throws an exception
*/
public void appendMessageToFolder(Message msg, int folderIdx) throws MailException {
Folder folder = getFolder(folderIdx);
appendMessageToFolder(msg, folder);
}
/**
* remove a folder and recreate it empty
* @param folder the folder to be removed (as one of <ul>
* <li>MessageManager.INBOX</li>
* <li>MessageManager.OUTOX</li>
* <li>MessageManager.DRAFTS</li>
* <li>MessageManager.SENT</li></ul>
* @throws MailException if we have problem recreating the folder
*/
public void emptyFolder(int folderId) throws MailException {
String folderName = "";
switch (folderId) {
case INBOX:
folderName = Store.INBOX;
break;
case OUTBOX:
folderName = Store.OUTBOX;
break;
case DRAFTS:
folderName = Store.DRAFTS;
break;
case SENT:
folderName = Store.SENT;
break;
}
if (!"".equals(folderName)) {
msgStore.removeFolder(folderName);
msgStore.createFolder(folderName);
}
}
/**
* Updates a message which is already stored in a folder (the folder
* information must be valid)
*
* @param msg the message
* @throws MailException if the underlying folder throws an exception
*/
public void updateMessage(Message msg) throws MailException {
Folder folder = msg.getParent();
folder.updateMessage(msg);
}
/**
* Sets Message Flags after a messages one or more of its property flag
* @param msg the message to be flagged
* @param flag is the flag to be added to the message <code>msg</code>
*/
public void updateMessageFlag(Message msg, int flag, boolean value)
throws MailException {
// The flag is already set, nothing to change.
boolean current = msg.getFlags().isSet(flag);
if(current == value) {
Log.debug("Flag: "+flag+" already set");
return;
}
//Creates MessageFlags to put into updated items HashTable
MessageFlags flags = new MessageFlags(msg.getFlags());
flags.setFlag(flag, value);
// OPENED or FLAGGED flags must be set into the current
// msg and saved immediately, the others will be set after the sync.
switch (flag) {
case MessageFlags.DELETED:
updateMessageFlags(msg, flags);
break;
case MessageFlags.ANSWERED:
case MessageFlags.DRAFT:
case MessageFlags.FORWARDED:
case MessageFlags.PARTIAL:
//Call update for SyncML
updateMessageFlags(msg, flags);
break;
case MessageFlags.OPENED:
case MessageFlags.FLAGGED:
//Call update for SyncML
updateMessageFlags(msg, flags);
//Save the updated flags message into the folder
msg.getFlags().setFlag(flag, value);
Folder msgFolder = msg.getParent();
msgFolder.appendMessage(msg);
break;
default:
break;
}
}
/**
* Update message flags Lists to be read by MailSyncSource
* with this implementation the flags are managed only for the Inbox folder
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?