/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/
#ifndef _SCTS_COMMAND_ELEMENTS_H
#define _SCTS_COMMAND_ELEMENTS_H
#include "SctsPlatformLib.h"
#include "SctsCommonElements.h"

#ifdef __SCTS_USING_RTK__
#include "smlerr.h"


/* Type Defines */
typedef enum {
    SCTS_ADD=0,
    SCTS_ALERT,
    SCTS_ATOMIC_START,
    SCTS_ATOMIC_END,
    SCTS_COPY,
    SCTS_DELETE,
    SCTS_EXEC,
    SCTS_GET,
    SCTS_MAP,
    SCTS_PUT,
    SCTS_RESULTS,
    SCTS_SEARCH,
    SCTS_SEQUENCE_START,
    SCTS_SEQUENCE_END,
    SCTS_STATUS,
    SCTS_SYNC_START,
    SCTS_SYNC_END,
    SCTS_REPLACE,
    SCTS_HEADER,
	SCTS_BODY_END,
	SCTS_COMMAND_COUNT,
    SCTS_NONECMD=-1
} SctsCommandType;

extern char sctsCommandStrings[SCTS_COMMAND_COUNT][15];

#define SCTSRTKSETCRED(CRED,SCTSCRED)\
	SCTSASSERT(rawData);\
	if (CRED) smlFreeCredPtr(CRED);\
	if (!SCTSCRED)\
	{\
		CRED=0;\
		return;\
	}\
	CRED=SCTSCRED->rawData;

#define SCTSRTKFREEITEMLISTDATA(ITEMLIST)\
	{\
		SmlItemListPtr_t itemList;\
		SCTSASSERT(rawData);\
		itemList=ITEMLIST;\
		if (!itemList) return;\
		for (;itemList;itemList=itemList->next)\
		{\
			if (itemList->item->data) smlFreePcdata(itemList->item->data);\
			if (itemList->item->meta) smlFreePcdata(itemList->item->meta);\
			itemList->item->data=0;\
			itemList->item->meta=0;\
		}\
	}

#define SCTSRTKSETITEMLIST(ITEMLIST,SCTSITEMLIST)\
	SCTSASSERT(rawData);\
	if (ITEMLIST) smlFreeItemList(ITEMLIST);\
	if (!SCTSITEMLIST)\
	{\
		ITEMLIST=0;\
		return;\
	}\
	ITEMLIST=SCTSITEMLIST->rawData;

#define SCTSRTKGETITEMLIST(ITEMLIST,SCTSITEMLIST)\
	SCTSASSERT(rawData);\
	SCTSITEMLIST.rawData=ITEMLIST;\
	return SCTSITEMLIST;

typedef InstanceID_t SctsSessionContext;

/*****************************************************************/
class SctsCommand
{
public:
	int	size;
	int	status;
	bool IsMultipleStatus();
	virtual void MakeStub()=0;  
	virtual bool VerifyDTD(SctsSyncMLProto proto=SYNCML_DM_1_2);
	virtual SctsCommandType GetType()=0;
	virtual SctsPcdata& GetCommandID(SctsPcdata&)=0;
	virtual void Build(SctsSessionContext)=0;
	virtual void BuildClose(SctsSessionContext) {}
	
	/* Destructor */
	virtual ~SctsCommand() {}
};

/*****************************************************************/

class SctsGeneric : public SctsCommand
{
public:
	SmlGenericCmdPtr_t rawData;
	SctsPcdata& GetCommandID(SctsPcdata&);
	void SetCommandID(int);	void MakeStub();
	void SetCred(SctsCred *);
	SctsCred& GetCred(SctsCred&);
	void SetMeta(SctsMeta *);
	SctsMeta& GetMeta(SctsMeta&);
	void SetItemList(SctsItemList *);
	SctsItemList& GetItemList(SctsItemList&);
	bool IsNoResp();
	void IsNoResp(bool);
	
	/* Constructors */
	SctsGeneric(SmlGenericCmdPtr_t);
	SctsGeneric(unsigned int cmdID,SctsItemList& itemList);
	/* Destructor */
	~SctsGeneric();
};

/*****************************************************************/

class SctsAdd : public SctsGeneric
{
public:
	void Build(SctsSessionContext);
	SctsCommandType GetType();
	bool VerifyDTD(SctsSyncMLProto proto=SYNCML_DM_1_2);

	/* Constructor */
	SctsAdd(SmlGenericCmdPtr_t pGeneric) : SctsGeneric(pGeneric) {}
	SctsAdd(unsigned int cmdID,SctsItemList& itemList) : SctsGeneric(cmdID,itemList) {}
};

/*****************************************************************/

class SctsCopy : public SctsGeneric
{
public:
	void Build(SctsSessionContext);
	SctsCommandType GetType();
	bool VerifyDTD(SctsSyncMLProto proto=SYNCML_DM_1_2);

	/* Constructor */
	SctsCopy(SmlGenericCmdPtr_t pGeneric) : SctsGeneric(pGeneric) {}
	SctsCopy(unsigned int cmdID,SctsItemList& itemList) : SctsGeneric(cmdID,itemList) {}
};

/*****************************************************************/

class SctsDelete : public SctsGeneric
{
public:
	void Build(SctsSessionContext);
	SctsCommandType GetType();
	bool VerifyDTD(SctsSyncMLProto proto=SYNCML_DM_1_2);
	void IsSoftDel(bool);
	bool IsSoftDel();
	void IsArchive(bool);
	bool IsArchive();

	/* Constructor */
	SctsDelete(SmlGenericCmdPtr_t pGeneric) : SctsGeneric(pGeneric) {}
	SctsDelete(unsigned int cmdID,SctsItemList& itemList) : SctsGeneric(cmdID,itemList) {}
};

/*****************************************************************/

class SctsReplace : public SctsGeneric
{
/* Implementation */
public:
	void Build(SctsSessionContext);
	SctsCommandType GetType();
	bool VerifyDTD(SctsSyncMLProto proto=SYNCML_DM_1_2);

	/* Constructor */
	SctsReplace(SmlGenericCmdPtr_t pGeneric) : SctsGeneric(pGeneric) {}
	SctsReplace(unsigned int cmdID,SctsItemList& itemList) : SctsGeneric(cmdID,itemList) {}
};

/*****************************************************************/

class SctsAtomic :public SctsCommand
{
public:
	SmlAtomicPtr_t rawData;
	void MakeStub();  
	bool VerifyDTD(SctsSyncMLProto proto=SYNCML_DM_1_2);
	SctsCommandType GetType();
	SctsPcdata& GetCommandID(SctsPcdata&);
	void Build(SctsSessionContext);
	void BuildClose(SctsSessionContext);
	void SetMeta(SctsMeta *);
	SctsMeta& GetMeta(SctsMeta&);
	bool IsNoResp();
	void IsNoResp(bool);

	/* Constructors */
	SctsAtomic(SmlAtomicPtr_t);
	SctsAtomic(unsigned int cmdID,SctsMeta *pMeta);
	/* Destructor */
	~SctsAtomic();
};

/*****************************************************************/

class SctsSequence :public SctsCommand
{
public:
	SmlSequencePtr_t rawData;	
	void MakeStub();  
	bool VerifyDTD(SctsSyncMLProto proto=SYNCML_DM_1_2);
	SctsCommandType GetType();
	SctsPcdata& GetCommandID(SctsPcdata&);
	void Build(SctsSessionContext);
	void BuildClose(SctsSessionContext);
	void SetMeta(SctsMeta *);
	SctsMeta& GetMeta(SctsMeta&);
	bool IsNoResp();
	void IsNoResp(bool);

	/* Constructors */
	SctsSequence(SmlSequencePtr_t);
	SctsSequence(unsigned int cmdID,SctsMeta *pMeta);
	/* Destructor */
	~SctsSequence();
};

/*****************************************************************/

class SctsSync :public SctsCommand
{
public:
	SmlSyncPtr_t rawData;	
	void MakeStub();  
	bool VerifyDTD(SctsSyncMLProto proto=SYNCML_DM_1_2);
	SctsCommandType GetType();
	SctsPcdata& GetCommandID(SctsPcdata&);
	void Build(SctsSessionContext);
	void BuildClose(SctsSessionContext);
	void SetMeta(SctsMeta *);
	SctsMeta& GetMeta(SctsMeta&);
	void SetCred(SctsCred *);
	SctsCred& GetCred(SctsCred&);
	void SetTarget(char *pLocURI,char *pLocName=0);
	SctsTargetSource& GetTarget(SctsTargetSource&);
	void SetSource(char *pLocURI,char *pLocName=0);
	SctsTargetSource& GetSource(SctsTargetSource&);
	/* SyncML 1.1 : N of M */
	void SetNumberOfChanges(int n);
	int GetNumberOfChanges();
	bool IsNoResp();
	void IsNoResp(bool);	

	/* Constructors */
	SctsSync(SmlSyncPtr_t);
	SctsSync(unsigned int cmdID,char *pSourceLocURI,char *pSourceLocName,char *pTargetLocURI,char *pTargetLocName);
	/* Destructor */
	~SctsSync();
};

/*****************************************************************/

class SctsGetPut :public SctsCommand
{
public:
	sml_get_put_s *rawData;
	SctsPcdata& GetCommandID(SctsPcdata&);
	void MakeStub();
	void SetCred(SctsCred *);
	SctsCred& GetCred(SctsCred&);
	void SetMeta(SctsMeta *);
	SctsMeta& GetMeta(SctsMeta&);
	void SetItemList(SctsItemList *);
	SctsItemList& GetItemList(SctsItemList &);
	void SetLang(SctsPcdata *);
	SctsPcdata& GetLang(SctsPcdata&);
	bool IsNoResp();
	void IsNoResp(bool);
	
	/* Constructors */
	SctsGetPut(sml_get_put_s *);
	SctsGetPut(unsigned int cmdID,SctsItemList& itemList);
	/* Destructor */
	~SctsGetPut();
};

/*****************************************************************/

class SctsGet : public SctsGetPut
{
public:
	void Build(SctsSessionContext);
	bool VerifyDTD(SctsSyncMLProto proto=SYNCML_DM_1_2);
	SctsCommandType GetType();
	
	/* Constructors */
	SctsGet(SmlGetPtr_t pGet) : SctsGetPut(pGet) {}
	SctsGet(unsigned int cmdID,SctsItemList& itemList) : SctsGetPut(cmdID,itemList) {}
};

/*****************************************************************/

class SctsPut : public SctsGetPut
{
public:
	void Build(SctsSessionContext);
	bool VerifyDTD(SctsSyncMLProto proto=SYNCML_DM_1_2);
	SctsCommandType GetType();
	
	/* Constructors */
	SctsPut(SmlPutPtr_t pPut) : SctsGetPut(pPut) {}
	SctsPut(unsigned int cmdID,SctsItemList& itemList) : SctsGetPut(cmdID,itemList) {}
};

/*****************************************************************/

class SctsSearch :public SctsCommand
{
public:
	SmlSearchPtr_t rawData;
	void MakeStub();  
	bool VerifyDTD(SctsSyncMLProto proto=SYNCML_DM_1_2);
	SctsCommandType GetType();
	SctsPcdata& GetCommandID(SctsPcdata&);
	void Build(SctsSessionContext);
	void SetMeta(SctsMeta *);
	SctsMeta& GetMeta(SctsMeta&);
	void SetCred(SctsCred *);
	SctsCred& GetCred(SctsCred&);
	void SetTarget(char *pLocURI,char *pLocName=0);
	SctsTargetSource& GetTarget(SctsTargetSource&);
	void SetSourceList(SctsSourceList *);
	SctsSourceList& GetSourceList(SctsSourceList&);
	void SetData(SctsPcdata *);
	SctsPcdata& GetData(SctsPcdata &);
	void SetLang(SctsPcdata *);
	SctsPcdata& GetLang(SctsPcdata &);
	void IsNoResults(bool);
	bool IsNoResults();
	bool IsNoResp();
	void IsNoResp(bool);
		
	/* Constructors */
	SctsSearch(SmlSearchPtr_t);
	SctsSearch(unsigned int cmdID,SctsMeta *pMeta,SctsPcdata *pData);
	/* Destructor */
	~SctsSearch();
};

/*****************************************************************/

class SctsResults :public SctsCommand
{
public:
	SmlResultsPtr_t rawData;	
	void MakeStub();  
	bool VerifyDTD(SctsSyncMLProto proto=SYNCML_DM_1_2);
	SctsCommandType GetType();
	SctsPcdata& GetCommandID(SctsPcdata&);
	void SetCommandID(int);
	void Build(SctsSessionContext);
	void SetMsgRef(int);
	int GetMsgRef();
	void SetCmdRef(SctsPcdata *);
	SctsPcdata& GetCmdRef(SctsPcdata&);
	void SetTargetRef(SctsPcdata *);
	SctsPcdata& GetTargetRef(SctsPcdata&);
	void SetSourceRef(SctsPcdata *);
	SctsPcdata& GetSourceRef(SctsPcdata&);
	void SetMeta(SctsMeta *);
	SctsMeta& GetMeta(SctsMeta&);
	void SetItemList(SctsItemList *);
	SctsItemList& GetItemList(SctsItemList&);
	
	/* Constructors */
	SctsResults(SmlResultsPtr_t);
	SctsResults(unsigned int cmdID,char *pCmdRef,SctsItemList& itemList);
	/* Destructor */
	~SctsResults();
};

/*****************************************************************/

class SctsAlert :public SctsCommand
{
public:
	SmlAlertPtr_t rawData;
	void MakeStub();  
	bool VerifyDTD(SctsSyncMLProto proto=SYNCML_DM_1_2);
	SctsCommandType GetType();
	SctsPcdata& GetCommandID(SctsPcdata&);
	void Build(SctsSessionContext);
	void SetData(SctsPcdata *);
	int GetData();
	void SetCred(SctsCred *);
	SctsCred& GetCred(SctsCred&);
	void SetItemList(SctsItemList *);
	SctsItemList& GetItemList(SctsItemList &);
	bool IsNoResp();
	void IsNoResp(bool);

    /*
     * Change Alert to support Correlator. 
     */
	char * GetCorrelator();
	void SetCorrelator(char *);

	/* Constructors */
	SctsAlert(SmlAlertPtr_t);
	SctsAlert(unsigned int cmdID,SctsItemList& itemList,int alertCode);
    /*
     * Change Alert to support Correlator. 
     */
	SctsAlert(unsigned int cmdID,SctsItemList& itemList,int alertCode,char *correlator);
	/* Destructor */
	~SctsAlert();
};

/*****************************************************************/
  
class SctsSyncHdr :public SctsCommand
{
private:
	bool final;
public:
	SmlSyncHdrPtr_t rawData;
	void IsFinal(bool isFinal) {final=isFinal;}
	bool IsFinal() {return final;}
	void MakeStub();  
	bool VerifyDTD(SctsSyncMLProto proto=SYNCML_DM_1_2);
	SctsCommandType GetType();
	SctsPcdata& GetCommandID(SctsPcdata&);
	void Build(SctsSessionContext);
	void BuildClose(SctsSessionContext);
	void SetMeta(SctsMeta *);
	SctsMeta& GetMeta(SctsMeta&);
	void SetCred(SctsCred *);
	SctsCred& GetCred(SctsCred&);
	void SetTarget(char *pLocURI,char *pLocName=0);
	SctsTargetSource& GetTarget(SctsTargetSource&);
	void SetSource(char *pLocURI,char *pLocName=0);
	SctsTargetSource& GetSource(SctsTargetSource&);
	bool IsNoResp();
	void IsNoResp(bool);
	void SetVersion(SctsPcdata *);
	SctsPcdata& GetVersion(SctsPcdata&);
	void SetProto(SctsPcdata *);
	SctsPcdata& GetProto(SctsPcdata&);
	void SetSessionID(SctsPcdata *);
	SctsPcdata& GetSessionID(SctsPcdata&);
	void SetMsgID(int);
	int GetMsgID();
	void SetRespURI(SctsPcdata *);
	SctsPcdata& GetRespURI(SctsPcdata&);

	/* Constructors */
	SctsSyncHdr(SmlSyncHdrPtr_t);
	SctsSyncHdr(SctsCred *pCred,SctsMeta *pMeta,SctsTargetSource *pSource,SctsTargetSource *pTarget,char *pVersion,char *pProto,char *sessionID,int msgID);
	/* Destructor */
	~SctsSyncHdr();
};

/*****************************************************************/

class SctsStatus :public SctsCommand
{
public:
	SmlStatusPtr_t rawData;
	void MakeStub();  
	bool VerifyDTD(SctsSyncMLProto proto=SYNCML_DM_1_2);
	SctsCommandType GetType();
	SctsPcdata& GetCommandID(SctsPcdata&);
	void Build(SctsSessionContext);
	void SetCred(SctsCred *);
	SctsCred& GetCred(SctsCred&);
	void SetData(int);
	int GetData();
	void SetMsgRef(int);
	int GetMsgRef();
	void SetCmdRef(SctsPcdata *);
	SctsPcdata& GetCmdRef(SctsPcdata&);
	void SetTargetRefList(SctsTargetRefList *);
	SctsTargetRefList& GetTargetRefList(SctsTargetRefList&);
	void SetSourceRefList(SctsSourceRefList *);
	SctsSourceRefList& GetSourceRefList(SctsSourceRefList&);
	void SetItemList(SctsItemList *);
	SctsItemList& GetItemList(SctsItemList&);
	void SetCmd(SctsPcdata *);
	SctsPcdata &GetCmd(SctsPcdata&);
	void SetChal(SctsChal *);
	SctsChal &GetChal(SctsChal&);
	void SetCommandID(int commandID);
	
	/* Constructors */
	SctsStatus(SmlStatusPtr_t);
	SctsStatus(unsigned int cmdID,int msgRef,char *pCmdRef,char *pCmd,int status);
	/* Destructor */
	~SctsStatus();
};

/*****************************************************************/

class SctsExec :public SctsCommand
{
public:
	SmlExecPtr_t rawData;
	void MakeStub();  
	bool VerifyDTD(SctsSyncMLProto proto=SYNCML_DM_1_2);
	SctsCommandType GetType();
	SctsPcdata& GetCommandID(SctsPcdata&);
	void Build(SctsSessionContext);
	void SetCred(SctsCred *);
	SctsCred& GetCred(SctsCred&);
	void SetItem(SctsItem *);
	SctsItem& GetItem(SctsItem&);

	bool IsNoResp();
	void IsNoResp(bool);

    /*
     * Change Alert to support Correlator. 
     */
	char * GetCorrelator();
	void SetCorrelator(char *);

	/* Constructors */
	SctsExec(SmlExecPtr_t);
	SctsExec(unsigned int cmdID,SctsItem *pItem);	
    /*
     * Change Alert to support Correlator. 
     */
	SctsExec(unsigned int cmdID,SctsItem *pItem,char *correlator);	
	/* Destructor */
	~SctsExec();
};


/*****************************************************************/

class SctsMap :public SctsCommand
{
public:
	SmlMapPtr_t rawData;
	void MakeStub();  
	bool VerifyDTD(SctsSyncMLProto proto=SYNCML_DM_1_2);
	SctsCommandType GetType();
	SctsPcdata& GetCommandID(SctsPcdata&);
	void SetCommandID(int);
	void Build(SctsSessionContext);
	void SetMeta(SctsMeta *);
	SctsMeta& GetMeta(SctsMeta&);
	void SetCred(SctsCred *);
	SctsCred& GetCred(SctsCred&);
	void SetTarget(char *pLocURI,char *pLocName=0);
	SctsTargetSource& GetTarget(SctsTargetSource&);
	void SetSource(char *pLocURI,char *pLocName=0);
	SctsTargetSource& GetSource(SctsTargetSource&);
	void SetMapItemList(SctsMapItemList *);
	SctsMapItemList& GetMapItemList(SctsMapItemList&);

	/* Constructors */
	SctsMap(SmlMapPtr_t);
	SctsMap(unsigned int cmdID,char *pSourceLocURI,char *pSourceLocName,char *pTargetLocURI,char *pTargetLocName,SctsMapItemList *pMapItemList);
	/* Destructor */
	~SctsMap();
};

/*****************************************************************/
#endif
#endif
