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