messagingapp/msgui/appengine/src/conversationmsgstorehandler.cpp
changeset 23 238255e8b033
child 25 84d9eb65b26f
equal deleted inserted replaced
5:4697dfb2d7ad 23:238255e8b033
       
     1 /*
       
     2 * Copyright (c) 2008 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:  
       
    15 *
       
    16 */
       
    17 
       
    18 #include "conversationmsgstorehandler.h"
       
    19 #include "draftsmodel.h"
       
    20 // SYSTEM INCLUDES
       
    21 #include <StringLoader.h>
       
    22 #include <ccsdefs.h> 
       
    23 #include <mmsconst.h>
       
    24 #include <SendUiConsts.h>
       
    25 
       
    26 // CONSTANTS
       
    27 
       
    28 // ================= MEMBER FUNCTIONS =======================
       
    29 
       
    30 // ---------------------------------------------------------
       
    31 // Default constructor.
       
    32 // ---------------------------------------------------------
       
    33 //
       
    34 ConversationMsgStoreHandler::ConversationMsgStoreHandler():
       
    35     iMsvSession(NULL),iDraftEntry(NULL),iDraftMessages(NULL),mDraftsModel(NULL)
       
    36     {
       
    37     TRAP_IGNORE(InitL());
       
    38     }
       
    39 
       
    40 // ---------------------------------------------------------
       
    41 // Destructor.
       
    42 // ---------------------------------------------------------
       
    43 //
       
    44 ConversationMsgStoreHandler::~ConversationMsgStoreHandler()
       
    45     {
       
    46     if ( iDraftEntry )
       
    47         {
       
    48         delete iDraftEntry;
       
    49         iDraftEntry = NULL;
       
    50         }
       
    51 
       
    52     if( iMsvSession )
       
    53         {
       
    54         delete iMsvSession;
       
    55         iMsvSession = NULL;
       
    56         }
       
    57 
       
    58     if ( iDraftMessages )
       
    59         {
       
    60         iDraftMessages->Reset();
       
    61         delete iDraftMessages;
       
    62         iDraftMessages = NULL;
       
    63         }
       
    64     }
       
    65 
       
    66 // ---------------------------------------------------------
       
    67 // 
       
    68 // ---------------------------------------------------------
       
    69 //
       
    70 void ConversationMsgStoreHandler::InitL( )
       
    71     {
       
    72     iMsvSession = CMsvSession::OpenSyncL(*this);
       
    73     }
       
    74 
       
    75 // ---------------------------------------------------------
       
    76 // ConversationMsgStoreHandler::HandleSessionEventL()
       
    77 // ---------------------------------------------------------
       
    78 //
       
    79 void ConversationMsgStoreHandler::HandleSessionEventL(TMsvSessionEvent aEvent,
       
    80         TAny* aArg1, TAny* aArg2, TAny* aArg3)
       
    81     {   
       
    82     //args
       
    83     if(aArg1 == NULL || aArg2 == NULL)
       
    84         {   
       
    85         return;
       
    86         }
       
    87 
       
    88     //start, processing the event
       
    89     CMsvEntrySelection* selection=NULL;
       
    90     TMsvId target = 0;
       
    91     TMsvId source = 0;
       
    92     selection= (CMsvEntrySelection*)aArg1;
       
    93     target = *(TMsvId*)aArg2; // target (current) parent
       
    94     if(aArg3 != NULL)
       
    95         {
       
    96         source = *(TMsvId*)aArg3; // source parent
       
    97         }
       
    98     //process for draft messages only
       
    99     if ( target == KMsvDraftEntryIdValue)
       
   100         {
       
   101         switch( aEvent )
       
   102             {
       
   103             //case EMsvEntriesCreated:
       
   104             case EMsvEntriesChanged:
       
   105             case EMsvEntriesMoved:
       
   106                 {
       
   107                 TMsvEntry entry;
       
   108                 TMsvId service;
       
   109                 TInt error= KErrNone;
       
   110 
       
   111                 for( TInt i=0 ; i < selection->Count() ; ++i )
       
   112                     {
       
   113                     error = iMsvSession->GetEntry(selection->At(i),service,entry);
       
   114                     if (KErrNone == error)
       
   115                         {
       
   116                         // process only visible entries
       
   117                         if (entry.Visible() && IsMtmSupported(entry.iMtm.iUid))
       
   118                             {
       
   119                                 if(mDraftsModel && mDraftsModel->isReady())
       
   120                                 {
       
   121                                     mDraftsModel->addRow(entry);
       
   122                                 }
       
   123                             }
       
   124                         }
       
   125                     }
       
   126                 }
       
   127                 break;
       
   128 
       
   129             case EMsvEntriesDeleted:
       
   130                 {
       
   131                 for( TInt i=0 ; i < selection->Count() ; ++i )
       
   132                     {
       
   133                     TMsvId id = selection->At( i );
       
   134                     if(mDraftsModel && mDraftsModel->isReady())
       
   135                         {
       
   136                         mDraftsModel->deleteRow(id);
       
   137                         }
       
   138                     }
       
   139                 }
       
   140                 break;
       
   141             }//end switch 
       
   142         }
       
   143     else if(KMsvDraftEntryIdValue == source)
       
   144         {
       
   145         //if message is moved from drafts to other folder
       
   146         // it needs to be removed.
       
   147         if(aEvent == EMsvEntriesMoved)
       
   148             {
       
   149             for( TInt i=0 ; i < selection->Count() ; ++i )
       
   150                 {
       
   151                 TMsvId id = selection->At( i );
       
   152                 if(mDraftsModel && mDraftsModel->isReady())
       
   153                     {
       
   154                     mDraftsModel->deleteRow(id);
       
   155                     }
       
   156                 }
       
   157             }
       
   158         }
       
   159     }
       
   160 
       
   161 // ---------------------------------------------------------
       
   162 // ConversationMsgStoreHandler::MsgStatus
       
   163 // ---------------------------------------------------------
       
   164 //
       
   165 TCsMmsNotificationMsgState ConversationMsgStoreHandler::
       
   166 MmsNotificationStatus( TInt aMsvId )
       
   167     {
       
   168     TMsvId dummy = 0x0;
       
   169     
       
   170     TMsvEntry entry;
       
   171     iMsvSession->GetEntry(aMsvId, dummy, entry);
       
   172     
       
   173     TCsMmsNotificationMsgState status = EMsgStatusNull;
       
   174 
       
   175     // operationMask includes operation type. It is not bitmap but ordinal number. 
       
   176     // It does not include operation status and result
       
   177     TInt operationMask = (entry.iMtmData2 & KMmsOperationIdentifier) ;
       
   178 
       
   179     // Note! Ongoing operation resets every bit of operation type, operation status
       
   180     // and result. E.g. If message has been forwarded and then fetching starts, 
       
   181     // information about forwarding is lost
       
   182 
       
   183     if( ( entry.iMtmData1 & KMmsMessageTypeMask ) == KMmsMessageMNotificationInd )
       
   184         {
       
   185         if(     operationMask == KMmsOperationFetch 
       
   186                 &&  OperationOngoing( entry ) )
       
   187             { 
       
   188             // It's in retrieving state
       
   189             status = EMsgStatusRetrieving;
       
   190             }
       
   191         else if(    operationMask == KMmsOperationForward
       
   192                 &&  OperationOngoing( entry ) )
       
   193             { 
       
   194             // It's in forwarding state
       
   195             status = EMsgStatusForwarding;
       
   196             }
       
   197         else if(    operationMask == KMmsOperationForward
       
   198                 &&  OperationFinished( entry )
       
   199                 &&  !( entry.iMtmData2 & KMmsOperationResult ) )
       
   200             { 
       
   201             // It's been forwarded succesfully
       
   202             status = EMsgStatusForwarded;
       
   203             }
       
   204         else if(    operationMask == KMmsOperationFetch 
       
   205                 &&  OperationFinished( entry )
       
   206                 &&   (  entry.iMtmData2 & KMmsOperationResult 
       
   207                 ||  entry.iError ) )
       
   208             { 
       
   209             // Fetch has been failed
       
   210             status = EMsgStatusFailed;
       
   211             }
       
   212         else if(    operationMask == KMmsOperationDelete
       
   213                 &&  OperationFinished( entry )
       
   214                 &&  !( entry.iMtmData2 & KMmsOperationResult ) )
       
   215             { 
       
   216             // It's been deleted succesfully
       
   217             status = EMsgStatusDeleted;
       
   218             }
       
   219         else 
       
   220             {   // Normal waiting state
       
   221             status = EMsgStatusReadyForFetching;
       
   222             }
       
   223         }
       
   224 
       
   225     return status;
       
   226     }
       
   227 
       
   228 // ---------------------------------------------------------
       
   229 // ConversationMsgStoreHandler::OperationOngoing
       
   230 // ---------------------------------------------------------
       
   231 //
       
   232 TBool ConversationMsgStoreHandler::OperationOngoing( const TMsvEntry& aEntry ) const
       
   233     {
       
   234     return (    aEntry.iMtmData2 & KMmsOperationOngoing 
       
   235             &&  !( aEntry.iMtmData2 & KMmsOperationFinished ) );
       
   236     }
       
   237 
       
   238 // ---------------------------------------------------------
       
   239 // ConversationMsgStoreHandler::OperationFinished
       
   240 // ---------------------------------------------------------
       
   241 //
       
   242 TBool ConversationMsgStoreHandler::OperationFinished( 
       
   243     const TMsvEntry& aEntry ) const
       
   244     {
       
   245     return (    aEntry.iMtmData2 & KMmsOperationFinished
       
   246             &&  !( aEntry.iMtmData2 & KMmsOperationOngoing ) );
       
   247     }
       
   248 
       
   249 // ---------------------------------------------------------
       
   250 // ConversationMsgStoreHandler::MarkMessagesReadL
       
   251 // ---------------------------------------------------------
       
   252 //
       
   253 void ConversationMsgStoreHandler::MarkMessagesReadL(RArray<TInt>& aIdArray)
       
   254     {    
       
   255     for ( int index = 0; index < aIdArray.Count(); index++ )
       
   256         {
       
   257         TMsvId id = aIdArray[index];
       
   258         CMsvEntry* cEntry = NULL;
       
   259         TRAPD(err, cEntry = iMsvSession->GetEntryL(id));
       
   260         if ( err == KErrNotFound )
       
   261             continue; // Entry is already deleted.
       
   262         TMsvEntry entry = cEntry->Entry();
       
   263         if ( entry.Unread() ) 
       
   264             {
       
   265             // Mark the entry as read
       
   266             entry.SetUnread( EFalse );
       
   267             cEntry->ChangeL( entry );
       
   268             }
       
   269         delete cEntry;
       
   270         }
       
   271     }
       
   272 
       
   273 // ---------------------------------------------------------
       
   274 // ConversationMsgStoreHandler::DeleteMessages
       
   275 // ---------------------------------------------------------
       
   276 //
       
   277 void ConversationMsgStoreHandler::DeleteMessages(RArray<TInt>& aIdArray)
       
   278 {
       
   279     for(int index=0;index < aIdArray.Count() ;++index)
       
   280     {
       
   281         TMsvId id = aIdArray[index];
       
   282         iMsvSession->RemoveEntry(id);
       
   283     }   
       
   284 }
       
   285 
       
   286 // ---------------------------------------------------------
       
   287 // ConversationMsgStoreHandler::DeleteMessagesL
       
   288 // ---------------------------------------------------------
       
   289 //
       
   290 CMsvSession& ConversationMsgStoreHandler::GetMsvSession()
       
   291 {
       
   292     return *iMsvSession;
       
   293 }
       
   294 
       
   295 // ---------------------------------------------------------
       
   296 // ConversationMsgStoreHandler::FetchDraftsMessages
       
   297 // ---------------------------------------------------------
       
   298 //
       
   299 void ConversationMsgStoreHandler::FetchDraftMessages(DraftsModel* draftsModel)
       
   300     {
       
   301     mDraftsModel = draftsModel;
       
   302     iState = EReadDrafts;
       
   303     TCallBack callback = TCallBack(ProcessDraftMessages, (TAny*) this);
       
   304     iIdle = CIdle::NewL(CActive::EPriorityStandard);
       
   305     iIdle->Start(callback);
       
   306     }
       
   307 
       
   308 // -----------------------------------------------------------------------------
       
   309 // ConversationMsgStoreHandler::ProcessDraftMessages
       
   310 // CIdle callback 
       
   311 // -----------------------------------------------------------------------------
       
   312 //
       
   313 TInt ConversationMsgStoreHandler::ProcessDraftMessages(TAny* aArg)
       
   314     {
       
   315     ConversationMsgStoreHandler* handler = (ConversationMsgStoreHandler*) aArg; 
       
   316     TInt ok = 0;
       
   317     TRAPD(err, ok = handler->ProcessDraftMessagesL());
       
   318     return ((err == KErrNone) && ok);
       
   319     }
       
   320 
       
   321 // ---------------------------------------------------------
       
   322 // ConversationMsgStoreHandler::ProcessDraftMessagesL
       
   323 // ---------------------------------------------------------
       
   324 //
       
   325 TInt ConversationMsgStoreHandler::ProcessDraftMessagesL()
       
   326     {
       
   327     switch(iState)
       
   328         {
       
   329         case EReadDrafts:
       
   330             {
       
   331             iDraftEntry = iMsvSession->GetEntryL(KMsvDraftEntryIdValue);
       
   332            
       
   333             iDraftMessages = iDraftEntry->ChildrenL();    
       
   334             iDraftMessageCount = iDraftEntry->Count();
       
   335 
       
   336             if ( iDraftMessageCount ) 
       
   337                 {
       
   338                 iState = EProcessDrafts;
       
   339                 }
       
   340             else
       
   341                 {
       
   342                 iState = EComplete; // no draft messages
       
   343                 mDraftsModel->setReady(); //ready to handle dynamic events
       
   344                 CleanupL();
       
   345                 return 0; //DONE
       
   346                 }
       
   347             return 1;
       
   348             }
       
   349         case EProcessDrafts:
       
   350             {
       
   351             if ( iDraftMessageCount ) 
       
   352                 {
       
   353                 iDraftMessageCount--;
       
   354                 TMsvEntry entry = iDraftEntry->ChildDataL(
       
   355                         iDraftMessages->At(iDraftMessageCount));
       
   356                 
       
   357                 // process only visible entries
       
   358                 if (entry.Visible() && IsMtmSupported(entry.iMtm.iUid))
       
   359                 {
       
   360                 //add message to model
       
   361                 mDraftsModel->addRow(entry);
       
   362                 }
       
   363                 //continue to process other draft messages
       
   364                 iState = EProcessDrafts;
       
   365                 }
       
   366             else
       
   367                 {
       
   368                 iState = EComplete;
       
   369                 mDraftsModel->setReady(); //ready to handle dynamic events
       
   370                 CleanupL();
       
   371                 return 0; // DONE
       
   372                 }
       
   373             return 1; 
       
   374             }
       
   375         }
       
   376 
       
   377     return 0;
       
   378     }
       
   379 
       
   380 // -----------------------------------------------------------------------------
       
   381 // ConversationMsgStoreHandler::CleanupL()
       
   382 // Helper function for state machine cleanup
       
   383 // -----------------------------------------------------------------------------
       
   384 //
       
   385 void ConversationMsgStoreHandler::CleanupL()
       
   386     {
       
   387     if ( iDraftEntry )
       
   388         {
       
   389         delete iDraftEntry;
       
   390         iDraftEntry = NULL;
       
   391         }
       
   392 
       
   393     if ( iDraftMessages )
       
   394         {
       
   395         iDraftMessages->Reset();
       
   396         delete iDraftMessages;
       
   397         iDraftMessages = NULL;
       
   398         }
       
   399 
       
   400     iDraftMessageCount = 0;
       
   401     }
       
   402 
       
   403 // -----------------------------------------------------------------------------
       
   404 // ConversationMsgStoreHandler::IsMtmSupported()
       
   405 // 
       
   406 // -----------------------------------------------------------------------------
       
   407 //
       
   408 TBool ConversationMsgStoreHandler::IsMtmSupported(long uid)	
       
   409     {
       
   410     if ( KSenduiMtmSmsUidValue == uid || KSenduiMtmMmsUidValue == uid )
       
   411         {
       
   412         return ETrue;
       
   413         }	
       
   414     return EFalse;
       
   415     }
       
   416 // End of file