email/pop3andsmtpmtm/smtpservermtm/src/csmtpsessionmanager.cpp
changeset 0 72b543305e3a
child 76 60a8a215b0ec
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 2007-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 "csmtpsessionmanager.h"
       
    17 #include "csmtpsettings.h"
       
    18 #include "imutcon.h"
       
    19 #include "IMSMSEND.H"
       
    20 #include "IMSM.H"
       
    21 #include "SMTSUTIL.H"
       
    22 #include "cimmobilitymanager.h"
       
    23 
       
    24 /**
       
    25 Factory constructor
       
    26 
       
    27 @param aMobilityManager Mobility manager (null if mobility not supported)
       
    28 @return Constructed session manager
       
    29 */
       
    30 CSmtpSessionManager* CSmtpSessionManager::NewL(CImMobilityManager* aMobilityManager, TMsvId aServiceId)
       
    31 	{
       
    32 	CSmtpSessionManager* self = new (ELeave) CSmtpSessionManager(aMobilityManager, aServiceId);
       
    33 	CleanupStack::PushL(self);
       
    34 	self->ConstructL();
       
    35 	CleanupStack::Pop(self);
       
    36 	return self;
       
    37 	}
       
    38 
       
    39 /**
       
    40 Constructor
       
    41 
       
    42 @param aMobilityManager Mobility manager (null if mobility not supported)
       
    43 */
       
    44 CSmtpSessionManager::CSmtpSessionManager(CImMobilityManager* aMobilityManager, TMsvId aServiceId) :
       
    45  CMsgActive(KImSmtpSessionPriority),
       
    46  iMobilityManager(aMobilityManager),
       
    47  iServiceId(aServiceId)
       
    48 	{
       
    49 	}
       
    50 
       
    51 /**
       
    52 Second phase constructor
       
    53 */
       
    54 void CSmtpSessionManager::ConstructL()
       
    55 	{
       
    56 	User::LeaveIfError(iServ.Connect());
       
    57 	CActiveScheduler::Add(this);
       
    58 	}
       
    59 
       
    60 /**
       
    61 Destructor
       
    62 */
       
    63 CSmtpSessionManager::~CSmtpSessionManager()
       
    64 	{
       
    65 	Cancel();
       
    66 	delete iConnect;
       
    67 	iServ.Close();
       
    68 	}
       
    69 
       
    70 /**
       
    71 Gets a new logged in SMTP session.
       
    72 The caller passes a reference to a session pointer which they should initially
       
    73 set to NULL. If the client status is completed with KErrNone, then their pointer
       
    74 will have been updated to point to the new session. If the client status completes
       
    75 with an error then their pointer will not be updated.
       
    76 
       
    77 @param aServerEntry SMTP service entry
       
    78 @param aSettings SMTP settings
       
    79 @param aSession Used to store session to pass back to caller
       
    80 @param aClientStatus Signals completion of the request
       
    81 */
       
    82 void CSmtpSessionManager::GetSessionL(CMsvServerEntry& aServerEntry,
       
    83                                       CSmtpSettings& aSettings,
       
    84                                       CImSmtpSession*& aSession,
       
    85                                       TRequestStatus& aClientStatus)
       
    86 	{
       
    87 	iServerEntry = &aServerEntry;
       
    88 	iSettings = &aSettings;
       
    89 	iStoreSession = &aSession;
       
    90 
       
    91 	CreateConnectionL();
       
    92 
       
    93 	Queue(aClientStatus);
       
    94 	}
       
    95 
       
    96 /**
       
    97 Deletes a session.
       
    98 
       
    99 @param aSession The session to delete. This routine takes immediate ownsership
       
   100                 of the session.
       
   101 @param aClientStatus Signals completion of the request
       
   102 */
       
   103 void CSmtpSessionManager::DeleteSession(CImSmtpSession& aSession,
       
   104                                         TRequestStatus& aClientStatus)
       
   105 	{
       
   106 	iSession = &aSession;
       
   107 
       
   108 	Queue(aClientStatus);
       
   109 
       
   110 	TRAPD(err, QuitSessionL());
       
   111 
       
   112 	// If we failed to quit from the server we should just delete the
       
   113 	// session anyway as that will drop the connection.
       
   114 	if (err != KErrNone)
       
   115 		{
       
   116 		delete iSession;
       
   117 		iSession = NULL;
       
   118 		TRequestStatus* status = &aClientStatus;
       
   119 		User::RequestComplete(status, KErrNone);
       
   120 		}
       
   121 	}
       
   122 
       
   123 /**
       
   124 Gets the progress of a connection
       
   125 
       
   126 @param aProgress Stores the progress information
       
   127 */
       
   128 void CSmtpSessionManager::ConnectionProgress(TImSmtpProgress& aProgress)
       
   129 	{
       
   130 	if (iSession)
       
   131 		{
       
   132 		aProgress.iSendFileProgress = iSession->FileProgress();
       
   133 		aProgress.SetMsgNo(iSession->GetConnectionStage());
       
   134 		aProgress.SetConnectionIAP(iSession->GetConnectionIAP());
       
   135 		}
       
   136 	else
       
   137 		{
       
   138 		TUint32 iap = 0;
       
   139 		TInt iapErr = KErrNotFound;
       
   140 	
       
   141 		aProgress.iSendFileProgress.iSessionState = EConnectingToSmtp;
       
   142 		aProgress.iSendFileProgress.iBytesSent = 0;
       
   143 		aProgress.iSendFileProgress.iBytesToSend = 0;
       
   144 
       
   145 		if (iConnect)
       
   146 			{
       
   147 			TNifProgress progress;
       
   148 			TInt err = iConnect->Progress(progress);
       
   149 			if (err == KErrNone)
       
   150 				{
       
   151 				aProgress.SetMsgNo(progress.iStage);
       
   152 				}
       
   153 			else
       
   154 				{
       
   155 				aProgress.SetMsgNo(err);
       
   156 				}
       
   157 
       
   158 			iapErr = iConnect->GetIAPValue(iap);
       
   159 			}
       
   160 		else
       
   161 			{
       
   162 			aProgress.SetMsgNo(KErrNotFound);
       
   163 			}
       
   164 
       
   165 		if (iapErr == KErrNone)
       
   166 			{
       
   167 			aProgress.SetConnectionIAP(iap);
       
   168 			}
       
   169 		else
       
   170 			{
       
   171 			aProgress.SetConnectionIAP(iapErr);
       
   172 			}
       
   173 		}
       
   174 	}
       
   175 
       
   176 /**
       
   177 Returns whether the session is connected
       
   178 
       
   179 @return Flag indicating if session is connected
       
   180 */
       
   181 TBool CSmtpSessionManager::IsSessionConnected()
       
   182 	{
       
   183 	if (iSession)
       
   184 		{
       
   185 		return iSession->IsConnected();
       
   186 		}
       
   187 
       
   188 	return EFalse;
       
   189 	}
       
   190 
       
   191 /**
       
   192 Returns whether the network connection has been started
       
   193 
       
   194 @return Flag indicating if network connection has been started
       
   195 */
       
   196 TBool CSmtpSessionManager::IsConnectionStarted()
       
   197 	{
       
   198 	if (iConnect)
       
   199 		{
       
   200 		return ETrue;
       
   201 		}
       
   202 
       
   203 	return EFalse;
       
   204 	}
       
   205 
       
   206 
       
   207 /**
       
   208 Called when asynchronous event completes.
       
   209 */
       
   210 void CSmtpSessionManager::DoRunL()
       
   211 	{
       
   212 	switch (iState)
       
   213 		{
       
   214 		case EStateCreatingConnection:
       
   215 			{
       
   216 			RegisterConnectionL();
       
   217 			CreateSessionL();
       
   218 			break;
       
   219 			}
       
   220 		
       
   221 		case EStateCreatingSession:
       
   222 			{
       
   223 			StoreSessionL();
       
   224 			break;
       
   225 			}
       
   226 
       
   227 		case EStateQuittingSession:
       
   228 			{
       
   229 			delete iSession;
       
   230 			iSession = NULL;
       
   231 			break;
       
   232 			}
       
   233 
       
   234 		case EStateStoringSession:
       
   235 		default:
       
   236 			{
       
   237 			gPanic(EImsmSessionManagerInvalidState);
       
   238 			break;
       
   239 			}
       
   240 		}
       
   241 	}
       
   242 
       
   243 /**
       
   244 Cancel an outstanding asynchronous request.
       
   245 */
       
   246 void CSmtpSessionManager::DoCancel()
       
   247 	{
       
   248 	if (iState == EStateCreatingConnection)
       
   249 		{
       
   250 		iConnect->Cancel();
       
   251 		}
       
   252 	else if (iSession)
       
   253 		{
       
   254 		iSession->Cancel();
       
   255 		}
       
   256 
       
   257 	CMsgActive::DoCancel();
       
   258 	}
       
   259 
       
   260 /**
       
   261 The clients outstanding asynchronous request has been completed.
       
   262 
       
   263 @param aStatus Status of completed operation.
       
   264 */
       
   265 void CSmtpSessionManager::DoComplete(TInt& aStatus)
       
   266 	{
       
   267 	if (aStatus != KErrNone)
       
   268 		{
       
   269 		delete iSession;
       
   270 		iSession = NULL;
       
   271 
       
   272 		if (iState == EStateCreatingConnection)
       
   273 			{
       
   274 			delete iConnect;
       
   275 			iConnect = NULL;
       
   276 			}
       
   277 		}
       
   278 	else
       
   279 		{
       
   280 		iSession = NULL;
       
   281 		}
       
   282 	}
       
   283 
       
   284 /**
       
   285 Creates the network connection if it has not already been created.
       
   286 */
       
   287 void CSmtpSessionManager::CreateConnectionL()
       
   288 	{
       
   289 	if (!iConnect)
       
   290 		{
       
   291 		iState = EStateCreatingConnection;
       
   292 
       
   293 		iConnect = CImConnect::NewL(iSettings->IapPrefs(), iServ);
       
   294 		iConnect->StartL(iStatus);
       
   295 		SetActive();
       
   296 		}
       
   297 	else
       
   298 		{
       
   299 		CreateSessionL();
       
   300 		}
       
   301 	}
       
   302 
       
   303 /**
       
   304 Registers the network connection with the bearer mobility manager (if defined)
       
   305 */
       
   306 void CSmtpSessionManager::RegisterConnectionL()
       
   307 	{
       
   308 	if (iMobilityManager)
       
   309 		{
       
   310 		iMobilityManager->SetConnection(iConnect->GetConnection());
       
   311 		}
       
   312 	}
       
   313 
       
   314 /**
       
   315 Starts the creation of a session.
       
   316 */
       
   317 void CSmtpSessionManager::CreateSessionL()
       
   318 	{
       
   319 	iState = EStateCreatingSession;
       
   320 
       
   321 	iSession = CImSmtpSession::NewL(*iServerEntry, *iSettings, iServ, *iConnect, iServiceId);
       
   322 	iSession->ConnectL(iStatus);
       
   323 	SetActive();
       
   324 	}
       
   325 
       
   326 /**
       
   327 Stores the created session via the reference passed by the client
       
   328 */
       
   329 void CSmtpSessionManager::StoreSessionL()
       
   330 	{
       
   331 	iState = EStateStoringSession;
       
   332 	*iStoreSession = iSession;
       
   333 
       
   334 	TUint32 iap;
       
   335 	TInt iapErr = iConnect->GetIAPValue(iap);
       
   336 
       
   337 	if (iapErr == KErrNone)
       
   338 		{
       
   339 		iSettings->SetIapL(iap);
       
   340 		}
       
   341 	else
       
   342 		{
       
   343 		iSettings->SetIapL(KDefaultSettingsIap);
       
   344 		}
       
   345 	}
       
   346 
       
   347 /**
       
   348 Quit from the server
       
   349 */
       
   350 void CSmtpSessionManager::QuitSessionL()
       
   351 	{
       
   352 	iState = EStateQuittingSession;
       
   353 	iSession->QuitL(iStatus);
       
   354 	SetActive();
       
   355 	}
       
   356 
       
   357 /**
       
   358 Gets the access point ID in use for the connection to the server
       
   359 
       
   360 @param aAccessPointId On return stores the access point ID value
       
   361 
       
   362 @return KErrNone if successful, or a system wide error code
       
   363 */
       
   364 TInt CSmtpSessionManager::GetAccessPointIdForConnection(TUint32& aAccessPointId) const
       
   365 	{
       
   366 	if (iConnect)
       
   367 		{
       
   368 		return iConnect->GetIAPValue(aAccessPointId);
       
   369 		}
       
   370 
       
   371 	return KErrNotFound;
       
   372 	}