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

📄 configmanager.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.config;

import com.funambol.mailclient.Account;
import com.funambol.mailclient.MailFilter;
import com.funambol.mailclient.sm.SyncClientConfig;

import com.funambol.storage.NamedObjectStore;
import com.funambol.storage.Serializable;
import com.funambol.util.DateUtil;

import com.funambol.util.Log;
import com.funambol.util.StringUtil;
import javax.microedition.rms.RecordStore;

import javax.microedition.rms.RecordStoreException;
import java.io.IOException;
import java.util.Date;
import javax.microedition.midlet.MIDlet;
import com.funambol.mailclient.Funambol;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 * 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 {
    
    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 NamedObjectStore confstore = new NamedObjectStore();
    // 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 clientVersion;
    
    private static int FILTER_WINDOW = 10;
    
    private static int storageSize;

    private static int maxMessageNumber = LO_MSG_NUMBER;
    
    
    //------------------------------------------------------------- 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 {
        try {
            confstore.open(CONFIGNAME);
            confstore.retrieve(name, obj);
        } catch (RecordStoreException e) {
            Log.error("Can't load " + name + ": " + 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 boolean save(String name, Serializable obj)
    throws ConfigException, IOException {
        try {
            confstore.create(CONFIGNAME);
            return confstore.store(name, obj);
        } catch (RecordStoreException e) {
            Log.error("Can't save " + name + ": " + 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(MailClientConfig.NAME, 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);
            recoverConfig(midlet, 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(midlet, false);
        } finally {
            // Settings always from Jad, never stored in Storage
            clientName = midlet.getAppProperty("MIDlet-Name");
            Log.info("ClientName: " + clientName);
            clientVersion = midlet.getAppProperty("MIDlet-Version");
            Log.info("ClientVersion: " + clientVersion);
            
            boolean flagCompress = StringUtil.getBooleanValue(
                    midlet.getAppProperty(ENABLE_COMPRESS));
            Log.info("Compress from Jad: " + "[" + flagCompress + "]");
            mailClientConfig.enableCompress(flagCompress);
            
            // Disable date filter only if present and set to false
            String dateFilter = midlet.getAppProperty(ENABLE_DATE_FILTER);
            if(dateFilter != null) {
                if (StringUtil.equalsIgnoreCase("false", dateFilter)) {
                    Log.info("DateFilter from Jad: [false]");
                    mailClientConfig.enableDateFilter(false);
                }
            }
            String appInfoAttr = midlet.getAppProperty(APP_INFO_ATTR);
            if (appInfoAttr!=null && !appInfoAttr.startsWith("Moto")) {
                maxMessageNumber=HI_MSG_NUMBER;
            }
        }
        return mailClientConfig;
    }
    
    private static void recoverConfig(Funambol midlet, boolean firstrun) 
            throws ConfigException, IOException {
        mailClientConfig = getConfigFromJad(midlet);
        int size =0;
        if (firstrun) {
            size = loadRmsSize();
        } else {
            size = 512*1024;
        }
        mailClientConfig.setRmsStoreSize(size);
        saveConfig(mailClientConfig);
        // init the SyncClientConfig with the device id from jad, if present
        // otherwise, a default one will be generated.

⌨️ 快捷键说明

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