javatools/javaapppreconverter/src.s60/midletlist.cpp
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 - 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Java platform 2.0 javaapppreconverter process
       
    15 *
       
    16 */
       
    17 
       
    18 #include "midletlist.h"
       
    19 
       
    20 
       
    21 
       
    22 CMidletInfo::CMidletInfo() :
       
    23         iMidletUid(TUid::Null()), iMidletId(KMaxTUint32), iSuiteId(KMaxTUint32),
       
    24         iMidletName(NULL), iGroupName(NULL), iDrive(EDriveE)
       
    25 {
       
    26 }
       
    27 
       
    28 CMidletInfo::CMidletInfo(CMidletInfo* aMidletInfo)
       
    29 {
       
    30     iMidletUid = aMidletInfo->GetMidletUid();
       
    31     iMidletId = aMidletInfo->GetMidletId();
       
    32     iSuiteId = aMidletInfo->GetSuiteId();
       
    33     iMidletName = aMidletInfo->GetMidletName().Alloc();
       
    34     iGroupName = aMidletInfo->GetGroupName().Alloc();
       
    35     iDrive = aMidletInfo->GetDrive();
       
    36     iIconFileName = aMidletInfo->GetIconFileName();
       
    37 }
       
    38 
       
    39 
       
    40 CMidletInfo::~CMidletInfo()
       
    41 {
       
    42     delete iMidletName;
       
    43     delete iGroupName;
       
    44 }
       
    45 
       
    46 
       
    47 TUid CMidletInfo::GetMidletUid() const
       
    48 {
       
    49     return iMidletUid;
       
    50 }
       
    51 
       
    52 TUint32 CMidletInfo::GetMidletId() const
       
    53 {
       
    54     return iMidletId;
       
    55 }
       
    56 
       
    57 TUint32 CMidletInfo::GetSuiteId() const
       
    58 {
       
    59     return iSuiteId;
       
    60 }
       
    61 
       
    62 const TDesC& CMidletInfo::GetMidletName()
       
    63 {
       
    64     if (NULL == iMidletName)
       
    65     {
       
    66         return KNullDesC;
       
    67     }
       
    68     else
       
    69     {
       
    70         return *iMidletName;
       
    71     }
       
    72 }
       
    73 
       
    74 const TDesC& CMidletInfo::GetGroupName()
       
    75 {
       
    76     if (NULL == iGroupName)
       
    77     {
       
    78         return KNullDesC;
       
    79     }
       
    80     else
       
    81     {
       
    82         return *iGroupName;
       
    83     }
       
    84 }
       
    85 
       
    86 TDriveNumber CMidletInfo::GetDrive()
       
    87 {
       
    88     return iDrive;
       
    89 }
       
    90 
       
    91 const TFileName& CMidletInfo::GetIconFileName()
       
    92 {
       
    93     return iIconFileName;
       
    94 }
       
    95 
       
    96 
       
    97 
       
    98 
       
    99 void CMidletInfo::SetMidletUid(const TUid aUid)
       
   100 {
       
   101     iMidletUid = aUid;
       
   102 }
       
   103 
       
   104 void CMidletInfo::SetMidletId(const TUint32 aId)
       
   105 {
       
   106     iMidletId = aId;
       
   107 }
       
   108 
       
   109 void CMidletInfo::SetSuiteId(const TUint32 aId)
       
   110 {
       
   111     iSuiteId = aId;
       
   112 }
       
   113 
       
   114 void CMidletInfo::SetMidletName(const TDesC& aName)
       
   115 {
       
   116     iMidletName = aName.Alloc();
       
   117 }
       
   118 
       
   119 void CMidletInfo::SetGroupName(const TDesC& aName)
       
   120 {
       
   121     iGroupName = aName.Alloc();
       
   122 }
       
   123 
       
   124 void CMidletInfo::SetDrive(const TDriveNumber aDrive)
       
   125 {
       
   126     iDrive = aDrive;
       
   127 }
       
   128 
       
   129 void CMidletInfo::SetIconFileName(HBufC16 *aIconFileName)
       
   130 {
       
   131     iIconFileName = *aIconFileName;
       
   132 }
       
   133 
       
   134 void CMidletInfo::SetIconFileName(const TFileName &aIconFileName)
       
   135 {
       
   136     iIconFileName = aIconFileName;
       
   137 }
       
   138 
       
   139 void CMidletInfo::ExternalizeL(RWriteStream& aStream) const
       
   140 {
       
   141     aStream.WriteUint32L(iMidletUid.iUid);
       
   142     aStream.WriteUint32L(iMidletId);
       
   143     aStream.WriteUint32L(iSuiteId);
       
   144 
       
   145     TInt32 nLen = 0;
       
   146     if (iMidletName)
       
   147     {
       
   148         nLen = iMidletName->Length();
       
   149     }
       
   150     aStream.WriteInt32L(nLen);
       
   151     if (nLen > 0)
       
   152     {
       
   153         aStream.WriteL(*iMidletName);
       
   154     }
       
   155 
       
   156     nLen = 0;
       
   157     if (iGroupName)
       
   158     {
       
   159         nLen = iGroupName->Length();
       
   160     }
       
   161     aStream.WriteInt32L(nLen);
       
   162     if (nLen > 0)
       
   163     {
       
   164         aStream.WriteL(*iGroupName);
       
   165     }
       
   166 
       
   167     aStream.WriteUint32L(iDrive);
       
   168 
       
   169     nLen = iIconFileName.Length();
       
   170     aStream.WriteInt32L(nLen);
       
   171     if (nLen > 0)
       
   172     {
       
   173         aStream.WriteL(iIconFileName);
       
   174     }
       
   175 
       
   176 }
       
   177 
       
   178 void CMidletInfo::InternalizeL(RReadStream& aStream)
       
   179 {
       
   180     iMidletUid.iUid = aStream.ReadUint32L();
       
   181     iMidletId = aStream.ReadUint32L();
       
   182     iSuiteId = aStream.ReadUint32L();
       
   183 
       
   184     TInt32 nLen = 0;
       
   185     nLen = aStream.ReadInt32L();
       
   186     if (nLen > 0)
       
   187     {
       
   188         if (!iMidletName)
       
   189         {
       
   190             iMidletName = HBufC16::NewL(nLen + 8);
       
   191         }
       
   192 
       
   193         TPtr16 tmpDes = iMidletName->Des();
       
   194         aStream.ReadL(tmpDes, nLen);
       
   195     }
       
   196     else
       
   197     {
       
   198         if (!iMidletName)
       
   199         {
       
   200             // empty midlet name
       
   201             iMidletName = HBufC16::NewL(8);
       
   202         }
       
   203     }
       
   204 
       
   205     nLen = aStream.ReadInt32L();
       
   206     if (nLen > 0)
       
   207     {
       
   208         if (!iGroupName)
       
   209         {
       
   210             iGroupName = HBufC16::NewL(nLen + 8);
       
   211         }
       
   212         TPtr16 tmpDes = iGroupName->Des();
       
   213         aStream.ReadL(tmpDes, nLen);
       
   214     }
       
   215     else
       
   216     {
       
   217         if (!iGroupName)
       
   218         {
       
   219             // empty group name
       
   220             iGroupName = HBufC16::NewL(8);
       
   221         }
       
   222     }
       
   223 
       
   224     iDrive = (TDriveNumber)aStream.ReadUint32L();
       
   225 
       
   226     nLen = aStream.ReadInt32L();
       
   227     if (nLen > 0)
       
   228     {
       
   229         aStream.ReadL(iIconFileName, nLen);
       
   230     }
       
   231 }
       
   232 
       
   233 void CMidletInfo::ToString8(TDes8& aDescription)
       
   234 {
       
   235     aDescription.Append(_L8("Uid: "));
       
   236     aDescription.AppendNumFixedWidth(iMidletUid.iUid, EHex, 8);
       
   237     aDescription.Append(_L8("\nMidlet id: "));
       
   238     aDescription.AppendNumFixedWidth(iMidletId, EDecimal, 8);
       
   239     aDescription.Append(_L8("\nSuite id: "));
       
   240     aDescription.AppendNumFixedWidth(iSuiteId, EDecimal, 8);
       
   241     aDescription.Append(_L8("\nName: "));
       
   242     // Midlet name len is not limited. Make sure that buffer does not overflow.
       
   243     aDescription.Append((*iMidletName).Left(256));
       
   244     aDescription.Append(_L8("\nGroup: "));
       
   245     // Group name is max 32 characters
       
   246     aDescription.Append(*iGroupName);
       
   247     aDescription.Append(_L8("\nDrive: "));
       
   248     aDescription.AppendNumFixedWidth(iDrive, EDecimal, 2);
       
   249 }
       
   250 
       
   251 /* ---------------------------------------------------------------------------- */
       
   252 
       
   253 
       
   254 
       
   255 
       
   256 CMidletList::CMidletList() :
       
   257         iMidletArray(RPointerArray<CMidletInfo>()), iGetNextIndex(0),
       
   258         iMidletArrayDestroyed(EFalse)
       
   259 {
       
   260 
       
   261 }
       
   262 
       
   263 CMidletList::~CMidletList()
       
   264 {
       
   265     // Free the memory used by the RPointerArray.
       
   266     // Do not destroy objects itself.
       
   267     if (!iMidletArrayDestroyed)
       
   268     {
       
   269         iMidletArray.Close();
       
   270     }
       
   271 }
       
   272 
       
   273 void CMidletList::ResetAndDestroy()
       
   274 {
       
   275     iMidletArray.ResetAndDestroy();
       
   276     iMidletArrayDestroyed = ETrue;
       
   277 }
       
   278 
       
   279 TInt CMidletList::Append(const CMidletInfo* aMidletInfo)
       
   280 {
       
   281     if (NULL != Find(aMidletInfo->GetMidletUid()))
       
   282     {
       
   283         // midlet is already in list, do nothing
       
   284         return KErrNone;
       
   285     }
       
   286 
       
   287     return iMidletArray.Append(aMidletInfo);
       
   288 }
       
   289 
       
   290 void CMidletList::Remove(const CMidletInfo* aMidletInfo)
       
   291 {
       
   292     TInt nInd = iMidletArray.Find(aMidletInfo);
       
   293     if (nInd < 0)
       
   294     {
       
   295         // The object is not in list, do nothing
       
   296         return;
       
   297     }
       
   298     else if (nInd < iGetNextIndex)
       
   299     {
       
   300         // Removing one of the objects already returned by
       
   301         // getNext(), the indexes of the objects not yet returned by
       
   302         // getNext() are decreased by one. Must decrease iGetNextIndex too.
       
   303         iGetNextIndex--;
       
   304     }
       
   305 
       
   306     iMidletArray.Remove(nInd);
       
   307 }
       
   308 
       
   309 TInt CMidletList::Count() const
       
   310 {
       
   311     return iMidletArray.Count();
       
   312 }
       
   313 
       
   314 CMidletInfo* CMidletList::GetFirst()
       
   315 {
       
   316     iGetNextIndex = 1;
       
   317 
       
   318     if (iMidletArray.Count() > 0)
       
   319     {
       
   320         return iMidletArray[0];
       
   321     }
       
   322     else
       
   323     {
       
   324         return NULL;
       
   325     }
       
   326 }
       
   327 
       
   328 CMidletInfo* CMidletList::GetNext()
       
   329 {
       
   330     if (iMidletArray.Count() > iGetNextIndex)
       
   331     {
       
   332         iGetNextIndex++;
       
   333         return iMidletArray[iGetNextIndex-1];
       
   334     }
       
   335     else
       
   336     {
       
   337         return NULL;
       
   338     }
       
   339 }
       
   340 
       
   341 CMidletInfo *CMidletList::Find(const TUid aMidletUid)
       
   342 {
       
   343     TInt nCount = iMidletArray.Count();
       
   344 
       
   345     for (TInt nInd = 0; nInd < nCount; nInd++)
       
   346     {
       
   347         if (iMidletArray[nInd]->GetMidletUid() == aMidletUid)
       
   348         {
       
   349             return iMidletArray[nInd];
       
   350         }
       
   351     }
       
   352 
       
   353     return NULL;
       
   354 }
       
   355 
       
   356 /**
       
   357  * Return the first midlet that has name specified by aName and
       
   358  * for which the midlet id and suite id have not yet been set.
       
   359  * If midlet id and suite id have been set for all midlets with
       
   360  * name specified by aName, returns the first midlet with name
       
   361  * specified by aName
       
   362  */
       
   363 CMidletInfo *CMidletList::Find(const TPtrC &aName)
       
   364 {
       
   365     CMidletInfo *firstFound = NULL;
       
   366     TInt nCount = iMidletArray.Count();
       
   367 
       
   368     for (TInt nInd = 0; nInd < nCount; nInd++)
       
   369     {
       
   370         if (iMidletArray[nInd]->GetMidletName() == aName)
       
   371         {
       
   372             // If midlet id and suite id have already been set,
       
   373             // find next midlet with the same name if any.
       
   374             // If no such midlet, return this one.
       
   375             if (NULL == firstFound)
       
   376             {
       
   377                 firstFound = iMidletArray[nInd];
       
   378             }
       
   379             if ((iMidletArray[nInd]->GetSuiteId() == KMaxTUint32) &&
       
   380                     (iMidletArray[nInd]->GetMidletId() == KMaxTUint32))
       
   381             {
       
   382                 return iMidletArray[nInd];
       
   383             }
       
   384         }
       
   385     }
       
   386 
       
   387     return firstFound;
       
   388 }
       
   389 
       
   390 void CMidletList::SetIds(
       
   391     const TUint32 aUid,
       
   392     const TUint32 aMidletSuiteId,
       
   393     const TUint32 aMidletId)
       
   394 {
       
   395     TUid uid;
       
   396     uid.iUid = aUid;
       
   397     CMidletInfo *midlet = Find(uid);
       
   398 
       
   399     if (NULL != midlet)
       
   400     {
       
   401         midlet->SetSuiteId(aMidletSuiteId);
       
   402         midlet->SetMidletId(aMidletId);
       
   403     }
       
   404 }
       
   405 
       
   406 void CMidletList::SetIds(
       
   407     const TPtrC &aName,
       
   408     const TUint32 aMidletSuiteId,
       
   409     const TUint32 aMidletId)
       
   410 {
       
   411     CMidletInfo *midlet = Find(aName);
       
   412 
       
   413     if (NULL != midlet)
       
   414     {
       
   415         midlet->SetSuiteId(aMidletSuiteId);
       
   416         midlet->SetMidletId(aMidletId);
       
   417     }
       
   418 }
       
   419 
       
   420 void CMidletList::ExportListL(RFs& aFs, const TFileName &aDirectory)
       
   421 {
       
   422     if (iMidletArrayDestroyed || (iMidletArray.Count() == 0))
       
   423     {
       
   424         // Trying to export empty list is an error
       
   425         User::Leave(KErrArgument);
       
   426     }
       
   427 
       
   428     // Create directory if it does not exist
       
   429     TInt err= aFs.MkDirAll(aDirectory);
       
   430     if ((KErrNone != err) && (KErrAlreadyExists != err))
       
   431     {
       
   432         User::Leave(err);
       
   433     }
       
   434 
       
   435     // Copy all icon files to directory
       
   436     CFileMan    *fm = CFileMan::NewL(aFs);
       
   437     CleanupStack::PushL(fm);
       
   438     CMidletInfo *midlet = GetFirst();
       
   439     TFileName    newIconFileName;
       
   440     TParse       nameParser;
       
   441     TUint32      nMidlets = 0;
       
   442 
       
   443     while (NULL != midlet)
       
   444     {
       
   445         User::LeaveIfError(fm->Copy(midlet->GetIconFileName(), aDirectory));
       
   446 
       
   447         // set new icon file name
       
   448         newIconFileName = aDirectory;
       
   449         User::LeaveIfError(nameParser.Set(midlet->GetIconFileName(), NULL, NULL));
       
   450         newIconFileName.Append(nameParser.NameAndExt());
       
   451         midlet->SetIconFileName(newIconFileName);
       
   452 
       
   453         nMidlets++;
       
   454         midlet = GetNext();
       
   455     }
       
   456 
       
   457     // Store all midlet info to a file in the directory
       
   458     TFileName dataFilename = aDirectory;
       
   459     dataFilename.Append(KMidletExportDataFileName);
       
   460 
       
   461     // Construct write stream so that new data file is created.
       
   462     // If the file already exists, leave
       
   463     RFileWriteStream writeStream;
       
   464     User::LeaveIfError(writeStream.Create(aFs, dataFilename, EFileWrite));
       
   465     CleanupClosePushL(writeStream);
       
   466 
       
   467     // Write the number of midlets
       
   468     writeStream.WriteUint32L(nMidlets);
       
   469 
       
   470     // write the midlet info one by one
       
   471     midlet = GetFirst();
       
   472     while (NULL != midlet)
       
   473     {
       
   474         midlet->ExternalizeL(writeStream);
       
   475         midlet = GetNext();
       
   476     }
       
   477 
       
   478     CleanupStack::PopAndDestroy();  // writeStream
       
   479     CleanupStack::PopAndDestroy(fm);
       
   480 }
       
   481 
       
   482 void CMidletList::ImportListL(RFs& aFs, const TFileName &aDirectory)
       
   483 {
       
   484     CMidletInfo *midlet = NULL;
       
   485 
       
   486     // Form the data file name
       
   487     TFileName dataFilename = aDirectory;
       
   488     dataFilename.Append(KMidletExportDataFileName);
       
   489 
       
   490     // Open existing data file. If the file does not exist, leave
       
   491     RFileReadStream readStream;
       
   492     User::LeaveIfError(readStream.Open(aFs, dataFilename, EFileRead));
       
   493     CleanupClosePushL(readStream);
       
   494 
       
   495     // Read the number of midlets
       
   496     TUint32 nMidlets = readStream.ReadUint32L();
       
   497 
       
   498     for (TInt nInd = 0; nInd < nMidlets; nInd++)
       
   499     {
       
   500         // Read info of one midlet
       
   501         midlet = new(ELeave) CMidletInfo;
       
   502         CleanupStack::PushL(midlet);
       
   503         midlet->InternalizeL(readStream);
       
   504 
       
   505         // Add it to list
       
   506         Append(midlet);
       
   507         CleanupStack::Pop(midlet);
       
   508     }
       
   509 
       
   510     // close readStream
       
   511     CleanupStack::PopAndDestroy();
       
   512 }
       
   513