messagingapp/msgui/appengine/src/conversationchangehandler.cpp
changeset 23 238255e8b033
child 34 84197e66a4bd
child 37 518b245aa84c
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:  ?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)
       
    65 {
       
    66     mConversationEntryList.ResetAndDestroy();
       
    67 
       
    68     for (TInt i = 0; i < aConversationEntryList.Count(); ++i)
       
    69     {
       
    70         CCsConversationEntry *entry = aConversationEntryList[i]->CloneL();
       
    71         mConversationEntryList.AppendL(entry);
       
    72     }
       
    73     if (aConversationEntryList.Count() > 0)
       
    74     {
       
    75         mFirstWindowCached = EFalse;
       
    76         mCurrentIndex = 0;
       
    77         mCurrentState = EInitialCache;
       
    78         IssueRequest();
       
    79     }
       
    80     else
       
    81     {
       
    82         //This is done for safety
       
    83         mConvEnginePrivate->registerForConversationUpdatesL();
       
    84         mCurrentState = EListenToEvents;
       
    85     }
       
    86 }
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 // Handling addition of new conversation entry from the server
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 void ConversationsChangeHandler::AddConversation(
       
    93     const CCsConversationEntry& aConvEntry)
       
    94 {
       
    95     mConversationsModel->addRow(aConvEntry, true);
       
    96     ConversationsEngine::instance()->emitConversationModelUpdated();
       
    97 }
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // Handling modification of existing conversation entry from the server
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 void ConversationsChangeHandler::ModifyConversation(
       
   104     const CCsConversationEntry& aConvEntry)
       
   105 {
       
   106     mConversationsModel->addRow(aConvEntry, true);
       
   107     ConversationsEngine::instance()->emitConversationModelUpdated();
       
   108 }
       
   109 
       
   110 // ---------------------------------------------------------------------------
       
   111 // Handling deletion of conversation entry from the server
       
   112 // ---------------------------------------------------------------------------
       
   113 //
       
   114 void ConversationsChangeHandler::DeleteConversation(
       
   115     const CCsConversationEntry& aConvEntry)
       
   116 {
       
   117     mConversationsModel->deleteRow(aConvEntry.EntryId());
       
   118     ConversationsEngine::instance()->emitConversationModelUpdated();
       
   119 }
       
   120 
       
   121 //-----------------------------------------------------------------------
       
   122 // This is for handling modify conversation event asynchronusly from the server
       
   123 //-----------------------------------------------------------------------
       
   124 //
       
   125 void ConversationsChangeHandler::RefreshConversation()
       
   126 {
       
   127 }
       
   128 
       
   129 // ---------------------------------------------------------------------------
       
   130 // RunL
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 void ConversationsChangeHandler::RunL()
       
   134 {
       
   135     if (iStatus != KErrNone)
       
   136     {
       
   137         return;
       
   138     }
       
   139     //process
       
   140     switch (mCurrentState)
       
   141     {
       
   142         case EInitialCache:
       
   143             HandleConversationsL();
       
   144             break;
       
   145     }
       
   146 }
       
   147 
       
   148 // ---------------------------------------------------------------------------
       
   149 // DoCancel
       
   150 // ---------------------------------------------------------------------------
       
   151 //
       
   152 void ConversationsChangeHandler::DoCancel()
       
   153 {
       
   154     mCurrentState = EListenToEvents;
       
   155 }
       
   156 
       
   157 // ---------------------------------------------------------------------------
       
   158 // Make the active object alive.
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 void ConversationsChangeHandler::IssueRequest()
       
   162 {
       
   163     if (!IsActive())
       
   164     {
       
   165         iStatus = KRequestPending;
       
   166         TRequestStatus* status = &iStatus;
       
   167         SetActive();
       
   168         User::RequestComplete(status, KErrNone);
       
   169     }
       
   170 }
       
   171 
       
   172 // ---------------------------------------------------------------------------
       
   173 //  Handles Conversations received from server and updates into model
       
   174 // ---------------------------------------------------------------------------
       
   175 //
       
   176 void ConversationsChangeHandler::HandleConversationsL()
       
   177 {
       
   178     for (int i = 0; i < WindowSize; ++i)
       
   179     {
       
   180         if (mCurrentIndex < mConversationEntryList.Count())
       
   181         {
       
   182             mConversationsModel->addRow(
       
   183                 * (mConversationEntryList[mCurrentIndex]));
       
   184             mCurrentIndex++;
       
   185         }
       
   186         else
       
   187         {
       
   188             break;
       
   189         }
       
   190     }
       
   191     if (mCurrentIndex < mConversationEntryList.Count())
       
   192     {
       
   193         if (!mFirstWindowCached)
       
   194         {
       
   195             ConversationsEngine::instance()->emitConversationModelPopulated();
       
   196             mFirstWindowCached = ETrue;
       
   197             return;
       
   198         }
       
   199         IssueRequest();
       
   200     }
       
   201     else
       
   202     {
       
   203         if (!mFirstWindowCached)
       
   204         {
       
   205             ConversationsEngine::instance()->emitConversationModelPopulated();
       
   206             mFirstWindowCached = ETrue;
       
   207         }
       
   208         mConversationEntryList.ResetAndDestroy();
       
   209         mConvEnginePrivate->registerForConversationUpdatesL();
       
   210         mCurrentState = EListenToEvents;
       
   211     }
       
   212 }
       
   213 
       
   214 // ---------------------------------------------------------------------------
       
   215 //  Starts fetching remaining conversations
       
   216 // ---------------------------------------------------------------------------
       
   217 //
       
   218 void ConversationsChangeHandler::restartHandleConversations()
       
   219 {
       
   220     IssueRequest();
       
   221 }
       
   222 
       
   223 //EOF