omads/omadsextensions/adapters/bookmark/src/omadsfolderobject.cpp
changeset 40 b63e67867dcd
equal deleted inserted replaced
39:9905f7d46607 40:b63e67867dcd
       
     1 /*
       
     2 * Copyright (c) 2005-2007 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:  OMA DS Folder object XML parser
       
    15 *
       
    16 */
       
    17 
       
    18 #include <SenXmlUtils.h>
       
    19 #include <SenXmlReader.h>
       
    20 #include <favouriteslimits.h>   // Localized names for folders
       
    21 #include <bookmarkdsa.rsg>
       
    22 #include <stringresourcereader.h>
       
    23 #include <bautils.h>
       
    24 #include <utf.h>
       
    25 #include <feedsserverfolderitem.h>
       
    26 
       
    27 #include "bookmarkdataproviderdefs.h"
       
    28 #include "omadsfolderobject.h"
       
    29 #include "logger.h"
       
    30 
       
    31 _LIT( KOMADSResourceName, "\\resource\\bookmarkdsa.rsc" );
       
    32 
       
    33 _LIT8(KOMADSFolderElement, "Folder");
       
    34 _LIT8(KOMADSNameElement, "name");
       
    35 _LIT8(KOMADSCreatedElement, "created");
       
    36 _LIT8(KOMADSModifiedElement, "modified");
       
    37 
       
    38 _LIT(KFolderBeginTag, "<Folder>");
       
    39 _LIT(KFolderEndTag, "</Folder>");
       
    40 _LIT(KNameBeginTag, "<name>");
       
    41 _LIT(KNameEndTag, "</name>");
       
    42 _LIT(KCreatedBeginTag, "<created>");
       
    43 _LIT(KCreatedEndTag, "</created>");
       
    44 _LIT(KModifiedBeginTag, "<modified>");
       
    45 _LIT(KModifiedEndTag, "</modified>");
       
    46 const TInt KDateFormatLength = 15;
       
    47 _LIT(KDateFormat, "%04d%02d%02dT%02d%02d%02d");
       
    48 
       
    49 
       
    50 COMADSFolderObject* COMADSFolderObject::NewLC()
       
    51     {
       
    52     COMADSFolderObject* pSelf = new (ELeave) COMADSFolderObject;
       
    53     CleanupStack::PushL(pSelf);
       
    54     pSelf->ConstructL();
       
    55     return pSelf;
       
    56     }
       
    57 
       
    58 COMADSFolderObject* COMADSFolderObject::NewL()
       
    59     {
       
    60     COMADSFolderObject* pSelf = COMADSFolderObject::NewLC();
       
    61     CleanupStack::Pop( pSelf );    
       
    62     return pSelf;
       
    63     }
       
    64 
       
    65 COMADSFolderObject::~COMADSFolderObject()
       
    66     {
       
    67     delete iXmlReader;
       
    68     }
       
    69 
       
    70 void COMADSFolderObject::ConstructL()
       
    71     {
       
    72     iXmlReader = CSenXmlReader::NewL();
       
    73     iXmlReader->SetContentHandler(*this);
       
    74     }
       
    75 
       
    76 TInt COMADSFolderObject::ImportFolderXml( RFs& aFs, const TDesC& aFilename )
       
    77     {
       
    78     LOGGER_ENTERFN( "COMADSFolderObject::ImportFolderXml" );    
       
    79         
       
    80     TInt error;
       
    81     
       
    82     Reset();
       
    83     TRAP( error, iXmlReader->ParseL( aFs, aFilename ) )
       
    84     if( error != KErrNone )
       
    85         {
       
    86         LOGGER_LEAVEFN( "COMADSFolderObject::ImportFolderXml" );
       
    87         return error;
       
    88         }
       
    89     
       
    90     LOGGER_LEAVEFN( "COMADSFolderObject::ImportFolderXml" );
       
    91     return iError;
       
    92     }
       
    93     
       
    94 TInt COMADSFolderObject::ImportFolderXml( const TDesC8& aData )
       
    95     {
       
    96     LOGGER_ENTERFN( "COMADSFolderObject::ImportFolderXml" ); 
       
    97     
       
    98     TInt error;
       
    99     
       
   100     Reset();
       
   101     
       
   102     TRAP(error, iXmlReader->ParseL(aData) );
       
   103     if(error != KErrNone )
       
   104         {
       
   105         LOGGER_LEAVEFN( "COMADSFolderObject::ImportFolderXml" );
       
   106         return error;
       
   107         }
       
   108     LOGGER_LEAVEFN( "COMADSFolderObject::ImportFolderXml" );
       
   109     return iError;
       
   110     }
       
   111     
       
   112 void COMADSFolderObject::ExportFolderXmlL( CBufBase& aBuffer )
       
   113     {
       
   114     iDesc = NULL;
       
   115     iBuffer = &aBuffer;
       
   116     iWriteBufPosition = 0;
       
   117     iWriteBufSize = aBuffer.Size();     
       
   118 
       
   119     ExportL();
       
   120     }
       
   121     
       
   122 void COMADSFolderObject::ExportFolderXmlL( TDes8& aBuffer )
       
   123     {
       
   124     iDesc = &aBuffer;
       
   125     iBuffer = NULL;
       
   126     
       
   127     ExportL();
       
   128     }
       
   129     
       
   130 void COMADSFolderObject::ExportL()
       
   131     {
       
   132     // Folder
       
   133     WriteL( KFolderBeginTag );
       
   134     
       
   135     // Name
       
   136     WriteL( KNameBeginTag );
       
   137     WriteEncodedXmlL( iName );
       
   138     WriteL( KNameEndTag );
       
   139     
       
   140     // Created Date
       
   141     TBuf<KDateFormatLength> tempdate;
       
   142     tempdate.Format( KDateFormat, iCreatedDate.Year(), iCreatedDate.Month() + 1,
       
   143         iCreatedDate.Day(), iCreatedDate.Hour(), iCreatedDate.Minute(), iCreatedDate.Second() );
       
   144     
       
   145     WriteL( KCreatedBeginTag );
       
   146     WriteL( tempdate );
       
   147     WriteL( KCreatedEndTag );
       
   148     
       
   149     // Modified Date
       
   150     tempdate.Format( KDateFormat, iModifiedDate.Year(), iModifiedDate.Month() + 1,
       
   151         iModifiedDate.Day(), iModifiedDate.Hour(), iModifiedDate.Minute(), iModifiedDate.Second() );
       
   152     
       
   153     WriteL( KModifiedBeginTag );
       
   154     WriteL( tempdate );
       
   155     WriteL( KModifiedEndTag );
       
   156     
       
   157     // Folder end
       
   158     WriteL(KFolderEndTag);
       
   159     }
       
   160     
       
   161 void COMADSFolderObject::WriteL( const TDesC &aData )
       
   162     {   
       
   163     CnvUtfConverter converter;
       
   164     User::LeaveIfError( converter.ConvertFromUnicodeToUtf8( iTemp, aData ) );
       
   165                     
       
   166     if ( iBuffer )
       
   167         {
       
   168         TInt newPosition = iWriteBufPosition + iTemp.Length();
       
   169 
       
   170         if ( newPosition > iWriteBufSize )
       
   171             {
       
   172             TInt expand = newPosition - iWriteBufSize;
       
   173             iBuffer->ExpandL( iWriteBufSize, expand );
       
   174             iWriteBufSize += expand;
       
   175             }
       
   176 
       
   177         iBuffer->Write( iWriteBufPosition, iTemp );
       
   178         iWriteBufPosition = newPosition;
       
   179         }
       
   180     else if( iDesc )
       
   181         {
       
   182         iDesc->Append( iTemp );       
       
   183         }   
       
   184     }
       
   185     
       
   186 void COMADSFolderObject::WriteEncodedXmlL( const TDesC &aData )
       
   187     {   
       
   188     CnvUtfConverter converter;
       
   189     User::LeaveIfError( converter.ConvertFromUnicodeToUtf8( iTemp, aData ) );
       
   190     
       
   191     HBufC8* xmlData = SenXmlUtils::EncodeHttpCharactersLC( iTemp ); 
       
   192                     
       
   193     if ( iBuffer )
       
   194         {
       
   195         TInt newPosition = iWriteBufPosition + xmlData->Length();
       
   196 
       
   197         if ( newPosition > iWriteBufSize )
       
   198             {
       
   199             TInt expand = newPosition - iWriteBufSize;
       
   200             iBuffer->ExpandL( iWriteBufSize, expand );
       
   201             iWriteBufSize += expand;
       
   202             }
       
   203 
       
   204         iBuffer->Write( iWriteBufPosition, *xmlData );
       
   205         iWriteBufPosition = newPosition;
       
   206         }
       
   207     else if( iDesc )
       
   208         {
       
   209         iDesc->Append( *xmlData );
       
   210         }   
       
   211         
       
   212     CleanupStack::PopAndDestroy( xmlData );   
       
   213     }           
       
   214     
       
   215 TInt COMADSFolderObject::StartDocument()
       
   216     {
       
   217     return KErrNone;
       
   218     }
       
   219 
       
   220 TInt COMADSFolderObject::EndDocument()
       
   221     {
       
   222     return KErrNone;
       
   223     }
       
   224 
       
   225 TInt COMADSFolderObject::StartElement(const TDesC8& /*aURI*/, 
       
   226                   const TDesC8& /*aLocalName*/, 
       
   227                   const TDesC8& aName, 
       
   228                   const RAttributeArray& /*apAttrs*/)
       
   229     {
       
   230     LOGGER_ENTERFN( "COMADSFolderObject::StartElement" );
       
   231     iCurrentElement.Copy( aName );
       
   232     LOGGER_LEAVEFN( "COMADSFolderObject::StartElement" );
       
   233     return KErrNone;
       
   234     }
       
   235                                     
       
   236 TInt COMADSFolderObject::EndElement( const TDesC8& /*aURI*/, 
       
   237     const TDesC8& /*aLocalName*/, const TDesC8& /*aName*/ )
       
   238     {
       
   239     LOGGER_ENTERFN( "COMADSFolderObject::EndElement" );        
       
   240     iCurrentElement.SetLength(0);
       
   241     LOGGER_LEAVEFN( "COMADSFolderObject::EndElement" );
       
   242     return KErrNone;
       
   243     }
       
   244 
       
   245 TInt COMADSFolderObject::Characters( const TDesC8& aBuf, TInt aStart, TInt aLength )
       
   246     {
       
   247     LOGGER_ENTERFN( "COMADSFolderObject::Characters" );
       
   248     
       
   249     TInt error;
       
   250     
       
   251     if( iCurrentElement.Compare( KOMADSFolderElement ) == 0 )
       
   252         {
       
   253         LOGGER_LEAVEFN( "COMADSFolderObject::Characters" );
       
   254         return KErrNone;
       
   255         }
       
   256     
       
   257     else if ( iCurrentElement.Compare( KOMADSNameElement ) == 0 )
       
   258         {                
       
   259         CnvUtfConverter converter;
       
   260         TBuf<KMaxFolderNameLength> buf;
       
   261         error = converter.ConvertToUnicodeFromUtf8( buf, aBuf.Mid( aStart, aLength ) );
       
   262         TInt length = buf.Length() + iName.Length();
       
   263         
       
   264         if ( error > 0 )
       
   265             {
       
   266             LOGGER_WRITE_1("Too long name, number of uncorverted bytes: %d", error);
       
   267             }
       
   268         else if ( error != KErrNone )
       
   269             {
       
   270             LOGGER_WRITE_1("ConvertToUnicodeFromUtf8 failed with %d", error);
       
   271             iError = error;
       
   272             }
       
   273         else if ( length > iName.MaxLength() )
       
   274             {
       
   275             LOGGER_WRITE_1("Too long name total: %d", length);
       
   276             }
       
   277         else
       
   278             {
       
   279             iName.Append( buf );
       
   280             }       
       
   281         }
       
   282     else if ( iCurrentElement.Compare( KOMADSCreatedElement ) == 0 )
       
   283         {
       
   284         error = ParseDateString( aBuf.Mid( aStart, aLength ), iCreatedDate );
       
   285         if( error != KErrNone )
       
   286             {
       
   287             iError = error;
       
   288             }
       
   289         }
       
   290     else if ( iCurrentElement.Compare( KOMADSModifiedElement ) == 0 )
       
   291         {
       
   292         error = ParseDateString( aBuf.Mid( aStart, aLength ), iModifiedDate );
       
   293         if ( error != KErrNone )
       
   294             {
       
   295             iError = error;
       
   296             }
       
   297         }
       
   298     LOGGER_LEAVEFN( "COMADSFolderObject::Characters" );
       
   299     return KErrNone;
       
   300     }
       
   301 
       
   302 TInt COMADSFolderObject::ParseDateString(const TDesC8& aString, TDateTime& aDateTime)
       
   303     {
       
   304     // Ensure we don't read beyond the buffer limits
       
   305     if ( aString.Length() < KDateFormatLength )
       
   306         {
       
   307         return KErrArgument;
       
   308         }
       
   309     // Extract the fields from the string
       
   310     TLex8 yearDesc( aString.Mid( 0, 4 ) );
       
   311     TLex8 monthDesc( aString.Mid( 4, 2 ) );
       
   312     TLex8 dayDesc( aString.Mid( 6, 2 ) );
       
   313     // Skip one character here, it's supposed to be 'T'
       
   314     TLex8 hourDesc( aString.Mid( 9, 2 ) );
       
   315     TLex8 minuteDesc( aString.Mid( 11, 2 ) );
       
   316     TLex8 secondDesc( aString.Mid( 13, 2 ) );
       
   317     
       
   318 
       
   319     TInt year, month, day, hour, minute, second;
       
   320     TInt error;
       
   321     
       
   322     // Fetch the values to temporary variables
       
   323     if ( ( error = yearDesc.Val(year) ) != KErrNone )
       
   324         return error;
       
   325     if ( ( error = monthDesc.Val(month) ) != KErrNone )
       
   326         return error;
       
   327     if ( ( error = dayDesc.Val(day) ) != KErrNone )
       
   328         return error;
       
   329     if ( ( error = hourDesc.Val(hour) ) != KErrNone )
       
   330         return error;
       
   331     if ( ( error = minuteDesc.Val(minute) ) != KErrNone )
       
   332         return error;
       
   333     if ( ( error = secondDesc.Val(second) ) != KErrNone )
       
   334         return error;
       
   335 
       
   336     // Assign values to datetime object
       
   337     if ( ( error = aDateTime.SetYear(year) ) != KErrNone )
       
   338         return error;
       
   339     if ( ( error = aDateTime.SetMonth((TMonth)(month-1) )) != KErrNone )
       
   340         return error;
       
   341     if ( ( error = aDateTime.SetDay(day) ) != KErrNone )
       
   342         return error;
       
   343     if ( ( error = aDateTime.SetHour(hour) ) != KErrNone )
       
   344         return error;
       
   345     if ( ( error = aDateTime.SetMinute(minute) ) != KErrNone )
       
   346         return error;
       
   347     if ( ( error = aDateTime.SetSecond(second) ) != KErrNone )
       
   348         return error;
       
   349         
       
   350     return KErrNone;
       
   351     }
       
   352 
       
   353 void COMADSFolderObject::Reset()
       
   354     {
       
   355     iError = KErrNone;
       
   356     iName.SetLength( 0 );
       
   357     iCreatedDate.Set( 2005, EJanuary, 1, 0, 0, 0, 0 );
       
   358     iModifiedDate.Set( 2005, EJanuary, 1, 0, 0, 0, 0 );
       
   359     }
       
   360     
       
   361 TInt COMADSFolderObject::Error( TInt aErrorCode )
       
   362     {
       
   363     iError = aErrorCode;
       
   364     return KErrNone;
       
   365     }
       
   366 	
       
   367 void COMADSFolderObject::ExportDbItemL(CFavouritesItem& aItem)
       
   368     {
       
   369     aItem.SetNameL(iName);
       
   370     aItem.SetType(CFavouritesItem::EFolder);
       
   371     }		
       
   372 
       
   373 void COMADSFolderObject::ImportDbItem(const CFavouritesItem& aItem)
       
   374     {
       
   375     LOGGER_ENTERFN( "COMADSFolderObject::ImportDbItem" );
       
   376     Reset();
       
   377     
       
   378     TInt resId(KErrNotFound);
       
   379     switch( aItem.ContextId() ) // Check if the item is a seamless link folder
       
   380         {
       
   381         case KFavouritesApplicationContextId:
       
   382            	{
       
   383            	LOGGER_WRITE("DOWNLOAD_APPLICATIONS");
       
   384             resId = R_BOOKMARKDSA_FOLDER_DOWNLOAD_APPLICATIONS;
       
   385             LOGGER_WRITE_1( "resId: %d", resId );
       
   386             break;
       
   387             }
       
   388         case KFavouritesImageContextId:
       
   389             {
       
   390             LOGGER_WRITE("DOWNLOAD_IMAGES");
       
   391             resId = R_BOOKMARKDSA_FOLDER_DOWNLOAD_IMAGES;
       
   392             break;
       
   393             }
       
   394         case KFavouritesAudioContextId:
       
   395             {
       
   396             LOGGER_WRITE("DOWNLOAD_TONES");
       
   397             resId = R_BOOKMARKDSA_FOLDER_DOWNLOAD_TONES;
       
   398             break;
       
   399             }
       
   400         case KFavouritesVideoContextId:
       
   401             {
       
   402             LOGGER_WRITE("DOWNLOAD_VIDEOS");
       
   403             resId = R_BOOKMARKDSA_FOLDER_DOWNLOAD_VIDEOS;
       
   404             break;
       
   405             }
       
   406         case KFavouritesSkinContextId:
       
   407             {
       
   408             LOGGER_WRITE("DOWNLOAD_SKINS");
       
   409             resId = R_BOOKMARKDSA_FOLDER_DOWNLOAD_SKINS;
       
   410             break;
       
   411             }
       
   412         case KFavouritesMusicContextId:
       
   413             {
       
   414             LOGGER_WRITE("DOWNLOAD_MUSIC");
       
   415             resId = R_BOOKMARKDSA_FOLDER_DOWNLOAD_MUSIC;
       
   416             break;
       
   417             }
       
   418         default:
       
   419             {
       
   420             break; // not a seamless link folder.
       
   421             }
       
   422         }
       
   423      
       
   424     if( resId != KErrNotFound )
       
   425         {
       
   426         LOGGER_WRITE( "before ReadFolderNameL" );
       
   427         TRAPD( error, ReadFolderNameL( iName, resId ) );
       
   428         LOGGER_WRITE( "after ReadFolderNameL" );
       
   429         if ( error )
       
   430             {
       
   431             LOGGER_WRITE_1( "Error reading folder name: %d", error );
       
   432             }
       
   433         }
       
   434     else
       
   435         {
       
   436         LOGGER_WRITE( "before iName = aItem.Name();" );
       
   437         iName = aItem.Name();
       
   438         }
       
   439     LOGGER_WRITE( "printing foldername.." );
       
   440     LOGGER_WRITE_1( "Foldername: %S", &iName);
       
   441     LOGGER_LEAVEFN( "COMADSFolderObject::ImportDbItem" );
       
   442     }
       
   443 
       
   444 
       
   445 void COMADSFolderObject::ImportDbItem( const CFolderItem& aItem )
       
   446     {
       
   447     LOGGER_ENTERFN( "COMADSFolderObject::ImportDbItem" );
       
   448     Reset();
       
   449     if ( aItem.Id() == 0 )
       
   450         {
       
   451         _LIT8( KRSSRootFolderName, "Web Feeds" );
       
   452         iName.Copy( KRSSRootFolderName );
       
   453         }
       
   454     else
       
   455         {
       
   456         iName = aItem.Name();
       
   457         }
       
   458     
       
   459     LOGGER_LEAVEFN( "COMADSFolderObject::ImportDbItem" );
       
   460     }
       
   461 
       
   462 
       
   463 void COMADSFolderObject::ReadFolderNameL( TDes& aName, const TInt aResID )
       
   464     {
       
   465     LOGGER_ENTERFN( "COMADSFolderObject::ReadFolderNameL" );
       
   466     
       
   467     TFileName resName( KOMADSResourceName );
       
   468     
       
   469     // Resource can be on ROM or on C-drive, if installed from SIS.
       
   470     TFileName dllName;
       
   471     Dll::FileName( dllName );
       
   472     
       
   473     LOGGER_WRITE_1("DllFileName: %S", &dllName );
       
   474     
       
   475     TParse parse;
       
   476     parse.Set( resName, &dllName, 0 );
       
   477     resName = parse.FullName();
       
   478     
       
   479     LOGGER_WRITE_1( "resName: %S", &resName );
       
   480     
       
   481     CStringResourceReader* reader = CStringResourceReader::NewLC( resName );
       
   482     LOGGER_WRITE_1( "readed resfile: %S", &resName );
       
   483     
       
   484     aName.Copy( reader->ReadResourceString( aResID ) );
       
   485     LOGGER_WRITE("Resource Readed ok");
       
   486     CleanupStack::PopAndDestroy( reader );
       
   487 
       
   488     LOGGER_LEAVEFN( "COMADSFolderObject::ReadFolderNameL" );
       
   489     }
       
   490