htiui/HtiServicePlugins/HtiPIMServicePlugin/src/HtiBookmarkHandler.cpp
changeset 0 d6fe6244b863
child 11 454d022d514b
equal deleted inserted replaced
-1:000000000000 0:d6fe6244b863
       
     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:  Implementation of browser bookmark handling.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "HtiBookmarkHandler.h"
       
    21 #include "HtiPIMServicePlugin.h"
       
    22 
       
    23 #include <cmconnectionmethod.h>
       
    24 #include <cmconnectionmethoddef.h>
       
    25 #include <cmdestination.h>
       
    26 #include <cmmanager.h>
       
    27 
       
    28 #include <FavouritesItem.h>
       
    29 #include <FavouritesItemList.h>
       
    30 #include <FavouritesDb.h>
       
    31 #include <FavouritesFile.h>
       
    32 #include <FavouritesWapAp.h>
       
    33 
       
    34 #include <HtiDispatcherInterface.h>
       
    35 #include <HTILogging.h>
       
    36 
       
    37 // EXTERNAL DATA STRUCTURES
       
    38 
       
    39 // EXTERNAL FUNCTION PROTOTYPES
       
    40 
       
    41 // CONSTANTS
       
    42 
       
    43 // MACROS
       
    44 
       
    45 // LOCAL CONSTANTS AND MACROS
       
    46 _LIT8( KErrorUnrecognizedCommand, "Unrecognized command" );
       
    47 _LIT8( KErrorInvalidParameters, "Invalid command parameters" );
       
    48 _LIT8( KErrorCreateFailed, "Bookmark creation failed" );
       
    49 _LIT8( KErrorDeleteFailed, "Bookmark deletion failed" );
       
    50 _LIT8( KErrorBookmarkNotFound, "Bookmark not found" );
       
    51 _LIT8( KErrorFolderNotFound, "Folder not found" );
       
    52 _LIT8( KErrorApNotFound, "Access point not found" );
       
    53 _LIT8( KErrorFolderSearchFailed, "Error when searching folder" );
       
    54 _LIT8( KErrorApSearchFailed, "Error when searching access point" );
       
    55 
       
    56 const TInt KCreateMessageMinLength = 10;
       
    57 const TInt KDeleteMessageMinLength = 3;
       
    58 
       
    59 // MODULE DATA STRUCTURES
       
    60 
       
    61 // LOCAL FUNCTION PROTOTYPES
       
    62 
       
    63 // FORWARD DECLARATIONS
       
    64 
       
    65 // ============================ MEMBER FUNCTIONS ===============================
       
    66 
       
    67 // -----------------------------------------------------------------------------
       
    68 // CHtiBookmarkHandler::NewL
       
    69 // Two-phased constructor.
       
    70 // -----------------------------------------------------------------------------
       
    71 CHtiBookmarkHandler* CHtiBookmarkHandler::NewL()
       
    72     {
       
    73     HTI_LOG_FUNC_IN( "CHtiBookmarkHandler::NewL" );
       
    74     CHtiBookmarkHandler* self = new ( ELeave ) CHtiBookmarkHandler();
       
    75     CleanupStack::PushL ( self );
       
    76     self->ConstructL();
       
    77     CleanupStack::Pop();
       
    78     HTI_LOG_FUNC_OUT( "CHtiBookmarkHandler::NewL" );
       
    79     return self;
       
    80     }
       
    81 
       
    82 
       
    83 // ----------------------------------------------------------------------------
       
    84 // CHtiBookmarkHandler::CHtiBookmarkHandler
       
    85 // C++ default constructor can NOT contain any code, that
       
    86 // might leave.
       
    87 // ----------------------------------------------------------------------------
       
    88 CHtiBookmarkHandler::CHtiBookmarkHandler()
       
    89     {
       
    90     HTI_LOG_FUNC_IN( "CHtiBookmarkHandler::CHtiBookmarkHandler" );
       
    91     HTI_LOG_FUNC_OUT( "CHtiBookmarkHandler::CHtiBookmarkHandler" );
       
    92     }
       
    93 
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // CHtiBookmarkHandler::~CHtiBookmarkHandler
       
    97 // Destructor.
       
    98 // -----------------------------------------------------------------------------
       
    99 CHtiBookmarkHandler::~CHtiBookmarkHandler()
       
   100     {
       
   101     HTI_LOG_FUNC_IN( "CHtiBookmarkHandler::~CHtiBookmarkHandler" );
       
   102     delete iUrl;
       
   103     iFavSession.Close();
       
   104     HTI_LOG_FUNC_OUT( "CHtiBookmarkHandler::~CHtiBookmarkHandler" );
       
   105     }
       
   106 
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CHtiBookmarkHandler::ConstructL
       
   110 // Symbian 2nd phase constructor can leave.
       
   111 // -----------------------------------------------------------------------------
       
   112 void CHtiBookmarkHandler::ConstructL()
       
   113     {
       
   114     HTI_LOG_FUNC_IN( "CHtiBookmarkHandler::ConstructL" );
       
   115     User::LeaveIfError( iFavSession.Connect() );
       
   116     HTI_LOG_FUNC_OUT( "CHtiBookmarkHandler::ConstructL" );
       
   117     }
       
   118 
       
   119 
       
   120 // -----------------------------------------------------------------------------
       
   121 // CHtiBookmarkHandler::SetDispatcher
       
   122 // Sets the dispatcher pointer.
       
   123 // -----------------------------------------------------------------------------
       
   124 
       
   125 void CHtiBookmarkHandler::SetDispatcher( MHtiDispatcher* aDispatcher )
       
   126     {
       
   127     HTI_LOG_FUNC_IN( "CHtiBookmarkHandler::SetDispatcher" );
       
   128     iDispatcher = aDispatcher;
       
   129     HTI_LOG_FUNC_OUT( "CHtiBookmarkHandler::SetDispatcher" );
       
   130     }
       
   131 
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // CHtiBookmarkHandler::ProcessMessageL
       
   135 // Parses the received message and calls handler functions.
       
   136 // -----------------------------------------------------------------------------
       
   137 void CHtiBookmarkHandler::ProcessMessageL( const TDesC8& aMessage,
       
   138     THtiMessagePriority /*aPriority*/ )
       
   139     {
       
   140     HTI_LOG_FUNC_IN( "CHtiBookmarkHandler::ProcessMessageL" );
       
   141     // Zero legth of aMessage tested already in CHtiPIMServicePlugin.
       
   142     // Other sanity checks must be done here.
       
   143     if ( aMessage[0] == CHtiPIMServicePlugin::ECreateBookmark )
       
   144         {
       
   145         TRAPD( err, ParseCreateMessageL( aMessage ) );
       
   146         if ( err != KErrNone )
       
   147             {
       
   148             SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
   149             }
       
   150         else
       
   151             {
       
   152             TRAP( err, HandleCreateBookmarkL() );
       
   153             if ( err != KErrNone )
       
   154                 {
       
   155                 SendErrorMessageL( err, KErrorCreateFailed );
       
   156                 }
       
   157             }
       
   158         }
       
   159 
       
   160     else if ( aMessage[0] == CHtiPIMServicePlugin::EDeleteBookmark )
       
   161         {
       
   162         TRAPD( err, ParseDeleteMessageL( aMessage ) );
       
   163         if ( err != KErrNone )
       
   164             {
       
   165             SendErrorMessageL( KErrArgument, KErrorInvalidParameters );
       
   166             }
       
   167         else
       
   168             {
       
   169             TRAP( err, HandleDeleteBookmarkL() );
       
   170             if ( err != KErrNone )
       
   171                 {
       
   172                 SendErrorMessageL( err, KErrorDeleteFailed );
       
   173                 }
       
   174             }
       
   175         }
       
   176 
       
   177     else
       
   178         {
       
   179         SendErrorMessageL( KErrArgument, KErrorUnrecognizedCommand );
       
   180         }
       
   181     HTI_LOG_FUNC_OUT( "CHtiBookmarkHandler::ProcessMessageL" );
       
   182     }
       
   183 
       
   184 
       
   185 // ----------------------------------------------------------------------------
       
   186 // CHtiBookmarkHandler::IsBusy
       
   187 // Returns the status whether this handler is busy processing a request.
       
   188 // ----------------------------------------------------------------------------
       
   189 TBool CHtiBookmarkHandler::IsBusy()
       
   190     {
       
   191     // Never busy if ProcessMessageL has completed.
       
   192     return EFalse;
       
   193     }
       
   194 
       
   195 
       
   196 // ----------------------------------------------------------------------------
       
   197 // CHtiBookmarkHandler::HandleCreateBookmarkL
       
   198 // Handles the bookmark and/or folder creation command.
       
   199 // ----------------------------------------------------------------------------
       
   200 void CHtiBookmarkHandler::HandleCreateBookmarkL()
       
   201     {
       
   202     HTI_LOG_FUNC_IN( "CHtiBookmarkHandler::HandleCreateBookmarkL" );
       
   203     TInt err = KErrNone;
       
   204     // If folder given, get the UID if it exists
       
   205     TInt folderUid = KFavouritesRootUid;
       
   206     if ( iFolderName.Length() > 0 )
       
   207         {
       
   208         TRAP( err, folderUid = GetFolderUidL( iFolderName ) );
       
   209         if ( err != KErrNone )
       
   210             {
       
   211             SendErrorMessageL( err, KErrorFolderSearchFailed );
       
   212             return;
       
   213             }
       
   214         }
       
   215     HTI_LOG_FORMAT( "Folder UID = %d", folderUid );
       
   216 
       
   217     // If access point given, search the UID. It's an error if AP is not found.
       
   218     TInt apUid = KErrNotFound;
       
   219     if ( iApName.Length() > 0 )
       
   220         {
       
   221         TRAP( err, apUid = GetApUidL( iApName ) );
       
   222         if ( err != KErrNone )
       
   223             {
       
   224             SendErrorMessageL( err, KErrorApSearchFailed );
       
   225             return;
       
   226             }
       
   227         if ( apUid == KErrNotFound )
       
   228             {
       
   229             SendErrorMessageL( KErrNotFound, KErrorApNotFound );
       
   230             return;
       
   231             }
       
   232         }
       
   233 
       
   234     RFavouritesDb db;
       
   235     User::LeaveIfError( db.Open( iFavSession, KBrowserBookmarks ) );
       
   236     CleanupClosePushL<RFavouritesDb>( db );
       
   237 
       
   238     TInt itemsCreated = 0;
       
   239     // If folder was given but was not found, create the folder first.
       
   240     if ( iFolderName.Length() > 0 && folderUid == KErrNotFound )
       
   241         {
       
   242         CFavouritesItem* folder = CFavouritesItem::NewLC();
       
   243         folder->SetType( CFavouritesItem::EFolder );
       
   244         folder->SetParentFolder( KFavouritesRootUid ) ;
       
   245         folder->SetNameL( iFolderName );
       
   246 
       
   247         HTI_LOG_TEXT( "Adding the folder item to DB" );
       
   248         err = db.Add( *folder, EFalse );
       
   249         HTI_LOG_FORMAT( "Add returned %d", err );
       
   250         if ( err != KErrNone )
       
   251             {
       
   252             User::Leave( err );
       
   253             }
       
   254         folderUid = folder->Uid();
       
   255         HTI_LOG_FORMAT( "New folder UID = %d", folderUid );
       
   256         CleanupStack::PopAndDestroy(); // folder
       
   257         itemsCreated++;
       
   258         }
       
   259 
       
   260     // Now create the actual bookmark item
       
   261     CFavouritesItem* item = CFavouritesItem::NewLC();
       
   262     item->SetType( CFavouritesItem::EItem );
       
   263     item->SetParentFolder( folderUid ) ;
       
   264     item->SetNameL( iBookmarkName );
       
   265     item->SetUrlL( *iUrl );
       
   266     if ( apUid != KErrNotFound )
       
   267         {
       
   268         TFavouritesWapAp favAp;
       
   269         favAp.SetApId(  ( TUint32 ) apUid );
       
   270         item->SetWapAp( favAp );
       
   271         }
       
   272     if ( iUserName.Length() > 0 )
       
   273         {
       
   274         item->SetUserNameL( iUserName );
       
   275         }
       
   276     if ( iPassword.Length() > 0 )
       
   277         {
       
   278         item->SetPasswordL( iPassword );
       
   279         }
       
   280 
       
   281     HTI_LOG_TEXT( "Adding the bookmark item to DB" );
       
   282     err = db.Add( *item, EFalse );
       
   283     HTI_LOG_FORMAT( "Add returned %d", err );
       
   284     if ( err )
       
   285         {
       
   286         User::Leave( err );
       
   287         }
       
   288     HTI_LOG_FORMAT( "Bookmark UID = %d", item->Uid() );
       
   289     CleanupStack::PopAndDestroy(); // item
       
   290     itemsCreated++;
       
   291 
       
   292     CleanupStack::PopAndDestroy(); // db
       
   293 
       
   294     TBuf8<1> response;
       
   295     response.Append( itemsCreated );
       
   296     SendOkMsgL( response );
       
   297 
       
   298     HTI_LOG_FUNC_OUT( "CHtiBookmarkHandler::HandleCreateBookmarkL" );
       
   299     }
       
   300 
       
   301 
       
   302 // ----------------------------------------------------------------------------
       
   303 // CHtiBookmarkHandler::HandleDeleteBookmarkL
       
   304 // Handles the bookmark and/or folder deletion.
       
   305 // ----------------------------------------------------------------------------
       
   306 void CHtiBookmarkHandler::HandleDeleteBookmarkL()
       
   307     {
       
   308     HTI_LOG_FUNC_IN( "CHtiBookmarkHandler::HandleDeleteBookmarkL" );
       
   309     TInt err = KErrNone;
       
   310     // If folder given, get the UID if it exists.
       
   311     // It is an error if the given folder does not exist.
       
   312     TInt folderUid = KFavouritesRootUid;
       
   313     if ( iFolderName.Length() > 0 )
       
   314         {
       
   315         TRAP( err, folderUid = GetFolderUidL( iFolderName ) );
       
   316         if ( err != KErrNone )
       
   317             {
       
   318             SendErrorMessageL( err, KErrorFolderSearchFailed );
       
   319             return;
       
   320             }
       
   321         if ( folderUid == KErrNotFound )
       
   322             {
       
   323             SendErrorMessageL( KErrNotFound, KErrorFolderNotFound );
       
   324             return;
       
   325             }
       
   326         }
       
   327     HTI_LOG_FORMAT( "Folder UID = %d", folderUid );
       
   328 
       
   329     RFavouritesDb db;
       
   330     User::LeaveIfError( db.Open( iFavSession, KBrowserBookmarks ) );
       
   331     CleanupClosePushL<RFavouritesDb>( db );
       
   332 
       
   333     CArrayFixFlat<TInt>* deleteList = new ( ELeave ) CArrayFixFlat<TInt>( 10 );
       
   334     CleanupStack::PushL( deleteList );
       
   335     if ( iBookmarkName.Length() > 0 )
       
   336         {
       
   337         // Get the UID of bookmark item matching to iBookmarkName
       
   338         // It is an error if nothing found.
       
   339         CFavouritesItemList* items = new ( ELeave ) CFavouritesItemList();
       
   340         CleanupStack::PushL( items );
       
   341         User::LeaveIfError( db.GetAll( *items, folderUid,
       
   342             CFavouritesItem::EItem ) );
       
   343         TInt uid = KErrNotFound;
       
   344         TInt itemCount = items->Count();
       
   345         HTI_LOG_FORMAT( "Found %d items", itemCount );
       
   346         TInt i = 0;
       
   347         while ( uid == KErrNotFound && i < itemCount )
       
   348             {
       
   349             if ( ( *items )[i]->Name().CompareF( iBookmarkName ) == 0 )
       
   350                 {
       
   351                 uid = ( *items )[i]->Uid();
       
   352                 }
       
   353             i++;
       
   354             }
       
   355         CleanupStack::PopAndDestroy(); // items
       
   356         HTI_LOG_FORMAT( "Matching item UID: %d", uid );
       
   357         if ( uid == KErrNotFound )
       
   358             {
       
   359             SendErrorMessageL( KErrNotFound, KErrorBookmarkNotFound );
       
   360             CleanupStack::PopAndDestroy( 2 ); // deleteList, db
       
   361             return;
       
   362             }
       
   363         else
       
   364             {
       
   365             deleteList->AppendL( uid );
       
   366             }
       
   367         }
       
   368     else
       
   369         {
       
   370         // We are deleting a folder (whose UID we already have) or deleting all.
       
   371         if ( folderUid != KFavouritesRootUid )
       
   372             {
       
   373             deleteList->AppendL( folderUid );
       
   374             }
       
   375         else
       
   376             {
       
   377             // We don't have folder or bookmark name - deleting all
       
   378             User::LeaveIfError( db.GetUids( *deleteList, KFavouritesRootUid ) );
       
   379             HTI_LOG_FORMAT( "Found %d matching items", deleteList->Count() );
       
   380             }
       
   381         }
       
   382 
       
   383     TInt beginningCount = KErrNotFound;
       
   384     User::LeaveIfError( db.Count( beginningCount ) );
       
   385     HTI_LOG_FORMAT( "Items in DB before delete: %d", beginningCount );
       
   386 
       
   387     // Start deleting
       
   388     TInt deleteCount = 0;
       
   389     for ( TInt i = 0; i < deleteList->Count(); i++ )
       
   390         {
       
   391         HTI_LOG_FORMAT( "Deleting with UID %d", ( *deleteList )[i] );
       
   392         err = db.Delete( ( *deleteList )[i] );
       
   393         HTI_LOG_FORMAT( "Delete returned %d", err );
       
   394         if ( err == KErrNone )
       
   395             {
       
   396             deleteCount++;
       
   397             }
       
   398         if ( err != KErrNone && err != KErrAccessDenied )
       
   399             {
       
   400             HTI_LOG_TEXT( "Unexpected error when deleting - leaving" );
       
   401             User::Leave( err );
       
   402             }
       
   403         }
       
   404 
       
   405     TInt endingCount = KErrNotFound;
       
   406     User::LeaveIfError( db.Count( endingCount ) );
       
   407     HTI_LOG_FORMAT( "Items in DB after delete: %d", endingCount );
       
   408 
       
   409     CleanupStack::PopAndDestroy( 2 ); // deleteList, db
       
   410 
       
   411     TBuf8<1> response;
       
   412     response.Append( beginningCount - endingCount );
       
   413     SendOkMsgL( response );
       
   414 
       
   415     HTI_LOG_FUNC_OUT( "CHtiBookmarkHandler::HandleDeleteBookmarkL" );
       
   416     }
       
   417 
       
   418 
       
   419 // ----------------------------------------------------------------------------
       
   420 // CHtiBookmarkHandler::ParseCreateMessageL
       
   421 // Extracts the parameters from a create bookmark message.
       
   422 // ----------------------------------------------------------------------------
       
   423 void CHtiBookmarkHandler::ParseCreateMessageL( const TDesC8& aMessage )
       
   424     {
       
   425     HTI_LOG_FUNC_IN( "CHtiBookmarkHandler::ParseCreateMessageL" );
       
   426     TInt length = aMessage.Length();
       
   427     HTI_LOG_FORMAT( "CreateBookmark message length = %d", length );
       
   428 
       
   429     if ( length < KCreateMessageMinLength )
       
   430         {
       
   431         User::Leave( KErrArgument );
       
   432         }
       
   433 
       
   434     // Extract folder name
       
   435     TInt offset = 1; // skip the command code in position 0
       
   436     offset = ExtractStringL( aMessage, offset, 1, 0, KFavouritesMaxName,
       
   437                              iFolderName );
       
   438     HTI_LOG_FORMAT( "Folder name: %S", &iFolderName );
       
   439 
       
   440     // Extract bookmark name
       
   441     offset = ExtractStringL( aMessage, offset, 1, 1, KFavouritesMaxName,
       
   442                              iBookmarkName );
       
   443     HTI_LOG_FORMAT( "Bookmark name: %S", &iBookmarkName );
       
   444 
       
   445     // Extract URL
       
   446     delete iUrl;
       
   447     iUrl = NULL;
       
   448     iUrl = HBufC::NewL( KFavouritesMaxUrl );
       
   449     TPtr urlPtr = iUrl->Des();
       
   450     offset = ExtractStringL( aMessage, offset, 2, 1, KFavouritesMaxUrl,
       
   451                              urlPtr );
       
   452     HTI_LOG_FORMAT( "URL: %S", iUrl );
       
   453 
       
   454     // Extract AP name
       
   455     offset = ExtractStringL( aMessage, offset, 1, 0, KApMaxConnNameLength,
       
   456                              iApName );
       
   457     HTI_LOG_FORMAT( "AP name: %S", &iApName );
       
   458 
       
   459     // Extract user name
       
   460     offset = ExtractStringL( aMessage, offset, 1, 0, KFavouritesMaxUserName,
       
   461                              iUserName );
       
   462     HTI_LOG_FORMAT( "User name: %S", &iUserName );
       
   463 
       
   464     // Extract password
       
   465     offset = ExtractStringL( aMessage, offset, 1, 0, KFavouritesMaxPassword,
       
   466                              iPassword );
       
   467     HTI_LOG_FORMAT( "Password: %S", &iPassword );
       
   468 
       
   469     // Check that there's no extra data
       
   470     if ( offset != length  )
       
   471         {
       
   472         User::Leave( KErrArgument );
       
   473         }
       
   474 
       
   475     HTI_LOG_FUNC_OUT( "CHtiBookmarkHandler::ParseCreateMessageL" );
       
   476     }
       
   477 
       
   478 
       
   479 // ----------------------------------------------------------------------------
       
   480 // CHtiBookmarkHandler::ParseDeleteMessageL
       
   481 // Extracts the parameters from a delete bookmark message.
       
   482 // ----------------------------------------------------------------------------
       
   483 void CHtiBookmarkHandler::ParseDeleteMessageL( const TDesC8& aMessage )
       
   484     {
       
   485     HTI_LOG_FUNC_IN( "CHtiBookmarkHandler::ParseDeleteMessageL" );
       
   486     TInt length = aMessage.Length();
       
   487     HTI_LOG_FORMAT( "DeleteBookmark message length = %d", length );
       
   488 
       
   489     if ( length < KDeleteMessageMinLength )
       
   490         {
       
   491         User::Leave( KErrArgument );
       
   492         }
       
   493 
       
   494     // Extract folder name
       
   495     TInt offset = 1; // skip the command code in position 0
       
   496     offset = ExtractStringL( aMessage, offset, 1, 0, KFavouritesMaxName,
       
   497                              iFolderName );
       
   498     HTI_LOG_FORMAT( "Folder name: %S", &iFolderName );
       
   499 
       
   500     // Extract bookmark name
       
   501     offset = ExtractStringL( aMessage, offset, 1, 0, KFavouritesMaxName,
       
   502                              iBookmarkName );
       
   503     HTI_LOG_FORMAT( "Bookmark name: %S", &iBookmarkName );
       
   504 
       
   505     HTI_LOG_FUNC_OUT( "CHtiBookmarkHandler::ParseDeleteMessageL" );
       
   506     }
       
   507 
       
   508 
       
   509 // ----------------------------------------------------------------------------
       
   510 // CHtiBookmarkHandler::ExtractStringL
       
   511 // Extract one string from the given message. Leaves on syntax errors.
       
   512 // ----------------------------------------------------------------------------
       
   513 TInt CHtiBookmarkHandler::ExtractStringL( const TDesC8& aMessage, TInt aOffset,
       
   514         TInt aLengthBytes, TInt aMinLength, TInt aMaxLength, TDes& aResult )
       
   515     {
       
   516     HTI_LOG_FUNC_IN( "CHtiBookmarkHandler::ExtractStringL" );
       
   517     __ASSERT_ALWAYS( aResult.MaxLength() >= aMaxLength,
       
   518                      User::Panic( _L( "HtiPimAssert" ), 1 ) );
       
   519     __ASSERT_ALWAYS( aLengthBytes > 0, User::Panic( _L( "HtiPimAssert" ), 2 ) );
       
   520 
       
   521     TInt length = aMessage.Length();
       
   522     if ( length < aOffset + 1 )
       
   523         {
       
   524         User::Leave( KErrArgument );
       
   525         }
       
   526     TInt parseLength = aMessage[aOffset];
       
   527     aOffset++;
       
   528     if ( aLengthBytes > 1 )
       
   529         {
       
   530         for ( TInt i = 1; i < aLengthBytes; i++ )
       
   531             {
       
   532             parseLength += aMessage[aOffset] << ( i * 8 );
       
   533             aOffset++;
       
   534             }
       
   535         }
       
   536     HTI_LOG_FORMAT( "Parse length = %d", parseLength );
       
   537     if ( parseLength < aMinLength  || parseLength > aMaxLength ||
       
   538          parseLength + aOffset > length )
       
   539         {
       
   540         User::Leave( KErrArgument );
       
   541         }
       
   542     else
       
   543         {
       
   544         aResult.Copy( aMessage.Mid( aOffset, parseLength ) );
       
   545         aOffset += parseLength;
       
   546         }
       
   547     HTI_LOG_FUNC_OUT( "CHtiBookmarkHandler::ExtractStringL" );
       
   548     return aOffset;
       
   549     }
       
   550 
       
   551 
       
   552 // ----------------------------------------------------------------------------
       
   553 // CHtiBookmarkHandler::GetFolderUidL
       
   554 // Finds the UID of the named folder if it exists.
       
   555 // ----------------------------------------------------------------------------
       
   556 TInt CHtiBookmarkHandler::GetFolderUidL( const TDesC& aFolderName )
       
   557     {
       
   558     HTI_LOG_FUNC_IN( "CHtiBookmarkHandler::GetFolderUidL" );
       
   559     HTI_LOG_FORMAT( "Searching folder %S", &aFolderName );
       
   560     RFavouritesDb db;
       
   561     User::LeaveIfError( db.Open( iFavSession, KBrowserBookmarks ) );
       
   562     CleanupClosePushL<RFavouritesDb>( db );
       
   563 
       
   564     /* Doing the name matching ourselves 'cause giving the name match pattern
       
   565      * parameter to GetAll or GetUids methods crashes the favourites server.
       
   566      */
       
   567     CFavouritesItemList* folders = new ( ELeave ) CFavouritesItemList();
       
   568     CleanupStack::PushL( folders );
       
   569     User::LeaveIfError( db.GetAll( *folders, KFavouritesNullUid,
       
   570         CFavouritesItem::EFolder ) );
       
   571     TInt uid = KErrNotFound;
       
   572     TInt folderCount = folders->Count();
       
   573     HTI_LOG_FORMAT( "Found %d folders", folderCount );
       
   574     TInt i = 0;
       
   575     while ( uid == KErrNotFound && i < folderCount )
       
   576         {
       
   577         if ( ( *folders )[i]->Name().CompareF( aFolderName ) == 0 )
       
   578             {
       
   579             uid = ( *folders )[i]->Uid();
       
   580             }
       
   581         i++;
       
   582         }
       
   583 
       
   584     CleanupStack::PopAndDestroy( 2 ); // folders, db
       
   585     HTI_LOG_FORMAT( "Matching UID: %d", uid );
       
   586     HTI_LOG_FUNC_OUT( "CHtiBookmarkHandler::GetFolderUidL" );
       
   587     return uid;
       
   588     }
       
   589 
       
   590 
       
   591 // ----------------------------------------------------------------------------
       
   592 // CHtiBookmarkHandler::GetApUidL
       
   593 // Gets the WAP Access Point ID of access point named by iConnName.
       
   594 // Favourites item needs the WAP ID for identifying access point.
       
   595 // Returns KErrNotFound if AP not found.
       
   596 // ----------------------------------------------------------------------------
       
   597 TInt CHtiBookmarkHandler::GetApUidL( const TDesC& aApName )
       
   598     {
       
   599     HTI_LOG_FUNC_IN( "CHtiBookmarkHandler::GetApUidL" );
       
   600     TInt uid = KErrNotFound;
       
   601 
       
   602     RCmManager cmManager;
       
   603     cmManager.OpenL();
       
   604     CleanupClosePushL( cmManager );
       
   605 
       
   606     // Search from uncategorised first
       
   607     RArray<TUint32> array = RArray<TUint32>();
       
   608     cmManager.ConnectionMethodL( array );
       
   609     CleanupClosePushL( array );
       
   610     TInt i = 0;
       
   611     while ( i < array.Count() && uid == KErrNotFound )
       
   612         {
       
   613         RCmConnectionMethod cm = cmManager.ConnectionMethodL( array[i] );
       
   614         CleanupClosePushL( cm );
       
   615         HBufC* name = cm.GetStringAttributeL( CMManager::ECmName );
       
   616         HTI_LOG_FORMAT( "Found name: %S", name );
       
   617         CleanupStack::PushL( name );
       
   618         if ( aApName.Compare( *name ) == 0 )
       
   619             {
       
   620             uid = cm.GetIntAttributeL( CMManager::ECmWapId );
       
   621             HTI_LOG_FORMAT( "Match: UID = %d", uid );
       
   622             }
       
   623         CleanupStack::PopAndDestroy(); // name
       
   624         CleanupStack::PopAndDestroy(); // cm
       
   625         i++;
       
   626         }
       
   627     CleanupStack::PopAndDestroy(); // array
       
   628 
       
   629     // If not found from uncategorised, search from all destinations
       
   630     if ( uid == KErrNotFound )
       
   631         {
       
   632         HTI_LOG_TEXT( "Not found from uncategorised" );
       
   633         RArray<TUint32> destIdArray = RArray<TUint32>();
       
   634         cmManager.AllDestinationsL( destIdArray );
       
   635         CleanupClosePushL( destIdArray );
       
   636         i = 0;
       
   637         while ( i < destIdArray.Count() && uid == KErrNotFound )
       
   638             {
       
   639             RCmDestination dest = cmManager.DestinationL( destIdArray[i] );
       
   640             CleanupClosePushL( dest );
       
   641             TInt j = 0;
       
   642             while ( j < dest.ConnectionMethodCount() && uid == KErrNotFound )
       
   643                 {
       
   644                 HBufC* name = dest.ConnectionMethodL( j ).GetStringAttributeL(
       
   645                         CMManager::ECmName );
       
   646                 CleanupStack::PushL( name );
       
   647                 HTI_LOG_FORMAT( "Found name: %S", name );
       
   648                 if ( aApName.Compare( *name ) == 0 )
       
   649                     {
       
   650                     uid = dest.ConnectionMethodL( j ).GetIntAttributeL(
       
   651                             CMManager::ECmWapId );
       
   652                     HTI_LOG_FORMAT( "Match: UID = %d", uid );
       
   653                     }
       
   654                 CleanupStack::PopAndDestroy(); // name
       
   655                 j++;
       
   656                 }
       
   657             CleanupStack::PopAndDestroy(); // dest
       
   658             i++;
       
   659             }
       
   660         CleanupStack::PopAndDestroy(); // destIdArray
       
   661         }
       
   662 
       
   663     CleanupStack::PopAndDestroy(); // cmManager
       
   664     HTI_LOG_FUNC_OUT( "CHtiBookmarkHandler::GetApUidL" );
       
   665     return uid;
       
   666     }
       
   667 
       
   668 
       
   669 // ----------------------------------------------------------------------------
       
   670 // CHtiBookmarkHandler::SendOkMsgL
       
   671 // Helper function for sending response messages.
       
   672 // ----------------------------------------------------------------------------
       
   673 void CHtiBookmarkHandler::SendOkMsgL( const TDesC8& aData )
       
   674     {
       
   675     HTI_LOG_FUNC_IN( "CHtiBookmarkHandler::SendOkMsgL" );
       
   676     User::LeaveIfNull( iDispatcher );
       
   677     HBufC8* temp = HBufC8::NewL( aData.Length() + 1 );
       
   678     TPtr8 response = temp->Des();
       
   679     response.Append( ( TChar ) CHtiPIMServicePlugin::EResultOk );
       
   680     response.Append( aData );
       
   681     User::LeaveIfError( iDispatcher->DispatchOutgoingMessage(
       
   682         temp, KPIMServiceUid ) );
       
   683     HTI_LOG_FUNC_OUT( "CHtiBookmarkHandler::SendOkMsgL" );
       
   684     }
       
   685 
       
   686 
       
   687 // ----------------------------------------------------------------------------
       
   688 // CHtiBookmarkHandler::SendErrorMessageL
       
   689 // Helper function for sending error response messages.
       
   690 // ----------------------------------------------------------------------------
       
   691 void CHtiBookmarkHandler::SendErrorMessageL( TInt aError,
       
   692         const TDesC8& aDescription )
       
   693     {
       
   694     HTI_LOG_FUNC_IN( "CHtiBookmarkHandler::SendErrorMessageL" );
       
   695     User::LeaveIfNull( iDispatcher );
       
   696     User::LeaveIfError( iDispatcher->DispatchOutgoingErrorMessage(
       
   697         aError, aDescription, KPIMServiceUid ) );
       
   698     HTI_LOG_FUNC_OUT( "CHtiBookmarkHandler::SendErrorMessageL" );
       
   699     }
       
   700 
       
   701 
       
   702 // End of file