videocollection/videocollectionwrapper/tsrc/testvideomodel_p/stub/src/videodatacontainer.cpp
changeset 15 cf5481c2bc0b
child 17 69946d1824c4
equal deleted inserted replaced
2:dec420019252 15:cf5481c2bc0b
       
     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 VideoListData class declaration*
       
    15 */
       
    16 
       
    17 #include <mpxmediageneraldefs.h>
       
    18 #include <mpxmedia.h>
       
    19 #include "videodatacontainer.h"
       
    20 #include "videocollectionutils.h"
       
    21 
       
    22 /**
       
    23  * global qHash function required fo creating hash values for TMPXItemId -keys
       
    24  */
       
    25 inline uint qHash(TMPXItemId key) 
       
    26 { 
       
    27     QPair<uint, uint> keyPair(key.iId1, key.iId2); 
       
    28 
       
    29     return qHash(keyPair);
       
    30 }
       
    31 
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // VideoDataContainer
       
    35 // -----------------------------------------------------------------------------
       
    36 //
       
    37 VideoDataContainer::VideoDataContainer()
       
    38 {
       
    39     // NOP
       
    40 }
       
    41     
       
    42 // -----------------------------------------------------------------------------
       
    43 // VideoDataContainer
       
    44 // -----------------------------------------------------------------------------
       
    45 //
       
    46 VideoDataContainer::~VideoDataContainer()
       
    47 {
       
    48     clear();
       
    49     clearRemoved();
       
    50 }
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // clear
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 void VideoDataContainer::clear()
       
    57 {
       
    58     QMultiHash<TMPXItemId, QPair<int, CMPXMedia*> >::iterator i = mMediaData.begin();
       
    59     while(i != mMediaData.end())
       
    60     {
       
    61         delete (*i).second;
       
    62         ++i;
       
    63     }
       
    64     mMediaData.clear();
       
    65     mMediaIds.clear();
       
    66 }
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // remove
       
    70 // -----------------------------------------------------------------------------
       
    71 //
       
    72 void VideoDataContainer::remove(const TMPXItemId &id)
       
    73 {   
       
    74     QMultiHash<TMPXItemId, QPair<int, CMPXMedia*> >::iterator removeIter = mMediaData.constFind(id);
       
    75     if(removeIter == mMediaData.constEnd())
       
    76     {
       
    77         return;
       
    78     }
       
    79       
       
    80     mMediaIds.removeAt(removeIter->first);    
       
    81     
       
    82     // sync item indexes whose ids exist in id- list after
       
    83     // recently removoved. 
       
    84     decHashIndexesAfter(removeIter->first);
       
    85 
       
    86     delete removeIter->second;
       
    87     mMediaData.erase(removeIter);
       
    88 }   
       
    89 
       
    90 void VideoDataContainer::decHashIndexesAfter(int fromIndex)
       
    91 {
       
    92     int count = mMediaIds.count();
       
    93     QMultiHash<TMPXItemId, QPair<int, CMPXMedia*> >::iterator hashIter;   
       
    94     for(int i = fromIndex; i < count; ++i)
       
    95     {
       
    96         hashIter = mMediaData.find(mMediaIds[i]);
       
    97         if(hashIter != mMediaData.end())
       
    98         {
       
    99             hashIter->first--;
       
   100         }
       
   101     }   
       
   102 }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // append
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 void VideoDataContainer::append(CMPXMedia *media)
       
   109 {
       
   110     TMPXItemId mediaId = TMPXItemId::InvalidId();
       
   111     VideoCollectionUtils::instance().mediaValue<TMPXItemId>(media, KMPXMediaGeneralId, mediaId );
       
   112 
       
   113     if(mediaId == TMPXItemId::InvalidId())
       
   114     {
       
   115         // could not get id or id does not match ==> NOP
       
   116         return;       
       
   117     }
       
   118     // do not append duplicates
       
   119     if(mMediaIds.contains(mediaId))
       
   120     {
       
   121         return;
       
   122     }
       
   123     mMediaIds.append(mediaId);
       
   124     mMediaData.insert(mediaId, qMakePair( mMediaIds.count() - 1, media));     
       
   125 }
       
   126 
       
   127 
       
   128 // -----------------------------------------------------------------------------
       
   129 // fromIndex
       
   130 // -----------------------------------------------------------------------------
       
   131 //
       
   132 CMPXMedia* VideoDataContainer::fromIndex(int index) const
       
   133 {   
       
   134     if(index >= 0 && index < mMediaIds.count() && mMediaData.contains(mMediaIds[index]))
       
   135     {   
       
   136         return (mMediaData.find(mMediaIds[index]))->second;
       
   137     }
       
   138     return 0;  
       
   139 }
       
   140    
       
   141  
       
   142 // -----------------------------------------------------------------------------
       
   143 // indexOfId
       
   144 // -----------------------------------------------------------------------------
       
   145 //
       
   146 int VideoDataContainer::indexOfId(const TMPXItemId &id) const
       
   147 {
       
   148     QMultiHash<TMPXItemId, QPair<int, CMPXMedia*> >::const_iterator iter = mMediaData.find(id);
       
   149     if( iter != mMediaData.constEnd())
       
   150     {
       
   151         return iter->first;
       
   152     }
       
   153     
       
   154     return -1;
       
   155 }
       
   156 
       
   157 // -----------------------------------------------------------------------------
       
   158 // idFromIndex
       
   159 // -----------------------------------------------------------------------------
       
   160 //
       
   161 TMPXItemId VideoDataContainer::idFromIndex(int index) const
       
   162 {
       
   163     if(index >= 0 && index < mMediaIds.count())
       
   164     {
       
   165         return mMediaIds[index];
       
   166     }
       
   167     return TMPXItemId::InvalidId();    
       
   168 }
       
   169 
       
   170 // -----------------------------------------------------------------------------
       
   171 // count
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 int VideoDataContainer::count() const
       
   175 {
       
   176     return mMediaData.count();
       
   177 }
       
   178 
       
   179 // -----------------------------------------------------------------------------
       
   180 // markItemsRemoved
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 TMPXItemId VideoDataContainer::markItemRemoved(const int &itemIndex)
       
   184 {
       
   185     // for all provided indexes:
       
   186     // - get item address from mMediaData
       
   187     // - get item index from mMediaData
       
   188     // - remove item from mMediaData, do not deallocate object
       
   189     // - remove item's id from mMediaIds -list 
       
   190     // - append item into mRemovedMedia
       
   191     // - append item's id into returned id -list
       
   192     TMPXItemId id = TMPXItemId::InvalidId();
       
   193     CMPXMedia *media = 0;
       
   194     id = idFromIndex(itemIndex);
       
   195     media = fromIndex(itemIndex);    
       
   196     if(id == TMPXItemId::InvalidId() || !media)
       
   197     {
       
   198         return id;
       
   199     }
       
   200     if(!mRemovedMedia.contains(id))
       
   201     {
       
   202         mRemovedMedia[id] = media;  
       
   203         
       
   204     }
       
   205     mMediaData.remove(id);
       
   206     mMediaIds.removeAt(itemIndex);
       
   207     // sync item indexes after this
       
   208     decHashIndexesAfter(itemIndex);
       
   209     return id;
       
   210 }
       
   211 
       
   212 // -----------------------------------------------------------------------------
       
   213 // clearRemoved
       
   214 // -----------------------------------------------------------------------------
       
   215 //
       
   216 int VideoDataContainer::clearRemoved(QList<TMPXItemId> *itemIds)
       
   217 {
       
   218     int count = 0;
       
   219     QList<TMPXItemId> ids;
       
   220 
       
   221     QList<TMPXItemId>::const_iterator iterEnd;
       
   222     if(!itemIds)
       
   223     {
       
   224         ids = mRemovedMedia.keys();
       
   225     }
       
   226     else
       
   227     {
       
   228         ids = *itemIds;
       
   229     }
       
   230     QList<TMPXItemId>::const_iterator idIter = ids.constBegin();
       
   231     QHash<TMPXItemId, CMPXMedia*>::iterator iter;
       
   232     while(idIter != ids.constEnd())
       
   233     {
       
   234         iter = mRemovedMedia.find((*idIter));
       
   235         if(iter != mRemovedMedia.end())
       
   236         {
       
   237             delete (*iter);
       
   238             mRemovedMedia.remove((*idIter));
       
   239             count++;
       
   240         }        
       
   241         ++idIter;
       
   242     }
       
   243     return count;
       
   244 }
       
   245 
       
   246 // -----------------------------------------------------------------------------
       
   247 // restoreRemovedItems
       
   248 // -----------------------------------------------------------------------------
       
   249 //
       
   250 int VideoDataContainer::restoreRemovedItems(QList<TMPXItemId> *itemIds)
       
   251 {  
       
   252     
       
   253     int count = 0;
       
   254     QList<TMPXItemId> ids;
       
   255 
       
   256     QList<TMPXItemId>::const_iterator iterEnd;
       
   257     if(!itemIds)
       
   258     {
       
   259         ids = mRemovedMedia.keys();
       
   260     }
       
   261     else
       
   262     {
       
   263         ids = *itemIds;
       
   264     }
       
   265     
       
   266     QList<TMPXItemId>::const_iterator idIter = ids.constBegin();
       
   267     QHash<TMPXItemId, CMPXMedia*>::iterator iter;
       
   268     while(idIter != ids.constEnd())
       
   269     {
       
   270         iter = mRemovedMedia.find((*idIter));        
       
   271         if(iter != mRemovedMedia.constEnd() && !mMediaData.contains(iter.key()))
       
   272         {
       
   273             mMediaIds.append(iter.key());
       
   274             mMediaData.insert(iter.key(), qMakePair(mMediaIds.count() - 1, iter.value()));              
       
   275             mRemovedMedia.remove((*idIter));
       
   276             count++;
       
   277         }
       
   278         ++idIter;
       
   279     }    
       
   280     return count;
       
   281 }
       
   282 
       
   283 // -----------------------------------------------------------------------------
       
   284 // getRemovedMedia
       
   285 // -----------------------------------------------------------------------------
       
   286 //
       
   287 CMPXMedia* VideoDataContainer::getRemovedMedia(TMPXItemId itemId)
       
   288 {
       
   289     QHash<TMPXItemId, CMPXMedia*>::const_iterator itemIter = 
       
   290                                             mRemovedMedia.constFind(itemId);
       
   291     if(itemIter != mRemovedMedia.constEnd())
       
   292     {
       
   293         return itemIter.value();
       
   294     }
       
   295    return 0;
       
   296 }
       
   297 
       
   298 // end of file