browserui/browser/BrowserAppSrc/BrowserBmOTABinSender.cpp
branchRCL_3
changeset 64 6385c4c93049
parent 63 4baee4f15982
child 65 8e6fa1719340
equal deleted inserted replaced
63:4baee4f15982 64:6385c4c93049
     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 *
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21         // System includes
       
    22 #include <eikdef.h>
       
    23 #include <eikenv.h>
       
    24 #include <sendui.h>
       
    25 #include <CMessageData.h>
       
    26 #include <TSendingCapabilities.h>
       
    27 #include <SenduiMtmUids.h>
       
    28 #include <msgbiouids.h>
       
    29 #include <txtrich.h>
       
    30 #include <smut.h>
       
    31 
       
    32 	// User includes
       
    33 #include "BrowserAppUi.h"
       
    34 #include "BrowserUtil.h"
       
    35 #include "BrowserDialogs.h"
       
    36 #include "BrowserBmOTABinSender.h"
       
    37 
       
    38 
       
    39 // MACROS
       
    40 
       
    41 #ifndef _BOOKMARK_SENT_ASCII
       
    42 // Calculates the byte size of 16bits (a)
       
    43 #define TUINT16_LEN(a)  ( sizeof(a) / sizeof(TUint16) )
       
    44 #endif
       
    45 
       
    46 // ================= LOCAL CONSTANTS =======================
       
    47 
       
    48 /// Granularity of the bookmark list.
       
    49 LOCAL_C const TInt KGranularity = 1;
       
    50 
       
    51 /// Attachment file name.
       
    52 _LIT( KAttachmentFilename, "c:\\system\\temp\\Bookmark.txt");
       
    53 
       
    54 #ifndef  _BOOKMARK_SENT_ASCII
       
    55 // Binary encoded BM data. See OTA spec
       
    56 static const TUint16 Kch_lstart[] = { 0x00, 0x01, 0x00, 0x45 };
       
    57 static const TUint16 Kch_lend[] = { 0x01 };
       
    58 static const TUint16 Kch_start[] = { 0xC6,0x7F,0x01,0x87,0x15,0x11,0x03 };
       
    59 static const TUint16 Kch_mid[] = { 0x00,0x01,0x87,0x17,0x11,0x03 };
       
    60 static const TUint16 Kch_end[] = { 0x00,0x01,0x01 };
       
    61 #endif  // _BOOKMARK_SENT_ASCII
       
    62 
       
    63 
       
    64 // ================= MEMBER FUNCTIONS =======================
       
    65 
       
    66 // ---------------------------------------------------------------------------
       
    67 // CBookmarkOTAItem::CBookmarkOTAItem
       
    68 // ---------------------------------------------------------------------------
       
    69 //
       
    70 CBookmarkOTAItem::CBookmarkOTAItem()
       
    71     {
       
    72     }
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // CBookmarkOTAItem::~CBookmarkOTAItem
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 CBookmarkOTAItem::~CBookmarkOTAItem()
       
    79     {
       
    80     delete iBookmark;
       
    81     }
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // CBookmarkOTAItem::NewL
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 CBookmarkOTAItem* CBookmarkOTAItem::NewL( const TDesC& aUrl, const TDesC& aTitle )
       
    88     {
       
    89     CBookmarkOTAItem *item = new (ELeave) CBookmarkOTAItem();
       
    90     CleanupStack::PushL( item );
       
    91 
       
    92     item->ConstructL( aUrl, aTitle );
       
    93 
       
    94     CleanupStack::Pop(); // item
       
    95 
       
    96     return item;
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // CBookmarkOTAItem::ConstructL
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 void CBookmarkOTAItem::ConstructL( const TDesC& aUrl, 
       
   104 #ifdef  _BOOKMARK_SENT_ASCII
       
   105                                    const TDesC& /*aTitle*/ )
       
   106 #else   // _BOOKMARK_SENT_ASCII
       
   107                                    const TDesC& aTitle )
       
   108 #endif  // _BOOKMARK_SENT_ASCII
       
   109 
       
   110     {
       
   111 #ifdef  _BOOKMARK_SENT_ASCII
       
   112     iBookmark = aUrl.AllocL();
       
   113 #else   // _BOOKMARK_SENT_ASCII
       
   114     TInt    length  = TUINT16_LEN( Kch_start ) + 
       
   115                       TUINT16_LEN( Kch_mid ) + 
       
   116                       TUINT16_LEN( Kch_end );
       
   117 
       
   118     // calculate full length of BOOKMARK
       
   119     length += aTitle.Length() + aUrl.Length();
       
   120     
       
   121     iBookmark = HBufC::NewL( length );
       
   122 
       
   123     TPtrC  start( Kch_start, TUINT16_LEN(Kch_start) );
       
   124     TPtrC  mid( Kch_mid, TUINT16_LEN(Kch_mid) );
       
   125     TPtrC  end( Kch_end, TUINT16_LEN(Kch_end) );
       
   126 
       
   127     iBookmark->Des().Append( start );
       
   128     iBookmark->Des().Append( aTitle );
       
   129     iBookmark->Des().Append( mid );
       
   130     iBookmark->Des().Append( aUrl );
       
   131     iBookmark->Des().Append( end );
       
   132 
       
   133 #endif  // _BOOKMARK_SENT_ASCII
       
   134     }
       
   135 
       
   136 // ---------------------------------------------------------------------------
       
   137 // CWmlBrowserBmOTABinSender::NewL
       
   138 // ---------------------------------------------------------------------------
       
   139 //
       
   140 CWmlBrowserBmOTABinSender* CWmlBrowserBmOTABinSender::NewL()
       
   141     {
       
   142     CWmlBrowserBmOTABinSender* sender = new (ELeave) 
       
   143                                             CWmlBrowserBmOTABinSender();
       
   144     CleanupStack::PushL( sender );
       
   145 
       
   146     sender->ConstructL();
       
   147 
       
   148     CleanupStack::Pop();    // sender
       
   149 
       
   150     return sender;
       
   151     }
       
   152 
       
   153 // ---------------------------------------------------------------------------
       
   154 // CWmlBrowserBmOTABinSender::~CWmlBrowserBmOTABinSender
       
   155 // ---------------------------------------------------------------------------
       
   156 //
       
   157 CWmlBrowserBmOTABinSender::~CWmlBrowserBmOTABinSender()
       
   158     {
       
   159     ResetAndDestroy();
       
   160     delete iSendUi;
       
   161     }
       
   162 
       
   163 // ---------------------------------------------------------------------------
       
   164 // CWmlBrowserBmOTABinSender::ResetAndDestroy
       
   165 // ---------------------------------------------------------------------------
       
   166 //
       
   167 void CWmlBrowserBmOTABinSender::ResetAndDestroy()
       
   168     {
       
   169     iBookmarks.ResetAndDestroy();
       
   170     }
       
   171 
       
   172 // ---------------------------------------------------------------------------
       
   173 // CWmlBrowserBmOTABinSender::AppendL
       
   174 // ---------------------------------------------------------------------------
       
   175 //
       
   176 void CWmlBrowserBmOTABinSender::AppendL( const TText *aUrl, 
       
   177                                          const TText *aTitle )
       
   178     {
       
   179 	__ASSERT_DEBUG( (aUrl != NULL), Util::Panic( Util::EUninitializedData ) );
       
   180     TPtrC tempUrl( aUrl );
       
   181     CBookmarkOTAItem *item = NULL;
       
   182 
       
   183     if( !aTitle )
       
   184         {
       
   185         _LIT( KEmptyText, "" );
       
   186 
       
   187         item = CBookmarkOTAItem::NewL( tempUrl, (TDesC&)KEmptyText );
       
   188         }
       
   189     else
       
   190         {
       
   191         TPtrC tempTitle( aTitle );
       
   192 
       
   193         item = CBookmarkOTAItem::NewL( tempUrl, tempTitle );
       
   194         }
       
   195 
       
   196     CleanupStack::PushL( item );
       
   197 
       
   198     iBookmarks.AppendL( item );
       
   199 
       
   200     CleanupStack::Pop(); // item
       
   201     }
       
   202 
       
   203 // ---------------------------------------------------------------------------
       
   204 // CWmlBrowserBmOTABinSender::AppendL
       
   205 // ---------------------------------------------------------------------------
       
   206 //
       
   207 void CWmlBrowserBmOTABinSender::AppendL( const TDesC& aUrl, const TDesC& aTitle )
       
   208     {
       
   209     CBookmarkOTAItem *item = CBookmarkOTAItem::NewL( aUrl, aTitle );
       
   210     CleanupStack::PushL( item );
       
   211 
       
   212     iBookmarks.AppendL( item );
       
   213 
       
   214     CleanupStack::Pop(); // item
       
   215     }
       
   216 
       
   217 // ---------------------------------------------------------------------------
       
   218 // CWmlBrowserBmOTABinSender::SendAddressL
       
   219 // ---------------------------------------------------------------------------
       
   220 //
       
   221 void CWmlBrowserBmOTABinSender::SendAddressL( )
       
   222     {
       
   223     CMessageData* msgData = CMessageData::NewLC();//Cl St: 1
       
   224     __ASSERT_DEBUG( iBookmarks.Length(), 
       
   225                      Util::Panic( Util::EUninitializedData ) );
       
   226 
       
   227     // sending error indicator
       
   228     // used with CreateAndSendMessageL()
       
   229     // to free up allocated memory
       
   230     // and than leave at the and of 
       
   231     // this function
       
   232     TInt error = KErrNone;
       
   233 
       
   234     // calculate full length of OTA message
       
   235     TInt    length = 0;
       
   236 
       
   237     TSendingCapabilities capa;
       
   238     CArrayFixFlat<TUid>* servicesToDim = new(ELeave) CArrayFixFlat<TUid>( 2 );
       
   239     CleanupStack::PushL( servicesToDim );
       
   240     servicesToDim->AppendL( KSenduiMtmAudioMessageUid );
       
   241 	// service: audio message to dim 
       
   242 	servicesToDim->AppendL( KSenduiMtmPostcardUid );    
       
   243 	// service: postcard to dim						  
       
   244     TUid service = iSendUi->ShowSendQueryL(NULL, capa, servicesToDim, KNullDesC);
       
   245 	iSendUi->ServiceCapabilitiesL( service, capa );
       
   246 	
       
   247 	CleanupStack::PopAndDestroy(); // destroy serviceToDim 
       
   248 
       
   249 #ifndef _BOOKMARK_SENT_ASCII
       
   250     length += TUINT16_LEN( Kch_lstart ) + TUINT16_LEN( Kch_lend );
       
   251 #endif  // _BOOKMARK_SENT_ASCII
       
   252 
       
   253     for( TInt index = 0; index < iBookmarks.Count(); ++index )
       
   254         {
       
   255         length += iBookmarks[index]->iBookmark->Length();
       
   256         }
       
   257 
       
   258 #ifdef _BOOKMARK_SENT_ASCII
       
   259     // +1 '\n' per bookmarks
       
   260     // no need '\n' after the last one
       
   261     length += iBookmarks.Count() - 1;
       
   262 #endif  // _BOOKMARK_SENT_ASCII
       
   263 
       
   264     // body contains the binary sequence
       
   265     HBufC* body = HBufC::NewLC( length );
       
   266 
       
   267 #ifndef _BOOKMARK_SENT_ASCII
       
   268     TPtrC  lstart( Kch_lstart, TUINT16_LEN( Kch_lstart ) );
       
   269     TPtrC  lend( Kch_lend, TUINT16_LEN( Kch_lend ) );
       
   270     // body starts with CHARACTERISTIC-LIST
       
   271     body->Des().Append( lstart );
       
   272 #endif  // _BOOKMARK_SENT_ASCII
       
   273 
       
   274     for( TInt index2 = 0; index2 < iBookmarks.Count(); ++index2 )
       
   275         {
       
   276         // Append CHARACTERISTIC to CHARACTERISTIC-LIST
       
   277         body->Des().Append( iBookmarks[index2]->iBookmark->Des().Ptr(), 
       
   278                             iBookmarks[index2]->iBookmark->Length() );
       
   279 
       
   280 #ifdef _BOOKMARK_SENT_ASCII
       
   281         // append '\n' after ever bookmark except the last one
       
   282         if( index2 < iBookmarks.Count() - 1 )
       
   283             {
       
   284             _LIT( KNewLine, "\n" );
       
   285             body->Des().Append( KNewLine );
       
   286             }
       
   287 #endif  // _BOOKMARK_SENT_ASCII
       
   288         }
       
   289 
       
   290 #ifndef _BOOKMARK_SENT_ASCII
       
   291     // CHARACTERISTIC-LIST termination
       
   292     body->Des().Append( lend );
       
   293 #endif  // _BOOKMARK_SENT_ASCII
       
   294 
       
   295 	// The issue is because of the deletion of the temp file .
       
   296 	// As we are sending address only , we can just create a buffer & send it in message
       
   297 	// body.
       
   298 
       
   299 	if ( capa.iFlags & (TSendingCapabilities::ESupportsBodyText || TSendingCapabilities::ESupportsAttachments))
       
   300 		{
       
   301 		CRichText* text = 
       
   302 		        CRichText::NewL(CEikonEnv::Static()->SystemParaFormatLayerL(),
       
   303 		                        CEikonEnv::Static()->SystemCharFormatLayerL());
       
   304 		CleanupStack::PushL(text);
       
   305 		text->InsertL(0, *body);        
       
   306 		msgData->SetBodyTextL( text );
       
   307 		CleanupStack::PopAndDestroy(text );
       
   308 	
       
   309 #ifdef _BOOKMARK_SENT_ASCII
       
   310 		TRAP( error, iSendUi->CreateAndSendMessageL( service, msgData ) );
       
   311 #else
       
   312     	TRAP( error, iSendUi->CreateAndSendMessageL( service, 
       
   313                                                  	 msgData,
       
   314                                                  	 KMsgBioUidWmlBrSettings ) );
       
   315 #endif  // _BOOKMARK_SENT_ASCII
       
   316 		}
       
   317 
       
   318     ResetAndDestroy();
       
   319 
       
   320     CleanupStack::PopAndDestroy( 2 ); // body, msgData
       
   321 
       
   322     // if any error occured during sending process
       
   323     User::LeaveIfError( error );
       
   324     }
       
   325 
       
   326 
       
   327 
       
   328 // ---------------------------------------------------------------------------
       
   329 // CWmlBrowserBmOTABinSender::SendOPMLFileL
       
   330 // ---------------------------------------------------------------------------
       
   331 //
       
   332 void CWmlBrowserBmOTABinSender::SendOPMLFileL(const TDesC& aOPMLFileName)
       
   333     {
       
   334     CMessageData* msgData = CMessageData::NewLC();
       
   335 
       
   336     // sending error indicator
       
   337     // used with CreateAndSendMessageL()
       
   338     // to free up allocated memory
       
   339     // and than leave at the and of 
       
   340     // this function
       
   341     TInt error = KErrNone;
       
   342 
       
   343     TSendingCapabilities capa;
       
   344     
       
   345     CArrayFixFlat<TUid>* servicesToDim = new(ELeave) CArrayFixFlat<TUid>( 2 );
       
   346     CleanupStack::PushL( servicesToDim );
       
   347     servicesToDim->AppendL( KSenduiMtmAudioMessageUid );
       
   348 	// service: audio message to dim 
       
   349 	servicesToDim->AppendL( KSenduiMtmPostcardUid );    
       
   350 	// service: postcard to dim						  
       
   351     TUid service = iSendUi->ShowSendQueryL(NULL, capa, servicesToDim, KNullDesC);
       
   352 	iSendUi->ServiceCapabilitiesL( service, capa );
       
   353 	
       
   354 	CleanupStack::PopAndDestroy(); // destroy serviceToDim 
       
   355 
       
   356 	// body contains the binary sequence
       
   357     //HBufC* body = HBufC::NewLC( aOPMLFileName.Length() );
       
   358     //body->Des().Copy( aOPMLFileName );
       
   359     
       
   360 	if ( capa.iFlags & TSendingCapabilities::ESupportsAttachments )
       
   361 	  	{
       
   362 	    // connect to file session
       
   363 	    RFs fsSession;
       
   364 	    User::LeaveIfError( fsSession.Connect() );
       
   365 	    CleanupClosePushL<RFs>( fsSession );
       
   366 	    CFileMan* fman = CFileMan::NewL( fsSession );
       
   367 	    CleanupStack::PushL( fman );
       
   368 	    
       
   369 	    // between WriteMessageBodyIntoFile()
       
   370 	    // and CreateAndSendMessageL, or
       
   371 	    // you have to take care of deleting 
       
   372 	    // temporary file
       
   373 	    //error = WriteMessageBodyIntoFileL( fsSession, *body );
       
   374 	        
       
   375         // send attachment
       
   376         // this function has to be trapped
       
   377         // because I have to be sure that the 
       
   378         // temproray file is removed!
       
   379         msgData->AppendAttachmentL( aOPMLFileName );            
       
   380         TRAP( error, iSendUi->CreateAndSendMessageL( service, 
       
   381                                                      msgData ) );
       
   382 	    // delete temporary file
       
   383 	    fman->Delete( aOPMLFileName );
       
   384 	    // close file session        
       
   385 	    fsSession.Close();
       
   386 
       
   387 	    CleanupStack::PopAndDestroy( 2 ); // fsSession, fman
       
   388 	  	}
       
   389 
       
   390     CleanupStack::PopAndDestroy( 1 ); // msgData
       
   391 
       
   392     // if any error occured during sending process
       
   393     User::LeaveIfError( error );
       
   394     }
       
   395 // ---------------------------------------------------------------------------
       
   396 // CWmlBrowserBmOTABinSender::ConstructL
       
   397 // ---------------------------------------------------------------------------
       
   398 //
       
   399 void CWmlBrowserBmOTABinSender::ConstructL()
       
   400     {
       
   401     iSendUi = CSendUi::NewL();
       
   402     }
       
   403 
       
   404 // ---------------------------------------------------------------------------
       
   405 // CWmlBrowserBmOTABinSender::CWmlBrowserBmOTABinSender
       
   406 // ---------------------------------------------------------------------------
       
   407 //
       
   408 CWmlBrowserBmOTABinSender::CWmlBrowserBmOTABinSender()
       
   409 : iBookmarks( KGranularity )
       
   410     {
       
   411     }
       
   412 
       
   413 // ---------------------------------------------------------------------------
       
   414 // CWmlBrowserBmOTABinSender::WriteMessageBodyIntoFileL
       
   415 //
       
   416 // This function leaves only if the file creating failed.
       
   417 // Do not need to be trapped to delete file. 
       
   418 // ---------------------------------------------------------------------------
       
   419 //
       
   420 TInt CWmlBrowserBmOTABinSender::WriteMessageBodyIntoFileL( RFs &fs, 
       
   421                                                           TDesC& aBody )
       
   422     {
       
   423     TInt result;
       
   424     RFile otaFile;
       
   425     TFileName filename( KAttachmentFilename );
       
   426 
       
   427     if( (result = otaFile.Replace( fs, filename, EFileShareExclusive)) == 
       
   428                                                                     KErrNone )
       
   429         {
       
   430 
       
   431 #ifdef _BOOKMARK_SENT_ASCII
       
   432         HBufC8 *tempBody = HBufC8::NewL( aBody.Length() );
       
   433         // convert 16bits -> 8bits
       
   434         tempBody->Des().Copy( aBody );
       
   435 
       
   436         // write message into file
       
   437         result = otaFile.Write( *tempBody );
       
   438 #else   // _BOOKMARK_SENT_ASCII
       
   439         // put 16bits binary data to 8bits (double byte -> two byte)
       
   440         TPtrC8  tempBody( (TUint8*)aBody.Ptr(), aBody.Length() * 2 );
       
   441 
       
   442         // write message into file
       
   443         result = otaFile.Write( tempBody );
       
   444 #endif  // _BOOKMARK_SENT_ASCII
       
   445 
       
   446         otaFile.Close();
       
   447         }
       
   448 
       
   449     return result;
       
   450     }
       
   451 
       
   452 // End of File
       
   453