commands/iap/iap.cpp
changeset 0 7f656887cf89
equal deleted inserted replaced
-1:000000000000 0:7f656887cf89
       
     1 // iap.cpp
       
     2 // 
       
     3 // Copyright (c) 2008 - 2010 Accenture. All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of the "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 // 
       
     9 // Initial Contributors:
       
    10 // Accenture - Initial contribution
       
    11 //
       
    12 
       
    13 #include <commsdattypesv1_1.h>
       
    14 #include <fshell/ioutils.h>
       
    15 
       
    16 using namespace CommsDat;
       
    17 using namespace IoUtils;
       
    18 
       
    19 class CCmdIap : public CCommandBase
       
    20 	{
       
    21 public:
       
    22 	static CCommandBase* NewLC();
       
    23 	~CCmdIap();
       
    24 private: // From CCommandBase.
       
    25 	virtual const TDesC& Name() const;
       
    26 	virtual void DoRunL();
       
    27 	virtual void OptionsL(RCommandOptionList& aOptions);
       
    28 private:
       
    29 	CCmdIap();
       
    30 	void ListIapsL();
       
    31 	void AddWinsockprtIapL();
       
    32 	void DoAddWinsockprtIapL(CMDBSession& aDbSession);
       
    33 private:
       
    34 	TBool iAddWinsockprtIap;
       
    35 	};
       
    36 
       
    37 	
       
    38 CCommandBase* CCmdIap::NewLC()
       
    39 	{
       
    40 	CCmdIap* self = new (ELeave) CCmdIap();
       
    41 	CleanupStack::PushL(self);
       
    42 	self->BaseConstructL();
       
    43 	return self;
       
    44 	}
       
    45 
       
    46 CCmdIap::~CCmdIap()
       
    47 	{
       
    48 	}
       
    49 	
       
    50 CCmdIap::CCmdIap()
       
    51 	{
       
    52 	}
       
    53 
       
    54 const TDesC& CCmdIap::Name() const
       
    55 	{
       
    56 	_LIT(KName, "iap");
       
    57 	return KName;
       
    58 	}
       
    59 
       
    60 void CCmdIap::DoRunL()
       
    61 	{
       
    62 	if (iAddWinsockprtIap)
       
    63 		{
       
    64 		AddWinsockprtIapL();
       
    65 		}
       
    66 	else
       
    67 		{
       
    68 		ListIapsL();
       
    69 		}
       
    70 	}
       
    71 
       
    72 void CCmdIap::ListIapsL()
       
    73 	{
       
    74 	CMDBSession* session = CMDBSession::NewL(CMDBSession::LatestVersion());
       
    75 	CleanupStack::PushL(session);
       
    76 	CMDBRecordSet<CCDIAPRecord>* iapRecordSet = new (ELeave) CMDBRecordSet<CCDIAPRecord>(KCDTIdIAPRecord);
       
    77 	CleanupStack::PushL(iapRecordSet);
       
    78 	iapRecordSet->LoadL(*session);
       
    79 	const TInt maxCount = iapRecordSet->iRecords.Count();
       
    80 	if (maxCount == 0)
       
    81 		{
       
    82 		Printf(_L("No IAP records present.\r\n"));
       
    83 		}
       
    84 	else
       
    85 		{
       
    86 		IoUtils::CTextFormatter* formatter = CTextFormatter::NewLC(Stdout());
       
    87 		IoUtils::CTextBuffer* buf = IoUtils::CTextBuffer::NewLC(0x100);
       
    88 		buf->AppendFormatL(_L("Name\tType\tIdentifier\r\n"));
       
    89 		for (TInt count = 0 ; count < maxCount ; count++)
       
    90 			{
       
    91 			// print to stdout details on this particular IAP record
       
    92 			CCDIAPRecord& rec = *((*iapRecordSet)[count]);
       
    93 			TPtrC ptr(static_cast<const TDesC&>(rec.iRecordName));
       
    94 			TPtrC iapServiceType(rec.iServiceType);
       
    95 			buf->AppendFormatL(_L("%S\t%S\t%d\r\n"), &ptr, &iapServiceType, rec.RecordId());
       
    96 			}
       
    97 		formatter->TabulateL(0, 2, buf->Descriptor());
       
    98 		Write(formatter->Descriptor());
       
    99 		CleanupStack::PopAndDestroy(2, formatter);
       
   100 		}
       
   101 	CleanupStack::PopAndDestroy(2); // iaprecordSet, session
       
   102 	}
       
   103 
       
   104 void CCmdIap::OptionsL(RCommandOptionList& aOptions)
       
   105 	{
       
   106 	_LIT(KOptWinsockprt, "winsockprt");
       
   107 	aOptions.AppendBoolL(iAddWinsockprtIap, KOptWinsockprt);
       
   108 	}
       
   109 
       
   110 void CCmdIap::AddWinsockprtIapL()
       
   111 	{
       
   112 	CMDBSession* session = CMDBSession::NewL(CMDBSession::LatestVersion());
       
   113 	CleanupStack::PushL(session);
       
   114 	session->OpenTransactionL();
       
   115 
       
   116 	TRAPD(err, DoAddWinsockprtIapL(*session));
       
   117 
       
   118 	if (err == KErrNone)
       
   119 		{
       
   120 		session->CommitTransactionL();
       
   121 		}
       
   122 	else
       
   123 		{
       
   124 		PrintError(err, _L("Unable to add WinSockPrt IAP, rolling back..."));
       
   125 		session->RollbackTransactionL();
       
   126 		}
       
   127 
       
   128 	CleanupStack::PopAndDestroy(session);
       
   129 	}
       
   130 
       
   131 void CCmdIap::DoAddWinsockprtIapL(CMDBSession& aDbSession)
       
   132 	{
       
   133 	_LIT(KIapName, "WinSockPrt");
       
   134 	_LIT(KIspType, "DialOutISP");
       
   135 	_LIT(KNifName, "nullnif");
       
   136 	_LIT(KAgentName, "nullagt.agt");
       
   137 	_LIT(KBearerType, "ModemBearer");
       
   138 	_LIT(KDescription, "A dummy IAP for use with WinSockPrt.");
       
   139 	_LIT(KNetwork, "ip");
       
   140 
       
   141 	CCDIAPRecord* iapRecord = static_cast<CCDIAPRecord*>(CCDRecordBase::RecordFactoryL(KCDTIdIAPRecord));
       
   142 	CleanupStack::PushL(iapRecord);
       
   143 	iapRecord->iRecordName.SetL(KIapName);
       
   144 	if (iapRecord->FindL(aDbSession))
       
   145 		{
       
   146 		iapRecord->DeleteL(aDbSession);
       
   147 		}
       
   148 
       
   149 	CCDDialOutISPRecord* ispRecord = static_cast<CCDDialOutISPRecord*>(CCDRecordBase::RecordFactoryL(KCDTIdDialOutISPRecord));
       
   150 	CleanupStack::PushL(ispRecord);
       
   151 	ispRecord->iRecordName.SetL(KIapName);
       
   152 	if (ispRecord->FindL(aDbSession))
       
   153 		{
       
   154 		ispRecord->DeleteL(aDbSession);
       
   155 		}
       
   156 	ispRecord->SetRecordId(KCDNewRecordRequest);
       
   157 	ispRecord->iDescription.SetL(KDescription);
       
   158 	ispRecord->iUseLoginScript = EFalse;
       
   159 	ispRecord->iPromptForLogin = EFalse;
       
   160 	ispRecord->iIfNetworks.SetL(KNetwork);
       
   161 	ispRecord->StoreL(aDbSession);
       
   162 
       
   163 	CCDModemBearerRecord* bearerRecord = static_cast<CCDModemBearerRecord*>(CCDRecordBase::RecordFactoryL(KCDTIdModemBearerRecord));
       
   164 	CleanupStack::PushL(bearerRecord);
       
   165 	bearerRecord->iRecordName.SetL(KIapName);
       
   166 	if (bearerRecord->FindL(aDbSession))
       
   167 		{
       
   168 		bearerRecord->DeleteL(aDbSession);
       
   169 		}
       
   170 	bearerRecord->SetRecordId(KCDNewRecordRequest);
       
   171 	bearerRecord->iBearerAgent.SetL(KAgentName);
       
   172 	bearerRecord->iNifName.SetL(KNifName);
       
   173 	bearerRecord->StoreL(aDbSession);
       
   174 
       
   175 	CCDNetworkRecord* networkRecord = static_cast<CCDNetworkRecord*>(CCDRecordBase::RecordFactoryL(KCDTIdNetworkRecord));
       
   176 	CleanupStack::PushL(networkRecord);
       
   177 	networkRecord->iRecordName.SetL(KIapName);
       
   178 	if (networkRecord->FindL(aDbSession))
       
   179 		{
       
   180 		networkRecord->DeleteL(aDbSession);
       
   181 		}
       
   182 	networkRecord->SetRecordId(KCDNewRecordRequest);
       
   183 	networkRecord->StoreL(aDbSession);
       
   184 
       
   185 	iapRecord->SetRecordId(KCDNewRecordRequest);
       
   186 	iapRecord->iRecordName.SetL(KIapName);
       
   187 	iapRecord->iServiceType.SetL(KIspType);
       
   188 	iapRecord->iService.SetL(ispRecord->RecordId());
       
   189 	iapRecord->iBearerType.SetL(KBearerType);
       
   190 	iapRecord->iBearer.SetL(bearerRecord->RecordId());
       
   191 	iapRecord->iNetwork.SetL(networkRecord->RecordId());
       
   192 	iapRecord->StoreL(aDbSession);
       
   193 
       
   194 	CleanupStack::PopAndDestroy(4, iapRecord);
       
   195 	}
       
   196 
       
   197 EXE_BOILER_PLATE(CCmdIap)