browserutilities/schemehandler/SchemeDispatcher/src/MailToHandler.cpp
changeset 0 dd21522fd290
child 36 0ed94ceaa377
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "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 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *      Implementation of Scheme handler interface implementation for mailto:// scheme
       
    16 *      
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 
       
    23 #include "MailToHandler.h"
       
    24 #include "SchemeDispLogger.h"
       
    25 #include <ECom.h>		// For REComSession
       
    26 #include <eikenv.h>
       
    27 #include <DocumentHandler.h>
       
    28 #include <apgcli.h>
       
    29 #include <apparc.h>
       
    30 #include <eikdoc.h>
       
    31 #include <eikproc.h>
       
    32 #include <f32file.h>
       
    33 
       
    34 
       
    35 // ================= CONSTANTS =======================
       
    36 
       
    37 _LIT( KMailto,"mailto:");
       
    38 _LIT( KSubject, "subject=" );
       
    39 _LIT( KBody, "body=" );
       
    40 _LIT( KCc, "cc=" );
       
    41 _LIT( KTo, "to=" );
       
    42 _LIT( KBcc, "bcc=" );
       
    43 _LIT( KComma, "," );
       
    44 
       
    45 // ================= MEMBER FUNCTIONS =======================
       
    46 
       
    47 // ---------------------------------------------------------
       
    48 // CMailToHandler::NewL()
       
    49 // ---------------------------------------------------------
       
    50 //
       
    51 CMailToHandler* CMailToHandler::NewL( const TDesC& aUrl )
       
    52 	{
       
    53 	CLOG_ENTERFN( "CMailToHandler::NewL()" );
       
    54 
       
    55 	CMailToHandler* self=new(ELeave) CMailToHandler();
       
    56 	CleanupStack::PushL(self);
       
    57 	self->ConstructL( aUrl );
       
    58 	CleanupStack::Pop(self);
       
    59 
       
    60 	CLOG_LEAVEFN( "CMailToHandler::NewL()" );
       
    61 
       
    62 	return self;
       
    63 	}
       
    64 
       
    65 // ---------------------------------------------------------
       
    66 // CMailToHandler::~CMailToHandler()
       
    67 // ---------------------------------------------------------
       
    68 //
       
    69 CMailToHandler::~CMailToHandler()
       
    70 	{
       
    71 	CLOG_ENTERFN( "CMailToHandler::~CMailToHandler()" );
       
    72 
       
    73     if( iTelService != NULL )
       
    74         {
       
    75 	    iTelService->RemoveObserver(this);
       
    76 	    delete iTelService;
       
    77         iTelService = NULL;
       
    78         }
       
    79 
       
    80 	CLOG_LEAVEFN( "CMailToHandler::~CMailToHandler()" );
       
    81 	}
       
    82 
       
    83 // ---------------------------------------------------------
       
    84 // CMailToHandler::CMailToHandler()
       
    85 // ---------------------------------------------------------
       
    86 //
       
    87 CMailToHandler::CMailToHandler() : CBaseHandler()
       
    88 	{
       
    89 	// Deliberately do nothing here : See ConstructL() for initialisation completion.
       
    90 	}
       
    91 
       
    92 // ---------------------------------------------------------
       
    93 // CMailToHandler::ConstructL()
       
    94 // ---------------------------------------------------------
       
    95 //
       
    96 void CMailToHandler::ConstructL( const TDesC& aUrl )
       
    97 	{
       
    98 	CLOG_ENTERFN( "CMailToHandler::ConstructL()" );
       
    99 
       
   100 	BaseConstructL( aUrl );
       
   101 
       
   102 	CLOG_LEAVEFN( "CMailToHandler::ConstructL()" );
       
   103 	}
       
   104 
       
   105 // ---------------------------------------------------------
       
   106 // CMailToHandler::HandleUrlEmbeddedL()
       
   107 // ---------------------------------------------------------
       
   108 //
       
   109 void CMailToHandler::HandleUrlEmbeddedL()
       
   110 	{
       
   111 	CLOG_ENTERFN( "CMailToHandler::HandleUrlEmbeddedL()" );
       
   112 
       
   113     //TPtrC path = iParsedUrl->Des();
       
   114 
       
   115 	iTelService = CBrowserTelService::NewL();
       
   116 	iTelService->AddObserver( this );
       
   117 
       
   118 	TPtrC recipient = GetField( KMailto );
       
   119 	TPtrC subject = GetField( KSubject );
       
   120 	TPtrC msgBody = GetField( KBody );
       
   121 	TPtrC cC = GetField( KCc );
       
   122     TPtrC tO = GetField( KTo );
       
   123     TPtrC bCC = GetField( KBcc );
       
   124 
       
   125     HBufC* rec = ChangeSeparationLC( recipient );
       
   126     HBufC* ccrec = ChangeSeparationLC( cC );
       
   127     HBufC* torec = ChangeSeparationLC( tO );
       
   128     HBufC* bccrec = ChangeSeparationLC( bCC );
       
   129 
       
   130     HBufC* allrec = HBufC::NewLC( ccrec->Length() +
       
   131                                   torec->Length() +
       
   132                                   bccrec->Length() + 3*KComma().Length() );
       
   133     if( ccrec->Length() != 0 )
       
   134         {
       
   135         if( allrec->Length() != 0 )
       
   136             {
       
   137             allrec->Des().Append( KComma() );
       
   138             }
       
   139         allrec->Des().Append( ccrec->Des() );
       
   140         }
       
   141     if( torec->Length() != 0 )
       
   142         {
       
   143         if( allrec->Length() != 0 )
       
   144             {
       
   145             allrec->Des().Append( KComma() );
       
   146             }
       
   147         allrec->Des().Append( torec->Des() );
       
   148         }
       
   149     if( bccrec->Length() != 0 )
       
   150         {
       
   151         if( allrec->Length() != 0 )
       
   152             {
       
   153             allrec->Des().Append( KComma() );
       
   154             }
       
   155         allrec->Des().Append( bccrec->Des() );
       
   156         }
       
   157 
       
   158     if( rec->Length() > 0 )
       
   159         {
       
   160         TChar ch1('?');
       
   161         TChar ch2('&');
       
   162         TChar recchar((*rec)[ rec->Length() - 1]);
       
   163         if( recchar == ch1 )
       
   164             {
       
   165             rec->Des().SetLength(rec->Length() - 1);
       
   166             }
       
   167         TChar recchar2((*rec)[ rec->Length() - 1]);
       
   168         if( recchar2 == ch2 )
       
   169             {
       
   170             rec->Des().SetLength(rec->Length() - 1);
       
   171             }
       
   172         }
       
   173 
       
   174     if( allrec->Length() > 0 )
       
   175         {
       
   176         TChar ch1('?');
       
   177         TChar ch2('&');
       
   178         TChar allrecchar1( (*allrec)[ allrec->Length() - 1] );
       
   179         if( allrecchar1 == ch1 )
       
   180             {
       
   181             allrec->Des().SetLength(allrec->Length() - 1);
       
   182             }
       
   183         TChar allrecchar2( (*allrec)[ allrec->Length() - 1] );
       
   184         if( allrecchar2 == ch2 )
       
   185             {
       
   186             allrec->Des().SetLength(allrec->Length() - 1);
       
   187             }
       
   188         }
       
   189 
       
   190  	TRAPD( err, iTelService->SendEmailMessageL( rec->Des(), allrec->Des(), subject, msgBody, ETrue) );
       
   191 
       
   192     CleanupStack::PopAndDestroy( 5 ); // rec, ccrec, torec, bccrec, allrec
       
   193 
       
   194     NotifyClient();
       
   195 
       
   196     ErrorHandlerL( err );
       
   197 
       
   198 	CLOG_LEAVEFN( "CMailToHandler::HandleUrlEmbeddedL()" );
       
   199 	}
       
   200 
       
   201 // ---------------------------------------------------------
       
   202 // CMailToHandler::HandleUrlStandaloneL()
       
   203 // ---------------------------------------------------------
       
   204 //
       
   205 void CMailToHandler::HandleUrlStandaloneL()
       
   206 	{
       
   207 	CLOG_ENTERFN( "CMailToHandler::HandleUrlStandaloneL()" );
       
   208 
       
   209     LaunchSchemeAppWithCommandLineL();
       
   210 
       
   211 	CLOG_LEAVEFN( "CMailToHandler::HandleUrlStandaloneL()" );
       
   212 	}
       
   213 
       
   214 // ---------------------------------------------------------
       
   215 // CMailToHandler::GetField()
       
   216 // ---------------------------------------------------------
       
   217 //
       
   218 TPtrC CMailToHandler::GetField(const TDesC& aHeader)
       
   219 	{
       
   220 	CLOG_ENTERFN( "CMailToHandler::GetField()" );
       
   221 
       
   222 	TPtrC path = iParsedUrl->Des();
       
   223 
       
   224 	TInt start = 0;
       
   225 	TInt end = 0;
       
   226 
       
   227 	CMailToHandler::TSchemeMailToFields posHeader = GetHeaderPos( aHeader );
       
   228 	// field start from; and end at 
       
   229 	switch( posHeader )
       
   230 		{
       
   231 		case ESchemeMailTo: // MailTo
       
   232 			{
       
   233 			start = FieldStart( KMailto );
       
   234 			end = FieldEnd( KMailto );
       
   235 			break;
       
   236 			}
       
   237 		case ESchemeSubject: // Subject
       
   238 			{
       
   239 			start = FieldStart( KSubject );
       
   240 			end = FieldEnd( KSubject );
       
   241 			break;
       
   242 			}
       
   243 		case ESchemeMsgBody: // Msg Body
       
   244 			{
       
   245 			start = FieldStart( KBody );
       
   246 			end = FieldEnd( KBody );
       
   247 			break;
       
   248 			}
       
   249 		case ESchemeCc: // Cc
       
   250 			{
       
   251 			start = FieldStart( KCc );
       
   252 			end = FieldEnd( KCc );
       
   253 			break;
       
   254 			}
       
   255 		case ESchemeTo: // To
       
   256 			{
       
   257 			start = FieldStart( KTo );
       
   258 			end = FieldEnd( KTo );
       
   259 			break;
       
   260 			}
       
   261 		case ESchemeBcc: // Bcc
       
   262 			{
       
   263 			start = FieldStart( KBcc );
       
   264 			end = FieldEnd( KBcc );
       
   265 			break;
       
   266 			}
       
   267 		case ESchemeNoMore:
       
   268 			break;
       
   269 		}
       
   270 
       
   271     // check that everything is OK
       
   272     if( start > end )
       
   273         {
       
   274         end = path.Length();
       
   275         }
       
   276 	CLOG_LEAVEFN( "CMailToHandler::GetField()" );
       
   277 
       
   278 	return path.Mid( start, end-start );
       
   279 	}
       
   280 
       
   281 // ---------------------------------------------------------
       
   282 // CMailToHandler::IsHeader()
       
   283 // ---------------------------------------------------------
       
   284 //
       
   285 TBool CMailToHandler::IsHeader(const TDesC& aHeader)
       
   286 	{
       
   287 	CLOG_ENTERFN( "CMailToHandler::IsHeader()" );
       
   288 
       
   289 	TBool retVal = EFalse;
       
   290 
       
   291 	TPtrC path = iParsedUrl->Des();
       
   292 
       
   293 	/* is the field in the mailto sheme */
       
   294 	if( KErrNotFound != path.FindF( aHeader ) )
       
   295 		{
       
   296 		retVal = ETrue;
       
   297 		}
       
   298 
       
   299 	CLOG_LEAVEFN( "CMailToHandler::IsHeader()" );
       
   300 
       
   301 	return retVal;
       
   302 	}
       
   303 
       
   304 // ---------------------------------------------------------
       
   305 // CMailToHandler::FieldStart()
       
   306 // ---------------------------------------------------------
       
   307 //
       
   308 TInt CMailToHandler::FieldStart(const TDesC& aHeader)
       
   309 	{
       
   310 	CLOG_ENTERFN( "CMailToHandler::FieldStart()" );
       
   311 
       
   312 	TPtrC path = iParsedUrl->Des();
       
   313 	TInt retVal = path.Length();
       
   314 
       
   315 	/* find the starting position of the specific filed */
       
   316 	if( IsHeader( aHeader ) )
       
   317 		{
       
   318 		retVal = path.FindF( aHeader ) + aHeader.Length();
       
   319 		}
       
   320 
       
   321 	CLOG_LEAVEFN( "CMailToHandler::FieldStart()" );
       
   322 
       
   323 	return retVal;
       
   324 	}
       
   325 
       
   326 // ---------------------------------------------------------
       
   327 // CMailToHandler::FieldEnd()
       
   328 // ---------------------------------------------------------
       
   329 //
       
   330 TInt CMailToHandler::FieldEnd(const TDesC& aHeader)
       
   331 	{
       
   332 	CLOG_ENTERFN( "CMailToHandler::FieldEnd()" );
       
   333 
       
   334 	TPtrC path = iParsedUrl->Des();
       
   335 	TInt length = path.Length(); // length of the scheme
       
   336     TInt retVal = length;
       
   337 
       
   338     TInt startPos = FieldStart( aHeader );
       
   339 
       
   340 	if( IsHeader( aHeader ) )
       
   341 		{
       
   342         TInt temp = GetNextField( startPos );
       
   343         /* we need to subtract 1 if the result is 
       
   344            not equal to length because of the & or ? */
       
   345         retVal = ( temp == length ) ? length : ( temp - 1);
       
   346 		}
       
   347 
       
   348 	CLOG_LEAVEFN( "CMailToHandler::FieldEnd()" );
       
   349 
       
   350 	return retVal;
       
   351 	}
       
   352 
       
   353 // ---------------------------------------------------------
       
   354 // CMailToHandler::GetHeaderPos()
       
   355 // ---------------------------------------------------------
       
   356 //
       
   357 CMailToHandler::TSchemeMailToFields CMailToHandler::GetHeaderPos(const TDesC& aHeader)
       
   358 	{
       
   359 	CLOG_ENTERFN( "CMailToHandler::GetHeaderPos()" );
       
   360 
       
   361 	TSchemeMailToFields retVal = ESchemeMailTo;
       
   362 
       
   363 	if( 0 == aHeader.Compare( KMailto ) )
       
   364 		{
       
   365 		retVal = ESchemeMailTo;
       
   366 		}
       
   367 	else if ( 0 == aHeader.Compare( KSubject ) )
       
   368 		{
       
   369 		retVal = ESchemeSubject;
       
   370 		}
       
   371 	else if ( 0 == aHeader.Compare( KBody ) )
       
   372 		{
       
   373 		retVal = ESchemeMsgBody;
       
   374 		}
       
   375 	else if ( 0 == aHeader.Compare( KCc ) )
       
   376 		{
       
   377 		retVal = ESchemeCc;
       
   378 		}
       
   379 	else if ( 0 == aHeader.Compare( KTo ) )
       
   380 		{
       
   381 		retVal = ESchemeTo;
       
   382 		}
       
   383 	else if ( 0 == aHeader.Compare( KBcc ) )
       
   384 		{
       
   385 		retVal = ESchemeBcc;
       
   386 		}
       
   387 
       
   388 	CLOG_LEAVEFN( "CMailToHandler::GetHeaderPos()" );
       
   389 
       
   390 	return retVal;
       
   391 	}
       
   392 
       
   393 // ---------------------------------------------------------
       
   394 // CMailToHandler::GetNextField()
       
   395 // ---------------------------------------------------------
       
   396 //
       
   397 TInt CMailToHandler::GetNextField( TInt aStart )
       
   398     {
       
   399 	TPtrC path = iParsedUrl->Des();
       
   400 	TInt retVal = path.Length();
       
   401     TPtrC scheme;
       
   402 
       
   403     //KSubject KBody KCc
       
   404     if( aStart < retVal )
       
   405         {
       
   406         scheme.Set( path.Right( retVal - aStart ) );
       
   407         }
       
   408     else
       
   409         {
       
   410         return retVal;
       
   411         }
       
   412 
       
   413     TInt subjPos = scheme.FindF( KSubject );
       
   414     subjPos = ( subjPos == KErrNotFound ) ? retVal : subjPos;
       
   415 
       
   416     TInt bodyPos = scheme.FindF( KBody );
       
   417     bodyPos = ( bodyPos == KErrNotFound ) ? retVal : bodyPos;
       
   418 
       
   419     TInt toPos = scheme.FindF( KTo );
       
   420     toPos = ( toPos == KErrNotFound ) ? retVal : toPos;
       
   421 
       
   422     TInt ccPos = scheme.FindF( KCc );
       
   423     ccPos = ( ccPos == KErrNotFound ) ? retVal : ccPos;
       
   424 
       
   425     TInt bccPos = scheme.FindF( KBcc );
       
   426     bccPos = ( bccPos == KErrNotFound ) ? retVal : bccPos;
       
   427 
       
   428     TInt temp = Minimum( subjPos, bodyPos, toPos, ccPos, bccPos );
       
   429     retVal = ( temp < retVal) ? temp + aStart : retVal;
       
   430 
       
   431     return retVal;
       
   432     }
       
   433 
       
   434 
       
   435 // ---------------------------------------------------------
       
   436 // CMailToHandler::Minimum()
       
   437 // ---------------------------------------------------------
       
   438 //
       
   439 TInt CMailToHandler::Minimum( TInt aPos1, TInt aPos2, TInt aPos3, TInt aPos4, TInt aPos5 )
       
   440     {
       
   441     TInt min = Min( aPos1, aPos2 );
       
   442     min = Min( min, aPos3 );
       
   443     min = Min( min, aPos4 );
       
   444     min = Min( min, aPos5 );
       
   445 
       
   446     return min;
       
   447     }
       
   448 
       
   449 // ---------------------------------------------------------
       
   450 // CMailToHandler::BrowserTelServiceEvent()
       
   451 // ---------------------------------------------------------
       
   452 //
       
   453 void CMailToHandler::BrowserTelServiceEvent( TBrowserTelServiceState /*aEvent*/)
       
   454 	{
       
   455 	}
       
   456 
       
   457 // ---------------------------------------------------------
       
   458 // CMailToHandler::BrowserTelServiceError()
       
   459 // ---------------------------------------------------------
       
   460 //
       
   461 void CMailToHandler::BrowserTelServiceError( TBrowserTelServiceError /*aError*/)
       
   462 	{
       
   463 	}
       
   464 
       
   465 // ---------------------------------------------------------
       
   466 // CMailToHandler::NotifyClient()
       
   467 // ---------------------------------------------------------
       
   468 //
       
   469 void CMailToHandler::NotifyClient()
       
   470 	{
       
   471 	if( NULL !=iSchemeDoc )
       
   472 		{
       
   473         iSchemeDoc->HandleServerAppExit( KErrNone );
       
   474 		}
       
   475 	}
       
   476 
       
   477 // ---------------------------------------------------------
       
   478 // CMailToHandler::ChangeSeparationLC()
       
   479 // ---------------------------------------------------------
       
   480 //
       
   481 HBufC* CMailToHandler::ChangeSeparationLC( const TDesC& aRecipients )
       
   482     {
       
   483     HBufC* newBuffer = aRecipients.AllocLC();
       
   484 
       
   485     TInt pos;
       
   486     while( KErrNotFound != ( pos = newBuffer->FindF( KTo ) ) )
       
   487         {
       
   488         TPtr16 ptr = newBuffer->Des();
       
   489         ptr.Replace( pos, KTo().Length(), KComma );
       
   490         }
       
   491 
       
   492     return newBuffer;
       
   493     }