mpserviceplugins/mpxsqlitedbcommon/src/mpxdbcommonutil.cpp
branchRCL_3
changeset 52 14979e23cb5e
equal deleted inserted replaced
50:26a1709b9fec 52:14979e23cb5e
       
     1 /*
       
     2 * Copyright (c) 2006 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:  The class CMPXDbCommonUtil which contains utilities functions
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <bautils.h>
       
    21 #include <caf/data.h>
       
    22 #include <caf/virtualpathptr.h>
       
    23 #include <hash.h>
       
    24 #include <apgcli.h>
       
    25 #include <apmstd.h>
       
    26 #include <sqldb.h>
       
    27 
       
    28 #include <mpxmediageneraldefs.h>
       
    29 #include <mpxmediamusicdefs.h>
       
    30 #include <mpxmediaaudiodefs.h>
       
    31 #include <mpxmediadrmdefs.h>
       
    32 #include <mpxmediacollectiondetaildefs.h>
       
    33 #include <mpxmedia.h>
       
    34 #include <mpxmediaarray.h>
       
    35 #include <mpxlog.h>
       
    36 
       
    37 #include "mpxdbcommonstd.h"
       
    38 #include "mpxdbcommondef.h"
       
    39 #include "mpxdbcommonutil.h"
       
    40 
       
    41 // CONSTANTS
       
    42 _LIT(KPathStart, ":\\");
       
    43 
       
    44 // ============================ FILE EXTENSION TO MIME MAP ==============================
       
    45 
       
    46 // ----------------------------------------------------------------------------------------------------------
       
    47 // Statically allocated Mime Map
       
    48 // ----------------------------------------------------------------------------------------------------------
       
    49 //
       
    50 
       
    51 _LIT( KExtensionAac,    "aac" );
       
    52 _LIT( KExtensionAif,    "aif"); 
       
    53 _LIT( KExtensionAifc,   "aifc"); 
       
    54 _LIT( KExtensionAiff,   "aiff");
       
    55 _LIT( KExtensionAmr,    "amr" );
       
    56 _LIT( KExtensionAu,     "au" );
       
    57 _LIT( KExtensionAwb,    "awb" );
       
    58 _LIT( KExtensionMid,    "mid" );
       
    59 _LIT( KExtensionMidi,   "midi" );
       
    60 _LIT( KExtensionMka,    "mka" );
       
    61 _LIT( KExtensionMp3,    "mp3" );
       
    62 _LIT( KExtensionOgg,    "ogg");
       
    63 _LIT( KExtensionRa,     "ra"); 
       
    64 _LIT( KExtensionRam,    "ram"); 
       
    65 _LIT( KExtensionRmi,    "rmi"); 
       
    66 _LIT( KExtensionSnd,    "snd" );
       
    67 _LIT( KExtensionSpMid,  "spmid" );
       
    68 _LIT( KExtensionWav,    "wav" );
       
    69 _LIT( KExtensionWma,    "wma" );
       
    70 
       
    71 _LIT8( KMimeTypeAac,      "audio/aac" );
       
    72 _LIT8( KMimeTypeAiff,     "audio/x-aiff");
       
    73 _LIT8( KMimeTypeAmr,      "audio/amr" );
       
    74 _LIT8( KMimeTypeAu,       "audio/au" );
       
    75 _LIT8( KMimeTypeAwb,      "audio/amr-wb" );
       
    76 _LIT8( KMimeTypeBasic,    "audio/basic");
       
    77 _LIT8( KMimeTypeMatroska, "audio/x-matroska");
       
    78 _LIT8( KMimeTypeMid,      "audio/mid");
       
    79 _LIT8( KMimeTypeMidi,     "audio/midi" );
       
    80 _LIT8( KMimeTypeMpeg,     "audio/mpeg" );
       
    81 _LIT8( KMimeTypeOgg,      "audio/ogg");
       
    82 _LIT8( KMimeTypeReal,     "audio/x-pn-realaudio");
       
    83 _LIT8( KMimeTypeSpMidi,   "audio/sp-midi" );
       
    84 _LIT8( KMimeTypeWav,      "audio/wav" ); 
       
    85 _LIT8( KMimeTypeWma,      "audio/x-ms-wma");
       
    86 
       
    87 struct TMimeMapItem {
       
    88 	const TDesC  * iExt;
       
    89 	const TDesC8 * iType;
       
    90 };
       
    91 
       
    92 // We need to explicitly cast here as LitC::operator& requires writable DLL data (even though it is just a cast)
       
    93 #define MIME_MAP_ITEM(ext,type) { &REINTERPRET_CAST(const TDesC&, ext), &REINTERPRET_CAST(const TDesC8&, type) }
       
    94 
       
    95 // THIS ARRAY MUST BE SORTED BY EXTENSION
       
    96 static const TMimeMapItem KMimeMap [] = {
       
    97     MIME_MAP_ITEM( KExtensionAac,   KMimeTypeAac),
       
    98     MIME_MAP_ITEM( KExtensionAif,   KMimeTypeAiff ),
       
    99     MIME_MAP_ITEM( KExtensionAifc,  KMimeTypeAiff ),
       
   100     MIME_MAP_ITEM( KExtensionAiff,  KMimeTypeAiff ),
       
   101     MIME_MAP_ITEM( KExtensionAmr,   KMimeTypeAmr ),
       
   102     MIME_MAP_ITEM( KExtensionAu,    KMimeTypeAu ), // KMimeTypeAudioBasic?  "audio/x-au"?
       
   103     MIME_MAP_ITEM( KExtensionAwb,   KMimeTypeAwb ),
       
   104     MIME_MAP_ITEM( KExtensionMid,   KMimeTypeMidi ),
       
   105     MIME_MAP_ITEM( KExtensionMidi,  KMimeTypeMidi ),
       
   106     MIME_MAP_ITEM( KExtensionMka,   KMimeTypeMatroska ),
       
   107     MIME_MAP_ITEM( KExtensionMp3,   KMimeTypeMpeg ),
       
   108     MIME_MAP_ITEM( KExtensionOgg,   KMimeTypeOgg ),
       
   109     MIME_MAP_ITEM( KExtensionRa,    KMimeTypeReal ),
       
   110     MIME_MAP_ITEM( KExtensionRam,   KMimeTypeReal ),
       
   111     MIME_MAP_ITEM( KExtensionRmi,   KMimeTypeMid ),
       
   112     MIME_MAP_ITEM( KExtensionSnd,   KMimeTypeBasic ),
       
   113     MIME_MAP_ITEM( KExtensionSpMid, KMimeTypeSpMidi ),
       
   114     MIME_MAP_ITEM( KExtensionWav,   KMimeTypeWav ), // "audio/x-wav"?
       
   115     MIME_MAP_ITEM( KExtensionWma,   KMimeTypeWma )
       
   116 };
       
   117 
       
   118 // ----------------------------------------------------------------------------------------------------------
       
   119 // Look for Mime Type from map by file extension
       
   120 // Returns NULL if file extension is not known
       
   121 // ----------------------------------------------------------------------------------------------------------
       
   122 //
       
   123 static const TDesC8 * FindMimeTypeFromMap ( const TDesC& aFilename )
       
   124     {
       
   125     // extract extension
       
   126    	TPtrC extension;
       
   127     TInt pos = aFilename.LocateReverseF( '.' );
       
   128     if ( pos < 0  || ++pos >= aFilename.Length() )
       
   129         {
       
   130         return NULL;
       
   131         }
       
   132     extension.Set( aFilename.Mid( pos ) );
       
   133 
       
   134     // binary search from Mime Map
       
   135     TUint begin = 0;
       
   136     TUint end = sizeof KMimeMap / sizeof (TMimeMapItem);
       
   137     while (begin < end)
       
   138         {
       
   139         TUint at = (begin + end) / 2;
       
   140         const TMimeMapItem & item = KMimeMap[at];
       
   141         TInt r = item.iExt->CompareF(extension);
       
   142         if (r == 0)
       
   143             {
       
   144             return item.iType;
       
   145             }
       
   146         else if (r > 0)
       
   147             {
       
   148             end = at;
       
   149             }
       
   150         else
       
   151             {
       
   152             begin = at + 1;
       
   153             }
       
   154         }
       
   155     return NULL;
       
   156 }
       
   157 
       
   158 // ============================ MEMBER FUNCTIONS ==============================
       
   159 
       
   160 // ----------------------------------------------------------------------------------------------------------
       
   161 // Set HBufC data member.
       
   162 // ----------------------------------------------------------------------------------------------------------
       
   163 //
       
   164 EXPORT_C TInt MPXDbCommonUtil::SetHBuf(
       
   165     HBufC*& aBuf,
       
   166     const TDesC* aSource,
       
   167     TInt aMaxLen /*= -1*/)
       
   168     {
       
   169     TInt ret( KErrNone );
       
   170     delete aBuf;
       
   171     aBuf = NULL;
       
   172 
       
   173     if (aSource)
       
   174         {
       
   175         if (aMaxLen != -1 && aSource->Length() > aMaxLen)
       
   176             {
       
   177             aBuf = aSource->Left(aMaxLen).Alloc();
       
   178             ret = KErrOverflow;
       
   179             }
       
   180         else
       
   181             {
       
   182             aBuf = aSource->Alloc();
       
   183             }
       
   184         }
       
   185     else
       
   186         {
       
   187         aBuf = HBufC::New(0);
       
   188         }
       
   189 
       
   190     if (!aBuf)
       
   191         {
       
   192         ret = KErrNoMemory;
       
   193         }
       
   194 
       
   195     return ret;
       
   196     }
       
   197 
       
   198 // ----------------------------------------------------------------------------------------------------------
       
   199 // Replace single quotes `'` with two single quotes `''` as text queries are
       
   200 // delimited by single quotes
       
   201 // also replace 0x00 and '\t' as they will cause crash in SQLite and UI respectively
       
   202 // ----------------------------------------------------------------------------------------------------------
       
   203 //
       
   204 EXPORT_C void MPXDbCommonUtil::FindAndReplaceSingleQuote(const TDesC& aSrc, TDes& aTrg)
       
   205     {
       
   206     TText ch;
       
   207     TInt srcLen(aSrc.Length());
       
   208 
       
   209     for (TInt i = 0; i < srcLen; ++i)
       
   210         {
       
   211         ch = aSrc[i];
       
   212         if((ch == 0) || (ch == TText('\t')))
       
   213             {
       
   214             aTrg.Append(TText(' '));
       
   215             }
       
   216         else
       
   217             {
       
   218             aTrg.Append(ch);
       
   219             if (ch == TText('\''))
       
   220                 {
       
   221                 aTrg.Append(ch);
       
   222                 }
       
   223             }
       
   224         }
       
   225     }
       
   226 
       
   227 // ----------------------------------------------------------------------------------------------------------
       
   228 // Delete file from file system
       
   229 // ----------------------------------------------------------------------------------------------------------
       
   230 //
       
   231 EXPORT_C TInt MPXDbCommonUtil::DeleteFile(
       
   232     RFs& aFs,
       
   233     const TDesC& aFile)
       
   234     {
       
   235     TInt ret(KErrNone);
       
   236     if(BaflUtils::FileExists(aFs, aFile))
       
   237         {
       
   238          ret = aFs.Delete(aFile);
       
   239 
       
   240         if (ret == KErrAccessDenied)
       
   241             {
       
   242             aFs.SetAtt(aFile,KEntryAttNormal,KEntryAttReadOnly);
       
   243             ret = aFs.Delete(aFile);
       
   244             }
       
   245         }
       
   246     return ret;
       
   247     }
       
   248 
       
   249 // ----------------------------------------------------------------------------------------------------------
       
   250 // Get the drive Id with a given volume unique Id
       
   251 // ----------------------------------------------------------------------------------------------------------
       
   252 //
       
   253 EXPORT_C TInt MPXDbCommonUtil::GetDriveIdMatchVolIdL(
       
   254     RFs& aFs,
       
   255     TUint aVolumeId)
       
   256     {
       
   257     TDriveList driveList;
       
   258     TBool found(EFalse);
       
   259     TInt driveNum(EDriveA);
       
   260 
       
   261     User::LeaveIfError(aFs.DriveList(driveList));
       
   262     for (; driveNum <= EDriveZ; ++driveNum)
       
   263         {
       
   264         if (driveList[driveNum])
       
   265             {
       
   266             TVolumeInfo volInfo;
       
   267 
       
   268             if ((aFs.Volume(volInfo, driveNum) == KErrNone) &&
       
   269                 (volInfo.iUniqueID == aVolumeId))
       
   270                 {
       
   271                 found = ETrue;
       
   272                 break;
       
   273                 }
       
   274             }
       
   275         }
       
   276 
       
   277     if (!found)
       
   278         {
       
   279         driveNum = KErrNotFound;
       
   280         }
       
   281 
       
   282     return driveNum;
       
   283     }
       
   284 
       
   285 // ----------------------------------------------------------------------------------------------------------
       
   286 // Get the volume Id with a given drive Id
       
   287 // ----------------------------------------------------------------------------------------------------------
       
   288 //
       
   289 EXPORT_C TUint MPXDbCommonUtil::GetVolIdMatchDriveIdL(
       
   290     RFs& aFs,
       
   291     TInt aDriveId)
       
   292     {
       
   293     TUint volId (0);
       
   294     TVolumeInfo volInfo;
       
   295     TInt err = aFs.Volume(volInfo, aDriveId);
       
   296     if (err == KErrNone)
       
   297         {
       
   298         volId = volInfo.iUniqueID;
       
   299         }
       
   300     else if (err == KErrNotReady)
       
   301         {
       
   302         volId = 0;
       
   303         }
       
   304     else
       
   305         {
       
   306         User::Leave(err);
       
   307         }
       
   308 
       
   309     return volId;
       
   310     }
       
   311 
       
   312 // ----------------------------------------------------------------------------
       
   313 // Generate a 32bits Unique Id with MD5
       
   314 // ----------------------------------------------------------------------------
       
   315 //
       
   316 EXPORT_C TUint32 MPXDbCommonUtil::GenerateUniqueIdL(
       
   317     RFs& aFs,
       
   318     TMPXGeneralCategory aTableId,
       
   319     const TDesC& aName,
       
   320     TBool aCaseSensitive)
       
   321     {
       
   322     MPX_FUNC("MPXDbCommonUtil::GenerateUniqueIdL");
       
   323 
       
   324     TInt extractPos(0);
       
   325 
       
   326     if( aName.Find(KPathStart) == KMCPathStartWithColon )
       
   327         {
       
   328         // aName is a filename
       
   329         extractPos = KMCPathStartPos;     // c:\..., include first '\' of the path
       
   330         }
       
   331 
       
   332     HBufC* fileNameBuf = HBufC::NewLC(aName.Length()+KMCIntegerLen);
       
   333     TPtr fileName(fileNameBuf->Des());
       
   334     if( extractPos )
       
   335         {
       
   336         // append volume's unique Id to path to maintain uniqueness
       
   337         TDriveUnit driveUnit( aName );
       
   338         TUint volId = MPXDbCommonUtil::GetVolIdMatchDriveIdL(aFs, driveUnit);
       
   339         if( volId )
       
   340             {
       
   341             fileName.AppendNum( volId );
       
   342             }
       
   343         else
       
   344             {
       
   345             // If media/drive doesn't exist, using whole path
       
   346             extractPos = 0;
       
   347             }
       
   348         }
       
   349 
       
   350     // append the path part
       
   351     fileName.Append( aName.Mid( extractPos ) );
       
   352 
       
   353     if( !aCaseSensitive )
       
   354         {
       
   355         fileName.LowerCase();
       
   356         }
       
   357 
       
   358     TInt narrowFileLen(0);
       
   359     TPtrC8 narrowFileName;
       
   360     #if defined(_UNICODE)
       
   361         narrowFileLen = fileNameBuf->Des().Length() * 2;
       
   362         narrowFileName.Set((TUint8*)fileName.Ptr(), narrowFileLen);
       
   363     #else
       
   364         narrowFileLen = fileName.Length();
       
   365         narrowFileName.Set(fileName.Ptr(), narrowFileLen);
       
   366     #endif
       
   367 
       
   368     CMD5* hasher = CMD5::NewL();
       
   369     CleanupStack::PushL( hasher );
       
   370     hasher->Reset();
       
   371     TBuf8<16> hash( hasher->Hash( narrowFileName ) );   //hashed to 128bits
       
   372     CleanupStack::PopAndDestroy( hasher );
       
   373 
       
   374     const TText8* ptr = hash.Ptr();
       
   375 
       
   376     TUint32 uniqueId(0);
       
   377     uniqueId = aTableId << 28;
       
   378     uniqueId |= (ptr[3]&0x0F)<<24 | (ptr[2]<<16) | (ptr[1]<<8) | ptr[0];
       
   379     
       
   380     CleanupStack::PopAndDestroy(fileNameBuf);
       
   381     
       
   382     return uniqueId;
       
   383     }
       
   384 
       
   385 // ---------------------------------------------------------------------------
       
   386 // Append an item into the media array
       
   387 // ---------------------------------------------------------------------------
       
   388 //
       
   389 EXPORT_C void MPXDbCommonUtil::AppendMediaL(
       
   390     CMPXMediaArray& aArray,
       
   391     const TDesC& aTitle,
       
   392     TMPXGeneralType aType,
       
   393     TMPXGeneralCategory aCat,
       
   394     TMPXItemId aId,
       
   395     TInt aNonPermissibleActions,
       
   396     TUint aDbflag)
       
   397     {
       
   398     MPX_FUNC("MPXDbCommonUtil::AppendMediaL");
       
   399 
       
   400     CMPXMedia* entry = ConstructMediaLC(aTitle, aType, aCat, aId, aNonPermissibleActions, aDbflag);
       
   401     aArray.AppendL(*entry);
       
   402     CleanupStack::PopAndDestroy(entry);
       
   403     }
       
   404 
       
   405 // ----------------------------------------------------------------------------
       
   406 // Append an item into the media array
       
   407 // ----------------------------------------------------------------------------
       
   408 //
       
   409 EXPORT_C void MPXDbCommonUtil::PrependMediaL(
       
   410     CMPXMediaArray& aArray,
       
   411     const TDesC& aTitle,
       
   412     TMPXGeneralType aType,
       
   413     TMPXGeneralCategory aCat,
       
   414     TMPXItemId aId /* = 0 */,
       
   415     TInt aNonPermissibleActions /* = 0 */,
       
   416     TUint aDbflag /* = 0 */,
       
   417     TInt aPos /* = 0 */)
       
   418     {
       
   419     MPX_FUNC("MPXDbCommonUtil::PrependMediaL");
       
   420 
       
   421     CMPXMedia* entry = ConstructMediaLC(aTitle, aType, aCat, aId, aNonPermissibleActions, aDbflag);
       
   422     User::LeaveIfError(aArray.Insert(*entry, aPos));
       
   423     CleanupStack::PopAndDestroy(entry);
       
   424     }
       
   425 
       
   426 // ----------------------------------------------------------------------------
       
   427 // Append an item into the media array
       
   428 // ----------------------------------------------------------------------------
       
   429 //
       
   430 EXPORT_C void MPXDbCommonUtil::FillInSupportedUIDsL(
       
   431     const TArray<TMPXAttribute>& aAttrs,
       
   432     RArray<TInt>& aSupportedIds)
       
   433     {
       
   434     CleanupClosePushL(aSupportedIds);
       
   435     MPX_FUNC("MPXDbCommonUtil::FillInSupportedUIDs");
       
   436 
       
   437     TInt attrCount(aAttrs.Count());
       
   438     for (TInt i = 0; i < attrCount; ++i)
       
   439         {
       
   440         if (aAttrs[i].ContentId() == KMPXMediaIdGeneral)
       
   441             {
       
   442             aSupportedIds.AppendL(KMPXMediaIdGeneral);
       
   443             }
       
   444         else if (aAttrs[i].ContentId() == KMPXMediaIdMusic)
       
   445             {
       
   446             aSupportedIds.AppendL(KMPXMediaIdMusic);
       
   447             }
       
   448         else if (aAttrs[i].ContentId() == KMPXMediaIdAudio)
       
   449             {
       
   450             aSupportedIds.AppendL(KMPXMediaIdAudio);
       
   451             }
       
   452         else if (aAttrs[i].ContentId() == KMPXMediaIdDrm)
       
   453             {
       
   454             aSupportedIds.AppendL(KMPXMediaIdDrm);
       
   455             }
       
   456         else if (aAttrs[i].ContentId() == KMPXMediaIdCollectionDetails)
       
   457             {
       
   458             aSupportedIds.AppendL(KMPXMediaIdCollectionDetails);
       
   459             }
       
   460         else
       
   461             {
       
   462             // ignore attribute
       
   463             }
       
   464         }
       
   465     CleanupStack::Pop();
       
   466     }
       
   467 
       
   468 // ----------------------------------------------------------------------------
       
   469 // Fill in change event message with the given info
       
   470 // ----------------------------------------------------------------------------
       
   471 //
       
   472 EXPORT_C void MPXDbCommonUtil::FillItemChangedMessageL(
       
   473     CMPXMessage& aMessage,
       
   474     TMPXItemId aId,
       
   475     TMPXChangeEventType aChangeType,
       
   476     TMPXGeneralCategory aCategory,
       
   477     TUint aUid,
       
   478     TMPXItemId aDeprecatedId)
       
   479     {
       
   480     MPX_FUNC("MPXDbCommonUtil::FillItemChangedMessageL");
       
   481     MPX_DEBUG5("MPXDbCommonUtil::FillItemChangedMessageL: type [%d], category [%d], id[0x%x], oldId[0x%x]",
       
   482                aChangeType, aCategory, aId.iId1, aDeprecatedId.iId1);
       
   483 
       
   484     aMessage.SetTObjectValueL<TMPXMessageId>(KMPXMessageGeneralId, KMPXMessageIdItemChanged);
       
   485     aMessage.SetTObjectValueL<TUid>(KMPXMessageCollectionId, TUid::Uid(aUid));
       
   486     aMessage.SetTObjectValueL<TMPXChangeEventType>(KMPXMessageChangeEventType, aChangeType);
       
   487     aMessage.SetTObjectValueL<TMPXGeneralCategory>(KMPXMessageMediaGeneralCategory, aCategory);
       
   488     aMessage.SetTObjectValueL<TMPXItemId>(KMPXMessageMediaGeneralId, aId);
       
   489 
       
   490     if ((aDeprecatedId != 0) && (aId != aDeprecatedId))
       
   491         {
       
   492         aMessage.SetTObjectValueL<TMPXItemId>(KMPXMessageMediaDeprecatedId, aDeprecatedId);
       
   493         }
       
   494     }
       
   495 
       
   496 // ----------------------------------------------------------------------------
       
   497 // Add an item changed message to the message array
       
   498 // ----------------------------------------------------------------------------
       
   499 //
       
   500 EXPORT_C void MPXDbCommonUtil::AddItemChangedMessageL(
       
   501     CMPXMessageArray& aMessageArray,
       
   502     TMPXItemId aId,
       
   503     TMPXChangeEventType aChangeType,
       
   504     TMPXGeneralCategory aCategory,
       
   505     TUint aUid,
       
   506     TMPXItemId aDeprecatedId /* = 0 */)
       
   507     {
       
   508     MPX_FUNC("MPXDbCommonUtil::AddItemChangedMessageL");
       
   509     CMPXMessage* message = CMPXMedia::NewL();
       
   510     CleanupStack::PushL(message);
       
   511 
       
   512     FillItemChangedMessageL(*message, aId, aChangeType, aCategory, aUid, aDeprecatedId);
       
   513     if (FindItemChangedMessageL(aMessageArray, *message) == KErrNotFound)
       
   514         {
       
   515         aMessageArray.AppendL(*message); // ownership xfer
       
   516         }
       
   517     CleanupStack::PopAndDestroy(message);
       
   518     }
       
   519 
       
   520 // ----------------------------------------------------------------------------
       
   521 // Find the message in the array, if not KErrNotFound is returned; otherwise the
       
   522 // index of the first matching message is returned
       
   523 // ----------------------------------------------------------------------------
       
   524 //
       
   525 EXPORT_C TInt MPXDbCommonUtil::FindItemChangedMessageL(
       
   526     const CMPXMessageArray& aMessageArray,
       
   527     const CMPXMessage& aMessage)
       
   528     {
       
   529     MPX_FUNC("MPXDbCommonUtil::FindItemChangedMessageL");
       
   530 
       
   531     TInt index(KErrNotFound);
       
   532     TInt messageCount(aMessageArray.Count());
       
   533 
       
   534     for (TInt i = 0; i < messageCount; ++i)
       
   535         {
       
   536         CMPXMessage* message = aMessageArray.AtL(i);
       
   537         
       
   538         if (message->IsSupported(KMPXMessageGeneralId) &&
       
   539             message->IsSupported(KMPXMessageCollectionId) &&
       
   540             message->IsSupported(KMPXMessageChangeEventType) &&
       
   541             message->IsSupported(KMPXMessageMediaGeneralCategory) &&
       
   542             message->IsSupported(KMPXMessageMediaGeneralId) &&
       
   543             aMessage.IsSupported(KMPXMessageGeneralId) &&
       
   544             aMessage.IsSupported(KMPXMessageCollectionId) &&
       
   545             aMessage.IsSupported(KMPXMessageChangeEventType) &&
       
   546             aMessage.IsSupported(KMPXMessageMediaGeneralCategory) &&
       
   547             aMessage.IsSupported(KMPXMessageMediaGeneralId))
       
   548             {
       
   549             if (message->ValueTObjectL<TMPXMessageId>(KMPXMessageGeneralId) == aMessage.ValueTObjectL<TMPXMessageId>(KMPXMessageGeneralId) &&
       
   550                 message->ValueTObjectL<TUid>(KMPXMessageCollectionId) == aMessage.ValueTObjectL<TUid>(KMPXMessageCollectionId) &&
       
   551                 message->ValueTObjectL<TMPXChangeEventType>(KMPXMessageChangeEventType) == aMessage.ValueTObjectL<TMPXChangeEventType>(KMPXMessageChangeEventType) &&
       
   552                 message->ValueTObjectL<TMPXGeneralCategory>(KMPXMessageMediaGeneralCategory) == aMessage.ValueTObjectL<TMPXGeneralCategory>(KMPXMessageMediaGeneralCategory) &&
       
   553                 message->ValueTObjectL<TMPXItemId>(KMPXMessageMediaGeneralId) == aMessage.ValueTObjectL<TMPXItemId>(KMPXMessageMediaGeneralId))
       
   554                 {
       
   555                 if (!message->IsSupported(KMPXMessageMediaDeprecatedId) &&
       
   556                     !aMessage.IsSupported(KMPXMessageMediaDeprecatedId))
       
   557                     {
       
   558                     index = i;
       
   559                     break;
       
   560                     }
       
   561                 else if (message->IsSupported(KMPXMessageMediaDeprecatedId) &&
       
   562                          aMessage.IsSupported(KMPXMessageMediaDeprecatedId))
       
   563                     {
       
   564                     if (message->ValueTObjectL<TMPXItemId>(KMPXMessageMediaDeprecatedId) ==
       
   565                         aMessage.ValueTObjectL<TMPXItemId>(KMPXMessageMediaDeprecatedId))
       
   566                         {
       
   567                         index = i;
       
   568                         break;
       
   569                        }
       
   570                     }
       
   571                 else
       
   572                     {
       
   573                     // else do nothing
       
   574                     }
       
   575                 }
       
   576             }
       
   577         }
       
   578 
       
   579     return index;
       
   580     }
       
   581 
       
   582 // ----------------------------------------------------------------------------
       
   583 // Get the DRM protection type of the file
       
   584 // ----------------------------------------------------------------------------
       
   585 //
       
   586 EXPORT_C TMCDrmType MPXDbCommonUtil::GetDRMTypeL(const TDesC& aFile)
       
   587     {
       
   588     MPX_FUNC("MPXDbCommonUtil::GetDRMTypeL");
       
   589 
       
   590     using namespace ContentAccess;
       
   591     TInt drmProtected(0);
       
   592     TVirtualPathPtr virtualPath(aFile, KDefaultContentObject);
       
   593     CData* content = CData::NewL(virtualPath, EPeek, EContentShareReadOnly);
       
   594     CleanupStack::PushL(content);
       
   595     User::LeaveIfError(content->GetAttribute(EIsProtected, drmProtected));
       
   596     CleanupStack::PopAndDestroy( content );
       
   597     return drmProtected? EMCDrmOmaDrm : EMCDrmNone;
       
   598     }
       
   599 
       
   600 // ----------------------------------------------------------------------------
       
   601 // MPXDbUtil::ProcessSingleQuotesLC
       
   602 // ----------------------------------------------------------------------------
       
   603 //
       
   604 EXPORT_C HBufC* MPXDbCommonUtil::ProcessSingleQuotesLC(
       
   605     const TDesC& aString)
       
   606     {
       
   607     MPX_FUNC("MPXDbCommonUtil::ProcessSingleQuotesLC");
       
   608 
       
   609     // reserve space for all single quotes (double the size)
       
   610     // coverity[incorrect_multiplication]
       
   611     // coverity[buffer_alloc]
       
   612     HBufC* value = HBufC::NewLC(aString.Length() * 2);
       
   613     TPtr valuePtr(value->Des());
       
   614 
       
   615     MPXDbCommonUtil::FindAndReplaceSingleQuote(aString, valuePtr);
       
   616 
       
   617     return value;
       
   618     }
       
   619 
       
   620 // ----------------------------------------------------------------------------
       
   621 // MPXDbUtil::ProcessPatternCharsLC
       
   622 // 1) a percentage needs to be escaped for sqlite followed by
       
   623 //    another percentage sign for escaping % for descriptor formatting, i.e.
       
   624 //    % --> %% (this percentage will be treated as a percentage instead of
       
   625 //    pattern matching)
       
   626 // 2) since back slash is used for escaping the pattern characters, user
       
   627 //    specified back slash should be escapped as well, i.e. \ --> \\
       
   628 // ----------------------------------------------------------------------------
       
   629 //
       
   630 EXPORT_C HBufC* MPXDbCommonUtil::ProcessPatternCharsLC(
       
   631     const TDesC& aString)
       
   632     {
       
   633     MPX_FUNC("MPXDbCommonUtil::ProcessPatternCharsLC");
       
   634 
       
   635     // reserve space for all percentage signs (triple the size because % should
       
   636     // be replaced by %%)
       
   637     TInt srcLen(aString.Length());
       
   638     // coverity[incorrect_multiplication]
       
   639     // coverity[buffer_alloc]
       
   640     HBufC* targetString = HBufC::NewLC(aString.Length() * 3);
       
   641     TPtr targetStringPtr(targetString->Des());
       
   642 
       
   643     TPtrC ch;
       
   644     for (TInt i = 0; i < srcLen; ++i)
       
   645         {
       
   646         ch.Set(&aString[i], 1);
       
   647         if (ch.CompareF(KMCPercentage) == 0)
       
   648             {
       
   649             targetStringPtr.Append(KMCPercentage);
       
   650             }
       
   651            else if ( ch.CompareF( KMCBackSlash ) == 0 )
       
   652             {
       
   653             targetStringPtr.Append(KMCBackSlash);
       
   654             }
       
   655         targetStringPtr.Append(ch);            
       
   656         }
       
   657 
       
   658     MPX_DEBUG3("    original=%S, new=%S", &aString, targetString);
       
   659     
       
   660     return targetString;
       
   661     }
       
   662 
       
   663 // ----------------------------------------------------------------------------
       
   664 // Constructs an int SQL criterion
       
   665 // ----------------------------------------------------------------------------
       
   666 //
       
   667 EXPORT_C HBufC* MPXDbCommonUtil::SqlCriterionLC(
       
   668     const TDesC& aCriterion,
       
   669     TInt aValue)
       
   670     {
       
   671     MPX_FUNC("MPXDbCommonUtil::SqlCriterionLC");
       
   672 
       
   673     HBufC* sqlCriterion = HBufC::NewLC(aCriterion.Length() + KMCIntegerLen);
       
   674     TPtr ptr = sqlCriterion->Des();
       
   675     ptr.Format(aCriterion, aValue);
       
   676     return sqlCriterion;
       
   677     }
       
   678 
       
   679 // ----------------------------------------------------------------------------
       
   680 // Constructs an int64 SQL criterion
       
   681 // ----------------------------------------------------------------------------
       
   682 //
       
   683 EXPORT_C HBufC* MPXDbCommonUtil::SqlCriterion64LC(
       
   684     const TDesC& aCriterion,
       
   685     TInt64 aValue)
       
   686     {
       
   687     MPX_FUNC("MPXDbCommonUtil::SqlCriterion64LC");
       
   688 
       
   689     HBufC* sqlCriterion = HBufC::NewLC(aCriterion.Length() + KMCInt64Len);
       
   690     TPtr ptr = sqlCriterion->Des();
       
   691     ptr.Format(aCriterion, aValue);
       
   692     return sqlCriterion;
       
   693     }
       
   694 
       
   695 // ----------------------------------------------------------------------------
       
   696 // Constructs an SQL criterion using two int values
       
   697 // ----------------------------------------------------------------------------
       
   698 //
       
   699 EXPORT_C HBufC* MPXDbCommonUtil::SqlCriterionLC(
       
   700     const TDesC& aCriterion,
       
   701     TInt aValue1,
       
   702     TInt aValue2)
       
   703     {
       
   704     MPX_FUNC("MPXDbCommonUtil::SqlCriterionLC 2");
       
   705 
       
   706     HBufC* sqlCriterion = HBufC::NewLC(aCriterion.Length() + 2 * KMCIntegerLen);
       
   707     TPtr ptr = sqlCriterion->Des();
       
   708     ptr.Format(aCriterion, aValue1, aValue2);
       
   709     return sqlCriterion;
       
   710     }
       
   711 
       
   712 // ----------------------------------------------------------------------------
       
   713 // Constructs an SQL criterion using two int64 values
       
   714 // ----------------------------------------------------------------------------
       
   715 //
       
   716 EXPORT_C HBufC* MPXDbCommonUtil::SqlCriterion64LC(
       
   717     const TDesC& aCriterion,
       
   718     TInt64 aValue1,
       
   719     TInt64 aValue2)
       
   720     {
       
   721     MPX_FUNC("MPXDbCommonUtil::SqlCriterion64LC 2");
       
   722 
       
   723     HBufC* sqlCriterion = HBufC::NewLC(aCriterion.Length() + 2 * KMCInt64Len);
       
   724     TPtr ptr = sqlCriterion->Des();
       
   725     ptr.Format(aCriterion, aValue1, aValue2);
       
   726     return sqlCriterion;
       
   727     }
       
   728 
       
   729 // ----------------------------------------------------------------------------
       
   730 // Constructs a descriptor SQL criterion
       
   731 // ----------------------------------------------------------------------------
       
   732 //
       
   733 EXPORT_C HBufC* MPXDbCommonUtil::SqlCriterionLC(
       
   734     const TDesC& aCriterion,
       
   735     const TDesC& aValue)
       
   736     {
       
   737     MPX_FUNC("MPXDbCommonUtil::SqlCriterionLC 3");
       
   738 
       
   739     HBufC* value = ProcessSingleQuotesLC(aValue);
       
   740 
       
   741     HBufC* sqlCriterion = HBufC::NewL(aCriterion.Length() + value->Length());
       
   742     TPtr ptr(sqlCriterion->Des());
       
   743     ptr.Format(aCriterion, value);
       
   744 
       
   745     CleanupStack::PopAndDestroy(value);
       
   746     CleanupStack::PushL(sqlCriterion);
       
   747     return sqlCriterion;
       
   748     }
       
   749     
       
   750 // ----------------------------------------------------------------------------
       
   751 // Constructs a descriptor SQL criterion
       
   752 // ----------------------------------------------------------------------------
       
   753 //
       
   754 EXPORT_C HBufC* MPXDbCommonUtil::SqlCriterionLC(
       
   755     const TDesC& aCriterion,
       
   756     const TDesC& aValue1,
       
   757     const TDesC& aValue2)
       
   758     {
       
   759     MPX_FUNC("MPXDbCommonUtil::SqlCriterionLC 3");
       
   760 
       
   761     HBufC* value1 = ProcessSingleQuotesLC(aValue1);
       
   762     HBufC* value2 = ProcessSingleQuotesLC(aValue2);
       
   763 
       
   764     HBufC* sqlCriterion = HBufC::NewL(aCriterion.Length() + 
       
   765                                       value1->Length() + 
       
   766                                       value2->Length());
       
   767     TPtr ptr(sqlCriterion->Des());
       
   768     ptr.Format(aCriterion, value1, value2);
       
   769 
       
   770     CleanupStack::PopAndDestroy(value2);
       
   771     CleanupStack::PopAndDestroy(value1);
       
   772     CleanupStack::PushL(sqlCriterion);
       
   773     return sqlCriterion;
       
   774     }
       
   775     
       
   776 // ----------------------------------------------------------------------------
       
   777 // Constructs a descriptor SQL criterion
       
   778 // ----------------------------------------------------------------------------
       
   779 //
       
   780 EXPORT_C HBufC* MPXDbCommonUtil::SqlCriterionLC(
       
   781     const TDesC& aCriterion,
       
   782     const TDesC& aValue1,
       
   783     TInt aValue2,
       
   784     const TDesC& aValue3,
       
   785     TInt aValue4)
       
   786     {
       
   787     MPX_FUNC("MPXDbCommonUtil::SqlCriterionLC 3");
       
   788 
       
   789     HBufC* value1 = ProcessSingleQuotesLC(aValue1);
       
   790     HBufC* value3 = ProcessSingleQuotesLC(aValue3);
       
   791 
       
   792     HBufC* sqlCriterion = HBufC::NewL(aCriterion.Length() + 
       
   793                                       value1->Length() + 
       
   794                                       value3->Length() + 
       
   795                                       2 * KMCIntegerLen);
       
   796     TPtr ptr(sqlCriterion->Des());
       
   797     ptr.Format(aCriterion, value1, aValue2, value3, aValue4);
       
   798 
       
   799     CleanupStack::PopAndDestroy(value3);
       
   800     CleanupStack::PopAndDestroy(value1);
       
   801     CleanupStack::PushL(sqlCriterion);
       
   802     return sqlCriterion;
       
   803     }    
       
   804 
       
   805 // ----------------------------------------------------------------------------
       
   806 // Constructs and adds an int SQL criterion to the criteria array
       
   807 // ----------------------------------------------------------------------------
       
   808 //
       
   809 EXPORT_C TInt MPXDbCommonUtil::AddSqlCriterionL(
       
   810     CDesCArray& aSqlCriteria,
       
   811     const TDesC& aCriterion,
       
   812     TInt aValue)
       
   813     {
       
   814     MPX_FUNC("MPXDbCommonUtil::AddSqlCriterionL");
       
   815 
       
   816     HBufC* sqlCriterion = SqlCriterionLC(aCriterion, aValue);
       
   817     TInt length = sqlCriterion->Length();
       
   818     aSqlCriteria.AppendL(*sqlCriterion);
       
   819     CleanupStack::PopAndDestroy(sqlCriterion);
       
   820     return length;
       
   821     }
       
   822 
       
   823 // ----------------------------------------------------------------------------
       
   824 // Constructs and adds a string SQL criterion to the criteria array
       
   825 // ----------------------------------------------------------------------------
       
   826 //
       
   827 EXPORT_C TInt MPXDbCommonUtil::AddSqlCriterionL(
       
   828     CDesCArray& aSqlCriteria,
       
   829     const TDesC& aCriterion,
       
   830     const TDesC& aValue)
       
   831     {
       
   832     MPX_FUNC("MPXDbCommonUtil::AddSqlCriterionL");
       
   833 
       
   834     HBufC* sqlCriterion = SqlCriterionLC(aCriterion, aValue);
       
   835     TInt length = sqlCriterion->Length();
       
   836     aSqlCriteria.AppendL(*sqlCriterion);
       
   837     CleanupStack::PopAndDestroy(sqlCriterion);
       
   838     return length;
       
   839     }
       
   840 
       
   841 
       
   842 // ----------------------------------------------------------------------------
       
   843 // Create a full path with input drive Id and path
       
   844 // ----------------------------------------------------------------------------
       
   845 //
       
   846 EXPORT_C HBufC* MPXDbCommonUtil::CreateFullPathL(
       
   847     TInt aDriveId,
       
   848     const TDesC& aPath)
       
   849     {
       
   850     MPX_FUNC("MPXDbCommonUtil::CreateFullPathL");
       
   851 
       
   852     HBufC* hbuf = HBufC::NewLC(aPath.Length() + KMaxDriveName);
       
   853     TPtr fullPath(hbuf->Des());
       
   854 
       
   855     if (aDriveId != KErrNotFound)
       
   856         {
       
   857         TDriveUnit driveUnit(aDriveId);
       
   858         fullPath.Copy(driveUnit.Name());
       
   859         }
       
   860 
       
   861     fullPath.Append(aPath);
       
   862     CleanupStack::Pop(hbuf);
       
   863     return hbuf;
       
   864     }
       
   865 
       
   866 // ----------------------------------------------------------------------------
       
   867 // MPXDbCommonUtil::AppendValueL
       
   868 // ----------------------------------------------------------------------------
       
   869 //
       
   870 EXPORT_C void MPXDbCommonUtil::AppendValueL(
       
   871     CDesCArray& aFields,
       
   872     CDesCArray& aValues,
       
   873     const TDesC& aField,
       
   874     const TDesC& aValue)
       
   875     {
       
   876     MPX_FUNC("MPXDbCommonUtil::AppendValueL");
       
   877 
       
   878     aFields.AppendL(aField);
       
   879 
       
   880     HBufC* value = HBufC::NewLC(aValue.Length() * 2 + 2);
       
   881     TPtr valuePtr(value->Des());
       
   882 
       
   883     MPXDbCommonUtil::FindAndReplaceSingleQuote(aValue, valuePtr);
       
   884 
       
   885     // use 'value' instead of value, 0 length strings should be ''
       
   886     //
       
   887     if( valuePtr.Length() )
       
   888         {
       
   889         valuePtr.Insert(0, KMCSingleQuote);
       
   890         valuePtr.Append(KMCSingleQuote);
       
   891         }
       
   892     else
       
   893         {
       
   894         valuePtr.Append(KMCSingleQuote);
       
   895         valuePtr.Append(KMCSingleQuote);
       
   896         }
       
   897     aValues.AppendL(valuePtr);
       
   898 
       
   899     CleanupStack::PopAndDestroy(value);
       
   900     }
       
   901 
       
   902 // ----------------------------------------------------------------------------
       
   903 // MPXDbCommonUtil::AppendValueL
       
   904 // ----------------------------------------------------------------------------
       
   905 //
       
   906 EXPORT_C void MPXDbCommonUtil::AppendValueL(
       
   907     CDesCArray& aFields,
       
   908     CDesCArray& aValues,
       
   909     const TDesC& aField,
       
   910     TUint32 aValue)
       
   911     {
       
   912     MPX_FUNC("MPXDbCommonUtil::AppendValueL");
       
   913 
       
   914     aFields.AppendL(aField);
       
   915     TBuf<KMCIntegerLen> value;
       
   916     value.AppendNum(static_cast<TInt64>(aValue));
       
   917     aValues.AppendL(value);
       
   918     }
       
   919 
       
   920 // ----------------------------------------------------------------------------
       
   921 // Gets the MIME type for a specified URI
       
   922 // ----------------------------------------------------------------------------
       
   923 //
       
   924 EXPORT_C TDataType MPXDbCommonUtil::GetMimeTypeForUriL(
       
   925     const TDesC& aUri)
       
   926     {
       
   927     MPX_FUNC("MPXDbUtil::GetMimeTypeForUriL");
       
   928 
       
   929     if ( const TDesC8 * type = FindMimeTypeFromMap (aUri) )
       
   930         {
       
   931         return TDataType(*type);
       
   932         }
       
   933     
       
   934     RApaLsSession appArc;
       
   935     User::LeaveIfError(appArc.Connect());
       
   936     CleanupClosePushL(appArc);
       
   937     TUid dummyUid(KNullUid);
       
   938     TDataType mimeType;
       
   939     appArc.AppForDocument(aUri, dummyUid, mimeType);
       
   940     CleanupStack::PopAndDestroy(&appArc);
       
   941 
       
   942     return mimeType;
       
   943     }
       
   944 
       
   945 // ----------------------------------------------------------------------------
       
   946 // MPXDbCommonUtil::StringFromArrayLC
       
   947 // ----------------------------------------------------------------------------
       
   948 //
       
   949 EXPORT_C HBufC* MPXDbCommonUtil::StringFromArrayLC(
       
   950     const CDesCArray& aArray,
       
   951     const TDesC& aSeparator)
       
   952     {
       
   953     MPX_FUNC("MPXDbCommonUtil::StringFromArrayLC");
       
   954 
       
   955     HBufC* str(NULL);
       
   956     TInt count(aArray.Count());
       
   957     if (count)
       
   958         {
       
   959         TInt len(0);
       
   960         TInt index(0);
       
   961         for (index = 0; index < count; ++index)
       
   962             {
       
   963             len += aArray[index].Length();
       
   964             }
       
   965 
       
   966         str = HBufC::NewLC(len + (aSeparator.Length() * (count - 1)));
       
   967         TPtr ptr(str->Des());
       
   968         TPtrC16 item;
       
   969         for (index = 0; index < count; ++index)
       
   970             {
       
   971             item.Set(aArray[index]);
       
   972             ptr.Append(item);
       
   973             MPX_DEBUG2("aArray[index] %S", &item);
       
   974             if (index < (count - 1))
       
   975                 {
       
   976                 ptr.Append(aSeparator);
       
   977                 }
       
   978             }
       
   979         }
       
   980     else
       
   981         {
       
   982         str = HBufC::NewLC(0);
       
   983         }
       
   984 
       
   985     return str;
       
   986     }
       
   987 
       
   988 // ----------------------------------------------------------------------------
       
   989 // MPXDbCommonUtil::StringFromArraysLC
       
   990 // ----------------------------------------------------------------------------
       
   991 //
       
   992 EXPORT_C HBufC* MPXDbCommonUtil::StringFromArraysLC(
       
   993     const CDesCArray& aNameArray,
       
   994     const CDesCArray& aValueArray,
       
   995     const TDesC& aValueSeparator,
       
   996     const TDesC& aEntitySeparator)
       
   997     {
       
   998     MPX_FUNC("MPXDbCommonUtil::StringFromArraysLC");
       
   999 
       
  1000     // calculate the length of the SET string
       
  1001     TInt len(0);
       
  1002     TInt index(0);
       
  1003     TInt count(aNameArray.Count());
       
  1004     for (index = 0; index < count; ++index)
       
  1005         {
       
  1006         len += aNameArray[index].Length() + aValueArray[index].Length();
       
  1007         }
       
  1008 
       
  1009     // construct the result string
       
  1010     HBufC* result = HBufC::NewLC(len +
       
  1011         ((aValueSeparator.Length() + aEntitySeparator.Length()) * count));
       
  1012     TPtr resultPtr(result->Des());
       
  1013     for (index = 0; index < count; ++index)
       
  1014         {
       
  1015         resultPtr.Append(aNameArray[index]);
       
  1016         resultPtr.Append(aValueSeparator);
       
  1017         resultPtr.Append(aValueArray[index]);
       
  1018 
       
  1019         if (index < count - 1)
       
  1020             {
       
  1021             resultPtr.Append(aEntitySeparator);
       
  1022             }
       
  1023         }
       
  1024 
       
  1025     return result;
       
  1026     }
       
  1027 
       
  1028 // ----------------------------------------------------------------------------
       
  1029 // MPXDbCommonUtil::TTimeToDesLC
       
  1030 // Converts a TTime to the internal SQLite format (YYYY-MM-DD HH:MM:SS).
       
  1031 // ----------------------------------------------------------------------------
       
  1032 //
       
  1033 EXPORT_C HBufC* MPXDbCommonUtil::TTimeToDesLC(
       
  1034     const TTime& aTime)
       
  1035     {
       
  1036     MPX_FUNC("MPXDbCommonUtil::TTimeToDesLC");
       
  1037 
       
  1038     HBufC* dateTime;
       
  1039     if (aTime == Time::NullTTime())
       
  1040         {
       
  1041         dateTime = HBufC::NewLC(0);
       
  1042         }
       
  1043     else
       
  1044         {
       
  1045         _LIT(KDateTimeFormat, "%04d-%02d-%02d %02d:%02d:%02d");
       
  1046         
       
  1047         TTime time(0);
       
  1048 
       
  1049         // negative time means BC, but format string will be invalid with our format,
       
  1050         // i.e. %04d in KDateTimeFormat when year is -1 will result in "00-1" and
       
  1051         // the whole string becomes "00-1-01-01 00:00:00" which will result in error,
       
  1052         // so set to 0 in such cases
       
  1053         TDateTime dt = aTime>time ? aTime.DateTime() : time.DateTime();
       
  1054         TInt dateTimeLen = KDateTimeFormat().Length();
       
  1055         dateTime = HBufC::NewLC(dateTimeLen);
       
  1056         TPtr dateTimePtr = dateTime->Des();
       
  1057         dateTimePtr.Format(KDateTimeFormat,
       
  1058                         dt.Year(),
       
  1059                         dt.Month() + 1, // zero based
       
  1060                         dt.Day() + 1,   // zero based
       
  1061                         dt.Hour(),
       
  1062                         dt.Minute(),
       
  1063                         dt.Second());
       
  1064         }
       
  1065     return dateTime;
       
  1066     }
       
  1067 
       
  1068 // ----------------------------------------------------------------------------
       
  1069 // MPXDbCommonUtil::DesToTTimeL
       
  1070 // Converts a date time string in the internal SQLite format (YYYY-MM-DD HH:MM:SS)
       
  1071 // to a TTime. TTime can only parse in the DD-MM-YYYY HH:MM:SS format.
       
  1072 // ----------------------------------------------------------------------------
       
  1073 //
       
  1074 EXPORT_C TTime MPXDbCommonUtil::DesToTTimeL(
       
  1075     const TDesC& aDateTime)
       
  1076     {
       
  1077     MPX_FUNC("MPXDbCommonUtil::DesToTTimeL");
       
  1078 
       
  1079     TTime time(0);
       
  1080     if (aDateTime.Length() != 0)
       
  1081         {
       
  1082         _LIT(KDash, "-");
       
  1083         _LIT(KSpace, " ");
       
  1084 
       
  1085         TLocale locale;
       
  1086         TDateFormat iDateFormat = locale.DateFormat();
       
  1087 
       
  1088         HBufC* dateTime = HBufC::NewLC(aDateTime.Length());
       
  1089         TPtr dateTimePtr = dateTime->Des();
       
  1090 
       
  1091         //as TTime::Parse is locale dependent, check it:
       
  1092         if(iDateFormat==EDateEuropean)
       
  1093             {
       
  1094             dateTimePtr.Append(aDateTime.Mid(8, 2));//day DD
       
  1095             dateTimePtr.Append(KDash);
       
  1096             dateTimePtr.Append(aDateTime.Mid(5, 2));//month MM
       
  1097             dateTimePtr.Append(KDash);
       
  1098             dateTimePtr.Append(aDateTime.Left(4));//year YYYY
       
  1099             dateTimePtr.Append(KSpace);
       
  1100             }
       
  1101         else if(iDateFormat==EDateJapanese)
       
  1102             {
       
  1103             dateTimePtr.Append(aDateTime.Left(4));//year YYYY
       
  1104             dateTimePtr.Append(KDash);
       
  1105             dateTimePtr.Append(aDateTime.Mid(5, 2));//month MM
       
  1106             dateTimePtr.Append(KDash);
       
  1107             dateTimePtr.Append(aDateTime.Mid(8, 2));//day DD
       
  1108             dateTimePtr.Append(KSpace);
       
  1109             }
       
  1110         else //iDateFormat==EDateAmerican
       
  1111             {
       
  1112             dateTimePtr.Append(aDateTime.Mid(5, 2));//month MM
       
  1113             dateTimePtr.Append(KDash);
       
  1114             dateTimePtr.Append(aDateTime.Mid(8, 2));//day DD
       
  1115             dateTimePtr.Append(KDash);
       
  1116             dateTimePtr.Append(aDateTime.Left(4));//year YYYY
       
  1117             dateTimePtr.Append(KSpace);            
       
  1118             }
       
  1119 
       
  1120 		    // When colon (:) is set as Date separator in Date and Time setting, 
       
  1121 		    // colon in Time descriptors is parsed as Date separator. 
       
  1122         if ( locale.DateSeparator(1) == ':')
       
  1123             {
       
  1124             _LIT(KDot, ".");
       
  1125         
       
  1126         		// time HH.MM.SS
       
  1127             dateTimePtr.Append( aDateTime.Mid(11, 2) );
       
  1128             dateTimePtr.Append( KDot );
       
  1129             dateTimePtr.Append( aDateTime.Mid(14, 2) );
       
  1130             dateTimePtr.Append( KDot );
       
  1131             dateTimePtr.Append( aDateTime.Mid(17, 2) );
       
  1132             }
       
  1133         else
       
  1134             {
       
  1135             dateTimePtr.Append(aDateTime.Right(8));//time HH:MM:SS
       
  1136             }
       
  1137 
       
  1138         User::LeaveIfError(time.Parse(dateTimePtr));
       
  1139         CleanupStack::PopAndDestroy(dateTime);
       
  1140         }
       
  1141     else
       
  1142         {
       
  1143         time = Time::NullTTime();
       
  1144         }
       
  1145     return time;
       
  1146     }
       
  1147 
       
  1148 // ----------------------------------------------------------------------------
       
  1149 // MPXDbCommonUtil::CurrentTimeDesLC
       
  1150 // ----------------------------------------------------------------------------
       
  1151 //
       
  1152 EXPORT_C HBufC* MPXDbCommonUtil::CurrentTimeDesLC()
       
  1153     {
       
  1154     MPX_FUNC("MPXDbCommonUtil::CurrentTimeDesLC");
       
  1155 
       
  1156     TTime time;
       
  1157     time.HomeTime();
       
  1158 
       
  1159     return TTimeToDesLC(time);
       
  1160     }
       
  1161 
       
  1162 // ----------------------------------------------------------------------------
       
  1163 // MPXDbCommonUtil::CurrentDateDesLC
       
  1164 // ----------------------------------------------------------------------------
       
  1165 //
       
  1166 EXPORT_C HBufC* MPXDbCommonUtil::CurrentDateDesLC()
       
  1167     {
       
  1168     MPX_FUNC("MPXDbCommonUtil::CurrentDateDesLC");
       
  1169 
       
  1170     TTime time;
       
  1171     time.HomeTime();
       
  1172 
       
  1173     // Round off to the nearest hour
       
  1174     TDateTime date(time.DateTime());
       
  1175     date.SetMinute(0);
       
  1176     date.SetSecond(0);
       
  1177     date.SetMicroSecond(0);
       
  1178 
       
  1179     return TTimeToDesLC(TTime(date));
       
  1180     }
       
  1181 
       
  1182 // ----------------------------------------------------------------------------
       
  1183 // MPXDbCommonUtil::GetDriveL
       
  1184 // ----------------------------------------------------------------------------
       
  1185 //
       
  1186 EXPORT_C TInt MPXDbCommonUtil::GetDriveL(
       
  1187     const TDesC& aUri,
       
  1188     TDriveUnit& aDrive)
       
  1189     {
       
  1190     MPX_FUNC("MPXDbCommonUtil::GetDriveL");
       
  1191 
       
  1192     TInt err(KErrNotFound);
       
  1193     TParsePtrC parser(aUri);
       
  1194     if (parser.Drive().Length())
       
  1195         {
       
  1196         aDrive = TDriveUnit(aUri);
       
  1197         err = KErrNone;
       
  1198         }
       
  1199 
       
  1200     return err;
       
  1201     }
       
  1202 
       
  1203 // ----------------------------------------------------------------------------
       
  1204 // MPXDbCommonUtil::AttributeExistsL
       
  1205 // ----------------------------------------------------------------------------
       
  1206 //
       
  1207 EXPORT_C TBool MPXDbCommonUtil::AttributeExists(
       
  1208     const TArray<TMPXAttribute>& aAttrs,
       
  1209     const TMPXAttribute& aAttribute)
       
  1210     {
       
  1211     MPX_FUNC("MPXDbCommonUtil::AttributeExists");
       
  1212 
       
  1213     TBool ret(EFalse);
       
  1214 
       
  1215     TUint content(aAttribute.ContentId());
       
  1216     TUint attribute(aAttribute.AttributeId());
       
  1217 
       
  1218     TInt count(aAttrs.Count());
       
  1219     for (TInt i = 0; i < count; ++i)
       
  1220         {
       
  1221         if ((aAttrs[i].ContentId() == content) &&
       
  1222             (aAttrs[i].AttributeId() & attribute))
       
  1223             {
       
  1224             ret = ETrue;
       
  1225             break;
       
  1226             }
       
  1227         }
       
  1228 
       
  1229     return ret;
       
  1230     }
       
  1231 
       
  1232 // ----------------------------------------------------------------------------
       
  1233 // MPXDbCommonUtil::ConstructMediaLC
       
  1234 // ----------------------------------------------------------------------------
       
  1235 //
       
  1236 CMPXMedia* MPXDbCommonUtil::ConstructMediaLC(
       
  1237     const TDesC& aTitle,
       
  1238     TMPXGeneralType aType,
       
  1239     TMPXGeneralCategory aCat,
       
  1240     TMPXItemId aId,
       
  1241     TInt aNonPermissibleActions,
       
  1242     TUint aDbflag)
       
  1243     {
       
  1244     MPX_FUNC("MPXDbCommonUtil::ConstructMediaLC");
       
  1245 
       
  1246     RArray<TInt> supportedIds;
       
  1247     CleanupClosePushL(supportedIds);
       
  1248     supportedIds.AppendL(KMPXMediaIdGeneral);
       
  1249     CMPXMedia* entry = CMPXMedia::NewL(supportedIds.Array());
       
  1250     CleanupStack::PopAndDestroy(&supportedIds);
       
  1251     CleanupStack::PushL(entry);
       
  1252     entry->SetTextValueL(KMPXMediaGeneralTitle, aTitle);
       
  1253     entry->SetTObjectValueL<TMPXGeneralType>(KMPXMediaGeneralType, aType);
       
  1254     entry->SetTObjectValueL<TMPXGeneralCategory>(KMPXMediaGeneralCategory, aCat);
       
  1255     entry->SetTObjectValueL<TMPXItemId>(KMPXMediaGeneralId, aId);
       
  1256     entry->SetTObjectValueL<TUint>(KMPXMediaGeneralFlags, aDbflag);
       
  1257 
       
  1258     if (aNonPermissibleActions)
       
  1259         {
       
  1260         // set non-permissible actions
       
  1261         entry->SetTObjectValueL<TMPXGeneralNonPermissibleActions>(KMPXMediaGeneralNonPermissibleActions,
       
  1262             static_cast<TMPXGeneralNonPermissibleActions>(aNonPermissibleActions));
       
  1263         }
       
  1264 
       
  1265     return entry;
       
  1266     }
       
  1267 
       
  1268 // ----------------------------------------------------------------------------
       
  1269 // MPXDbCommonUtil::GetColumnTextL
       
  1270 // ----------------------------------------------------------------------------
       
  1271 //
       
  1272 EXPORT_C TPtrC MPXDbCommonUtil::GetColumnTextL(
       
  1273 	RSqlStatement& aStatement, 
       
  1274 	TInt aField)
       
  1275 	{
       
  1276     MPX_FUNC("MPXDbCommonUtil::GetColumnTextL");
       
  1277     
       
  1278     TPtrC text;
       
  1279 	if (aStatement.ColumnSize(aField))
       
  1280 		{
       
  1281 	    text.Set(aStatement.ColumnTextL(aField));
       
  1282 		}
       
  1283 	else
       
  1284 		{
       
  1285 	    text.Set(KNullDesC);    	        		
       
  1286 		}	
       
  1287 
       
  1288     return text;
       
  1289 	}
       
  1290 
       
  1291 // ----------------------------------------------------------------------------
       
  1292 // Add an album item changed message to the message array
       
  1293 // ----------------------------------------------------------------------------
       
  1294 //
       
  1295 EXPORT_C void MPXDbCommonUtil::AddItemAlbumChangedMessageL(
       
  1296     CMPXMessageArray& aMessageArray,
       
  1297     TMPXItemId aId,
       
  1298     TMPXChangeEventType aChangeType,
       
  1299     TMPXGeneralCategory aCategory,
       
  1300     TUint aUid,
       
  1301     TBool aAlbumArt,
       
  1302     TMPXItemId aDeprecatedId)
       
  1303     {
       
  1304     MPX_FUNC("MPXDbCommonUtil::AddItemChangedMessageL");
       
  1305     CMPXMessage* message = CMPXMedia::NewL();
       
  1306     CleanupStack::PushL(message);
       
  1307 
       
  1308     FillItemChangedMessageL(*message, aId, aChangeType, aCategory, aUid,
       
  1309     		aDeprecatedId );
       
  1310     if ( aAlbumArt )
       
  1311         {
       
  1312         message->SetTObjectValueL<TMPXItemId>(KMPXMessageMediaDeprecatedId, aId);
       
  1313         }
       
  1314     if (FindItemChangedMessageL(aMessageArray, *message) == KErrNotFound)
       
  1315         {
       
  1316         aMessageArray.AppendL(*message); // ownership xfer
       
  1317         }
       
  1318     CleanupStack::PopAndDestroy(message);
       
  1319     }
       
  1320 // End of File