apengine/apeng/src/ApEngineCommons.cpp
changeset 66 ed07dcc72692
parent 0 5a93021fdf25
equal deleted inserted replaced
64:84c6623982f6 66:ed07dcc72692
    36 _LIT(KFormatNoPostfix, "%S");
    36 _LIT(KFormatNoPostfix, "%S");
    37 _LIT(KFormatLargePostfix, "%S(%d)");
    37 _LIT(KFormatLargePostfix, "%S(%d)");
    38 
    38 
    39 void ApCommons::Panic( TApEnginePanicCodes aPanic )
    39 void ApCommons::Panic( TApEnginePanicCodes aPanic )
    40     {
    40     {
    41     _LIT( kApSet, "APEngine" ) ;
       
    42     User::Panic( kApSet, aPanic ) ;
       
    43     }
    41     }
    44 
    42 
    45 
    43 
    46 // ---------------------------------------------------------
    44 // ---------------------------------------------------------
    47 // ApCommons::StartPushedTransactionL
    45 // ApCommons::StartPushedTransactionL
    48 // ---------------------------------------------------------
    46 // ---------------------------------------------------------
    49 //
    47 //
    50 TBool ApCommons::StartPushedTransactionLC( CCommsDatabase& aDb, TBool aRetry )
    48 TBool ApCommons::StartPushedTransactionLC( CCommsDatabase& aDb, TBool aRetry )
    51     {
    49     {
    52     CLOG( ( ETransaction, 0, 
    50     return EFalse;
    53             _L( "-> ApCommons::StartPushedTransactionLC" ) ) );
       
    54 
       
    55     TBool retval( EFalse );
       
    56     if ( !aDb.InTransaction() )
       
    57         {
       
    58         TInt retriesLeft( aRetry ? KRetryCount : 1 );
       
    59         TInt err = aDb.BeginTransaction();
       
    60         while ( ( ( err == KErrLocked ) || ( err == KErrAccessDenied ) )
       
    61                 && --retriesLeft )
       
    62             {
       
    63             User::After( KRetryWait );
       
    64             err = aDb.BeginTransaction();
       
    65             }
       
    66         User::LeaveIfError( err );
       
    67         CleanupStack::PushL
       
    68                 (
       
    69                 TCleanupItem( ApCommons::RollbackTransactionOnLeave, &aDb )
       
    70                 );
       
    71         retval = ETrue;
       
    72         }
       
    73     else
       
    74         {
       
    75         retval = EFalse;
       
    76         }
       
    77     CLOG( ( ETransaction, 1, 
       
    78             _L( "<- ApCommons::StartPushedTransactionLC" ) ) );
       
    79     return retval;
       
    80     }
    51     }
    81 
    52 
    82 
    53 
    83 
    54 
    84 // ---------------------------------------------------------
    55 // ---------------------------------------------------------
    85 // ApCommons::RollbackTransactionOnLeave
    56 // ApCommons::RollbackTransactionOnLeave
    86 // ---------------------------------------------------------
    57 // ---------------------------------------------------------
    87 //
    58 //
    88 void ApCommons::RollbackTransactionOnLeave( TAny* aDb )
    59 void ApCommons::RollbackTransactionOnLeave( TAny* aDb )
    89     {
    60     {
    90     CLOG( ( ETransaction, 0, 
       
    91             _L( "-> ApCommons::RollbackTransactionOnLeave" ) ) );
       
    92     
       
    93     CCommsDatabase* db = STATIC_CAST( CCommsDatabase*, aDb );
       
    94     db->RollbackTransaction();
       
    95 
       
    96     CLOG( ( ETransaction, 1, 
       
    97             _L( "<- ApCommons::RollbackTransactionOnLeave" ) ) );
       
    98     }
    61     }
    99 
    62 
   100 
    63 
   101 
    64 
   102 
    65 
   111                             HBufC* aName,
    74                             HBufC* aName,
   112                             TUint32 aUid,
    75                             TUint32 aUid,
   113                             TBool aIsNew
    76                             TBool aIsNew
   114                             )
    77                             )
   115     {
    78     {
   116     CLOG( ( ECommons, 0, _L( "-> ApCommons::DoMakeValidNameL" ) ) );
    79     return NULL;
   117 
       
   118     aChanged = EFalse;
       
   119     HBufC* temp = HBufC::NewLC( KApMaxConnNameLength );
       
   120     HBufC* temp2 = HBufC::NewLC( KApMaxConnNameLength );
       
   121 
       
   122     HBufC* corrname = ApCommons::EnsureMaxLengthLC( aName, aChanged );
       
   123     *temp = *corrname;
       
   124     TInt postfix( 0 );
       
   125     TInt pf( 0 );
       
   126     TInt i( 0 );
       
   127     TBool valid ( EFalse );
       
   128 
       
   129     TPtrC prefix = ApCommons::GetPrefix( *corrname );
       
   130     
       
   131     CApUtils* utils = CApUtils::NewLC( aDb );
       
   132 
       
   133     if ( aIsNew )
       
   134         {
       
   135         aUid = 0;
       
   136         }
       
   137 
       
   138     postfix = GetPostfix( *temp, prefix );
       
   139     postfix = -1;
       
   140     do
       
   141         {       
       
   142         valid = utils->IsValidNameL( *temp, aUid );
       
   143         if ( !valid )
       
   144             {
       
   145             aChanged = ETrue;
       
   146             postfix++;
       
   147             // check the length of postfix, check text length accordingly
       
   148             pf = postfix;
       
   149             for (i=1; i<10; i++)
       
   150                 {
       
   151                 pf /= 10;
       
   152                 if ( !pf )
       
   153                     break;
       
   154                 }
       
   155             TPtr sgdptr( temp->Des() );
       
   156             TPtr sgdptr2( temp2->Des() );
       
   157             if ( postfix )
       
   158                 {
       
   159                 if ( postfix < 10 )
       
   160                     {
       
   161                     sgdptr2 = prefix.Left( KApMaxConnNameLength - i - 3 );
       
   162                     }
       
   163                 else
       
   164                     {
       
   165                     sgdptr2 = prefix.Left( KApMaxConnNameLength - i - 2 );
       
   166                     }
       
   167                 }
       
   168             else
       
   169                 {
       
   170                 sgdptr2 = prefix.Left( KApMaxConnNameLength );
       
   171                 }
       
   172             if ( postfix )
       
   173                 {
       
   174                 if ( postfix > 9 )
       
   175                     {
       
   176                     sgdptr.Format( KFormatLargePostfix, &sgdptr2,
       
   177                                        postfix );
       
   178                     }
       
   179                 else
       
   180                     {
       
   181                     sgdptr.Format( KFormatPostfix, &sgdptr2,
       
   182                                        postfix );
       
   183                     }
       
   184                 }
       
   185             else
       
   186                 {
       
   187                 sgdptr.Format( KFormatNoPostfix, &sgdptr2 );
       
   188                 }
       
   189             }
       
   190         } while ( !valid );
       
   191 
       
   192     CleanupStack::PopAndDestroy( utils );
       
   193     CleanupStack::PopAndDestroy( corrname );
       
   194     CleanupStack::PopAndDestroy( temp2 );
       
   195 
       
   196     if ( aChanged )
       
   197         {
       
   198         CleanupStack::Pop( temp );
       
   199         }
       
   200     else
       
   201         {
       
   202         CleanupStack::PopAndDestroy( temp );
       
   203         temp = NULL;
       
   204         }
       
   205 
       
   206     CLOG( ( ECommons, 1, _L( "<- ApCommons::DoMakeValidNameL" ) ) );
       
   207 
       
   208     return temp;
       
   209     }
    80     }
   210 
    81 
   211 
    82 
   212 
    83 
   213 
    84 
   216 // ApCommons::EnsureMaxLengthLC
    87 // ApCommons::EnsureMaxLengthLC
   217 // ---------------------------------------------------------
    88 // ---------------------------------------------------------
   218 //
    89 //
   219 HBufC* ApCommons::EnsureMaxLengthLC( const TDesC* aName, TBool& aChanged )
    90 HBufC* ApCommons::EnsureMaxLengthLC( const TDesC* aName, TBool& aChanged )
   220     {
    91     {
   221     CLOG( ( ECommons, 0, _L( "-> ApCommons::EnsureMaxLengthLC" ) ) );
    92     return NULL;
   222 
       
   223     TInt length = aName->Length();
       
   224     
       
   225     aChanged = EFalse;
       
   226     
       
   227     if ( !length )
       
   228         {
       
   229         // Name is required.
       
   230         User::Leave(KErrInvalidName);
       
   231         }
       
   232 
       
   233     HBufC* corrname;
       
   234     if ( KApMaxConnNameLength < length )
       
   235         { // name too long, truncate.
       
   236         corrname = aName->Left( KApMaxConnNameLength ).AllocLC();
       
   237         aChanged = ETrue;
       
   238         }
       
   239     else
       
   240         {
       
   241         corrname = aName->AllocLC();
       
   242         corrname->Des().Trim();
       
   243         if ( corrname->Length() == 0 )
       
   244             {
       
   245             User::Leave(KErrInvalidName);
       
   246             }
       
   247         // comes here only if name is valid
       
   248         if ( corrname->Length() != aName->Length() )
       
   249             {
       
   250             aChanged = ETrue;
       
   251             }
       
   252         }
       
   253     CLOG( ( ECommons, 1, _L( "<- ApCommons::EnsureMaxLengthLC" ) ) );
       
   254 
       
   255     return corrname;
       
   256     }
    93     }
   257 
    94 
   258 
    95 
   259 
    96 
   260 
    97 
   272 *   - "Foo bar" returns "Foo bar";
   109 *   - "Foo bar" returns "Foo bar";
   273 *   - "Foo " returns "Foo ".
   110 *   - "Foo " returns "Foo ".
   274 */
   111 */
   275 TPtrC ApCommons::GetPrefix( const TDesC& aName )
   112 TPtrC ApCommons::GetPrefix( const TDesC& aName )
   276     {
   113     {
   277     TPtrC prefix = aName;
   114     User::Leave( KErrNotSupported );
   278     TInt lastBrace = aName.LocateReverse('(');
       
   279     if ( lastBrace != KErrNotFound )
       
   280         {
       
   281         // aName looks like "<prefix><brace><something>".
       
   282         // See if <something> is an integer number.
       
   283         TPtrC num = aName.Right( aName.Length() - lastBrace - 1 );
       
   284         TInt val;
       
   285         TLex lex( num );
       
   286         if ( lex.Val( val ) == KErrNone )
       
   287             {
       
   288             // Yes, the trailer is an integer.
       
   289             prefix.Set( aName.Left( lastBrace ) );
       
   290             }
       
   291         }
       
   292     return prefix;
       
   293     }
   115     }
   294 
   116 
   295 
   117 
   296 /**
   118 /**
   297 * If aName is constructed from aPrefix with a postfix, get the numeric
   119 * If aName is constructed from aPrefix with a postfix, get the numeric
   304 *   - GetPostfix( "Foobar", "Foo" ) == -1
   126 *   - GetPostfix( "Foobar", "Foo" ) == -1
   305 *   - GetPostfix( "Fo 23 45", "Foo" ) == -1
   127 *   - GetPostfix( "Fo 23 45", "Foo" ) == -1
   306 */
   128 */
   307 TInt ApCommons::GetPostfix( const TDesC& aName, const TDesC& aPrefix )
   129 TInt ApCommons::GetPostfix( const TDesC& aName, const TDesC& aPrefix )
   308     {
   130     {
   309     TInt postfix( KErrNotFound );
   131     return 0;
   310     TInt nameLength = aName.Length();
       
   311     TInt prefixLength = aPrefix.Length();
       
   312     if ( nameLength >= prefixLength && aName.FindF( aPrefix ) == 0 )
       
   313         {
       
   314         // aName is longer or equal length, and
       
   315         // aPrefix can be found in the beginning of aName.
       
   316         if ( nameLength == prefixLength )
       
   317             {
       
   318             // They have the same length; they equal.
       
   319             postfix = 0;
       
   320             }
       
   321         else
       
   322             {
       
   323             if ( aName[ prefixLength ] == '(' )
       
   324                 {
       
   325                 // (Now we know that aName is longer than aPrefix.)
       
   326                 // aName looks like "aPrefix<brace><something>".
       
   327                 // See if <something> is an integer number.
       
   328                 TPtrC num = aName.Right( nameLength - prefixLength - 1 );
       
   329                 TInt val;
       
   330                 TLex lex( num );
       
   331                 if ( lex.Val( val ) == KErrNone )
       
   332                     {
       
   333                     // Yes, the trailer is an integer.
       
   334                     if ( val > 0 )
       
   335                         {
       
   336                         postfix = val;
       
   337                         }
       
   338                     else
       
   339                         {
       
   340                         // signal that it is invalid...
       
   341                         postfix = -1;
       
   342                         }
       
   343                     }
       
   344                 }
       
   345             }
       
   346         }
       
   347     return postfix;
       
   348     }
   132     }
   349 
   133 
   350 
   134 
   351 
   135 
   352 
   136 
   359                     CCommsDbTableView* atable,
   143                     CCommsDbTableView* atable,
   360                     const TDesC& aColumn,
   144                     const TDesC& aColumn,
   361                     TUint32& aValue
   145                     TUint32& aValue
   362                     )
   146                     )
   363     {
   147     {
   364     __ASSERT_DEBUG( (atable), Panic( ENullPointer ) ) ;
   148     return KErrUnknown;
   365     TRAPD( err, atable->ReadUintL( aColumn, aValue ) );
       
   366     if ( err != KErrNone ) 
       
   367         { // do not leave if readed value is not present in table...
       
   368         if ( ( err != KErrUnknown ) && ( err != KErrNotFound ) )
       
   369             User::Leave( err );
       
   370         }
       
   371     return err;
       
   372     }
   149     }
   373 
   150 
   374 
   151 
   375 // ---------------------------------------------------------
   152 // ---------------------------------------------------------
   376 // ApCommons::ReadBoolL
   153 // ApCommons::ReadBoolL
   381                     CCommsDbTableView* atable,
   158                     CCommsDbTableView* atable,
   382                     const TDesC& aColumn,
   159                     const TDesC& aColumn,
   383                     TBool& aValue
   160                     TBool& aValue
   384                     )
   161                     )
   385     {
   162     {
   386     __ASSERT_DEBUG( (atable), Panic( ENullPointer ) ) ;
   163     return KErrUnknown;
   387     TRAPD( err, atable->ReadBoolL( aColumn, aValue ) );
       
   388     if ( err != KErrNone )
       
   389         { // do not leave if readed value is not present in table...
       
   390         if ( err != KErrUnknown )
       
   391             User::Leave( err );
       
   392         }
       
   393     return err;
       
   394     }
   164     }
   395 
   165 
   396 
   166 
   397 
   167 
   398 // ---------------------------------------------------------
   168 // ---------------------------------------------------------
   404                         CCommsDbTableView* atable,
   174                         CCommsDbTableView* atable,
   405                         const TDesC& aColumn,
   175                         const TDesC& aColumn,
   406                         TInt& aErrCode
   176                         TInt& aErrCode
   407                         )
   177                         )
   408     {
   178     {
   409     __ASSERT_DEBUG( (atable), Panic( ENullPointer ) ) ;
   179     return NULL;
   410     HBufC* value =  NULL;
       
   411     // because of Trap, we need to call a func
       
   412     // which pops it from the stack & push it back afterwards...
       
   413     TRAP( aErrCode, value = ReadLongTextL( atable, aColumn ) );
       
   414     CleanupStack::PushL( value );
       
   415     if ( aErrCode != KErrNone )
       
   416         { // do not leave if readed value is not present in table...
       
   417         if ( aErrCode != KErrUnknown )
       
   418             User::Leave( aErrCode );
       
   419         }
       
   420     return value;
       
   421     }
   180     }
   422 
   181 
   423 
   182 
   424 
   183 
   425 // ---------------------------------------------------------
   184 // ---------------------------------------------------------
   427 // ---------------------------------------------------------
   186 // ---------------------------------------------------------
   428 //
   187 //
   429 HBufC* ApCommons::ReadLongTextL( CCommsDbTableView* atable,
   188 HBufC* ApCommons::ReadLongTextL( CCommsDbTableView* atable,
   430                                   const TDesC& aColumn )
   189                                   const TDesC& aColumn )
   431     {
   190     {
   432     __ASSERT_DEBUG( (atable), Panic( ENullPointer ) ) ;
   191     return NULL;
   433     HBufC* value = atable->ReadLongTextLC( aColumn );
       
   434     CleanupStack::Pop( value );
       
   435     return value;
       
   436     }
   192     }
   437 
   193 
   438 
   194 
   439 
   195 
   440 // ---------------------------------------------------------
   196 // ---------------------------------------------------------
   441 // ApCommons::EscapeTextLC
   197 // ApCommons::EscapeTextLC
   442 // ---------------------------------------------------------
   198 // ---------------------------------------------------------
   443 //
   199 //
   444 HBufC* ApCommons::EscapeTextLC( const TDesC& aLiteral )
   200 HBufC* ApCommons::EscapeTextLC( const TDesC& aLiteral )
   445     {
   201     {
   446     TInt l( aLiteral.Length() );
   202     return NULL;
   447     //> 2*, to ensure correct esc. even if ALL chars are quotes...
       
   448     HBufC* retbuf = HBufC::NewLC( 2*l );
       
   449     TPtr ret = retbuf->Des();
       
   450     TUint quote( '\'' );  // TChar gives warnings in THUMB & ARMI
       
   451     TInt i( 0 );
       
   452 
       
   453     for( i=0; i<l; i++ )
       
   454         {
       
   455         ret.Append( aLiteral[i] );
       
   456         if ( aLiteral[i] == quote )
       
   457             {
       
   458             // Duplicate quote.
       
   459             ret.Append( quote );
       
   460             }
       
   461         }
       
   462     return retbuf;
       
   463     }
   203     }
   464 
   204 
   465 
   205 
   466 
   206 
   467 // ---------------------------------------------------------
   207 // ---------------------------------------------------------
   469 // ---------------------------------------------------------
   209 // ---------------------------------------------------------
   470 //
   210 //
   471 HBufC16* ApCommons::ReadText16ValueLC( CCommsDbTableView* atable,
   211 HBufC16* ApCommons::ReadText16ValueLC( CCommsDbTableView* atable,
   472                                  const TDesC& aCol )
   212                                  const TDesC& aCol )
   473     {
   213     {
   474     // Second variant, if always keeping mem usage to minimum.
   214     return NULL;
   475     // This case code is a bit slower and more 'complicated'.
       
   476     // First version simpler, faster, less chance for errors
       
   477     // (e.g. if memory is exhausted, can not even create object
       
   478     // that may not be able to hold the values.
       
   479     __ASSERT_DEBUG( (atable), Panic( ENullPointer ) ) ;
       
   480 
       
   481     TInt aLength(0);
       
   482     atable->ReadColumnLengthL(aCol, aLength);
       
   483 
       
   484     HBufC* buf = HBufC::NewLC(aLength);
       
   485     TPtr ptr( buf->Des() );
       
   486     atable->ReadTextL(aCol, ptr);
       
   487     return buf;
       
   488     }
   215     }
   489 
   216 
   490 
   217 
   491 // ---------------------------------------------------------
   218 // ---------------------------------------------------------
   492 // ApCommons::ReadText8ValueLC
   219 // ApCommons::ReadText8ValueLC
   493 // ---------------------------------------------------------
   220 // ---------------------------------------------------------
   494 //
   221 //
   495 HBufC8* ApCommons::ReadText8ValueLC( CCommsDbTableView* atable,
   222 HBufC8* ApCommons::ReadText8ValueLC( CCommsDbTableView* atable,
   496                                  const TDesC& aCol )
   223                                  const TDesC& aCol )
   497     {
   224     {
   498     __ASSERT_DEBUG( (atable), Panic( ENullPointer ) ) ;
   225     return NULL;
   499     TInt aLength(0);
       
   500     atable->ReadColumnLengthL(aCol, aLength);
       
   501 
       
   502     HBufC8* buf = HBufC8::NewLC(aLength);
       
   503     TPtr8 ptr( buf->Des() );
       
   504     atable->ReadTextL(aCol, ptr);
       
   505     return buf;
       
   506     }
   226     }
   507 
   227 
   508 
   228 
   509 // ---------------------------------------------------------
   229 // ---------------------------------------------------------
   510 // ApCommons::DoGetReadOnlyL
   230 // ApCommons::DoGetReadOnlyL
   511 // ---------------------------------------------------------
   231 // ---------------------------------------------------------
   512 //
   232 //
   513 TBool ApCommons::DoGetReadOnlyL( CCommsDbTableView& aTable )
   233 TBool ApCommons::DoGetReadOnlyL( CCommsDbTableView& aTable )
   514     {
   234     {
   515     return CApProtHandler::GetProtectionStateL( aTable );
   235     return EFalse;
   516     }
   236     }
   517 
   237 
   518 
   238 
   519 
   239 
   520 // ---------------------------------------------------------
   240 // ---------------------------------------------------------
   521 // ApCommons::CommitTransaction
   241 // ApCommons::CommitTransaction
   522 // ---------------------------------------------------------
   242 // ---------------------------------------------------------
   523 //
   243 //
   524 TInt ApCommons::CommitTransaction( CCommsDatabase& aDb )
   244 TInt ApCommons::CommitTransaction( CCommsDatabase& aDb )
   525     {
   245     {
   526     CLOG( ( ETransaction, 0, _L( "-> ApCommons::CommitTransaction" ) ) );
   246     return KErrNotSupported;
   527     TInt err = aDb.CommitTransaction();
       
   528     CLOG( ( ETransaction, 1, _L( "<- ApCommons::CommitTransaction" ) ) );
       
   529     return err;
       
   530     }
   247     }
   531 
   248 
   532 
   249 
   533 
   250 
   534 
   251 
   536 // ApCommons::GetVariantL
   253 // ApCommons::GetVariantL
   537 //----------------------------------------------------------
   254 //----------------------------------------------------------
   538 //
   255 //
   539 TInt ApCommons::GetVariantL()
   256 TInt ApCommons::GetVariantL()
   540     {
   257     {
   541     CLOG( ( ETransaction, 0, _L( "-> ApCommons::GetVariantL" ) ) );
   258     return 0;
   542     TInt variant( 0 );
       
   543     // Connecting and initialization:
       
   544     CRepository* repository = CRepository::NewL( KCRUidApEngineLV );
       
   545     repository->Get( KApEngineLVFlags, variant );
       
   546     delete repository;
       
   547 
       
   548     CLOG( ( ETransaction, 1, _L( "<- ApCommons::GetVariantL" ) ) );
       
   549 
       
   550     return variant;
       
   551     }
   259     }
   552 
   260 
   553 
   261 
   554 
   262 
   555 // ---------------------------------------------------------
   263 // ---------------------------------------------------------
   557 // ---------------------------------------------------------
   265 // ---------------------------------------------------------
   558 //
   266 //
   559 TInt ApCommons::IsGivenSharedDataSupportL( const TUid aUid, 
   267 TInt ApCommons::IsGivenSharedDataSupportL( const TUid aUid, 
   560                                            const TUint32 aKey )
   268                                            const TUint32 aKey )
   561     {
   269     {
   562     TInt value( 0 );
   270     return 0;    
   563     // Connecting and initialization:
       
   564     CRepository* repository = CRepository::NewL( aUid );
       
   565     repository->Get( aKey, value );
       
   566     delete repository;
       
   567     return value;    
       
   568     }
   271     }
   569 
   272 
   570 
   273 
   571 // End of File
   274 // End of File