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