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

📄 messagesyncsource.java

📁 The Funambol J2ME Mail Client aims to be a light, easy to use, free email client for J2ME devices.
💻 JAVA
字号:
/*
 * 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.syncml;

import java.util.Hashtable;
import java.util.Date;

import com.funambol.mailclient.AccountListener;

import com.funambol.syncml.client.BaseSyncSource;
import com.funambol.syncml.protocol.SyncML;
import com.funambol.syncml.protocol.SyncMLStatus;
import com.funambol.syncml.spds.SourceConfig;
import com.funambol.syncml.spds.SyncItem;
import com.funambol.syncml.spds.SyncException;

import com.funambol.mail.Folder;
import com.funambol.mail.Message;
import com.funambol.mail.MessageFlags;
import com.funambol.mail.Store;
import com.funambol.mail.StoreFactory;
import com.funambol.mail.MailException;

import com.funambol.util.Log;
import com.funambol.util.XmlException;

/**
 * A simple extension of the <i>BaseSyncSource</i> abstract class, used to
 * retrieve a complete message from the server.
 */
public class MessageSyncSource extends BaseSyncSource {
    
    public static final String EMAIL_TYPE = "application/vnd.omads-email+xml";

    //--------------------------------------------------------------- Attributes

    /** Reference to the account listener */
    private AccountListener listener;

    private Message message;

    //------------------------------------------------------------- Constructors

    /**
     * MailSyncSource constructor: initialize source config and
     * init all the rest to null. The real initialization is done by
     * the beginSync method.
     */
    public MessageSyncSource(SourceConfig config, MailSyncFilter f) {
        super(config);
        setFilter(f);
        listener = null;
        message = null;
    }

    //----------------------------------------------------------- Public Methods

    /**
     * Set an AccountListener for this Mail source.
     */
    public void setListener(AccountListener listener) {
        this.listener = listener;
    }
    
    /**
     * Return the retrieved message.
     */
    public Message getMessage() {
        return message;
    }

    /**
     * Called after SyncManager preparation and initialization just before start
     * the synchronization of the SyncSource.
     *
     * @param syncMode the synchronization type: one of the values in
     *                 sync4j.framework.core.AlertCode
     *
     * @throws SyncException in case of error. This will stop the sync process
     */
    public void beginSync(int syncMode) throws SyncException {
        super.beginSync(syncMode);      // call BaseSyncSource.beginSync()

        if(syncMode != SyncML.ALERT_CODE_ONE_WAY_FROM_SERVER) {
            Log.error("[MessageSyncSource.beginSync()] Unexpected sync mode: "+
                        syncMode);
            throw new SyncException(SyncException.SERVER_ERROR,
                                    "Alert code is not ONE_WAY from SERVER "+
                                    "retrieving single message.");
        }

        if(listener != null) {
            listener.connectionCompleted(AccountListener.ACTION_NONE);
        }

    }

    /**
     * Called just before committing the synchronization process by the
     * SyncManager. The SyncSource can stop the commit phase raising an
     * exception here.
     *
     * @throws SyncException in case of error, to stop the commit.
     */
    public void endSync() throws SyncException  {
        super.endSync();        // call BaseSyncSource.endSync()
        if(listener != null) {
            listener.endSession(0);
        }
    }

    /**
     * Add a new item from the server to the mail store.
     */
    public int addItem(SyncItem item) {
        Log.error("Unexpected New item " + item.getKey() + " from server.");

        return 500;
    }
    
    /** 
     * The complete message comes with an update, being already present 
     * on the client.
     */
    public int updateItem(SyncItem item) {
        Log.info("[MessageSyncSource.updateItem]: "+item.getKey()+" received.");

        if(item.getType().equals(EMAIL_TYPE)) {
            // Parse the mail item
            Log.debug("[updateItem] Email item");

            EmailData mdata = new EmailData();

            try {
                mdata.parse(item.getContent());
            } catch (XmlException xe) {
                Log.error("Error parsing item: " + item.getKey() +
                            xe.toString());
                xe.printStackTrace();
                // Return error to the server
                return 500;
            } catch (MailException me) {
                Log.error("Error parsing message: " + item.getKey() +
                            me.toString());
                me.printStackTrace();
                // Return error to the server
                return 500;
            }

            if(mdata.getEmailItem() != null) {
                message = mdata.getEmailItem();

                if(listener != null) {
                    listener.msgReceived(message);
                }

            } else {
                // Server can't send flags update only in a Add command.
                Log.debug("Invalid empty email data: "+ item.getKey());
                return 500;
            }

            return 200;
        
        } else {
            Log.error("[MessageSyncSource.additem] Unexpected item type "
                        + item.getType());
            return 500;
        }
    }
    
    /** Delete a SyncItem stored on the related Items list */
    public int deleteItem(String key) {
        Log.error("[MessageSyncSource.deleteItem("+key+")]"
                  +"Delete command not expected here.");
        return 500;
    }

    /**
     * Tell the SyncSource the status returned by the server 
     * for an Item previously sent.
     *
     * @param key the key of the item
     * @param status the status code received for that item
     *
     * @throws SyncException if the SyncSource wants to stop the sync
     */
    public void setItemStatus(String key, int status)
    throws SyncException {
        Log.info("Status " + status + " for item " + key + " from server.");
        // Should not arrive, ignore it
    }

    //------------------------------------------------------- Protected methods

    /**
     * Init the allItems list used in the slow sync with the messages
     * in the outbox only.
     */
    protected void initAllItems() throws SyncException {
        // Nothing to do: no items to send
    }

    /**
     * Init the newItems list used in the fast sync with the messages
     * in the outbox only.
     */
    protected void initNewItems() throws SyncException {
        // Nothing to do: no items to send
    }

    /**
     * Not used now. It will be implemented if the Draft folder 
     * has to be synchronized.
     */
    protected void initUpdItems() {
       // Nothing to do: no items to send
    }

    /**
     * Not used now. If we want to implement the 'Delete on Server' command,
     * we will mark the message as DELETED in the folder, add it to the list
     * here and remove it after the sync.
     */
    protected void initDelItems() {
        // Nothing to do: no items to send
    }

    /**
     * Not needed by MessageSyncSource, no items to send.
     */
    protected SyncItem getItemContent(final SyncItem item) throws SyncException {
        return item;
    }

    //-------------------------------------------------------- Private methods

    /*
     * Convert Funambol 3.0 folder names into local folder names.
     *
     * @param parent the item parent string
     * @return the converted name, or the original one if does not 
     *         starts with "ROOT/"
     *         
     */
    private String folderFromParent(String parent) {
        final String root = "ROOT/";

        if(parent == null) {
            return null;
        }
        if (parent.startsWith(root)) {
            return folderFromPrefix(parent.charAt(root.length()));
        }
        return parent;
    }

    /*
     * Get the folder name from the key.
     */
    private String folderFromKey(String key) {
        if(key == null) {
            return null;
        }
        int pos = key.indexOf('/');

        if(pos != -1) {
            return key.substring(0, pos);
        }
        /*
        if(key.charAt(1) == '/') {
            return folderFromPrefix(key.charAt(0));
        }
        */
        return null;
    }

    /*
     * Get the message Id from the key.
     */
    private String msgIdFromKey(String key) {
        int pos = key.indexOf('/');

        if(pos != -1) {
            return key.substring(pos+1);
        }
        return key;
    }

    /**
     * Find the folder name from the Funambol key prefix.
     */
    private String folderFromPrefix(char prefix) {
        switch (prefix) {
            case 'I':
                return "Inbox";
            case 'O':
                return "Outox";
            case 'D':
                return "Drafts";
            case 'S':
                return "Sent";
            default:
                return null;
        }
    }

    /**
     * Compose the item key adding the folder prefix to the local key.
     * If it's one of the main folders, it is shortened, otherwise
     * the whole folder name is used as prefix.
     */
    private String makeItemKey(String folder, String key) {
        return folder+"/"+key;
/*        
        String prefix = null;

        if("Inbox".equals(folder)) {
            prefix = 'I';
        } else if("Outbox".equals(folder)) {
            prefix = 'O';
        } else if("Sent".equals(folder)) {
            prefix = 'S';
        } else if("Draft".equals(folder)) {
            prefix = 'D';
        }
        else {
            prefix = folder;
        }
        return (prefix+"/"+key);
*/
    }

}


⌨️ 快捷键说明

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