accountsettingsform.java

来自「moblie syncml mail javame」· Java 代码 · 共 378 行

JAVA
378
字号
/*
 * 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.ui.view;

import com.funambol.mailclient.sm.SyncClient;
import com.funambol.mailclient.ui.controller.UIController;
import com.funambol.mailclient.config.*;
import com.funambol.mailclient.Account;
import com.funambol.mailclient.ui.view.ModalPopup;
import com.funambol.mailclient.ui.view.PopupAction;
import com.funambol.mailclient.loc.Localization;
import com.funambol.mailclient.ui.controller.UIConnectionHandler;
import com.funambol.push.CTPService;
import com.funambol.push.PushConfig;
import com.funambol.util.Log;
import com.funambol.util.StringUtil;
import java.io.IOException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;

/**
 * This form shows all the settings related to the
 * Account functions
 * The user will be able to configure username, password,
 * sync server url.
 * The form shows also the deviceId used internally to
 * synchronize
 */
public class AccountSettingsForm extends Form implements CommandListener {

    private Command backCommand;
    private Command saveCommand;
    private TextField serverUrlTF;
    private TextField userNameTF;
    //private TextField emailRemoteNameTF;
    private TextField passwordTF;
    private StringItem deviceIdSringItem;
    private TextField emailAddressTF;
    private TextField visibleNameTF;
    //private TextField contactsRemoteNameTF;
    private MailClientConfig mailClientConfig;
    private ModalPopup funPopUP;
    private boolean syncIsBusy;

    /**
     * Creates a new instance of AccountSettingsForm
     */
    public AccountSettingsForm() {
        super(Localization.getMessages().CONNECTION_SETTINGS_FORM_TITLE);

        mailClientConfig = UIController.getConfig();

        //#ifdef isBlackberry
        //# saveCommand = new Command(Localization.getMessages().SAVE_COMMAND, Command.OK, 5);
        //# backCommand = new Command(Localization.getMessages().BACK_COMMAND, Command.BACK, 10);
        //# addCommand(UIController.blackberryExitCommand);
        //#else
        backCommand = UIController.backCommand;
        saveCommand = UIController.saveCommand;
        //#endif
        syncIsBusy = SyncClient.getSyncClient().isBusy();

        append(getServerUrlTF());
        append(getUserNameTF());
        append(getPasswordTF());
        append(getVisibleNameTF());
        append(getEmailAddressTF());

        //append(getEmailRemoteNameTF());
        //append(getContactsRemoteNameTF());

        addCommand(backCommand);

        if (!UIController.isSyncInProgress()) {
            addCommand(saveCommand);
        }

        setCommandListener(this);
    }

    /**
     * 
     * used to find the item that should be focused.
     * if username or passowrd textfield are empty, that item is returned
     * otherwise server textfield is returned
     * 
     * @return the item that should be focused
     */
    public Item getFocusedItem() {
        if ("".equals(getUserNameTF().getString())) {
            Log.debug("returning username TF");
            return getUserNameTF();
        } else if ("".equals(getPasswordTF().getString())) {
            Log.debug("returning pwd TF");
            return getPasswordTF();

        } else {
            Log.debug("returning url TF");
            return getServerUrlTF();
        }

    }

    /**
     * command handling method
     */
    public void commandAction(Command command, Displayable displayable) {
        if (command == backCommand) {
            UIController.showBackScreen();
            UIController.accountSettingsForm = null;
        } else if (command == saveCommand) {
            // save settings

            // Sanity checks: empty url or default and empty credentials
            //                are not valid.
            // FIXME: why can these be null??

            if (StringUtil.isNullOrEmpty(userNameTF.getString()) ||
                    StringUtil.isNullOrEmpty(serverUrlTF.getString()) ||
                    StringUtil.isNullOrEmpty(passwordTF.getString())) {

                Log.info("Empty Url or credentials field found: fill it!!");
                UIController.showAlert(
                        Localization.getMessages().CSF_EMPTY_CREDENTIALS_ERROR);
                return;
            } else if (!checkValidEmail(emailAddressTF.getString())) {
                Log.info("Email Address field needs valid email address value.");
                UIController.showAlert(
                        Localization.getMessages().CSF_INVALID_EMAIL_ERROR);
                return;
            }

            //before saving the account trim the fields
            trimFields();

            // Get the current account
            Account mailAccount = UIController.getConfig().getMailAccount();

            // If the account was already set
            if (mailAccount.isSet()) {
                // If username or url are changed, we must alert the user
                // and reset the account.
                boolean userChanged =
                        !userNameTF.getString().equals(mailAccount.getUser());
                boolean urlChanged =
                        !serverUrlTF.getString().equals(mailAccount.getUrl());


                // if the pass has been changed, we need to restart the ctp
                boolean passChanged =
                        !passwordTF.getString().equals(mailAccount.getPassword());

                if (userChanged || urlChanged) {
                    Log.info("Saving new account. Account has changed");
                    funPopUP = new ModalPopup(
                            Localization.getMessages().SAVING_ACCOUNT_POPUP_TITLE,
                            Localization.getMessages().SAVING_ACCOUNT_POPUP_MSG,
                            new SavingAccountAction());
                    UIController.showModalPopup(funPopUP, this);
                } else {

                    if (passChanged) {
                        Log.debug("[AccountSettingsForm] pass has been changed, checking if we need to restart ctp");

                        if (mailClientConfig.getCtpPushStatus() == mailClientConfig.CTP_ENABLED) {
                            CTPService.getInstance().getConfig().setCtpPassword(passwordTF.getString());
                            CTPService.getInstance().restartService();
                        }
                    }

                    mailClientConfig = getNewConfigFromUser();
                    UIController.saveNewConfig(mailClientConfig);
                    UIController.showBackScreen();
                }

            } else {
                // if it's the first start, save the config and
                // start the initial sync.
                mailClientConfig = getNewConfigFromUser();
                UIController.saveNewConfig(mailClientConfig);
                UIController.showInboxScreen();
                UIController.setSyncCaller(UIController.USER);
                UIController.sync(true);
            }
        } else if (command == UIController.blackberryExitCommand) {
            UIController.midlet.destroyApp(false);
        }
    }

    private MailClientConfig getNewConfigFromUser() {
        Account userAccount = new Account();
        userAccount.setUser(userNameTF.getString());
        userAccount.setPassword(passwordTF.getString());
        userAccount.setUrl(serverUrlTF.getString());
        //userAccount.setRemoteURI(emailRemoteNameTF.getString());
        //userAccount.setPimRemoteURI(contactsRemoteNameTF.getString());
        userAccount.setAddress(emailAddressTF.getString());
        userAccount.setName(visibleNameTF.getString());
        mailClientConfig.setMailAccount(userAccount);
        return mailClientConfig;

    }

    private TextField getServerUrlTF() {
        if (serverUrlTF == null) {
            serverUrlTF = new TextField(Localization.getMessages().CSF_SERVER_URL_TF_LABEL,
                    mailClientConfig.getMailAccount().getUrl(), 120, TextField.ANY | TextField.NON_PREDICTIVE);
            serverUrlTF.setInitialInputMode("MIDP_LOWERCASE_LATIN");
        }

        setUneditableTextField(serverUrlTF);
        return serverUrlTF;
    }

    private TextField getUserNameTF() {
        if (userNameTF == null) {
            userNameTF = new TextField(Localization.getMessages().CSF_USERNAME_TF_LABEL,
                    mailClientConfig.getMailAccount().getUser(), 120, TextField.ANY | TextField.NON_PREDICTIVE);
            userNameTF.setInitialInputMode("MIDP_LOWERCASE_LATIN");

        }
        setUneditableTextField(userNameTF);
        return userNameTF;
    }

    /*
    private TextField getEmailRemoteNameTF() {
    if (emailRemoteNameTF == null) {
    emailRemoteNameTF = new TextField(Localization.getMessages().CSF_EMAIL_REMOTE_NAME_TF_LABEL,
    mailClientConfig.getMailAccount().getRemoteURI(), 120, TextField.ANY | TextField.NON_PREDICTIVE);
    emailRemoteNameTF.setInitialInputMode("MIDP_LOWERCASE_LATIN");
    }
    setUneditableTextField(emailRemoteNameTF);
    return emailRemoteNameTF;
    }*/
    private TextField getPasswordTF() {
        if (passwordTF == null) {
            passwordTF = new TextField(Localization.getMessages().CSF_PASSWORD_TF_LABEL,
                    mailClientConfig.getMailAccount().getPassword(), 120, TextField.PASSWORD);
            passwordTF.setInitialInputMode("MIDP_LOWERCASE_LATIN");
        }
        setUneditableTextField(passwordTF);
        return passwordTF;
    }

    /*
    private TextField getContactsRemoteNameTF() {
    if (contactsRemoteNameTF == null){
    contactsRemoteNameTF =
    new TextField(Localization.getMessages().CSF_CONTACTS_REMOTE_NAME_TF_LABEL,
    mailClientConfig.getMailAccount().getPimRemoteURI(), 120, TextField.ANY | TextField.NON_PREDICTIVE);
    getContactsRemoteNameTF().setInitialInputMode("MIDP_LOWERCASE_LATIN");
    }
    setUneditableTextField(contactsRemoteNameTF);
    return contactsRemoteNameTF;
    }*/
    private TextField getEmailAddressTF() {
        if (emailAddressTF == null) {
            emailAddressTF =
                    new TextField(Localization.getMessages().CSF_EMAIL_ADDRESS_TF_LABEL,
                    mailClientConfig.getMailAccount().getAddress(), 120, TextField.ANY | TextField.NON_PREDICTIVE);
            emailAddressTF.setInitialInputMode("MIDP_LOWERCASE_LATIN");
        }
        setUneditableTextField(emailAddressTF);
        return emailAddressTF;
    }

    private TextField getVisibleNameTF() {
        if (visibleNameTF == null) {
            visibleNameTF =
                    new TextField(Localization.getMessages().CSF_VISIBLE_NAME_TF_LABEL,
                    mailClientConfig.getMailAccount().getName(), 120, TextField.ANY | TextField.NON_PREDICTIVE);
            visibleNameTF.setInitialInputMode("MIDP_LOWERCASE_LATIN");
        }
        setUneditableTextField(visibleNameTF);
        return visibleNameTF;
    }

    //This method disable editing of TextFields when
    //a sync process was started
    private void setUneditableTextField(TextField textField) {
        //#ifndef EditTextField
        if (syncIsBusy) {
            textField.setConstraints(TextField.UNEDITABLE | textField.getConstraints());
        }
    //#endif
    }

    private boolean checkValidEmail(String emailTF) {
        if ((emailTF == null) || (emailTF.equals(""))) {
            return true;
        } else {
            return (emailTF.indexOf("@") != -1);
        }
    }

    // trim fields removing useless and wrong spaces at the beginning and at the end
    private void trimFields() {
        TextField tempTF = null;
        for (int i = 0; i < this.size(); i++) {
            tempTF = (TextField) this.get(i);
            tempTF.setString(tempTF.getString().trim());
        }
    }

    /**
     * enable or disable the send command
     * @param enable if true enable the command, if false disable the command
     */
    public void enableSaveCommand(boolean enable) {
        Log.debug(this, "Enable save commands: " + enable);
        if (enable) {
            addCommand(saveCommand);
        } else {
            removeCommand(saveCommand);
        }
    }

    private class SavingAccountAction implements PopupAction, Runnable {

        public SavingAccountAction() {
        }

        public void cancel() {
            UIController.showBackScreen();
        }

        public void confirm() {
            funPopUP.setTitle(Localization.getMessages().RESETTING_MSG);
            mailClientConfig = getNewConfigFromUser();
            UIController.saveNewConfig(mailClientConfig);
            UIController.getThreadPool().startThread(this);

        }

        public void run() {
            UIController.resetAccount();
        }
    }
}

⌨️ 快捷键说明

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