meetingrequest/mrtasks/src/cesmrmailplaitextformatter.cpp
branchRCL_3
changeset 33 da5135c61bad
parent 32 a3a1ae9acec6
child 34 cd2816114bd1
equal deleted inserted replaced
32:a3a1ae9acec6 33:da5135c61bad
     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 // Definition for time string length (13:15)
       
    51 const TInt KTimeStringLength = 20;
       
    52 
       
    53 // Definition for date stinng length (31.12.2007)
       
    54 const TInt KDateStringLength = 20;
       
    55 
       
    56 }//namespace
       
    57 
       
    58 // ======== MEMBER FUNCTIONS ========
       
    59 
       
    60 // ---------------------------------------------------------------------------
       
    61 // CESMRMailPlainTextFormatter::CESMRMailPlainTextFormatter
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 inline CESMRMailPlainTextFormatter::CESMRMailPlainTextFormatter(
       
    65         CMRMailboxUtils& aMailboxUtils ) :
       
    66     iMailboxUtils(aMailboxUtils)
       
    67     {
       
    68     FUNC_LOG;
       
    69     //do nothing
       
    70     }
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // CESMRMailPlainTextFormatter::~CESMRMailPlainTextFormatter
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 CESMRMailPlainTextFormatter::~CESMRMailPlainTextFormatter()
       
    77     {
       
    78     FUNC_LOG;
       
    79     if ( iResourceOffset )
       
    80         {
       
    81         CCoeEnv::Static()->DeleteResourceFile( iResourceOffset );
       
    82         }
       
    83     }
       
    84 
       
    85 // ---------------------------------------------------------------------------
       
    86 // CESMRMailPlainTextFormatter::NewL
       
    87 // ---------------------------------------------------------------------------
       
    88 //
       
    89 CESMRMailPlainTextFormatter* CESMRMailPlainTextFormatter::NewL(
       
    90         CMRMailboxUtils& aMailboxUtils )
       
    91     {
       
    92     FUNC_LOG;
       
    93     CESMRMailPlainTextFormatter* self = NewLC( aMailboxUtils );
       
    94     CleanupStack::Pop( self );
       
    95     return self;
       
    96     }
       
    97 
       
    98 // ---------------------------------------------------------------------------
       
    99 // CESMRMailPlainTextFormatter::NewLC
       
   100 // ---------------------------------------------------------------------------
       
   101 //
       
   102 CESMRMailPlainTextFormatter* CESMRMailPlainTextFormatter::NewLC(
       
   103         CMRMailboxUtils& aMailboxUtils )
       
   104     {
       
   105     FUNC_LOG;
       
   106     CESMRMailPlainTextFormatter* self =
       
   107         new (ELeave) CESMRMailPlainTextFormatter(aMailboxUtils);
       
   108     CleanupStack::PushL( self );
       
   109     self->ConstructL();
       
   110     return self;
       
   111     }
       
   112 
       
   113 
       
   114 // ---------------------------------------------------------------------------
       
   115 // CESMRMailPlainTextFormatter::ConstructL
       
   116 // ---------------------------------------------------------------------------
       
   117 //
       
   118 void CESMRMailPlainTextFormatter::ConstructL()
       
   119     {
       
   120     FUNC_LOG;
       
   121     CCoeEnv* coeEnv = CCoeEnv::Static();// codescanner::eikonenvstatic
       
   122 
       
   123     TFileName resourceFile;
       
   124     ESMRHelper::LocateResourceFile(
       
   125             KResourceFileName,
       
   126             KDC_RESOURCE_FILES_DIR,
       
   127             resourceFile,
       
   128             &coeEnv->FsSession());
       
   129 
       
   130     // Find the resource file for the nearest language
       
   131     BaflUtils::NearestLanguageFile(coeEnv->FsSession(), resourceFile );
       
   132     iResourceOffset = coeEnv->AddResourceFileL( resourceFile );
       
   133     }
       
   134 
       
   135 // ---------------------------------------------------------------------------
       
   136 // CESMRMailPlainTextFormatter::Body16LC
       
   137 // ---------------------------------------------------------------------------
       
   138 //
       
   139 HBufC* CESMRMailPlainTextFormatter::Body16LC(
       
   140         CCalEntry& aEntry )
       
   141     {
       
   142     FUNC_LOG;
       
   143     HBufC* dateFormatString = StringLoader::LoadLC( R_QTN_DATE_USUAL );
       
   144 
       
   145     HBufC* timeFormatString = StringLoader::LoadLC( R_QTN_TIME_USUAL );
       
   146 
       
   147     TTime mrStartTime = aEntry.StartTimeL().TimeLocalL();
       
   148     TTime mrEndTime   = aEntry.EndTimeL().TimeLocalL();
       
   149 
       
   150     TBuf<KTimeStringLength> startTimeStr, endTimeStr;
       
   151     TBuf<KDateStringLength> startDateStr, endDateStr;
       
   152 
       
   153     mrStartTime.FormatL( startTimeStr, *timeFormatString );
       
   154     mrStartTime.FormatL( startDateStr, *dateFormatString );
       
   155     mrEndTime.FormatL( endTimeStr, *timeFormatString );
       
   156     mrEndTime.FormatL( endDateStr, *dateFormatString );
       
   157 
       
   158     CleanupStack::PopAndDestroy( 2, dateFormatString );
       
   159 
       
   160     HBufC* subject =
       
   161         StringLoader::LoadLC(R_QTN_MEET_REQ_PLAIN_TEXT_SUBJECT);
       
   162 
       
   163     HBufC* location =
       
   164         StringLoader::LoadLC(R_QTN_MEET_REQ_PLAIN_TEXT_LOCATION);
       
   165 
       
   166     HBufC* startTime =
       
   167         StringLoader::LoadLC(R_QTN_MEET_REQ_PLAIN_TEXT_START_TIME);
       
   168 
       
   169     HBufC* endTime =
       
   170         StringLoader::LoadLC(R_QTN_MEET_REQ_PLAIN_TEXT_END_TIME);
       
   171 
       
   172     HBufC* description =
       
   173         StringLoader::LoadLC(R_QTN_MEET_REQ_PLAIN_TEXT_DESCRIPTION);
       
   174 
       
   175     HBufC* details =
       
   176         HBufC::NewLC( subject->Length() +
       
   177                       location->Length() +
       
   178                       startTime->Length() +
       
   179                       endTime->Length() +
       
   180                       description->Length() +
       
   181                       aEntry.SummaryL().Length() +
       
   182                       aEntry.LocationL().Length() +
       
   183                       aEntry.DescriptionL().Length() +
       
   184                       KNewLine().Length() * KNewLinesNeeded +
       
   185                       KTimeStringLength * 2 + // start and end time
       
   186                       KDateStringLength * 2 + // start and end date
       
   187                       KWhiteSpace().Length() * 2 ); // start and end date times
       
   188 
       
   189     TPtr detailsPtr( details->Des() );
       
   190 
       
   191     detailsPtr.Append( *subject );
       
   192     detailsPtr.Append( aEntry.SummaryL() );
       
   193     detailsPtr.Append( KNewLine );
       
   194 
       
   195     detailsPtr.Append( *location );
       
   196     detailsPtr.Append( aEntry.LocationL() );
       
   197     detailsPtr.Append( KNewLine );
       
   198 
       
   199     detailsPtr.Append( *startTime );
       
   200     detailsPtr.Append( startDateStr );
       
   201     detailsPtr.Append( KWhiteSpace );
       
   202     detailsPtr.Append( startTimeStr );
       
   203     detailsPtr.Append( KNewLine );
       
   204 
       
   205     detailsPtr.Append( *endTime );
       
   206     detailsPtr.Append( endDateStr );
       
   207     detailsPtr.Append( KWhiteSpace );
       
   208     detailsPtr.Append( endTimeStr );
       
   209     detailsPtr.Append( KNewLine );
       
   210 
       
   211     detailsPtr.Append( *description );
       
   212     detailsPtr.Append( aEntry.DescriptionL() );
       
   213     detailsPtr.Append( KNewLine );
       
   214 
       
   215     CleanupStack::Pop( details );
       
   216 
       
   217     // subject, location, startTime, endTime, description
       
   218     CleanupStack::PopAndDestroy( 5, subject );
       
   219 
       
   220     CleanupStack::PushL( details );
       
   221     return details;
       
   222     }
       
   223 
       
   224 // ---------------------------------------------------------------------------
       
   225 // CESMRMailPlainTextFormatter::Body8LC
       
   226 // ---------------------------------------------------------------------------
       
   227 //
       
   228 HBufC8* CESMRMailPlainTextFormatter::Body8LC(
       
   229         CCalEntry& aEntry )
       
   230     {
       
   231     FUNC_LOG;
       
   232     HBufC* buf16 = Body16LC( aEntry );
       
   233     HBufC8* buf8 = HBufC8::NewLC( buf16->Length() );
       
   234     TPtr8 ptr8( buf8->Des() );
       
   235 
       
   236     TInt err = CnvUtfConverter::ConvertFromUnicodeToUtf8( ptr8, *buf16 );
       
   237     User::LeaveIfError( err );
       
   238     CleanupStack::Pop( buf8 );
       
   239     CleanupStack::PopAndDestroy( buf16 );
       
   240     CleanupStack::PushL( buf8 );
       
   241     return buf8;
       
   242     }
       
   243 
       
   244 // ---------------------------------------------------------------------------
       
   245 // CESMRMailPlainTextFormatter::Subject16LC
       
   246 // ---------------------------------------------------------------------------
       
   247 //
       
   248 HBufC* CESMRMailPlainTextFormatter::Subject16LC(
       
   249             CCalEntry& aEntry,
       
   250             TBool aIsForwarded,
       
   251             TBool aIsUpdate )
       
   252     {
       
   253     FUNC_LOG;
       
   254     CESMRCalUserUtil* caluserUtil = CESMRCalUserUtil::NewLC( aEntry );
       
   255     TESMRRole role = caluserUtil->PhoneOwnerRoleL();
       
   256     CleanupStack::PopAndDestroy( caluserUtil );
       
   257 
       
   258     // create prefix for subject:
       
   259     HBufC* prefix = SubjectLinePrefix16LC(
       
   260                             aEntry,
       
   261                             aIsForwarded,
       
   262                             aIsUpdate ); //if returns NULL no need to CleanupStack::Pop later
       
   263 
       
   264     // user's email address is attached to subject if user is
       
   265     // responding to meeting request:
       
   266     HBufC* responseAddress = KNullDesC().AllocLC();
       
   267     if ( !aIsForwarded )
       
   268         {
       
   269         if ( EESMRRoleRequiredAttendee == role ||
       
   270              EESMRRoleOptionalAttendee == role ||
       
   271              EESMRRoleNonParticipant == role  )
       
   272              {
       
   273              CleanupStack::PopAndDestroy( responseAddress );
       
   274              responseAddress = NULL;
       
   275 
       
   276              CCalUser* phoneOwner = aEntry.PhoneOwnerL();
       
   277              responseAddress = phoneOwner->Address().AllocLC();
       
   278              }
       
   279         }
       
   280 
       
   281     // count the subject line buffer length
       
   282     TInt subjectLineLength( aEntry.SummaryL().Length() );
       
   283 
       
   284     if (prefix)
       
   285         {
       
   286         subjectLineLength += prefix->Length();
       
   287         }
       
   288 
       
   289     if (responseAddress)
       
   290         {
       
   291         subjectLineLength += responseAddress->Length();
       
   292         }
       
   293 
       
   294     // create and fill the subject buffer
       
   295     HBufC* subjectLine =
       
   296         HBufC::NewLC( subjectLineLength );
       
   297     TPtr ptrSubject( subjectLine->Des() );
       
   298 
       
   299      if (responseAddress)
       
   300         {
       
   301         ptrSubject.Append(*responseAddress);
       
   302         }
       
   303 
       
   304      if (prefix)
       
   305         {
       
   306         ptrSubject.Copy( *prefix );
       
   307         }
       
   308 
       
   309     ptrSubject.Append( aEntry.SummaryL() );
       
   310 
       
   311     CleanupStack::Pop( subjectLine );
       
   312 
       
   313     if (prefix)
       
   314         {
       
   315         CleanupStack::PopAndDestroy( 2, prefix ); // responseAddress
       
   316         }
       
   317     else
       
   318         {
       
   319         CleanupStack::PopAndDestroy( responseAddress );
       
   320         }
       
   321     CleanupStack::PushL( subjectLine );
       
   322     return subjectLine;
       
   323     }
       
   324 
       
   325 // ---------------------------------------------------------------------------
       
   326 // CESMRMailPlainTextFormatter::Subject8LC
       
   327 // ---------------------------------------------------------------------------
       
   328 //
       
   329 HBufC8* CESMRMailPlainTextFormatter::Subject8LC(
       
   330             CCalEntry& aEntry,
       
   331             TBool aIsForwarded,
       
   332             TBool aIsUpdate  )
       
   333     {
       
   334     FUNC_LOG;
       
   335     HBufC* buf16 = Subject16LC( aEntry, aIsForwarded, aIsUpdate );
       
   336     HBufC8* buf8 = HBufC8::NewLC( buf16->Length() );
       
   337     TPtr8 ptr8(buf8->Des() );
       
   338 
       
   339     TInt err = CnvUtfConverter::ConvertFromUnicodeToUtf8( ptr8, *buf16 );
       
   340     User::LeaveIfError( err );
       
   341     CleanupStack::Pop( buf8 );
       
   342     CleanupStack::PopAndDestroy( buf16 );
       
   343     CleanupStack::PushL( buf8 );
       
   344     return buf8;
       
   345     }
       
   346 
       
   347 // ---------------------------------------------------------------------------
       
   348 // CESMRMailPlainTextFormatter::SubjectLinePrefix16LC
       
   349 // ---------------------------------------------------------------------------
       
   350 //
       
   351 HBufC* CESMRMailPlainTextFormatter::SubjectLinePrefix16LC(
       
   352         CCalEntry& aEntry,
       
   353         TBool aIsForwarded,
       
   354         TBool aIsUpdate  )
       
   355     {
       
   356     FUNC_LOG;
       
   357     HBufC* prefix = NULL;
       
   358 
       
   359     CESMRCalUserUtil* caluserUtil = CESMRCalUserUtil::NewLC( aEntry );
       
   360     TESMRRole role = caluserUtil->PhoneOwnerRoleL();
       
   361     CleanupStack::PopAndDestroy( caluserUtil );
       
   362     caluserUtil = NULL;
       
   363 
       
   364     if ( EESMRRoleOrganizer == role )
       
   365         {
       
   366         CCalEntry::TMethod method = aEntry.MethodL();
       
   367         if ( CCalEntry::EMethodCancel == method )
       
   368             {
       
   369             prefix =
       
   370                 StringLoader::LoadLC( R_QTN_MEET_REQ_PLAIN_TEXT_CANCELLED );
       
   371             }
       
   372         else if ( CCalEntry::EMethodRequest == method  &&
       
   373                   (aIsUpdate || aIsForwarded) )
       
   374             {
       
   375             prefix =
       
   376                 StringLoader::LoadLC( R_QTN_MEET_REQ_PLAIN_TEXT_UPDATE );
       
   377             }
       
   378         else
       
   379             {
       
   380             prefix =
       
   381                 KNullDesC().AllocLC();
       
   382             }
       
   383         }
       
   384 
       
   385     else if ( EESMRRoleRequiredAttendee == role ||
       
   386               EESMRRoleOptionalAttendee == role ||
       
   387               EESMRRoleNonParticipant == role )
       
   388         {
       
   389         CCalAttendee* attendee =
       
   390             iMailboxUtils.ThisAttendeeL( aEntry );
       
   391 
       
   392         if ( attendee )
       
   393             {
       
   394             switch( attendee->StatusL() )
       
   395                 {
       
   396                 case CCalAttendee::EAccepted:
       
   397                     prefix =
       
   398                             StringLoader::LoadLC(
       
   399                                 R_QTN_MEET_REQ_PLAIN_TEXT_ACCEPTED );
       
   400                     break;
       
   401                 case CCalAttendee::ETentative:
       
   402                     prefix =
       
   403                             StringLoader::LoadLC(
       
   404                                 R_QTN_MEET_REQ_PLAIN_TEXT_TENTATIVE );
       
   405                     break;
       
   406                 case CCalAttendee::EDeclined:
       
   407                     prefix =
       
   408                             StringLoader::LoadLC(
       
   409                                 R_QTN_MEET_REQ_PLAIN_TEXT_DECLINED );
       
   410                     break;
       
   411                 default:
       
   412                     prefix =
       
   413                         KNullDesC().AllocLC();
       
   414                     break;
       
   415                 }
       
   416             }
       
   417         else
       
   418             {
       
   419             prefix =
       
   420                 KNullDesC().AllocLC();
       
   421             }
       
   422         }
       
   423     return prefix;
       
   424     }
       
   425 
       
   426 // ---------------------------------------------------------------------------
       
   427 // CESMRMailPlainTextFormatter::UpdatedStringLC
       
   428 // ---------------------------------------------------------------------------
       
   429 //
       
   430 HBufC* CESMRMailPlainTextFormatter::UpdatedStringLC()
       
   431     {
       
   432     FUNC_LOG;
       
   433     HBufC* updatedString =
       
   434             StringLoader::LoadLC( R_QTN_MEET_REQ_PLAIN_TEXT_UPDATE );
       
   435 
       
   436     return updatedString;
       
   437     }
       
   438 
       
   439 // ---------------------------------------------------------------------------
       
   440 // CESMRMailPlainTextFormatter::CanceledStringLC
       
   441 // ---------------------------------------------------------------------------
       
   442 //
       
   443 HBufC* CESMRMailPlainTextFormatter::CanceledStringLC()
       
   444     {
       
   445     FUNC_LOG;
       
   446     HBufC* canceledString =
       
   447             StringLoader::LoadLC( R_QTN_MEET_REQ_PLAIN_TEXT_CANCELLED );
       
   448 
       
   449     return canceledString;
       
   450     }
       
   451 
       
   452 // ---------------------------------------------------------------------------
       
   453 // CESMRMailPlainTextFormatter::ReplyStringLC
       
   454 // ---------------------------------------------------------------------------
       
   455 //
       
   456 HBufC* CESMRMailPlainTextFormatter::ReplyStringLC( CCalEntry& aEntry )
       
   457     {
       
   458     FUNC_LOG;
       
   459     HBufC* replyString = NULL;
       
   460     HBufC* replyStringPrefix =
       
   461             StringLoader::LoadLC( R_QTN_MEET_REQ_VIEWER_SUBJECT_PREFIX_REPL );
       
   462     
       
   463     TPtrC subject( aEntry.SummaryL() );
       
   464     
       
   465     TInt pos = subject.Find( *replyStringPrefix );
       
   466     if ( KErrNotFound == pos || KFirstPos != pos )
       
   467         {
       
   468         // There is no reply prefix already or reply prefix is not at the 
       
   469         // beginning --> We need to add it to description
       
   470 
       
   471         // No need to put into cleanupstack because calling only non leaving 
       
   472         // functions before exiting
       
   473         replyString = HBufC::NewL( 
       
   474                             replyStringPrefix->Length() + subject.Length() );
       
   475         
       
   476         TPtr ptrReply( replyString->Des() );
       
   477         
       
   478         ptrReply.Copy( *replyStringPrefix );
       
   479         ptrReply.Append( subject );
       
   480         }
       
   481     else
       
   482         {
       
   483         replyString = subject.AllocL();
       
   484         }
       
   485     
       
   486     CleanupStack::PopAndDestroy( replyStringPrefix );
       
   487     CleanupStack::PushL( replyString );
       
   488     
       
   489     return replyString;
       
   490     }
       
   491 
       
   492 // EOF