javamanager/javabackup/midp2backup_usif/src.s60/apparcbackuputil.cpp
branchRCL_3
changeset 24 6c158198356e
equal deleted inserted replaced
23:e5618cc85d74 24:6c158198356e
       
     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:  Implementation of CAppArcBackupUtil
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "apparcbackuputil.h"
       
    20 #include "javaapparcutil.h"
       
    21 
       
    22 #include "javasymbianoslayer.h"
       
    23 #include "javauid.h"
       
    24 #include "logger.h"
       
    25 #include "javacommonutils.h"
       
    26 #include "s60commonutils.h"
       
    27 
       
    28 #include <apgcli.h>
       
    29 #include <wchar.h>
       
    30 #include <s32mem.h>
       
    31 #include <sysutil.h>
       
    32 #include <f32file.h>
       
    33 
       
    34 #include <memory>
       
    35 #include <string>
       
    36 
       
    37 #ifndef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    38 #include <apgicnfl.h>
       
    39 #else
       
    40 #include <apgicnflpartner.h>
       
    41 #endif
       
    42 
       
    43 
       
    44 using namespace std;
       
    45 using namespace java::storage;
       
    46 using namespace java::backup;
       
    47 
       
    48 const TUid  KMidletApplicationTypeUid = { 0x10210E26 };
       
    49 _LIT(KDefaultFolder, "");
       
    50 
       
    51 // ======== MEMBER FUNCTIONS ========
       
    52 
       
    53 CAppArcBackupUtil::CAppArcBackupUtil()
       
    54 {
       
    55     LOG(EBackup, EInfo, "CAppArcBackupUtil constructor");
       
    56 }
       
    57 
       
    58 void CAppArcBackupUtil::ConstructL(RFs& aFs)
       
    59 {
       
    60     LOG(EBackup, EInfo, "CAppArcBackupUtil::ConstructL");
       
    61 
       
    62     User::LeaveIfError(iApparcServer.Connect());
       
    63 
       
    64     iNumberOfIcons = 0;
       
    65     iFs = aFs;
       
    66 }
       
    67 
       
    68 
       
    69 CAppArcBackupUtil* CAppArcBackupUtil::NewL(RFs& aFs)
       
    70 {
       
    71     LOG(EBackup, EInfo, "CAppArcBackupUtil::NewL");
       
    72 
       
    73     CAppArcBackupUtil* self = new(ELeave) CAppArcBackupUtil();
       
    74     CleanupStack::PushL(self);
       
    75     self->ConstructL(aFs);
       
    76     CleanupStack::Pop();
       
    77 
       
    78     return self;
       
    79 }
       
    80 
       
    81 
       
    82 CAppArcBackupUtil::~CAppArcBackupUtil()
       
    83 {
       
    84     LOG(EBackup, EInfo, "CAppArcBackupUtil destructor");
       
    85 
       
    86     iApparcServer.Close();
       
    87 }
       
    88 
       
    89 
       
    90 void CAppArcBackupUtil::RegisterAppL(TPtr8& aRestoreIconPtr, TDriveNumber& aDrive)
       
    91 {
       
    92     LOG(EBackup, EInfo, "CAppArcBackupUtil::RegisterApp");
       
    93 
       
    94     RDesReadStream stream(aRestoreIconPtr);
       
    95     CleanupClosePushL(stream);
       
    96 
       
    97     // Read Uid
       
    98     int midletUid = stream.ReadUint32L();
       
    99 
       
   100     if (!CheckIfAlreadyRegisteredL(midletUid))
       
   101     {
       
   102         // Read filename
       
   103         TInt32 fileNameSize = stream.ReadInt32L();
       
   104         HBufC* fileName = HBufC::NewL(fileNameSize);
       
   105         CleanupStack::PushL(fileName);
       
   106         TPtr16 fileNamePtr = fileName->Des();
       
   107         stream.ReadL(fileNamePtr, fileNameSize / 2);   // half of size
       
   108 
       
   109         // Read number of icons
       
   110         TInt numberOfIcons = stream.ReadInt32L();
       
   111 //        numberOfIcons = 1;
       
   112         //LOG1(EBackup, EInfo, "Number of Icons = %d", numberOfIcons);
       
   113 
       
   114         TInt groupNameSize = stream.ReadInt32L();
       
   115         TBuf<KApaMaxAppGroupName> groupName;
       
   116 
       
   117         if (groupNameSize != 0)
       
   118         {
       
   119             stream.ReadL(groupName, groupNameSize / 2);
       
   120         }
       
   121 
       
   122         // check disk space
       
   123         TInt index = sizeof(TUint32)    // midletUid
       
   124                      + sizeof(TInt32)                // size of fileNameSize
       
   125                      + fileNameSize                  // size of filename
       
   126                      + sizeof(TInt32)                // size of numberOfIcons
       
   127                      + sizeof(TInt32)                // group name size
       
   128                      + groupNameSize;                // groupname
       
   129 
       
   130         if (SysUtil::DiskSpaceBelowCriticalLevelL(&iFs, aRestoreIconPtr.Size() - index, aDrive))
       
   131         {
       
   132             ELOG(EBackup, "Disk space below critical level. Leaving with KErrDiskFull.");
       
   133             User::Leave(KErrDiskFull);
       
   134         }
       
   135 
       
   136         // Make directory if necessary
       
   137         iFs.CreatePrivatePath(aDrive);
       
   138 
       
   139         // Make full path of temporary icon file
       
   140         TPath tempPath;
       
   141         TPath tempPrivatePath;
       
   142         iFs.PrivatePath(tempPrivatePath);
       
   143 
       
   144         TDriveUnit* driveUnit = new(ELeave) TDriveUnit(aDrive);
       
   145         CleanupStack::PushL(driveUnit);
       
   146 
       
   147         tempPath.Append(driveUnit->Name().Left(1));
       
   148         tempPath.Append(KDriveDelimiter);
       
   149         tempPath.Append(tempPrivatePath);
       
   150         tempPath.Append(fileNamePtr);
       
   151 
       
   152         // Set private session path to point same drive where restoring
       
   153         iFs.SetSessionToPrivate(aDrive);
       
   154 
       
   155 
       
   156         RFile iconFile;
       
   157         User::LeaveIfError(iconFile.Create(iFs, fileNamePtr, EFileWrite));
       
   158 
       
   159         CleanupStack::PopAndDestroy(fileName);
       
   160         CleanupClosePushL(iconFile);
       
   161 
       
   162         iconFile.Write(aRestoreIconPtr.Mid(index));
       
   163 
       
   164         // Register application
       
   165         LOG1(EBackup,EInfo,"Registering app %d", midletUid);
       
   166         TUid uid = TUid::Uid(midletUid);
       
   167         TBuf<128>   appName;
       
   168         CJavaApparcUtil::AppName(uid, appName);
       
   169 
       
   170         CApaRegistrationResourceFileWriter* writer =
       
   171             CApaRegistrationResourceFileWriter::NewL(uid,
       
   172                     appName,
       
   173                     TApaAppCapability::ENonNative);
       
   174         CleanupStack::PushL(writer);
       
   175 
       
   176         const TInt  KOpaqueDataLength = 4;
       
   177         TBuf8<KOpaqueDataLength>    opaqueData;
       
   178         RDesWriteStream             writeStream(opaqueData);
       
   179         CleanupClosePushL(writeStream);
       
   180 
       
   181         LOG(EBackup, EInfo, "Getting midlet entry");
       
   182 
       
   183         JavaStorageApplicationEntry_t appEntries = StorageEntryL(uid);
       
   184 
       
   185         if (appEntries.size() == 0)
       
   186         {
       
   187             ELOG(EBackup, "No midlet entry found in Java Storage; Mismatch between AppArc and Storage data. Abort backup");
       
   188             User::Leave(KErrNotFound);
       
   189         }
       
   190 
       
   191         JavaStorageEntry attribute;
       
   192         attribute.setEntry(NAME, L"");
       
   193 
       
   194         // Get Name attribute from read attributes.
       
   195         JavaStorageApplicationEntry_t::const_iterator findIterator = appEntries.find(attribute);
       
   196         wstring midName;
       
   197 
       
   198         if (findIterator != appEntries.end())
       
   199         {
       
   200             midName = (*findIterator).entryValue();
       
   201 
       
   202         }
       
   203         else
       
   204         {
       
   205             User::Leave(KErrBadHandle);
       
   206         }
       
   207 
       
   208         appEntries.clear();
       
   209 
       
   210         HBufC* tempstr = java::util::S60CommonUtils::wstringToDes(midName.c_str());
       
   211         TPtrC midletName = tempstr->Des();
       
   212 
       
   213         if (groupNameSize != 0)
       
   214         {
       
   215             writer->SetGroupNameL(groupName);
       
   216         }
       
   217         else
       
   218         {
       
   219             writer->SetGroupNameL(KDefaultFolder);
       
   220         }
       
   221 
       
   222         writeStream.WriteUint32L(midletUid);
       
   223         writeStream.CommitL();
       
   224 
       
   225         LOG(EBackup, EInfo, "Setting opaque data");
       
   226         writer->SetOpaqueDataL(opaqueData);
       
   227 
       
   228         CApaLocalisableResourceFileWriter*  lWriter =
       
   229             CApaLocalisableResourceFileWriter::NewL(KNullDesC,
       
   230                                                     midletName,
       
   231                                                     numberOfIcons,
       
   232                                                     KNullDesC);
       
   233         CleanupStack::PushL(lWriter);
       
   234 
       
   235         // Prepare AppArc for deregistration
       
   236         LOG(EBackup, EInfo, "PrepareNonNativeApplicationsUpdatesL");
       
   237         iApparcServer.PrepareNonNativeApplicationsUpdatesL();
       
   238 
       
   239         // Register application
       
   240         LOG(EBackup, EInfo, "RegisterNonNativeApplicationL");
       
   241         iApparcServer.RegisterNonNativeApplicationL(KMidletApplicationTypeUid,
       
   242                 aDrive,
       
   243                 *writer,
       
   244                 lWriter,
       
   245                 &iconFile);
       
   246 
       
   247         // Commit registration changes
       
   248         LOG(EBackup, EInfo, "CommitNonNativeApplicationsUpdatesL");
       
   249         iApparcServer.CommitNonNativeApplicationsUpdatesL();
       
   250 
       
   251         delete tempstr;
       
   252         CleanupStack::PopAndDestroy(lWriter);
       
   253         CleanupStack::PopAndDestroy(&writeStream);
       
   254         CleanupStack::PopAndDestroy(writer);
       
   255         iconFile.Close();
       
   256         CleanupStack::PopAndDestroy(&iconFile);
       
   257 
       
   258         TInt err = iFs.Delete(tempPath);
       
   259         LOG1(EBackup, EInfo, "iFs.Delete err = %d", err);
       
   260         CleanupStack::PopAndDestroy(driveUnit);
       
   261     }
       
   262 
       
   263     CleanupStack::PopAndDestroy(&stream);
       
   264 }
       
   265 
       
   266 
       
   267 void CAppArcBackupUtil::DeregisterAppsL(TDriveNumber aDrive)
       
   268 {
       
   269     LOG1(EBackup, EInfo, "CAppArcBackupUtil::DeregisterApps, drive: %d", aDrive);
       
   270 
       
   271     // Initialise the process of getting apps
       
   272     iApparcServer.GetFilteredApps(TApaAppCapability::ENonNative,
       
   273                                   TApaAppCapability::ENonNative);
       
   274 
       
   275     // Getting the apps one-by-one
       
   276     TApaAppInfo* info = new(ELeave) TApaAppInfo();
       
   277     CleanupStack::PushL(info);
       
   278 
       
   279     TUid typeUid;
       
   280 
       
   281     // Prepare AppArc for deregistration
       
   282     LOG(EBackup, EInfo, "PrepareNonNativeApplicationsUpdatesL");
       
   283     iApparcServer.PrepareNonNativeApplicationsUpdatesL();
       
   284 
       
   285     while (!iApparcServer.GetNextApp((*info)))
       
   286     {
       
   287         LOG1(EBackup, EInfo, "Getting Next App from AppArc, UID = %d", info->iUid);
       
   288 
       
   289         // Getting the type of the application
       
   290         iApparcServer.GetAppType(typeUid, info->iUid);
       
   291 
       
   292         // If it's a MIDlet, check if it has an entry in Java Registry
       
   293         TInt drive = 0;
       
   294         GetDriveOfApp(info->iFullName, drive);
       
   295 
       
   296         if (typeUid == KMidletApplicationTypeUid && drive == aDrive)
       
   297         {
       
   298             LOG1(EBackup, EInfo, "Checking registry, midlet id = %d", info->iUid);
       
   299             wstring PackageID;
       
   300             // If there's no entry on the drive, deregister it from AppArc
       
   301             JavaStorageApplicationEntry_t appEntries = StorageEntryL(info->iUid);
       
   302 
       
   303             if (appEntries.size() != 0)
       
   304             {
       
   305                 JavaStorageEntry attribute;
       
   306                 attribute.setEntry(PACKAGE_ID, L"");
       
   307 
       
   308                 JavaStorageApplicationEntry_t::const_iterator findIterator = appEntries.find(attribute);
       
   309 
       
   310                 if (findIterator != appEntries.end())
       
   311                 {
       
   312                     PackageID = (*findIterator).entryValue();
       
   313                 }
       
   314                 else
       
   315                 {
       
   316                     User::Leave(KErrBadHandle);
       
   317                 }
       
   318 
       
   319                 if (GetDrivefromStorageL(PackageID) != aDrive)
       
   320                 {
       
   321                     iApparcServer.DeregisterNonNativeApplicationL(info->iUid);
       
   322                     LOG1(EBackup, EInfo, "Deregistering midlet id = %d", info->iUid);
       
   323                 }
       
   324             }
       
   325             else
       
   326             {
       
   327                 LOG(EBackup, EInfo, "Midlet entry not found in Java Storage; Deregister the Application");
       
   328                 iApparcServer.DeregisterNonNativeApplicationL(info->iUid);
       
   329                 LOG1(EBackup, EInfo, "Deregistering midlet id = %d", info->iUid);
       
   330             }
       
   331             appEntries.clear();
       
   332         }
       
   333     }
       
   334 
       
   335     // Commit registration changes
       
   336     LOG(EBackup, EInfo, "CommitNonNativeApplicationsUpdatesL");
       
   337     iApparcServer.CommitNonNativeApplicationsUpdatesL();
       
   338     CleanupStack::PopAndDestroy(info);
       
   339 }
       
   340 
       
   341 
       
   342 void CAppArcBackupUtil::GetMidletsFromAppArcL(RArray<TUid>& aUidArray, TDriveNumber aDrive)
       
   343 {
       
   344     LOG(EBackup, EInfo, "CAppArcBackupUtil::GetMidletsFromAppArc");
       
   345     CleanupClosePushL(aUidArray);
       
   346 
       
   347     // Initialise the process of getting apps
       
   348     iApparcServer.GetFilteredApps(TApaAppCapability::ENonNative,
       
   349                                   TApaAppCapability::ENonNative);
       
   350 
       
   351     // Getting the apps one-by-one
       
   352     TApaAppInfo* info = new(ELeave) TApaAppInfo();
       
   353     CleanupStack::PushL(info);
       
   354 
       
   355     TUid typeUid;
       
   356     LOG1(EBackup, EInfo, "Drive Number aDrive = %d", aDrive);
       
   357 
       
   358     while (!iApparcServer.GetNextApp((*info)))
       
   359     {
       
   360         // Getting the type of the application
       
   361         iApparcServer.GetAppType(typeUid, info->iUid);
       
   362 
       
   363         // If it's a MIDlet, check if it's registered to this drive
       
   364         if (typeUid == KMidletApplicationTypeUid)
       
   365         {
       
   366             JavaStorageApplicationEntry_t appEntries = StorageEntryL(info->iUid);
       
   367 
       
   368             if (appEntries.size() != 0)
       
   369             {
       
   370                 JavaStorageEntry attribute;
       
   371                 attribute.setEntry(PACKAGE_ID, L"");
       
   372 
       
   373                 JavaStorageApplicationEntry_t::const_iterator findIterator = appEntries.find(attribute);
       
   374                 wstring PackageID;
       
   375 
       
   376                 if (findIterator != appEntries.end())
       
   377                 {
       
   378                     PackageID = (*findIterator).entryValue();
       
   379 
       
   380                 }
       
   381                 else
       
   382                 {
       
   383                     User::Leave(KErrBadHandle);
       
   384                 }
       
   385                 appEntries.clear();
       
   386 
       
   387                 if (GetDrivefromStorageL(PackageID) == aDrive)
       
   388                 {
       
   389                     aUidArray.AppendL(info->iUid);
       
   390                     iNumberOfIcons++;
       
   391                 }
       
   392             }
       
   393             else
       
   394             {
       
   395                 ELOG(EBackup, "No entries from registry; midlet does not exist");
       
   396             }
       
   397         }
       
   398     }
       
   399     CleanupStack::PopAndDestroy(info);
       
   400     CleanupStack::Pop();
       
   401 
       
   402 }
       
   403 
       
   404 
       
   405 void CAppArcBackupUtil::GetIconFilename(TUid aUid, HBufC*& aFullFileName)
       
   406 {
       
   407     LOG(EBackup, EInfo, "CAppArcBackupUtil::GetIconFilename");
       
   408 
       
   409     iApparcServer.GetAppIcon(aUid, aFullFileName);
       
   410 }
       
   411 
       
   412 TInt CAppArcBackupUtil::NumberOfIcons()
       
   413 {
       
   414     LOG(EBackup, EInfo, "CAppArcBackupUtil::NumberOfIcons");
       
   415 
       
   416     return iNumberOfIcons;
       
   417 }
       
   418 
       
   419 void CAppArcBackupUtil::NumberOfOwnDefinedIcons(TUid aUid, TInt& aIconCount)
       
   420 {
       
   421     LOG(EBackup, EInfo, "CAppArcBackupUtil::NumberOfOwnDefinedIcons");
       
   422 
       
   423     iApparcServer.NumberOfOwnDefinedIcons(aUid, aIconCount);
       
   424 }
       
   425 
       
   426 TBool CAppArcBackupUtil::CheckIfAlreadyRegisteredL(TInt& aUid)
       
   427 {
       
   428     LOG(EBackup, EInfo, "CAppArcBackupUtil::CheckIfAlreadyRegisteredL");
       
   429 
       
   430     TApaAppInfo* info = new(ELeave) TApaAppInfo();
       
   431 
       
   432     TInt err = iApparcServer.GetAppInfo(*info, TUid::Uid(aUid));
       
   433     delete info;
       
   434     if (err == KErrNone)
       
   435     {
       
   436         return ETrue;
       
   437     }
       
   438     else
       
   439     {
       
   440         return EFalse;
       
   441     }
       
   442 
       
   443 
       
   444 }
       
   445 
       
   446 void CAppArcBackupUtil::GetDriveOfApp(TFileName aPath, TInt& aDrive)
       
   447 {
       
   448     LOG(EBackup, EInfo, "CAppArcBackupUtil::DriveOfApp");
       
   449 
       
   450     iFs.CharToDrive(aPath[0], aDrive);
       
   451 }
       
   452 
       
   453 
       
   454 TApaAppGroupName CAppArcBackupUtil::GetMidletGroupName(TUid aUid)
       
   455 {
       
   456     TApaAppCapabilityBuf cap;
       
   457 
       
   458     TInt err = iApparcServer.GetAppCapability(cap, aUid);
       
   459     return cap().iGroupName;
       
   460 }
       
   461 
       
   462 
       
   463 JavaStorageApplicationEntry_t CAppArcBackupUtil::StorageEntryL(const TUid& aMidletUID)
       
   464 {
       
   465     auto_ptr<JavaStorage> js(JavaStorage::createInstance());
       
   466 
       
   467     try
       
   468     {
       
   469         js->open(JAVA_DATABASE_NAME);
       
   470         LOG(EBackup, EInfo, "Java database opened");
       
   471     }
       
   472     catch (JavaStorageException jse)
       
   473     {
       
   474         ELOG(EBackup, "Opening database connection failed");
       
   475         User::Leave(KErrNotFound);
       
   476     }
       
   477 
       
   478     JavaStorageApplicationEntry_t entries;
       
   479 
       
   480     java::util::Uid appUid;
       
   481     TUidToUid(aMidletUID, appUid);
       
   482 
       
   483     try
       
   484     {
       
   485         js->read(APPLICATION_TABLE, appUid, entries);
       
   486     }
       
   487     catch (JavaStorageException jse)
       
   488     {
       
   489         ELOG(EBackup, "Read in Java Storage failed");
       
   490         js->close();
       
   491         User::Leave(KErrGeneral);
       
   492     }
       
   493     js->close();
       
   494 
       
   495     return entries;
       
   496 }
       
   497 
       
   498 TDriveNumber CAppArcBackupUtil::GetDrivefromStorageL(wstring& aPackageID)
       
   499 {
       
   500     wchar_t Drive;
       
   501     auto_ptr<JavaStorage> js(JavaStorage::createInstance());
       
   502 
       
   503     try
       
   504     {
       
   505         js->open(JAVA_DATABASE_NAME);
       
   506     }
       
   507     catch (JavaStorageException jse)
       
   508     {
       
   509         ELOG(EBackup, "Opening database connection failed");
       
   510         User::Leave(KErrNotFound);
       
   511     }
       
   512 
       
   513     JavaStorageApplicationEntry_t entries;
       
   514     java::util::Uid PackageID(aPackageID);
       
   515 
       
   516     try
       
   517     {
       
   518         js->read(APPLICATION_PACKAGE_TABLE, PackageID, entries);
       
   519     }
       
   520     catch (JavaStorageException jse)
       
   521     {
       
   522         ELOG(EBackup, "Read in Java Storage failed");
       
   523         js->close();
       
   524         User::Leave(KErrGeneral);
       
   525     }
       
   526 
       
   527     js->close();
       
   528 
       
   529     JavaStorageEntry attribute;
       
   530     attribute.setEntry(ROOT_PATH, L"");
       
   531 
       
   532     JavaStorageApplicationEntry_t::const_iterator findIterator = entries.find(attribute);
       
   533     wstring RootPath;
       
   534 
       
   535     if (findIterator != entries.end())
       
   536     {
       
   537         RootPath = (*findIterator).entryValue();
       
   538 
       
   539     }
       
   540     else
       
   541     {
       
   542         User::Leave(KErrBadHandle);
       
   543     }
       
   544 
       
   545     Drive = (wchar_t)RootPath[0];
       
   546     LOG1(EBackup, EInfo, "Drive = %c", Drive);
       
   547     TInt i = Drive - (wchar_t)'a';
       
   548     LOG1(EBackup, EInfo, "Drive = %d", (TDriveNumber)i);
       
   549     return (TDriveNumber)i;
       
   550 }