menufw/hierarchynavigator/hnmetadatamodel/src/hnutils.cpp
branchv5backport
changeset 14 1abc632eb502
parent 13 6205fd287e8a
child 20 636d517f67e6
equal deleted inserted replaced
13:6205fd287e8a 14:1abc632eb502
     1 /*
       
     2 * Copyright (c) 2007-2008 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:  
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <bautils.h>
       
    20 //#include <mnaiwservices.h>
       
    21 #include <liwservicehandler.h>
       
    22 #include <e32hashtab.h>
       
    23 #include <eikenv.h> 
       
    24 
       
    25 
       
    26 #include "hnutils.h"
       
    27 #include "hnliwutils.h"
       
    28 #include "hnmdservicecommand.h"
       
    29 #include "hnservicehandler.h"
       
    30 #include "hnglobals.h"
       
    31 #include "hnmdbasekey.h"
       
    32 #include "menudebug.h"
       
    33 #include "hnconvutils.h"
       
    34 
       
    35 // ======== MEMBER FUNCTIONS ========
       
    36 
       
    37 // ---------------------------------------------------------------------------
       
    38 // 
       
    39 // ---------------------------------------------------------------------------
       
    40 //
       
    41 EXPORT_C HBufC8* HnUtils::ReadFileLC(const TDesC& aPath)
       
    42     {
       
    43     RFs& fs = CEikonEnv::Static()->FsSession();
       
    44     
       
    45     RFile file;
       
    46     User::LeaveIfError( file.Open(fs, aPath, EFileRead) );
       
    47     CleanupClosePushL( file );
       
    48 
       
    49     TInt fileSize(0);
       
    50     User::LeaveIfError( file.Size( fileSize ) );
       
    51     HBufC8* buf = HBufC8::NewLC( fileSize );
       
    52     TPtr8 bufPtr( buf->Des() );
       
    53     User::LeaveIfError( file.Read( bufPtr ) );
       
    54 
       
    55     // clean up
       
    56     CleanupStack::Pop(buf);
       
    57     CleanupStack::PopAndDestroy(&file);
       
    58     CleanupStack::PushL(buf);
       
    59 
       
    60     return buf;
       
    61     }
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // 
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 EXPORT_C HBufC8* HnUtils::ReadFileL( const TDesC& aPath )
       
    68     {
       
    69     HBufC8* buf = ReadFileLC( aPath );
       
    70     CleanupStack::Pop( buf );
       
    71     return buf;
       
    72     }
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // 
       
    76 // ---------------------------------------------------------------------------
       
    77 //      
       
    78 EXPORT_C HBufC* HnUtils::LocateFileLC( const TDesC& aFile )
       
    79     {
       
    80     HBufC* filePath = HBufC::NewLC(KMaxName);
       
    81     RFs& fs = CEikonEnv::Static()->FsSession();
       
    82 
       
    83     TDriveList driveList;
       
    84     TChar driveLetter;
       
    85     TInt driveNumber=EDriveY;
       
    86     User::LeaveIfError(fs.DriveList(driveList));
       
    87     for(; driveNumber>=EDriveA-1; driveNumber-- )
       
    88         {
       
    89         if (driveNumber==EDriveA-1)
       
    90         driveNumber = EDriveZ;
       
    91         if (driveList[driveNumber])
       
    92             {
       
    93             User::LeaveIfError(fs.DriveToChar(driveNumber, driveLetter));
       
    94             filePath->Des().Zero();
       
    95             filePath->Des().Append(driveLetter);
       
    96             filePath->Des().Append(KRscPath);
       
    97             filePath->Des().Append(aFile);
       
    98 
       
    99             if (BaflUtils::FileExists(fs,*filePath))
       
   100                 {
       
   101                 return filePath;
       
   102                 }
       
   103             }
       
   104         if (driveNumber == EDriveZ)
       
   105         break;
       
   106         }
       
   107 
       
   108     User::Leave(KErrNotFound);
       
   109     return filePath;
       
   110     }
       
   111 
       
   112 // ---------------------------------------------------------------------------
       
   113 // 
       
   114 // ---------------------------------------------------------------------------
       
   115 //      
       
   116 EXPORT_C HBufC* HnUtils::LocateNearestLanguageFileLC( const TDesC& aFile )
       
   117     {
       
   118     _LIT( KExtRSC, ".r");
       
   119     if (aFile.Find( KExtRSC ) == (aFile.Length() - 4) )
       
   120         {
       
   121         HBufC* filePath = HBufC::NewLC(KMaxName);
       
   122         RFs& fs = CEikonEnv::Static()->FsSession();
       
   123         TDriveList driveList;
       
   124         TChar driveLetter;
       
   125         TInt driveNumber=EDriveY;
       
   126         User::LeaveIfError(fs.DriveList(driveList));
       
   127         TFileName file;
       
   128         for(; driveNumber>=EDriveA-1; driveNumber-- )
       
   129             {
       
   130             if (driveNumber==EDriveA-1)
       
   131             driveNumber = EDriveZ;
       
   132             if (driveList[driveNumber])
       
   133                 {
       
   134                 User::LeaveIfError(fs.DriveToChar(driveNumber, driveLetter));
       
   135                 filePath->Des().Zero();
       
   136                 filePath->Des().Append(driveLetter);
       
   137                 filePath->Des().Append(KRscPath);
       
   138                 filePath->Des().Append(aFile);
       
   139     
       
   140                 file = *filePath;
       
   141                 BaflUtils::NearestLanguageFile( fs, file);
       
   142                 if( BaflUtils::FileExists( fs, file))
       
   143                     {
       
   144                     CleanupStack::PopAndDestroy(filePath);    
       
   145                     return file.AllocLC();
       
   146                     }
       
   147                 }
       
   148             if (driveNumber == EDriveZ)
       
   149             break;
       
   150             }
       
   151         CleanupStack::PopAndDestroy(filePath);
       
   152         }
       
   153     return KNullDesC().AllocLC();
       
   154     }
       
   155 
       
   156 // ---------------------------------------------------------------------------
       
   157 // 
       
   158 // ---------------------------------------------------------------------------
       
   159 // 
       
   160 EXPORT_C void HnUtils::SetAttributeL( const TXmlEngElement& aElement,
       
   161         RBuf8& aAttribute )
       
   162     {
       
   163     TPtrC8 v = aElement.Value();
       
   164 
       
   165     if ( v.Length()> 0 )
       
   166         {
       
   167         HBufC8* trimmed = v.AllocLC();
       
   168         trimmed->Des().Trim();
       
   169         aAttribute.Close();
       
   170         aAttribute.CreateL( trimmed->Length() );
       
   171         aAttribute.Copy( *trimmed );
       
   172         CleanupStack::PopAndDestroy( trimmed );
       
   173         }
       
   174     }
       
   175 
       
   176 // ---------------------------------------------------------------------------
       
   177 // 
       
   178 // ---------------------------------------------------------------------------
       
   179 // 
       
   180 EXPORT_C void HnUtils::SetAttributeL( const TXmlEngElement& aElement,
       
   181         const TDesC8& aAttributeName, RBuf8& aValue8 )
       
   182     {
       
   183     if ( aElement.HasAttributeL( aAttributeName ) )
       
   184         {
       
   185         TPtrC8 value8 =
       
   186         aElement.AttributeNodeL( aAttributeName ).Value();
       
   187 
       
   188         aValue8.Close();
       
   189         aValue8.CreateL( value8.Length() );
       
   190         aValue8.Copy( value8 );
       
   191         }
       
   192     }
       
   193 
       
   194 // ---------------------------------------------------------------------------
       
   195 // 
       
   196 // ---------------------------------------------------------------------------
       
   197 // 
       
   198 EXPORT_C HBufC8 * HnUtils::GetColumnTextLC( TInt aColumn,
       
   199         const TDesC8 & aSourceText, TChar aColumnSeparator )
       
   200     {
       
   201     HBufC8 * ret = NULL;
       
   202     TLex8 processedText ( aSourceText );
       
   203 
       
   204     TInt column( 0 );
       
   205     processedText.Mark();
       
   206     
       
   207     while ( !processedText.Eos() )
       
   208         {
       
   209         if ( processedText.Get() == aColumnSeparator )
       
   210             {
       
   211             if ( column == aColumn )
       
   212                 {
       
   213                 processedText.UnGet();
       
   214                 break;
       
   215                 }
       
   216             processedText.Mark();
       
   217             column++;
       
   218             }
       
   219         }
       
   220 
       
   221     if ( aColumn <= column )
       
   222         {
       
   223         ret = processedText.MarkedToken().AllocLC();
       
   224         }
       
   225 
       
   226     return ret;
       
   227     }
       
   228 
       
   229 // ---------------------------------------------------------------------------
       
   230 // 
       
   231 // ---------------------------------------------------------------------------
       
   232 // 
       
   233 EXPORT_C TInt HnUtils::GetColumnTextColumnCount(
       
   234         const TDesC8 & aSourceText, TChar aColumnSeparator )
       
   235     {
       
   236     TInt ret(1);
       
   237     TLex8 processedText(aSourceText);
       
   238     while ( !processedText.Eos() )
       
   239         {
       
   240         if (processedText.Get() == aColumnSeparator)
       
   241             {
       
   242             ret++;
       
   243             }
       
   244         }
       
   245     return ret;
       
   246     }
       
   247