csxhelp/HelpEngine/src/CSXHRuntimeIndexing.cpp
changeset 0 1f04cf54edd8
child 5 d06b1526f62c
equal deleted inserted replaced
-1:000000000000 0:1f04cf54edd8
       
     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:  CCSXHRuntimeIndexing class definition
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <pathinfo.h>
       
    20 #include <utf.h>
       
    21 #include <bautils.h>
       
    22 #include <eikenv.h>
       
    23 #include <aknnotedialog.h> // for CAknNoteDialog
       
    24 #include <aknnotewrappers.h>
       
    25 #include <SenXmlUtils.h>
       
    26 #include <SenXmlConstants.h>
       
    27 
       
    28 #include "CSXHRuntimeIndexing.h"
       
    29 #include "CSXHHtmlTOC1.h"
       
    30 #include "csxhconstants.h"
       
    31 
       
    32 // buffer length of Drive information in index file
       
    33 //
       
    34 const TInt KMaxDriveInfo = 100;
       
    35 
       
    36 CCSXHRuntimeIndexing::CCSXHRuntimeIndexing()
       
    37     {
       
    38     iCoeEnv = CCoeEnv::Static();
       
    39     }
       
    40     
       
    41 CCSXHRuntimeIndexing::~CCSXHRuntimeIndexing()
       
    42     {
       
    43     delete iFullDirName;
       
    44     iFile.Close();
       
    45     }
       
    46     
       
    47 CCSXHRuntimeIndexing* CCSXHRuntimeIndexing::NewL()
       
    48     {
       
    49     CCSXHRuntimeIndexing* self = new ( ELeave ) CCSXHRuntimeIndexing();
       
    50     CleanupStack::PushL( self );
       
    51     self->ConstructL();
       
    52     CleanupStack::Pop( self );
       
    53     return self;
       
    54     }
       
    55     
       
    56 void CCSXHRuntimeIndexing::ConstructL()
       
    57     {
       
    58     RFs& fileSession = iCoeEnv->FsSession();
       
    59     iFullDirName = HBufC::NewL( KMaxFileName );
       
    60 
       
    61     TFileName fullDirName;
       
    62     
       
    63     GetPrivatePath( fullDirName );
       
    64     if ( !BaflUtils::PathExists( fileSession, fullDirName ) )
       
    65         {
       
    66         fileSession.CreatePrivatePath( EDriveC );
       
    67         }
       
    68     
       
    69     TInt len = fullDirName.Length();
       
    70     TDriveList dirList; 
       
    71     if( fileSession.DriveList( dirList ) == KErrNone )
       
    72         {
       
    73         TDriveInfo info;
       
    74         TInt driveListLen = dirList.Length();
       
    75         
       
    76         for ( TInt i = 0; i < driveListLen; i++ )
       
    77             {
       
    78             // make separate folder for C/D/E/... drive
       
    79             // e.g. C:\\private\\10005234\\C\\
       
    80 
       
    81             TInt err = fileSession.Drive( info, i );
       
    82             if ( err == KErrNone &&
       
    83                     info.iType != EMediaNotPresent )
       
    84                 {
       
    85                 TChar driveChar;
       
    86                 RFs::DriveToChar( i, driveChar );
       
    87                 fullDirName.Append( driveChar );
       
    88                 fullDirName.Append( KFwdSlash );
       
    89                 fileSession.MkDir( fullDirName );
       
    90                 fullDirName.Delete( len, 3 );
       
    91                 }
       
    92             }
       
    93         
       
    94         }
       
    95     }
       
    96     
       
    97 TBool CCSXHRuntimeIndexing::IndexFileExists()
       
    98     {
       
    99     RFs& fileSession = iCoeEnv->FsSession();
       
   100     TBuf<KMaxFileName> fileName;
       
   101     GetPrivatePath( fileName );
       
   102     fileName.Append( _L("Z\\") );
       
   103     AppendLocale( fileName );
       
   104     fileName.Append( KMasterMetaFile );
       
   105     
       
   106     // Now the dirName is C:\\private\\<app-uid>\\Z\\index.xml
       
   107     // check if it exists, if yes, runtime indexing must be launched before
       
   108     // since Z based Help content must be delivered with cell phone
       
   109     //
       
   110     return BaflUtils::FileExists( fileSession, fileName );
       
   111     
       
   112     }
       
   113     
       
   114 void CCSXHRuntimeIndexing::BeginIndexFile( const TChar& aDrive )
       
   115     {
       
   116     RFs& fileSession = iCoeEnv->FsSession();
       
   117     TPtr des = iFullDirName->Des();
       
   118     iDriveLetter = aDrive;
       
   119     des.Zero();
       
   120     GetPrivatePath( des );
       
   121     des.Append( aDrive );
       
   122     des.Append( KFwdSlash );
       
   123     AppendLocale( des );
       
   124     if ( !BaflUtils::PathExists( fileSession, des ) )
       
   125         {
       
   126         // generate C:\\private\\<help-uid>\\<aDrive>\\ folder
       
   127         //
       
   128         fileSession.MkDir( des );
       
   129         }
       
   130     
       
   131     // for temporary usage, once it's finished successfully,
       
   132     // rename to index.xml. otherwise, the old index file still exists.
       
   133     //
       
   134     des.Append( KMasterMetaTmpFile );
       
   135     iFile.Replace( fileSession, des, EFileWrite );
       
   136     
       
   137     HBufC8* driveInfo = HBufC8::NewLC( KMaxDriveInfo );
       
   138     TPtr8 ptr = driveInfo->Des();
       
   139     ptr.Append( KIndexXMLVesion );
       
   140     ptr.Append( _L( "<collections drive=\"" ) );
       
   141     ptr.Append( aDrive);
       
   142     ptr.Append( _L( "\">\r\n" ) );
       
   143     iFile.Write( *driveInfo );
       
   144     CleanupStack::PopAndDestroy( driveInfo );
       
   145     }
       
   146     
       
   147 void CCSXHRuntimeIndexing::FinishAndCloseIndexFileL()
       
   148     {
       
   149     RFs& fileSession = iCoeEnv->FsSession();
       
   150     iFile.Write( KIndexCollectEnd );
       
   151     iFile.Close();  
       
   152       
       
   153     TTime time;
       
   154     // Replace the index.xml with temporary file, in case it fails, will have no impact
       
   155     // the original index.xml, it's a backup solution
       
   156     //
       
   157     TBuf<KMaxFileName> newName;
       
   158     TInt err = KErrNone;
       
   159     newName.Copy( *iFullDirName );
       
   160     TInt pos = newName.LocateReverse( TChar('\\') );
       
   161     newName.Delete( pos + 1, newName.Length() - pos - 1 ); // remove the temp name
       
   162     newName.Append( KMasterMetaFile ); //  and append index.xml
       
   163     
       
   164     err = fileSession.Replace( *iFullDirName, newName );
       
   165     
       
   166     if ( err == KErrNone )
       
   167         {
       
   168         // set the modified date of master index.xml
       
   169         // to be the same with related help content folder
       
   170         // e.g. C:\\resource\\xhtml\\01\\ modified time is 2009.3.1
       
   171         // C:\\private\\10005234\\C\\01\\index.xml will be 2009.3.1
       
   172         // this will make sure once help content folder changed, index.xml can be
       
   173         // determined to change.
       
   174         TBuf<KMaxFileName> helpDir;
       
   175         helpDir.Append( iDriveLetter );  
       
   176         helpDir.Append( KInstallPath );
       
   177         // append locale information
       
   178         AppendLocale( helpDir );
       
   179         
       
   180         fileSession.Modified( helpDir, time );
       
   181         err = fileSession.SetModified( newName, time );
       
   182         
       
   183         }
       
   184     
       
   185     }
       
   186       
       
   187 void CCSXHRuntimeIndexing::RuntimeGenerateIndexL( const CCSXHHtmlTOC1& aToc1, const TDesC& aFeature )
       
   188     {
       
   189     // Form into an entry, like
       
   190     // <collection FeatureID="-1" id="0x10005951" navtitle="Bluetooth"></collection>
       
   191     HBufC8* appName = CnvUtfConverter::ConvertFromUnicodeToUtf8L( aToc1.GetName() );
       
   192     CleanupStack::PushL(appName);
       
   193     TRAPD( err, SenXmlUtils::LeaveOnXmlEscapesL( *appName ) );
       
   194     TInt length = KMaxIndexEntryExclude;
       
   195     TBuf8<KMaxUidLength> buffUid;
       
   196     
       
   197     buffUid.Append( KHexPrefix );
       
   198     buffUid.AppendNumFixedWidth( aToc1.GetAppUid().iUid, EHex, 8 );
       
   199     length += appName->Length();
       
   200     length += aFeature.Length();
       
   201     HBufC8* entry = HBufC8::NewLC( length );
       
   202     TPtr8 des = entry->Des();
       
   203     // Append entry tag
       
   204     des.Append( KIndexXMLEntryBegin );
       
   205     // Append feature id attribute "FeatureID"
       
   206     des.Append( KAppFeatureIDTag );
       
   207     des.Append( KIndexQuoteBegin );
       
   208     // Append feature id
       
   209     des.Append( aFeature );
       
   210     des.Append( KIndexQuoteEnd );
       
   211     // Append app id attribute "id"
       
   212     des.Append( KMasterCollection_idTag );
       
   213     des.Append( KIndexQuoteBegin );
       
   214     // Append app id
       
   215     des.Append( buffUid );
       
   216     des.Append( KIndexQuoteEnd );
       
   217     // Append app name attribut "navtitle"
       
   218     des.Append( KTOC2NameTag );
       
   219     des.Append( KIndexQuoteBegin );
       
   220     if ( err == KErrSenInvalidCharacters )
       
   221         {
       
   222         // special handling to the XML-Escaped char
       
   223         HBufC8* newElement = SenXmlUtils::EncodeHttpCharactersLC( *appName );
       
   224         des.Append( *newElement );
       
   225         CleanupStack::PopAndDestroy( newElement );
       
   226         }
       
   227     else
       
   228         {
       
   229         des.Append( *appName );
       
   230         }
       
   231 
       
   232     des.Append( KIndexXMLEntryEnd );
       
   233     iFile.Write( *entry );
       
   234     
       
   235     CleanupStack::PopAndDestroy( entry );
       
   236     CleanupStack::PopAndDestroy( appName );
       
   237     
       
   238     }
       
   239     
       
   240 void CCSXHRuntimeIndexing::GetPrivatePath( TDes& aName )
       
   241     {
       
   242     RFs& fileSession = iCoeEnv->FsSession();
       
   243     TBuf<KMaxFileName> privatePath;
       
   244     TChar drive;
       
   245     RFs::DriveToChar( EDriveC, drive );
       
   246     
       
   247     fileSession.PrivatePath( privatePath );
       
   248     aName.Append( drive );
       
   249     aName.Append( TChar(':') ); 
       
   250     aName.Append( privatePath );
       
   251     }
       
   252 
       
   253 void CCSXHRuntimeIndexing::AppendLocale( TDes& aDir )
       
   254     {
       
   255     RFs& fileSession = iCoeEnv->FsSession();
       
   256     RArray<TLanguage> langs;
       
   257     BaflUtils::GetDowngradePathL( fileSession, User::Language(), langs );
       
   258     TInt len = aDir.Length();
       
   259     
       
   260     if ( len > 0 )
       
   261         {
       
   262         TLanguage lang = langs[0];
       
   263         if(lang < 10)
       
   264             {
       
   265             aDir.AppendNumFixedWidth( lang, EDecimal,2 );
       
   266             }
       
   267         else
       
   268             {
       
   269             aDir.AppendNum( lang );
       
   270             }
       
   271         }
       
   272     else
       
   273         {
       
   274         // rarely case, if no current language, use English
       
   275         aDir.Append( _L("01") );
       
   276         }
       
   277         
       
   278     aDir.Append( KFwdSlash );
       
   279     langs.Reset();
       
   280     }
       
   281     
       
   282 void CCSXHRuntimeIndexing::DeleteIndexFile( const TChar& aDrive )
       
   283     {
       
   284     RFs& fileSession = iCoeEnv->FsSession();
       
   285     TBuf<KMaxFileName> des;
       
   286     GetPrivatePath( des );
       
   287     des.Append( aDrive );
       
   288     des.Append( KFwdSlash );
       
   289     AppendLocale( des );
       
   290     // C:\\private\\<app-uid>\\<drive letter>\\<language-id>\\index.xml
       
   291     des.Append( KMasterMetaFile );
       
   292     
       
   293     if ( BaflUtils::FileExists( fileSession, des ) )
       
   294         {
       
   295         fileSession.Delete( des );
       
   296         }
       
   297 
       
   298     }
       
   299     
       
   300 TInt CCSXHRuntimeIndexing::CheckDirChange( const TChar& aDrive )
       
   301     {
       
   302     RFs& fsSession = iCoeEnv->FsSession();
       
   303     
       
   304     // Begin to search help content folder to see if there are date modify
       
   305     // meanwhile, compare to the related folder in private directory
       
   306     TFileName dirName;
       
   307     dirName.Append( aDrive );
       
   308     dirName.Append( KInstallPath );
       
   309 
       
   310     // Begin to append locale, note this will append current locale,
       
   311     // thru which, app can determine if it's caused by user changed locales
       
   312     AppendLocale( dirName );
       
   313 
       
   314     TTime dirModified(0);
       
   315     TTime fileModified(0);
       
   316     
       
   317     if ( BaflUtils::PathExists( fsSession, dirName ) )
       
   318         {
       
   319         // here is modified time of help content folder
       
   320         fsSession.Modified( dirName, dirModified );
       
   321         }
       
   322     else
       
   323         {
       
   324         // fast return, no need to go on checking
       
   325         return KNoDirExist;
       
   326         }
       
   327 
       
   328     TFileName filename;
       
   329     GetPrivatePath( filename );
       
   330     filename.Append( aDrive );
       
   331     filename.Append( KFwdSlash );
       
   332     AppendLocale( filename );
       
   333     filename.Append( KMasterMetaFile );
       
   334                 
       
   335     if ( BaflUtils::FileExists( fsSession, filename ) )
       
   336         {
       
   337         // here is modified time of index.xml
       
   338         //
       
   339         fsSession.Modified( filename, fileModified );
       
   340         }
       
   341     else
       
   342         {
       
   343         return KNoIndexExist;
       
   344         }
       
   345                     
       
   346     if ( fileModified != dirModified )
       
   347         {
       
   348         // folder changed, return it
       
   349         TInt ret = KErrNotFound;
       
   350         RFs::CharToDrive( aDrive, ret );
       
   351         return ret;
       
   352         }
       
   353     
       
   354     return KNoChange;
       
   355     }