hgcacheproxymodel/tsrc/unit/bmhelper.cpp
changeset 1 e48454f237ca
child 2 49c70dcc3f17
equal deleted inserted replaced
0:89c329efa980 1:e48454f237ca
       
     1 /*
       
     2 * Copyright (c) 2009 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 *  Version     : %version: 1 %
       
    17 */
       
    18 #include "bmhelper.h"
       
    19 #include <QDebug>
       
    20 
       
    21 BMHelper::BMHelper(int totalSize)
       
    22 {
       
    23     for ( int i(0); i < totalSize; i++)
       
    24         mBuffer.append(false);
       
    25 }
       
    26 
       
    27 BMHelper::~BMHelper()
       
    28 {
       
    29 }
       
    30 
       
    31 void BMHelper::release(int start, int end)
       
    32 {
       
    33     if ( start<0)
       
    34         start = 0;
       
    35     if (end>mBuffer.size() - 1)
       
    36         end = mBuffer.size() - 1;
       
    37     
       
    38     for ( int i = start; i <= end; i++){
       
    39         mBuffer.replace(i, false);
       
    40     }
       
    41 }
       
    42 
       
    43 void BMHelper::request(int start, int end, HgRequestOrder order)
       
    44 {
       
    45     Q_UNUSED(order);
       
    46     if ( start<0)
       
    47         start = 0;
       
    48     if (end>mBuffer.size() - 1)
       
    49         end = mBuffer.size() - 1;
       
    50 
       
    51     for ( int i = start; i <= end; i++){
       
    52         mBuffer.replace(i, true);
       
    53     }
       
    54 }
       
    55 
       
    56 bool BMHelper::isIntergal(int bufferSize)
       
    57 {
       
    58     int c = mBuffer.count(true);
       
    59     bool res = (bufferSize == c);
       
    60     if (res){ ///check integrity ( if all items from first true, to size are true;
       
    61         int f = mBuffer.indexOf(true);
       
    62         for ( int i =0; i < mBuffer.count(); i++){
       
    63             if (mBuffer[i] != (i>=f && i < f+bufferSize) ){
       
    64                 res = false;
       
    65                 break;
       
    66             }
       
    67         }
       
    68     } else {
       
    69         qWarning()<<QString("isIntergal mBuffer.count(true)=%1 bufferSize=%2").arg(c).arg(bufferSize);
       
    70     }
       
    71     
       
    72     return res;
       
    73 }
       
    74 
       
    75 void BMHelper::itemCountChanged( int aIndex, bool aRemoved, int aNewTotalCount )
       
    76 {
       
    77     Q_UNUSED(aRemoved);
       
    78     
       
    79     if ( aIndex < 0)
       
    80         aIndex = 0;
       
    81     if ( aIndex > mBuffer.count())
       
    82         aIndex = mBuffer.count()-1;
       
    83     
       
    84     if ((mBuffer.count() - aNewTotalCount)>0){
       
    85         while (mBuffer.count()!=aNewTotalCount){
       
    86             if (aIndex > mBuffer.count() )
       
    87                 aIndex = mBuffer.count() -1;
       
    88             mBuffer.removeAt(aIndex);
       
    89         }
       
    90     } else if ((mBuffer.count() - aNewTotalCount)<0){
       
    91         while (mBuffer.count()!=aNewTotalCount){
       
    92             mBuffer.insert(aIndex, false);
       
    93         }
       
    94     }
       
    95 }