s4jproxy.cpp

来自「funambol window mobile客户端源代码」· C++ 代码 · 共 303 行

CPP
303
字号
/*
 * 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 "s4jproxy.h"
#include "client/SyncClient.h"
#include "base/startcmd.h"
#include "base/util/utils.h"
#include "spdm/DMTree.h"
#include "spdm/DMTreefactory.h"

#include "pim/maincpp.h"
#include "pim/SettingFunctions.h"
#include "notify/timed_msgbox.h"
#include "notify/util.h"
#include "processUtils.h"



/**
* Singleton instance of the object
*/
S4JProxy * S4JProxy::instance = 0;

//-------------------------------------------------------------- Static functions
/**
* Return the name of the source in the device starting from the name
* sent by the server
*/
wchar_t *getSSourceName(const char *root, const char *uri)
{
    int namelen = strlen(root) + strlen(CONTEXT_SPDS_SOURCES) + 5;
    int i = 0;
    char *nodeName = new char[namelen];
    wchar_t *ret = NULL;

    if(!nodeName)
        return NULL;

    sprintf(nodeName, "%s/%s", root, CONTEXT_SPDS_SOURCES);

    DMTree* dmt = DMTreeFactory::getDMTree(root);

    ManagementNode *node = dmt->readManagementNode(nodeName);
    if ( ! node ) {
        //lastErrorCode = ERR_INVALID_CONTEXT;
        //sprintf(lastErrorMsg, ERRMSG_INVALID_CONTEXT, nodeName);
        setErrorF(ERR_INVALID_CONTEXT, ERRMSG_INVALID_CONTEXT, nodeName);
        LOG.error("getSourceName: error: %s", getLastErrorMsg());
        goto finally;
    }
    for(i=0; i<node->getChildrenMaxCount(); i++) {
        ManagementNode *src = node->getChild(i);
        char *tmp = src->readPropertyValue(PROPERTY_SOURCE_URI);
        //LOG.debug("i:%d uri:%s tmp:%s", i, uri, tmp);
        if (strcmp(uri, tmp) == 0) {
            char *val = src->readPropertyValue(PROPERTY_SOURCE_NAME);
            ret = toWideChar(val);
            delete [] val;
            break;
        }
        delete [] tmp;
    }

    /*
    * If the client has modified the source name, in the push the server continue
    * to send i.e. scard for the contact, scal for the calendar and stask for the task.
    * mail for the mail. So if the previous loop doesn't find anything, try in this way
    */
    if (!ret) {
        if (!strcmp(uri, "scard") || !strcmp(uri, "card")) {
            ret = toWideChar("contact");  
        } else if (!strcmp(uri, "scal")  || !strcmp(uri, "cal") || !strcmp(uri, "event")) {
            ret = toWideChar("calendar");  
        } else if (!strcmp(uri, "stask") || !strcmp(uri, "cal") || !strcmp(uri, "task") ) {
            ret = toWideChar("task");  
        } else if (!strcmp(uri, "mail") ) {
            ret = toWideChar("mail");  
        }
    }

finally:
    if (dmt) {
        delete dmt;
    }
    if (node) {
        delete node;
    }
    return ret;
}

/**
* Print the notification message on the log file
*/
static void PrintMessage(SyncNotification& np)
{
    LOG.debug("Version:\t%d", np.getVersion());
    LOG.debug("UIMode:\t%d", np.getUIMode());
    LOG.debug("Initiator:\t%s", (np.getInitiator() == UserInitiated)? "UserInitiated" : "ServerInitiated");
    LOG.debug("Session ID:\t%d (%x)", np.getSessionId(), np.getSessionId() );
    LOG.debug("Server ID:\t%s", np.getServerId() );
    LOG.debug("NumSyncs:\t%d ", np.getNumSyncs() );

    for (int i=0; i<np.getNumSyncs(); i++){
        SyncAlert *sync;
        sync=np.getSyncAlert(i);
        if (!sync){
            LOG.debug("Error on sync #%d", i);
        }
        LOG.debug("Sync type:\t%d", sync->getSyncType() );
        LOG.debug("Content type:\t%d", sync->getContentType() );
        LOG.debug("Server URI:\t%s", sync->getServerURI() );
    }
}

/**
* Get the instance of the singleton object
*/
S4JProxy* S4JProxy::getInstance()
{
    if( !instance )
        instance=new S4JProxy();

    return instance;
}
/**
* Dispose the singleton object
*/
void S4JProxy::dispose()
{
    if( instance ) {
        delete instance;
        instance = 0;
    }
}

S4JProxy::S4JProxy()
{
    errcode=SNErr_Undefined;
    syncMessage=0;
}

S4JProxy::~S4JProxy()
{
    if(syncMessage)
        delete [] syncMessage;
}

S4JProxy::RetCode S4JProxy::parsePkg0(const char *msg, int msglen)
{
    __try {
        LOG.debug("calling parse");
        errcode=np.parse(msg,msglen);
        LOG.debug("parse returned: %d", errcode);
        PrintMessage(np);
    }
    __except (EXCEPTION_EXECUTE_HANDLER) {
        LOG.error("Exception in message parser.");
        return Error;
    }

    switch (errcode) {
        case SNErr_Ok :
            //PrintMessage(np);
            return Ok;                  // OK
        case SNErr_Incomplete :
            return Continue;            // Get more bytes
        default:
            return Error;               // Error
    }
}

const char *S4JProxy::getResponse()
{
    switch(errcode){
        case SNErr_Ok:
            return RESP_OK;
        case SNErr_BadReq:
            return RESP_BADREQ;
        case SNErr_InvSync:
            return RESP_INVSYNC;
        case SNErr_Incomplete:
            return "400 - Message incomplete\n";
        case SNErr_NullValue:
            return RESP_NULLP;
        default:
            return "500 - Internal error\n";
    }
}

int S4JProxy::sync(bool debug)
{
#ifdef DEBUG
    // XXX Debug code
    if (debug){
        //- Generated with xxd --------------------------------------------------
        unsigned char message_bin[] = {
            0x1d, 0xca, 0x85, 0xbf, 0x59, 0x2e, 0xff, 0x5d, 0x53, 0xf6, 0xee, 0xff,
            0x57, 0x3d, 0xc1, 0x36, 0x03, 0x18, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0f,
            0x73, 0x79, 0x6e, 0x63, 0x2e, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2e,
            0x63, 0x6f, 0x6d, 0x10, 0x60, 0x00, 0x00, 0x07, 0x07, 0x63, 0x6f, 0x6e,
            0x74, 0x61, 0x63, 0x74
        };
        unsigned int message_bin_len = 52;
        //-----------------------------------------------------------------------
        errcode=np.parse((char *)message_bin, message_bin_len);
        if(errcode){
            fprintf(stderr,"Error number: %d\n", errcode);
            return errcode;
        }
    }
#endif

    // No sync info from server?
    if (np.getNumSyncs() == 0) {
        LOG.debug("No server alert info.");
        return -1;
    }

    wchar_t cmdline[MAX_PATH] = TEXT("tcpip ");

    int count = 0;
    for (int i=0; i<np.getNumSyncs(); i++){
        SyncAlert *sync;
        wchar_t *srcname;

        sync = np.getSyncAlert(i);

        if( !sync ){
            LOG.error("Error on sync #%d", i);
            return -1;
        }

        srcname = getSSourceName(APPLICATION_URI, sync->getServerURI());
        if (srcname) {
            // Add separator, if not first
            if (count>0)
                wcscat( cmdline, TEXT(",") );
            wcscat( cmdline, srcname );
            count ++;
            delete [] srcname;
        }
    }
    if (count > 0) {
        if (askConfirm()) {
            
            LOG.info("Starting: %ls %ls", SYNCAPP, cmdline);
            //if(startcmd(application.c_str(), cmdline)){
            if(startProgram(SYNCAPP, cmdline)){
                LOG.debug("Sync process started.");
            }
            else {
                LOG.error("Error starting sync process.");
                return -1;
            }
        }
    } else {
        LOG.error("Notification: no source found from server notification request.");
    }
    return 0;
}


const wchar_t *S4JProxy::getSyncMessage() {
    return syncMessage;
}

bool S4JProxy::hasErrors() {
    return errcode != SNErr_Ok;
}


⌨️ 快捷键说明

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