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