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