videocollection/videocollectionwrapper/src/videodeleteworker.cpp
author hgs
Thu, 08 Jul 2010 13:05:19 +0300
changeset 44 518105d52e45
parent 36 8aed59de29f9
permissions -rw-r--r--
201027
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
*
hgs
parents:
diff changeset
    14
* Description: VideoDeleteWorker class implementation
hgs
parents:
diff changeset
    15
* 
hgs
parents:
diff changeset
    16
*/
36
hgs
parents: 34
diff changeset
    17
hgs
parents: 34
diff changeset
    18
// Version : %version: %
34
hgs
parents:
diff changeset
    19
36
hgs
parents: 34
diff changeset
    20
// INCLUDE FILES
34
hgs
parents:
diff changeset
    21
#include <qtimer.h>
36
hgs
parents: 34
diff changeset
    22
34
hgs
parents:
diff changeset
    23
#include "videocollectioncommon.h"
hgs
parents:
diff changeset
    24
#include "videocollectionclient.h"
hgs
parents:
diff changeset
    25
#include "videodeleteworker.h"
36
hgs
parents: 34
diff changeset
    26
#include "videocollectiontrace.h"
34
hgs
parents:
diff changeset
    27
hgs
parents:
diff changeset
    28
// ================= MEMBER FUNCTIONS =======================
hgs
parents:
diff changeset
    29
//
hgs
parents:
diff changeset
    30
hgs
parents:
diff changeset
    31
/**
hgs
parents:
diff changeset
    32
 * private global inline hash function for TMPXItemId keys in QSet
hgs
parents:
diff changeset
    33
 */
hgs
parents:
diff changeset
    34
inline uint qHash(TMPXItemId key) 
hgs
parents:
diff changeset
    35
{ 
hgs
parents:
diff changeset
    36
    QPair<uint, uint> keyPair(key.iId1, key.iId2); 
hgs
parents:
diff changeset
    37
hgs
parents:
diff changeset
    38
    return qHash(keyPair);
hgs
parents:
diff changeset
    39
}
hgs
parents:
diff changeset
    40
hgs
parents:
diff changeset
    41
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    42
// VideoDeleteWorker()
hgs
parents:
diff changeset
    43
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    44
//
hgs
parents:
diff changeset
    45
VideoDeleteWorker::VideoDeleteWorker(VideoCollectionClient &collection, QObject *parent) :
hgs
parents:
diff changeset
    46
QObject(parent),
hgs
parents:
diff changeset
    47
mCollectionClient(collection),
hgs
parents:
diff changeset
    48
mRequestWaitTimer(0),
hgs
parents:
diff changeset
    49
mLastStatus(0),
hgs
parents:
diff changeset
    50
mLastStatusData(QVariant())
hgs
parents:
diff changeset
    51
{
36
hgs
parents: 34
diff changeset
    52
	FUNC_LOG;
34
hgs
parents:
diff changeset
    53
    
hgs
parents:
diff changeset
    54
}
hgs
parents:
diff changeset
    55
hgs
parents:
diff changeset
    56
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    57
// ~VideoDeleteWorker()
hgs
parents:
diff changeset
    58
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    59
//
hgs
parents:
diff changeset
    60
VideoDeleteWorker::~VideoDeleteWorker()
hgs
parents:
diff changeset
    61
{
36
hgs
parents: 34
diff changeset
    62
	FUNC_LOG;
34
hgs
parents:
diff changeset
    63
    if(mRequestWaitTimer && mRequestWaitTimer->isActive())
hgs
parents:
diff changeset
    64
    {
hgs
parents:
diff changeset
    65
        mRequestWaitTimer->stop();
hgs
parents:
diff changeset
    66
    }
hgs
parents:
diff changeset
    67
    // if we're still doing some background deletion, 
hgs
parents:
diff changeset
    68
    // dump all deletes to collection and stop timer
hgs
parents:
diff changeset
    69
    flushAll();
hgs
parents:
diff changeset
    70
}
hgs
parents:
diff changeset
    71
hgs
parents:
diff changeset
    72
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    73
// requestDelete()
hgs
parents:
diff changeset
    74
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    75
//
hgs
parents:
diff changeset
    76
void VideoDeleteWorker::requestDelete(const QList<TMPXItemId> &indexList)
hgs
parents:
diff changeset
    77
{
36
hgs
parents: 34
diff changeset
    78
	FUNC_LOG;
34
hgs
parents:
diff changeset
    79
    if(!mRequestWaitTimer)
hgs
parents:
diff changeset
    80
    {
hgs
parents:
diff changeset
    81
        mRequestWaitTimer = new QTimer();
hgs
parents:
diff changeset
    82
        connect(mRequestWaitTimer, SIGNAL(timeout()), this, SLOT(execDeleteBlockSlot()));
hgs
parents:
diff changeset
    83
    }
hgs
parents:
diff changeset
    84
    
hgs
parents:
diff changeset
    85
    mRemoveBuffer.unite(QSet<TMPXItemId>::fromList(indexList));
hgs
parents:
diff changeset
    86
hgs
parents:
diff changeset
    87
    if(!mRequestWaitTimer->isActive())
hgs
parents:
diff changeset
    88
    {
hgs
parents:
diff changeset
    89
        // first startup throught zero timer, after that
hgs
parents:
diff changeset
    90
        // deletion is chained. Next delete block starts from
hgs
parents:
diff changeset
    91
        // modelChangedSlot
hgs
parents:
diff changeset
    92
        mRequestWaitTimer->setSingleShot(true);
hgs
parents:
diff changeset
    93
        mRequestWaitTimer->start(0);
hgs
parents:
diff changeset
    94
    }
hgs
parents:
diff changeset
    95
}
hgs
parents:
diff changeset
    96
hgs
parents:
diff changeset
    97
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
    98
// removeFromRequest()
hgs
parents:
diff changeset
    99
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   100
//
hgs
parents:
diff changeset
   101
int VideoDeleteWorker::removeFromRequest(TMPXItemId &itemId)
hgs
parents:
diff changeset
   102
{
36
hgs
parents: 34
diff changeset
   103
	FUNC_LOG;
34
hgs
parents:
diff changeset
   104
    mRemoveBuffer.remove(itemId);
hgs
parents:
diff changeset
   105
    return mRemoveBuffer.count();
hgs
parents:
diff changeset
   106
}
hgs
parents:
diff changeset
   107
   
hgs
parents:
diff changeset
   108
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   109
// isDeleting()
hgs
parents:
diff changeset
   110
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   111
//
hgs
parents:
diff changeset
   112
bool VideoDeleteWorker::isDeleting()
hgs
parents:
diff changeset
   113
{
36
hgs
parents: 34
diff changeset
   114
	FUNC_LOG;
34
hgs
parents:
diff changeset
   115
    return mRemoveBuffer.count() ? true : false;
hgs
parents:
diff changeset
   116
}
hgs
parents:
diff changeset
   117
hgs
parents:
diff changeset
   118
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   119
// updateStatus()
hgs
parents:
diff changeset
   120
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   121
//
hgs
parents:
diff changeset
   122
void VideoDeleteWorker::updateStatus(int status, QVariant data)
hgs
parents:
diff changeset
   123
{
36
hgs
parents: 34
diff changeset
   124
	FUNC_LOG;
hgs
parents: 34
diff changeset
   125
	INFO_1("VideoDeleteWorker::updateStatus() status: %d", status);
hgs
parents: 34
diff changeset
   126
	
34
hgs
parents:
diff changeset
   127
    // do not update invalid status
hgs
parents:
diff changeset
   128
    if(status != VideoCollectionCommon::statusDeleteSucceed && 
hgs
parents:
diff changeset
   129
       status != VideoCollectionCommon::statusSingleDeleteFail &&
hgs
parents:
diff changeset
   130
       status != VideoCollectionCommon::statusMultipleDeleteFail)
hgs
parents:
diff changeset
   131
    {
hgs
parents:
diff changeset
   132
        return;
hgs
parents:
diff changeset
   133
    }
hgs
parents:
diff changeset
   134
    
hgs
parents:
diff changeset
   135
    if(!mLastStatus || mLastStatus == VideoCollectionCommon::statusDeleteSucceed)
hgs
parents:
diff changeset
   136
    {
hgs
parents:
diff changeset
   137
        mLastStatus = status;
hgs
parents:
diff changeset
   138
        mLastStatusData = data;
hgs
parents:
diff changeset
   139
        return;    
hgs
parents:
diff changeset
   140
    }
hgs
parents:
diff changeset
   141
    if(status == VideoCollectionCommon::statusDeleteSucceed)
hgs
parents:
diff changeset
   142
    {
hgs
parents:
diff changeset
   143
        return;
hgs
parents:
diff changeset
   144
    }
hgs
parents:
diff changeset
   145
    int count = 0;
hgs
parents:
diff changeset
   146
    if(mLastStatus == VideoCollectionCommon::statusSingleDeleteFail)
hgs
parents:
diff changeset
   147
    {
hgs
parents:
diff changeset
   148
        // old status was single fail
hgs
parents:
diff changeset
   149
        mLastStatus = VideoCollectionCommon::statusMultipleDeleteFail;
hgs
parents:
diff changeset
   150
        count = 2;
hgs
parents:
diff changeset
   151
        if(status == VideoCollectionCommon::statusMultipleDeleteFail)
hgs
parents:
diff changeset
   152
        {
hgs
parents:
diff changeset
   153
            count = data.toInt() + 1;
hgs
parents:
diff changeset
   154
        }
hgs
parents:
diff changeset
   155
        // count of failed is now 2
hgs
parents:
diff changeset
   156
        mLastStatusData = count;
hgs
parents:
diff changeset
   157
        return;
hgs
parents:
diff changeset
   158
    }
hgs
parents:
diff changeset
   159
    // all other cases mean multi
hgs
parents:
diff changeset
   160
    count = data.toInt();
hgs
parents:
diff changeset
   161
    mLastStatus = VideoCollectionCommon::statusMultipleDeleteFail;
hgs
parents:
diff changeset
   162
    count ? mLastStatusData = mLastStatusData.toInt() + count : 
hgs
parents:
diff changeset
   163
                        mLastStatusData = mLastStatusData.toInt() + 1;
hgs
parents:
diff changeset
   164
}
hgs
parents:
diff changeset
   165
hgs
parents:
diff changeset
   166
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   167
// getStatus()
hgs
parents:
diff changeset
   168
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   169
//
hgs
parents:
diff changeset
   170
int VideoDeleteWorker::getLastStatus(QVariant &data)
hgs
parents:
diff changeset
   171
{
36
hgs
parents: 34
diff changeset
   172
	FUNC_LOG;
34
hgs
parents:
diff changeset
   173
    data = mLastStatusData;
hgs
parents:
diff changeset
   174
    return mLastStatus;
hgs
parents:
diff changeset
   175
}
hgs
parents:
diff changeset
   176
hgs
parents:
diff changeset
   177
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   178
// clearStatus()
hgs
parents:
diff changeset
   179
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   180
//
hgs
parents:
diff changeset
   181
void VideoDeleteWorker::clearStatus()
hgs
parents:
diff changeset
   182
{
36
hgs
parents: 34
diff changeset
   183
	FUNC_LOG;
34
hgs
parents:
diff changeset
   184
    mLastStatus = 0;
hgs
parents:
diff changeset
   185
    mLastStatusData = QVariant();
hgs
parents:
diff changeset
   186
}
hgs
parents:
diff changeset
   187
hgs
parents:
diff changeset
   188
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   189
// continueSlot()
hgs
parents:
diff changeset
   190
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   191
//
hgs
parents:
diff changeset
   192
void VideoDeleteWorker::continueSlot()
hgs
parents:
diff changeset
   193
{
36
hgs
parents: 34
diff changeset
   194
    FUNC_LOG;
hgs
parents: 34
diff changeset
   195
    if(!mRequestWaitTimer || !mRemoveBuffer.count())
hgs
parents: 34
diff changeset
   196
    {
hgs
parents: 34
diff changeset
   197
        return;
hgs
parents: 34
diff changeset
   198
    }
hgs
parents: 34
diff changeset
   199
    if(!mRequestWaitTimer->isActive())
hgs
parents: 34
diff changeset
   200
    {
hgs
parents: 34
diff changeset
   201
        mRequestWaitTimer->setSingleShot(true);
hgs
parents: 34
diff changeset
   202
        mRequestWaitTimer->start(0);
hgs
parents: 34
diff changeset
   203
    }
34
hgs
parents:
diff changeset
   204
}
hgs
parents:
diff changeset
   205
hgs
parents:
diff changeset
   206
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   207
// doBackgroundDeleteSlot()
hgs
parents:
diff changeset
   208
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   209
//
hgs
parents:
diff changeset
   210
void VideoDeleteWorker::execDeleteBlockSlot()
hgs
parents:
diff changeset
   211
{
36
hgs
parents: 34
diff changeset
   212
	FUNC_LOG;
34
hgs
parents:
diff changeset
   213
    if(!mRemoveBuffer.count())
hgs
parents:
diff changeset
   214
    {
hgs
parents:
diff changeset
   215
        return;
hgs
parents:
diff changeset
   216
    }
hgs
parents:
diff changeset
   217
    QList<TMPXItemId> deleteBlock;
hgs
parents:
diff changeset
   218
    
hgs
parents:
diff changeset
   219
    // create block of max 3 items
hgs
parents:
diff changeset
   220
    int counter = 0;
hgs
parents:
diff changeset
   221
    QSet<TMPXItemId>::iterator iter = mRemoveBuffer.begin();
hgs
parents:
diff changeset
   222
    while(iter != mRemoveBuffer.end() && counter < 3)
hgs
parents:
diff changeset
   223
    {
hgs
parents:
diff changeset
   224
        deleteBlock.append((*iter));
hgs
parents:
diff changeset
   225
        iter = mRemoveBuffer.erase(iter);
hgs
parents:
diff changeset
   226
        counter++;
hgs
parents:
diff changeset
   227
    }
hgs
parents:
diff changeset
   228
    
hgs
parents:
diff changeset
   229
    // need to handle errors somehow
hgs
parents:
diff changeset
   230
    if(mCollectionClient.deleteVideos(&deleteBlock) != 0)
hgs
parents:
diff changeset
   231
    {
hgs
parents:
diff changeset
   232
        // signal block delete startup failed
hgs
parents:
diff changeset
   233
        emit deleteStartupFailed(deleteBlock);
hgs
parents:
diff changeset
   234
    } 
hgs
parents:
diff changeset
   235
}
hgs
parents:
diff changeset
   236
hgs
parents:
diff changeset
   237
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   238
// flushAll()
hgs
parents:
diff changeset
   239
// -----------------------------------------------------------------------------
hgs
parents:
diff changeset
   240
//
hgs
parents:
diff changeset
   241
void VideoDeleteWorker::flushAll()
hgs
parents:
diff changeset
   242
{
36
hgs
parents: 34
diff changeset
   243
	FUNC_LOG;
34
hgs
parents:
diff changeset
   244
    if(!mRemoveBuffer.count())
hgs
parents:
diff changeset
   245
    {
hgs
parents:
diff changeset
   246
        return;
hgs
parents:
diff changeset
   247
    }
hgs
parents:
diff changeset
   248
    QList<TMPXItemId> ids = mRemoveBuffer.toList();
hgs
parents:
diff changeset
   249
    mCollectionClient.deleteVideos(&ids);
hgs
parents:
diff changeset
   250
    mRemoveBuffer.clear();
hgs
parents:
diff changeset
   251
}
hgs
parents:
diff changeset
   252
hgs
parents:
diff changeset
   253
// End of file