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

📄 configmanager.java

📁 moblie syncml mail javame
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * 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.config;

import com.funambol.mailclient.Account;
import com.funambol.mailclient.MailFilter;
import com.funambol.mailclient.sm.SyncClientConfig;
import com.funambol.mailclient.Funambol;
import com.funambol.mailclient.ui.controller.AppProperties;
import com.funambol.mailclient.ui.controller.UIController;
import com.funambol.push.PushConfig;

import com.funambol.syncml.spds.SourceConfig;
import com.funambol.storage.AbstractRecordStore;
import com.funambol.storage.ObjectStore;
import com.funambol.storage.Serializable;

import com.funambol.util.DateUtil;
import com.funambol.util.Log;
import com.funambol.util.StringUtil;

import java.io.IOException;
import java.io.DataInputStream;
import java.util.Date;
import java.util.Hashtable;

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreNotOpenException;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.midlet.MIDlet;

/**
 * The configuration manager is resposible for the store and retrieve
 * of the client config data in the Storage.
 *
 * Usage:
 *
 * mailConfig = ConfigManager.getConfig(); // to obtain the mail config params
 *
 * // To load and save a source config
 * ConfigManager.load("source.name", SourceConfig);
 * ConfigManager.save("source.name", SourceConfig);
 */
public class ConfigManager {
    
    
    public static final int MAIL_CLIENT_CONFIG    = 1;
    public static final int MAIL_SOURCE_CONFIG    = 2;
    public static final int CONTACT_SOURCE_CONFIG = 3;
    public static final int SYNCML_CONFIG         = 4;
    public static final int PUSH_CONFIG           = 5;
    
    private static final String CONFIGNAME = "MailConfig";
    private static final int HI_MSG_NUMBER = 350;
    private static final int LO_MSG_NUMBER = 50;
    
    // Config storage
    private static ObjectStore confstore = new ObjectStore();
    // Mail client config
    private static MailClientConfig mailClientConfig;
    
    // Set of JAD Attributes for first configuration of client
    /*
    private static final String LOGLEVEL_ATTR = "LogLevel";
    private static final String ENABLE_SCHEDULER_ATTR = "EnableScheduler";
    private static final String ENABLE_SMS_LISTENER_ATTR = "EnableSmsListener";
    private static final String USER_ATTR = "User";
    private static final String URL_ATTR = "Url";
    private static final String PASSWORD_ATTR = "Password";
    private static final String ADDRESS_ATTR = "Address";
    private static final String NAME_ATTR = "Name";
    private static final String REMOTEURI_ATTR = "RemoteUri";
    private static final String DEVID_ATTR = "Device-ID";
    private static final String PIM_REMOTEURI_ATTR = "vCardRemoteURI";
    private static final String POLL_INTERVAL_ATTR = "PollInterval";
    private static final String ENABLE_DELETE_PROPAGATION = "EnableDeletePropagation";
    private static final String ENABLE_COMPRESS = "Compress";
    private static final String ENABLE_DATE_FILTER = "DateFilter";
    private static final String APP_INFO_ATTR = "AppInfo";
    */
    
    private static String clientName;
    private static String clientVendor;
    private static String clientVersion;
    
    private static int FILTER_WINDOW = 10;
    
    private static int storageSize;
    
    private static int maxMessageNumber = LO_MSG_NUMBER;
    
    private static boolean firstRun = false;
    
    public static String appInfoAttr;
    
    /** Temporary property (not serialized) for switching
     * from compressed sync to uncompressed sync
     */
    public static boolean _tmpCompress;
    
    //------------------------------------------------------------- Constructors
    
    /**
     * This class is used by calling static members, prevent
     * callers from creating instances.
     */
    private ConfigManager() {
    }
    
    //----------------------------------------------------------- Public methods
    
    /**
     * Load the Serializable object in the config store, with the given name
     */
    public static Serializable load(String name, Serializable obj)
    throws ConfigException, IOException {
        
        int id = getId(name);
        return load(id, obj);
    }
    
    public static void save(String name, Serializable obj)
    throws ConfigException, IOException {
        
        int id = getId(name);
        save(id, obj);
    }
    
    
    public static Serializable load(int configId, Serializable obj)
    throws ConfigException, IOException {
        try {
            confstore.open(CONFIGNAME);
            obj = confstore.retrieve(configId, obj);
            if (obj == null) {
                throw new ConfigException("Non existing config");
            }
        } catch (RecordStoreException e) {
            Log.info("Can't load " + configId + ": " + e.toString());
            throw new ConfigException(e.toString());
        }
        return obj;
    }
    
    /**
     * Save the Serializable object in the config store, with the given name.
     *
     * @return true if the config has been saved for the first time.
     */
    public static void save(int configId, Serializable obj)
    throws ConfigException, IOException {
        
        try {
            confstore.create(CONFIGNAME);
            confstore.store(configId, obj);
        } catch (RecordStoreException e) {
            Log.error("Can't save " + configId + ": " + e.toString());
            throw new ConfigException(e.toString());
        }
    }
    
    /**
     * Return the cached mail config.
     *
     * @return the cached MailClientConfig.
     *
     * @throws ConfigException if the config is not present
     */
    public static MailClientConfig getConfig()
    throws ConfigException{
        
        if(mailClientConfig == null) {
            // init configuration before using this method
            throw new ConfigException("Error: Configuration not yet initialized");
        }
        
        return mailClientConfig;
    }
    
    /**
     * Return the cached or stored mail config. If not present
     * loads it from JAD or create a default one.
     *
     * @return the MailClientConfig stored or cached or from JAD.
     *
     * @throws ConfigException if the config is not present
     * @throws IOException if an error occurred during the deserialization
     */
    public static MailClientConfig getConfig(Funambol midlet)
    throws ConfigException, IOException {
        MailClientConfig tmp = new MailClientConfig();
        try {
            if(mailClientConfig == null) {
                mailClientConfig = (MailClientConfig) load(MAIL_CLIENT_CONFIG, tmp);
                Log.info("MailClientConfig from Storage\n");
            }
        } catch (ConfigException e) {
            // First Run. Loading configuration from JAD or create a default one
            Log.info("First run. Configuration from JAD");
            midlet.setSplashPhase("Preparing for first run...", 0);
            firstRun = true;
            recoverConfig(true);
        } catch (IOException e) {
            // Error retreiving configuration from storage
            // Loading configuration from JAD or create a default one
            Log.error("Error loading configuration from storage");
            midlet.setSplashPhase("Initialization error, recovering...", 0);
            recoverConfig(false);
        } finally {
            // Settings always from Jad, never stored in Storage
            clientName = UIController.appProperties.get(
                    AppProperties.MIDLET_NAME);
            Log.info("ClientName: " + clientName);
            clientVersion = UIController.appProperties.get(AppProperties.MIDLET_VERSION);
            Log.info("ClientVersion: " + clientVersion);
            clientVendor = UIController.appProperties.get(AppProperties.MIDLET_VENDOR);
            Log.info("ClientVendor: " + clientVendor);
            //#ifdef isBlackberry
            //# boolean flagCompress = true;
            //#else
            boolean flagCompress = StringUtil.getBooleanValue(
                    UIController.appProperties.get(AppProperties.ENABLE_COMPRESS));
            //#endif
            Log.info("Compress from Jad: " + "[" + flagCompress + "]");
            mailClientConfig.enableCompress(flagCompress);
            _tmpCompress = flagCompress;
            
            // Disable date filter only if present and set to false
            String dateFilter = UIController.appProperties.get(AppProperties.ENABLE_DATE_FILTER);
            if(dateFilter != null) {
                if (StringUtil.equalsIgnoreCase("false", dateFilter)) {
                    Log.info("DateFilter from Jad: [false]");
                    mailClientConfig.enableDateFilter(false);
                }
            }
            
            
            appInfoAttr = UIController.appProperties.get(AppProperties.APP_INFO_ATTR);
            /**if (appInfoAttr!=null && !appInfoAttr.startsWith("Moto")) {
             * maxMessageNumber=HI_MSG_NUMBER;
             * }**/
            
            //#ifndef LowMsgNum
            maxMessageNumber=HI_MSG_NUMBER;
            //#endif
        }
        
        return mailClientConfig;
    }
    
    
    
    /**
     * Save the cached mail config, if previously loaded.
     */
    public static void saveConfig() throws ConfigException, IOException {
        if(mailClientConfig != null) {
            save(MAIL_CLIENT_CONFIG, mailClientConfig);
        }
    }
    
    /**
     * Specifies if this is the first run (no config was pre-saved)
     */
    public static boolean getFirstRun() {
        return firstRun;
    }
    
    /**
     * Save the new mail config, and cache it.
     */
    public static void saveConfig(MailClientConfig mcc)
    throws ConfigException, IOException {
        if(mcc != null) {
            save(MAIL_CLIENT_CONFIG, mcc);
            mailClientConfig = mcc;
        }
    }
    
    /** Get the name of the Mail Client from jad*/
    public static String getClientName() {
        return clientName;
    }
    
    /** Get the version of the mail client from jad */
    public static String getClientVersion() {
        return clientVersion;
    }
    
    /** Get the name of the Vendor from jad*/
    public static String getClientVendor() {
        return clientVendor;
    }
    
    /**
     * Accessor method to retrieve Storage size
     * @return int value of rms storage size.
     */
    public int getStorageSize() {
        return storageSize;
    }
    
    public static int getMaxMessageNumber() {
        return maxMessageNumber;
    }
    
    /** This method converts the configuration version 102 to 103.
     * It is for backward compatibily only and it can be removed in future
     * releases.
     * The method loads the configuration and rewrites it in the new format. The
     * main purpose it to replace the NamedObjectStore with an ObjectStore for
     * configuration storage.
     *

⌨️ 快捷键说明

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