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