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

📄 windowssyncsource.cpp

📁 funambol windows mobile plugin source code, the source code is taken from the funambol site
💻 CPP
字号:
/*
 * 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 General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY, TITLE, NONINFRINGEMENT 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
 */
#include "pim/WindowsSyncSource.h"
#include "spds/SyncItemStatus.h"
#include "base/util/utils.h"
#include "base/Log.h"
#include "pim/POOMClient2Server.h"
#include "pim/POOMServer2Client.h"
#include "spdm/errors.h"
#include "notify/timed_msgbox.h"
#include "events/SyncSourceListenerClient.h"
#include "OutOfMemoryException.h"


#define ASK_REFRESH_PIM_MESSAGE TEXT("All the local %s will be deleted before adding the server items. Are you sure you want to continue?")
#define ASK_REFRESH_PIM_TIMEOUT 10

WindowsSyncSource::WindowsSyncSource(const wchar_t* name, SyncSourceConfig* sc) : 
                                     SyncSource(name, sc) {
    c = NULL;
    setPath(TEXT("\\"));
    syncItem = NULL;
    itemError = FALSE;
    friendlyName[0] = 0;  // char 64
    newFromServer = 0;
    updatedFromServer = 0;
    deletedFromServer = 0;
    mimeType = "";
    isSif = false;
    oid_hash.clear();
}

WindowsSyncSource::~WindowsSyncSource() {
    if (c) {
        delete c;
        c = NULL;
    }
    if (syncItem) {
        syncItem = NULL;
    }
    oid_hash.clear();
}

void WindowsSyncSource::setType(SourceType t) {
    type = t;
}

SourceType WindowsSyncSource::getType() {
    return type;
}

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

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

void WindowsSyncSource::setPath(const wchar_t* p) {
    if (p == NULL) {
        path = wstrdup(TEXT(""));
    }
    else {
        path = wstrdup(p);
    }

}

wchar_t* WindowsSyncSource::getPath() {
    return path;
}

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

SyncItem* WindowsSyncSource::getFirstItem() {

    if (syncItem) {
        syncItem = NULL;
    }

    if (c == NULL) {
        c = new Container();
    }    

    
    setAllItemsPOOM(c, getType(), getPath()); // olFolderContact = 10

    LOG.info("The client number of %s to sync are %i", friendlyName, c->getAllItemsSize());

    // send nr of items message to UI
    HWND wnd = HwndFunctions::getWindowHandle();
    if(wnd)
        SendMessage(wnd, ID_MYMSG_TOTAL_ITEMS,(WPARAM) -1, c->getAllItemsSize());


    if (c->getAllItemsSize() == 0) {
        return NULL;
    }
    
    isSif = isSIF(getMimeType());

    ArrayList* list = c->getAllItems();
    syncItem = (SyncItem*)((SyncItem*)list->front())->clone();
    fillSyncItem(syncItem, getType(), isSif, oid_hash);
    return syncItem;
}

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

SyncItem* WindowsSyncSource::getNextItem() {

    if (syncItem) {
        syncItem = NULL;
    }

    if (c == NULL){
        LOG.error("getNextItem: container is null.");
        return NULL;
    }

    if (c->getAllItemsSize() == 0){
        LOG.error("getNextItem: container is null.");
        return NULL;
    }

    SyncItem *next = (SyncItem*)(c->getAllItems()->next());
    if (next) {
        syncItem = (SyncItem*)next->clone();
        fillSyncItem(syncItem, getType(), isSif, oid_hash);
    }
    return syncItem;
}

/*
* This method is always called by the sync manager. So the setModifiedItemsPOOM is always called
*/

SyncItem* WindowsSyncSource::getFirstNewItem() {

    if (syncItem) {
        syncItem = NULL;
    }

    if (c == NULL) {
        c = new Container();
    }
    
    isSif = isSIF(getMimeType());

    setModifiedItemsPOOM(c, getType(), getPath(), isSif, oid_hash); // olFolderContact = 10

    //Log the number of item to sync from client
    LOG.info("The client number of new %s to sync are %i", friendlyName, c->getNewItemsSize());
    LOG.info("The client number of updated %s to sync are %i", friendlyName, c->getUpdatedItemsSize());
    LOG.info("The client number of deleted %s to sync are %i", friendlyName, c->getDeletedItemsSize());

    if (c->getNewItemsSize() == 0) {
        return NULL;
    }

    // send nr of items message to UI
    HWND wnd = HwndFunctions::getWindowHandle();
    if(wnd) {
         SendMessage(wnd, ID_MYMSG_TOTAL_ITEMS,(WPARAM) -1,
                     c->getNewItemsSize()+
                     c->getUpdatedItemsSize()+
                     c->getDeletedItemsSize());
     }

    ArrayList* list = c->getNewItems();
    return (SyncItem*)list->front()->clone();

}

SyncItem* WindowsSyncSource::getFirstItemKey() {

    int deleted = deleteAllItems(getType());

    // reset the deleted item number.
    deletedFromServer = 0;

    if (c) { delete c; c = NULL; }
    return NULL;
}

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

SyncItem* WindowsSyncSource::getNextNewItem() {

    if (syncItem) {
        //delete syncItem; syncItem = NULL;
        syncItem = NULL;
    }

    if (c == NULL) {
        LOG.error("getNextNewItem: container is null.");
        return NULL;
    }

    SyncItem *next = (SyncItem*)c->getNewItems()->next();
    if (next) {
        syncItem = (SyncItem*)next->clone();
    }

    return syncItem;
}

SyncItem* WindowsSyncSource::getFirstUpdatedItem() {

    if (syncItem) {
        syncItem = NULL;
    }

    if (c == NULL) {
        c = new Container();
    }

    if (c->getUpdatedItemsSize() == 0) {
        return NULL;
    }

    ArrayList* list = c->getUpdatedItems();
    return (SyncItem*)list->front()->clone();

}

SyncItem* WindowsSyncSource::getNextUpdatedItem() {

    if (syncItem) {
        syncItem = NULL;
    }

    if (c == NULL) {
        LOG.error("getNextUpdatedItem: container is null.");
        return NULL;
    }

    SyncItem *next = (SyncItem*)c->getUpdatedItems()->next();
    if (next) {
        syncItem = (SyncItem*)next->clone();
    }
    return syncItem;
}

SyncItem* WindowsSyncSource::getFirstDeletedItem() {

    if (c == NULL) {
        c = new Container();
    }

    if (c->getDeletedItemsSize() == 0) {
        return NULL;
    }
    ArrayList* list = c->getDeletedItems();

    return (SyncItem*)list->front()->clone();
}

SyncItem* WindowsSyncSource::getNextDeletedItem() {

    if (syncItem) {
        syncItem = NULL;
    }

    if (c == NULL) {
        LOG.error("getNextDeletedItem: container is null.");
        return NULL;
    }

    SyncItem *next = (SyncItem*)c->getDeletedItems()->next();
    if (next) {
        syncItem = (SyncItem*)next->clone();
    }
    return syncItem;
}

void WindowsSyncSource::setItemStatus(const wchar_t* key, int status) {
    LOG.debug("key: %ls, status: %i", key, status);
    if (status > 299 && status != 418 && status != 419)
        itemError = TRUE;
    else
        itemError = FALSE;
}

int WindowsSyncSource::addItem(SyncItem& item) {

    int ret = STC_COMMAND_FAILED; // command Failed;
    long oid = 0;
    if (!isMemoryAvailable()) {
        LOG.error(LOW_DEVICE_MEMORY);
        throw OutOfMemoryException(0, -50);  //defined in startsync //return ret;
    }
    
    isSif = isSIF(item.getDataType());
    long h = manageNewItems(&item, getType(), &oid, getPath(), isSif, oid_hash);

    wchar_t tmp[256];
    if (h == 0) {
        ret = STC_ITEM_ADDED;
        LOG.debug("Added item: %ls LUID: %i", item.getKey(), oid);
        wsprintf(tmp, TEXT("%i"), oid);
        item.setKey(tmp);

        newFromServer++;
    }
    return ret;
}

int WindowsSyncSource::updateItem(SyncItem& item) {

    int ret = STC_COMMAND_FAILED; // command Failed;

    if (!isMemoryAvailable()) {
        LOG.error(LOW_DEVICE_MEMORY);
        throw OutOfMemoryException(0, -50);    //defined in startsync //return ret;
    }
    isSif = isSIF(item.getDataType());
    long h = manageUpdatedItems(&item, getType(), getPath(), isSif, oid_hash);

    if (h == 0) {
        ret = STC_OK;
        LOG.debug("updated item: %ls", item.getKey());

        updatedFromServer++;
    }
    return ret;
}

int WindowsSyncSource::deleteItem(SyncItem& item) {
    int ret = STC_COMMAND_FAILED; // command Failed;
    long h = manageDeletedItems(&item, getType(), oid_hash);

    if (h == 0) {
        ret = STC_OK;
        LOG.debug("deleted item: %ls", item.getKey());

        deletedFromServer++;
    }
    return ret;
}

int WindowsSyncSource::beginSync() {
    /*
    OL_CONTACTS     = 10,
    OL_CALENDAR     = 9,
    OL_TASK         = 13,
    OL_BRIEFCASE    = 600,
    OL_NOTES        = 601,
    OL_FAVORITES    = 602
    */

    if (getType() == OL_CONTACTS)
    {
        strcpy(friendlyName, "contacts");
    }
    else if (getType() == OL_CALENDAR)
    {
        strcpy(friendlyName, "calendars");
        normalizeAllExceptions();
    }
    else if (getType() == OL_TASK)
    {
        strcpy(friendlyName, "tasks");
    }

    // counter to get the number of items from server
    newFromServer = 0;
    updatedFromServer = 0;
    deletedFromServer = 0;

    return 0;
}

int WindowsSyncSource::endSync() {

    writeCurrentItems(getType(), getPath(), oid_hash);

    //
    //
    // from 6.0: dispose the outlook application
    disposeOutlookApp();

    if (TRUE == itemError) {
        return ERR_ITEM_ERROR;
    }
    else {
        //Log the number of item synced from server
        LOG.info("The server number of new %s synced are %i", friendlyName, newFromServer);
        LOG.info("The server number of updated %s synced are %i", friendlyName, updatedFromServer);
        LOG.info("The server number of deleted %s synced are %i", friendlyName, deletedFromServer);

        return 0;
    }
}

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

⌨️ 快捷键说明

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