messagingfw/msgurlhandler/urlhandler/src/MTSCHEME.CPP
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     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 // This file contains the implementation for the class defined in MtScheme.h
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @see	MtScheme.h
       
    21 */
       
    22 
       
    23 // System includes
       
    24 //		
       
    25 #include <txtrich.h>
       
    26 #include <inetprottextutils.h>
       
    27 #include <escapeutils.h>
       
    28 #include <sendas2.h>
       
    29 
       
    30 // Local includes
       
    31 //
       
    32 #include "MTSCHEME.H"
       
    33 #include "msgurlparser.h"
       
    34 
       
    35 // Mailto field name
       
    36 //
       
    37 _LIT(KTo, "to");
       
    38 _LIT(KCc, "cc");
       
    39 _LIT(KBcc, "bcc");
       
    40 _LIT(KSubject, "subject");
       
    41 _LIT(KBody, "body");
       
    42 
       
    43 // Constants
       
    44 //
       
    45 const TUid KMailMtm = {0x10001028};
       
    46 const TInt KEqualChar = '=';
       
    47 
       
    48 
       
    49 CMailtoSchemeHandler::CMailtoSchemeHandler()
       
    50 	{
       
    51 	}
       
    52 
       
    53 CMailtoSchemeHandler* CMailtoSchemeHandler::NewLC()
       
    54 	{
       
    55 	CMailtoSchemeHandler* self=new (ELeave) CMailtoSchemeHandler();
       
    56 	CleanupStack::PushL(self);
       
    57 	self->ConstructL();
       
    58 	return self;
       
    59 	}
       
    60 
       
    61 CMailtoSchemeHandler* CMailtoSchemeHandler::NewL()
       
    62 	{
       
    63 	CMailtoSchemeHandler* self = NewLC();
       
    64 	CleanupStack::Pop(self);
       
    65 	return self;
       
    66 	}
       
    67 
       
    68 void CMailtoSchemeHandler::ConstructL()
       
    69 	{
       
    70 	CMsgSchemeHandlerBase::ConstructL();
       
    71 	}
       
    72 
       
    73 CMailtoSchemeHandler::~CMailtoSchemeHandler()
       
    74 	{
       
    75 	iTo.Close();
       
    76 	delete iMailtoPart;
       
    77 	}
       
    78 
       
    79 void CMailtoSchemeHandler::ParseUrlL(const TDesC& aUrl)
       
    80 	{
       
    81 	// Get the scheme specific part
       
    82 	TPtrC mailtoPart;
       
    83 	GetSchemeSpecificPartL(aUrl, mailtoPart);
       
    84 
       
    85 	// Convert %xx chars in the URL to ascii characters
       
    86 	HBufC* temp = EscapeUtils::EscapeDecodeL(mailtoPart);
       
    87 	delete iMailtoPart;
       
    88 	iMailtoPart = temp;
       
    89 
       
    90 	// Parse the scheme specific part
       
    91 	TMailtoUrlParser parser;
       
    92 	parser.Parse(*iMailtoPart);
       
    93 	TPtrC segment;
       
    94 
       
    95 	// Parse the address segment separated by ','
       
    96 	TDelimitedAddressParser address = parser.ExtractAddresses();
       
    97 	while (address.GetNext(segment) != KErrNotFound)
       
    98 		{
       
    99 		SetFieldL(ETo, segment);
       
   100 		}
       
   101 
       
   102 	// Parse the body segment separated by '&'
       
   103 	TDelimitedBodyParser body = parser.ExtractBody();
       
   104 	while (body.GetNext(segment) != KErrNotFound)
       
   105 		{
       
   106 		TPtrC value;
       
   107 		TField field = GetFieldAndValue(segment, value);
       
   108 		SetFieldL(field, value);
       
   109 		}	
       
   110 	}
       
   111 
       
   112 void CMailtoSchemeHandler::SetFieldL(TField aField, const TDesC& aFieldValue)
       
   113 	{
       
   114 	
       
   115 	TPtrC  value(aFieldValue); 
       
   116 	InetProtTextUtils::RemoveWhiteSpace(value, InetProtTextUtils::ERemoveBoth);
       
   117 
       
   118 	// Set the field values
       
   119 	switch (aField)
       
   120 		{
       
   121 		case ETo:
       
   122 		case ECc:
       
   123 		case EBcc:
       
   124 			User::LeaveIfError(iTo.Append(value));
       
   125 			break;
       
   126 
       
   127 		case ESubject:
       
   128 			__ASSERT_DEBUG(!iSubject.Ptr(), User::Leave(KErrAlreadyExists));
       
   129 			iSubject.Set(value);
       
   130 			break;
       
   131 
       
   132 		case EBody:
       
   133 			__ASSERT_DEBUG(!iBody.Ptr(), User::Leave(KErrAlreadyExists));
       
   134 			iBody.Set(value);
       
   135 			break;
       
   136 
       
   137 		case EUnknown:
       
   138 		default:
       
   139 			break;
       
   140 		}
       
   141 	}
       
   142 
       
   143 CMailtoSchemeHandler::TField CMailtoSchemeHandler::GetFieldAndValue(const TDesC& aComponent, TPtrC& aFieldValue) const
       
   144 	{
       
   145 	// Initialise
       
   146 	TField field = EUnknown;
       
   147 	
       
   148 	// Look for the field separator
       
   149 	TInt pos = aComponent.Locate(KEqualChar);	
       
   150 
       
   151 	if (pos != KErrNotFound)
       
   152 		{
       
   153 		// Set the field name
       
   154 		TPtrC fieldName = aComponent.Left(pos);
       
   155 		InetProtTextUtils::RemoveWhiteSpace(fieldName, InetProtTextUtils::ERemoveBoth);
       
   156 
       
   157 		// Set the field value i.e everything after the equal.
       
   158 		aFieldValue.Set(aComponent.Mid(pos+1));
       
   159 
       
   160 		// Check the field name is valid and set the field.
       
   161 		if (fieldName.CompareF(KTo) == 0)
       
   162 			field = ETo;
       
   163 		else if (fieldName.CompareF(KCc) == 0)
       
   164 			field = ECc;
       
   165 		else if (fieldName.CompareF(KBcc) == 0)
       
   166 			field = EBcc;
       
   167 		else if (fieldName.CompareF(KSubject) == 0)
       
   168 			field = ESubject;
       
   169 		else if (fieldName.CompareF(KBody) == 0)
       
   170 			field = EBody;
       
   171 		}
       
   172 	else 
       
   173 		aFieldValue.Set(KNullDesC);
       
   174 
       
   175 	return field;
       
   176 	}
       
   177 
       
   178 
       
   179 void CMailtoSchemeHandler::SendL()
       
   180 	{
       
   181 	RSendAs sendAs;
       
   182 	CleanupClosePushL(sendAs);
       
   183 	User::LeaveIfError(sendAs.Connect()); // Connect to the SendAs2 server.
       
   184 
       
   185 	RSendAsMessage mailMessage;
       
   186 	CleanupClosePushL(mailMessage);
       
   187 
       
   188 	mailMessage.CreateL(sendAs, KMailMtm);
       
   189 	// Set recipient list
       
   190 	const TInt count = iTo.Count();
       
   191 	for (TInt i=0; i<count; ++i)
       
   192 		{
       
   193 		mailMessage.AddRecipientL(iTo[i], RSendAsMessage::ESendAsRecipientTo);
       
   194 		}
       
   195 	mailMessage.SetSubjectL(iSubject);
       
   196 	mailMessage.SetBodyTextL(iBody);
       
   197 	mailMessage.LaunchEditorAndCloseL();
       
   198 	CleanupStack::Pop(&mailMessage); // Closed by LaunchEditorAndCloseL
       
   199 
       
   200 	CleanupStack::PopAndDestroy(&sendAs);
       
   201 
       
   202 	}