configsyncsource.cpp

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

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


ConfigSyncSource::ConfigSyncSource(const wchar_t* name, AbstractSyncSourceConfig* sc) :
                  BaseSyncSource(name, sc), c() {
}

ConfigSyncSource::~ConfigSyncSource() {  
}

void ConfigSyncSource::setMimeType(string t) {
    mimeType = t;
}

string ConfigSyncSource::getMimeType() {
    return mimeType;
}


int ConfigSyncSource::beginSync() {

    if (c.read()) {
        int dirty = 0;

        // Check if account settings have been modified.
        // We don't capture this immediately, so we check it here.
        if (getRegConfig()->getLastEmailAdress() != c.getEmailAddress()) {
            dirty |= CP_DIRTY_EMAIL_ADDRESS;
        }
        if (getRegConfig()->getLastEmailVisibleName() != c.getVisibleName()) {
            dirty |= CP_DIRTY_VISIBLE_NAME;
        }

        // Update the flag.
        getRegConfig()->setConfigSyncDirty(dirty);
        return 0;
    }

    return -1;
}

int ConfigSyncSource::endSync() {
    
    if (c.save()) {

        int dirty = 0;
        ClientSettings* cs = getRegConfig();

        // Config sync finished, so we save account settings for next time.
        if (cs->getLastEmailAdress() != c.getEmailAddress()) {
            cs->setLastEmailAdress(c.getEmailAddress());
        }
        if (cs->getLastEmailVisibleName() != c.getVisibleName()) {
            cs->setLastEmailVisibleName(c.getVisibleName());
        }

        // Save now to registry: root params are not saved in ClientSettings::save().
        cs->setDirty(CS_DIRTY_ROOT_CONFIG);
        cs->saveDirty();

        return 0;
    }

    return -1;    
}


/*
* Return the first SyncItem of all. It is used in case of slow or refresh sync
* and retrieve the entire data source content.
*/
SyncItem* ConfigSyncSource::getFirstItem() {

    // Create a list of all items to be synced.
    // note: "emailAddress" and "emailVisibleName" are NOT included.
    itemKeys.clear();
    itemKeys.push_back(PUSH_STATUS_FIELD);
    itemKeys.push_back(EMAIL_FILTER_STATUS_FIELD);
  //itemKeys.push_back(IP_ADDRESS_FIELD);


    // Get the first item.
    iter = itemKeys.begin();
    
    string& data = c.getParam(*iter);
    SyncItem* sItem = new SyncItem((*iter).c_str());
    sItem->setData(data.c_str(), data.size());
    iter ++;

    return sItem;
}

/*
* Return the next SyncItem of all. It is used in case of slow or refresh sync
* and retrieve the entire data source content.
*/

SyncItem* ConfigSyncSource::getNextItem() {

    if (iter != itemKeys.end()) {
        // Get the next item.
        string& data = c.getParam(*iter);
        SyncItem* sItem = new SyncItem((*iter).c_str());
        sItem->setData(data.c_str(), data.size());
        iter ++;

        return sItem;
    }

    return NULL;
}



SyncItem* ConfigSyncSource::getFirstUpdatedItem() {

    // Dirty flag for local modifications. 
    // Each bit will be cleared once the status 200 is received from Server.
    int dirty = getRegConfig()->getConfigSyncDirty();
    if (dirty == 0) { 
        return NULL;
    }

    // Create the list of modified itemKeys (params locally changed).
    itemKeys.clear();
    if (dirty & CP_DIRTY_VISIBLE_NAME)        { itemKeys.push_back(VISIBLE_NAME_FIELD);        }
    if (dirty & CP_DIRTY_EMAIL_ADDRESS)       { itemKeys.push_back(EMAIL_ADDRESS_FIELD);       }
  //if (dirty & CP_DIRTY_IP_ADDRESS)          { itemKeys.push_back(IP_ADDRESS_FIELD);          }
    if (dirty & CP_DIRTY_PUSH_STATUS)         { itemKeys.push_back(PUSH_STATUS_FIELD);         }
    if (dirty & CP_DIRTY_EMAIL_FILTER_STATUS) { itemKeys.push_back(EMAIL_FILTER_STATUS_FIELD); }


    // Get the first item.
    iter = itemKeys.begin();
    
    if (iter != itemKeys.end()) {
        string& data = c.getParam(*iter);
        SyncItem* sItem = new SyncItem((*iter).c_str());
        sItem->setData(data.c_str(), data.size());
        iter ++;

        return sItem;
    }

    // Finished: DON'T reset dirty flag for local modifications.
    // It's done clearing each bit on the setItemStatus()

    return NULL;
}


SyncItem* ConfigSyncSource::getNextUpdatedItem() {

    if (iter != itemKeys.end()) {
        // Get the next item.
        string& data = c.getParam(*iter);
        SyncItem* sItem = new SyncItem((*iter).c_str());
        sItem->setData(data.c_str(), data.size());
        iter ++;

        return sItem;
    }

    // Finished: DON'T reset dirty flag for local modifications.
    // It's done clearing each bit on the setItemStatus()

    return NULL;
}


// Not used.
SyncItem* ConfigSyncSource::getFirstNewItem()     { return NULL; }
SyncItem* ConfigSyncSource::getNextNewItem()      { return NULL; }
SyncItem* ConfigSyncSource::getFirstDeletedItem() { return NULL; }
SyncItem* ConfigSyncSource::getNextDeletedItem()  { return NULL; }



void ConfigSyncSource::setItemStatus(const wchar_t* key, int status) {

    LOG.debug("key: %ls, status: %i", key, status);

    if (status > 299 && status != 418 && status != 419) {
        //
        // Error from Server. Just don't touch the configSyncDirty.
        // In UI, we check that flag to know if params were updated.
        //
    }
    else {
        //
        // OK from Server: clear the corresponding bit from configSyncDirty.
        //
        ClientSettings* cs = getRegConfig();
        LOG.debug("configSyncDirty flag was: %d", cs->getConfigSyncDirty());

        if      (!wcscmp(key, PUSH_STATUS_FIELD))         cs->clearConfigSyncDirty(CP_DIRTY_PUSH_STATUS);
        else if (!wcscmp(key, EMAIL_ADDRESS_FIELD))       cs->clearConfigSyncDirty(CP_DIRTY_EMAIL_ADDRESS);
        else if (!wcscmp(key, VISIBLE_NAME_FIELD))        cs->clearConfigSyncDirty(CP_DIRTY_VISIBLE_NAME);
        else if (!wcscmp(key, EMAIL_FILTER_STATUS_FIELD)) cs->clearConfigSyncDirty(CP_DIRTY_EMAIL_FILTER_STATUS);
        LOG.debug("Clear configSyncDirty flag: %d", cs->getConfigSyncDirty());

        // Save immediately! we want to know which params have been correctly
        // upsated on the Server, even if the sync then fails for something else.
        cs->setDirty(CS_DIRTY_ROOT_CONFIG);
        cs->saveDirty();
    }
}

int ConfigSyncSource::addItem(SyncItem& item) {

    int ret = STC_COMMAND_FAILED;
    int h = updateConfigParams(item);
    
    if (h == 0) {
        ret = STC_ITEM_ADDED;
        LOG.debug("ConfigSync - added item: %ls", item.getKey());                
    }    
    return ret;
}

int ConfigSyncSource::updateItem(SyncItem& item) {
    
    int ret = STC_COMMAND_FAILED;
    int h = updateConfigParams(item);
    
    if (h == 0) {
        ret = STC_OK;
        LOG.debug("ConfigSync - updated item: %ls", item.getKey());                
    }    
    return ret;
}

int ConfigSyncSource::deleteItem(SyncItem& item) {
    return STC_COMMAND_FAILED;
}


void ConfigSyncSource::assign(ConfigSyncSource& s) {
    setSyncMode(s.getSyncMode());
    setLastSync(s.getLastSync());
    setNextSync(s.getNextSync());
    setLastAnchor(s.getLastAnchor());
    setNextAnchor(s.getNextAnchor());
    setFilter(s.getFilter());   
    setReport(s.getReport());
    setMimeType(s.getMimeType());
}

ArrayElement* ConfigSyncSource::clone() {
    ConfigSyncSource* s = new ConfigSyncSource(getName(), &(getConfig()));    
    s->assign(*this);
    return s;
}

SyncItem* ConfigSyncSource::getFirstItemKey() {
    return NULL;
}

SyncItem* ConfigSyncSource::getNextItemKey() {
    return NULL;
}

/*************************************************/
/*************************************************/
/**************** Custom Methods *****************/
/*************************************************/
/*************************************************/


int ConfigSyncSource::updateConfigParams(SyncItem& item) {

    int ret = 0;
    StringBuffer data((const char*)item.getData(), item.getDataSize());   

    if (wcscmp(item.getKey(), EMAIL_ADDRESS_FIELD) == 0) {
        c.setEmailAddress(data.c_str());
    } 
    else if (wcscmp(item.getKey(), VISIBLE_NAME_FIELD) == 0) {
        c.setVisibleName(data.c_str());
    } 
    else if (wcscmp(item.getKey(), EMAIL_FILTER_STATUS_FIELD) == 0) {
        // Every value != "0" is considered 'true'.
        bool value = (data != "0")? true : false;
        c.setEmailFilterStatus(value);
    }
    else if (wcscmp(item.getKey(), PUSH_STATUS_FIELD) == 0) {
        // Every value != "0" is considered 'true'.
        bool value = (data != "0")? true : false;
        c.setPushStatus(value);
    }
    //else if (wcscmp(item.getKey(), IP_ADDRESS_FIELD) == 0) {
    // nothing to do: 'ipAddress' is read only
    //}
    //else if (wcscmp(item.getKey(), EMAIL_SIGNATURE_FIELD) == 0) {
    //    c.setEmailSignature(data.c_str());
    //}
    else {
        LOG.debug("Configuration parameter '%ls' not recognized.", item.getKey());
        ret = 1;
    }

    return ret;
}

⌨️ 快捷键说明

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