mpxplugins/serviceplugins/collectionplugins/mpxsqlitedbhgplugin/src/mpxdbautoplaylist.cpp
branchRCL_3
changeset 26 3de6c4cf6b67
equal deleted inserted replaced
25:14979e23cb5e 26:3de6c4cf6b67
       
     1 /*
       
     2 * Copyright (c) 2007 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:  Responsible for the auto playlist functionality.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <mpxlog.h>
       
    21 #include <mpxcollectiondbhgres.rsg>
       
    22 #include <sqldb.h>
       
    23 
       
    24 #include "mpxresource.h"
       
    25 #include "mpxdbcommonutil.h"
       
    26 #include "mpxdbmanager.h"
       
    27 #include "mpxdbutil.h"
       
    28 #include "mpxcollectiondbdef.h"
       
    29 #include "mpxdbautoplaylist.h"
       
    30 #include "mpxdbpluginqueries.h"
       
    31 
       
    32 const TInt KMPXTableDefaultIndex = 0;
       
    33 const TInt32 KMPXRecentlyPlayedPlaylistId = 0x20000000;
       
    34 const TInt32 KMPXMostPlayedPlaylistId = 0x20000001;
       
    35 const TInt32 KMPXRecentlyAddedPlaylistId = 0x20000002;
       
    36 
       
    37 // ============================ MEMBER FUNCTIONS ==============================
       
    38 
       
    39 // ----------------------------------------------------------------------------
       
    40 // Two-phased constructor.
       
    41 // ----------------------------------------------------------------------------
       
    42 //
       
    43 CMPXDbAutoPlaylist* CMPXDbAutoPlaylist::NewL(
       
    44     CMPXDbManager& aDbManager,
       
    45     RFs& aFs,
       
    46     CMPXResource& aResource)
       
    47     {
       
    48     MPX_FUNC("CMPXDbAutoPlaylist::NewL");
       
    49 
       
    50     CMPXDbAutoPlaylist* self = CMPXDbAutoPlaylist::NewLC(aDbManager, aFs, aResource);
       
    51     CleanupStack::Pop(self);
       
    52     return self;
       
    53     }
       
    54 
       
    55 // ----------------------------------------------------------------------------
       
    56 // Two-phased constructor.
       
    57 // ----------------------------------------------------------------------------
       
    58 //
       
    59 CMPXDbAutoPlaylist* CMPXDbAutoPlaylist::NewLC(
       
    60     CMPXDbManager& aDbManager,
       
    61     RFs& aFs,
       
    62     CMPXResource& aResource)
       
    63     {
       
    64     MPX_FUNC("CMPXDbAutoPlaylist::NewLC");
       
    65 
       
    66     CMPXDbAutoPlaylist* self = new (ELeave) CMPXDbAutoPlaylist(aDbManager);
       
    67     CleanupStack::PushL(self);
       
    68     self->ConstructL(aFs, aResource);
       
    69     return self;
       
    70     }
       
    71 
       
    72 // ----------------------------------------------------------------------------
       
    73 // Destructor
       
    74 // ----------------------------------------------------------------------------
       
    75 //
       
    76 CMPXDbAutoPlaylist::~CMPXDbAutoPlaylist()
       
    77     {
       
    78     MPX_FUNC("CMPXDbAutoPlaylist::~CMPXDbAutoPlaylist");
       
    79 
       
    80     delete iRecentlyPlayedPlaylist;
       
    81     delete iMostPlayedPlaylist;
       
    82     delete iRecentlyAddedPlaylist;
       
    83     
       
    84     delete iTitleRecentlyPlayed;
       
    85     delete iTitleMostPlayed;
       
    86     delete iTitleRecentlyAdded;
       
    87 
       
    88     }
       
    89 
       
    90 // ----------------------------------------------------------------------------
       
    91 // Constructor
       
    92 // ----------------------------------------------------------------------------
       
    93 //
       
    94 CMPXDbAutoPlaylist::CMPXDbAutoPlaylist(CMPXDbManager& aDbManager)
       
    95     : iDbManager(aDbManager)
       
    96     {
       
    97     MPX_FUNC("CMPXDbAutoPlaylist::CMPXDbAutoPlaylist");
       
    98     }
       
    99 
       
   100 // ----------------------------------------------------------------------------
       
   101 // Second phase constructor.
       
   102 // ----------------------------------------------------------------------------
       
   103 //
       
   104 void CMPXDbAutoPlaylist::ConstructL(
       
   105     RFs& /*aFs*/,
       
   106     CMPXResource& aResource)
       
   107     {
       
   108     MPX_FUNC("CMPXDbAutoPlaylist::ConstructL");
       
   109 
       
   110     iRecentlyPlayedPlaylist = aResource.ReadHBufCL(R_MC_RECENTLY_PLAYED_PLAYLIST);
       
   111     iMostPlayedPlaylist = aResource.ReadHBufCL(R_MC_MOST_PLAYED_PLAYLIST);
       
   112     iRecentlyAddedPlaylist = aResource.ReadHBufCL(R_MC_RECENTLY_ADDED_PLAYLIST);
       
   113 
       
   114     iTitleRecentlyPlayed = aResource.ReadHBufCL(R_MC_TITLE_RECENTLY_PLAYED );
       
   115     iTitleMostPlayed = aResource.ReadHBufCL(R_MC_TITLE_MOST_PLAYED );
       
   116     iTitleRecentlyAdded = aResource.ReadHBufCL(R_MC_TITLE_RECENTLY_DOWNLOADED );
       
   117     
       
   118     iRecentlyPlayedPlaylistId = KMPXRecentlyPlayedPlaylistId;
       
   119     iMostPlayedPlaylistId = KMPXMostPlayedPlaylistId;
       
   120     iRecentlyAddedPlaylistId = KMPXRecentlyAddedPlaylistId;
       
   121     }
       
   122 
       
   123 // ----------------------------------------------------------------------------
       
   124 // CMPXDbAutoPlaylist::AutoPlaylistIdL
       
   125 // ----------------------------------------------------------------------------
       
   126 //
       
   127 TUint32 CMPXDbAutoPlaylist::AutoPlaylistIdL(
       
   128     TMPXAutoPlaylistType aPlaylistType)
       
   129     {
       
   130     MPX_FUNC("CMPXDbAutoPlaylist::AutoPlaylistIdL");
       
   131 
       
   132     TUint32 id(0);
       
   133     switch (aPlaylistType)
       
   134         {
       
   135         case EMPXRecentlyPlayedPlaylist:
       
   136             {
       
   137             id = iRecentlyPlayedPlaylistId;
       
   138             }
       
   139             break;
       
   140 
       
   141         case EMPXMostPlayedPlaylist:
       
   142             {
       
   143             id = iMostPlayedPlaylistId;
       
   144             }
       
   145             break;
       
   146 
       
   147         case EMPXRecentlyAddedPlaylist:
       
   148             {
       
   149             id = iRecentlyAddedPlaylistId;
       
   150             }
       
   151             break;
       
   152 
       
   153         default:
       
   154             User::Leave(KErrNotSupported);
       
   155         }
       
   156 
       
   157     return id;
       
   158     }
       
   159 
       
   160 // ----------------------------------------------------------------------------
       
   161 // CMPXDbAutoPlaylist::AutoPlaylistNameL
       
   162 // ----------------------------------------------------------------------------
       
   163 //
       
   164 TPtrC CMPXDbAutoPlaylist::AutoPlaylistNameL(
       
   165     TUint32 aPlaylistId)
       
   166     {
       
   167     MPX_FUNC("CMPXDbAutoPlaylist::AutoPlaylistNameL");
       
   168 
       
   169     TPtrC ptr;
       
   170     if (aPlaylistId == iRecentlyPlayedPlaylistId)
       
   171         {
       
   172         ptr.Set(*iTitleRecentlyPlayed);
       
   173         }
       
   174     else if (aPlaylistId == iMostPlayedPlaylistId)
       
   175         {
       
   176         ptr.Set(*iTitleMostPlayed);
       
   177         }
       
   178     else if (aPlaylistId == iRecentlyAddedPlaylistId)
       
   179         {
       
   180         ptr.Set(*iTitleRecentlyAdded);
       
   181         }
       
   182     else
       
   183         {
       
   184         User::Leave(KErrNotSupported);
       
   185         }
       
   186 
       
   187     return ptr;
       
   188     }
       
   189 
       
   190 // ----------------------------------------------------------------------------
       
   191 // CMPXDbAutoPlaylist::AutoPlaylistTypeL
       
   192 // ----------------------------------------------------------------------------
       
   193 //
       
   194 TMPXAutoPlaylistType CMPXDbAutoPlaylist::AutoPlaylistTypeL(
       
   195     TUint32 aPlaylistId)
       
   196     {
       
   197     MPX_FUNC("CMPXDbAutoPlaylist::AutoPlaylistTypeL");
       
   198 
       
   199     TMPXAutoPlaylistType type(EMPXNoAutoPlaylist);
       
   200     if (aPlaylistId == iRecentlyPlayedPlaylistId)
       
   201         {
       
   202         type = EMPXRecentlyPlayedPlaylist;
       
   203         }
       
   204     else if (aPlaylistId == iMostPlayedPlaylistId)
       
   205         {
       
   206         type = EMPXMostPlayedPlaylist;
       
   207         }
       
   208     else if (aPlaylistId == iRecentlyAddedPlaylistId)
       
   209         {
       
   210         type = EMPXRecentlyAddedPlaylist;
       
   211         }
       
   212     else
       
   213         {
       
   214         // do nothing
       
   215         }
       
   216 
       
   217     return type;
       
   218     }
       
   219 
       
   220 // ----------------------------------------------------------------------------
       
   221 // CMPXDbAutoPlaylist::GetPlaylistL
       
   222 // ----------------------------------------------------------------------------
       
   223 //
       
   224 void CMPXDbAutoPlaylist::GetPlaylistL(
       
   225     TUint32 aPlaylistId,
       
   226     const TArray<TMPXAttribute>& aAttrs,
       
   227     CMPXMedia& aMedia)
       
   228     {
       
   229     MPX_FUNC("CMPXDbAutoPlaylist::GetPlaylistL");
       
   230 
       
   231     if (EMPXNoAutoPlaylist == AutoPlaylistTypeL(aPlaylistId))
       
   232         {
       
   233         User::Leave(KErrNotSupported);
       
   234         }
       
   235 
       
   236     UpdateMediaL(aPlaylistId, aAttrs, aMedia);
       
   237     }
       
   238 
       
   239 // ----------------------------------------------------------------------------
       
   240 // CMPXDbAutoPlaylist::GetAllPlaylistsL
       
   241 // ----------------------------------------------------------------------------
       
   242 //
       
   243 void CMPXDbAutoPlaylist::GetAllPlaylistsL(
       
   244     CMPXMediaArray& aMediaArray)
       
   245     {
       
   246     MPX_FUNC("CMPXDbAutoPlaylist::GetAllPlaylistsL");
       
   247 
       
   248     MPX_ASSERT(aMediaArray.Count());
       
   249     MPX_ASSERT(aMediaArray[0]->IsSupported(KMPXMediaGeneralValue));
       
   250     TInt pPath = aMediaArray[0]->ValueTObjectL<TInt>(KMPXMediaGeneralValue);
       
   251     MPX_ASSERT(pPath);
       
   252     MPXDbCommonUtil::AppendMediaL(aMediaArray, *iMostPlayedPlaylist, EMPXItem,
       
   253         EMPXPlaylist, iMostPlayedPlaylistId, EMPXWrite);
       
   254     TInt count = aMediaArray.Count();
       
   255     TMPXItemId id = aMediaArray[count-1]->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId);
       
   256     if (count==2)
       
   257         { // the first playlist
       
   258         if (((CMPXCollectionPath*)pPath)->Id()==KMPXInvalidItemId)
       
   259             { // Remove top level with invalid id
       
   260             ((CMPXCollectionPath*)pPath)->Back();
       
   261             }
       
   262         ((CMPXCollectionPath*)pPath)->AppendL(id);
       
   263         }
       
   264     else
       
   265         { // append the end
       
   266         ((CMPXCollectionPath*)pPath)->InsertL(id,
       
   267                                               ((CMPXCollectionPath*)pPath)->Count());
       
   268         }
       
   269     TInt songCount = 0;
       
   270     TInt duration = 0;
       
   271     GetPlaylistDetailsL(EMPXMostPlayedPlaylist, songCount, duration);
       
   272     aMediaArray[count-1]->SetTObjectValueL<TInt>(KMPXMediaGeneralCount, songCount);
       
   273     aMediaArray[count-1]->SetTObjectValueL<TInt>(KMPXMediaGeneralDuration, duration);
       
   274 
       
   275     MPXDbCommonUtil::AppendMediaL(aMediaArray, *iRecentlyPlayedPlaylist, EMPXItem,
       
   276         EMPXPlaylist, iRecentlyPlayedPlaylistId, EMPXWrite);
       
   277     id = aMediaArray[count]->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId);
       
   278     ((CMPXCollectionPath*)pPath)->InsertL(id, ((CMPXCollectionPath*)pPath)->Count());
       
   279     songCount = 0;
       
   280     duration = 0;
       
   281     GetPlaylistDetailsL(EMPXRecentlyPlayedPlaylist, songCount, duration);
       
   282 	aMediaArray[count]->SetTObjectValueL<TInt>(KMPXMediaGeneralCount, songCount);
       
   283    	aMediaArray[count]->SetTObjectValueL<TInt>(KMPXMediaGeneralDuration, duration);
       
   284 
       
   285     MPXDbCommonUtil::AppendMediaL(aMediaArray, *iRecentlyAddedPlaylist, EMPXItem,
       
   286         EMPXPlaylist, iRecentlyAddedPlaylistId, EMPXWrite);
       
   287     id = aMediaArray[count+1]->ValueTObjectL<TMPXItemId>(KMPXMediaGeneralId);
       
   288     ((CMPXCollectionPath*)pPath)->InsertL(id, ((CMPXCollectionPath*)pPath)->Count());
       
   289     songCount = 0;
       
   290 	duration = 0;
       
   291 	GetPlaylistDetailsL(EMPXRecentlyAddedPlaylist, songCount, duration);
       
   292 	aMediaArray[count+1]->SetTObjectValueL<TInt>(KMPXMediaGeneralCount, songCount);
       
   293 	aMediaArray[count+1]->SetTObjectValueL<TInt>(KMPXMediaGeneralDuration, duration);
       
   294     }
       
   295 
       
   296 // ----------------------------------------------------------------------------
       
   297 // CMPXDbAutoPlaylist::GetPlaylistDetails
       
   298 // ----------------------------------------------------------------------------
       
   299 //
       
   300 void CMPXDbAutoPlaylist::GetPlaylistDetailsL(TMPXAutoPlaylistType aType, TInt& aCount, TInt& aDuration)
       
   301     {
       
   302     MPX_FUNC("CMPXDbAutoPlaylist::GetPlaylistDetails");
       
   303 
       
   304     HBufC* query = NULL;
       
   305 
       
   306     switch ( aType )
       
   307     	{
       
   308 		case EMPXMostPlayedPlaylist:
       
   309 		    query = HBufC::NewLC(KQueryMusicGetDurationMostPlayed().Length() + KMCIntegerLen);
       
   310 		    query->Des().Format(KQueryMusicGetDurationMostPlayed(), KMPXMaxMostPlayedSongs);
       
   311 		    break;
       
   312 		case EMPXRecentlyPlayedPlaylist:
       
   313 		    query = HBufC::NewLC(KQueryMusicGetDurationRecentlyPlayed().Length() + KMCIntegerLen);
       
   314 		    query->Des().Format(KQueryMusicGetDurationRecentlyPlayed(), KMPXMaxRecentlyPlayedSongs);
       
   315 			break;
       
   316 		case EMPXRecentlyAddedPlaylist:
       
   317 		    query = HBufC::NewLC(KQueryMusicGetDurationRecentlyAdded().Length());
       
   318 		    query->Des().Append(KQueryMusicGetDurationRecentlyAdded());
       
   319 			break;
       
   320 		default:
       
   321 			User::Leave(KErrArgument);
       
   322 			break;
       
   323 		}
       
   324 
       
   325     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(*query));
       
   326     CleanupClosePushL(recordset);
       
   327 
       
   328     TInt sum(0);
       
   329     TInt err(KErrNone);
       
   330     while ((err = recordset.Next()) == KSqlAtRow)
       
   331         {
       
   332         aCount++;
       
   333         sum += recordset.ColumnInt64(KMPXTableDefaultIndex);
       
   334         }
       
   335 
       
   336     CleanupStack::PopAndDestroy(&recordset);
       
   337     CleanupStack::PopAndDestroy(query);
       
   338 
       
   339     if (err != KSqlAtEnd)
       
   340         {
       
   341         User::Leave(KErrCorrupt);
       
   342         }
       
   343 
       
   344     aDuration = sum;
       
   345     MPX_DEBUG3("    Count[%d], Duration[%d]", aCount, aDuration);
       
   346     }
       
   347 
       
   348 // ----------------------------------------------------------------------------
       
   349 // CMPXDbAutoPlaylist::UpdateMediaL
       
   350 // ----------------------------------------------------------------------------
       
   351 //
       
   352 void CMPXDbAutoPlaylist::UpdateMediaL(
       
   353     TUint32 aPlaylistId,
       
   354     const TArray<TMPXAttribute>& aAttrs,
       
   355     CMPXMedia& aMedia)
       
   356     {
       
   357     MPX_FUNC("CMPXDbAutoPlaylist::UpdateMediaL");
       
   358 
       
   359     TInt count(aAttrs.Count());
       
   360     for (TInt i = 0; i < count; ++i)
       
   361         {
       
   362         TInt contentId(aAttrs[i].ContentId());
       
   363 
       
   364         if (contentId == KMPXMediaIdGeneral)
       
   365             {
       
   366             TUint attributeId(aAttrs[i].AttributeId());
       
   367 
       
   368             if (attributeId & EMPXMediaGeneralId)
       
   369                 {
       
   370                 aMedia.SetTObjectValueL<TMPXItemId>(KMPXMediaGeneralId, aPlaylistId);
       
   371                 }
       
   372             if (attributeId & EMPXMediaGeneralCollectionId )
       
   373                 {
       
   374                 aMedia.SetTObjectValueL<TUid>(KMPXMediaGeneralCollectionId,
       
   375                     TUid::Uid(KDBPluginUid));
       
   376                 }
       
   377             if (attributeId & EMPXMediaGeneralTitle)
       
   378                 {
       
   379                 if (aPlaylistId == iRecentlyPlayedPlaylistId)
       
   380                     {
       
   381                     aMedia.SetTextValueL(KMPXMediaGeneralTitle, *iRecentlyPlayedPlaylist);
       
   382                     }
       
   383                  else if (aPlaylistId == iMostPlayedPlaylistId)
       
   384                     {
       
   385                     aMedia.SetTextValueL(KMPXMediaGeneralTitle,*iMostPlayedPlaylist);
       
   386                     }
       
   387                  else if (aPlaylistId == iRecentlyAddedPlaylistId)
       
   388                     {
       
   389                     aMedia.SetTextValueL(KMPXMediaGeneralTitle, *iRecentlyAddedPlaylist);
       
   390                     }
       
   391                  else
       
   392                     {
       
   393                     User::Leave(KErrArgument);
       
   394                     }
       
   395                 }
       
   396             if (attributeId & EMPXMediaGeneralSynchronized)
       
   397                 {
       
   398                 aMedia.SetTObjectValueL<TBool>(KMPXMediaGeneralSynchronized, EFalse);
       
   399                 }
       
   400             if (attributeId & EMPXMediaGeneralNonPermissibleActions)
       
   401                 {
       
   402                 aMedia.SetTObjectValueL<TMPXGeneralNonPermissibleActions>(
       
   403                     KMPXMediaGeneralNonPermissibleActions, EMPXWrite);
       
   404                 }
       
   405             // EMPXMediaGeneralCount, EMPXMediaGeneralDate, EMPXMediaGeneralDuration,
       
   406             // EMPXMediaGeneralUri, EMPXMediaGeneralDrive, and EMPXMediaGeneralFlags
       
   407             // attributes are not applicable for auto-playlists
       
   408             } // end if contentId == KMPXMediaIdGeneral
       
   409         } // end for
       
   410 
       
   411     aMedia.SetTObjectValueL<TMPXGeneralType>(KMPXMediaGeneralType, EMPXItem);
       
   412     aMedia.SetTObjectValueL<TMPXGeneralCategory>(KMPXMediaGeneralCategory, EMPXPlaylist);
       
   413     }
       
   414 
       
   415 // End of File