mpxplugins/serviceplugins/collectionplugins/mpxsqlitedbhgplugin/src/mpxdbabstractalbum.cpp
branchRCL_3
changeset 13 c8156a91d13c
child 14 c54d95799c80
equal deleted inserted replaced
12:171e07ac910f 13:c8156a91d13c
       
     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 the License "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 category tables:
       
    15 *                Artist, Album, Genre, Composer and AbstractAlbum
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <mpxlog.h>
       
    22 #include <bautils.h>
       
    23 #include <mpxmediamusicdefs.h>
       
    24 #include "mpxdbabstractalbum.h"
       
    25 #include "mpxdbpluginqueries.h"
       
    26 #include "mpxdbmanager.h"
       
    27 #include "mpxdbcommonutil.h"
       
    28 #include "mpxcollectiondbstd.h"
       
    29 #include "mpxdbcommondef.h"
       
    30 #include "mpxcollectiondbdef.h"
       
    31 
       
    32 #ifdef RD_MPX_TNM_INTEGRATION
       
    33 _LIT( KImageFileType, "image/jpeg" );
       
    34 #endif //RD_MPX_TNM_INTEGRATION
       
    35 
       
    36 
       
    37 // CONSTANTS
       
    38 
       
    39 
       
    40 
       
    41 
       
    42 // ============================ MEMBER FUNCTIONS ==============================
       
    43 
       
    44 // ----------------------------------------------------------------------------
       
    45 // Two-phased constructor.
       
    46 // ----------------------------------------------------------------------------
       
    47 //
       
    48 CMPXDbAbstractAlbum* CMPXDbAbstractAlbum::NewL(
       
    49     CMPXDbManager& aDbManager,
       
    50     TMPXGeneralCategory aCategory)
       
    51     {
       
    52     MPX_FUNC("CMPXDbAbstractAlbum::NewL");
       
    53 
       
    54     CMPXDbAbstractAlbum* self = CMPXDbAbstractAlbum::NewLC(aDbManager, aCategory);
       
    55     CleanupStack::Pop(self);
       
    56     return self;
       
    57     }
       
    58 
       
    59 // ----------------------------------------------------------------------------
       
    60 // Two-phased constructor.
       
    61 // ----------------------------------------------------------------------------
       
    62 //
       
    63 CMPXDbAbstractAlbum* CMPXDbAbstractAlbum::NewLC(
       
    64     CMPXDbManager& aDbManager,
       
    65     TMPXGeneralCategory aCategory)
       
    66     {
       
    67     MPX_FUNC("CMPXDbAbstractAlbum::NewLC");
       
    68 
       
    69     CMPXDbAbstractAlbum* self = new (ELeave) CMPXDbAbstractAlbum(aDbManager, aCategory);
       
    70     CleanupStack::PushL(self);
       
    71     self->ConstructL();
       
    72     return self;
       
    73     }
       
    74 
       
    75 // ----------------------------------------------------------------------------
       
    76 // Destructor
       
    77 // ----------------------------------------------------------------------------
       
    78 //
       
    79 CMPXDbAbstractAlbum::~CMPXDbAbstractAlbum()
       
    80     {
       
    81     MPX_FUNC("CMPXDbAbstractAlbum::~CMPXDbAbstractAlbum");
       
    82 #ifdef RD_MPX_TNM_INTEGRATION
       
    83     delete iTNManager;
       
    84 #endif //RD_MPX_TNM_INTEGRATION
       
    85     }
       
    86 
       
    87 // ----------------------------------------------------------------------------
       
    88 // CMPXDbAbstractAlbum::GetDriveIdL
       
    89 // ----------------------------------------------------------------------------
       
    90 //
       
    91 TInt CMPXDbAbstractAlbum::GetDriveIdL(
       
    92     TUint32 aAbstractAlbumId)
       
    93     {
       
    94     MPX_FUNC("CMPXDbAbstractAlbum::GetDriveIdL");
       
    95     return MPXDbCommonUtil::GetDriveIdMatchVolIdL(iDbManager.Fs(),
       
    96         ExecuteIntQueryL(KCriterionCategoryVolumeId, aAbstractAlbumId));
       
    97     }
       
    98 
       
    99 
       
   100 // ----------------------------------------------------------------------------
       
   101 // CMPXDbAbstractAlbum::AddItemL
       
   102 // ----------------------------------------------------------------------------
       
   103 //
       
   104 TUint32 CMPXDbAbstractAlbum::AddItemL(
       
   105     const TDesC& aName,
       
   106     const TDesC& aAlbumArtist,
       
   107     const TDesC& aGenre,
       
   108     TInt aDriveId,
       
   109     TBool& aNewRecord,
       
   110     TBool aCaseSensitive)
       
   111     {
       
   112     MPX_FUNC("CMPXDbAbstractAlbum::AddItemL");
       
   113 
       
   114     TUint32 rowId(MPXDbCommonUtil::GenerateUniqueIdL(iDbManager.Fs(), EMPXAbstractAlbum,
       
   115         aName, aCaseSensitive));
       
   116   
       
   117     if ( aNewRecord )
       
   118         {
       
   119         HBufC* name = MPXDbCommonUtil::ProcessSingleQuotesLC(aName);
       
   120         HBufC* albumartist = MPXDbCommonUtil::ProcessSingleQuotesLC(aAlbumArtist);
       
   121         HBufC* genre = MPXDbCommonUtil::ProcessSingleQuotesLC(aGenre);
       
   122         
       
   123         iDbManager.ExecuteQueryL(aDriveId, KQueryAbstractAlbumInsert, rowId, name, albumartist, genre, 0, MPXDbCommonUtil::GetVolIdMatchDriveIdL(iDbManager.Fs(), aDriveId));
       
   124         CleanupStack::PopAndDestroy(3, name);        
       
   125         }
       
   126     else
       
   127         {
       
   128         // increment the number of songs for the category
       
   129         HBufC* query = PreProcessStringLC(KQueryCategoryIncrementSongCount);
       
   130         iDbManager.ExecuteQueryL(aDriveId, *query, rowId);
       
   131         CleanupStack::PopAndDestroy(query);
       
   132         }
       
   133 
       
   134     return rowId;
       
   135     }
       
   136 
       
   137 
       
   138 
       
   139 // ----------------------------------------------------------------------------
       
   140 // CMPXDbAbstractAlbum::DecrementSongsForCategoryL
       
   141 // ----------------------------------------------------------------------------
       
   142 //
       
   143 void CMPXDbAbstractAlbum::DecrementSongsForCategoryL(
       
   144     const TUint32 aId,
       
   145     TInt aDriveId,
       
   146     CMPXMessageArray* aItemChangedMessages,
       
   147     TBool& aItemExist,
       
   148     TBool aMtpInUse)
       
   149     {
       
   150     MPX_FUNC("CMPXDbAbstractAlbum::DecrementSongsForCategoryL");
       
   151     
       
   152     TInt songCount = GetSongsCountL(aDriveId, aId);
       
   153        // if just one song uses this category. Use <= just in case
       
   154        //while MTP connection, just decrease songCount in DB, do not do deletion.
       
   155     if ( songCount > 0 )
       
   156         {
       
   157         aItemExist = ETrue;
       
   158         // decrement the number of songs for the category
       
   159         HBufC* query = PreProcessStringLC(KQueryCategoryDecrementSongCount);
       
   160         iDbManager.ExecuteQueryL(aDriveId, *query, aId);
       
   161         CleanupStack::PopAndDestroy(query);
       
   162         songCount--;
       
   163         }
       
   164     
       
   165     if ((songCount == 0) && !aMtpInUse)
       
   166         {
       
   167         HBufC* uri = DeleteAbstractAlbumL(aId, aDriveId);
       
   168         MPX_DEBUG1("CMPXDbAbstractAlbum::DeleteAbstractAlbumL,implicitly delete AbstractAlbum Object when it has 0 references");  
       
   169         aItemExist = EFalse;
       
   170         if (aItemChangedMessages)
       
   171             {
       
   172             // add the item changed message
       
   173             MPXDbCommonUtil::AddItemChangedMessageL(*aItemChangedMessages, aId, EMPXItemDeleted,
       
   174                 EMPXAbstractAlbum, KDBPluginUid);   
       
   175             }
       
   176         delete uri;
       
   177         }
       
   178     }
       
   179 
       
   180 
       
   181 // ----------------------------------------------------------------------------
       
   182 // CMPXDbAbstractAlbum::DeleteAbstractAlbumL
       
   183 // ----------------------------------------------------------------------------
       
   184 //
       
   185 HBufC* CMPXDbAbstractAlbum::DeleteAbstractAlbumL(
       
   186     TUint32 aAbstractAlbumId, TInt aDriveId)
       
   187     {
       
   188     MPX_FUNC("CMPXDbAbstractAlbum::DeleteAbstractAlbumL");
       
   189 
       
   190     //before delete category, get the abstract album path
       
   191     HBufC* uri = GetNameL(aAbstractAlbumId);
       
   192     // delete the category
       
   193     if (uri)
       
   194         {
       
   195         CleanupStack::PushL(uri);
       
   196          
       
   197     if (aDriveId)
       
   198         DeleteCategoryL(aAbstractAlbumId, aDriveId);
       
   199     else
       
   200         {
       
   201         TInt drive = MPXDbCommonUtil::GetDriveIdMatchVolIdL(iDbManager.Fs(),
       
   202             ExecuteIntQueryL(KCriterionCategoryVolumeId, aAbstractAlbumId));
       
   203         DeleteCategoryL(aAbstractAlbumId, drive);
       
   204         }
       
   205 //delete abstract album .alb from TN table
       
   206 #ifdef RD_MPX_TNM_INTEGRATION
       
   207         // remove from thumbnail database table
       
   208         CThumbnailObjectSource* source = CThumbnailObjectSource::NewLC(
       
   209                 *uri, KImageFileType );
       
   210         iTNManager->DeleteThumbnails( *source );
       
   211         CleanupStack::PopAndDestroy( source );
       
   212 #endif //RD_MPX_TNM_INTEGRATION
       
   213 
       
   214        
       
   215         //delete abstract album .alb file from file system
       
   216         RFs rFs;
       
   217         User::LeaveIfError( rFs.Connect() );
       
   218         CleanupClosePushL( rFs );
       
   219 
       
   220         TInt err = BaflUtils::DeleteFile(rFs, *uri);
       
   221         if(KErrNone != err)
       
   222             {
       
   223             MPX_DEBUG2("CMPXDbAbstractAlbum::DeleteAbstractAlbumL,File not deleted from file system with err=%d", err);
       
   224             }
       
   225 
       
   226         CleanupStack::PopAndDestroy( &rFs );
       
   227         }
       
   228 
       
   229     CleanupStack::Pop(uri);
       
   230     return uri;
       
   231     }
       
   232 
       
   233 
       
   234 // ----------------------------------------------------------------------------
       
   235 // CMPXDbAbstractAlbum::UpdateItemL
       
   236 // ----------------------------------------------------------------------------
       
   237 //
       
   238 void CMPXDbAbstractAlbum::UpdateItemL(
       
   239     TUint32 aId,
       
   240     const CMPXMedia& aMedia,
       
   241     TInt aDriveId,
       
   242     CMPXMessageArray* aItemChangedMessages)
       
   243     {
       
   244     MPX_FUNC("CMPXDbAbstractAlbum::UpdateItemL");
       
   245 
       
   246     CDesCArrayFlat* fields = new (ELeave) CDesCArrayFlat(EAbstractAlbumFieldCount);
       
   247     CleanupStack::PushL(fields);
       
   248     CDesCArrayFlat* values = new (ELeave) CDesCArrayFlat(EAbstractAlbumFieldCount);
       
   249     CleanupStack::PushL(values);
       
   250 
       
   251     // process the media parameter and construct the fields and values array
       
   252     GenerateAbstractAlbumFieldsValuesL(aMedia, *fields, *values);
       
   253 
       
   254     // construct the SET string
       
   255     HBufC* setStr = MPXDbCommonUtil::StringFromArraysLC(*fields, *values, KMCEqualSign, KMCCommaSign);
       
   256 
       
   257     if (setStr->Length())
       
   258         {
       
   259         // execute the query
       
   260         iDbManager.ExecuteQueryL(aDriveId, KQueryAbstractAlbumUpdate, setStr, aId);
       
   261         MPXDbCommonUtil::AddItemAlbumChangedMessageL(*aItemChangedMessages, aId, EMPXItemModified,
       
   262                 EMPXAbstractAlbum, KDBPluginUid, ETrue, 0 );
       
   263         }
       
   264 
       
   265     CleanupStack::PopAndDestroy(setStr);
       
   266     CleanupStack::PopAndDestroy(values);
       
   267     CleanupStack::PopAndDestroy(fields);
       
   268     }
       
   269 
       
   270 // ----------------------------------------------------------------------------
       
   271 // CMPXDbAbstractAlbum::UpdateMediaL
       
   272 // ----------------------------------------------------------------------------
       
   273 //
       
   274 void CMPXDbAbstractAlbum::UpdateMediaL(
       
   275     RSqlStatement& aRecord,
       
   276     const TArray<TMPXAttribute>& aAttrs,
       
   277     CMPXMedia& aMedia)
       
   278     {
       
   279      MPX_FUNC("CMPXDbAbstractAlbum::UpdateMediaL");
       
   280 
       
   281        TInt count(aAttrs.Count());
       
   282        for (TInt i = 0; i < count; ++i)
       
   283            {
       
   284            TInt contentId(aAttrs[i].ContentId());
       
   285            TUint attributeId(aAttrs[i].AttributeId());
       
   286 
       
   287            if (contentId == KMPXMediaIdGeneral)
       
   288                {
       
   289                if (attributeId & EMPXMediaGeneralId)
       
   290                    {
       
   291                    aMedia.SetTObjectValueL<TMPXItemId>(KMPXMediaGeneralId,
       
   292                        aRecord.ColumnInt64(EAbstractAlbumUniqueId));
       
   293                    }
       
   294                if (attributeId & EMPXMediaGeneralTitle)
       
   295                    {
       
   296                    TPtrC title(MPXDbCommonUtil::GetColumnTextL(aRecord, EAbstractAlbumName));
       
   297                    aMedia.SetTextValueL(KMPXMediaGeneralTitle, title);
       
   298                    MPX_DEBUG2("    AbstractAlbumName[%S]", &title);
       
   299                    }                          
       
   300                if (attributeId & EMPXMediaGeneralCount)
       
   301                    {
       
   302                    TInt songCount = GetSongsCountL(KDbManagerAllDrives,
       
   303                                 aRecord.ColumnInt64(EAbstractAlbumUniqueId));
       
   304                    aMedia.SetTObjectValueL<TInt>(KMPXMediaGeneralCount, songCount );
       
   305                    }
       
   306                if (attributeId & EMPXMediaGeneralDrive)
       
   307                    {
       
   308                    TUint32 volId(aRecord.ColumnInt64(EAbstractAlbumVolumeId));
       
   309                    TInt driveId = MPXDbCommonUtil::GetDriveIdMatchVolIdL(iDbManager.Fs(), volId);
       
   310 
       
   311                    // LTAN-7GH6BZ, crash if eject memory card when adding song to existing AbstractAlbum
       
   312                    // due to special timing issue, it is possible drive number is -1 and create a
       
   313                    // panic when use for TDriveUnit
       
   314                    MPX_DEBUG3("volId = %d, driveId = %d", volId, driveId);
       
   315 
       
   316                    // handle possibly delay from framework notification
       
   317                    if (driveId < 0)
       
   318                        {
       
   319                        MPX_DEBUG1("invalid driveId, leave with KErrNotReady");
       
   320                        User::Leave(KErrNotReady);
       
   321                        }
       
   322                    TDriveUnit driveUnit(driveId);
       
   323 
       
   324                    aMedia.SetTextValueL(KMPXMediaGeneralDrive, driveUnit.Name());
       
   325 
       
   326                    }
       
   327                } // end if contentId == KMPXMediaIdGeneral          
       
   328            else if ( contentId == KMPXMediaIdMusic)
       
   329                {
       
   330                if (attributeId & EMPXMediaMusicAlbumArtist)
       
   331                    {         
       
   332                    TPtrC albumartist(MPXDbCommonUtil::GetColumnTextL(aRecord, EAbstractAlbumArtist));
       
   333                    aMedia.SetTextValueL(KMPXMediaMusicAlbumArtist, albumartist);
       
   334                    
       
   335                   
       
   336                    
       
   337                    MPX_DEBUG2("    albumartist[%S]", &albumartist);
       
   338                    } 
       
   339                if (attributeId & EMPXMediaMusicGenre)
       
   340                    {                 
       
   341                    TPtrC genre(MPXDbCommonUtil::GetColumnTextL(aRecord, EAbstractAlbumGenre));
       
   342                    aMedia.SetTextValueL(KMPXMediaMusicGenre, genre);
       
   343                    MPX_DEBUG2("    Genre[%S]", &genre);
       
   344                    }
       
   345                }
       
   346                
       
   347            } // end for
       
   348        aMedia.SetTObjectValueL<TMPXGeneralType>(KMPXMediaGeneralType, EMPXItem);
       
   349        aMedia.SetTObjectValueL<TMPXGeneralCategory>(KMPXMediaGeneralCategory, EMPXAbstractAlbum);
       
   350     }
       
   351 
       
   352 
       
   353 // ----------------------------------------------------------------------------
       
   354 // CMPXDbAbstractAlbum::GenerateAlbumFieldsValuesL
       
   355 // ----------------------------------------------------------------------------
       
   356 //
       
   357 void CMPXDbAbstractAlbum::GenerateAbstractAlbumFieldsValuesL(const CMPXMedia& aMedia, CDesCArray& aFields, CDesCArray& aValues)
       
   358     {
       
   359     MPX_FUNC("CMPXDbAbstractAlbum::GenerateAbstractAlbumFieldsValuesL");
       
   360 //support Winlogo use case
       
   361    if (aMedia.IsSupported(KMPXMediaGeneralTitle))
       
   362         {
       
   363         TPtrC truncatedName(aMedia.ValueText(KMPXMediaGeneralTitle).Left(KMCMaxTextLen));
       
   364         MPXDbCommonUtil::AppendValueL(aFields, aValues, KMCCategoryName, truncatedName);
       
   365         }
       
   366 //support Winlogo use case
       
   367    if (aMedia.IsSupported(KMPXMediaMusicAlbumArtist))
       
   368         {
       
   369         TPtrC truncatedAlbumArtist(aMedia.ValueText(KMPXMediaMusicAlbumArtist).Left(KMCMaxTextLen));
       
   370         MPXDbCommonUtil::AppendValueL(aFields, aValues, KMCMusicAlbumArtist, truncatedAlbumArtist);
       
   371         }
       
   372    if (aMedia.IsSupported(KMPXMediaMusicGenre))
       
   373         {
       
   374         TPtrC truncatedGenre(aMedia.ValueText(KMPXMediaMusicGenre).Left(KMCMaxTextLen));
       
   375         MPXDbCommonUtil::AppendValueL(aFields, aValues, KMCMusicGenre, truncatedGenre);    
       
   376         }
       
   377     }
       
   378 
       
   379 
       
   380 // ----------------------------------------------------------------------------
       
   381 // CMPXDbAlbum::CreateTableL
       
   382 // ----------------------------------------------------------------------------
       
   383 //
       
   384 void CMPXDbAbstractAlbum::CreateTableL(
       
   385     RSqlDatabase& aDatabase,
       
   386     TBool /* aCorruptTable */)
       
   387     {
       
   388     MPX_FUNC("CMPXDbAbstractAlbum::CreateTableL");
       
   389 
       
   390     // create the table
       
   391     HBufC* query = PreProcessStringLC(KAbstractAlbumCreateTable);
       
   392     User::LeaveIfError(aDatabase.Exec(*query));
       
   393     CleanupStack::PopAndDestroy(query);
       
   394 
       
   395     // do not create an index on the Name field
       
   396     // as it only slows down the insert/update queries overall
       
   397     }
       
   398 
       
   399 // ----------------------------------------------------------------------------
       
   400 // CMPXDbAlbum::CheckTableL
       
   401 // ----------------------------------------------------------------------------
       
   402 //
       
   403 TBool CMPXDbAbstractAlbum::CheckTableL(
       
   404     RSqlDatabase& aDatabase)
       
   405     {
       
   406     MPX_FUNC("CMPXDbAbstractAlbum::CheckTableL");
       
   407 
       
   408     HBufC* query = PreProcessStringLC(KAbstractAlbumCheckTable);
       
   409     TBool check(DoCheckTable(aDatabase, *query));
       
   410     CleanupStack::PopAndDestroy(query);
       
   411 
       
   412     return check;
       
   413     }
       
   414 
       
   415 
       
   416 // ----------------------------------------------------------------------------
       
   417 // Constructor
       
   418 // ----------------------------------------------------------------------------
       
   419 //
       
   420 CMPXDbAbstractAlbum::CMPXDbAbstractAlbum(
       
   421     CMPXDbManager& aDbManager,
       
   422     TMPXGeneralCategory aCategory) :
       
   423     CMPXDbCategory(aDbManager, aCategory)
       
   424     {
       
   425     MPX_FUNC("CMPXDbAbstractAlbum::CMPXDbAbstractAlbum");
       
   426     }
       
   427 
       
   428 // ----------------------------------------------------------------------------
       
   429 // Second phase constructor.
       
   430 // ----------------------------------------------------------------------------
       
   431 //
       
   432 void CMPXDbAbstractAlbum::ConstructL()
       
   433     {
       
   434     MPX_FUNC("CMPXDbAbstractAlbum::ConstructL");
       
   435 
       
   436     BaseConstructL();
       
   437 #ifdef RD_MPX_TNM_INTEGRATION
       
   438     // Create Thumbnail Manager instance. This object is the observer.
       
   439     iTNManager = CThumbnailManager::NewL( *this );
       
   440 #endif //RD_MPX_TNM_INTEGRATION
       
   441     }
       
   442 
       
   443 
       
   444 
       
   445 // ---------------------------------------------------------------------------
       
   446 // CMPXDbAbstractAlbum::ThumbnailReady 
       
   447 // Callback but not used here
       
   448 // ---------------------------------------------------------------------------
       
   449 void CMPXDbAbstractAlbum::ThumbnailPreviewReady( 
       
   450         MThumbnailData& /*aThumbnail*/, TThumbnailRequestId /*aId*/ )
       
   451     {
       
   452     }
       
   453         
       
   454 
       
   455 // ---------------------------------------------------------------------------
       
   456 // CMPXDbAbstractAlbum::ThumbnailReady
       
   457 // Callback but not used here
       
   458 // ---------------------------------------------------------------------------
       
   459 void CMPXDbAbstractAlbum::ThumbnailReady( TInt /*aError*/, 
       
   460         MThumbnailData& /*aThumbnail*/, TThumbnailRequestId /*aId*/ )
       
   461     {
       
   462     }
       
   463 // End of File