pushmtm/MtmUtilSrc/PushMtmUtil.cpp
branchRCL_3
changeset 48 8e6fa1719340
equal deleted inserted replaced
47:6385c4c93049 48:8e6fa1719340
       
     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:  CPushMtmUtil class definition.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 
       
    22 #include "PushMtmUtil.h"
       
    23 #include "PushMtmUtilPanic.h"
       
    24 #include "PushMtmLog.h"
       
    25 #include <push/CSIPushMsgEntry.h>
       
    26 #include <push/CSLPushMsgEntry.h>
       
    27 #include <msvids.h>
       
    28 #include <msvuids.h>
       
    29 #include <msvapi.h>
       
    30 #include <Uri16.h>
       
    31 #include <UriUtils.h>
       
    32 
       
    33 // ================= MEMBER FUNCTIONS =======================
       
    34 
       
    35 // ---------------------------------------------------------
       
    36 // CPushMtmUtil::NewL
       
    37 // ---------------------------------------------------------
       
    38 //
       
    39 EXPORT_C CPushMtmUtil* CPushMtmUtil::NewL( CMsvSession& aMsvSession )
       
    40     {
       
    41     CPushMtmUtil* self = new (ELeave) CPushMtmUtil( aMsvSession );
       
    42     CleanupStack::PushL( self );
       
    43     self->ConstructL();
       
    44     CleanupStack::Pop(); // self
       
    45     return self;
       
    46     }
       
    47 
       
    48 // ---------------------------------------------------------
       
    49 // CPushMtmUtil::~CPushMtmUtil
       
    50 // ---------------------------------------------------------
       
    51 //
       
    52 EXPORT_C CPushMtmUtil::~CPushMtmUtil()
       
    53     {
       
    54     }
       
    55 
       
    56 // ---------------------------------------------------------
       
    57 // CPushMtmUtil::MarkServiceUnreadL
       
    58 // ---------------------------------------------------------
       
    59 //
       
    60 EXPORT_C void CPushMtmUtil::MarkServiceUnreadL( TMsvId aEntryId,
       
    61                                                 TBool aUnread )
       
    62     {
       
    63     PUSHLOG_ENTERFN("CPushMtmUtil::MarkServiceUnreadL")
       
    64 
       
    65     CMsvEntry* cEntry = iMsvSession.GetEntryL( aEntryId );
       
    66     CleanupStack::PushL( cEntry );
       
    67 
       
    68     TMsvEntry tEntry( cEntry->Entry() );
       
    69     // Change the context only if necessary.
       
    70     if ( tEntry.New() != aUnread || tEntry.Unread() != aUnread )
       
    71         {
       
    72         tEntry.SetNew( aUnread );
       
    73         tEntry.SetUnread( aUnread );
       
    74         cEntry->ChangeL( tEntry );
       
    75         }
       
    76 
       
    77     CleanupStack::PopAndDestroy( cEntry ); // cEntry
       
    78 
       
    79     PUSHLOG_LEAVEFN("CPushMtmUtil::MarkServiceUnreadL")
       
    80     }
       
    81 
       
    82 // ---------------------------------------------------------
       
    83 // CPushMtmUtil::FindMessagesL
       
    84 // ---------------------------------------------------------
       
    85 //
       
    86 EXPORT_C CMsvEntrySelection* CPushMtmUtil::FindMessagesL
       
    87                                                ( TMsvId aFolderId,
       
    88                                                  TUid aMsgType,
       
    89                                                  TBool aRecursive )
       
    90     {
       
    91     CMsvEntrySelection* sel = new (ELeave) CMsvEntrySelection;
       
    92     CleanupStack::PushL( sel );
       
    93 
       
    94     FindMessagesL( aFolderId, aMsgType, aRecursive, *sel );
       
    95 
       
    96     CleanupStack::Pop(); // sel
       
    97     return sel;
       
    98     }
       
    99 
       
   100 // ---------------------------------------------------------
       
   101 // CPushMtmUtil::FindMessagesL
       
   102 // ---------------------------------------------------------
       
   103 //
       
   104 EXPORT_C CMsvEntrySelection* CPushMtmUtil::FindMessagesL
       
   105 ( const CMsvEntrySelection& aFolders, TUid aMsgType )
       
   106     {
       
   107     CMsvEntrySelection* sel = new (ELeave) CMsvEntrySelection;
       
   108     CleanupStack::PushL( sel );
       
   109 
       
   110     const TInt count( aFolders.Count() );
       
   111     for ( TInt i = 0; i < count; ++i )
       
   112         {
       
   113         FindMessagesL( aFolders[i], aMsgType, EFalse, *sel );
       
   114         }
       
   115 
       
   116     CleanupStack::Pop(); // sel
       
   117     return sel;
       
   118     }
       
   119 
       
   120 // ---------------------------------------------------------
       
   121 // CPushMtmUtil::FindMessagesL
       
   122 // ---------------------------------------------------------
       
   123 //
       
   124 EXPORT_C CMsvEntrySelection* CPushMtmUtil::FindMessagesL( TUid aMsgType )
       
   125     {
       
   126     CMsvEntrySelection* pushFolders = FindPushFoldersL();
       
   127     CleanupStack::PushL( pushFolders );
       
   128 
       
   129     CMsvEntrySelection* msgs = FindMessagesL( *pushFolders, aMsgType );
       
   130 
       
   131     CleanupStack::PopAndDestroy( pushFolders ); // pushFolders
       
   132     return msgs;
       
   133     }
       
   134 
       
   135 // ---------------------------------------------------------
       
   136 // CPushMtmUtil::FindPushFoldersL
       
   137 // ---------------------------------------------------------
       
   138 //
       
   139 EXPORT_C CMsvEntrySelection* CPushMtmUtil::FindPushFoldersL()
       
   140     {
       
   141     // Only the Inbox, the Documents folder and the user
       
   142     // defined folders can contain push messages.
       
   143     // The user defined folders can only be in the Documents folder.
       
   144     // (See the definition of CMsgFolderSelectionListArray::ConstructL
       
   145     // in msgavkon for more details.)
       
   146     CMsvEntrySelection* pushFolders = new (ELeave) CMsvEntrySelection;
       
   147     CleanupStack::PushL( pushFolders );
       
   148 
       
   149     // Check if Inbox folder exists.
       
   150     TMsvEntry dummyFolder;
       
   151     TMsvId dummyService;
       
   152     TInt err( iMsvSession.GetEntry
       
   153         ( KMsvGlobalInBoxIndexEntryId, dummyService, dummyFolder) );
       
   154 
       
   155     if ( !err )
       
   156         {
       
   157         pushFolders->AppendL( KMsvGlobalInBoxIndexEntryId );
       
   158         }
       
   159 
       
   160     // Check if Documents folder exists.
       
   161     const TMsvId KDocumentsIndexEntryId( 0x1008 );
       
   162     err = iMsvSession.GetEntry
       
   163         ( KDocumentsIndexEntryId, dummyService, dummyFolder);
       
   164 
       
   165     if ( !err )
       
   166         {
       
   167         pushFolders->AppendL( KDocumentsIndexEntryId );
       
   168         // Get the user defined folders in Documents.
       
   169         const TMsvSelectionOrdering selOrd
       
   170             ( KMsvGroupByType, EMsvSortByDescription, ETrue );
       
   171         CMsvEntry* documentsFolder = CMsvEntry::NewL
       
   172             ( iMsvSession, KDocumentsIndexEntryId, selOrd );
       
   173         CleanupStack::PushL( documentsFolder );
       
   174 
       
   175         CMsvEntrySelection* userDefFolders =
       
   176             documentsFolder->ChildrenWithTypeL( KUidMsvFolderEntry );
       
   177         CleanupStack::PopAndDestroy(); // documentsFolder
       
   178         CleanupStack::PushL( userDefFolders );
       
   179 
       
   180         const TMsvId KTemplatesIndexEntryId( 0x1009 );
       
   181         const TInt count( userDefFolders->Count() );
       
   182         for ( TInt i = 0; i < count; ++i )
       
   183             {
       
   184             // However Templates folder is in Documents, no messages
       
   185             // can be moved there - it cannot contain push messages.
       
   186             if ( userDefFolders->At( i ) != KTemplatesIndexEntryId )
       
   187                 {
       
   188                 pushFolders->AppendL( userDefFolders->At( i ) );
       
   189                 }
       
   190             }
       
   191 
       
   192         CleanupStack::PopAndDestroy(); // userDefFolders
       
   193         }
       
   194 
       
   195     CleanupStack::Pop(); // pushFolders
       
   196     return pushFolders;
       
   197     }
       
   198 
       
   199 // ---------------------------------------------------------
       
   200 // CPushMtmUtil::FindSiIdLC
       
   201 // ---------------------------------------------------------
       
   202 //
       
   203 EXPORT_C CMsvEntrySelection* CPushMtmUtil::FindSiIdLC( const TDesC& aSiId )
       
   204     {
       
   205 	CMsvEntrySelection* matching = new (ELeave) CMsvEntrySelection;
       
   206 	CleanupStack::PushL( matching );
       
   207 
       
   208 	CSIPushMsgEntry* siEntry = CSIPushMsgEntry::NewL();
       
   209 	CleanupStack::PushL( siEntry );
       
   210 
       
   211     CMsvEntrySelection* allSi = FindMessagesL( KUidWapPushMsgSI );
       
   212     CleanupStack::PushL( allSi );
       
   213 
       
   214 	// loop until we find a match or reach the end of the SI entries
       
   215     const TInt numEntries( allSi->Count() );
       
   216 	for (TInt count = 0; count < numEntries; ++count )
       
   217 		{
       
   218         siEntry->RetrieveL( iMsvSession, allSi->At( count ) );
       
   219 		if ( aSiId.CompareF( siEntry->Id() ) == 0 ) // Found a match
       
   220             {
       
   221 			matching->AppendL( allSi->At( count ) );
       
   222             }
       
   223 		}
       
   224 
       
   225 	CleanupStack::PopAndDestroy( 2 ); // allSi, siEntry
       
   226 
       
   227 	return matching; //Still on CleanupStack
       
   228     }
       
   229 
       
   230 // ---------------------------------------------------------
       
   231 // CPushMtmUtil::FindUrlLC
       
   232 // ---------------------------------------------------------
       
   233 //
       
   234 EXPORT_C CMsvEntrySelection* CPushMtmUtil::FindUrlLC( const TDesC& aUrl,
       
   235                                                       TUid aPushType )
       
   236 	{
       
   237     __ASSERT_ALWAYS( aPushType == KUidWapPushMsgSI ||
       
   238                      aPushType == KUidWapPushMsgSL,
       
   239                      UtilPanic( EPushMtmUtilPanBadBioType ) );
       
   240 
       
   241 	CMsvEntrySelection* matching = new (ELeave) CMsvEntrySelection;
       
   242 	CleanupStack::PushL( matching );
       
   243 
       
   244     CMsvEntrySelection* all = FindMessagesL( aPushType );
       
   245     CleanupStack::PushL( all );
       
   246 
       
   247 	// loop until we find a match or reach the end of the SI entries
       
   248     const TInt numEntries( all->Count() );
       
   249 
       
   250     if ( aPushType == KUidWapPushMsgSI )
       
   251         {
       
   252 	    CSIPushMsgEntry* siEntry = CSIPushMsgEntry::NewL();
       
   253 	    CleanupStack::PushL( siEntry );
       
   254 
       
   255 	    for (TInt count = 0; count < numEntries; ++count )
       
   256 		    {
       
   257             siEntry->RetrieveL( iMsvSession, all->At( count ) );
       
   258 		    if ( aUrl.CompareF( siEntry->Url() ) == 0 ) // Found a match
       
   259                 {
       
   260 			    matching->AppendL( all->At( count ) );
       
   261                 }
       
   262 		    }
       
   263 
       
   264         CleanupStack::PopAndDestroy( siEntry ); // siEntry
       
   265         }
       
   266     else // aPushType == KUidWapPushMsgSL
       
   267         {
       
   268 	    CSLPushMsgEntry* slEntry = CSLPushMsgEntry::NewL();
       
   269 	    CleanupStack::PushL( slEntry );
       
   270 
       
   271 	    for (TInt count = 0; count < numEntries; ++count )
       
   272 		    {
       
   273             slEntry->RetrieveL( iMsvSession, all->At( count ) );
       
   274 		    if ( aUrl.CompareF( slEntry->Url() ) == 0 ) // Found a match
       
   275                 {
       
   276 			    matching->AppendL( all->At( count ) );
       
   277                 }
       
   278 		    }
       
   279 
       
   280         CleanupStack::PopAndDestroy( slEntry ); // slEntry
       
   281         }
       
   282 
       
   283 	CleanupStack::PopAndDestroy( all ); // all
       
   284 
       
   285 	return matching; //Still on CleanupStack
       
   286 	}
       
   287 
       
   288 // ---------------------------------------------------------
       
   289 // CPushMtmUtil::DeleteEntryL
       
   290 // ---------------------------------------------------------
       
   291 //
       
   292 EXPORT_C void CPushMtmUtil::DeleteEntryL( TMsvId aId )
       
   293 	{
       
   294     CMsvEntry* cEntry = iMsvSession.GetEntryL( aId );
       
   295     CleanupStack::PushL( cEntry );
       
   296     // Set as parent.
       
   297     cEntry->SetEntryL( cEntry->Entry().Parent() );
       
   298     // Delete the child.
       
   299     cEntry->DeleteL( aId );
       
   300     CleanupStack::PopAndDestroy( cEntry ); // cEntry
       
   301 	}
       
   302 
       
   303 // ---------------------------------------------------------
       
   304 // CPushMtmUtil::ConvertDetailsL
       
   305 // ---------------------------------------------------------
       
   306 //
       
   307 EXPORT_C HBufC* CPushMtmUtil::ConvertDetailsL( const TDesC8& aFrom ) const
       
   308     {
       
   309     HBufC* from = HBufC::NewMaxLC( aFrom.Length() );
       
   310     from->Des().Copy( aFrom );
       
   311 
       
   312     TUriParser pars;
       
   313     User::LeaveIfError( pars.Parse( *from ) );
       
   314 
       
   315     HBufC* res = NULL;
       
   316     if ( pars.IsPresent( EUriHost ) )
       
   317         {
       
   318         res = pars.Extract( EUriHost ).AllocL();
       
   319         }
       
   320     else
       
   321         {
       
   322         res = from->AllocL();
       
   323         }
       
   324 
       
   325     CleanupStack::PopAndDestroy( from ); // from
       
   326     return res;
       
   327     }
       
   328 
       
   329 // ---------------------------------------------------------
       
   330 // CPushMtmUtil::SetAttrs
       
   331 // ---------------------------------------------------------
       
   332 //
       
   333 EXPORT_C void CPushMtmUtil::SetAttrs( TMsvEntry& aContext, TUint32 aAttrs )
       
   334     {
       
   335     aContext.iMtmData2 = aContext.iMtmData2 |
       
   336                          (aAttrs<<KPushMtmShiftOnlyAttrs);
       
   337     }
       
   338 
       
   339 // ---------------------------------------------------------
       
   340 // CPushMtmUtil::ResetAttrs
       
   341 // ---------------------------------------------------------
       
   342 //
       
   343 EXPORT_C void CPushMtmUtil::ResetAttrs( TMsvEntry& aContext, TUint32 aAttrs )
       
   344     {
       
   345     aContext.iMtmData2 = aContext.iMtmData2 &
       
   346                          ~(aAttrs<<KPushMtmShiftOnlyAttrs);
       
   347     }
       
   348 
       
   349 // ---------------------------------------------------------
       
   350 // CPushMtmUtil::Attrs
       
   351 // ---------------------------------------------------------
       
   352 //
       
   353 EXPORT_C TUint32 CPushMtmUtil::Attrs( const TMsvEntry& aContext )
       
   354     {
       
   355     return (aContext.iMtmData2 >> KPushMtmShiftOnlyAttrs) &
       
   356            KPushMtmMaskOnlyAttrs;
       
   357     }
       
   358 
       
   359 // ---------------------------------------------------------
       
   360 // CPushMtmUtil::ConvertUriToDisplayFormL
       
   361 // ---------------------------------------------------------
       
   362 //
       
   363 EXPORT_C HBufC* CPushMtmUtil::ConvertUriToDisplayFormL( const TDesC& aUri )
       
   364     {
       
   365     HBufC8* uri8 = HBufC8::NewMaxLC( aUri.Length() );
       
   366     uri8->Des().Copy( aUri );
       
   367     TUriParser8 uriParser8;
       
   368     User::LeaveIfError( uriParser8.Parse( *uri8 ) );
       
   369     CUri* convertedCUri = UriUtils::ConvertToDisplayFormL( uriParser8 );
       
   370     CleanupStack::PopAndDestroy( uri8 );
       
   371     CleanupStack::PushL( convertedCUri );
       
   372     HBufC* convertedUri = convertedCUri->Uri().UriDes().AllocL();
       
   373     CleanupStack::PopAndDestroy( convertedCUri );
       
   374     return convertedUri;
       
   375     }
       
   376 
       
   377 // ---------------------------------------------------------
       
   378 // CPushMtmUtil::CPushMtmUtil
       
   379 // ---------------------------------------------------------
       
   380 //
       
   381 CPushMtmUtil::CPushMtmUtil( CMsvSession& aMsvSession )
       
   382 : iMsvSession( aMsvSession )
       
   383     {
       
   384     }
       
   385 
       
   386 // ---------------------------------------------------------
       
   387 // CPushMtmUtil::ConstructL
       
   388 // ---------------------------------------------------------
       
   389 //
       
   390 void CPushMtmUtil::ConstructL()
       
   391     {
       
   392     }
       
   393 
       
   394 // ---------------------------------------------------------
       
   395 // CPushMtmUtil::FindMessagesL
       
   396 // ---------------------------------------------------------
       
   397 //
       
   398 void CPushMtmUtil::FindMessagesL( TMsvId aFolderId,
       
   399                                   TUid aMsgType,
       
   400                                   TBool aRecursive,
       
   401                                   CMsvEntrySelection& aResult )
       
   402     {
       
   403     // Find the push entries of the specified type in the specified folder.
       
   404     CMsvEntry* thisFolder = iMsvSession.GetEntryL( aFolderId );
       
   405     CleanupStack::PushL( thisFolder );
       
   406 
       
   407 	TMsvSelectionOrdering selOrd( thisFolder->SortType() );
       
   408 	selOrd.SetShowInvisibleEntries( EFalse );
       
   409 	thisFolder->SetSortTypeL( selOrd );
       
   410     CMsvEntrySelection* msgSel = thisFolder->ChildrenWithMtmL( KUidMtmWapPush );
       
   411     CleanupStack::PushL( msgSel );
       
   412 
       
   413     // The selection may contain any kind of push message.
       
   414     // Select only those that have the specified type.
       
   415     TMsvEntry tEntry;
       
   416     TMsvId service;
       
   417     TInt err;
       
   418     const TInt msgCnt( msgSel->Count() );
       
   419     for ( TInt i = 0; i < msgCnt; ++i )
       
   420         {
       
   421         err = iMsvSession.GetEntry( msgSel->At(i), service, tEntry );
       
   422         if ( err == KErrNotFound )
       
   423             {
       
   424             // Do nothing, just continue.
       
   425             }
       
   426         else if ( !err )
       
   427             {
       
   428             if ( tEntry.iBioType == aMsgType.iUid )
       
   429                 {
       
   430                 // We have found an entry of the requested type.
       
   431                 aResult.AppendL( msgSel->At(i) );
       
   432                 }
       
   433             }
       
   434         else
       
   435             {
       
   436             User::Leave( err );
       
   437             }
       
   438         }
       
   439 
       
   440     CleanupStack::PopAndDestroy( msgSel ); // msgSel
       
   441 
       
   442     // Find messages in the subfolders if required.
       
   443     if ( aRecursive )
       
   444         {
       
   445         CMsvEntrySelection* subFolders =
       
   446             thisFolder->ChildrenWithTypeL( KUidMsvFolderEntry );
       
   447         CleanupStack::PushL( subFolders );
       
   448 
       
   449         const TInt count( subFolders->Count() );
       
   450         for ( TInt i = 0; i < count; ++i )
       
   451             {
       
   452             FindMessagesL( subFolders->At(i), aMsgType, aRecursive, aResult );
       
   453             }
       
   454 
       
   455         CleanupStack::PopAndDestroy( subFolders ); // subFolders
       
   456         }
       
   457 
       
   458     CleanupStack::PopAndDestroy( thisFolder ); // thisFolder
       
   459     }
       
   460 
       
   461 // End of file.