configparams.cpp

来自「funambol window mobile客户端源代码」· C++ 代码 · 共 262 行

CPP
262
字号
/*
 * 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".
 */
#include "pim/ConfigParams.h"
#include "pim/account.h"
#include "pim/ClientSettings.h"
#include "pim/ConfigSyncSource.h"
#include "processUtils.h"


ConfigParams::ConfigParams() {
   visibleName       = "";
   emailAddress      = "default@email";
 //ipAddress         = "";
   pushStatus        = false;
   emailFilterStatus = false;
}

ConfigParams::~ConfigParams() {
   

}

bool ConfigParams::save() {
    
    if (dirty == 0) {
        // Nothing to do.
        return true; 
    }

    bool ret = true;
    bool dirtyRootConfig = false;

    //
    // Email account settings. *** signature: TODO ***
    //
    if ((dirty & CP_DIRTY_VISIBLE_NAME) || 
        (dirty & CP_DIRTY_EMAIL_ADDRESS)) {     // We have to save the account settings.

        HRESULT hr = S_OK;
        if (dirty & CP_DIRTY_EMAIL_ACCOUNT_ALL) { 
            // Both set. Don't read the current account info (faster).
        }
        else {
            // Set only the param that is dirty.
            string newEmailAddress = emailAddress;
            string newVisibleName  = visibleName;

            ret = readEmailAccountParams();

            if (dirty & CP_DIRTY_EMAIL_ADDRESS) {
                emailAddress = newEmailAddress;
            }
            if (dirty & CP_DIRTY_VISIBLE_NAME) {
                visibleName = newVisibleName;
            }
        }

        // Safe check.
        if (emailAddress == "") {
            emailAddress = "default@email";
        }

        // Save the Email account info. 
        wchar_t* wemailAddress = toWideChar(emailAddress.c_str());
        wchar_t* wvisibleName  = toWideChar(visibleName.c_str());

        LOG.debug("Saving Email account info from Server: %s (%s)", emailAddress.c_str(), visibleName.c_str());
        hr = modifyAccount(wvisibleName, wemailAddress);
        if (FAILED(hr)) {
            LOG.error("ConfigParams::save() - Error saving the Email account info.");
            ret = false;
        }

        if (wemailAddress) { delete [] wemailAddress; }
        if (wvisibleName)  { delete [] wvisibleName;  }
    }

    //
    // Push status.
    //
    if (dirty & CP_DIRTY_PUSH_STATUS) {

        ClientSettings* cs = getRegConfig();
        string currentPush = cs->getPush();
        bool currentPushStatus = (currentPush == "0")? false : true;

        if (!currentPushStatus && pushStatus) {
            // Enable push
            LOG.debug("ConfigParams - enabling Push service...");
            cs->setSms    ("1");
            cs->setPush   ("3");
            cs->setDirty(CS_DIRTY_ROOT_CONFIG);
            cs->saveDirty();        // Need to save before registering dll
            startProgram(TEXT("startsync.exe"),    TEXT("register"));
            startProgram(TEXT("handleservice.exe"),TEXT("register"));
        }
        else if (currentPushStatus && !pushStatus) {
            // Disable push
            LOG.debug("ConfigParams - disabling Push service...");
            cs->setSms    ("0");
            cs->setPush   ("0");
            cs->setDirty(CS_DIRTY_ROOT_CONFIG);
            cs->saveDirty();        // Need to save before deregistering dll
            startProgram(TEXT("startsync.exe"),    TEXT("deregister"));
            startProgram(TEXT("handleservice.exe"),TEXT("deregister"));
        }
    }

    //
    // Email filtering status.
    //
    if (dirty & CP_DIRTY_EMAIL_FILTER_STATUS) {

        ClientSettings* cs = getRegConfig();
        string currentEmailFilter = cs->getEmailFilter();
        bool currentEmailFilterStatus = (currentEmailFilter == "0")? false : true;

        if (!currentEmailFilterStatus && emailFilterStatus) {
            // Enable email filtering
            LOG.debug("ConfigParams - enabling email filter");
            cs->setEmailFilter("1");
            dirtyRootConfig = true;
        }
        else if (currentEmailFilterStatus && !emailFilterStatus) {
            // Disable email filtering
            LOG.debug("ConfigParams - disabling email filter");
            cs->setEmailFilter("0");
            dirtyRootConfig = true;
        }
    }

    if (dirtyRootConfig) {
        ClientSettings* cs = getRegConfig();
        cs->setDirty(CS_DIRTY_ROOT_CONFIG);
        cs->saveDirty();
    }

    // Everything saved: reset the dirty for Server2Client changes.
    dirty = 0;

    return ret;
}


bool ConfigParams::read() {

    bool ret = readEmailAccountParams();
    
    ClientSettings* cs = getRegConfig();
    pushStatus        = (cs->getPush()        != "0")? true : false;
    emailFilterStatus = (cs->getEmailFilter() != "0")? true : false;
  //ipAddress         = cs->getIp();
    
    return ret;
}



string ConfigParams::getParam(const wstring paramName) {

    if (paramName == PUSH_STATUS_FIELD) {
         string ret = (pushStatus)? "1" : "0";
         return ret;
    }
    else if (paramName == EMAIL_FILTER_STATUS_FIELD) {
         string ret = (emailFilterStatus)? "1" : "0";
         return ret;
    }
    else if (paramName == EMAIL_ADDRESS_FIELD) { return emailAddress; }
    else if (paramName == VISIBLE_NAME_FIELD)  { return visibleName;  }
  //else if (paramName == IP_ADDRESS_FIELD)    { return ipAddress;    }
    else {
        LOG.error("Internal error: config param not found '%ls'", paramName.c_str());
        return "";
    }
}



void ConfigParams::setVisibleName(const string v) { 
    visibleName = v;
    setDirtyFlag(CP_DIRTY_VISIBLE_NAME);
} 
void ConfigParams::setEmailAddress(const string v) { 
    emailAddress = v;
    setDirtyFlag(CP_DIRTY_EMAIL_ADDRESS);
}
void ConfigParams::setPushStatus(const bool v) { 
    pushStatus = v;
    setDirtyFlag(CP_DIRTY_PUSH_STATUS);
}
void ConfigParams::setEmailFilterStatus(const bool v) { 
    emailFilterStatus = v;
    setDirtyFlag(CP_DIRTY_EMAIL_FILTER_STATUS);
}
//void ConfigParams::setEmailSignature(const string v) { 
//    emailSignature = v;
//    setDirtyFlag(CP_DIRTY_EMAIL_SIGNATURE);
//}



bool ConfigParams::readEmailAccountParams() {

    wchar_t* currentVisibleName = new wchar_t[ACCOUNT_PARAM_SIZE];
    wchar_t* currentAddress     = new wchar_t[ACCOUNT_PARAM_SIZE];
    bool ret = true;

    LOG.debug("Reading Email account info...");
    HRESULT hr = getAccountInfo(currentVisibleName, currentAddress);
    if (FAILED(hr)) {
        LOG.error("ConfigParams - Error reading the Email account info.");
        ret = false;
        wsprintf(currentVisibleName, TEXT(""));
        wsprintf(currentAddress,     TEXT(""));
    }

    char* tmp = toMultibyte(currentAddress);
    emailAddress = tmp;
    delete [] tmp;

    tmp = toMultibyte(currentVisibleName);
    visibleName = tmp;
    delete [] tmp;

    if (currentVisibleName) { delete [] currentVisibleName; }
    if (currentAddress)     { delete [] currentAddress;     }
    return ret;
}

⌨️ 快捷键说明

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