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

📄 mailsyncfilter.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.Date;

import com.funambol.mailclient.MailFilter;
import com.funambol.syncml.protocol.SyncFilter;
import com.funambol.util.MailDateFormatter;
import com.funambol.util.DateUtil;

/**
 * This class implements the interface <i>SyncFilter</i> to apply a 
 * MailFilter to the SyncML stack.
 */
public class MailSyncFilter extends MailFilter implements SyncFilter {
    
    //-------------------------------------------------------------- Attributes

    /**
     * If this field is set, retrieve the specified message only.
     */
    private String messageId;

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

    /**
     * Default constructor. 
     */
    public MailSyncFilter() {
        super();
        messageId = null;
    }

    /** 
     * Copy constructor from a MailFilter object. Used to create a new
     * MailSyncFilter with a defined MailFilter.
     */
    public MailSyncFilter(MailFilter ref) {
        super(ref);
        messageId = null;
    }

    /**
     * Single message filter. Initialize a filter to get a single message using
     * the id as key. By default, the entire message is retrieved, but it's
     * possible to limit it changing the other parameters.
     */
    public MailSyncFilter(String messageId) {
        super();
        this.messageId = messageId;
    }

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

    /**
     * Return the message id of the single message to be retrieved
     */
    public String getMessageId() {
        return messageId;
    }

    /**
     * Set the message id of the single message to be retrieved
     */
    public void setMessageId(String id) {
        this.messageId = id;
    }

    /**
     * Override equals method to compare two filters
     */
    public boolean equals(MailSyncFilter f) {
        if(!super.equals(f)) {
            return false;
        }
        if(this.messageId == null) {
            if(f.messageId != null) return false;
        }
        else {
            if(!this.messageId.equals(f.messageId)) {
                return false;
            }
        }
        return true;
    }

    /**
     * Format the Filter in SyncML syntax, according to the parameter
     * contained in the SyncFilter implementation.
     * @return the SyncML filter to add to the Alert command.
     */
    public String toSyncML() {
        return toSyncML(0);
    }

    /**
     * Format the Filter in SyncML syntax. This is done according to the
     * parameter contained in the SyncFilter implementation, but with a size
     * limit forced by the sync engine. This limit is usually maxMsgSize
     * or MaxObjSize.
     *
     * @return the SyncML filter to add to the Alert command.
     */
    public String toSyncML(int maxSize) {

        Date date = getDate();
        int size = getSize();

        // A maximum size is sent by the SyncManager, use it as a limit.
        if(maxSize > 0) {
            if(size == 0 || size > maxSize) {
                size = maxSize;
            }
        }
        else if(messageId == null) {
            // If no limit is sent by the engine, and the filter is the
            // full one, not inclusive, return an empty string
            if(this.equals(new MailSyncFilter())) {
                return "";
            }
        }

        StringBuffer ret = new StringBuffer("<Filter>");

        if(messageId != null || date != null) {
            // Set Record Filter
            ret.append("<Record><Item><Meta>\n").
                append("<Type xmlns=\"syncml:metinf\">").
                append("syncml:filtertype-cgi</Type>\n").
                append("</Meta>\n<Data><![CDATA[");

            // If the message id is present, get that only, otherwise
            // use the date, if present
            if (messageId != null) {
                ret.append("&LUID&iEQ;"+messageId);
            }
            else if (date != null) {
                Date gmt = new DateUtil(date).toGMT().getTime();
                ret.append("modified&iGE;"+MailDateFormatter.dateToUTC(gmt));
            }

            ret.append("]]></Data>\n</Item></Record>\n");
        }

        // Set field-level filter.
        ret.append("<Field><Item><Meta>\n").
            append("<Type xmlns=\"syncml:metinf\">").
            append("application/vnd.syncml-devinf+xml</Type>\n").
            append("</Meta>\n<Data><Property>\n<PropName>emailitem</PropName>\n").
            append("<MaxSize>").append(size).append("</MaxSize>\n").
            append("<PropParam><ParamName>texttype</ParamName></PropParam>\n");

        if(downloadAttachments()) {
            ret.append("<PropParam><ParamName>attachtype</ParamName>").
                append("</PropParam>\n");
        }
        
        ret.append("</Property>\n</Data>\n</Item>\n</Field>\n");

        // Set INCLUSIVE if messageId is set
        if (messageId != null) {
            ret.append("<FilterType>INCLUSIVE</FilterType>\n");
        }

        ret.append("</Filter>");

        return ret.toString();
    }

/*

Esclusivo:


<Record><Item><Meta><Type xmlns="syncml:metinf">syncml:filtertype-cgi</Type>
</Meta>
<Data><![CDATA[modified&iGE;20061201T000000Z]]></Data>
</Item>
</Record>
<Field><Item><Meta><Type xmlns="syncml:metinf">application/vnd.syncml-devinf+xml</Type>
</Meta>
<Data><Property><PropName>emailitem</PropName>
<MaxSize>491520</MaxSize>
<PropParam><ParamName>texttype</ParamName>
</PropParam>
</Property>
</Data>
</Item>
</Field>

Inclusivo:

<Record><Item><Meta><Type xmlns="syncml:metinf">syncml:filtertype-cgi</Type>
</Meta>
<Data><![CDATA[&LUID&iEQ;I/AAAAAKMAADQCAAAAAAAAACEAAAA=]]></Data>
</Item>
</Record>
<Field><Item><Meta><Type xmlns="syncml:metinf">application/vnd.syncml-devinf+xml</Type>
</Meta>
<Data><Property><PropName>emailitem</PropName>
<MaxSize>491520</MaxSize>
<PropParam><ParamName>texttype</ParamName>
</PropParam>
<PropParam><ParamName>attachtype</ParamName>
</PropParam>
</Property>
</Data>
</Item>
</Field>
<FilterType>INCLUSIVE</FilterType>

     */

     

}

⌨️ 快捷键说明

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