⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 messagemanager.java

📁 The Funambol J2ME Mail Client aims to be a light, easy to use, free email client for J2ME devices.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (C) 2006-2007 Funambol
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.funambol.mailclient.mm;

import com.funambol.mail.MessageFlags;
import com.funambol.mailclient.Account;
import com.funambol.mailclient.syncml.FlagQueue;
import com.funambol.mailclient.syncml.MailSyncFilter;
import com.funambol.mailclient.syncml.MailSyncSource;
import com.funambol.mailclient.syncml.MessageSyncSource;
import com.funambol.mailclient.sm.SyncClient;
import com.funambol.syncml.protocol.SyncFilter;
import com.funambol.syncml.protocol.SyncML;
import com.funambol.syncml.spds.SourceConfig;
import com.funambol.syncml.spds.SyncException;
import java.io.IOException;
import java.util.Date;

import com.funambol.mailclient.AccountListener;
import com.funambol.mailclient.MailFilter;

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.util.Log;

/**
 * This class is used by the UI to manage local messages and to trigger
 * the communications with the server.
 */
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 ;
    //private long MIN_HEAP_SIZE = 1024000;
    //public static final int TRASH   = 4 ; trash is hidden
    
    //-------------------------------------------------------------- Attributes
    private AccountListener listener = null;
    private Store msgStore = null;
    
    private static MessageManager mm;
    
    //private long heapSize;
    
    //------------------------------------------------------------ Constructors
    
    /**
     * Default constructor.
     *
     *
     * @throws MailClientException if an error occur
     * @throws ConfigException if the configuration is not present
     * @throws IOException if there is an error reading the configuration
     */
    private MessageManager()
    throws ConfigException, IOException {
        msgStore = StoreFactory.getStore();
        //heapSize = Runtime.getRuntime().totalMemory();
    }
    
    public static MessageManager getInstance() throws ConfigException, IOException {
        if (mm == null) {
            mm = new MessageManager();
        }
        return mm;
    }
    
    //---------------------------------------------------------- Public Methods
    
    /**
     * Set an account listener, used to signal the calling thread
     * of the relevant events occurred during the mail synchronization.
     *
     * @param listener the AccountListener to set, or null to remove
     *                 the listener
     */
    public void setListener(AccountListener listener){
        this.listener = listener;
    }
    
    
    /**
     * 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();
        }
        /*
        if (heapSize<MIN_HEAP_SIZE) {
            msgStore.setDefaultMessageNumber(msgStore.DEFAULT_MAX_MESSAGE_NUMBER);
         
        } else {
            msgStore.setDefaultMessageNumber(-1);
        }
        Log.info("Max number of Messages per Folder is " + msgStore.getDefaultMessageNumber());*/
    }
    
    /**
     * Get the list of the top level folders from the store.
     */
    public FolderInfo[] getRootFolders() {
        Folder[] flist = msgStore.list();
        
        int len=flist.length;
        FolderInfo[] ret = new FolderInfo[len];
        
        for(int i=0, j=4; i<flist.length; i++) {
            String name = flist[i].getName();
            int idx = 0;
            
            if(name.equals(Store.INBOX)) {
                idx = INBOX;
            } else if (name.equals(Store.OUTBOX)) {
                idx = OUTBOX;
            } else if (name.equals(Store.DRAFTS)) {
                idx = DRAFTS;
            } else if (name.equals(Store.SENT)) {
                idx = SENT;
            } else {
                idx = j++;
            }
            
            ret[idx] = new FolderInfo(flist[i].getName(), msgStore);
        }
        
        return ret;
    }
    
    /**
     * Get the info for the named folder.
     */
    public FolderInfo getFolderInfo(String name) throws MailException {
        // Get the folder to validate the name.
        return new FolderInfo(msgStore.getFolder(name).getName(), msgStore);
    }
    
    /**
     * Send a message. This method copies the message in Outbox, setting the
     * sent date, and call the SyncClient to send it immediately.
     *
     * @param msg the message to send.
     */
/*                              FIXME: NOT USED
    public void send(Message msg)
    throws ConfigException, MailClientException, MailException {
        Log.info("MessageManager: sending message " + msg.getMessageId());
 
        // Set the sent date to now
        msg.setSentDate(new Date());
 
        // Put the message in outbox
        msgStore.getFolder(Store.OUTBOX).appendMessage(msg);
 
        this.sync(SyncML.ALERT_CODE_ONE_WAY_FROM_CLIENT);
    }
 */
    
    /**
     * Send messages. This method sends all the messages in Outbox.
     *
     * @param msg the message to send.
     */
    public void send()
    throws ConfigException, SyncException {
        Log.info("MessageManager: sending messages");
        
        this.sync(SyncML.ALERT_CODE_ONE_WAY_FROM_CLIENT);
    }
    
    /**
     * Send and receive messages: all messages in outbox are sent,
     * and modification on the server are checked (new messages, but also
     * deletion)
     */
    public void sync() throws SyncException, ConfigException {
        this.sync(-1); // FIXME
    }
    
    /**
     * Send and receive messages: all messages in outbox are sent,
     * and modification on the server are checked (new messages, but also
     * deletion).
     *
     * TODO: remove the synchronization from the message manager??
     */
    public void sync(int mode) throws SyncException, ConfigException {
        Log.info("MessageManager: synchronizing messages ");
        Log.info("- Start Sync -");
        long start = System.currentTimeMillis();
        
        Account a = ConfigManager.getConfig().getMailAccount();
        SyncClient client = SyncClient.getSyncClient(a);
        
        MailSyncSource mailsrc =
                new MailSyncSource(client.loadSourceConfig(
                SourceConfig.MAIL,
                SourceConfig.EMAIL_OBJECT_TYPE,
                a.getRemoteURI()
                )

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -