videofeeds/utils/src/CIptvIapList.cpp
changeset 0 96612d01cf9f
equal deleted inserted replaced
-1:000000000000 0:96612d01cf9f
       
     1 /*
       
     2 * Copyright (c) 2005-2006 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:    Class for handling group of IAPs.*
       
    15 */
       
    16 
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CIptvIapList.h"
       
    22 #include "CIptvUtil.h"
       
    23 #include "IptvDebug.h"
       
    24 
       
    25 // ============================ MEMBER FUNCTIONS ===============================
       
    26 
       
    27 //defined to move default constructor to private
       
    28 CIptvIapList::CIptvIapList()
       
    29     {
       
    30     }
       
    31 
       
    32 // -----------------------------------------------------------------------------
       
    33 // CIptvIapList::NewL
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 EXPORT_C CIptvIapList* CIptvIapList::NewL()
       
    37     {
       
    38     CIptvIapList* self = new (ELeave) CIptvIapList;
       
    39     CleanupStack::PushL(self);
       
    40     self->ConstructL();
       
    41     CleanupStack::Pop(self);
       
    42     return self;
       
    43     }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CIptvIapList::ConstructL
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 void CIptvIapList::ConstructL()
       
    50     {
       
    51     iIapList.Reset();
       
    52     }
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // CIptvIapList::~CIptvIapList
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 EXPORT_C CIptvIapList::~CIptvIapList()
       
    59     {
       
    60     iIapList.Close();
       
    61     }
       
    62 
       
    63 // -----------------------------------------------------------------------------
       
    64 // CIptvIapList::AddIap
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 EXPORT_C TInt CIptvIapList::AddIap(TIptvIap& aIap)
       
    68     {
       
    69     if ( iIapList.Count() >= KIptvSmServicesDbMaxIaps )
       
    70         {
       
    71         return KErrOverflow;
       
    72         }
       
    73     return iIapList.Append( aIap );
       
    74     }
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CIptvIapList::AddIap
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 EXPORT_C TInt CIptvIapList::AddIap( TIptvIap& aIap, TBool aIgnoreMax )
       
    81     {
       
    82     if ( iIapList.Count() >= KIptvSmServicesDbMaxIaps && !aIgnoreMax )
       
    83         {
       
    84         return KErrOverflow;
       
    85         }
       
    86     return iIapList.Append( aIap );
       
    87     }
       
    88 
       
    89 // -----------------------------------------------------------------------------
       
    90 // CIptvIapList::DeleteIap
       
    91 // -----------------------------------------------------------------------------
       
    92 //    
       
    93 EXPORT_C TInt CIptvIapList::DeleteIap( TUint8 aIndex )
       
    94     {
       
    95     if ( aIndex >= iIapList.Count() )
       
    96         {
       
    97         return KErrOverflow;
       
    98         }
       
    99         
       
   100     iIapList.Remove( aIndex );
       
   101     iIapList.Compress();
       
   102     
       
   103     return KErrNone;
       
   104     }
       
   105 
       
   106 // -----------------------------------------------------------------------------
       
   107 // CIptvIapList::DeleteIap
       
   108 // -----------------------------------------------------------------------------
       
   109 //    
       
   110 EXPORT_C TInt CIptvIapList::DeleteIap( TIptvIap& aIap )
       
   111     {
       
   112     TInt i;
       
   113     for ( i = 0; i < iIapList.Count(); i++ )
       
   114         {
       
   115         if ( aIap.iId == iIapList[i].iId )
       
   116             {
       
   117             iIapList.Remove( i );
       
   118             iIapList.Compress();
       
   119             return KErrNone;
       
   120             }
       
   121         }
       
   122              
       
   123     return KErrNotFound;
       
   124     }
       
   125 
       
   126 // -----------------------------------------------------------------------------
       
   127 // CIptvIapList::ModifyIap
       
   128 // -----------------------------------------------------------------------------
       
   129 //
       
   130 EXPORT_C TInt CIptvIapList::ModifyIap(TUint8 aIndex, TIptvIap& aIap)
       
   131     {
       
   132     if(aIndex > (iIapList.Count()-1))
       
   133         {
       
   134         return KErrOverflow;
       
   135         }
       
   136     iIapList.Remove(aIndex);
       
   137     iIapList.Insert(aIap, aIndex);
       
   138     return KErrNone;
       
   139     }
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // CIptvIapList::IapL
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 EXPORT_C TIptvIap& CIptvIapList::IapL( TUint8 aIndex )
       
   146     {
       
   147     if ( aIndex > (iIapList.Count()-1) )
       
   148         {
       
   149         User::Leave( KErrOverflow );
       
   150         }
       
   151     return iIapList[aIndex];
       
   152     }
       
   153 
       
   154 // -----------------------------------------------------------------------------
       
   155 // CIptvIapList::GetIap
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 EXPORT_C TInt CIptvIapList::GetIap( TUint8 aIndex, TIptvIap& aIap )
       
   159     {
       
   160     if ( aIndex > (iIapList.Count()-1) )
       
   161         {
       
   162         return KErrOverflow;
       
   163         }
       
   164 
       
   165     aIap.iId       = iIapList[aIndex].iId;
       
   166     aIap.iPriority = iIapList[aIndex].iPriority;
       
   167     return KErrNone;
       
   168     }
       
   169     
       
   170 // -----------------------------------------------------------------------------
       
   171 // CIptvIapList::Count
       
   172 // -----------------------------------------------------------------------------
       
   173 //    
       
   174 EXPORT_C TUint8 CIptvIapList::Count()
       
   175     {
       
   176     return static_cast<TUint8>(iIapList.Count());
       
   177     }
       
   178     
       
   179 // -----------------------------------------------------------------------------
       
   180 // CIptvIapList::ExternalizeL
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 EXPORT_C void CIptvIapList::ExternalizeL(RDesWriteStream& aStream) const
       
   184     {
       
   185     TUint8 count;
       
   186     count = static_cast<TUint8>(iIapList.Count());    
       
   187     aStream.WriteUint8L(count); 
       
   188 
       
   189     TUint8 i;
       
   190     for(i = 0; i < count; i++)
       
   191         {
       
   192         iIapList[i].ExternalizeL(aStream);
       
   193         }    
       
   194     }
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // CIptvIapList::InternalizeL
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 EXPORT_C void CIptvIapList::InternalizeL(RDesReadStream& aStream)
       
   201     {
       
   202     TUint8 count;    
       
   203     count = aStream.ReadUint8L();
       
   204 
       
   205     //delete previous list data
       
   206     iIapList.Reset();
       
   207     
       
   208     //add uninitialized iaps
       
   209     TUint8 i;
       
   210     TIptvIap iap;
       
   211     for(i = 0; i < count; i++)
       
   212         {
       
   213         iIapList.Append(iap);
       
   214         }
       
   215     
       
   216     //init iaps with stream data
       
   217     for(i = 0; i < count; i++)
       
   218         {
       
   219         iIapList[i].InternalizeL(aStream);
       
   220         }
       
   221     }
       
   222 
       
   223 // -----------------------------------------------------------------------------
       
   224 // CIptvIapList::CountExternalizeSize
       
   225 // -----------------------------------------------------------------------------
       
   226 //
       
   227 EXPORT_C TUint32 CIptvIapList::CountExternalizeSize()
       
   228     {
       
   229     TUint8 i;
       
   230     TUint32 externalizeSize = 1; //first item is count (1 byte)
       
   231     
       
   232     for(i = 0; i < iIapList.Count(); i++)
       
   233         {
       
   234         externalizeSize += iIapList[i].CountExternalizeSize();
       
   235         }
       
   236     return externalizeSize;
       
   237     }
       
   238     
       
   239 // -----------------------------------------------------------------------------
       
   240 // CIptvIapList::SetL
       
   241 // -----------------------------------------------------------------------------
       
   242 //
       
   243 EXPORT_C void CIptvIapList::SetL(CIptvIapList& aIapList)
       
   244     {
       
   245     HBufC8* iapDes = HBufC8::NewL(aIapList.CountExternalizeSize() +1);
       
   246     CleanupStack::PushL(iapDes);
       
   247     RDesWriteStream writeStream;
       
   248     TPtr8 iapPtr8(iapDes->Des());
       
   249     writeStream.Open(iapPtr8);
       
   250     aIapList.ExternalizeL(writeStream);
       
   251     writeStream.Close();
       
   252     RDesReadStream readStream;
       
   253     readStream.Open(iapDes->Des());
       
   254     InternalizeL(readStream);
       
   255     readStream.Close();
       
   256     CleanupStack::PopAndDestroy(iapDes);
       
   257     }
       
   258 
       
   259 // -----------------------------------------------------------------------------
       
   260 // CIptvIapList::SortByPriorityL
       
   261 // Iaplist can have max of 10 items. This sorting is done as simply as possible
       
   262 // to avoid errors. Don't copy paste this code to any larger lists.
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 EXPORT_C void CIptvIapList::SortByPriorityL()
       
   266     {
       
   267     IPTVLOGSTRING_LOW_LEVEL("CIptvIapList::SortByPriority()");
       
   268 
       
   269     CIptvIapList* iapListSorted;
       
   270     iapListSorted = CIptvIapList::NewL();
       
   271     CleanupStack::PushL( iapListSorted );
       
   272     
       
   273     TUint8 highestPriorityIndex;
       
   274     TUint8 i;
       
   275     TUint8 count;
       
   276     
       
   277     count = Count();
       
   278     
       
   279     for ( i = 0; i < count; i++ )
       
   280         {
       
   281         highestPriorityIndex = GetHighestPriorityIapIndexL();
       
   282         iapListSorted->AddIap( iIapList[highestPriorityIndex] );
       
   283         DeleteIap( highestPriorityIndex );
       
   284         }
       
   285     
       
   286     SetL(*iapListSorted);
       
   287     CleanupStack::PopAndDestroy(iapListSorted);
       
   288 
       
   289     IPTVLOGSTRING_LOW_LEVEL("CIptvIapList::SortByPriority() exit");
       
   290     }
       
   291     
       
   292 // -----------------------------------------------------------------------------
       
   293 // CIptvIapList::GetHighestPriorityIapIndexL
       
   294 // -----------------------------------------------------------------------------
       
   295 //
       
   296 TUint8 CIptvIapList::GetHighestPriorityIapIndexL()
       
   297     {
       
   298     IPTVLOGSTRING_LOW_LEVEL("CIptvIapList::GetHighestPriorityIapIndexL()");
       
   299 
       
   300     if ( Count() < 1 )
       
   301         {
       
   302         IPTVLOGSTRING_HIGH_LEVEL("CIptvIapList:: Error: No IAPs set");
       
   303         User::Leave( KErrNotFound );
       
   304         }
       
   305     
       
   306     //small number = high priority
       
   307     TUint8 highestPriorityFound = 255;
       
   308     TUint8 highestPriorityIndex = 0;
       
   309     TUint16 i;
       
   310 
       
   311     for ( i = 0; i < Count(); i++ )
       
   312         {
       
   313         
       
   314         if ( highestPriorityFound > iIapList[i].iPriority )
       
   315             {
       
   316             highestPriorityFound = iIapList[i].iPriority;  //store current best priority
       
   317             highestPriorityIndex = i; //store current best priority location
       
   318             }
       
   319         }
       
   320     
       
   321     
       
   322     IPTVLOGSTRING_LOW_LEVEL("CIptvIapList::GetHighestPriorityIapIndex() exit");
       
   323 
       
   324     //return the index which had the highest priority (= smallest value)
       
   325     return highestPriorityIndex;
       
   326     }
       
   327 
       
   328 // -----------------------------------------------------------------------------
       
   329 // CIptvIapList::FindIap
       
   330 // -----------------------------------------------------------------------------
       
   331 //
       
   332 EXPORT_C TInt CIptvIapList::FindIap( TUint32 aIapId, TUint8& aIndex )
       
   333     {
       
   334     TInt i;
       
   335     for ( i = 0; i < Count(); i++ )
       
   336         {
       
   337         if( iIapList[i].iId == aIapId )
       
   338             {
       
   339             aIndex = i;
       
   340             return KErrNone;
       
   341             } 
       
   342         }
       
   343     return KErrNotFound;
       
   344     }
       
   345