📄 fileobjectsyncsource.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/FileObjectSyncSource.h"
#include "spds/SyncItemStatus.h"
#include "base/util/utils.h"
#include "base/Log.h"
#include "pim/FILEClient2Server.h"
#include "pim/FILEServer2Client.h"
#include "pim/FILEFileManagement.h"
#include "base/startcmd.h"
#include "Winbase.h"
#include "pim/SettingFunctions.h"
#include "spds/FileData.h"
#include "notify/timed_msgbox.h"
#include "HwndFunctions.h"
#include "OutOfMemoryException.h"
static int call;
static int cnew;
static int ckey;
static int cupdated;
static int cdeleted;
#define ASK_REFRESH_FILE_MESSAGE TEXT("All the local %s will be deleted before adding the server items. Are you sure you want to continue?")
#define ASK_REFRESH_FILE_TIMEOUT 10
#define BACKUP_FILE TEXT("FunambolBackup")
FileObjectSyncSource::FileObjectSyncSource(const wchar_t* name, SyncSourceConfig* sc) : SyncSource(name, sc) {
c = NULL;
path = NULL;
dir = NULL;
setPath(TEXT("\\"));
setDir(TEXT("\\"));
friendlyName[0] = 0; // char 64
newFromServer = 0;
updatedFromServer = 0;
deletedFromServer = 0;
}
FileObjectSyncSource::~FileObjectSyncSource() {
if (c)
delete c;
if (path)
delete [] path;
if(dir)
delete [] dir;
}
void FileObjectSyncSource::setType(SourceType t) {
type = t;
}
SourceType FileObjectSyncSource::getType() {
return type;
}
void FileObjectSyncSource::setPath(const wchar_t* p) {
if (path)
delete [] path;
path = (p) ? wstrdup(p) : wstrdup(TEXT("\\"));
}
const wchar_t* FileObjectSyncSource::getPath() {
return path;
}
void FileObjectSyncSource::setDir(const wchar_t* p) {
if (dir)
delete [] dir;
dir = (p) ? wstrdup(p) : wstrdup(TEXT("\\"));
}
const wchar_t* FileObjectSyncSource::getDir() {
return dir;
}
/*
* Return the first SyncItem of all. It is used in case of slow or refresh sync
* and retrieve the entire data source content.
*/
SyncItem* FileObjectSyncSource::getFirstItem() {
if (c == NULL) {
c = new Container();
}
setAllItemsFILE(c, getType(), TEXT(""), getDir()); //
LOG.info("The client number of %s to sync are %i", friendlyName, c->getAllItemsSize());
// send nr of items to sync to UI
HWND wnd = HwndFunctions::getWindowHandle();
if(wnd)
SendMessage(wnd, ID_MYMSG_TOTAL_ITEMS, (WPARAM) -1,(LPARAM)c->getAllItemsSize());
if (c->getAllItemsSize() == 0) {
return NULL;
}
call = 0;
ArrayList* list = c->getAllItems();
return (SyncItem*)list->get(call)->clone();
}
/*
* Return the next SyncItem of all. It is used in case of slow or refresh sync
* and retrieve the entire data source content.
*/
SyncItem* FileObjectSyncSource::getNextItem() {
if (c == NULL)
return NULL;
if (c->getAllItemsSize() == 0)
return NULL;
call++;
if (call == c->getAllItemsSize()) {
return NULL;
}
return (SyncItem*)c->getAllItems()->get(call)->clone();
}
SyncItem* FileObjectSyncSource::getFirstItemKey() {
if (c == NULL) {
c = new Container();
}
ArrayList* list = NULL;
setAllItemsFILEKey(c, getType(), TEXT(""), getDir()); //
if (c->getAllItemsSize() == 0) {
goto finally;
}
ckey = 0;
list = c->getAllItems();
if (list && list->size() > 0) {
for (ckey = 0; ckey < list->size(); ckey++) {
deleteItem(*(SyncItem*)list->get(ckey));
}
}
deletedFromServer = 0;
finally:
if (c) { delete c; c = NULL; }
return NULL;
}
SyncItem* FileObjectSyncSource::getNextItemKey() {
return NULL;
}
/*
* This method is always called by the sync manager. So the setModifiedItemsPOOM is always called
*/
SyncItem* FileObjectSyncSource::getFirstNewItem() {
if (c == NULL) {
c = new Container();
}
setModifiedItemsFILE(c, getType(), getPath(), getDir() );
//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());
// 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());
if (c->getNewItemsSize() == 0) {
return NULL;
}
cnew = 0;
ArrayList* list = c->getNewItems();
return (SyncItem*)list->get(cnew)->clone();
}
SyncItem* FileObjectSyncSource::getNextNewItem() {
if (c == NULL)
return NULL;
cnew++;
if (cnew == c->getNewItemsSize()) {
return NULL;
}
return (SyncItem*)c->getNewItems()->get(cnew)->clone();
}
SyncItem* FileObjectSyncSource::getFirstUpdatedItem() {
if (c == NULL) {
c = new Container();
}
if (c->getUpdatedItemsSize() == 0) {
return NULL;
}
cupdated = 0;
ArrayList* list = c->getUpdatedItems();
return (SyncItem*)list->get(cupdated)->clone();
}
SyncItem* FileObjectSyncSource::getNextUpdatedItem() {
if (c == NULL)
return NULL;
cupdated++;
if (cupdated == c->getUpdatedItemsSize()) {
return NULL;
}
return (SyncItem*)c->getUpdatedItems()->get(cupdated)->clone();
}
SyncItem* FileObjectSyncSource::getFirstDeletedItem() {
if (c == NULL) {
c = new Container();
}
if (c->getDeletedItemsSize() == 0) {
return NULL;
}
cdeleted = 0;
ArrayList* list = c->getDeletedItems();
return (SyncItem*)list->get(cdeleted)->clone();
}
SyncItem* FileObjectSyncSource::getNextDeletedItem() {
if (c == NULL)
return NULL;
// TODO: cupdated == c->getDeletedItemsSize()
// here it should compare with cdeleted
cdeleted++;
if (cdeleted == c->getDeletedItemsSize()) {
return NULL;
}
return (SyncItem*)c->getDeletedItems()->get(cdeleted)->clone();
}
void FileObjectSyncSource::setItemStatus(const wchar_t* key, int status) {
LOG.debug("key: %S, status: %i", key, status);
}
int FileObjectSyncSource::addItem(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;
}
FileData file;
char* data = NULL;
wchar_t* oid = NULL;
int size = 0;
int res = 0;
size = item.getDataSize();
res = file.parse(item.getData(), size);
const wchar_t *encod = file.getEnc();
// TODO bug #304192
if (res == -1)// the file was not sent by client as FileObject version 1.2
{
oid = FILEmanageNewItems(&item, getType(), getPath(), getDir());
}
else
if (wcslen(encod) > 0)
{
item.setKey(file.getName());
item.setData(file.getBody(), file.getSize());
oid = FILEmanageNewItems(&item, getType(), getPath(), getDir());
}
if (oid != NULL) {
ret = STC_ITEM_ADDED;
LOG.debug("Added item: %S LUID: %S", item.getKey(), oid);
item.setKey(oid);
delete [] oid; oid = NULL;
newFromServer++;
}
return ret;
}
int FileObjectSyncSource::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;
}
FileData file;
char* data = NULL;
long h = 0;
wchar_t* encod = NULL;
int size = 0;
int res = 0;
size = item.getDataSize();
res = file.parse(item.getData(), size);
encod = (wchar_t*)file.getEnc();
if (res == -1)// the file was not sent by client as FileObject version 1.2
{
h = FILEmanageUpdatedItems(&item, getType(), getPath(), getDir());
}
else
if (wcslen(encod) > 0)
{
item.setData(file.getBody(), file.getSize());
h = FILEmanageUpdatedItems(&item, getType(), getPath(), getDir());
}
if (h == 0) {
ret = STC_OK;
LOG.debug("updated item: %S", item.getKey());
updatedFromServer++;
}
return ret;
}
int FileObjectSyncSource::deleteItem(SyncItem& item) {
int ret = STC_COMMAND_FAILED; // command Failed;
long h = FILEmanageDeletedItems(&item, getType(), getPath(), getDir());
if (h == 0) {
ret = STC_OK;
LOG.debug("deleted item: %S", item.getKey());
deletedFromServer++;
}
return ret;
}
int FileObjectSyncSource::beginSync() {
/*
OL_CONTACTS = 10,
OL_CALENDAR = 9,
OL_TASK = 13,
OL_BRIEFCASE = 600,
OL_NOTES = 601,
OL_FAVORITES = 60
*/
if (getType() == OL_NOTES){
strcpy(friendlyName, "notes");
}
else if (getType() == OL_BRIEFCASE){
strcpy(friendlyName, "briefcase");
};
// counter to get the number of items from server
newFromServer = 0;
updatedFromServer = 0;
deletedFromServer = 0;
return 0;
}
int FileObjectSyncSource::endSync() {
writeCurrentFileItems(getType(), getPath(), getDir());
//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 FileObjectSyncSource::assign(FileObjectSyncSource& s) {
setSyncMode(s.getSyncMode());
setLastSync(s.getLastSync());
setNextSync(s.getNextSync());
setLastAnchor(s.getLastAnchor());
setNextAnchor(s.getNextAnchor());
setFilter(s.getFilter());
setPath(getPath());
setType(getType());
setDir(getDir());
setReport(s.getReport());
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -