mulwidgets/muldatamodel/src/mulasyncdataprovider.cpp
branchRCL_3
changeset 26 0e9bb658ef58
parent 0 e83bab7cf002
equal deleted inserted replaced
25:4ea6f81c838a 26:0e9bb658ef58
       
     1 /*
       
     2 * Copyright (c) 2007-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:  Active Object Implementation for providing data asynchronously
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "mulasyncdataprovider.h"
       
    20 
       
    21 #include <osn/osnnew.h>
       
    22 #include <algorithm> //for find algorithm
       
    23 
       
    24 #include "mullog.h"
       
    25 
       
    26 namespace Alf
       
    27     {
       
    28 
       
    29 // ---------------------------------------------------------------------------
       
    30 // MulAsyncDataProvider
       
    31 // ---------------------------------------------------------------------------
       
    32 //
       
    33 MulAsyncDataProvider::MulAsyncDataProvider( IMulModelProvider& aDataProvider,
       
    34                                             const MulPageDataWindow& aDataWindow )
       
    35                      :CActive( EPriorityNormal ),
       
    36                       mDataProvider(aDataProvider),
       
    37                       mDataWindow(aDataWindow)
       
    38     {
       
    39     MUL_LOG_ENTRY_EXIT("MUL::MulAsyncDataProvider::MulAsyncDataProvider");
       
    40     CActiveScheduler::Add(this);
       
    41     }
       
    42 
       
    43 // ---------------------------------------------------------------------------
       
    44 // ~MulAsyncDataProvider
       
    45 // ---------------------------------------------------------------------------
       
    46 //
       
    47 MulAsyncDataProvider::~MulAsyncDataProvider()
       
    48     {
       
    49     MUL_LOG_ENTRY_EXIT("MUL::MulAsyncDataProvider::~MulAsyncDataProvider");  
       
    50     Cancel();
       
    51     mDataQueue.clear();
       
    52     }
       
    53 
       
    54 // ---------------------------------------------------------------------------
       
    55 // RunL
       
    56 // ---------------------------------------------------------------------------
       
    57 //
       
    58 void MulAsyncDataProvider::RunL()
       
    59     {  
       
    60     vector<MulAsyncData>::iterator iter = mDataQueue.begin();
       
    61     if( iter < mDataQueue.end() )
       
    62         {
       
    63         bool inDataWindow = false;
       
    64         MulAsyncData asyncData = *iter;
       
    65         
       
    66         if(mDataWindow.IsItemInDataWindow(asyncData.mStartIndex) /*&& mDataWindow->IsItemInDataWindow(mStartIndex+ITEMS)*/)
       
    67             { 
       
    68             if( asyncData.mRange < mDataWindow.WindowSize() )
       
    69                 {
       
    70                 mDataProvider.ProvideData( asyncData.mStartIndex, asyncData.mRange, asyncData.mPath); 
       
    71                 }
       
    72             else
       
    73                 {
       
    74                 mDataProvider.ProvideData( asyncData.mStartIndex, mDataWindow.WindowSize() , asyncData.mPath); 
       
    75                 }
       
    76             inDataWindow = true;
       
    77             }
       
    78                   
       
    79         asyncData.mStartIndex += mDataWindow.WindowSize();
       
    80         asyncData.mRange -= mDataWindow.WindowSize();
       
    81         mDataQueue.erase(iter);
       
    82         
       
    83        if( asyncData.mRange > 0 && inDataWindow )
       
    84             {     
       
    85             mDataQueue.insert( iter, asyncData ); 
       
    86             if( !IsActive() )
       
    87                 {
       
    88                 ActivateAndComplete();
       
    89                 }
       
    90             }
       
    91          else if(mDataQueue.begin() < mDataQueue.end())
       
    92             {  
       
    93             if( !IsActive() )
       
    94                 {
       
    95                 ActivateAndComplete();
       
    96                 }
       
    97             }
       
    98         }     
       
    99     }
       
   100 
       
   101 // ---------------------------------------------------------------------------
       
   102 // DoCancel
       
   103 // ---------------------------------------------------------------------------
       
   104 //
       
   105 void MulAsyncDataProvider::DoCancel()
       
   106     {
       
   107     //does this function required??
       
   108     }
       
   109 //    
       
   110 // ---------------------------------------------------------------------------
       
   111 // ProvideData
       
   112 // ---------------------------------------------------------------------------
       
   113 //
       
   114 void MulAsyncDataProvider::ProvideData( int aStartIndex, int aRange, const MulDataPath& aPath, const bool aIsSync )
       
   115     { 
       
   116     if( aIsSync )
       
   117         {
       
   118         mDataProvider.ProvideData( aStartIndex, aRange, aPath);
       
   119         return;
       
   120         }
       
   121     MulAsyncData asyncData;
       
   122     asyncData.mStartIndex = aStartIndex;
       
   123     asyncData.mRange = aRange;
       
   124     asyncData.mPath = aPath;
       
   125     if( std::find( mDataQueue.begin(), mDataQueue.end(), asyncData ) ==  mDataQueue.end())
       
   126         {
       
   127         mDataQueue.push_back( asyncData );
       
   128         }
       
   129                  
       
   130     for( int i = 0 ; i < mDataQueue.size() ; ++i )
       
   131         {
       
   132         MulAsyncData data = mDataQueue[i]; 
       
   133         if ( (mDataWindow.IsItemInDataWindow( data.mStartIndex ) &&
       
   134               !mDataWindow.IsItemInDataWindow( data.mStartIndex + data.mRange - 1 ) ) )
       
   135             {
       
   136             int index = GetFirstIndexForWhichDataIsPresent(data.mStartIndex , data.mStartIndex + data.mRange - 1 );
       
   137             if( index!= -1 &&( index - data.mStartIndex + 1) > 0)
       
   138                 {
       
   139                 mDataQueue.erase(mDataQueue.begin() + i);
       
   140                 data.mRange = index - data.mStartIndex + 1;
       
   141                 mDataQueue.insert(mDataQueue.begin() + i, data);
       
   142                 }
       
   143             else
       
   144                 {
       
   145                 mDataQueue.erase(mDataQueue.begin() + i);
       
   146                 i--;
       
   147                 }    
       
   148             
       
   149             }
       
   150         else if ( (!mDataWindow.IsItemInDataWindow( data.mStartIndex ) &&
       
   151               mDataWindow.IsItemInDataWindow( data.mStartIndex + data.mRange - 1 ) ) )
       
   152             {
       
   153             int index = GetFirstIndexForWhichDataIsPresent(data.mStartIndex , data.mStartIndex + data.mRange - 1 );
       
   154             if( index!= -1 &&( data.mStartIndex + data.mRange - index ) > 0 )
       
   155                 {
       
   156                 mDataQueue.erase(mDataQueue.begin() + i);
       
   157                 int endIndex = (data.mStartIndex + data.mRange - 1);
       
   158                 data.mStartIndex = index;
       
   159                 data.mRange = (endIndex - index + 1);
       
   160                 mDataQueue.insert(mDataQueue.begin() + i, data);
       
   161                 }
       
   162             else
       
   163                 {
       
   164                 mDataQueue.erase(mDataQueue.begin() + i);
       
   165                 i--;
       
   166                 }
       
   167             }
       
   168         else if ( (!mDataWindow.IsItemInDataWindow( data.mStartIndex ) &&
       
   169               !mDataWindow.IsItemInDataWindow( data.mStartIndex + data.mRange - 1 ) ) )
       
   170            {
       
   171            mDataQueue.erase(mDataQueue.begin() + i);
       
   172            i--;
       
   173            MUL_LOG_INFO2( "MUL::MulAsyncDataProvider::ProvideData Removing From Q: startIndx=%d,range=%d", data.mStartIndex, data.mRange ); 
       
   174            }
       
   175         }
       
   176     
       
   177     if( !IsActive() )
       
   178         { 
       
   179         ActivateAndComplete();
       
   180         }
       
   181     }
       
   182 // ---------------------------------------------------------------------------
       
   183 // GetFirstIndexForWhichDataIsPresent
       
   184 // ---------------------------------------------------------------------------
       
   185 //
       
   186 int MulAsyncDataProvider::GetFirstIndexForWhichDataIsPresent(int aStartIndex, int aEndIndex)
       
   187     {
       
   188     if ( aStartIndex > aEndIndex )
       
   189         {
       
   190         return -1;
       
   191         }
       
   192     int mid = (aStartIndex + aEndIndex)/2;
       
   193     if(mDataWindow.IsItemInDataWindow(aStartIndex))
       
   194         {
       
   195         if(mid == aStartIndex)
       
   196             {
       
   197             return mid;
       
   198             }
       
   199         if(mDataWindow.IsItemInDataWindow(mid))
       
   200             {
       
   201             return GetFirstIndexForWhichDataIsPresent(mid, aEndIndex);
       
   202             }
       
   203         else
       
   204             {
       
   205             return GetFirstIndexForWhichDataIsPresent(aStartIndex, mid);
       
   206             }
       
   207         }
       
   208      else if(mDataWindow.IsItemInDataWindow(aEndIndex))
       
   209         {
       
   210         if(mid == aStartIndex)
       
   211             {
       
   212             return (mid + 1);
       
   213             }
       
   214         if(mDataWindow.IsItemInDataWindow(mid))
       
   215             {
       
   216             return GetFirstIndexForWhichDataIsPresent(aStartIndex, mid);
       
   217             }
       
   218         else
       
   219             {
       
   220             return GetFirstIndexForWhichDataIsPresent(mid, aEndIndex);
       
   221             }           
       
   222         }   
       
   223     return -1;
       
   224     }
       
   225   
       
   226 // ---------------------------------------------------------------------------
       
   227 // ActivateAndComplete
       
   228 // ---------------------------------------------------------------------------
       
   229 //
       
   230  void MulAsyncDataProvider::ActivateAndComplete()
       
   231     {
       
   232     TRequestStatus* lStatus = &iStatus;
       
   233     iStatus = KRequestPending;
       
   234     SetActive();
       
   235     User::RequestComplete(lStatus, KErrNone);
       
   236     }
       
   237  
       
   238     } // End of namespace Alf .
       
   239 
       
   240 
       
   241 // End of file