javacommons/javaenv/src.s60/javaenvinfo.cpp
changeset 21 2a9601315dfc
child 57 59b3b4473dc8
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 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:  CJavaEnvInfo implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <f32file.h>
       
    21 #include <pathinfo.h>
       
    22 
       
    23 #include <javaenvinfo.h>
       
    24 #include "logger.h"
       
    25 
       
    26 namespace Java
       
    27 {
       
    28 
       
    29 TInt ParseVersionL(TLex& aVersionText,TBool aLastVersionNumber);
       
    30 
       
    31 // User-agent header substring
       
    32 _LIT(KUserAgentJavaString, "Configuration/CLDC-1.1 Profile/MIDP-2.1");
       
    33 
       
    34 // About application Java version text
       
    35 _LIT(KAboutApplicationString, "MIDP 2.1, CLDC 1.1");
       
    36 
       
    37 // The path to resource file with java version
       
    38 _LIT(KMicroeditionJavaVersionFile, "\\resource\\versions\\java.txt");
       
    39 
       
    40 // Length of temporary buffer for string manipulation
       
    41 const TUint KBufferSize = 32;
       
    42 
       
    43 // The min and max values for each version number
       
    44 // const TUint KMajorVersionMinValue = 1;
       
    45 const TUint KMajorVersionMaxValue = 127;
       
    46 // const TUint KMinorVersionMaxValue = 9;
       
    47 // const TUint KBuildVersionMinValue = 1;
       
    48 // const TUint KBuildVersionMaxValue = 32767;
       
    49 
       
    50 
       
    51 // ======== MEMBER FUNCTIONS ========
       
    52 
       
    53 // ---------------------------------------------------------------------------
       
    54 // JavaEnvInfo::Version()
       
    55 //
       
    56 // The format of the version text in the version file is:
       
    57 //    <major> '.', <minor> '.' <build>
       
    58 //
       
    59 // @return Java environment version
       
    60 // ---------------------------------------------------------------------------
       
    61 EXPORT_C TVersion JavaEnvInfo::Version()
       
    62 {
       
    63     JELOG2(EUtils);
       
    64 
       
    65     TVersion version(0, 0, 0);
       
    66 
       
    67     // the error occured during reading a version file is ignored
       
    68     TRAP_IGNORE(version = GetJavaVersionL());
       
    69 
       
    70     LOG3(EUtils,EInfo," JavaEnvInfo::Version: major = %d, minor = %d, build = %d",
       
    71          version.iMajor, version.iMinor, version.iBuild);
       
    72 
       
    73     return version;
       
    74 }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 // JavaEnvInfo::GetUserAgentHeaderL returns the user agent header part.
       
    78 //
       
    79 // @return Java related user agent header substring
       
    80 // ---------------------------------------------------------------------------
       
    81 EXPORT_C HBufC* JavaEnvInfo::GetUserAgentHeaderL()
       
    82 {
       
    83     JELOG2(EUtils);
       
    84     HBufC* userAgentHeader = KUserAgentJavaString().AllocL();
       
    85     return userAgentHeader;
       
    86 }
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 // JavaEnvInfo::GetUserAgentHeaderL just returns the hardcoded literal. In
       
    90 // the future, this might be read from a resource file if there is need for
       
    91 // localization.
       
    92 //
       
    93 // @return About application text
       
    94 // ---------------------------------------------------------------------------
       
    95 EXPORT_C HBufC* JavaEnvInfo::GetPlatformInfoL()
       
    96 {
       
    97     JELOG2(EUtils);
       
    98     HBufC* aboutString = KAboutApplicationString().AllocL();
       
    99     return aboutString;
       
   100 }
       
   101 
       
   102 // ---------------------------------------------------------------------------
       
   103 // JavaEnvInfo::GetJavaVersionL()
       
   104 //
       
   105 // The format of the version text in the version file is:
       
   106 //    <major> '.', <minor> '.' <build>
       
   107 //
       
   108 // The version file can be either in C-drive or in ROM (Z-drive). All the
       
   109 // other locations are ignored. This means that when updating the Java
       
   110 // environment, the new text file must be copied to C-drive.
       
   111 // ---------------------------------------------------------------------------
       
   112 TVersion JavaEnvInfo::GetJavaVersionL()
       
   113 {
       
   114     JELOG2(EUtils);
       
   115 
       
   116     TInt size;
       
   117 
       
   118     RFs rfs;
       
   119     User::LeaveIfError(rfs.Connect());
       
   120     CleanupClosePushL(rfs);
       
   121 
       
   122     RFile file;
       
   123     //first try read the version information from C: drive
       
   124 
       
   125     LOG(EUtils,EInfo,"GetJavaVersionL: reading file from C: drive");
       
   126     TFileName alternateFile = TParsePtrC(
       
   127                                   PathInfo::PhoneMemoryRootPath()).Drive();
       
   128     alternateFile.Append(KMicroeditionJavaVersionFile);
       
   129     TInt ret = file.Open(rfs, alternateFile , EFileRead);
       
   130 
       
   131     if (ret != KErrNone)
       
   132     {
       
   133         LOG(EUtils,EInfo, " GetJavaVersionL: reading file from Z: drive");
       
   134 
       
   135         // if the version information file is not in C:-drive (KErrPathNotFound,
       
   136         // KErrNotFound) or can not be accessed for another reason, version
       
   137         // information is read from Z:-drive
       
   138         TFileName defaultFile = TParsePtrC(
       
   139                                     PathInfo::RomRootPath()).Drive();
       
   140         defaultFile.Append(KMicroeditionJavaVersionFile);
       
   141         User::LeaveIfError(file.Open(rfs, defaultFile, EFileRead));
       
   142 
       
   143         LOG(EUtils,EInfo, " GetJavaVersionL: reading file from Z: drive");
       
   144     }
       
   145 
       
   146     CleanupClosePushL(file);
       
   147 
       
   148     User::LeaveIfError(file.Size(size));
       
   149     if (size > KBufferSize)
       
   150     {
       
   151         size = KBufferSize;
       
   152     }
       
   153 
       
   154     HBufC8* buffer = HBufC8::NewLC(size);
       
   155     TPtr8 des = buffer->Des();
       
   156     User::LeaveIfError(file.Read(des, size));
       
   157 
       
   158     HBufC16* buffer16 = HBufC16::NewLC(size);
       
   159     buffer16->Des().Copy(des);
       
   160 
       
   161     // parse the version numbers (major, minor, build) from the string
       
   162     // leave if  error occurs in string or in its format (e.g.: missing dot char, wrong value)
       
   163     TLex versionText(buffer16->Des());
       
   164     TUint32 majorVersion = ParseVersionL(versionText,EFalse);
       
   165     TUint32 minorVersion = ParseVersionL(versionText,EFalse);
       
   166     TUint32 buildVersion = ParseVersionL(versionText,ETrue);
       
   167 
       
   168     TVersion version(majorVersion, minorVersion, buildVersion);
       
   169 
       
   170     CleanupStack::PopAndDestroy(buffer16);
       
   171     CleanupStack::PopAndDestroy(buffer);
       
   172     CleanupStack::PopAndDestroy(&file);
       
   173     CleanupStack::PopAndDestroy(&rfs);
       
   174 
       
   175     return version;
       
   176 }
       
   177 
       
   178 //
       
   179 //
       
   180 //
       
   181 TInt ParseVersionL(TLex& aVersionText,TBool aLastVersionNumber)
       
   182 {
       
   183     JELOG2(EUtils);
       
   184 
       
   185     aVersionText.Mark();
       
   186     while ((aVersionText.Peek()).IsDigit())
       
   187     {
       
   188         // move to next char
       
   189         aVersionText.Inc();
       
   190     }
       
   191     TLex partialVersionText(aVersionText.MarkedToken());
       
   192     TUint32 version(0);
       
   193     if (partialVersionText.BoundedVal(version,
       
   194                                       EDecimal,
       
   195                                       Java::KMajorVersionMaxValue) != KErrNone)
       
   196     {
       
   197         LOG(EUtils,EInfo, " GetJavaVersionL: format of value is corrupted (major version)");
       
   198         User::Leave(KErrCorrupt);
       
   199     }
       
   200     // check format
       
   201     if (!aLastVersionNumber)
       
   202     {
       
   203         if (aVersionText.Peek() == '.')
       
   204         {
       
   205             // move to the next char (should be number)
       
   206             aVersionText.Inc();
       
   207         }
       
   208         else
       
   209         {
       
   210             LOG(EUtils,EInfo, " GetJavaVersionL: format of value is corrupted");
       
   211             User::Leave(KErrCorrupt);
       
   212         }
       
   213     }
       
   214     return version;
       
   215 }
       
   216 
       
   217 }
       
   218