syncmlbuilder.cpp
来自「funambol window mobile客户端源代码」· C++ 代码 · 共 970 行 · 第 1/2 页
CPP
970 行
/*
* 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 <string.h>
#include "base/fscapi.h"
#include "base/base64.h"
#include "base/util/utils.h"
#include "base/util/StringBuffer.h"
#include "filter/ClauseUtil.h"
#include "spds/constants.h"
#include "spds/DataTransformerFactory.h"
#include "spds/SyncItem.h"
#include "spds/SyncMLBuilder.h"
#include "event/FireEvent.h"
#include "base/globalsdef.h"
USE_NAMESPACE
SyncMLBuilder::SyncMLBuilder() {
initialize();
set(NULL, NULL);
}
SyncMLBuilder::~SyncMLBuilder() {
safeDelete(&target );
safeDelete(&device );
}
SyncMLBuilder::SyncMLBuilder(char* t, char* d) {
initialize();
set(t, d);
}
void SyncMLBuilder::set(const char*t, const char*d) {
target = stringdup(t);
device = stringdup(d);
}
void SyncMLBuilder::initialize() {
sessionID = (unsigned long)time(NULL);
msgRef = 0 ;
msgID = 0 ;
cmdID = 0 ;
}
void SyncMLBuilder::resetCommandID() {
cmdID = 0;
}
void SyncMLBuilder::increaseMsgRef() {
msgRef++;
}
void SyncMLBuilder::resetMsgRef() {
msgRef = 0;
}
void SyncMLBuilder::addItemStatus(ArrayList* previousStatus, Status* status) {
if (previousStatus->size() == 0) {
previousStatus->add(*status);
return;
}
bool found = false;
Status* s = NULL;
if (status == NULL)
return;
ArrayList* list = new ArrayList();
for (int i = 0; i < previousStatus->size(); i++) {
s = (Status*)previousStatus->get(i);
if ((strcmp(s->getCmd(), status->getCmd()) == 0) &&
(strcmp(s->getData()->getData(), status->getData()->getData()) == 0) &&
(strcmp(s->getCmdRef(), status->getCmdRef()) == 0) ) {
list = s->getItems();
for (int j = 0; j < status->getItems()->size(); j++) {
list->add(*((Item*)(status->getItems())->get(j)));
found = true;
}
}
}
if (!found)
previousStatus->add(*status);
}
/*
* Return the status of the items sent by server. Used to create the status to respond
* after a add, replace or delete command
*/
Status* SyncMLBuilder::prepareItemStatus(const char* COMMAND,
const char* key,
const char* cmdRef,
int code) {
/*
<Status> CmdID* cmdID ,
<CmdID>3</CmdID> char* msgRef ,
<MsgRef>2</MsgRef> char* cmdRef ,
<CmdRef>4</CmdRef> char* cmd ,
<Cmd>Replace</Cmd> ArrayList* targetRefs,
<Data>201</Data> ArrayList* sourceRefs,
<Item> Cred* cred ,
<Source> Chal* chal ,
<LocURI>item0</LocURI> Data* data ,
</Source> ArrayList* items );
</Item>
<Item>
<Source>
<LocURI>item1</LocURI>
</Source>
</Item>
</Status>
*/
++cmdID;
char* cmdid = itow(cmdID);
CmdID* commandID = new CmdID(cmdid);
ArrayList* empty = new ArrayList();
Data* data = new Data(code);
ArrayList* list = new ArrayList();
Source* sou = new Source(key);
Item* item = new Item(NULL, sou, NULL, NULL, false);
list->add(*item);
char *mRef = itow(msgRef);
Status* s = new Status(commandID, mRef, cmdRef, COMMAND, empty, empty, NULL, NULL, data, list);
delete [] mRef;
safeDelete(&cmdid);
deleteCmdID(&commandID);
deleteData(&data);
deleteSource(&sou);
deleteItem(&item);
deleteArrayList(&empty);
deleteArrayList(&list);
if (list) { delete list; list = NULL; }
if (empty) { delete empty; empty = NULL; }
return s;
}
/*
* Return the status of the server authentication process
*/
Status* SyncMLBuilder::prepareSyncHdrStatus(Chal*chal, int d) {
++cmdID;
char* cmdid = itow(cmdID);
CmdID* commandID = new CmdID(cmdid);
ArrayList* targetRefs = new ArrayList();
ArrayList* sourceRefs = new ArrayList();
TargetRef* tar = new TargetRef(target);
SourceRef* sou = new SourceRef(device);
Data* data = new Data(d);
targetRefs->add(*tar);
sourceRefs->add(*sou);
char* wmsgRef = itow(msgRef);
Status* s = new Status(commandID, wmsgRef, "0", SYNC_HDR, targetRefs, sourceRefs, NULL, chal, data, NULL);
if (wmsgRef){
delete [] wmsgRef;
wmsgRef = NULL;
}
// Fire Sync Status Event: syncHdr status from client
fireSyncStatusEvent(SYNC_HDR, s->getStatusCode(), NULL, NULL, NULL , CLIENT_STATUS);
safeDelete(&cmdid);
deleteCmdID(&commandID);
//deleteArrayList(&targetRefs);
//deleteArrayList(&sourceRefs);
deleteTargetRef(&tar);
deleteSourceRef(&sou);
deleteData(&data);
if (targetRefs){
delete targetRefs;
targetRefs = NULL;
}
if (sourceRefs){
delete sourceRefs;
sourceRefs = NULL;
}
return s;
}
Status* SyncMLBuilder::prepareSyncStatus(SyncSource& source, Sync* sync) {
if (sync == NULL)
return NULL;
++cmdID;
char* cmdid = itow(cmdID);
CmdID* commandID = new CmdID(cmdid);
delete [] cmdid; cmdid = NULL;
ArrayList* targetRefs = new ArrayList();
ArrayList* sourceRefs = new ArrayList();
CmdID* cmdRef = sync->getCmdID();
TargetRef* tar = new TargetRef(source.getConfig().getURI());
SourceRef* sou = new SourceRef(_wcc(source.getName()));
targetRefs->add(*tar);
sourceRefs->add(*sou);
Data* d = new Data(200);
char* wmsgRef = itow(msgRef);
Status* s = new Status(commandID, wmsgRef, cmdRef->getCmdID(), SYNC, targetRefs, sourceRefs, NULL, NULL, d, NULL);
if (wmsgRef){
delete [] wmsgRef;
wmsgRef = NULL;
}
// Fire Sync Status Event: sync status from client
fireSyncStatusEvent(SYNC, s->getStatusCode(), source.getConfig().getName(), source.getConfig().getURI(), NULL, CLIENT_STATUS);
deleteCmdID(&commandID);
//deleteArrayList(&targetRefs);
//deleteArrayList(&sourceRefs);
deleteTargetRef(&tar);
deleteSourceRef(&sou);
deleteData(&d);
if (targetRefs) {
delete targetRefs;
targetRefs = NULL;
}
if (sourceRefs) {
delete sourceRefs;
sourceRefs = NULL;
}
return s;
}
/*
* Return the status against an alert command. It could be releated on the authentication process
* or any other else
*/
Status* SyncMLBuilder::prepareAlertStatus(SyncSource& source, ArrayList* alerts, int authStatusCode) {
/*
next = source.getNextAnchor();
<Status>\n
<CmdID>2</CmdID>\n
<MsgRef>1</MsgRef>
<CmdRef>1</CmdRef>
<Cmd>Alert</Cmd>\n
<TargetRef>
sb.append(source.getRemoteURI());
</TargetRef>\n
<SourceRef>
sb.append(source.getName(NULL, -1));
</SourceRef>\n
<Data>200</Data>\n
<Item>\n
<Data>
<Anchor xmlns='syncml:metinf'>
<Next>
sb.append(next);
</Next>
</Anchor>\n
</Data>\n
</Item>\n
</Status>
*/
if (alerts == NULL || alerts->size() == 0)
return NULL;
Alert* a = NULL;
Item* item = NULL;
ArrayList* list = NULL; // new ArrayList();
bool found = false;
for (int i = 0; i < alerts->size(); i++) {
a = (Alert*)alerts->get(i);
list = a->getItems();
if (list->size() == 1) {
Item* it = (Item*)list->get(0);
if (strcmp(it->getTarget()->getLocURI(), _wcc(source.getName())) == 0) {
found = true;
break;
}
}
}
//
// Currently it returns null. It could return a status value if necessary
//
if (!found) {
return NULL;
}
++cmdID;
char* cmdid = itow(cmdID);
CmdID* commandID = new CmdID(cmdid);
delete [] cmdid; cmdid = NULL;
ArrayList* targetRefs = new ArrayList();
ArrayList* sourceRefs = new ArrayList();
TargetRef* tar = new TargetRef(source.getConfig().getURI());
SourceRef* sou = new SourceRef(_wcc(source.getName()));
targetRefs->add(*tar);
sourceRefs->add(*sou);
CmdID* cmdRef = a->getCmdID();
char* next = NULL;
int authStatus = 0;
if (authStatusCode >= 200 && authStatusCode <=299)
authStatus = 200;
else
authStatus = authStatusCode;
Data* d = new Data(authStatus);
ComplexData* data = NULL;
ArrayList* items = new ArrayList();
Anchor* anchor = NULL;
if (authStatusCode < 400) {
list = a->getItems();
if (list->size() > 0) {
for (int i = 0; i < list->size(); i++) {
Item* it = (Item*)list->get(i);
Meta* m = it->getMeta();
if (m) {
anchor = m->getAnchor();
next = stringdup(anchor->getNext());
}
}
anchor = new Anchor(NULL, next);
data = new ComplexData();
data->setAnchor(anchor);
item = new Item(NULL, NULL, NULL, data, false);
items->add(*item);
}
}
char* wmsgRef = itow(msgRef);
Status* s = new Status(commandID, wmsgRef, cmdRef->getCmdID(), ALERT, targetRefs, sourceRefs, NULL, NULL, d, items);
if (wmsgRef){
delete [] wmsgRef;
wmsgRef = NULL;
}
// Fire Sync Status Event: alert status from client
fireSyncStatusEvent(ALERT, s->getStatusCode(), source.getConfig().getName(), source.getConfig().getURI(), NULL , CLIENT_STATUS);
deleteCmdID(&commandID);
//deleteArrayList(&targetRefs);
//deleteArrayList(&sourceRefs);
deleteTargetRef(&tar);
deleteSourceRef(&sou);
deleteItem(&item);
deleteAnchor(&anchor);
deleteComplexData(&data);
deleteData(&d);
safeDel(&next);
if (items) {
delete items;
items = NULL;
}
if (targetRefs) {
delete targetRefs;
targetRefs = NULL;
}
if (sourceRefs) {
delete sourceRefs;
sourceRefs = NULL;
}
return s;
}
/*
* Return the status against an arbitrary command.
*/
Status* SyncMLBuilder::prepareCmdStatus(AbstractCommand &cmd, int status) {
/*
<Status>\n
<CmdID>2</CmdID>\n
<MsgRef>1</MsgRef>
<CmdRef>1</CmdRef>
<Cmd>cmd</Cmd>\n
<Data>status</Data>\n
</Status>
*/
++cmdID;
char* cmdid = itow(cmdID);
CmdID commandID(cmdid);
delete [] cmdid; cmdid = NULL;
Data d(status);
char *msgRefStr = itow(msgRef);
ArrayList empty;
Status* s = new Status(&commandID, msgRefStr, cmd.getCmdID()->getCmdID(), cmd.getName(), &empty, &empty, NULL, NULL, &d, NULL);
// Fire Sync Status Event: status from client
fireSyncStatusEvent(s->getCmd(), s->getStatusCode(), NULL, NULL, NULL , CLIENT_STATUS);
delete [] msgRefStr;
return s;
}
AbstractCommand *SyncMLBuilder::prepareDevInf(AbstractCommand *cmd, DevInf &devInf)
{
AbstractCommand *res = NULL;
char *msgRefStr = NULL;
Source source(DEVINF_URI);
Meta meta;
meta.setType(DEVINF_FORMAT);
// meta.setFormat("xml");
ComplexData complexData;
complexData.setDevInf(&devInf);
Item item(NULL,
&source,
NULL,
&complexData,
false);
++cmdID;
char* cmdid = itow(cmdID);
CmdID commandID(cmdid);
delete [] cmdid; cmdid = NULL;
ArrayList items;
items.add(item);
if (cmd) {
/*
<Result>
<CmdID>2</CmdID>
<MsgRef>1</MsgRef>
<CmdRef>4</CmdRef>
<Meta><Type xmlns='syncml:metinf'>application/vnd.syncml-devinf+xml</Type></Meta>
<Item>
<SourceRef><LocURI>./devinf11</LocURI></SourceRef>
<Data>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?