email/pop3andsmtpmtm/autosend/src/autosend.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 2001-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 <e32base.h>
       
    17 #include <e32std.h>
       
    18 #include <msventry.h>
       
    19 #include <msvapi.h>
       
    20 #include <msvids.h>
       
    21 #include <mtclreg.h>
       
    22 #include <autosend.h>
       
    23 #include <smtcmtm.h>
       
    24 #include <bacline.h>
       
    25 
       
    26 const TInt KProgressPollingRate = 10000000; // 10 seconds
       
    27 
       
    28 //uncomment the following line if you do not want logging.
       
    29 //#define NO_AUTOSEND_LOGGING
       
    30 
       
    31 #ifdef AUTOSEND_LOGGING
       
    32 _LIT(KAutoSendLogDir, "email");
       
    33 _LIT(KAutoSendLogFile, "autosend.txt");
       
    34 #endif
       
    35 
       
    36 TInt Execute(const TMsvId aDestinationId)
       
    37 	{
       
    38 	__UHEAP_MARK;
       
    39 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
    40 
       
    41 	TRAPD(leaveValue, DoExecuteL(aDestinationId));
       
    42 
       
    43 	delete cleanup;
       
    44 	__UHEAP_MARKEND;
       
    45 	return leaveValue;
       
    46 	}
       
    47 
       
    48 #include <u32std.h>
       
    49 
       
    50 void TDummyMsvSessionObserver::HandleSessionEventL(TMsvSessionEvent /*aEvent*/, TAny */*aArg1*/, TAny */*aArg2*/, TAny */*aArg3*/)
       
    51 	{
       
    52 	}
       
    53 
       
    54 void CheckProcessCreatorL(void)
       
    55 	{
       
    56 	RProcess me;
       
    57 	TSecurityInfo inf(me);
       
    58 	inf.SetToCreatorInfo();
       
    59 	TDummyMsvSessionObserver dummyObserver;
       
    60 	
       
    61 	CMsvSession *sess = CMsvSession::OpenSyncL(dummyObserver);
       
    62 	CleanupStack::PushL(sess);
       
    63 	
       
    64 	// inf.iCaps should be the TCapabilitySet of the creator process.
       
    65 	TCapabilitySet mtmCapSet;
       
    66 	sess->GetMtmRequiredCapabilitiesL(KUidMsgTypeSMTP, mtmCapSet);
       
    67 
       
    68 	if(!inf.iCaps.HasCapabilities(mtmCapSet))
       
    69 		{
       
    70 		// work out the missing capabilities for the diagnostics
       
    71 		mtmCapSet.Remove(inf.iCaps);
       
    72 		User::LeaveIfError(PlatSec::CreatorCapabilityCheckFail(mtmCapSet,__PLATSEC_DIAGNOSTIC_STRING("Autorun.exe: Creator Caps do not match those of the SMTP Server MTM")));
       
    73 		}
       
    74 		
       
    75 	CleanupStack::PopAndDestroy(sess);
       
    76 	}
       
    77 
       
    78 void DoExecuteL(const TMsvId aDestinationId)
       
    79 	{
       
    80 	CActiveScheduler* activeSch = new (ELeave) CActiveScheduler();
       
    81 	CleanupStack::PushL(activeSch);
       
    82 
       
    83 	CActiveScheduler::Install(activeSch);
       
    84 
       
    85 	CheckProcessCreatorL();
       
    86 
       
    87 	CImAutoSend::StartLC(aDestinationId);
       
    88 
       
    89 	CleanupStack::PopAndDestroy(2); //activeSch, CAutoSend
       
    90 	}
       
    91 
       
    92 /*
       
    93 //
       
    94 	CImAutoSend Implementation
       
    95 //
       
    96 */
       
    97 
       
    98 /*
       
    99 	CImAutoSend Constructor
       
   100 */
       
   101 CImAutoSend::CImAutoSend(const TMsvId aDestinationId)
       
   102 	: CActive(KDefaultPriority), iDestinationId(aDestinationId)
       
   103 	{
       
   104 	//Add this to the Active Scheduler.
       
   105 	//This is a requirement if a class is derived from CActive
       
   106 	CActiveScheduler::Add(this);
       
   107 	}
       
   108 
       
   109 /*
       
   110 	CImAutoSend Destructor
       
   111 */
       
   112 CImAutoSend::~CImAutoSend()
       
   113 	{
       
   114 	Cancel();
       
   115 	delete iTimer;
       
   116 	delete iOperation;
       
   117 	delete iSession;
       
   118 	}
       
   119 
       
   120 /*
       
   121 	StartLC
       
   122 
       
   123 	1. Constructs CImAutoSend
       
   124 	2. Attempts to send the messages
       
   125 */
       
   126 CImAutoSend* CImAutoSend::StartLC(const TMsvId aDestinationId)
       
   127 	{
       
   128 	CImAutoSend* self = new (ELeave) CImAutoSend(aDestinationId);
       
   129 	CleanupStack::PushL(self);
       
   130 	self->ConstructL();
       
   131 	return self;
       
   132 	}
       
   133 
       
   134 void CImAutoSend::ConstructL()
       
   135 	{
       
   136 #ifdef AUTOSEND_LOGGING
       
   137 	FLog(_L("Autosend using service %d"), iDestinationId);
       
   138 #endif
       
   139 	iSession = CMsvSession::OpenSyncL(*this);
       
   140 
       
   141 	CallMtmL();
       
   142 	iActiveSchedulerWait.Start();
       
   143 	}
       
   144 
       
   145 /*
       
   146 	CallMtmL
       
   147 
       
   148 	Attempts to send the messages in the Outbox.  The selection of messages is determined
       
   149 	on the Server side dependant on which IAP the user is connected to.
       
   150 */
       
   151 void CImAutoSend::CallMtmL()
       
   152 	{
       
   153 #ifdef AUTOSEND_LOGGING
       
   154 	FLog(_L("\tCallMtmL"));
       
   155 #endif
       
   156 
       
   157 	TBuf8<1> buf;
       
   158 	CMsvEntrySelection* selection = new (ELeave) CMsvEntrySelection();
       
   159 	CleanupStack::PushL(selection);
       
   160 	selection->AppendL(iDestinationId);
       
   161 	iOperation = iSession->TransferCommandL(*selection, KSMTPMTMSendOnNextConnection, buf, iStatus);
       
   162 
       
   163 	CleanupStack::PopAndDestroy(selection);
       
   164 
       
   165 	delete iTimer;
       
   166 	iTimer = NULL;
       
   167 
       
   168 	iTimer = CAutoSendProgressTimer::NewL(*iOperation, KProgressPollingRate);
       
   169 	iTimer->Start();
       
   170 	iStatus=KRequestPending;
       
   171 	SetActive();
       
   172 	}
       
   173 
       
   174 void CImAutoSend::DoCancel()
       
   175 	{
       
   176 #ifdef AUTOSEND_LOGGING
       
   177 	FLog(_L("\tDoCancel"));
       
   178 #endif
       
   179 	iActiveSchedulerWait.AsyncStop();
       
   180 	}
       
   181 
       
   182 /*
       
   183 	RunL
       
   184 
       
   185 	Called by the Active Scheduler with iStatus is complete,
       
   186 	i.e when the selection of messages have been sent to the appropriate Mtm.
       
   187 */
       
   188 void CImAutoSend::RunL()
       
   189 	{
       
   190 #ifdef AUTOSEND_LOGGING
       
   191 	FLog(_L("\tRunL iStatus=%d"), iStatus.Int());
       
   192 #endif
       
   193 	iActiveSchedulerWait.AsyncStop();
       
   194 	}
       
   195 
       
   196 #ifdef AUTOSEND_LOGGING
       
   197 void CImAutoSend::FLog(TRefByValue<const TDesC> aFormat, ...)
       
   198 	{
       
   199 	VA_LIST list;
       
   200 	VA_START(list, aFormat);
       
   201 	RFileLogger::WriteFormat(KAutoSendLogDir, KAutoSendLogFile, EFileLoggingModeAppend,
       
   202 		aFormat, list);
       
   203 	}
       
   204 
       
   205 void CImAutoSend::FLog(const TDesC& buf)
       
   206 	{
       
   207 	RFileLogger::Write(KAutoSendLogDir, KAutoSendLogFile, EFileLoggingModeAppend,buf);
       
   208 	}
       
   209 #endif
       
   210 
       
   211 
       
   212 /*
       
   213 //
       
   214 	CAutoSendProgressTimer Implementation
       
   215 //
       
   216 */
       
   217 
       
   218 CAutoSendProgressTimer* CAutoSendProgressTimer::NewL(CMsvOperation& aOperation, const TTimeIntervalMicroSeconds32& aPollInterval)
       
   219 	{
       
   220 	CAutoSendProgressTimer* self = new (ELeave) CAutoSendProgressTimer(aOperation, aPollInterval);
       
   221 	CleanupStack::PushL(self);
       
   222 
       
   223 	self->ConstructL();
       
   224 
       
   225 	CleanupStack::Pop();
       
   226 
       
   227 	return self;
       
   228 	}
       
   229 
       
   230 CAutoSendProgressTimer::CAutoSendProgressTimer(CMsvOperation& aOperation, const TTimeIntervalMicroSeconds32& aPollInterval)
       
   231 : CTimer(EPriorityHigh), iOperation(aOperation), iPollInterval(aPollInterval)
       
   232 	{
       
   233 	CActiveScheduler::Add(this);
       
   234 	}
       
   235 
       
   236 CAutoSendProgressTimer::~CAutoSendProgressTimer()
       
   237 	{
       
   238 	Cancel();
       
   239 	}
       
   240 
       
   241 void CAutoSendProgressTimer::Start()
       
   242 	{
       
   243 	if (iPollInterval.Int() > 0)
       
   244 		{
       
   245 		After(iPollInterval);
       
   246 		}
       
   247 	}
       
   248 
       
   249 void CAutoSendProgressTimer::RunL()
       
   250 	{
       
   251 	if (iPollInterval.Int() > 0)
       
   252 		{
       
   253 		// Ignore progress errors (we don't care)
       
   254 		TRAPD(error, iOperation.ProgressL());
       
   255 
       
   256 #ifdef AUTOSEND_LOGGING
       
   257 		RFileLogger::WriteFormat(KAutoSendLogDir, KAutoSendLogFile, EFileLoggingModeAppend,
       
   258 			_L("\tPollProgress error=%d"), error);
       
   259 #endif
       
   260 
       
   261 		After(iPollInterval);
       
   262 		}
       
   263 	}
       
   264 
       
   265 
       
   266 /*
       
   267 //
       
   268 	The function that actually does the stuff
       
   269 //
       
   270 */
       
   271 
       
   272 GLDEF_C TInt E32Main()
       
   273 	{
       
   274 	TMsvId serviceId = KMsvNullIndexEntryId;
       
   275 
       
   276 	TBuf<0x100> cmd;
       
   277 		User::CommandLine(cmd);
       
   278 	
       
   279 	TLex lex(cmd);
       
   280 	lex.Val(serviceId);
       
   281 
       
   282 	if (serviceId)
       
   283 		return Execute(serviceId);
       
   284 
       
   285 	return KErrArgument;
       
   286 	}
       
   287