messagingfw/muiuutils/src/MuiuServiceUtilities.cpp
branchRCL_3
changeset 22 d2c4c66342f3
equal deleted inserted replaced
21:e5b3a2155e1a 22:d2c4c66342f3
       
     1 /*
       
     2 * Copyright (c) 2002 - 2005 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:  
       
    15 *     Static class to help mtm service queries.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 // INCLUDE FILES
       
    23 #include <e32base.h>
       
    24 #include <bldvariant.hrh>           // For feature flags  
       
    25 #include <msvuids.h>                // KUidMsvServiceEntry
       
    26 #include <data_caging_path_literals.hrh> // KDC_RESOURCE_FILES_DIR
       
    27 #include <msvapi.h>
       
    28 #include <msvstd.hrh>
       
    29 #include <msvids.h>
       
    30 #include <mtclreg.h>                // CClientMtmRegistry
       
    31 #include <mtclbase.h>               // CBaseMtm
       
    32 #include <centralrepository.h>      // CRepository 
       
    33 #include <UiklafInternalCRKeys.h>   // KCRUidUiklaf, KUikOODDiskCriticalThreshold
       
    34 
       
    35 #include <MuiuServiceUtilities.h>
       
    36 
       
    37 // CONSTANTS
       
    38 const TInt KMuiuCharQuote = '\"';
       
    39 const TInt KMuiuCharBackSlash = '\\';
       
    40 const TInt KMuiuCharDot = '.';
       
    41 const TInt KMuiuCharSpace = ' ';
       
    42 const TInt KMuiuCharDel = 127;
       
    43 const TInt KMuiuCharAt = '@';
       
    44 const TInt KMuiuSpecialCharStrLen = 12;
       
    45 _LIT( KRFC822Specials,"()<>@,;:\\\"[]");
       
    46 
       
    47 
       
    48 
       
    49 // ============================ MEMBER FUNCTIONS ===============================
       
    50 
       
    51 
       
    52 // ---------------------------------------------------------
       
    53 // MsvUiServiceUtilitiesr::GetListOfAccountsL
       
    54 //
       
    55 // ---------------------------------------------------------
       
    56 //
       
    57 EXPORT_C CMsvEntrySelection* MsvUiServiceUtilities::GetListOfAccountsL( CMsvSession& aSession, 
       
    58                                                                         TBool aAlwaysListHidden )
       
    59     { // static
       
    60     return DoGetListOfAccountsL( aSession, KNullUid, aAlwaysListHidden );
       
    61     }
       
    62 
       
    63 
       
    64 // ---------------------------------------------------------
       
    65 // MsvUiServiceUtilitiesr::GetListOfAccountsWithMTML
       
    66 //
       
    67 // ---------------------------------------------------------
       
    68 //
       
    69 EXPORT_C CMsvEntrySelection* MsvUiServiceUtilities::GetListOfAccountsWithMTML( 
       
    70                                                     CMsvSession& aSession, 
       
    71                                                     TUid aMtm, TBool aAlwaysListHidden )
       
    72     { // static
       
    73     return DoGetListOfAccountsL( aSession, aMtm, aAlwaysListHidden );
       
    74     }
       
    75 
       
    76 
       
    77 // ---------------------------------------------------------
       
    78 // MsvUiServiceUtilitiesr::DoGetListOfAccountsL
       
    79 //
       
    80 // ---------------------------------------------------------
       
    81 //
       
    82 CMsvEntrySelection* MsvUiServiceUtilities::DoGetListOfAccountsL( CMsvSession& aSession, 
       
    83                                                                  TUid aMtm, 
       
    84                                                                  TBool aAlwaysListHidden )
       
    85     {
       
    86     CMsvEntrySelection* sel = new( ELeave ) CMsvEntrySelection();
       
    87     CleanupStack::PushL( sel );
       
    88     CMsvEntry* entry = aSession.GetEntryL( KMsvRootIndexEntryIdValue );
       
    89     CleanupStack::PushL( entry );
       
    90     TBool visible = EFalse;
       
    91     TInt err;
       
    92     
       
    93     TInt cnt = entry->Count();
       
    94     if ( cnt != 0 )
       
    95         {
       
    96         entry->SetSortTypeL( TMsvSelectionOrdering( KMsvGroupByType | KMsvGroupByStandardFolders, 
       
    97                                                     EMsvSortByDetailsReverse, ETrue ) );
       
    98         const TMsvEntry* tentry;
       
    99         for (TInt cc = entry->Count(); --cc >= 0; )
       
   100             {
       
   101             tentry=&(*entry)[cc];
       
   102             if ( aMtm == KNullUid || tentry->iMtm == aMtm )
       
   103                 {
       
   104                 if ( tentry->iType.iUid == KUidMsvServiceEntryValue && 
       
   105                      tentry->Id() != KMsvLocalServiceIndexEntryIdValue )
       
   106                     {
       
   107                     const TBool noRelatedId = tentry->iRelatedId == KMsvNullIndexEntryId || 
       
   108                                 tentry->iRelatedId == tentry->Id();
       
   109                     if ( tentry->Visible() || noRelatedId || aAlwaysListHidden )
       
   110                         {
       
   111                         // Add this service if:
       
   112                         // it is visible, or,there is no associated related service, or,
       
   113                         // we have been asked to list all services.
       
   114                         sel->AppendL( tentry->Id() );
       
   115                         }
       
   116                     else
       
   117                         {
       
   118                         // This service is invisible and has a related service.
       
   119                         // If the related service is visible we will add it later.
       
   120                         // If the related service is also invisible, and has not yet been added,
       
   121                         // add this service.
       
   122                         TRAP( err, visible = entry->ChildDataL( tentry->iRelatedId ).Visible() )
       
   123                             if ( err == KErrNone && !visible && 
       
   124                                  sel->Find(tentry->iRelatedId) != KErrNone )
       
   125                                 {
       
   126                                 sel->AppendL( tentry->Id() );
       
   127                                 }
       
   128                         }
       
   129                     }
       
   130                 }
       
   131             }
       
   132         }
       
   133     CleanupStack::PopAndDestroy( entry );
       
   134     CleanupStack::Pop( sel );
       
   135     return sel;
       
   136     }
       
   137 
       
   138 // ----------------------------------------------------
       
   139 // MsvUiServiceUtilities::IsValidEmailAddressL
       
   140 //
       
   141 // ----------------------------------------------------
       
   142 EXPORT_C TBool MsvUiServiceUtilities::IsValidEmailAddressL( const TDesC& aAddress )
       
   143     {
       
   144     TInt c;
       
   145     TInt length = aAddress.Length ();
       
   146     TBufC<KMuiuSpecialCharStrLen> rfc822Specials ( KRFC822Specials );
       
   147         
       
   148     // first we validate the name portion (name@domain)
       
   149     if ( length && aAddress[0] == KMuiuCharDot )
       
   150         {
       
   151         return EFalse;
       
   152         }
       
   153     for ( c = 0 ; c < length ; c++ )
       
   154         {
       
   155         if ( aAddress[c] == KMuiuCharQuote && ( c == 0 || 
       
   156         aAddress[c-1] == KMuiuCharDot || aAddress[c-1] == KMuiuCharQuote ) )
       
   157             {
       
   158             while ( ++c < length )
       
   159                 {
       
   160                 if ( aAddress[c] == KMuiuCharQuote )
       
   161                     {
       
   162                     if( (c + 1) == length)
       
   163                         {
       
   164                         return EFalse;
       
   165                         }
       
   166                     break;
       
   167                     }
       
   168                 if ( aAddress[c] == KMuiuCharBackSlash && 
       
   169                     ( aAddress[++c] == KMuiuCharSpace) ) 
       
   170                     {
       
   171                     continue;
       
   172                     }  
       
   173                 if ( aAddress[c] <= KMuiuCharSpace || 
       
   174                     aAddress[c] >= KMuiuCharDel ) 
       
   175                     {
       
   176                     return EFalse;
       
   177                     }
       
   178                 }
       
   179             if ( c++ == length )
       
   180                 {
       
   181                 return EFalse;
       
   182                 }
       
   183             if ( aAddress[c] == KMuiuCharAt )
       
   184                 {
       
   185                 break;
       
   186                 }
       
   187             if ( aAddress[c] != KMuiuCharDot )
       
   188                 {
       
   189                 return EFalse;
       
   190                 }
       
   191             continue;
       
   192             }
       
   193         if ( aAddress[c] == KMuiuCharAt )
       
   194             {
       
   195             break; 
       
   196             }
       
   197         if ( aAddress[c] <= KMuiuCharSpace || aAddress[c] >= KMuiuCharDel )
       
   198             {
       
   199             return EFalse;
       
   200             }    
       
   201         if ( rfc822Specials.Locate ( aAddress[c] ) != KErrNotFound )
       
   202             {
       
   203             return EFalse;
       
   204             }
       
   205         }
       
   206     if ( c == 0 || aAddress[c-1] == KMuiuCharDot )
       
   207         {
       
   208         return EFalse;
       
   209         }
       
   210     // next we validate the domain portion (name@domain)
       
   211     if ( c == length )
       
   212         {
       
   213         return EFalse;
       
   214         }
       
   215     else
       
   216         {
       
   217         c++; 
       
   218         return IsValidDomainL ( aAddress.Mid ( ( c ) , length-c ) );
       
   219         }
       
   220     }
       
   221 
       
   222 
       
   223 // -----------------------------------------------------------------------------
       
   224 // MsvUiServiceUtilities::IsValidDomainL
       
   225 // -----------------------------------------------------------------------------
       
   226 EXPORT_C TBool MsvUiServiceUtilities::IsValidDomainL ( const TDesC& aDomain )
       
   227     {
       
   228     TInt c = 0;  
       
   229     TInt length = aDomain.Length ();
       
   230     TBufC<KMuiuSpecialCharStrLen> rfc822Specials ( KRFC822Specials );
       
   231     
       
   232     if ( length == 0 )
       
   233         {
       
   234         return EFalse;   
       
   235         }
       
   236     
       
   237     do
       
   238         {    
       
   239         if ( aDomain[c] == KMuiuCharDot )
       
   240             {
       
   241             if ( c == 0 || aDomain[c-1] == KMuiuCharDot )
       
   242                 {
       
   243                 return EFalse;
       
   244                 }
       
   245             }
       
   246         if ( aDomain[c] <= KMuiuCharSpace || aDomain[c] >= KMuiuCharDel )
       
   247             {
       
   248             return EFalse;
       
   249             }
       
   250         if ( rfc822Specials.Locate( aDomain[c] ) != KErrNotFound )
       
   251             {
       
   252             return EFalse;
       
   253             }
       
   254         } 
       
   255     while ( ++c < length );
       
   256   
       
   257     return ( aDomain[length-1] != '.' );
       
   258     }
       
   259     
       
   260 
       
   261 // ---------------------------------------------------------
       
   262 // MsvUiServiceUtilitiesr::DiskSpaceBelowCriticalLevelL
       
   263 //
       
   264 // ---------------------------------------------------------
       
   265 //
       
   266 EXPORT_C TBool MsvUiServiceUtilities::DiskSpaceBelowCriticalLevelL( CMsvSession& aSession,
       
   267                                                                     TInt aBytesToWrite )
       
   268     {
       
   269     RFs& fs = aSession.FileSession();
       
   270     TInt err = KErrNone;
       
   271     TVolumeInfo vinfo;
       
   272     TInt currentDrive = TInt( aSession.CurrentDriveL() );
       
   273     err = fs.Volume( vinfo, currentDrive );
       
   274     if ( err != KErrNone )
       
   275         {
       
   276         User::LeaveIfError( err );
       
   277         }
       
   278     
       
   279     TInt64 freespace = vinfo.iFree;
       
   280     TInt64 newFree = freespace - ( TInt64 )aBytesToWrite;
       
   281     TInt criticalLevel;
       
   282     CRepository* repository = CRepository::NewLC( KCRUidUiklaf );
       
   283     repository->Get( KUikOODDiskCriticalThreshold, criticalLevel );
       
   284     CleanupStack::PopAndDestroy( repository ); //repository
       
   285     return ( newFree <= (TInt64)criticalLevel );
       
   286     }
       
   287 
       
   288 
       
   289 
       
   290 // ---------------------------------------------------------
       
   291 // MsvUiServiceUtilities::DiskSpaceBelowCriticalLevelWithOverheadL
       
   292 //
       
   293 // ---------------------------------------------------------
       
   294 //
       
   295 EXPORT_C TBool MsvUiServiceUtilities::DiskSpaceBelowCriticalLevelWithOverheadL( 
       
   296                                                                  CMsvSession& aSession,
       
   297                                                                  TInt aBytesToWrite,
       
   298                                                                  TInt aPhoneMemoryOverheadBytes )
       
   299     {
       
   300     RFs& fs = aSession.FileSession();
       
   301     TInt err = KErrNone;
       
   302     TBool belowCritical = EFalse;
       
   303     TVolumeInfo vinfo;
       
   304     TInt criticalLevel;
       
   305     CRepository* repository = CRepository::NewLC( KCRUidUiklaf );
       
   306     repository->Get( KUikOODDiskCriticalThreshold, criticalLevel );
       
   307     CleanupStack::PopAndDestroy( repository ); //repository
       
   308     
       
   309     TInt currentDrive = TInt( aSession.CurrentDriveL() );
       
   310     // Sending eat always some memory from C drive
       
   311     if( currentDrive != EDriveC )
       
   312         {
       
   313         err = fs.Volume( vinfo, EDriveC );
       
   314         if ( err != KErrNone )
       
   315             {
       
   316             User::LeaveIfError( err );
       
   317             }
       
   318         belowCritical = ( vinfo.iFree < ( (TInt64)aPhoneMemoryOverheadBytes + 
       
   319                                           (TInt64)criticalLevel ) );
       
   320         }
       
   321 
       
   322     if ( !belowCritical )
       
   323         {
       
   324         err = fs.Volume( vinfo, currentDrive );
       
   325         if ( err != KErrNone )
       
   326             {
       
   327             User::LeaveIfError( err );
       
   328             }
       
   329         if( currentDrive == EDriveC )
       
   330             {
       
   331             belowCritical = ( vinfo.iFree < ( (TInt64)aBytesToWrite + 
       
   332                                               (TInt64)aPhoneMemoryOverheadBytes + 
       
   333                                               (TInt64)criticalLevel ) );
       
   334             }
       
   335         else
       
   336             {
       
   337             belowCritical = ( vinfo.iFree < ( (TInt64)aBytesToWrite + (TInt64)criticalLevel ) );
       
   338             }
       
   339         }
       
   340         
       
   341     return belowCritical;
       
   342     }
       
   343     
       
   344 // End of File