videocollection/videocollectionwrapper/src/videolistdatamodel.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: VideoListDataModel class implementation
       
    15 * 
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 
       
    20 #include <hbglobal.h>
       
    21 #include <vcxmyvideosdefs.h>
       
    22 #include "videolistdatamodel.h"
       
    23 #include "videolistdatamodel_p.h"
       
    24 #include "videocollectionclient.h"
       
    25 #include "videocollectioncommon.h"
       
    26 #include "videocollectionutils.h"
       
    27 #include "videodeleteworker.h"
       
    28 #include "videocollectionwrapper.h"
       
    29 
       
    30 // ================= MEMBER FUNCTIONS =======================
       
    31 //
       
    32 
       
    33 // -----------------------------------------------------------------------------
       
    34 // VideoListDataModel()
       
    35 // -----------------------------------------------------------------------------
       
    36 //
       
    37 VideoListDataModel::VideoListDataModel(QObject *parent) :
       
    38 QAbstractItemModel(parent),
       
    39 d_ptr( new VideoListDataModelPrivate(this)), 
       
    40 mCollectionClient(0),
       
    41 mDeleteWorker(0),
       
    42 mInitialized(false)
       
    43 {
       
    44 }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // ~VideoListDataModel()
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 VideoListDataModel::~VideoListDataModel()
       
    51 {
       
    52     delete mDeleteWorker;
       
    53     delete d_ptr;
       
    54     delete mCollectionClient;
       
    55 }
       
    56 
       
    57 // -----------------------------------------------------------------------------
       
    58 // initialize()
       
    59 // -----------------------------------------------------------------------------
       
    60 //
       
    61 int VideoListDataModel::initialize()
       
    62 {
       
    63     if(mInitialized)
       
    64     {
       
    65         return 0;
       
    66     }
       
    67     if(!mCollectionClient)
       
    68     {
       
    69         mCollectionClient = new VideoCollectionClient();
       
    70         if(!mCollectionClient || mCollectionClient->initialize(d_ptr) < 0)
       
    71         {
       
    72             delete mCollectionClient;
       
    73             mCollectionClient = 0;
       
    74             return -1;
       
    75         }
       
    76     }
       
    77 
       
    78     if(!mDeleteWorker)
       
    79     {
       
    80         mDeleteWorker = new VideoDeleteWorker(*mCollectionClient);
       
    81         if(!mDeleteWorker)
       
    82         {
       
    83             delete mCollectionClient;
       
    84             mCollectionClient = 0;
       
    85             return -1;
       
    86         }
       
    87     }
       
    88     
       
    89     if( d_ptr->initialize() == -1)            
       
    90     {
       
    91         return -1;
       
    92     }
       
    93 
       
    94     if(connectSignals() == -1)
       
    95     {
       
    96         disconnectSignals();
       
    97         return -1;
       
    98     }
       
    99 
       
   100     mInitialized = true;
       
   101     return 0;
       
   102 }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // getCollectionClient()
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 VideoCollectionClient* VideoListDataModel::getCollectionClient()
       
   109 {
       
   110     return mCollectionClient;
       
   111 }
       
   112  
       
   113 // -----------------------------------------------------------------------------
       
   114 // connectSignals()
       
   115 // -----------------------------------------------------------------------------
       
   116 //
       
   117 int VideoListDataModel::connectSignals()
       
   118 {
       
   119     if(!connect(d_ptr, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
       
   120                            this, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&))))
       
   121     {
       
   122         return -1;
       
   123     }
       
   124     if(!connect(d_ptr, SIGNAL(videoDetailsReady(TMPXItemId)),
       
   125                        this, SIGNAL(fullVideoDetailsReady(TMPXItemId))))
       
   126     {
       
   127         return -1;
       
   128     }
       
   129     if(!connect(this, SIGNAL(modelChanged()), mDeleteWorker, SLOT(continueSlot())))
       
   130     {
       
   131         return -1;
       
   132     }
       
   133     if(!connect(this, SIGNAL(modelReady()), mDeleteWorker, SLOT(continueSlot())))
       
   134     {
       
   135         return -1;
       
   136     }
       
   137     if(!connect(mDeleteWorker, SIGNAL(deleteStartupFailed(QList<TMPXItemId>)), 
       
   138             this, SLOT(deleteStartingFailsSlot(QList<TMPXItemId>))))
       
   139     {
       
   140         return -1;
       
   141     }
       
   142    
       
   143     return 0;
       
   144 }
       
   145 
       
   146 // -----------------------------------------------------------------------------
       
   147 // disconnectSignals()
       
   148 // -----------------------------------------------------------------------------
       
   149 //
       
   150 void VideoListDataModel::disconnectSignals()
       
   151 {
       
   152     disconnect(d_ptr, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)),
       
   153                                this, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)));
       
   154     disconnect(d_ptr, SIGNAL(videoDetailsReady(TMPXItemId)),
       
   155                            this, SIGNAL(fullVideoDetailsReady(TMPXItemId)));
       
   156     disconnect(this, SIGNAL(modelChanged()), mDeleteWorker, SLOT(continueSlot()));
       
   157     disconnect(this, SIGNAL(modelReady()), mDeleteWorker, SLOT(continueSlot()));
       
   158     disconnect(mDeleteWorker, SIGNAL(deleteStartupFailed(QList<TMPXItemId>)), 
       
   159                 this, SLOT(deleteStartingFailsSlot(QList<TMPXItemId>)));
       
   160 }
       
   161 
       
   162 // -----------------------------------------------------------------------------
       
   163 // mediaIdAtIndex()
       
   164 // -----------------------------------------------------------------------------
       
   165 //
       
   166 TMPXItemId VideoListDataModel::mediaIdAtIndex(int index) const
       
   167 {
       
   168     TMPXItemId mpxId =TMPXItemId::InvalidId();
       
   169 
       
   170     if(index >= 0 && index < d_ptr->getVideoCount())
       
   171     {
       
   172         mpxId = d_ptr->getMediaIdFromIndex(index);
       
   173     }
       
   174 
       
   175     return mpxId;
       
   176 }
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // indexOfId()
       
   180 // -----------------------------------------------------------------------------
       
   181 //
       
   182 QModelIndex VideoListDataModel::indexOfId(TMPXItemId id)
       
   183 {
       
   184     int rowIndex = d_ptr->mMediaData.indexOfId(id);
       
   185     return index(rowIndex, 0, QModelIndex());     
       
   186 }
       
   187 
       
   188 // -----------------------------------------------------------------------------
       
   189 // mediaFilePathForId()
       
   190 // -----------------------------------------------------------------------------
       
   191 //
       
   192 QString VideoListDataModel::mediaFilePathForId(TMPXItemId mediaId) const
       
   193 {
       
   194     QString filePath;
       
   195     filePath = d_ptr->getFilePathForId(mediaId);
       
   196 
       
   197     return filePath;
       
   198 }
       
   199 
       
   200 // -----------------------------------------------------------------------------
       
   201 // rowCount()
       
   202 // -----------------------------------------------------------------------------
       
   203 //
       
   204 int VideoListDataModel::rowCount(const QModelIndex &parent ) const
       
   205 {
       
   206     // according to Qt documentation if parent is valid this should return 0 if
       
   207     // implementing a table based implementation like this.
       
   208     if (parent.isValid())
       
   209     {
       
   210         return 0;
       
   211     }
       
   212     
       
   213     return d_ptr->getVideoCount();
       
   214 }
       
   215 
       
   216 // -----------------------------------------------------------------------------
       
   217 // itemData()
       
   218 // -----------------------------------------------------------------------------
       
   219 //
       
   220 QMap<int, QVariant> VideoListDataModel::itemData(const QModelIndex &index) const
       
   221 {
       
   222     QMap<int, QVariant> itemData;
       
   223     
       
   224     if (index.isValid()) 
       
   225     {
       
   226         // returns only basic data of the item
       
   227         itemData.insert(Qt::DisplayRole, data(index, Qt::DisplayRole));
       
   228         itemData.insert(Qt::DecorationRole, data(index, Qt::DecorationRole)); 
       
   229         itemData.insert(Qt::BackgroundRole, data(index, Qt::BackgroundRole)); 
       
   230     }
       
   231     return itemData;
       
   232 
       
   233 }
       
   234 
       
   235 // -----------------------------------------------------------------------------
       
   236 // prepareDetailRow()
       
   237 // -----------------------------------------------------------------------------
       
   238 //
       
   239 QString VideoListDataModel::prepareDetailRow(int index) const
       
   240 {
       
   241     // TODO: download -status?
       
   242 
       
   243     QString retString;
       
   244     
       
   245     TMPXItemId itemId = mediaIdAtIndex(index);
       
   246     
       
   247     if(itemId.iId2 != KVcxMvcMediaTypeVideo) //category || album
       
   248     {
       
   249         retString = prepareVideoCountString(index);
       
   250     }
       
   251     else //video
       
   252     {
       
   253         const QString sizeStr = prepareSizeString(index);
       
   254         const QStringList list = prepareLengthStrings( index );
       
   255         QString duration;
       
   256         duration.append(list.at(0));
       
   257         duration.append(":");
       
   258         duration.append(list.at(1));
       
   259         duration.append(":");
       
   260         duration.append(list.at(2));
       
   261         retString = hbTrId("txt_videos_dblist_captured_val_1_l1").arg(duration).arg(sizeStr);
       
   262     }
       
   263     return retString; 
       
   264 }
       
   265 
       
   266 // -----------------------------------------------------------------------------
       
   267 // prepareVideoCountString()
       
   268 // -----------------------------------------------------------------------------
       
   269 //
       
   270 QString VideoListDataModel::prepareVideoCountString(int index) const
       
   271 {
       
   272     QString videoCountString("");
       
   273 
       
   274     quint32 items = d_ptr->getCategoryVideoCountFromIndex(index);
       
   275     if(items > 0)
       
   276     {
       
   277         videoCountString = hbTrId("txt_videos_dblist_val_ln_videos", items);
       
   278     }
       
   279     else
       
   280     {
       
   281         videoCountString = hbTrId("txt_videos_info_no_videos");
       
   282     }
       
   283 
       
   284     return videoCountString;
       
   285 }
       
   286 
       
   287 // -----------------------------------------------------------------------------
       
   288 // prepareSizeString()
       
   289 // -----------------------------------------------------------------------------
       
   290 //
       
   291 QString VideoListDataModel::prepareSizeString(int index) const
       
   292 {
       
   293     QString sizeStr("");
       
   294 
       
   295     quint32 size = d_ptr->getVideoSizeFromIndex(index);
       
   296     sizeStr = VideoCollectionUtils::instance().prepareSizeString(size);
       
   297 
       
   298     return sizeStr;
       
   299 }
       
   300 
       
   301 // -----------------------------------------------------------------------------
       
   302 // VideoListDataModel::prepareLengthStrings()
       
   303 // -----------------------------------------------------------------------------
       
   304 //
       
   305 QStringList VideoListDataModel::prepareLengthStrings(int index) const
       
   306 {
       
   307     quint32 total = d_ptr->getVideodurationFromIndex(index);
       
   308     return VideoCollectionUtils::instance().prepareLengthStrings(total);
       
   309 }
       
   310 
       
   311 // -----------------------------------------------------------------------------
       
   312 // data()
       
   313 // -----------------------------------------------------------------------------
       
   314 //
       
   315 QVariant VideoListDataModel::data(const QModelIndex & index, int role) const
       
   316 {
       
   317     QVariant returnValue = QVariant();
       
   318 
       
   319    
       
   320     if (index.isValid()) 
       
   321     {
       
   322         int rowIndex = index.row();
       
   323 
       
   324         if (role == Qt::DisplayRole)
       
   325         {
       
   326             QStringList itemTxt;
       
   327             itemTxt.append(d_ptr->getVideoNameFromIndex(rowIndex));
       
   328                 
       
   329             // Item's 2nd row: details
       
   330             itemTxt.append(prepareDetailRow(rowIndex));
       
   331             returnValue = itemTxt;
       
   332         }
       
   333         else if (role == Qt::DecorationRole)
       
   334         {
       
   335             const QIcon *thumbnail = d_ptr->getVideoThumbnailFromIndex(rowIndex); 
       
   336             QVariant tnVariant(QVariant::Icon, thumbnail);
       
   337             returnValue = tnVariant;
       
   338         }
       
   339         else if (role == VideoCollectionCommon::KeyDateTime)
       
   340         {
       
   341             QDateTime date = d_ptr->getVideoDateFromIndex(rowIndex);
       
   342             returnValue = date;
       
   343         }
       
   344         else if(role == VideoCollectionCommon::KeySizeValue)
       
   345         {
       
   346             quint32 size = d_ptr->getVideoSizeFromIndex(rowIndex);
       
   347             returnValue = size;
       
   348             
       
   349         }
       
   350         else if(role == VideoCollectionCommon::KeyMetaData)
       
   351         {
       
   352             returnValue = d_ptr->getMetaDataFromIndex(rowIndex);
       
   353         }
       
   354         else if(role == VideoCollectionCommon::KeyFilePath)
       
   355         {
       
   356             QString path = d_ptr->getFilePathFromIndex(rowIndex);
       
   357             if(!(path.isNull()) && !(path.isEmpty())) { 
       
   358                 returnValue = path;
       
   359             }
       
   360         }
       
   361     }
       
   362     
       
   363     return returnValue;
       
   364 }
       
   365 
       
   366 // -----------------------------------------------------------------------------
       
   367 // columnCount()
       
   368 // -----------------------------------------------------------------------------
       
   369 //
       
   370 int VideoListDataModel::columnCount(const QModelIndex & parent) const
       
   371 {
       
   372     // according to Qt documentation if parent is valid this should return 0 if
       
   373     // implementing a table based implementation like this.
       
   374     if (parent.isValid())
       
   375     {
       
   376         return 0;
       
   377     }
       
   378     else
       
   379     {
       
   380         return 1;
       
   381     }
       
   382 }
       
   383 
       
   384 // -----------------------------------------------------------------------------
       
   385 // index()
       
   386 // -----------------------------------------------------------------------------
       
   387 //
       
   388 QModelIndex VideoListDataModel::index(int row, int column, const QModelIndex & /*parent*/) const
       
   389 {
       
   390 
       
   391     if(row >= 0 && row < d_ptr->getVideoCount())
       
   392     {
       
   393         return createIndex(row, column);
       
   394     }
       
   395 
       
   396     return QModelIndex();
       
   397 }
       
   398 
       
   399 // -----------------------------------------------------------------------------
       
   400 // parent()
       
   401 // -----------------------------------------------------------------------------
       
   402 //
       
   403 QModelIndex VideoListDataModel::parent(const QModelIndex & /*index*/) const
       
   404 {
       
   405     return QModelIndex();
       
   406 }
       
   407 
       
   408 // -----------------------------------------------------------------------------
       
   409 // removeRows()
       
   410 // -----------------------------------------------------------------------------
       
   411 //
       
   412 bool VideoListDataModel::removeRows(const QModelIndexList &indexList)
       
   413 {
       
   414     if(!mCollectionClient || !mDeleteWorker)
       
   415     {
       
   416         return false;
       
   417     }
       
   418     // generate list of deletable item ids
       
   419     TMPXItemId id = TMPXItemId::InvalidId();
       
   420     QList<TMPXItemId> removedIds;
       
   421     QModelIndexList sortable(indexList);
       
   422     qSort(sortable);
       
   423     
       
   424     // since items are removed from actual container when being marked
       
   425     // causing total item count to be decreased,
       
   426     // we need to mark items starting from biggest index, otherwise we might 
       
   427     // end up to mark invalid items or we are referring into null item
       
   428     QModelIndexList::const_iterator iter = sortable.constEnd();
       
   429     while(iter != sortable.constBegin())
       
   430     {
       
   431         iter--;
       
   432         beginRemoveRows(QModelIndex(), (*iter).row(), (*iter).row());
       
   433         // marking might fail if either because
       
   434         // item does no exist or index is incorrect.
       
   435         // Both reasons mean that item is incorrect and should be removed
       
   436         // from the view. No need to restain them
       
   437         id = d_ptr->markVideoRemoved((*iter));
       
   438         endRemoveRows();
       
   439         if(id != TMPXItemId::InvalidId())
       
   440         {
       
   441             removedIds.append(id);
       
   442         }
       
   443     }
       
   444     emit modelChanged();
       
   445     if(removedIds.count() > 0 )
       
   446     {                         
       
   447         mDeleteWorker->requestDelete(removedIds);
       
   448         return true;
       
   449     }
       
   450     return false;
       
   451 }
       
   452 
       
   453 // -----------------------------------------------------------------------------
       
   454 // belongsToAlbum()
       
   455 // -----------------------------------------------------------------------------
       
   456 //
       
   457 bool VideoListDataModel::belongsToAlbum(const TMPXItemId &itemId,
       
   458       TMPXItemId albumId ) const
       
   459 {
       
   460     return d_ptr->belongsToAlbum(itemId, albumId);
       
   461 }
       
   462 
       
   463 // -----------------------------------------------------------------------------
       
   464 // setAlbumInUse()
       
   465 // -----------------------------------------------------------------------------
       
   466 //
       
   467 void VideoListDataModel::setAlbumInUse(TMPXItemId albumId)
       
   468 {
       
   469     d_ptr->setAlbumInUse(albumId);
       
   470 }
       
   471 
       
   472 // -----------------------------------------------------------------------------
       
   473 // albumInUse()
       
   474 // -----------------------------------------------------------------------------
       
   475 //
       
   476 TMPXItemId VideoListDataModel::albumInUse()
       
   477 {
       
   478     return d_ptr->mCurrentAlbum;
       
   479 }
       
   480 
       
   481 // -----------------------------------------------------------------------------
       
   482 // removeItemsFromAlbum()
       
   483 // -----------------------------------------------------------------------------
       
   484 //
       
   485 int VideoListDataModel::removeItemsFromAlbum(TMPXItemId &albumId, const QList<TMPXItemId> &items)
       
   486 {
       
   487     int removeCount = d_ptr->removeItemsFromAlbum(albumId, items);
       
   488     if(removeCount)
       
   489     {
       
   490         if(mCollectionClient->removeItemsFromAlbum(albumId, items) < 0)
       
   491         {
       
   492             return -1;
       
   493         }
       
   494         emit albumChanged();
       
   495     }
       
   496     return removeCount;
       
   497 }   
       
   498 
       
   499 // -----------------------------------------------------------------------------
       
   500 // deleteStartingFailsSlot()
       
   501 // -----------------------------------------------------------------------------
       
   502 //
       
   503 void VideoListDataModel::deleteStartingFailsSlot(QList<TMPXItemId> ids)
       
   504 {
       
   505     if(ids.count())
       
   506     {        
       
   507         d_ptr->restoreRemoved(&ids);
       
   508         QVariant data = ids.count();
       
   509         reportAsyncStatus(VideoCollectionCommon::statusMultipleDeleteFail, data);
       
   510     }    
       
   511     // signal for worker to proceed
       
   512     emit modelChanged();
       
   513 }
       
   514 
       
   515 // -----------------------------------------------------------------------------
       
   516 // reportAsyncStatus()
       
   517 // -----------------------------------------------------------------------------
       
   518 //
       
   519 void VideoListDataModel::reportAsyncStatus(int statusCode, QVariant &additional)
       
   520 {
       
   521     bool report = true;
       
   522     if(statusCode == VideoCollectionCommon::statusSingleDeleteFail ||
       
   523        statusCode ==  VideoCollectionCommon::statusMultipleDeleteFail ||
       
   524        statusCode ==  VideoCollectionCommon::statusDeleteSucceed)
       
   525     {
       
   526         report = false;
       
   527         if(!mDeleteWorker)
       
   528         {
       
   529             return;
       
   530         }
       
   531         mDeleteWorker->updateStatus(statusCode, additional);  
       
   532         statusCode = mDeleteWorker->getLastStatus(additional);
       
   533         if(!mDeleteWorker->isDeleting())
       
   534         {
       
   535             report = true;
       
   536             // delete procedure ends, reset statuses
       
   537             mDeleteWorker->clearStatus();
       
   538         }
       
   539     }
       
   540     if(report)
       
   541     {
       
   542         VideoCollectionWrapper::instance().sendAsyncStatus(statusCode, additional); 
       
   543     }
       
   544 }
       
   545 
       
   546 // End of file