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

📄 messagesyncsource.java

📁 moblie syncml mail javame
💻 JAVA
字号:
/*
 * 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.syncml;

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

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.mailclient.mm.MessageManager;

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

    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);
        message = null;
    }

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

    /**
     * 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.");
        }
    }

    /**
     * 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()
    }

    /**
     * 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 SyncMLStatus.GENERIC_ERROR;
    }
    
    /** 
     * 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");

            MessageManager mm = MessageManager.getInstance();
            Message msg;
            try {
               msg = mm.updateMessageContent(item.getKey(), item.getContent());
            } catch (XmlException xe) {
                Log.error("Error parsing item: " + item.getKey() +
                            xe.toString());
                xe.printStackTrace();
                // Return error to the server
                return SyncMLStatus.GENERIC_ERROR;
            } catch (MailException me) {
                Log.error("Error parsing message: " + item.getKey() +
                            me.toString());
                me.printStackTrace();
                // Return error to the server
                return SyncMLStatus.GENERIC_ERROR;
            }

            item.setClientRepresentation(msg);
            return SyncMLStatus.SUCCESS;
        
        } else {
            Log.error("[MessageSyncSource.additem] Unexpected item type "
                        + item.getType());
            return SyncMLStatus.GENERIC_ERROR;
        }
    }
    
    /** 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 SyncMLStatus.GENERIC_ERROR;
    }

    /**
     * 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;
    }

    /**
     * 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 + -