messagingapp/msgui/appengine/src/conversationchangehandler.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:  ?Description
       
    15  *
       
    16  */
       
    17 
       
    18 #include "conversationchangehandler.h"
       
    19 // SYSTEM INCLUDES
       
    20 #include <ccsconversationentry.h>
       
    21 
       
    22 // USER INCLUDES
       
    23 #include "conversationsengine.h"
       
    24 #include "conversationsengine_p.h"
       
    25 #include "conversationsmodel.h"
       
    26 
       
    27 const TInt WindowSize = 7;
       
    28 
       
    29 // ---------------------------------------------------------------------------
       
    30 // Constructor
       
    31 // ---------------------------------------------------------------------------
       
    32 //
       
    33 ConversationsChangeHandler::ConversationsChangeHandler(
       
    34     ConversationsEnginePrivate* convEnginePrivate,
       
    35     ConversationsModel* conversationsModel) :
       
    36     CActive(EPriorityStandard), 
       
    37     mCurrentState(EInit),
       
    38     mFirstWindowCached(EFalse),
       
    39     mCurrentIndex(-1), 
       
    40     mConversationsModel(conversationsModel), 
       
    41     mConvEnginePrivate(convEnginePrivate)
       
    42 
       
    43 {
       
    44     CActiveScheduler::Add(this);
       
    45 }
       
    46 
       
    47 // ---------------------------------------------------------------------------
       
    48 // Destructor
       
    49 // ---------------------------------------------------------------------------
       
    50 //
       
    51 ConversationsChangeHandler::~ConversationsChangeHandler()
       
    52 {
       
    53     mConversationEntryList.ResetAndDestroy();
       
    54     // Cancel the active object
       
    55     Cancel();
       
    56 }
       
    57 
       
    58 // ---------------------------------------------------------------------------
       
    59 // This is for handling GetConversation results asynchronusly
       
    60 // from the server.
       
    61 // ---------------------------------------------------------------------------
       
    62 //
       
    63 void ConversationsChangeHandler::ConversationsL(RPointerArray<
       
    64         CCsConversationEntry>& aConversationEntryList, TInt& aTotalCount)
       
    65 {
       
    66     
       
    67     mTotalCount = aTotalCount;
       
    68 
       
    69     for (TInt i = 0; i < aConversationEntryList.Count(); ++i)
       
    70     {
       
    71         CCsConversationEntry *entry = aConversationEntryList[i]->CloneL();
       
    72         mConversationEntryList.AppendL(entry);
       
    73     }
       
    74     if (aConversationEntryList.Count() > 0)
       
    75     {
       
    76         mCurrentState = EInitialCache;
       
    77         IssueRequest();
       
    78     }
       
    79     else
       
    80     {
       
    81         //This is done for safety
       
    82         mConvEnginePrivate->registerForConversationUpdatesL();
       
    83         mCurrentState = EListenToEvents;
       
    84     }
       
    85 }
       
    86 // ---------------------------------------------------------------------------
       
    87 // This is for resetting the values before initiating a request 
       
    88 // for fetching entries for a new conversation
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 void ConversationsChangeHandler::ResetValuesForNewConversation()
       
    92 {
       
    93 	mConvEnginePrivate->registerForConversationUpdatesL();
       
    94     mCurrentIndex = 0;
       
    95     mFirstWindowCached = EFalse;
       
    96     mConversationEntryList.ResetAndDestroy();
       
    97     
       
    98 
       
    99 }
       
   100 
       
   101 // ---------------------------------------------------------------------------
       
   102 // Handling addition of new conversation entry from the server
       
   103 // ---------------------------------------------------------------------------
       
   104 //
       
   105 void ConversationsChangeHandler::AddConversation(
       
   106     const CCsConversationEntry& aConvEntry)
       
   107 {
       
   108     mConversationsModel->addRow(aConvEntry, true);
       
   109     ConversationsEngine::instance()->emitConversationModelUpdated();
       
   110 }
       
   111 
       
   112 // ---------------------------------------------------------------------------
       
   113 // Handling modification of existing conversation entry from the server
       
   114 // ---------------------------------------------------------------------------
       
   115 //
       
   116 void ConversationsChangeHandler::ModifyConversation(
       
   117     const CCsConversationEntry& aConvEntry)
       
   118 {
       
   119     mConversationsModel->addRow(aConvEntry, true);
       
   120 }
       
   121 
       
   122 // ---------------------------------------------------------------------------
       
   123 // Handling deletion of conversation entry from the server
       
   124 // ---------------------------------------------------------------------------
       
   125 //
       
   126 void ConversationsChangeHandler::DeleteConversation(
       
   127     const CCsConversationEntry& aConvEntry)
       
   128 {
       
   129     mConversationsModel->deleteRow(aConvEntry.EntryId());
       
   130     
       
   131     //check if the CV model row count has become zero
       
   132     // then needs to emit signal
       
   133     if (mConversationsModel->rowCount() == 0)
       
   134     {
       
   135         mConversationsModel->emitConversationViewEmpty();
       
   136     }
       
   137 }
       
   138 
       
   139 //-----------------------------------------------------------------------
       
   140 // This is for handling modify conversation event asynchronusly from the server
       
   141 //-----------------------------------------------------------------------
       
   142 //
       
   143 void ConversationsChangeHandler::RefreshConversation()
       
   144 {
       
   145 }
       
   146 
       
   147 // ---------------------------------------------------------------------------
       
   148 // RunL
       
   149 // ---------------------------------------------------------------------------
       
   150 //
       
   151 void ConversationsChangeHandler::RunL()
       
   152 {
       
   153     if (iStatus != KErrNone)
       
   154     {
       
   155         return;
       
   156     }
       
   157     //process
       
   158     switch (mCurrentState)
       
   159     {
       
   160         case EInitialCache:
       
   161             HandleConversationsL();
       
   162             break;
       
   163         case EFetchMoreConversations:
       
   164             FetchRemainingConversations(mConversationEntryList.Count());
       
   165             break;
       
   166     }
       
   167 }
       
   168 
       
   169 // ---------------------------------------------------------------------------
       
   170 // DoCancel
       
   171 // ---------------------------------------------------------------------------
       
   172 //
       
   173 void ConversationsChangeHandler::DoCancel()
       
   174 {
       
   175     mCurrentState = EListenToEvents;
       
   176 }
       
   177 
       
   178 // ---------------------------------------------------------------------------
       
   179 // Make the active object alive.
       
   180 // ---------------------------------------------------------------------------
       
   181 //
       
   182 void ConversationsChangeHandler::IssueRequest()
       
   183 {
       
   184     if (!IsActive())
       
   185     {
       
   186         iStatus = KRequestPending;
       
   187         TRequestStatus* status = &iStatus;
       
   188         SetActive();
       
   189         User::RequestComplete(status, KErrNone);
       
   190     }
       
   191 }
       
   192 
       
   193 // ---------------------------------------------------------------------------
       
   194 //  Handles Conversations received from server and updates into model
       
   195 // ---------------------------------------------------------------------------
       
   196 //
       
   197 void ConversationsChangeHandler::HandleConversationsL()
       
   198 {
       
   199     for (int i = 0; i < WindowSize; ++i)
       
   200     {
       
   201         if (mCurrentIndex < mConversationEntryList.Count())
       
   202         {
       
   203             mConversationsModel->addRow(
       
   204                 * (mConversationEntryList[mCurrentIndex]));
       
   205             mCurrentIndex++;
       
   206         }
       
   207         else
       
   208         {
       
   209             break;
       
   210         }
       
   211     }
       
   212     if (mCurrentIndex < mConversationEntryList.Count())
       
   213     {
       
   214         if (!mFirstWindowCached)
       
   215         {
       
   216             ConversationsEngine::instance()->emitConversationModelPopulated();
       
   217             mFirstWindowCached = ETrue;
       
   218             return;
       
   219         }
       
   220         IssueRequest();
       
   221         return;
       
   222     }
       
   223     //if more entries have to be fetched , issue a request,
       
   224     // else listen for events.
       
   225     if(mCurrentIndex < mTotalCount )
       
   226     {
       
   227         //fetch more
       
   228         mCurrentState = EFetchMoreConversations;
       
   229         IssueRequest();
       
   230     }
       
   231     else
       
   232     {
       
   233         if (!mFirstWindowCached)
       
   234         {
       
   235             ConversationsEngine::instance()->emitConversationModelPopulated();
       
   236             mFirstWindowCached = ETrue;
       
   237         }
       
   238         mConversationEntryList.ResetAndDestroy();
       
   239         mCurrentState = EListenToEvents;
       
   240     }
       
   241 }
       
   242 
       
   243 // ---------------------------------------------------------------------------
       
   244 //  Fetches remaining conversations from the server 
       
   245 // ---------------------------------------------------------------------------
       
   246 //
       
   247 void ConversationsChangeHandler::FetchRemainingConversations(TInt aTotalCount)
       
   248 {
       
   249     mConvEnginePrivate->fetchRemainingConversations(aTotalCount);
       
   250     
       
   251 }
       
   252 // ---------------------------------------------------------------------------
       
   253 //  Starts fetching remaining conversations
       
   254 // ---------------------------------------------------------------------------
       
   255 //
       
   256 void ConversationsChangeHandler::restartHandleConversations()
       
   257 {
       
   258     IssueRequest();
       
   259 }
       
   260 
       
   261 //EOF