browserui/browser/BrowserAppSrc/BrowserUtil.cpp
changeset 0 84ad3b177aa3
child 18 4530440261a8
equal deleted inserted replaced
-1:000000000000 0:84ad3b177aa3
       
     1 /*
       
     2 * Copyright (c) 2002 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 the License "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 *      Utilities.
       
    16 *      
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 
       
    23 #include "logger.h"
       
    24 
       
    25 #include <e32base.h>
       
    26 #include <FavouritesLimits.h>
       
    27 #include <FavouritesItem.h>
       
    28 #include <uri16.h>
       
    29 #include <SysUtil.h>
       
    30 #include <ErrorUi.h>
       
    31 
       
    32 #include "BrowserUtil.h"
       
    33 #include "Browser.hrh"
       
    34 
       
    35 #include "CommonConstants.h"
       
    36 #include "ApiProvider.h"
       
    37 #include <commdb.h>
       
    38 #include <aputils.h>
       
    39 #include "commsmodel.h"
       
    40 #include "preferences.h"
       
    41 #include <FeatMgr.h>
       
    42 
       
    43 // ================= MEMBER FUNCTIONS =======================
       
    44 
       
    45 // ---------------------------------------------------------
       
    46 // Util::Panic
       
    47 // ---------------------------------------------------------
       
    48 //
       
    49 void Util::Panic( Util::TPanicReason aReason )
       
    50     {
       
    51     _LIT( KAppName, "WmlBrowser" );
       
    52 	User::Panic( KAppName, aReason );
       
    53     }
       
    54 
       
    55 // ---------------------------------------------------------
       
    56 // Util::SanityCheckL
       
    57 // ---------------------------------------------------------
       
    58 //
       
    59 void Util::SanityCheckL()
       
    60     {
       
    61 #ifdef _DEBUG
       
    62 
       
    63     // Check if the Engine limits for bookmarks match editing control sizes.
       
    64     // Cannot do this compile time, because Engine limits are TInts instead of
       
    65     // #define-s, so we must do run-time check (debug build only). Naturally,
       
    66     // the conditional expression here is constant. Avoid warnings by using
       
    67     // dummy variables (maxEngine...).
       
    68     TInt maxEngineUrl = KFavouritesMaxUrl;
       
    69     TInt maxEngineUsername = KFavouritesMaxUserName;
       
    70     TInt maxEnginePassword = KFavouritesMaxPassword;
       
    71     TInt maxEngineBookmarkName = KFavouritesMaxName;
       
    72 
       
    73     if (
       
    74         KFavouritesMaxUrlDefine != maxEngineUrl ||
       
    75         KFavouritesMaxUsernameDefine != maxEngineUsername ||
       
    76         KFavouritesMaxPasswordDefine != maxEnginePassword ||
       
    77         KFavouritesMaxBookmarkNameDefine != maxEngineBookmarkName
       
    78        )
       
    79         {
       
    80         Panic( ELimitMismatch );
       
    81         }
       
    82 
       
    83 #endif
       
    84     }
       
    85 
       
    86 // ---------------------------------------------------------
       
    87 // Util::RetreiveUsernameAndPasswordFromUrlL
       
    88 // ---------------------------------------------------------
       
    89 //
       
    90 void Util::RetreiveUsernameAndPasswordFromUrlL
       
    91 	( const TDesC& aUrl, CFavouritesItem& aItem )
       
    92 	{
       
    93     /// Empty string.
       
    94     _LIT( KEmptyBuf, "" ); 
       
    95 /*	CUrl* url = NULL;
       
    96 	TRAPD( error, url = CUrl::NewL( aUrl ) );
       
    97 	if( error == KErrNone )
       
    98 		{ 
       
    99 		CleanupStack::PushL( url );
       
   100 */
       
   101     TUriParser16 url;
       
   102     url.Parse(aUrl);
       
   103 	TPtrC userInfo = url.Extract( EUriUserinfo );
       
   104     TPtrC userName ;
       
   105     TPtrC pass ;
       
   106     userName.Set( KEmptyBuf );
       
   107     pass.Set( KEmptyBuf );
       
   108 	if( userInfo.Length() )
       
   109         { 
       
   110         TInt position = userInfo.LocateF( ':' );
       
   111 	    if( ( position != KErrNotFound ) )
       
   112             {
       
   113             userName.Set( userInfo.Left( position ) );
       
   114 	        aItem.SetUserNameL( userName );
       
   115             pass.Set( userInfo.Right( userInfo.Length()-position-1  ) );
       
   116     		aItem.SetPasswordL( pass );
       
   117             }
       
   118 	    }
       
   119 
       
   120 
       
   121 	HBufC* parsedUrl = HBufC::NewLC( url.UriDes().Length() );
       
   122 	// get the pure Url (without username & password)
       
   123 	TInt position = url.UriDes().LocateF( '@' );
       
   124 	if( ( position != KErrNotFound ) && ( userName.Length() )
       
   125            && ( url.UriDes().Find(userInfo) != KErrNotFound) )
       
   126 		{
       
   127 		parsedUrl->Des().Copy( url.Extract( EUriScheme ) );
       
   128 		_LIT( KDoubleSlash, "://" );
       
   129 		parsedUrl->Des().Append( KDoubleSlash );
       
   130 		parsedUrl->Des().Append( url.UriDes().Right
       
   131 			( url.UriDes().Length() - position - 1  ) );
       
   132 		}
       
   133 	else
       
   134 		{
       
   135 		parsedUrl->Des().Copy( url.UriDes() );
       
   136     	}
       
   137     if( parsedUrl->Length() )
       
   138 		{
       
   139 		aItem.SetUrlL( *parsedUrl );
       
   140 		}
       
   141 	CleanupStack::PopAndDestroy( 1 ); //  parsedUrl
       
   142 	}
       
   143 
       
   144 
       
   145 // ---------------------------------------------------------
       
   146 // Util::StripUrl
       
   147 // ---------------------------------------------------------
       
   148 //
       
   149 TPtrC Util::StripUrl( const TDesC& aUrl )
       
   150 	{
       
   151 	TUriParser url;
       
   152 	TInt startPos;
       
   153 	TInt endPos;
       
   154 	
       
   155 	url.Parse ( aUrl );
       
   156 
       
   157 	//Return parsed url only if we have a scheme. Otherwise return full url
       
   158 	if ( url.IsPresent ( EUriScheme ) )
       
   159 		{
       
   160 		startPos = url.Extract ( EUriScheme ).Length();
       
   161 		//jump over the :// chars (or a mistyped version like :/ or :)
       
   162 		while ( startPos < url.UriDes().Length() &&
       
   163 				( aUrl.Mid( startPos, 1) == _L("/") || 
       
   164 				  aUrl.Mid( startPos, 1) == _L(":") ) )
       
   165 			{
       
   166 			startPos++;
       
   167 			}
       
   168 		endPos = url.UriDes().Length() - url.Extract ( EUriFragment ).Length();
       
   169 		}
       
   170 	else
       
   171 		{
       
   172 		startPos = 0;
       
   173 		endPos = url.UriDes().Length();
       
   174 		}
       
   175 	
       
   176 	return aUrl.Mid( startPos, endPos - startPos );
       
   177 	}
       
   178 
       
   179 // ---------------------------------------------------------
       
   180 // Util::UrlFromFileNameLC
       
   181 // ---------------------------------------------------------
       
   182 //
       
   183 HBufC* Util::UrlFromFileNameLC( const TDesC& aFileName )
       
   184 	{
       
   185     HBufC* buf = HBufC::NewLC
       
   186         ( aFileName.Length() + KWmlValueFileSlashSlashSlashStr().Length() );
       
   187 
       
   188     HBufC* url = HBufC::NewLC
       
   189         ( aFileName.Length() + KWmlValueFileSlashSlashSlashStr().Length() );
       
   190     url->Des().Append( KWmlValueFileSlashSlashSlashStr );
       
   191     url->Des().Append( aFileName );
       
   192 
       
   193     for ( TInt i = 0; i < url->Length(); i++ )
       
   194         {
       
   195         if ( !url->Mid( i, 1 ).Compare( KWmlBackSlash ) )
       
   196             {
       
   197             buf->Des().Append( TChar('/') );
       
   198             }
       
   199         else
       
   200             {
       
   201             buf->Des().Append( url->Mid( i, 1 ) );
       
   202             }
       
   203         }
       
   204     CleanupStack::PopAndDestroy();  // url
       
   205     return buf;
       
   206 	}
       
   207 
       
   208 // ---------------------------------------------------------
       
   209 // Util::FFSSpaceBelowCriticalLevelL
       
   210 // ---------------------------------------------------------
       
   211 //
       
   212 TBool Util::FFSSpaceBelowCriticalLevelL
       
   213         ( TBool aShowErrorNote, TInt aBytesToWrite /*=0*/ )
       
   214     {
       
   215     TBool ret( EFalse );
       
   216     if ( SysUtil::FFSSpaceBelowCriticalLevelL
       
   217                 ( &(CCoeEnv::Static()->FsSession()), aBytesToWrite ) )
       
   218         {
       
   219         ret = ETrue;
       
   220         if ( aShowErrorNote )
       
   221             {
       
   222             CErrorUI* errorUi = CErrorUI::NewLC( *(CCoeEnv::Static()) );
       
   223             errorUi->ShowGlobalErrorNoteL( KErrDiskFull );
       
   224             CleanupStack::PopAndDestroy();  // errorUi
       
   225             }
       
   226         }
       
   227     return ret;
       
   228     }
       
   229 
       
   230 // ---------------------------------------------------------
       
   231 // Util::CheckBookmarkApL
       
   232 // ---------------------------------------------------------
       
   233 //
       
   234 TBool Util::CheckBookmarkApL(
       
   235         const MApiProvider& aApiProvider,
       
   236         const TFavouritesWapAp& aItem )
       
   237     {
       
   238     TBool apValid( EFalse );
       
   239 
       
   240     if ( !aItem.IsNull() && !aItem.IsDefault() )
       
   241         {
       
   242         CCommsDatabase& db = aApiProvider.CommsModel().CommsDb();
       
   243         
       
   244         CApUtils* apUtils = CApUtils::NewLC( db );
       
   245         // if error, wapId is not found.
       
   246         TRAPD( err, apValid = apUtils->WapApExistsL( aItem.ApId() ) );
       
   247         if ( /* !apValid || */ err )
       
   248             {
       
   249             apValid = EFalse;
       
   250             }
       
   251         CleanupStack::PopAndDestroy();  // apUtils
       
   252         }
       
   253     else 
       
   254         {
       
   255         apValid = ETrue;
       
   256         }
       
   257     return apValid;
       
   258     }
       
   259 
       
   260 // ---------------------------------------------------------
       
   261 // Util::IsValidAPFromParamL
       
   262 // ---------------------------------------------------------
       
   263 //
       
   264 TBool Util::IsValidAPFromParamL(
       
   265         const MApiProvider& aApiProvider,
       
   266         TUint32 aIAPid )
       
   267     {
       
   268     CCommsDatabase& db = aApiProvider.CommsModel().CommsDb();
       
   269     CApUtils* apUtils = CApUtils::NewLC( db );
       
   270     TBool retVal = apUtils->WapApExistsL( aIAPid );
       
   271     CleanupStack::PopAndDestroy();  // apUtils
       
   272     return retVal;
       
   273     }
       
   274 
       
   275 // ---------------------------------------------------------
       
   276 // Util::CheckApValidityL
       
   277 // ---------------------------------------------------------
       
   278 //
       
   279 TBool Util::CheckApValidityL(
       
   280         const MApiProvider& aApiProvider,
       
   281         const TUint32 aItem )
       
   282     {
       
   283     CCommsDatabase& db = aApiProvider.CommsModel().CommsDb();
       
   284     CApUtils* apUtils = CApUtils::NewLC( db );
       
   285     TBool apValid( EFalse );
       
   286 
       
   287     // if error, IapId is not found.
       
   288     TRAPD( err, apUtils->IapIdFromWapIdL( aItem ) );
       
   289     if( err == KErrNone )
       
   290         {
       
   291         apValid = ETrue;
       
   292         }
       
   293     CleanupStack::PopAndDestroy(); // apUtils
       
   294 
       
   295     return apValid;
       
   296     }
       
   297 
       
   298 // ---------------------------------------------------------
       
   299 // Util::IapIdFromWapIdL
       
   300 // ---------------------------------------------------------
       
   301 //
       
   302 TUint32 Util::IapIdFromWapIdL( const MApiProvider& aApiProvider, const TUint32 aItem )
       
   303 {
       
   304     CCommsDatabase& db = aApiProvider.CommsModel().CommsDb();
       
   305     CApUtils* apUtils = CApUtils::NewLC( db );
       
   306     TUint32 ap = apUtils->IapIdFromWapIdL( aItem );
       
   307     CleanupStack::PopAndDestroy();  // apUtil
       
   308     return ap;
       
   309 }
       
   310 
       
   311 // ---------------------------------------------------------
       
   312 // Util::WapIdFromIapIdL
       
   313 // ---------------------------------------------------------
       
   314 //
       
   315 TUint32 Util::WapIdFromIapIdL( const MApiProvider& aApiProvider, const TUint32 aItem )
       
   316 {
       
   317     CCommsDatabase& db = aApiProvider.CommsModel().CommsDb();
       
   318     CApUtils* apUtils = CApUtils::NewLC( db );
       
   319     TUint32 ap = apUtils->WapIdFromIapIdL( aItem );
       
   320     CleanupStack::PopAndDestroy();  // apUtil
       
   321     return ap;
       
   322 }
       
   323 
       
   324 // ---------------------------------------------------------
       
   325 // Util::EncodeSpaces
       
   326 // ---------------------------------------------------------
       
   327 //
       
   328 void Util::EncodeSpaces(HBufC*& aString)
       
   329     {
       
   330 	_LIT(KSpace," ");
       
   331 	_LIT(KSpaceEncoded,"%20");
       
   332 
       
   333     TInt space(KErrNotFound);
       
   334     TInt spaces(0);
       
   335     TPtr ptr = aString->Des();
       
   336     
       
   337     for (TInt i = ptr.Length()-1; i > 0 ; i--)
       
   338         {
       
   339         if (ptr[i] == ' ')
       
   340             {
       
   341             spaces++;
       
   342             space = i;
       
   343             }
       
   344         }
       
   345         
       
   346     if (spaces)
       
   347         {
       
   348         TInt newLen = aString->Length() + (spaces * KSpaceEncoded().Length());
       
   349         TRAPD(err,aString = aString->ReAllocL(newLen));
       
   350         if (err != KErrNone)
       
   351             {
       
   352             return;  // aString was not changed
       
   353             }
       
   354         ptr.Set( aString->Des() );
       
   355        
       
   356         }
       
   357         
       
   358 	while (space != KErrNotFound)
       
   359     	{
       
   360     	ptr.Replace(space,1,KSpaceEncoded);
       
   361     	space = ptr.Find(KSpace);
       
   362     	}
       
   363         
       
   364     }
       
   365 
       
   366 // ---------------------------------------------------------
       
   367 // Util::AllocateUrlWithSchemeL
       
   368 // ---------------------------------------------------------
       
   369 //
       
   370 HBufC* Util::AllocateUrlWithSchemeL( const TDesC& aUrl )
       
   371     {
       
   372     _LIT( KBrowserDefaultScheme, "http://" );
       
   373     TBool addDefaultScheme( EFalse );
       
   374     TInt len = aUrl.Length(); 
       
   375 
       
   376     // Do not check return value from parse. If the caller wants to allocate a
       
   377     // badly formed url then let them do so.
       
   378     TUriParser url;
       
   379     url.Parse( aUrl ); 
       
   380 
       
   381     if( !url.IsPresent( EUriScheme ) )
       
   382         { 
       
   383         addDefaultScheme = ETrue; 
       
   384         len = len + KBrowserDefaultScheme().Length();
       
   385         }
       
   386 
       
   387     HBufC* urlBuffer = HBufC::NewL( len );
       
   388     TPtr ptr = urlBuffer->Des();
       
   389 
       
   390     if ( addDefaultScheme )
       
   391         {
       
   392         ptr.Copy( KBrowserDefaultScheme );
       
   393         }
       
   394     ptr.Append( aUrl );
       
   395 
       
   396     // Handle rare case that the url needs escape encoding.
       
   397     // Below function will realloc enough space for encoded urlBuffer.
       
   398     Util::EncodeSpaces( urlBuffer );
       
   399 
       
   400     return urlBuffer;
       
   401     }
       
   402 
       
   403 
       
   404 // End of File