videocollection/videocollectionwrapper/tsrc/testvideoproxymodelgeneric/stub/src/videolistdatamodel.cpp
changeset 62 0e1e938beb1a
equal deleted inserted replaced
59:a76e86df7ccd 62:0e1e938beb1a
       
     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: Stub model to be used when unit testing proxy model. 
       
    15 * 
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include "videocollectionclient.h"
       
    21 #include "videolistdatamodel.h"
       
    22 
       
    23 bool VideoListDataModel::mInitFails = false;
       
    24 bool VideoListDataModel::mRemoveRowsFails = false;
       
    25 bool VideoListDataModel::mGetMediaIdAtIndexFails = false;
       
    26 bool VideoListDataModel::mBelongsToAlbum = false;
       
    27 bool VideoListDataModel::mReturnInvalid = false;
       
    28 int VideoListDataModel::mRemoveFrAlbumReturn = -1;
       
    29 int VideoListDataModel::mLastDeletedIndexRow = -1;
       
    30 QString VideoListDataModel::mMediaFilePathReturnValue = "";
       
    31 bool VideoListDataModel::mGetCollectionClientFails = false;
       
    32 
       
    33 TMPXItemId gInvalidId  = TMPXItemId::InvalidId();
       
    34 
       
    35 // ---------------------------------------------------------------------------
       
    36 // VideoListDataModel
       
    37 // ---------------------------------------------------------------------------
       
    38 //
       
    39 VideoListDataModel::VideoListDataModel(QObject *parent) :
       
    40 QAbstractItemModel(parent),
       
    41  mCollectionClient(0)
       
    42 {
       
    43     
       
    44 }
       
    45 
       
    46 // ---------------------------------------------------------------------------
       
    47 // ~VideoListDataModel
       
    48 // ---------------------------------------------------------------------------
       
    49 //
       
    50 VideoListDataModel::~VideoListDataModel()
       
    51 {
       
    52     removeAll();
       
    53    
       
    54     delete mCollectionClient;
       
    55 }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // initialize
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 int VideoListDataModel::initialize()
       
    62 {
       
    63     if(mInitFails)
       
    64     {
       
    65         return -1;
       
    66     }
       
    67     return 0;
       
    68 }
       
    69 
       
    70 // ---------------------------------------------------------------------------
       
    71 // mediaIdAtIndex
       
    72 // ---------------------------------------------------------------------------
       
    73 //
       
    74 TMPXItemId &VideoListDataModel::mediaIdAtIndex(int index) const
       
    75 {
       
    76 	if( mGetMediaIdAtIndexFails || index < 0 || index >= mData.count())
       
    77 	{
       
    78 		return gInvalidId;
       
    79 	}
       
    80 	
       
    81 	return mData.at(index)->mId;
       
    82 }
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 // mediaFilePathForId
       
    86 // ---------------------------------------------------------------------------
       
    87 //
       
    88 QString VideoListDataModel::mediaFilePathForId(int mediaId) const
       
    89 {
       
    90     Q_UNUSED(mediaId);
       
    91     return mMediaFilePathReturnValue;
       
    92 }
       
    93 
       
    94 // ---------------------------------------------------------------------------
       
    95 // removeRows
       
    96 // ---------------------------------------------------------------------------
       
    97 //
       
    98 bool VideoListDataModel::removeRows(const QModelIndexList &indexList)
       
    99 {
       
   100     mLastDeletedIndexRow = -1;
       
   101     if( mRemoveRowsFails )
       
   102     	return false;
       
   103     else
       
   104     {
       
   105         if(indexList.count() > 0)
       
   106         {
       
   107             mLastDeletedIndexRow = indexList.at(indexList.count() - 1).row();
       
   108         }
       
   109         QModelIndexList sortable(indexList);
       
   110         qSort(sortable);
       
   111         QModelIndexList::const_iterator iter = sortable.constEnd();
       
   112         QModelIndex index;
       
   113         while(iter != sortable.constBegin())
       
   114         {
       
   115             iter--;
       
   116             index = (*iter); 
       
   117             beginRemoveRows(QModelIndex(), index.row(), index.row());
       
   118 
       
   119             delete mData.at(index.row());
       
   120             mData.removeAt(index.row());
       
   121             
       
   122             endRemoveRows();
       
   123         }
       
   124         
       
   125     	return true;
       
   126     }
       
   127 }
       
   128 
       
   129 // ---------------------------------------------------------------------------
       
   130 // getCollectionClient
       
   131 // ---------------------------------------------------------------------------
       
   132 //
       
   133 VideoCollectionClient* VideoListDataModel::getCollectionClient()
       
   134 {
       
   135     if(mGetCollectionClientFails)
       
   136     {
       
   137         return 0;
       
   138     }
       
   139     
       
   140     if(!mCollectionClient)
       
   141     {
       
   142         mCollectionClient = new VideoCollectionClient();
       
   143     }
       
   144     return mCollectionClient;
       
   145 }
       
   146 
       
   147 // ---------------------------------------------------------------------------
       
   148 // rowCount
       
   149 // ---------------------------------------------------------------------------
       
   150 //
       
   151 int VideoListDataModel::rowCount(const QModelIndex &parent) const
       
   152 {
       
   153     if (parent.isValid())
       
   154     {
       
   155         return 0;
       
   156     }
       
   157     return mData.count();
       
   158 }
       
   159 
       
   160 // ---------------------------------------------------------------------------
       
   161 // itemData
       
   162 // ---------------------------------------------------------------------------
       
   163 //
       
   164 QMap<int, QVariant> VideoListDataModel::itemData(const QModelIndex &index) const
       
   165 {
       
   166     Q_UNUSED(index);
       
   167     QMap<int, QVariant> itemData;
       
   168         
       
   169     return itemData;
       
   170 }
       
   171   
       
   172 // ---------------------------------------------------------------------------
       
   173 // data
       
   174 // ---------------------------------------------------------------------------
       
   175 //
       
   176 QVariant VideoListDataModel::data(const QModelIndex & index, int role) const
       
   177 {
       
   178     QVariant returnValue = QVariant();
       
   179     if(!index.isValid())
       
   180     {
       
   181         return returnValue;
       
   182     }
       
   183     if(mReturnInvalid)
       
   184     {
       
   185         return returnValue;
       
   186     }
       
   187     int row = index.row();
       
   188     if(role == VideoCollectionCommon::KeyTitle)
       
   189     {       
       
   190         if(row >= 0 && row < mData.count())
       
   191         {
       
   192             returnValue = mData.at(row)->mName;
       
   193         }
       
   194     }
       
   195     else if(role == VideoCollectionCommon::KeySizeValue || 
       
   196             role == VideoCollectionCommon::KeyNumberOfItems)
       
   197     {
       
   198         if(row >= 0 && row < mData.count())
       
   199         {
       
   200             returnValue = mData.at(row)->mSize;
       
   201         }
       
   202     }
       
   203     else if(role == VideoCollectionCommon::KeyDateTime)
       
   204     {
       
   205         if(row >= 0 && row < mData.count())
       
   206         {
       
   207             returnValue = mData.at(row)->mDate;
       
   208         }
       
   209     }
       
   210     else if(role == VideoCollectionCommon::KeyStatus)
       
   211     {
       
   212         if(row >= 0 && row < mData.count())
       
   213         {
       
   214             returnValue = mData.at(row)->mStatus;
       
   215         }
       
   216     }
       
   217     else if(role == INVALID_ROLE_FOR_SORTING)
       
   218     {
       
   219         if(row >= 0 && row < mData.count())
       
   220         {
       
   221             returnValue = mData.at(row)->mName;
       
   222         }
       
   223     }    
       
   224     return returnValue;    
       
   225 }
       
   226  
       
   227 // ---------------------------------------------------------------------------
       
   228 // columnCount
       
   229 // ---------------------------------------------------------------------------
       
   230 //
       
   231 int VideoListDataModel::columnCount(const QModelIndex & parent) const
       
   232 {
       
   233     if (parent.isValid())
       
   234    {
       
   235        return 0;
       
   236    }
       
   237    else
       
   238    {
       
   239        return 1;
       
   240    }
       
   241 }
       
   242 
       
   243 // ---------------------------------------------------------------------------
       
   244 // index
       
   245 // ---------------------------------------------------------------------------
       
   246 //
       
   247 QModelIndex VideoListDataModel::index(int row, int column, const QModelIndex & parent) const
       
   248 {
       
   249     Q_UNUSED(parent);
       
   250     if(row >= 0 && row < mData.count())
       
   251     {
       
   252         return createIndex(row, column);
       
   253     }
       
   254     return QModelIndex();
       
   255 }
       
   256   
       
   257 // ---------------------------------------------------------------------------
       
   258 // parent
       
   259 // ---------------------------------------------------------------------------
       
   260 //
       
   261 QModelIndex VideoListDataModel::parent(const QModelIndex & index) const
       
   262 {
       
   263     Q_UNUSED(index);
       
   264     return QModelIndex();
       
   265 }
       
   266 
       
   267 void VideoListDataModel::removeAll()
       
   268 {
       
   269     beginRemoveRows(QModelIndex(), 0,0);
       
   270     QList<DummyData*>::iterator iter = mData.begin();
       
   271     while(iter != mData.end())
       
   272     {
       
   273         delete *iter;
       
   274         ++iter;
       
   275     }
       
   276     mData.clear();
       
   277     endRemoveRows();
       
   278 }
       
   279 
       
   280 
       
   281 // ---------------------------------------------------------------------------
       
   282 // emitAlbumChanged
       
   283 // ---------------------------------------------------------------------------
       
   284 //
       
   285 void VideoListDataModel::emitAlbumChanged()
       
   286 {
       
   287     emit albumChanged();
       
   288 }
       
   289 
       
   290 // ---------------------------------------------------------------------------
       
   291 // appendData
       
   292 // ---------------------------------------------------------------------------
       
   293 //
       
   294 void VideoListDataModel::appendData(TMPXItemId data)
       
   295 {
       
   296     DummyData* obj;
       
   297     for(int i = 0; i < mData.count(); ++i)
       
   298     {
       
   299         obj = mData.at(i);
       
   300         if(obj->mId == TMPXItemId::InvalidId() || obj->mId.iId1 == data.iId1)
       
   301         {
       
   302             obj->mId = data;
       
   303             QModelIndex change = index(i,0);
       
   304             emit dataChanged(change, change);
       
   305             return;
       
   306         }
       
   307     }
       
   308     beginInsertRows(QModelIndex(), mData.count(), mData.count());
       
   309     obj = new DummyData;
       
   310     obj->mId = data;
       
   311     mData.append(obj);
       
   312     endInsertRows();  
       
   313 }
       
   314 
       
   315 
       
   316 // ---------------------------------------------------------------------------
       
   317 // appendData
       
   318 // ---------------------------------------------------------------------------
       
   319 //
       
   320 void VideoListDataModel::appendData(QString data)
       
   321 {
       
   322     DummyData* obj;
       
   323     for(int i = 0; i < mData.count(); ++i)
       
   324     {
       
   325         obj = mData.at(i);
       
   326         if(!obj->mName.length())
       
   327         {
       
   328             obj->mName = data;
       
   329             QModelIndex change = index(i,0);
       
   330             emit dataChanged(change, change);
       
   331             return;
       
   332         }
       
   333     }
       
   334     beginInsertRows(QModelIndex(), mData.count(), mData.count());
       
   335     obj = new DummyData;
       
   336     obj->mId = TMPXItemId( mData.count(), 0);
       
   337     obj->mName = data;
       
   338     mData.append(obj);
       
   339     endInsertRows();
       
   340 }
       
   341  
       
   342 // ---------------------------------------------------------------------------
       
   343 // appendData
       
   344 // ---------------------------------------------------------------------------
       
   345 //
       
   346 void VideoListDataModel::appendData(uint data)
       
   347 {
       
   348     DummyData* obj;
       
   349     for(int i = 0; i < mData.count(); ++i)
       
   350     {
       
   351         obj = mData.at(i);
       
   352         if(obj->mSize == 666)
       
   353         {
       
   354             obj->mSize = data;
       
   355             QModelIndex change = index(i,0);
       
   356             emit dataChanged(change, change);
       
   357             return;
       
   358         }
       
   359     }
       
   360     beginInsertRows(QModelIndex(), mData.count(), mData.count());
       
   361     obj = new DummyData;
       
   362     obj->mId = TMPXItemId( mData.count(), 0);
       
   363     obj->mSize = data;
       
   364     mData.append(obj);
       
   365     endInsertRows();
       
   366 }
       
   367 
       
   368 // ---------------------------------------------------------------------------
       
   369 // appendData
       
   370 // ---------------------------------------------------------------------------
       
   371 //
       
   372 void VideoListDataModel::appendData(QDateTime data)
       
   373 {
       
   374     DummyData* obj;
       
   375     for(int i = 0; i < mData.count(); ++i)
       
   376     {
       
   377         obj = mData.at(i);
       
   378         if(!obj->mDate.isValid())
       
   379         {
       
   380             obj->mDate = data;
       
   381             QModelIndex change = index(i,0);
       
   382             emit dataChanged(change, change);
       
   383             return;
       
   384         }
       
   385     }
       
   386     beginInsertRows(QModelIndex(), mData.count(), mData.count());
       
   387     obj = new DummyData;
       
   388     obj->mId = TMPXItemId( mData.count(), 0);
       
   389     obj->mDate = data;
       
   390     mData.append(obj);
       
   391     endInsertRows();
       
   392 }
       
   393 
       
   394 // ---------------------------------------------------------------------------
       
   395 // appendStatus
       
   396 // ---------------------------------------------------------------------------
       
   397 //
       
   398 void VideoListDataModel::appendStatus(int status)
       
   399 {
       
   400     DummyData* obj;
       
   401     for(int i = 0; i < mData.count(); ++i)
       
   402     {
       
   403         obj = mData.at(i);
       
   404         if(obj->mStatus == -1)
       
   405         {
       
   406             obj->mStatus = status;
       
   407             QModelIndex change = index(i,0);
       
   408             emit dataChanged(change, change);
       
   409             return;
       
   410         }
       
   411     }
       
   412     beginInsertRows(QModelIndex(), mData.count(), mData.count());
       
   413     obj = new DummyData;
       
   414     obj->mId = TMPXItemId( mData.count(), 0);
       
   415     obj->mStatus = status;
       
   416     mData.append(obj);
       
   417     endInsertRows();
       
   418 }
       
   419 
       
   420 // ---------------------------------------------------------------------------
       
   421 // setAlbumInUse
       
   422 // ---------------------------------------------------------------------------
       
   423 //
       
   424 void VideoListDataModel::setAlbumInUse(TMPXItemId itemId)
       
   425 {
       
   426     mAlbumInUse = itemId;
       
   427 }
       
   428 
       
   429 // ---------------------------------------------------------------------------
       
   430 // albumInUse
       
   431 // ---------------------------------------------------------------------------
       
   432 //
       
   433 TMPXItemId VideoListDataModel::albumInUse()
       
   434 {
       
   435     return mAlbumInUse;
       
   436 }
       
   437 
       
   438 // ---------------------------------------------------------------------------
       
   439 // belongsToAlbum
       
   440 // ---------------------------------------------------------------------------
       
   441 //
       
   442 bool VideoListDataModel::belongsToAlbum(TMPXItemId itemId, TMPXItemId albumId )
       
   443 {
       
   444     Q_UNUSED(itemId);
       
   445     Q_UNUSED(albumId);
       
   446     return mBelongsToAlbum;
       
   447 }
       
   448 
       
   449 // ---------------------------------------------------------------------------
       
   450 // removeItemsFromAlbum
       
   451 // ---------------------------------------------------------------------------
       
   452 //
       
   453 int VideoListDataModel::removeItemsFromAlbum(TMPXItemId &albumId, const QList<TMPXItemId> &items)
       
   454 {
       
   455     Q_UNUSED(albumId);
       
   456     Q_UNUSED(items);
       
   457     return mRemoveFrAlbumReturn;
       
   458 }
       
   459 
       
   460 // ---------------------------------------------------------------------------
       
   461 // indexOfId
       
   462 // ---------------------------------------------------------------------------
       
   463 //
       
   464 QModelIndex VideoListDataModel::indexOfId(TMPXItemId id)
       
   465 {
       
   466     QModelIndex itemIndex;
       
   467     DummyData* obj;
       
   468     for(int i = 0; i < mData.count(); ++i)
       
   469     {
       
   470        obj = mData.at(i);
       
   471        if(obj->mId == id)
       
   472        {
       
   473            itemIndex = index(i,0,QModelIndex());
       
   474            break;
       
   475        }
       
   476     }
       
   477     return itemIndex;
       
   478 }
       
   479 
       
   480 // End of file
       
   481     
       
   482 
       
   483