meetingrequest/mrtasks/mrtaskplugin/src/cesmrmailplaitextformatter.cpp
branchRCL_3
changeset 12 4ce476e64c59
child 16 b5fbb9b25d57
equal deleted inserted replaced
11:0396474f30f5 12:4ce476e64c59
       
     1 /*
       
     2 * Copyright (c) 2007-2009 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:  Implementation for plain text formatter for ES MR entries
       
    15 *
       
    16 */
       
    17 
       
    18 #include "emailtrace.h"
       
    19 #include "cesmrmailplaitextformatter.h"
       
    20 
       
    21 #include <esmrtasks.rsg>
       
    22 #include <cmrmailboxutils.h>
       
    23 #include <stringloader.h>
       
    24 #include <calentry.h>
       
    25 #include <caluser.h>
       
    26 #include <avkon.rsg>
       
    27 #include <utf.h>
       
    28 #include <data_caging_path_literals.hrh>
       
    29 #include "cesmrcaluserutil.h"
       
    30 #include "esmrhelper.h"
       
    31 #include <bautils.h>
       
    32 
       
    33 // Unnamed namespace for local definitions
       
    34 namespace  {
       
    35 
       
    36 // Resource file name definition
       
    37 _LIT( KResourceFileName,"esmrtasks.rsc" );
       
    38 
       
    39 // Definition for new line
       
    40 _LIT( KNewLine,"\n" );
       
    41 
       
    42 _LIT( KWhiteSpace, " " );
       
    43 
       
    44 // Definition for first position
       
    45 const TInt KFirstPos( 0 );
       
    46 
       
    47 // Denition for amount of needed newlines
       
    48 const TInt KNewLinesNeeded = 5;
       
    49 
       
    50 }//namespace
       
    51 
       
    52 // ======== MEMBER FUNCTIONS ========
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // CESMRMailPlainTextFormatter::CESMRMailPlainTextFormatter
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 inline CESMRMailPlainTextFormatter::CESMRMailPlainTextFormatter(
       
    59         CMRMailboxUtils& aMailboxUtils ) :
       
    60     iMailboxUtils(aMailboxUtils)
       
    61     {
       
    62     FUNC_LOG;
       
    63     //do nothing
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // CESMRMailPlainTextFormatter::~CESMRMailPlainTextFormatter
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 CESMRMailPlainTextFormatter::~CESMRMailPlainTextFormatter()
       
    71     {
       
    72     FUNC_LOG;
       
    73     if ( iResourceOffset )
       
    74         {
       
    75         CCoeEnv::Static()->DeleteResourceFile( iResourceOffset );
       
    76         }
       
    77     }
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // CESMRMailPlainTextFormatter::NewL
       
    81 // ---------------------------------------------------------------------------
       
    82 //
       
    83 CESMRMailPlainTextFormatter* CESMRMailPlainTextFormatter::NewL(
       
    84         CMRMailboxUtils& aMailboxUtils )
       
    85     {
       
    86     FUNC_LOG;
       
    87     CESMRMailPlainTextFormatter* self = NewLC( aMailboxUtils );
       
    88     CleanupStack::Pop( self );
       
    89     return self;
       
    90     }
       
    91 
       
    92 // ---------------------------------------------------------------------------
       
    93 // CESMRMailPlainTextFormatter::NewLC
       
    94 // ---------------------------------------------------------------------------
       
    95 //
       
    96 CESMRMailPlainTextFormatter* CESMRMailPlainTextFormatter::NewLC(
       
    97         CMRMailboxUtils& aMailboxUtils )
       
    98     {
       
    99     FUNC_LOG;
       
   100     CESMRMailPlainTextFormatter* self =
       
   101         new (ELeave) CESMRMailPlainTextFormatter(aMailboxUtils);
       
   102     CleanupStack::PushL( self );
       
   103     self->ConstructL();
       
   104     return self;
       
   105     }
       
   106 
       
   107 
       
   108 // ---------------------------------------------------------------------------
       
   109 // CESMRMailPlainTextFormatter::ConstructL
       
   110 // ---------------------------------------------------------------------------
       
   111 //
       
   112 void CESMRMailPlainTextFormatter::ConstructL()
       
   113     {
       
   114     FUNC_LOG;
       
   115     CCoeEnv* coeEnv = CCoeEnv::Static();// codescanner::eikonenvstatic
       
   116 
       
   117     TFileName resourceFile;
       
   118     ESMRHelper::LocateResourceFile(
       
   119             KResourceFileName,
       
   120             KDC_RESOURCE_FILES_DIR,
       
   121             resourceFile,
       
   122             &coeEnv->FsSession());
       
   123 
       
   124     // Find the resource file for the nearest language
       
   125     BaflUtils::NearestLanguageFile(coeEnv->FsSession(), resourceFile );
       
   126     iResourceOffset = coeEnv->AddResourceFileL( resourceFile );
       
   127     }
       
   128 
       
   129 // ---------------------------------------------------------------------------
       
   130 // CESMRMailPlainTextFormatter::Body16LC
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 HBufC* CESMRMailPlainTextFormatter::Body16LC(
       
   134         CCalEntry& aEntry )
       
   135     {
       
   136     FUNC_LOG;
       
   137 
       
   138     HBufC* details =
       
   139         HBufC::NewLC( aEntry.DescriptionL().Length() +
       
   140                       KNewLine().Length() * KNewLinesNeeded +
       
   141                       KWhiteSpace().Length() * 2 ); 
       
   142 
       
   143     TPtr detailsPtr( details->Des() );
       
   144 
       
   145     detailsPtr.Append( aEntry.DescriptionL() );
       
   146     detailsPtr.Append( KNewLine );
       
   147 
       
   148     return details;
       
   149     }
       
   150 
       
   151 // ---------------------------------------------------------------------------
       
   152 // CESMRMailPlainTextFormatter::Body8LC
       
   153 // ---------------------------------------------------------------------------
       
   154 //
       
   155 HBufC8* CESMRMailPlainTextFormatter::Body8LC(
       
   156         CCalEntry& aEntry )
       
   157     {
       
   158     FUNC_LOG;
       
   159     HBufC* buf16 = Body16LC( aEntry );
       
   160     HBufC8* buf8 = HBufC8::NewLC( buf16->Length() );
       
   161     TPtr8 ptr8( buf8->Des() );
       
   162 
       
   163     TInt err = CnvUtfConverter::ConvertFromUnicodeToUtf8( ptr8, *buf16 );
       
   164     User::LeaveIfError( err );
       
   165     CleanupStack::Pop( buf8 );
       
   166     CleanupStack::PopAndDestroy( buf16 );
       
   167     CleanupStack::PushL( buf8 );
       
   168     return buf8;
       
   169     }
       
   170 
       
   171 // ---------------------------------------------------------------------------
       
   172 // CESMRMailPlainTextFormatter::Subject16LC
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 HBufC* CESMRMailPlainTextFormatter::Subject16LC(
       
   176             CCalEntry& aEntry,
       
   177             TBool aIsForwarded,
       
   178             TBool aIsUpdate )
       
   179     {
       
   180     FUNC_LOG;
       
   181     CESMRCalUserUtil* caluserUtil = CESMRCalUserUtil::NewLC( aEntry );
       
   182     TESMRRole role = caluserUtil->PhoneOwnerRoleL();
       
   183     CleanupStack::PopAndDestroy( caluserUtil );
       
   184 
       
   185     // create prefix for subject:
       
   186     HBufC* prefix = SubjectLinePrefix16LC(
       
   187                             aEntry,
       
   188                             aIsForwarded,
       
   189                             aIsUpdate ); //if returns NULL no need to CleanupStack::Pop later
       
   190 
       
   191     // user's email address is attached to subject if user is
       
   192     // responding to meeting request:
       
   193     HBufC* responseAddress = KNullDesC().AllocLC();
       
   194     if ( !aIsForwarded )
       
   195         {
       
   196         if ( EESMRRoleRequiredAttendee == role ||
       
   197              EESMRRoleOptionalAttendee == role ||
       
   198              EESMRRoleNonParticipant == role  )
       
   199              {
       
   200              CleanupStack::PopAndDestroy( responseAddress );
       
   201              responseAddress = NULL;
       
   202 
       
   203              CCalUser* phoneOwner = aEntry.PhoneOwnerL();
       
   204              responseAddress = phoneOwner->Address().AllocLC();
       
   205              }
       
   206         }
       
   207 
       
   208     // count the subject line buffer length
       
   209     TInt subjectLineLength( aEntry.SummaryL().Length() );
       
   210 
       
   211     if (prefix)
       
   212         {
       
   213         subjectLineLength += prefix->Length();
       
   214         }
       
   215 
       
   216     if (responseAddress)
       
   217         {
       
   218         subjectLineLength += responseAddress->Length();
       
   219         }
       
   220 
       
   221     // create and fill the subject buffer
       
   222     HBufC* subjectLine =
       
   223         HBufC::NewLC( subjectLineLength );
       
   224     TPtr ptrSubject( subjectLine->Des() );
       
   225 
       
   226      if (responseAddress)
       
   227         {
       
   228         ptrSubject.Append(*responseAddress);
       
   229         }
       
   230 
       
   231      if (prefix)
       
   232         {
       
   233         ptrSubject.Copy( *prefix );
       
   234         }
       
   235 
       
   236     ptrSubject.Append( aEntry.SummaryL() );
       
   237 
       
   238     CleanupStack::Pop( subjectLine );
       
   239 
       
   240     if (prefix)
       
   241         {
       
   242         CleanupStack::PopAndDestroy( 2, prefix ); // responseAddress
       
   243         }
       
   244     else
       
   245         {
       
   246         CleanupStack::PopAndDestroy( responseAddress );
       
   247         }
       
   248     CleanupStack::PushL( subjectLine );
       
   249     return subjectLine;
       
   250     }
       
   251 
       
   252 // ---------------------------------------------------------------------------
       
   253 // CESMRMailPlainTextFormatter::Subject8LC
       
   254 // ---------------------------------------------------------------------------
       
   255 //
       
   256 HBufC8* CESMRMailPlainTextFormatter::Subject8LC(
       
   257             CCalEntry& aEntry,
       
   258             TBool aIsForwarded,
       
   259             TBool aIsUpdate  )
       
   260     {
       
   261     FUNC_LOG;
       
   262     HBufC* buf16 = Subject16LC( aEntry, aIsForwarded, aIsUpdate );
       
   263     HBufC8* buf8 = HBufC8::NewLC( buf16->Length() );
       
   264     TPtr8 ptr8(buf8->Des() );
       
   265 
       
   266     TInt err = CnvUtfConverter::ConvertFromUnicodeToUtf8( ptr8, *buf16 );
       
   267     User::LeaveIfError( err );
       
   268     CleanupStack::Pop( buf8 );
       
   269     CleanupStack::PopAndDestroy( buf16 );
       
   270     CleanupStack::PushL( buf8 );
       
   271     return buf8;
       
   272     }
       
   273 
       
   274 // ---------------------------------------------------------------------------
       
   275 // CESMRMailPlainTextFormatter::SubjectLinePrefix16LC
       
   276 // ---------------------------------------------------------------------------
       
   277 //
       
   278 HBufC* CESMRMailPlainTextFormatter::SubjectLinePrefix16LC(
       
   279         CCalEntry& aEntry,
       
   280         TBool aIsForwarded,
       
   281         TBool aIsUpdate  )
       
   282     {
       
   283     FUNC_LOG;
       
   284     HBufC* prefix = NULL;
       
   285 
       
   286     CESMRCalUserUtil* caluserUtil = CESMRCalUserUtil::NewLC( aEntry );
       
   287     TESMRRole role = caluserUtil->PhoneOwnerRoleL();
       
   288     CleanupStack::PopAndDestroy( caluserUtil );
       
   289     caluserUtil = NULL;
       
   290 
       
   291     if ( EESMRRoleOrganizer == role )
       
   292         {
       
   293         CCalEntry::TMethod method = aEntry.MethodL();
       
   294         if ( CCalEntry::EMethodCancel == method )
       
   295             {
       
   296             prefix =
       
   297                 StringLoader::LoadLC( R_QTN_MEET_REQ_PLAIN_TEXT_CANCELLED );
       
   298             }
       
   299         else if ( CCalEntry::EMethodRequest == method  &&
       
   300                    aIsForwarded )
       
   301             {
       
   302             prefix =
       
   303                 StringLoader::LoadLC( R_QTN_MEET_REQ_PLAIN_TEXT_FORWARDED );
       
   304             }
       
   305         else if ( CCalEntry::EMethodRequest == method  &&
       
   306                   aIsUpdate )
       
   307             {
       
   308             prefix =
       
   309                 StringLoader::LoadLC( R_QTN_MEET_REQ_PLAIN_TEXT_UPDATE );
       
   310             }
       
   311         else
       
   312             {
       
   313             prefix =
       
   314                 KNullDesC().AllocLC();
       
   315             }
       
   316         }
       
   317 
       
   318     else if ( EESMRRoleRequiredAttendee == role ||
       
   319               EESMRRoleOptionalAttendee == role ||
       
   320               EESMRRoleNonParticipant == role )
       
   321         {
       
   322         if( aIsForwarded )
       
   323         	{
       
   324         	prefix =
       
   325         	StringLoader::LoadLC( R_QTN_MEET_REQ_PLAIN_TEXT_FORWARDED );
       
   326         	}
       
   327         else 
       
   328         	{
       
   329             CCalAttendee* attendee =
       
   330             iMailboxUtils.ThisAttendeeL( aEntry );
       
   331         	if ( attendee )
       
   332         		{
       
   333         		switch( attendee->StatusL() )
       
   334         			{
       
   335         			case CCalAttendee::EAccepted:
       
   336         				prefix =
       
   337         				StringLoader::LoadLC(
       
   338         						R_QTN_MEET_REQ_PLAIN_TEXT_ACCEPTED );
       
   339         				break;
       
   340         			case CCalAttendee::ETentative:
       
   341         				prefix =
       
   342         				StringLoader::LoadLC(
       
   343         						R_QTN_MEET_REQ_PLAIN_TEXT_TENTATIVE );
       
   344         				break;
       
   345         			case CCalAttendee::EDeclined:
       
   346         				prefix =
       
   347         				StringLoader::LoadLC(
       
   348         						R_QTN_MEET_REQ_PLAIN_TEXT_DECLINED );
       
   349         				break;
       
   350         			default:
       
   351         				prefix =
       
   352         				KNullDesC().AllocLC();
       
   353         				break;
       
   354         			}
       
   355         		}
       
   356         	else
       
   357         		{
       
   358         		prefix =
       
   359         		KNullDesC().AllocLC();
       
   360         		}
       
   361         	}
       
   362         }
       
   363     return prefix;
       
   364     }
       
   365 
       
   366 // ---------------------------------------------------------------------------
       
   367 // CESMRMailPlainTextFormatter::UpdatedStringLC
       
   368 // ---------------------------------------------------------------------------
       
   369 //
       
   370 HBufC* CESMRMailPlainTextFormatter::UpdatedStringLC()
       
   371     {
       
   372     FUNC_LOG;
       
   373     HBufC* updatedString =
       
   374             StringLoader::LoadLC( R_QTN_MEET_REQ_PLAIN_TEXT_UPDATE );
       
   375 
       
   376     return updatedString;
       
   377     }
       
   378 
       
   379 // ---------------------------------------------------------------------------
       
   380 // CESMRMailPlainTextFormatter::CanceledStringLC
       
   381 // ---------------------------------------------------------------------------
       
   382 //
       
   383 HBufC* CESMRMailPlainTextFormatter::CanceledStringLC()
       
   384     {
       
   385     FUNC_LOG;
       
   386     HBufC* canceledString =
       
   387             StringLoader::LoadLC( R_QTN_MEET_REQ_PLAIN_TEXT_CANCELLED );
       
   388 
       
   389     return canceledString;
       
   390     }
       
   391 
       
   392 // ---------------------------------------------------------------------------
       
   393 // CESMRMailPlainTextFormatter::ReplyStringLC
       
   394 // ---------------------------------------------------------------------------
       
   395 //
       
   396 HBufC* CESMRMailPlainTextFormatter::ReplyStringLC( CCalEntry& aEntry )
       
   397     {
       
   398     FUNC_LOG;
       
   399     HBufC* replyString = NULL;
       
   400     HBufC* replyStringPrefix =
       
   401             StringLoader::LoadLC( R_QTN_MEET_REQ_VIEWER_SUBJECT_PREFIX_REPL );
       
   402     
       
   403     TPtrC subject( aEntry.SummaryL() );
       
   404     
       
   405     TInt pos = subject.Find( *replyStringPrefix );
       
   406     if ( KErrNotFound == pos || KFirstPos != pos )
       
   407         {
       
   408         // There is no reply prefix already or reply prefix is not at the 
       
   409         // beginning --> We need to add it to description
       
   410 
       
   411         // No need to put into cleanupstack because calling only non leaving 
       
   412         // functions before exiting
       
   413         replyString = HBufC::NewL( 
       
   414                             replyStringPrefix->Length() + subject.Length() );
       
   415         
       
   416         TPtr ptrReply( replyString->Des() );
       
   417         
       
   418         ptrReply.Copy( *replyStringPrefix );
       
   419         ptrReply.Append( subject );
       
   420         }
       
   421     else
       
   422         {
       
   423         replyString = subject.AllocL();
       
   424         }
       
   425     
       
   426     CleanupStack::PopAndDestroy( replyStringPrefix );
       
   427     CleanupStack::PushL( replyString );
       
   428     
       
   429     return replyString;
       
   430     }
       
   431 
       
   432 // EOF