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

📄 syncmlbuilder.cpp

📁 funambol windows mobile plugin source code, the source code is taken from the funambol site
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
 * 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 <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"


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);

    Status* s = new Status(commandID, itow(msgRef), "0", SYNC_HDR, targetRefs, sourceRefs, NULL, chal, data, 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);

    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);

    Status* s = new Status(commandID, itow(msgRef), cmdRef->getCmdID(), SYNC, targetRefs, sourceRefs, NULL, NULL, d, 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);

    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 = 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;
            }
        }
    }

    //
    // 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);
        }
    }

    Status* s = new Status(commandID, itow(msgRef), cmdRef->getCmdID(), ALERT, targetRefs, sourceRefs, NULL, NULL, d, items);

    // 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);

    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>
              <DevInf>...</DevInf>
            </Data>
          </Item>
          </Result>
        */

        msgRefStr = itow(msgRef);
        ArrayList empty;
        res = new Results(&commandID,
                          msgRefStr,
                          cmd->getCmdID()->getCmdID(),
                          &meta,
                          &empty,
                          &empty,
                          &items);

⌨️ 快捷键说明

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