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