mpxplugins/serviceplugins/collectionplugins/mpxsqlitedbhgplugin/src/mpxdbartist.cpp
branchRCL_3
changeset 26 3de6c4cf6b67
child 27 2cbbefa9af78
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 interation with the Artist table
       
    15 *
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <sqldb.h>
       
    22 
       
    23 #include <mpxlog.h>
       
    24 
       
    25 #include "mpxdbcommonutil.h"
       
    26 #include "mpxdbcommondef.h"
       
    27 #include "mpxmediamusicdefs.h"
       
    28 #include "mpxdbmanager.h"
       
    29 
       
    30 #include "mpxcollectiondbdef.h"
       
    31 #include "mpxdbpluginqueries.h"
       
    32 #include "mpxdbutil.h"
       
    33 #include "mpxdbartist.h"
       
    34 
       
    35 // CONSTANTS
       
    36 
       
    37 // ============================ MEMBER FUNCTIONS ==============================
       
    38 
       
    39 // ----------------------------------------------------------------------------
       
    40 // Two-phased constructor.
       
    41 // ----------------------------------------------------------------------------
       
    42 //
       
    43 CMPXDbArtist* CMPXDbArtist::NewL(
       
    44     CMPXDbManager& aDbManager,
       
    45     TMPXGeneralCategory aCategory,
       
    46     MMPXDbArtistObserver& aObserver)
       
    47     {
       
    48     MPX_FUNC("CMPXDbArtist::NewL");
       
    49 
       
    50     CMPXDbArtist* self = CMPXDbArtist::NewLC(aDbManager, aCategory, aObserver);
       
    51     CleanupStack::Pop(self);
       
    52     return self;
       
    53     }
       
    54 
       
    55 // ----------------------------------------------------------------------------
       
    56 // Two-phased constructor.
       
    57 // ----------------------------------------------------------------------------
       
    58 //
       
    59 CMPXDbArtist* CMPXDbArtist::NewLC(
       
    60     CMPXDbManager& aDbManager,
       
    61     TMPXGeneralCategory aCategory,
       
    62     MMPXDbArtistObserver& aObserver)
       
    63     {
       
    64     MPX_FUNC("CMPXDbArtist::NewLC");
       
    65 
       
    66     CMPXDbArtist* self = new (ELeave) CMPXDbArtist(aDbManager, aCategory, aObserver);
       
    67     CleanupStack::PushL(self);
       
    68     self->ConstructL();
       
    69     return self;
       
    70     }
       
    71 
       
    72 // ----------------------------------------------------------------------------
       
    73 // Destructor
       
    74 // ----------------------------------------------------------------------------
       
    75 //
       
    76 CMPXDbArtist::~CMPXDbArtist()
       
    77     {
       
    78     MPX_FUNC("CMPXDbArtist::~CMPXDbArtist");
       
    79     }
       
    80 
       
    81 // ----------------------------------------------------------------------------
       
    82 // Constructor
       
    83 // ----------------------------------------------------------------------------
       
    84 //
       
    85 CMPXDbArtist::CMPXDbArtist(
       
    86     CMPXDbManager& aDbManager,
       
    87     TMPXGeneralCategory aCategory,
       
    88     MMPXDbArtistObserver& aObserver) :
       
    89     CMPXDbCategory(aDbManager, aCategory),
       
    90     iObserver(aObserver)
       
    91     {
       
    92     MPX_FUNC("CMPXDbArtist::CMPXDbArtist");
       
    93     }
       
    94 
       
    95 // ----------------------------------------------------------------------------
       
    96 // Second phase constructor.
       
    97 // ----------------------------------------------------------------------------
       
    98 //
       
    99 void CMPXDbArtist::ConstructL()
       
   100     {
       
   101     MPX_FUNC("CMPXDbArtist::ConstructL");
       
   102 
       
   103     BaseConstructL();
       
   104     }
       
   105 
       
   106 // ----------------------------------------------------------------------------
       
   107 // CMPXDbArtist::AddItemL
       
   108 // ----------------------------------------------------------------------------
       
   109 //
       
   110 TUint32 CMPXDbArtist::AddItemL(
       
   111     const TDesC& aName,
       
   112     const TDesC& aArt,
       
   113     TInt aDriveId,
       
   114     TBool& aNewRecord,
       
   115     TBool aCaseSensitive)
       
   116     {
       
   117     MPX_FUNC("CMPXDbArtist::AddItemL");
       
   118 
       
   119     // try to find the item first
       
   120     TUint32 rowId(MPXDbCommonUtil::GenerateUniqueIdL(iDbManager.Fs(), iCategory,
       
   121         aName, aCaseSensitive));
       
   122     aNewRecord = !CategoryItemExistsL(aDriveId, rowId);
       
   123 
       
   124     if (aNewRecord)
       
   125         {
       
   126         // insert new
       
   127         HBufC* query = PreProcessStringLC(KQueryArtistInsert);
       
   128         HBufC* name = MPXDbCommonUtil::ProcessSingleQuotesLC(aName);
       
   129         HBufC* art = MPXDbCommonUtil::ProcessSingleQuotesLC(aArt);
       
   130 
       
   131         iDbManager.ExecuteQueryL(aDriveId, *query, rowId, name, 1, art);
       
   132 
       
   133         CleanupStack::PopAndDestroy(art);
       
   134         CleanupStack::PopAndDestroy(name);
       
   135         CleanupStack::PopAndDestroy(query);
       
   136         }
       
   137     else
       
   138         {
       
   139         // retrieve the existing record
       
   140         HBufC* query = NULL;
       
   141         query = PreProcessStringLC(KQueryCategoryItem);
       
   142         RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(*query, rowId));
       
   143         CleanupStack::PopAndDestroy(query);
       
   144 
       
   145         CleanupClosePushL(recordset);
       
   146 
       
   147         if (recordset.Next() != KSqlAtRow)
       
   148             {
       
   149             User::Leave(KErrNotFound);
       
   150         }
       
   151 
       
   152         // Album Art
       
   153         TPtrC art(KNullDesC);
       
   154         art.Set(MPXDbCommonUtil::GetColumnTextL(recordset, EArtistArt));
       
   155 
       
   156         // the current one is Unknown and the new one is Not Unknown
       
   157         if ( art == KNullDesC && aArt != KNullDesC )
       
   158             {
       
   159             HBufC* artReplaceSingleQuote = 
       
   160                 MPXDbCommonUtil::ProcessSingleQuotesLC( aArt );
       
   161             _LIT( KFormatArt, "Art=\'%S\'" );
       
   162             HBufC* setStr = HBufC::NewLC(256);
       
   163             setStr->Des().Format( KFormatArt, artReplaceSingleQuote );
       
   164 
       
   165             iDbManager.ExecuteQueryL(aDriveId, KQueryArtistUpdate, setStr, rowId);
       
   166             CleanupStack::PopAndDestroy(setStr);
       
   167             CleanupStack::PopAndDestroy(artReplaceSingleQuote);
       
   168             }
       
   169 
       
   170         CleanupStack::PopAndDestroy(&recordset);
       
   171 
       
   172         // increment the number of songs for the category
       
   173         query = PreProcessStringLC(KQueryCategoryIncrementSongCount);
       
   174         iDbManager.ExecuteQueryL(aDriveId, *query, rowId);
       
   175         CleanupStack::PopAndDestroy(query);
       
   176         }
       
   177 
       
   178     return rowId;
       
   179     }
       
   180 
       
   181 // ----------------------------------------------------------------------------
       
   182 // CMPXDbArtist::IsUnknownArtistL
       
   183 // ----------------------------------------------------------------------------
       
   184 //
       
   185 TBool CMPXDbArtist::IsUnknownArtistL(TUint32 aId)
       
   186     {
       
   187 	MPX_FUNC("CMPXDbArtist::IsUnknownArtistL");
       
   188 
       
   189     HBufC* name = GetNameL(aId);
       
   190 
       
   191 	TInt ret = EFalse;
       
   192 	if (*name == KNullDesC)
       
   193 		{
       
   194 		ret = ETrue;
       
   195 		}
       
   196 	delete name;
       
   197 
       
   198 	return ret;
       
   199     }
       
   200 
       
   201 // ----------------------------------------------------------------------------
       
   202 // CMPXDbArtist::UpdateMediaL
       
   203 // ----------------------------------------------------------------------------
       
   204 //
       
   205 void CMPXDbArtist::UpdateMediaL(
       
   206     RSqlStatement& aRecord,
       
   207     const TArray<TMPXAttribute>& aAttrs,
       
   208     CMPXMedia& aMedia)
       
   209     {
       
   210     MPX_FUNC("CMPXDbArtist::UpdateMediaL");
       
   211 
       
   212     TInt count(aAttrs.Count());
       
   213     for (TInt i = 0; i < count; ++i)
       
   214         {
       
   215         TInt contentId(aAttrs[i].ContentId());
       
   216         TUint attributeId(aAttrs[i].AttributeId());
       
   217 
       
   218         if (contentId == KMPXMediaIdGeneral)
       
   219             {
       
   220             if (attributeId & EMPXMediaGeneralId)
       
   221                 {
       
   222                 MPX_DEBUG1("	EMPXMediaGeneralId");
       
   223 
       
   224                 aMedia.SetTObjectValueL<TMPXItemId>(KMPXMediaGeneralId,
       
   225                     aRecord.ColumnInt64(EArtistUniqueId));
       
   226 				MPX_DEBUG2("	Id[%d]", aRecord.ColumnInt64(EArtistUniqueId));
       
   227                 }
       
   228             if (attributeId & EMPXMediaGeneralTitle)
       
   229                 {
       
   230                 MPX_DEBUG1("	EMPXMediaGeneralTitle");
       
   231 
       
   232                 TPtrC artist( MPXDbCommonUtil::GetColumnTextL(aRecord, EArtistName) );
       
   233                 aMedia.SetTextValueL(KMPXMediaGeneralTitle,
       
   234                     MPXDbCommonUtil::GetColumnTextL(aRecord, EArtistName));
       
   235 
       
   236 				MPX_DEBUG2("	Artist[%S]", &artist);
       
   237                 }
       
   238             if (attributeId & EMPXMediaGeneralCount)
       
   239                 {
       
   240 //				TInt albumCount = GetAlbumsCountL(aRecord.ColumnInt64(EArtistUniqueId));
       
   241 //              aMedia.SetTObjectValueL<TInt>(KMPXMediaGeneralCount,albumCount);
       
   242 //              MPX_DEBUG1("	EMPXMediaGeneralCount");
       
   243 //				MPX_DEBUG2("	AlbumCount[%d]", albumCount);
       
   244                 // get songs count from all drives
       
   245                 TInt songCount = GetSongsCountL( KDbManagerAllDrives,
       
   246                                     aRecord.ColumnInt64( EArtistUniqueId ) );
       
   247 				aMedia.SetTObjectValueL<TInt>(KMPXMediaGeneralCount, songCount); // mod by anjokela
       
   248 
       
   249                 MPX_DEBUG1("	EMPXMediaGeneralCount");
       
   250 				MPX_DEBUG2("	SongCount[%d]", songCount);
       
   251                 }
       
   252             } // end if contentId == KMPXMediaIdGeneral
       
   253         else if ( contentId == KMPXMediaIdMusic )
       
   254             {
       
   255             if (attributeId & EMPXMediaMusicAlbumArtFileName)
       
   256                 {
       
   257                 MPX_DEBUG1("    EMPXMediaMusicAlbumArtFileName");
       
   258                 TPtrC art(KNullDesC);
       
   259                 art.Set(MPXDbCommonUtil::GetColumnTextL(aRecord, EArtistArt));
       
   260                 aMedia.SetTextValueL(KMPXMediaMusicAlbumArtFileName, art);
       
   261                 MPX_DEBUG2("    Art[%S]", &art);
       
   262                 }
       
   263             }
       
   264 		} // end for
       
   265 
       
   266     aMedia.SetTObjectValueL<TMPXGeneralType>(KMPXMediaGeneralType, EMPXItem);
       
   267     aMedia.SetTObjectValueL<TMPXGeneralCategory>(KMPXMediaGeneralCategory, iCategory);
       
   268     }
       
   269 
       
   270 // ----------------------------------------------------------------------------
       
   271 // CMPXDbArtist::GenerateArtistFieldsValuesL
       
   272 // ----------------------------------------------------------------------------
       
   273 //
       
   274 void CMPXDbArtist::GenerateArtistFieldsValuesL(const CMPXMedia& aMedia, CDesCArray& aFields, CDesCArray& aValues)
       
   275     {
       
   276     if (aMedia.IsSupported(KMPXMediaMusicAlbumArtFileName))
       
   277         {
       
   278         const TDesC& albumArtFilename = aMedia.ValueText(KMPXMediaMusicAlbumArtFileName).Left(KMCMaxTextLen);
       
   279         MPXDbCommonUtil::AppendValueL(aFields, aValues, KMCMusicArt, albumArtFilename);
       
   280         }
       
   281     }
       
   282 
       
   283 // ----------------------------------------------------------------------------
       
   284 // CMPXDbArtist::GetAlbumsCountL
       
   285 // ----------------------------------------------------------------------------
       
   286 //
       
   287 TInt CMPXDbArtist::GetAlbumsCountL(TUint32 aId)
       
   288     {
       
   289     MPX_FUNC("CMPXDbArtist::GetAlbumsCountL");
       
   290 
       
   291 	return iObserver.HandleGetAlbumsCountForArtistL(aId);
       
   292     }
       
   293 
       
   294 // ----------------------------------------------------------------------------
       
   295 // CMPXDbArtist::GetAllCategoryItemsL
       
   296 // ----------------------------------------------------------------------------
       
   297 //
       
   298 void CMPXDbArtist::GetAllCategoryItemsL(
       
   299     const TArray<TMPXAttribute>& aAttrs,
       
   300     CMPXMediaArray& aMediaArray)
       
   301     {
       
   302     MPX_FUNC("CMPXDbArtist::GetAllCategoryItemsL");
       
   303     HBufC* query = PreProcessStringLC(KQueryArtistAll);
       
   304     RSqlStatement recordset(iDbManager.ExecuteSelectQueryL(*query));
       
   305     CleanupStack::PopAndDestroy(query);
       
   306     CleanupClosePushL(recordset);
       
   307     ProcessRecordsetL(aAttrs, recordset, aMediaArray);
       
   308     CleanupStack::PopAndDestroy(&recordset);
       
   309     }
       
   310 
       
   311 // ----------------------------------------------------------------------------
       
   312 // CMPXDbArtist::UpdateItemL
       
   313 // ----------------------------------------------------------------------------
       
   314 //
       
   315 void CMPXDbArtist::UpdateItemL(
       
   316     TUint32 aId,
       
   317     const CMPXMedia& aMedia,
       
   318     TInt aDriveId,
       
   319     CMPXMessageArray* aItemChangedMessages)
       
   320     {
       
   321     MPX_FUNC("CMPXDbAlbum::UpdateItemL");
       
   322 
       
   323     CDesCArrayFlat* fields = new (ELeave) CDesCArrayFlat(EArtistFieldCount);
       
   324     CleanupStack::PushL(fields);
       
   325     CDesCArrayFlat* values = new (ELeave) CDesCArrayFlat(EArtistFieldCount);
       
   326     CleanupStack::PushL(values);
       
   327 
       
   328     // process the media parameter and construct the fields and values array
       
   329     GenerateArtistFieldsValuesL(aMedia, *fields, *values);
       
   330 
       
   331     // construct the SET string
       
   332     HBufC* setStr = MPXDbCommonUtil::StringFromArraysLC(*fields, *values, KMCEqualSign, KMCCommaSign);
       
   333 
       
   334     if (setStr->Length())
       
   335         {
       
   336         // execute the query
       
   337         iDbManager.ExecuteQueryL(aDriveId, KQueryArtistUpdate, setStr, aId);
       
   338         MPXDbCommonUtil::AddItemAlbumChangedMessageL(*aItemChangedMessages, aId, EMPXItemModified,
       
   339         		EMPXArtist, KDBPluginUid, ETrue, 0 );          
       
   340         }
       
   341 
       
   342     CleanupStack::PopAndDestroy(setStr);
       
   343     CleanupStack::PopAndDestroy(values);
       
   344     CleanupStack::PopAndDestroy(fields);
       
   345     }
       
   346 // ----------------------------------------------------------------------------
       
   347 // CMPXDbAlbum::CreateTableL
       
   348 // ----------------------------------------------------------------------------
       
   349 //
       
   350 void CMPXDbArtist::CreateTableL(
       
   351     RSqlDatabase& aDatabase,
       
   352     TBool /* aCorruptTable */)
       
   353     {
       
   354     MPX_FUNC("CMPXDbCategory::CreateTableL");
       
   355 
       
   356     // create the table
       
   357     HBufC* query = PreProcessStringLC(KArtistCreateTable);
       
   358     User::LeaveIfError(aDatabase.Exec(*query));
       
   359     CleanupStack::PopAndDestroy(query);
       
   360 
       
   361     // do not create an index on the Name field
       
   362     // as it only slows down the insert/update queries overall
       
   363     }
       
   364 
       
   365 // ----------------------------------------------------------------------------
       
   366 // CMPXDbAlbum::CheckTableL
       
   367 // ----------------------------------------------------------------------------
       
   368 //
       
   369 TBool CMPXDbArtist::CheckTableL(
       
   370     RSqlDatabase& aDatabase)
       
   371     {
       
   372     MPX_FUNC("CMPXDbCategory::CheckTableL");
       
   373 
       
   374     HBufC* query = PreProcessStringLC(KArtistCheckTable);
       
   375     TBool check(DoCheckTable(aDatabase, *query));
       
   376     CleanupStack::PopAndDestroy(query);
       
   377 
       
   378     return check;
       
   379     }
       
   380 
       
   381 // End of File