email/pop3andsmtpmtm/servermtmutils/src/imutcon.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <cdbpreftable.h>
       
    17 
       
    18 #include <commsdat.h>
       
    19 #include <commsdattypesv1_1.h>
       
    20 #include <commdbconnpref.h>
       
    21 #include <miut_err.h>
       
    22 #include <es_enum.h>		// For TConnectionInfo
       
    23 #include <imutcon.h>
       
    24 #include "IMSK.H"
       
    25 
       
    26 
       
    27 #ifdef _DEBUG
       
    28 #define __IMSK_SIMULATION
       
    29 #include <etel.h>
       
    30 #endif
       
    31 
       
    32 #define __IMSK_LOGGING
       
    33 
       
    34 
       
    35 CImConnect* CImConnect::NewL(const CImIAPPreferences& aIAPPreferences, CImTextServerSession &aTextSession)
       
    36 	{
       
    37 	CImConnect* self = new (ELeave) CImConnect(aIAPPreferences, &aTextSession, NULL);
       
    38 	CActiveScheduler::Add(self);
       
    39 	return self;
       
    40 	}
       
    41 
       
    42 EXPORT_C CImConnect* CImConnect::NewL(const CImIAPPreferences& aIAPPreferences, RSocketServ& aSocketServerHandle)
       
    43 	{
       
    44 	CImConnect* self = new (ELeave) CImConnect(aIAPPreferences, NULL, &aSocketServerHandle);
       
    45 	CActiveScheduler::Add(self);
       
    46 	return self;
       
    47 	}
       
    48 
       
    49 void CImConnect::RunL()
       
    50 	{
       
    51 	User::RequestComplete(iReportStatus, iStatus.Int());
       
    52 	}
       
    53 
       
    54 void CImConnect::DoCancel()
       
    55 	{
       
    56 	iConn.Close();
       
    57 	User::RequestComplete(iReportStatus, KErrCancel);
       
    58 	}
       
    59 
       
    60 EXPORT_C void CImConnect::StartL(TRequestStatus& aStatus)
       
    61 	{
       
    62 	// Attempt the first connection using the appropriate IAP.
       
    63 	iReportStatus = &aStatus;
       
    64 	if (iTextSession != NULL)
       
    65 		{
       
    66 		User::LeaveIfError(iConn.Open(iTextSession->GetSocketServ()));
       
    67 		}
       
    68 	else
       
    69 		{
       
    70 		User::LeaveIfError(iConn.Open(*iSocketServerHandle));
       
    71 		}
       
    72 	ConnectL();
       
    73 	SetActive();
       
    74 	aStatus=KRequestPending;
       
    75 	}
       
    76 
       
    77 #ifdef __IMSK_SIMULATION
       
    78 void CImConnect::SetIAPsToFail(TInt aIAPsToFail)
       
    79 	{
       
    80 	iIAPsToFail=aIAPsToFail;
       
    81 	}
       
    82 #endif
       
    83 
       
    84 void CImConnect::ConnectL()
       
    85 	{
       
    86 	if (iIAPPreferences.SNAPDefined())
       
    87 		{
       
    88 		ConnectWithSNAP();
       
    89 		}
       
    90 	else
       
    91 		{
       
    92 		ConnectWithIAPListL();
       
    93 		}
       
    94 	}
       
    95 
       
    96 void CImConnect::ConnectWithSNAP()
       
    97 	{
       
    98 	TConnSnapPref snapPref(iIAPPreferences.SNAPPreference());
       
    99 	iConn.Start(snapPref, iStatus);
       
   100 	}
       
   101 
       
   102 void CImConnect::ConnectWithIAPListL()
       
   103 	{
       
   104 	TInt iapCount=iIAPPreferences.NumberOfIAPs();
       
   105 
       
   106 	if(iapCount==0)
       
   107 		{
       
   108 #ifdef __IMSK_LOGGING
       
   109 		_LIT8(KImConDefault,"CImConnect connecting with defaults");
       
   110 		if (iTextSession != NULL)
       
   111 			{
       
   112 			iTextSession->LogText(KImConDefault);
       
   113 			}
       
   114 #endif
       
   115 		iConn.Start(iStatus);
       
   116 		}
       
   117 	else
       
   118 		{
       
   119 #ifdef __IMSK_SIMULATION
       
   120 		if(iapCount<=iIAPsToFail)
       
   121 			{
       
   122 #ifdef __IMSK_LOGGING
       
   123 			_LIT8(KImConSimFail,"CImConnect simulated failure of all IAPs");
       
   124 			if (iTextSession != NULL)
       
   125 				{
       
   126 				iTextSession->LogText(KImConSimFail);
       
   127 				}
       
   128 #endif
       
   129 			TRequestStatus* status = &iStatus;
       
   130 			// Just pick an etel error and pretend we failed
       
   131    			User::RequestComplete(status, KErrEtelModemNotDetected);  
       
   132 			return;
       
   133 			}
       
   134 #endif
       
   135 		// Set up the appropriate override
       
   136 #ifdef __IMSK_LOGGING
       
   137 		_LIT8(KImConOverride,"CImConnect trying overrides");
       
   138 		if (iTextSession != NULL)
       
   139 			{
       
   140 			iTextSession->LogText(KImConOverride);
       
   141 			}
       
   142 #endif
       
   143 
       
   144 		TInt rank=1;
       
   145 #ifdef __IMSK_SIMULATION
       
   146 		for(TInt i=iIAPsToFail;i<iapCount;i++)
       
   147 #else
       
   148 		for(TInt i=0;i<iapCount;i++)
       
   149 #endif
       
   150 			{
       
   151 			TCommDbConnPref pref;
       
   152 			pref.SetDirection(ECommDbConnectionDirectionOutgoing);
       
   153 			
       
   154 			// legacy code which restricted the type of bearer
       
   155 			pref.SetBearerSet(KCommDbBearerCSD | KCommDbBearerWcdma | KCommDbBearerLAN |
       
   156 			                  KCommDbBearerVirtual | KCommDbBearerPAN | KCommDbBearerWLAN);
       
   157 
       
   158 			TImIAPChoice iapChoice = iIAPPreferences.IAPPreference(i);
       
   159 
       
   160 			pref.SetIapId(iapChoice.iIAP);
       
   161 			pref.SetDialogPreference(iapChoice.iDialogPref);
       
   162 
       
   163 			User::LeaveIfError(iPrefs.SetPreference(rank, pref));
       
   164 
       
   165 			rank++;
       
   166 			}
       
   167 
       
   168 		iPrefs.SetConnectionAttempts(rank-1);
       
   169 		iConn.Start(iPrefs, iStatus);
       
   170 		}
       
   171 	}
       
   172 
       
   173 CImConnect::~CImConnect()
       
   174 	{
       
   175 	Cancel();
       
   176 	// Fix for DEF40210:
       
   177 	// Although it is not necessary because the RSocketServ will delete all RConnection handles when it is closed, 
       
   178 	// we call this for completeness as this class should tidy up all resources it creates
       
   179 	iConn.Close();
       
   180 	}
       
   181 
       
   182 CImConnect::CImConnect(const CImIAPPreferences& aIAPPreferences, CImTextServerSession* aTextSession, RSocketServ* aSocketServerHandle) : 
       
   183 		CActive(EPriorityStandard), iIAPPreferences(aIAPPreferences), iTextSession(aTextSession), iSocketServerHandle(aSocketServerHandle)
       
   184 	{
       
   185 	}
       
   186 
       
   187 TInt CImConnect::RunError(TInt aError)
       
   188 	{
       
   189 	if (iReportStatus)
       
   190 		User::RequestComplete(iReportStatus, aError);
       
   191 
       
   192 	return KErrNone;
       
   193 	}
       
   194 
       
   195 EXPORT_C TInt CImConnect::GetIAPValue(TUint32& aIap)
       
   196 	{
       
   197 	// INC040630 - AV20 Panic while disconnecting a service.
       
   198 	// Cancel closes the RConnection. Check we still have a valid subsession
       
   199 	// handle.
       
   200 	TInt iapValue = KErrBadHandle;
       
   201 	if (iConn.SubSessionHandle() != KNullHandle)
       
   202 		{		
       
   203 		TBuf<KCommsDbSvrMaxColumnNameLength> settingName;
       
   204 		settingName.Copy(KCDTypeNameIAP);
       
   205 		settingName.Append('\\');
       
   206 		settingName.Append(TPtrC(KCDTypeNameRecordTag));
       
   207 		iapValue = iConn.GetIntSetting(settingName, aIap);
       
   208 		}
       
   209 	return iapValue;
       
   210 	}
       
   211 
       
   212 EXPORT_C TInt CImConnect::GetIAPBearer(TUint32& aBearer)
       
   213 	{
       
   214 	TInt err = KErrBadHandle;
       
   215 	if (iConn.SubSessionHandle() != KNullHandle)
       
   216 		{
       
   217 		TBuf<KCommsDbSvrMaxColumnNameLength> iapservicename;	
       
   218 		TBuf<KCommsDbSvrMaxColumnNameLength> settingName;
       
   219 		settingName.Copy(KCDTypeNameIAP);
       
   220 		settingName.Append('\\');
       
   221 		settingName.Append(TPtrC(KCDTypeNameServiceType));
       
   222 		
       
   223 		// Get the current active IAP's service name
       
   224 		err = iConn.GetDesSetting(settingName, iapservicename);
       
   225 		if(err==KErrNone)
       
   226 			{
       
   227 			// Set the bearer corresponding to the service name
       
   228 			if( iapservicename.Compare(TPtrC(KCDTypeNameDialOutISP))==0 ||
       
   229 				iapservicename.Compare(TPtrC(KCDTypeNameDialInISP))==0)
       
   230 				aBearer=KCommDbBearerCSD;
       
   231 			else if( iapservicename.Compare(TPtrC(KCDTypeNameOutgoingWCDMA))==0 ||
       
   232 					 iapservicename.Compare(TPtrC(KCDTypeNameIncomingWCDMA))==0)
       
   233 				aBearer=KCommDbBearerWcdma;
       
   234 			else if(iapservicename.Compare(TPtrC(KCDTypeNameLANService))==0)
       
   235 				aBearer=KCommDbBearerLAN;	
       
   236 			else 
       
   237 				err = KErrNotFound;
       
   238 			}	
       
   239 		}
       
   240 	return err;
       
   241 	}
       
   242 
       
   243 EXPORT_C RConnection& CImConnect::GetConnection()
       
   244 	{
       
   245 	return iConn;
       
   246 	}
       
   247 
       
   248 EXPORT_C TInt CImConnect::Progress(TNifProgress& aProgress)
       
   249 	{
       
   250 	if (iConn.SubSessionHandle() != KNullHandle)
       
   251 		return iConn.Progress(aProgress);
       
   252 	return KErrBadHandle;
       
   253 	}
       
   254 
       
   255 EXPORT_C TInt CImConnect::GetRConnectionName(TName &aName)
       
   256 	{
       
   257 	return iConn.Name(aName);
       
   258 	}
       
   259 
       
   260 EXPORT_C TInt CImConnect::GetLastSocketActivityTimeout(TUint32& aTimeout)
       
   261 	{
       
   262 	TInt err = KErrBadHandle;
       
   263 
       
   264 	if (iConn.SubSessionHandle() != KNullHandle)
       
   265 		{
       
   266 		TBuf<KCommsDbSvrMaxColumnNameLength> settingName;
       
   267 		TBuf<KCommsDbSvrMaxColumnNameLength> iapBearer;
       
   268 
       
   269 		settingName.Copy(KCDTypeNameIAP);
       
   270 		settingName.Append('\\');
       
   271 		settingName.Append(TPtrC(KCDTypeNameBearerType));
       
   272 
       
   273 		// Get the current active IAP's bearer table name
       
   274 		err = iConn.GetDesSetting(settingName, iapBearer);
       
   275 
       
   276 		if (err == KErrNone)
       
   277 			{
       
   278 			// Get the last socket activity timeout from the bearer table
       
   279 			iapBearer.Append('\\');
       
   280 			iapBearer.Append(TPtrC(KCDTypeNameLastSocketActivityTimeout));
       
   281 			err = iConn.GetIntSetting(iapBearer, aTimeout);
       
   282 			}
       
   283 		}
       
   284 
       
   285 	return err;
       
   286 	}
       
   287 
       
   288 // Continuing the Existing RConnection	
       
   289 void CImConnect::SecondaryStartL(CImTextServerSession* aPrimaryTextServerSession)
       
   290 	{
       
   291 	// openign the connection from socketserver.
       
   292 	User::LeaveIfError(iConn.Open(iTextSession->GetSocketServ()));
       
   293 
       
   294 #ifdef __IMSK_LOGGING
       
   295 	_LIT8(KImConDefault,"SecondarySession connecting with existing RConnection");
       
   296 	iTextSession->LogText(KImConDefault);
       
   297 #endif
       
   298 
       
   299 	TConnectionInfo connInfo;
       
   300 	TPckg<TConnectionInfo> connInfoPckg(connInfo);
       
   301 	TUint count;
       
   302 	// Enumerates the number of currently active interfaces
       
   303 	User::LeaveIfError(aPrimaryTextServerSession->GetCImConnect()->GetConnection().EnumerateConnections(count));
       
   304 	// Get the information about currently active connection	
       
   305 	User::LeaveIfError(aPrimaryTextServerSession->GetCImConnect()->GetConnection().GetConnectionInfo(count, connInfoPckg) );
       
   306 	// Attaches the RConnection object to an existing interface
       
   307 	User::LeaveIfError(iConn.Attach(connInfoPckg, RConnection::EAttachTypeNormal));
       
   308 	}
       
   309