messagingappbase/schemeplugin/src/SmsHandler.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 /*
       
     2 * Copyright (c) 2004 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 "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 
       
    16 *      for sms:// scheme
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 
       
    22 // INCLUDE FILES
       
    23 
       
    24 #include "SmsHandler.h"
       
    25 #include "SchemeDefs.h"
       
    26 #include "SchemeDispLogger.h"
       
    27 #include <ecom.h>		// For REComSession
       
    28 #include <eikenv.h>
       
    29 #include <DocumentHandler.h>
       
    30 #include <apgcli.h>
       
    31 #include <apparc.h>
       
    32 #include <eikdoc.h>
       
    33 #include <eikproc.h>
       
    34 #include <f32file.h>
       
    35 #include <sendui.h> // CSendUi
       
    36 #include <SenduiMtmUids.h> 
       
    37 #include <txtrich.h>
       
    38 #include <CMessageData.h>
       
    39 
       
    40 
       
    41 // ================= CONSTANTS =======================
       
    42 
       
    43 // ================= MEMBER FUNCTIONS =======================
       
    44 
       
    45 // ---------------------------------------------------------
       
    46 // CSmsHandler::NewL()
       
    47 // ---------------------------------------------------------
       
    48 //
       
    49 CSmsHandler* CSmsHandler::NewL( const TDesC& aUrl )
       
    50 	{
       
    51 	CLOG_ENTERFN( "CSmsHandler::NewL()" );
       
    52 
       
    53 	CSmsHandler* self = new( ELeave ) CSmsHandler();
       
    54 	CleanupStack::PushL( self );
       
    55 	self->ConstructL( aUrl );
       
    56 	CleanupStack::Pop( self );
       
    57 
       
    58 	CLOG_LEAVEFN( "CSmsHandler::NewL()" );
       
    59 
       
    60 	return self;
       
    61 	}
       
    62 
       
    63 // ---------------------------------------------------------
       
    64 // CSmsHandler::~CSmsHandler()
       
    65 // ---------------------------------------------------------
       
    66 //
       
    67 CSmsHandler::~CSmsHandler()
       
    68 	{
       
    69 	CLOG_ENTERFN( "CSmsHandler::~CSmsHandler()" );
       
    70     delete iSendUi;
       
    71 
       
    72 	CLOG_LEAVEFN( "CSmsHandler::~CSmsHandler()" );
       
    73 	}
       
    74 
       
    75 // ---------------------------------------------------------
       
    76 // CSmsHandler::CSmsHandler()
       
    77 // ---------------------------------------------------------
       
    78 //
       
    79 CSmsHandler::CSmsHandler() : CBaseHandler()
       
    80 	{
       
    81 	// Deliberately do nothing here : 
       
    82 	// See ConstructL() for initialisation completion.
       
    83 	}
       
    84 
       
    85 // ---------------------------------------------------------
       
    86 // CSmsHandler::ConstructL()
       
    87 // ---------------------------------------------------------
       
    88 //
       
    89 void CSmsHandler::ConstructL( const TDesC& aUrl )
       
    90 	{
       
    91 	CLOG_ENTERFN( "CSmsHandler::ConstructL()" );
       
    92 
       
    93 	BaseConstructL( aUrl );
       
    94 	
       
    95 	CLOG_LEAVEFN( "CSmsHandler::ConstructL()" );
       
    96 	}
       
    97 
       
    98 // ---------------------------------------------------------
       
    99 // CSmsHandler::HandleUrlEmbeddedL()
       
   100 // ---------------------------------------------------------
       
   101 //
       
   102 void CSmsHandler::HandleUrlEmbeddedL()
       
   103 	{
       
   104 	CLOG_ENTERFN( "CSmsHandler::HandleUrlEmbeddedL()" );
       
   105     HandleUrlL( ETrue );    
       
   106 	CLOG_LEAVEFN( "CSmsHandler::HandleUrlEmbeddedL()" );
       
   107 	}
       
   108 
       
   109 // ---------------------------------------------------------
       
   110 // CSmsHandler::HandleUrlStandaloneL()
       
   111 // ---------------------------------------------------------
       
   112 //
       
   113 void CSmsHandler::HandleUrlStandaloneL()
       
   114 	{
       
   115 	CLOG_ENTERFN( "CSmsHandler::HandleUrlStandaloneL()" );
       
   116     LaunchSchemeAppWithCommandLineL();   
       
   117 	CLOG_LEAVEFN( "CSmsHandler::HandleUrlStandaloneL()" );
       
   118 	}
       
   119     
       
   120 // ---------------------------------------------------------
       
   121 // CSmsHandler::HandleUrlL()
       
   122 // ---------------------------------------------------------
       
   123 //
       
   124 void CSmsHandler::HandleUrlL(TBool aLaunchEmbedded)
       
   125     {
       
   126     CMessageData* messageData = CMessageData::NewLC();
       
   127 
       
   128     // Parse url
       
   129 	TPtrC recipient = GetField( KSms );
       
   130     // To is supported because of localapp scheme
       
   131     TPtrC to = GetField( KTo );
       
   132 	TPtrC msgBody = GetField( KBody );
       
   133 
       
   134     // Create richtext format for body
       
   135     CParaFormatLayer* paraFormat = CParaFormatLayer::NewL();
       
   136     CleanupStack::PushL( paraFormat );
       
   137 
       
   138     CCharFormatLayer* charFormat = CCharFormatLayer::NewL(); 
       
   139     CleanupStack::PushL( charFormat );
       
   140 
       
   141     CRichText* body = CRichText::NewL( paraFormat, charFormat );
       
   142     CleanupStack::PushL( body );
       
   143     if( msgBody.Length() )
       
   144 	    {
       
   145         body->InsertL( 0, msgBody );
       
   146 	    }
       
   147     else
       
   148 	    {
       
   149         body->InsertL( 0, KNullDesC );
       
   150 	    }
       
   151     messageData->SetBodyTextL(body);
       
   152 
       
   153     // Add addresses to messageData
       
   154     if( recipient.Length() )
       
   155 		{
       
   156 		SeparateAndAppendAddressL(KComma, recipient, *messageData, KToRecipient );
       
   157 		}
       
   158     if( to.Length() )
       
   159         {
       
   160         SeparateAndAppendAddressL(KComma, to, *messageData, KToRecipient );
       
   161         }
       
   162 	
       
   163     if ( !iSendUi )
       
   164         {
       
   165         iSendUi = CSendUi::NewL();
       
   166         }
       
   167 
       
   168     iSendUi->CreateAndSendMessageL(
       
   169         KSenduiMtmSmsUid, messageData, KNullUid, aLaunchEmbedded);
       
   170 
       
   171     CleanupStack::PopAndDestroy( 4, messageData );
       
   172     NotifyClient();
       
   173     }
       
   174 
       
   175 // ---------------------------------------------------------
       
   176 // CSmsHandler::GetField()
       
   177 // ---------------------------------------------------------
       
   178 //
       
   179 TPtrC CSmsHandler::GetField(const TDesC& aHeader)
       
   180 	{
       
   181 	CLOG_ENTERFN( "CSmsHandler::GetField()" );
       
   182 
       
   183 	TPtrC path = iParsedUrl->Des();
       
   184 
       
   185 	TInt start = 0;
       
   186 	TInt end = 0;
       
   187 
       
   188 	CSmsHandler::TSchemeSmsFields posHeader = GetHeaderPos( aHeader );
       
   189 	// field start from; and end at 
       
   190 	switch( posHeader )
       
   191 		{
       
   192 		case ESchemeSms: // Sms
       
   193 			{
       
   194 			start = FieldStart( KSms );
       
   195 			end = FieldEnd( KSms );
       
   196 			break;
       
   197 			}
       
   198 		case ESchemeMsgBody: // Msg Body
       
   199 			{
       
   200 			start = FieldStart( KBody );
       
   201 			end = FieldEnd( KBody );
       
   202 			break;
       
   203 			}
       
   204         case ESchemeTo:
       
   205             {
       
   206             start = FieldStart( KTo );
       
   207 			end = FieldEnd( KTo );
       
   208 			break;
       
   209             }
       
   210 		case ESchemeNoMore:
       
   211 			{
       
   212 			break;
       
   213 			}
       
   214 		default:
       
   215 			{
       
   216 			break;
       
   217 			}
       
   218 		}
       
   219 
       
   220 	CLOG_LEAVEFN( "CSmsHandler::GetField()" );
       
   221 
       
   222 	return path.Mid( start, end-start );
       
   223 	}
       
   224 
       
   225 // ---------------------------------------------------------
       
   226 // CSmsHandler::IsHeader()
       
   227 // ---------------------------------------------------------
       
   228 //
       
   229 TBool CSmsHandler::IsHeader(const TDesC& aHeader)
       
   230 	{
       
   231 	CLOG_ENTERFN( "CSmsHandler::IsHeader()" );
       
   232 
       
   233 	TBool retVal = EFalse;
       
   234 
       
   235 	TPtrC path = iParsedUrl->Des();
       
   236 
       
   237 	// is the field in the mmsto scheme 
       
   238 	if( KErrNotFound != path.FindF( aHeader ) )
       
   239 		{
       
   240 		retVal = ETrue;
       
   241 		}
       
   242 
       
   243 	CLOG_LEAVEFN( "CSmsHandler::IsHeader()" );
       
   244 
       
   245 	return retVal;
       
   246 	}
       
   247 
       
   248 // ---------------------------------------------------------
       
   249 // CSmsHandler::FieldStart()
       
   250 // ---------------------------------------------------------
       
   251 //
       
   252 TInt CSmsHandler::FieldStart(const TDesC& aHeader)
       
   253 	{
       
   254 	CLOG_ENTERFN( "CSmsHandler::FieldStart()" );
       
   255 
       
   256 	TPtrC path = iParsedUrl->Des();
       
   257 	TInt retVal = path.Length();
       
   258 
       
   259 	// find the starting position of the specific filed
       
   260 	if( IsHeader( aHeader ) )
       
   261 		{
       
   262 		retVal = path.FindF( aHeader ) + aHeader.Length();
       
   263 		}
       
   264 
       
   265 	CLOG_LEAVEFN( "CSmsHandler::FieldStart()" );
       
   266 
       
   267 	return retVal;
       
   268 	}
       
   269 
       
   270 // ---------------------------------------------------------
       
   271 // CSmsHandler::FieldEnd()
       
   272 // ---------------------------------------------------------
       
   273 //
       
   274 TInt CSmsHandler::FieldEnd(const TDesC& aHeader)
       
   275 	{
       
   276 	CLOG_ENTERFN( "CSmsHandler::FieldEnd()" );
       
   277 
       
   278 	TPtrC path = iParsedUrl->Des();
       
   279 	TInt length = path.Length(); // length of the scheme
       
   280     TInt retVal = length;
       
   281 
       
   282     TInt startPos = FieldStart( aHeader );
       
   283 
       
   284     if( IsHeader( aHeader ) )
       
   285 		{
       
   286         TInt temp = GetNextField( startPos );
       
   287         /* we need to subtract 1 if the result is 
       
   288            not equal to length because of the & or ? */
       
   289         retVal = ( temp == length ) ? length : ( temp - 1);
       
   290 		}
       
   291 
       
   292 	CLOG_LEAVEFN( "CSmsHandler::FieldEnd()" );
       
   293 
       
   294 	return retVal;
       
   295 	}
       
   296 
       
   297 // ---------------------------------------------------------
       
   298 // CSmsHandler::GetHeaderPos()
       
   299 // ---------------------------------------------------------
       
   300 //
       
   301 CSmsHandler::TSchemeSmsFields CSmsHandler::GetHeaderPos(const TDesC& aHeader)
       
   302 	{
       
   303 	CLOG_ENTERFN( "CSmsHandler::GetHeaderPos()" );
       
   304 
       
   305 	TSchemeSmsFields retVal = ESchemeSms;
       
   306 
       
   307 	if( 0 == aHeader.Compare( KSms ) )
       
   308 		{
       
   309 		retVal = ESchemeSms;
       
   310 		}
       
   311 	else if ( 0 == aHeader.Compare( KBody ) )
       
   312 		{
       
   313 		retVal = ESchemeMsgBody;
       
   314 		}
       
   315     else if ( 0 == aHeader.Compare( KTo ) )
       
   316 		{
       
   317 		retVal = ESchemeTo;
       
   318 		}
       
   319     else
       
   320         {
       
   321         }
       
   322 
       
   323 	CLOG_LEAVEFN( "CSmsHandler::GetHeaderPos()" );
       
   324 	return retVal;
       
   325 	}
       
   326 
       
   327 // ---------------------------------------------------------
       
   328 // CSmsHandler::GetNextField()
       
   329 // ---------------------------------------------------------
       
   330 //
       
   331 TInt CSmsHandler::GetNextField( TInt aStart )
       
   332     {
       
   333 	TPtrC path = iParsedUrl->Des();
       
   334 	TInt retVal = path.Length();
       
   335     TPtrC scheme;
       
   336     RArray<TInt> array;
       
   337 
       
   338     if( aStart < retVal )
       
   339         {
       
   340         scheme.Set( path.Right( retVal - aStart ) );
       
   341         }
       
   342     else
       
   343         {
       
   344         return retVal;
       
   345         }
       
   346 
       
   347     TInt bodyPos = scheme.FindF( KBody );
       
   348     bodyPos = ( bodyPos == KErrNotFound ) ? retVal : bodyPos;
       
   349     array.Append( bodyPos );
       
   350 
       
   351     TInt toPos = scheme.FindF( KTo );
       
   352     toPos = ( toPos == KErrNotFound ) ? retVal : toPos;
       
   353     array.Append( toPos );   
       
   354 
       
   355     //retVal = ( bodyPos < retVal) ? bodyPos + aStart : retVal;
       
   356     TInt temp = Minimum( array );
       
   357     retVal = ( temp < retVal) ? temp + aStart : retVal;
       
   358 
       
   359     return retVal;
       
   360     }
       
   361 
       
   362 //  End of File
       
   363