📄 s4jproxy.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 "s4jproxy.h"
#include "logutils.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);
DebugPrint(TEXT("getSourceName: error: %s"), lastErrorMsg);
goto finally;
}
for(i=0; i<node->getChildrenMaxCount(); i++) {
ManagementNode *src = node->getChild(i);
char *tmp = src->readPropertyValue(PROPERTY_SOURCE_URI);
//DebugPrint(TEXT("i:%d uri:%s tmp:%s\n"), 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)
{
DebugPrint(L"Version:\t%d\n", np.getVersion());
DebugPrint(L"UIMode:\t%d\n", np.getUIMode());
DebugPrint(L"Initiator:\t%S\n",
( np.getInitiator() == UserInitiated )
? "UserInitiated" : "ServerInitiated" );
DebugPrint(L"Session ID:\t%d (%x)\n",
np.getSessionId(), np.getSessionId() );
DebugPrint(L"Server ID:\t%S\n", np.getServerId() );
DebugPrint(L"NumSyncs:\t%d\n", np.getNumSyncs() );
for (int i=0; i<np.getNumSyncs(); i++){
SyncAlert *sync;
sync=np.getSyncAlert(i);
if( ! sync ){
DebugPrint(L"Error on sync #%d\n", i);
}
DebugPrint(L"Sync type:\t%d\n", sync->getSyncType() );
DebugPrint(L"Content type:\t%d\n", sync->getContentType() );
DebugPrint(L"Server URI:\t%S\n", 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 {
DebugPrint(TEXT("calling parse\n"));
errcode=np.parse(msg,msglen);
DebugPrint(TEXT("parse returned: %d\n"), errcode);
PrintMessage(np);
}
__except (EXCEPTION_EXECUTE_HANDLER) {
DebugPrint(TEXT("Exception in message parser.\n\n"));
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) {
DebugPrint(TEXT("No server alert info.\n"));
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 ){
DebugPrint(TEXT("Error on sync #%d\n"), 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(ASK_TCP_IP)) {
wstring application = SYNCAPP;
DebugPrint( TEXT("Starting: %s %s"), SYNCAPP, cmdline );
//if(startcmd(application.c_str(), cmdline)){
if(startProgram(application.c_str(), cmdline)){
DebugPrint( TEXT("Sync process started.\n") );
}
else {
DebugPrint( TEXT("Error starting sync process.\n") );
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -