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

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.

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

#include "SctsDevManDMAccount.h"
#include "string"
#include "SctsThread.h"
//#include "../DevManGUI/UIAInput.h"

/******************************************************************
SctsDMAccount Class
******************************************************************/

SctsDMAccount::SctsDMAccount(SctsDMTree *_tree, SctsDMNode *_dmAcc, const char *_dmAccPath)
    : verify(false)
{
    SCTSASSERT(_tree && _dmAcc);
    char *path = 0;

    std::string rootDMAccPath = _dmAccPath?_dmAccPath:"";
    if (_dmAccPath) rootDMAccPath += "/";
    std::string subDMAccPath;

    subDMAccPath = rootDMAccPath + "AppAddr/*/Addr";
    SctsDMNodePtrVec nodeVec = _tree->FindAnyDMNode((char *)subDMAccPath.c_str(), _dmAcc);
    if (nodeVec.size())
    {
        path = _tree->GetPath(nodeVec[0]->parent, 0, _dmAcc);
        subAppAddr = path;
        sctsFree(path);
    }

    subDMAccPath = rootDMAccPath + "AppAddr/*/Port/*/PortNbr";
    nodeVec = _tree->FindAnyDMNode((char *)subDMAccPath.c_str(), _dmAcc);
    if (nodeVec.size())
    {
        path = _tree->GetPath(nodeVec[0]->parent, 0, _dmAcc);
        subPort = path;
        sctsFree(path);
    }

    subDMAccPath = rootDMAccPath + "AppAuth/*/AAuthLevel";
    nodeVec = _tree->FindAnyDMNode((char *)subDMAccPath.c_str(), _dmAcc);
    for (SctsDMNodePtrVec::iterator it = nodeVec.begin(); it != nodeVec.end(); ++it)
    {
        if (!subAppAuthClient.length() && !sctsStrcmp((char *)(*it)->data, "CLCRED"))
        {
            path = _tree->GetPath((*it)->parent, 0, _dmAcc);
            subAppAuthClient = path;
            sctsFree(path);
        }
        else if(!subAppAuthServer.length() && !sctsStrcmp((char *)(*it)->data, "SRVCRED"))
        {
            path = _tree->GetPath((*it)->parent, 0, _dmAcc);
            subAppAuthServer = path;
            sctsFree(path);
        }
    }
    /*
    if (!subAppAuthClient.length())
    {
        SctsSemaphore lock(0,1);
        CString input = "AppAuth/xxx";
        CUIAInput *pDlg = new CUIAInput(lock,"Enter DMAccount information","Please specify node path store client side authentication as AppAuth/xxx",input,'T','A',0,1);
        pDlg->DoModal();
        subAppAuthClient = input;
    }
    if (!subAppAuthServer.length())
    {
        SctsSemaphore lock(0,1);
        CString input = "AppAuth/xxx";
        CUIAInput *pDlg = new CUIAInput(lock,"Enter DMAccount information","Please specify node path store server side authentication as AppAuth/xxx",input,'T','A',0,1);
        pDlg->DoModal();
        subAppAuthServer = input;
    }
    */

    subDMAccPath = rootDMAccPath + "ToConRef/*/ConRef";
    nodeVec = _tree->FindAnyDMNode((char *)subDMAccPath.c_str(), _dmAcc);
    if (nodeVec.size())
    {
        path = _tree->GetPath(nodeVec[0]->parent, 0, _dmAcc);
        subToConRef = path;
        sctsFree(path);
    }

    verify = (subAppAddr.length() != 0 ||
              subPort.length() != 0 ||
              subAppAuthClient.length() != 0 ||
              subAppAuthServer.length() != 0 ||
              subToConRef.length() != 0);
    path = _tree->GetPath(_dmAcc);
    nodePath = path;
    sctsFree(path);
}

const char * SctsDMAccount::GetPath() const
{
    return nodePath.c_str();
}

const char * SctsDMAccount::GetSubAppAddr() const
{
    return subAppAddr.c_str();
}

const char * SctsDMAccount::GetSubPort() const
{
    return subPort.c_str();
}

const char * SctsDMAccount::GetSubAppAuthClient() const
{
    return subAppAuthClient.c_str();
}

const char * SctsDMAccount::GetSubAppAuthServer() const
{
    return subAppAuthServer.c_str();
}

const char * SctsDMAccount::GetSubToConRef() const
{
    return subToConRef.c_str();
}
