windowssyncsource.cpp
来自「funambol window mobile客户端源代码」· C++ 代码 · 共 474 行
CPP
474 行
/*
* 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/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"
#include "pim/BaseSyncSource.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, AbstractSyncSourceConfig* sc) :
BaseSyncSource(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 is: %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.info("getNextItem: no items on the device.");
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("New %s from client: %i", friendlyName, c->getNewItemsSize());
LOG.info("Updated %s from client: %i", friendlyName, c->getUpdatedItemsSize());
LOG.info("Deleted %s from client: %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();
syncItem = (SyncItem*)((SyncItem*)list->front())->clone();
fillSyncItem(syncItem, getType(), isSif, oid_hash);
return syncItem;
}
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();
fillSyncItem(syncItem, getType(), isSif, oid_hash);
}
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();
syncItem = (SyncItem*)((SyncItem*)list->front())->clone();
fillSyncItem(syncItem, getType(), isSif, oid_hash);
return syncItem;
}
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();
fillSyncItem(syncItem, getType(), isSif, oid_hash);
}
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;
}
// check for the storage memory...
checkStorageMemory(0);
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;
}
// check for the storage memory...
checkStorageMemory(0);
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
*/
// check for the storage memory...
checkStorageMemory(0);
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;
//}
SyncSourceReport* ssr = getReport();
int newFailed = ssr->getItemReportFailedCount(SERVER, COMMAND_ADD);
int modFailed = ssr->getItemReportFailedCount(SERVER, COMMAND_REPLACE);
int delFailed = ssr->getItemReportFailedCount(SERVER, COMMAND_DELETE);
if ((newFailed > 1) || (modFailed > 1) || (delFailed > 1)) {
ssr->setState(SOURCE_ERROR);
return ERR_ITEM_ERROR;
}
else {
//Log the number of item synced from server
LOG.info("New %s from server: %i", friendlyName, newFromServer);
LOG.info("Updated %s from server: %i", friendlyName, updatedFromServer);
LOG.info("Deleted %s from server: %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 + =
减小字号Ctrl + -
显示快捷键?