messagingapp/msgui/appengine/src/conversationlistchangehandler.cpp
branchRCL_3
changeset 26 ebe688cedc25
equal deleted inserted replaced
25:fa1df4b99609 26:ebe688cedc25
       
     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:  This Class handles Add, Delete & Modify conversation list client events.
       
    15  *
       
    16  */
       
    17 
       
    18 #include "conversationlistchangehandler.h"
       
    19 
       
    20 // SYSTEM INCLUDES
       
    21 #include <ccsconversationentry.h>
       
    22 #include <ccsclientconversation.h>
       
    23 // USER INCLUDE
       
    24 #include "conversationsengine_p.h"
       
    25 #include "conversationsengine.h"
       
    26 #include "conversationssummarymodel.h"
       
    27 
       
    28 TInt ListWindowSize = 7;
       
    29 // ---------------------------------------------------------------------------
       
    30 // Constructor
       
    31 // ---------------------------------------------------------------------------
       
    32 //
       
    33 ConversationsListChangeHandler::ConversationsListChangeHandler(
       
    34     ConversationsEnginePrivate* convEnginePrivate,
       
    35     ConversationsSummaryModel* conversationsSummaryModel) :
       
    36     CActive(EPriorityStandard), 
       
    37     mCurrentState(EInit),
       
    38     mFirstWindowCached(EFalse),
       
    39     mCurrentIndex(-1),     
       
    40     mConvSummaryModel(conversationsSummaryModel), 
       
    41     mConvEnginePrivate(convEnginePrivate)
       
    42 {
       
    43     CActiveScheduler::Add(this);
       
    44 }
       
    45 
       
    46 // ---------------------------------------------------------------------------
       
    47 // Destructor
       
    48 // ---------------------------------------------------------------------------
       
    49 //
       
    50 ConversationsListChangeHandler::~ConversationsListChangeHandler()
       
    51 {
       
    52     mClientConversationList.ResetAndDestroy();
       
    53     // Cancel the active object
       
    54     Cancel();
       
    55 }
       
    56 
       
    57 //-----------------------------------------------------------------------
       
    58 // From CActive class.
       
    59 //-----------------------------------------------------------------------
       
    60 //
       
    61 void ConversationsListChangeHandler::RunL()
       
    62 {
       
    63     if (iStatus != KErrNone)
       
    64     {
       
    65         // Reading events has failed
       
    66         return;
       
    67     }
       
    68     else
       
    69     {
       
    70         switch (mCurrentState)
       
    71         {
       
    72             case EInitialCache:
       
    73             {
       
    74                 HandleConversationListL();
       
    75                 break;
       
    76             }
       
    77         }
       
    78     }
       
    79 }
       
    80 
       
    81 //-----------------------------------------------------------------------
       
    82 // From CActive class.
       
    83 //-----------------------------------------------------------------------
       
    84 //
       
    85 void ConversationsListChangeHandler::DoCancel()
       
    86 {
       
    87     mCurrentState = EListenToEvents;
       
    88 }
       
    89 
       
    90 //-----------------------------------------------------------------------
       
    91 // From CActive class.
       
    92 //-----------------------------------------------------------------------
       
    93 //
       
    94 void ConversationsListChangeHandler::ConversationListL(RPointerArray<
       
    95         CCsClientConversation>& aClientConversationList)
       
    96 {
       
    97     mClientConversationList.ResetAndDestroy();
       
    98     
       
    99     for (TInt i = 0; i < aClientConversationList.Count(); ++i)
       
   100     {        
       
   101         CCsClientConversation *entry = aClientConversationList[i]->CloneL();
       
   102         mClientConversationList.InsertInOrderAllowRepeats( entry,
       
   103                 ConversationsListChangeHandler::CompareByConvTimeStamp );
       
   104     }
       
   105 
       
   106     if (aClientConversationList.Count() > 0)
       
   107     {
       
   108         mCurrentIndex = 0;
       
   109         mFirstWindowCached = EFalse;
       
   110         mCurrentState = EInitialCache;
       
   111         IssueRequest();
       
   112     }
       
   113     else
       
   114     {
       
   115         mConvEnginePrivate->registerForConversationListUpdatesL();
       
   116         mCurrentState = EListenToEvents;
       
   117     }
       
   118 }
       
   119 
       
   120 //-----------------------------------------------------------------------
       
   121 // This is for handling new conversation event asynchronusly from the server
       
   122 //-----------------------------------------------------------------------
       
   123 //
       
   124 void ConversationsListChangeHandler::AddConversationList(
       
   125     const CCsClientConversation& aClientConversation)
       
   126 {
       
   127     mConvSummaryModel->addRow(aClientConversation);
       
   128 }
       
   129 
       
   130 //-----------------------------------------------------------------------
       
   131 // This is for handling delete conversation event asynchronusly from the server
       
   132 //-----------------------------------------------------------------------
       
   133 //
       
   134 void ConversationsListChangeHandler::DeleteConversationList(
       
   135     const CCsClientConversation& aClientConversation)
       
   136 {
       
   137     mConvSummaryModel->deleteRow(aClientConversation.GetConversationEntryId());
       
   138 }
       
   139 
       
   140 //-----------------------------------------------------------------------
       
   141 // This is for handling partial delete conversation event asynchronusly from the server
       
   142 //-----------------------------------------------------------------------
       
   143 //
       
   144 
       
   145 void ConversationsListChangeHandler::PartialDeleteConversationList(
       
   146             const CCsClientConversation& aClientConversation)
       
   147 {
       
   148     ConversationsEngine::instance()->emitConversationListModelEntryDeleted( aClientConversation.GetConversationEntryId() );
       
   149 }
       
   150 
       
   151 //-----------------------------------------------------------------------
       
   152 // This is for handling modify conversation event asynchronusly from the server
       
   153 //-----------------------------------------------------------------------
       
   154 //
       
   155 void ConversationsListChangeHandler::ModifyConversationList(
       
   156     const CCsClientConversation& aClientConversation)
       
   157 {
       
   158     mConvSummaryModel->addRow(aClientConversation);
       
   159 }
       
   160 
       
   161 //-----------------------------------------------------------------------
       
   162 // This is for handling refresh conversation event asynchronusly from the server
       
   163 //-----------------------------------------------------------------------
       
   164 //
       
   165 void ConversationsListChangeHandler::RefreshConversationList()
       
   166 {
       
   167 }
       
   168 
       
   169 // ---------------------------------------------------------------------------
       
   170 // Make the active object alive.
       
   171 // ---------------------------------------------------------------------------
       
   172 //
       
   173 void ConversationsListChangeHandler::IssueRequest()
       
   174 {
       
   175     if (!IsActive())
       
   176     {
       
   177         iStatus = KRequestPending;
       
   178         TRequestStatus* status = &iStatus;
       
   179         SetActive();
       
   180         User::RequestComplete(status, KErrNone);
       
   181     }
       
   182 }
       
   183 
       
   184 // ---------------------------------------------------------------------------
       
   185 // Handles Conversation List received from server and updates into model
       
   186 // ---------------------------------------------------------------------------
       
   187 //
       
   188 void ConversationsListChangeHandler::HandleConversationListL()
       
   189 {
       
   190     for (int i = 0; i < ListWindowSize; ++i)
       
   191     {
       
   192         if (mCurrentIndex < mClientConversationList.Count())
       
   193         {
       
   194             mConvSummaryModel->addRow(
       
   195                 * (mClientConversationList[mCurrentIndex]),true);
       
   196             mCurrentIndex++;
       
   197         }
       
   198         else
       
   199         {
       
   200             break;
       
   201         }
       
   202     }
       
   203     if (mCurrentIndex < mClientConversationList.Count())
       
   204     {
       
   205         if (!mFirstWindowCached)
       
   206         {
       
   207             ConversationsEngine::instance()->
       
   208                                            emitConversationListModelPopulated();
       
   209             mFirstWindowCached = ETrue;
       
   210         }
       
   211         IssueRequest();
       
   212     }
       
   213     else
       
   214     {
       
   215         if (!mFirstWindowCached)
       
   216         {
       
   217             ConversationsEngine::instance()->
       
   218                                            emitConversationListModelPopulated();
       
   219             mFirstWindowCached = ETrue;
       
   220         }
       
   221         mClientConversationList.ResetAndDestroy();
       
   222         mConvEnginePrivate->registerForConversationListUpdatesL();
       
   223         mCurrentState = EListenToEvents;
       
   224     }
       
   225 
       
   226 }
       
   227 
       
   228 // ----------------------------------------------------------------------------
       
   229 // ConversationsListChangeHandler::CompareByConvTimeStamp
       
   230 // ---------------------------------------------------------------------------
       
   231 TInt ConversationsListChangeHandler::CompareByConvTimeStamp(const CCsClientConversation& aObj1,
       
   232         const CCsClientConversation& aObj2)
       
   233     {
       
   234     TInt64 timestamp1 = aObj1.GetConversationEntry()->TimeStamp();
       
   235     TInt64 timestamp2 = aObj2.GetConversationEntry()->TimeStamp();
       
   236 
       
   237     if (timestamp1 == timestamp2)
       
   238         {
       
   239         return 0;
       
   240         }
       
   241     else if (timestamp1 < timestamp2)
       
   242         {
       
   243         return 1;
       
   244         }
       
   245     return -1;
       
   246     }
       
   247 
       
   248 // EOF