photosgallery/viewframework/medialists/src/glxerrormanager.cpp
branchRCL_3
changeset 60 5b3385a43d68
equal deleted inserted replaced
59:8e5f6eea9c9f 60:5b3385a43d68
       
     1 /*
       
     2 * Copyright (c) 2008-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:    Utility class to manage attribute retrieval errors
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include "glxerrormanager.h"
       
    22 
       
    23 //#include <glxerrors.h>
       
    24 
       
    25 #include "glxfetcherrorarray.h"
       
    26 #include "glxcachemanager.h"
       
    27 #include "glxmedialist.h"
       
    28 #include <glxtracer.h>                         // For Logs
       
    29 #include <glxlog.h>                         // For Logs
       
    30 
       
    31 /** Error attribute content ID */
       
    32 const TInt KGlxErrorContentId = 0x200071AC;
       
    33 const TMPXAttributeData KGlxMediaErrorArray = { KGlxErrorContentId, 0x01 }; // CGlxFetchErrorArray
       
    34 
       
    35 const TInt KGlxErrorTimeOut = -10033;
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // Return errors
       
    39 // -----------------------------------------------------------------------------
       
    40 //
       
    41 inline CGlxFetchErrorArray* Errors( const CGlxMedia& aMedia ) 
       
    42     {
       
    43     TRACER("GlxErrorManager::Errors");
       
    44     
       
    45     return const_cast<CGlxFetchErrorArray*>(
       
    46         static_cast<const CGlxFetchErrorArray*>( aMedia.ValueCObject( 
       
    47             KGlxMediaErrorArray ) ) );
       
    48     }
       
    49 
       
    50 // -----------------------------------------------------------------------------
       
    51 // SetAttributeErrorL
       
    52 // -----------------------------------------------------------------------------
       
    53 //
       
    54 void GlxErrorManager::SetAttributeErrorL(CGlxMedia* aItem, const RArray<TMPXAttribute>& aAttributes, TInt aError)
       
    55     {
       
    56     TRACER("GlxErrorManager::SetAttributeErrorL");
       
    57     
       
    58     if ( aItem )
       
    59         {
       
    60         CGlxFetchErrorArray* errorArray = NULL;
       
    61             
       
    62         if ( aItem->IsSupported(KGlxMediaErrorArray) )
       
    63             {
       
    64     		const CGlxFetchErrorArray* existingArray = static_cast<const CGlxFetchErrorArray*>(aItem->ValueCObject(KGlxMediaErrorArray));
       
    65     		
       
    66     		if ( existingArray )
       
    67     		    {
       
    68                 errorArray = CGlxFetchErrorArray::NewLC(existingArray);
       
    69                 }
       
    70             }
       
    71             
       
    72         if ( !errorArray )            
       
    73             {
       
    74             errorArray = new(ELeave) CGlxFetchErrorArray();
       
    75             CleanupStack::PushL(errorArray);
       
    76             }
       
    77 
       
    78         TInt attrCount = aAttributes.Count();
       
    79         
       
    80         for ( TInt attrIndex = 0; attrIndex < attrCount; attrIndex++ )
       
    81             {
       
    82             errorArray->AddErrorL(TGlxFetchError(aAttributes[attrIndex], aError));
       
    83             }
       
    84         
       
    85         aItem->SetCObjectValueL(KGlxMediaErrorArray, errorArray);
       
    86 
       
    87         CleanupStack::Pop(errorArray);
       
    88         }            
       
    89     }
       
    90     
       
    91 // -----------------------------------------------------------------------------
       
    92 // HasAttributeErrorL
       
    93 // -----------------------------------------------------------------------------
       
    94 //
       
    95 EXPORT_C TInt GlxErrorManager::HasAttributeErrorL(const CGlxMedia* aMedia, 
       
    96         const TMPXAttribute& aAttribute)
       
    97     {
       
    98     TRACER("GlxErrorManager::HasAttributeErrorL");
       
    99     
       
   100     TInt retVal = KErrNone;
       
   101     
       
   102     if ( aMedia )
       
   103         {
       
   104         const CGlxFetchErrorArray* errors = Errors( *aMedia );
       
   105 		
       
   106 		if ( errors )
       
   107 		    {
       
   108 		    TInt index = errors->FindError(aAttribute);
       
   109 		    if ( KErrNotFound != index )
       
   110 		        {
       
   111     		    TGlxFetchError error = errors->Error(index);
       
   112 		        retVal = error.iError;
       
   113 		        
       
   114 	            if ( IsExpired( error ) )
       
   115 	                {
       
   116 	                retVal = KErrNone;
       
   117 	                }
       
   118 	                
       
   119 	            if ( KErrNone != retVal )
       
   120 	                {
       
   121 	                if ( IsTemporaryError(retVal) )
       
   122 	                    {
       
   123     		            // Inform cache manager that at least one temporary error still exists
       
   124     		            CGlxCacheManager* cacheManager = CGlxCacheManager::InstanceL();
       
   125     		            cacheManager->SetTemporaryErrorFlag();
       
   126     		            cacheManager->Close();
       
   127 	                    }
       
   128 	                }
       
   129 		        }
       
   130 		    }
       
   131         }
       
   132         
       
   133     return retVal;
       
   134     }
       
   135     
       
   136 // -----------------------------------------------------------------------------
       
   137 // HasAttributeErrorL
       
   138 // -----------------------------------------------------------------------------
       
   139 //
       
   140 EXPORT_C TInt GlxErrorManager::HasAttributeErrorL(const CGlxMedia* aMedia, 
       
   141         TInt aContentId)
       
   142     {
       
   143     TRACER("GlxErrorManager::HasAttributeErrorL");
       
   144     
       
   145     TInt retVal = KErrNone;
       
   146     if ( aMedia )
       
   147         {
       
   148         const CGlxFetchErrorArray* errors = Errors( *aMedia );
       
   149 		
       
   150 		if ( errors )
       
   151 		    {
       
   152 		    TInt index = errors->FindError(aContentId);
       
   153 		    if ( KErrNotFound != index )
       
   154 		        {
       
   155     		    TGlxFetchError error = errors->Error(index);
       
   156 		        retVal = error.iError;
       
   157 		        
       
   158 	            if ( IsExpired( error ) )
       
   159 	                {
       
   160 	                retVal = KGlxErrorTimeOut;
       
   161 	                }
       
   162 	                
       
   163 	            if ( KErrNone != retVal && KGlxErrorTimeOut != retVal)
       
   164 	                {
       
   165 	                if ( IsTemporaryError(retVal) )
       
   166 	                    {
       
   167     		            // Inform cache manager that at least one temporary error still exists
       
   168     		            CGlxCacheManager* cacheManager = CGlxCacheManager::InstanceL();
       
   169     		            cacheManager->SetTemporaryErrorFlag();
       
   170     		            cacheManager->Close();
       
   171 	                    }
       
   172 	                }
       
   173 		        }
       
   174 		        
       
   175 		    }
       
   176         }
       
   177         
       
   178     return retVal;
       
   179     }
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // IsTemporaryError
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 TBool GlxErrorManager::IsTemporaryError(TInt aErrorCode)
       
   186     {
       
   187     TRACER("GlxErrorManager::IsTemporaryError");
       
   188  	GLX_DEBUG2("GlxErrorManager::IsTemporaryError() aErrorCode=%d", aErrorCode);
       
   189 	               	 	    
       
   190     /// @todo Implement this when list of temporary and permanent errors are known. Currently assume
       
   191     ///       all errors are temporary
       
   192     switch ( aErrorCode )
       
   193         {
       
   194         case KErrArgument:      // fallthrough
       
   195         case KErrNotSupported:  // fallthrough
       
   196         case KErrNoMemory:      // fallthrough
       
   197         case KErrCorrupt:       // fallthrough
       
   198         case KErrDiskFull:      
       
   199         // add other permanent error codes here
       
   200             return EFalse;
       
   201             
       
   202         default:
       
   203             return ETrue;
       
   204         };
       
   205     }
       
   206     
       
   207 // -----------------------------------------------------------------------------
       
   208 // IsErrorStillVal
       
   209 // -----------------------------------------------------------------------------
       
   210 //
       
   211 TBool GlxErrorManager::IsExpired( const TGlxFetchError& aError )
       
   212     {
       
   213     TRACER("GlxErrorManager::IsExpired");
       
   214     
       
   215     if ( IsTemporaryError(aError.iError) )
       
   216         {
       
   217         TTime now;
       
   218         now.UniversalTime();
       
   219 
       
   220         TTime errorTime(aError.iTimestamp);
       
   221 
       
   222         if ( now - TTimeIntervalSeconds(KGlxTemporaryErrorValidityPeriodInSeconds) > errorTime )
       
   223             {
       
   224             return ETrue;
       
   225             }
       
   226         }
       
   227         
       
   228     return EFalse;
       
   229     }
       
   230     
       
   231 // -----------------------------------------------------------------------------
       
   232 // HasError
       
   233 // -----------------------------------------------------------------------------
       
   234 //
       
   235 TBool GlxErrorManager::HasError(const CGlxMedia* aMedia)
       
   236     {
       
   237     TRACER("GlxErrorManager::HasError");
       
   238     
       
   239     if ( aMedia )
       
   240         {
       
   241         return ( Errors( *aMedia ) != NULL );
       
   242         }
       
   243     
       
   244     return EFalse;
       
   245     }
       
   246     
       
   247 // -----------------------------------------------------------------------------
       
   248 // ClearExpiredAndUnusedErrorsL
       
   249 // -----------------------------------------------------------------------------
       
   250 //
       
   251 void GlxErrorManager::ClearExpiredAndUnusedErrorsL( CGlxMedia& aMedia, 
       
   252         const RArray<TMPXAttribute>& aAttributesInUse )
       
   253     {
       
   254     TRACER("GlxErrorManager::ClearExpiredAndUnusedErrorsL");
       
   255     
       
   256     CGlxFetchErrorArray* errors = Errors( aMedia );
       
   257     
       
   258     if ( errors )
       
   259         {
       
   260         // Iterate through the list of errors backwards, so that removal is more
       
   261         // efficient, and loop control easier
       
   262         for ( TInt i = errors->ErrorCount() - 1; i >= 0; i-- )
       
   263             {
       
   264             TGlxFetchError error = errors->Error( i );
       
   265             // remove the error if it has expired, or if the attribute 
       
   266             // that the error is for is no longer in use
       
   267             if ( IsExpired( error ) || 
       
   268                  KErrNotFound == aAttributesInUse.Find( error.iAttr, TMPXAttribute::Match ) )
       
   269                 {
       
   270                 errors->Remove( i );
       
   271                 }
       
   272             }
       
   273             
       
   274         // no further errors are remaining, so can remove the error array 
       
   275         // attribute from the media object
       
   276         if ( errors->ErrorCount() == 0 )
       
   277             {
       
   278             aMedia.DeleteAttribute( KGlxMediaErrorArray );
       
   279             }
       
   280         }
       
   281     }
       
   282 
       
   283 // -----------------------------------------------------------------------------
       
   284 // Return error attribute's id
       
   285 // -----------------------------------------------------------------------------
       
   286 //
       
   287 TMPXAttribute GlxErrorManager::ErrorAttribute()
       
   288     {
       
   289     TRACER("GlxErrorManager::ErrorAttribute");
       
   290     
       
   291     return KGlxMediaErrorArray;
       
   292     }