dmtclientconfig.cpp

来自「funambol window mobile客户端源代码」· C++ 代码 · 共 957 行 · 第 1/2 页

CPP
957
字号
/*
 * 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 "base/fscapi.h"
#include "base/debug.h"
#include "base/errors.h"
#include "base/Log.h"
#include "base/util/utils.h"
#include "client/DMTClientConfig.h"
#include "spdm/constants.h"
#include "spdm/DMTreeFactory.h"
#include "spdm/DMTree.h"
#include "spdm/ManagementNode.h"
#include "base/globalsdef.h"

USE_NAMESPACE


void DMTClientConfig::initialize() {
    dmt = NULL;
    syncMLNode = NULL;
    sourcesNode = NULL;
}

DMTClientConfig::DMTClientConfig() : SyncManagerConfig() {
    initialize();
    rootContext = 0;
}


DMTClientConfig::DMTClientConfig(const char* root): SyncManagerConfig() {
    initialize();
    rootContext = new char[strlen(root)+1];
    strcpy(rootContext, root);
}


DMTClientConfig::~DMTClientConfig() {
    if (rootContext) {
        delete [] rootContext;
    }
    close();
}

SyncSourceConfig* DMTClientConfig::getSyncSourceConfig(const char* name, bool refresh) {
    if ((name == NULL) || (strlen(name) == 0)) {
        return NULL;
    }

    //
    // If refresh is true, we need to re-read the syncsource settings
    // from the DM tree.
    //
    // PS: for now we use a brute force approach so that we refresh the
    // entire configuration. A better implementation would be to just
    // refresh the single source.
    //
    if (refresh) {
      read();
    }

    for (unsigned int i=0; i<sourceConfigsCount; ++i) {
        if (strcmp(sourceConfigs[i].getName(), name) == 0) {
            return &sourceConfigs[i];
        }
    }

    return NULL;
}


SyncSourceConfig* DMTClientConfig::getSyncSourceConfig(unsigned int i, bool refresh) {
    if (i >= sourceConfigsCount) {
        return NULL;
    }

    //
    // If refresh is true, we need to re-read the syncsource settings
    // from the DM tree.
    //
    // PS: for now we use a brute force approach so that we refresh the
    // entire configuration. A better implementation would be to just
    // refresh the single source.
    //
    if (refresh) {
      read();
    }

    return &sourceConfigs[i];
}


bool DMTClientConfig::read() {
    int n = 0, i = 0; // number of sync sources

    bool ret = false;

    LOG.debug("%s", DBG_READING_CONFIG_FROM_DM);

    //
    // Reading syncml node
    //
    //char nodeName[DIM_MANAGEMENT_PATH];

    if (!open()) {
        return false;
    }

    readAccessConfig(*syncMLNode);
    readDeviceConfig(*syncMLNode);

    n = sourcesNode->getChildrenMaxCount();

    //
    // Let's remove previously created config objects and reinitialize
    // sourceConfigs
    //
    if (sourceConfigs) {
        delete [] sourceConfigs;
    }
    if (n>0) {
        sourceConfigs = new SyncSourceConfig[n];
    }
    sourceConfigsCount = n;

    for (i=0; i<n; ++i) {
        // node owns children, we must not delete them
        readSourceConfig(i, *(sourcesNode) );
    }

    ret = true;

//finally:

    close();
    return ret;
}


bool DMTClientConfig::save() {
    bool ret = false;
    unsigned int i = 0;

    LOG.debug("%s", DBG_WRITING_CONFIG_TO_DM);
    if (!open()) {
        return false;
    }

    if (accessConfig.getDirty()) {
        resetError();

        //
        // SyncML management node
        //
        saveAccessConfig(*syncMLNode);
    }
    //
    // TBD: handle the dirty flag
    //

    saveDeviceConfig(*syncMLNode);

    //
    // Sources management node
    //
    //lastErrorCode = ERR_NONE;
    resetError();
    for(i=0; i<sourceConfigsCount; ++i) {
        saveSourceConfig(i, *(sourcesNode) );
    }

    resetError();
    ret = (getLastErrorCode() != 0);

    close();
    return ret;
}

bool DMTClientConfig::open() {
    char nodeName[DIM_MANAGEMENT_PATH];
    nodeName[0] = 0;

    dmt = DMTreeFactory::getDMTree(rootContext);

    sprintf(nodeName, "%s%s", rootContext, CONTEXT_SPDS_SYNCML);
    syncMLNode = dmt->readManagementNode(nodeName);
    if (!syncMLNode ) {
        goto failed;
    }

    sprintf(nodeName, "%s%s", rootContext, CONTEXT_SPDS_SOURCES);
    sourcesNode = dmt->readManagementNode(nodeName);
    if (!sourcesNode) {
        goto failed;
    }

    return true;

failed:
    //lastErrorCode = ERR_INVALID_CONTEXT;
    //sprintf(lastErrorMsg, ERRMSG_INVALID_CONTEXT, nodeName);
    setErrorF(ERR_INVALID_CONTEXT, ERRMSG_INVALID_CONTEXT, nodeName);
    close();
    return false;
}

ManagementNode* DMTClientConfig::getSyncMLNode() {
    return syncMLNode;
}

int DMTClientConfig::getNumSources() {
    return sourcesNode ?
        sourcesNode->getChildrenMaxCount() :
        -1;
}

ManagementNode* DMTClientConfig::getSyncSourceNode(int index) {
    return sourcesNode ?
        sourcesNode->getChild(index) :
        NULL;
}

ManagementNode* DMTClientConfig::getSyncSourceNode(const char* name) {
    return sourcesNode ?
        sourcesNode->getChild(name) :
        NULL;
}

void DMTClientConfig::close() {
    if (syncMLNode) {
        delete syncMLNode;
        syncMLNode = NULL;
    }
    if (sourcesNode) {
        delete sourcesNode;
        sourcesNode = NULL;
    }
    if (dmt) {
        delete dmt;
        dmt = NULL;
    }
}


/*
 * Read Access Config properties stored in DMTree.
 * Access properties are placed in 3 nodes under syncML node
 * (Auth - Conn - Ext)
 *
 * @param n: the 'syncml' node (parent node)
 * @return : true if config is correctly read
 */
bool DMTClientConfig::readAccessConfig(ManagementNode& n) {

    bool ret = true;
    char nodeName[DIM_MANAGEMENT_PATH];
    nodeName[0] = 0;
    ManagementNode* node;

    char syncMLContext[DIM_MANAGEMENT_PATH];
    char* fn = n.createFullName();
    sprintf(syncMLContext, "%s", fn);
    delete [] fn;

    //
    // Auth properties
    //
    sprintf(nodeName, "%s%s", syncMLContext, CONTEXT_AUTH);
    node = dmt->readManagementNode(nodeName);
    if (node) {
        if (!readAuthConfig(n, *node)) {
            ret = false;
        }
        delete node;
        node = NULL;
    }
    else {
        ret = false;
    }

    //
    // Conn properties
    //
    sprintf(nodeName, "%s%s", syncMLContext, CONTEXT_CONN);
    node = dmt->readManagementNode(nodeName);
    if (node) {
        if (!readConnConfig(n, *node)) {
            ret = false;
        }
        delete node;
        node = NULL;
    }
    else {
        ret = false;
    }

    //
    // Ext properties (other misc props)
    //
    sprintf(nodeName, "%s%s", syncMLContext, CONTEXT_EXT);
    node = dmt->readManagementNode(nodeName);
    if (node) {
        if (!readExtAccessConfig(n, *node)) {
            ret = false;
        }

        delete node;
        node = NULL;
    }
    else {
        ret = false;
    }

    return true;
}

/*
 * Save Access Config properties in DMTree.
 * Access properties are placed in 3 nodes under syncML node
 * (Auth - Conn - Ext)
 *
 * @param n: the 'syncml' node (parent node)
 */
void DMTClientConfig::saveAccessConfig(ManagementNode& n) {

    ManagementNode* node;
    char nodeName[DIM_MANAGEMENT_PATH];

    char syncMLContext[DIM_MANAGEMENT_PATH];
    char* fn = n.createFullName();
    sprintf(syncMLContext, "%s", fn);
    delete [] fn;

    //
    // Auth properties
    //
    sprintf(nodeName, "%s%s", syncMLContext, CONTEXT_AUTH);
    node = dmt->readManagementNode(nodeName);
    if (node) {
        saveAuthConfig(n, *node);
        delete node;
        node = NULL;
    }

    //
    // Conn properties
    //
    sprintf(nodeName, "%s%s", syncMLContext, CONTEXT_CONN);
    node = dmt->readManagementNode(nodeName);
    if (node) {
        saveConnConfig(n, *node);
        delete node;
        node = NULL;
    }

    //
    // Ext properties (other misc props)
    //
    sprintf(nodeName, "%s%s", syncMLContext, CONTEXT_EXT);
    node = dmt->readManagementNode(nodeName);
    if (node) {
        saveExtAccessConfig(n, *node);
        delete node;
        node = NULL;
    }
}


/*
 * Read Device Config properties stored in DMTree.
 * Device properties are placed in 3 nodes under syncML node
 * (DevInfo - DevDetail - Ext)
 *
 * @param n: the 'syncml' node (parent node)
 * @return : true if config is correctly read
 */
bool DMTClientConfig::readDeviceConfig(ManagementNode& n) {

    bool ret = true;
    char nodeName[DIM_MANAGEMENT_PATH];
    nodeName[0] = 0;
    ManagementNode* node;

    char syncMLContext[DIM_MANAGEMENT_PATH];
    char* fn = n.createFullName();
    sprintf(syncMLContext, "%s", fn);
    delete [] fn;
    //
    // DevInfo properties
    //
    sprintf(nodeName, "%s%s", syncMLContext, CONTEXT_DEV_INFO);
    node = dmt->readManagementNode(nodeName);
    if (node) {
        if (!readDevInfoConfig(n, *node)) {
            ret = false;
        }
        delete node;
        node = NULL;
    }
    else {
        ret = false;
    }

    //
    // DevDetail properties
    //
    sprintf(nodeName, "%s%s", syncMLContext, CONTEXT_DEV_DETAIL);
    node = dmt->readManagementNode(nodeName);
    if (node) {
        if (!readDevDetailConfig(n, *node)) {
            ret = false;
        }
        delete node;
        node = NULL;
    }
    else {
        ret = false;
    }

    //
    // Ext properties (other misc props)
    //
    sprintf(nodeName, "%s%s", syncMLContext, CONTEXT_EXT);
    node = dmt->readManagementNode(nodeName);
    if (node) {
        if (!readExtDevConfig(n, *node)) {
            ret = false;
        }
        delete node;
        node = NULL;
    }
    else {
        ret = false;
    }

    return ret;
}


/*
 * Save Device Config properties in DMTree.
 * Device properties are placed in 3 nodes under syncML node
 * (DevInfo - DevDetail - Ext)
 *
 * @param n: the 'syncml' node (parent node)
 */
void DMTClientConfig::saveDeviceConfig(ManagementNode& n) {

⌨️ 快捷键说明

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