videofeeds/server/IptvServiceManager/src/CIptvServiceManager.cpp
changeset 0 96612d01cf9f
equal deleted inserted replaced
-1:000000000000 0:96612d01cf9f
       
     1 /*
       
     2 * Copyright (c) 2005-2007 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:    Offers interface to services database*
       
    15 */
       
    16 
       
    17 
       
    18 
       
    19 
       
    20 #include <s32file.h>    // CFileStore & CPermanentFileStore
       
    21 #include <bautils.h>    // file helpers
       
    22 #include <eikenv.h>
       
    23 #include <sysutil.h>    // SysUtil
       
    24 #include "IptvDebug.h"
       
    25 #include <f32file.h>
       
    26 #include <sqldb.h>
       
    27 #include "CIptvDriveMonitor.h"
       
    28 
       
    29 #if defined(__SERIES60_30__) || defined(__SERIES60_31__) || defined(__SERIES60_32__)
       
    30 #include "CIptvUiEngine.h"
       
    31 #include <aspaclienthelper.h>
       
    32 #include "iptvlocalisationliterals.h"
       
    33 #include <ipvideo/IptvActivationMessage.h>
       
    34 #endif // defined(__SERIES60_30__) || defined(__SERIES60_31__) || defined(__SERIES60_32__)
       
    35 
       
    36 #include "CIptvService.h"
       
    37 #include "CIptvServices.h"
       
    38 #include "CIptvServiceManagementClient.h"
       
    39 #include "CIptvProvisioningProcessor.h"
       
    40 #include "CIptvServer.h"
       
    41 #include "IptvServer.pan"
       
    42 #include "MIptvServiceManagerObserver.h"
       
    43 #include "TIptvSmPassword.h"
       
    44 #include "CIptvServiceManager.h"
       
    45 
       
    46 const TInt KIptvServiceManagerFreeSpace = 512000; // 500 KB
       
    47 
       
    48 //all valid versions are > 0
       
    49 const TUint32 KIptvSmDbVersion = 3; //1 = Rel1, 2 = Rel2, Rel3, Rel4, 3 = Rel6/NFP1/Rel4 Touch
       
    50 
       
    51 #if defined(__SERIES60_30__) || defined(__SERIES60_31__) || defined(__SERIES60_32__)
       
    52  const TUid KIptvMain = {0x102750E2};
       
    53  const TInt KIptvMaxLengthOfEmptyThumbPath( 1 );
       
    54 #endif // defined(__SERIES60_30__) || defined(__SERIES60_31__) || defined(__SERIES60_32__)
       
    55 
       
    56 // ============================ MEMBER FUNCTIONS ===============================
       
    57 
       
    58 // -----------------------------------------------------------------------------
       
    59 // CIptvServiceManager::CIptvServiceManager
       
    60 // C++ default constructor can NOT contain any code, that
       
    61 // might leave.
       
    62 // -----------------------------------------------------------------------------
       
    63 //
       
    64 CIptvServiceManager::CIptvServiceManager()
       
    65     {
       
    66    	iServicesDbOpen  = EFalse;
       
    67    	iPasswordsDbOpen = EFalse;
       
    68     }
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CIptvServiceManager::ConstructL
       
    72 // Second phase construction. Leaves, if RFs session cannot be created.
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 void CIptvServiceManager::ConstructL()
       
    76     {
       
    77     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::ConstructL() begin");	
       
    78 
       
    79     iObservers.Reset();
       
    80 	
       
    81     User::LeaveIfError( iFsSession.Connect());
       
    82 
       
    83     CIptvDriveMonitor* driveMonitor = CIptvDriveMonitor::NewL( iFsSession );
       
    84     CleanupStack::PushL( driveMonitor ); // 1->
       
    85     
       
    86     for ( TInt i = 0; i < driveMonitor->iAvailableDrives.Count(); i++ )
       
    87         {
       
    88         CreateWritableRootDirL( static_cast<TDriveNumber>( driveMonitor->iAvailableDrives[i].iDrive ) );
       
    89         }
       
    90 
       
    91     CleanupStack::PopAndDestroy( driveMonitor ); // <-1
       
    92             
       
    93     // Services database to private folder, create private folder if it does not exist yet
       
    94     iPrivatePath.Zero();
       
    95     iServicesFile  = KIptvSmCDiskPrefix;
       
    96     iPasswordsFile = KIptvSmCDiskPrefix;
       
    97 		
       
    98     // Get private path
       
    99     if ( iFsSession.PrivatePath( iPrivatePath ) )
       
   100         {
       
   101         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::ConstructL() can't get private path");	
       
   102         User::Panic(KIptvServiceManager, KErrPathNotFound);
       
   103         }
       
   104 	
       
   105     // Append privatepath after drive letter
       
   106     iServicesFile.Append( iPrivatePath );
       
   107     iPasswordsFile.Append( iPrivatePath );
       
   108 	
       
   109     if ( BaflUtils::CheckFolder(iFsSession, iServicesFile) != KErrNone )
       
   110         {
       
   111         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::ConstructL() Creating private path ");	
       
   112 		
       
   113         if ( iFsSession.CreatePrivatePath( EDriveC ) != KErrNone )
       
   114             {
       
   115             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::ConstructL() can't create private path ");	
       
   116             // This is fatal, owner process is always able to create its private folder
       
   117             User::Panic( KIptvServiceManager, KErrDiskFull );
       
   118             }
       
   119 			
       
   120         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::ConstructL() Private path created");
       
   121         }
       
   122 
       
   123     iServicesFile.Append( KIptvSmServicesDbFileName );
       
   124     iPasswordsFile.Append( KIptvSmPasswordsDbFileName );
       
   125         
       
   126     CheckSmDbVersionL();
       
   127     OpenL();
       
   128 
       
   129 #if defined(__SERIES60_30__) || defined(__SERIES60_31__) || defined(__SERIES60_32__)
       
   130  	TRAPD( err, iActiveSpaceHelper = CActiveSpaceHelper::NewL() );
       
   131  	if ( err == KErrNotFound )
       
   132  	    {
       
   133  	    iActiveSpaceHelper = NULL;      // Active Space not found
       
   134  	    }
       
   135 #endif // defined(__SERIES60_30__) || defined(__SERIES60_31__) || defined(__SERIES60_32__)
       
   136 
       
   137     //  Check if there is service update file and if found, do provisioning
       
   138     TFileName serviceUpdateXml;
       
   139     if (IsServiceUpdateFoundL(serviceUpdateXml))
       
   140         {
       
   141         ExecuteProvisioningL();
       
   142         }
       
   143 
       
   144     CompactDatabases();
       
   145     Close();
       
   146         
       
   147     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::ConstructL() end");
       
   148     }
       
   149 
       
   150 // -----------------------------------------------------------------------------
       
   151 // CIptvServiceManager::NewL
       
   152 // Two-phased constructor.
       
   153 // -----------------------------------------------------------------------------
       
   154 //
       
   155 CIptvServiceManager* CIptvServiceManager::NewL()
       
   156     {
       
   157     CIptvServiceManager* self = new( ELeave ) CIptvServiceManager;
       
   158     CleanupStack::PushL( self );
       
   159     self->ConstructL();
       
   160     CleanupStack::Pop( self );
       
   161     return self;
       
   162     }
       
   163 
       
   164     
       
   165 // Destructor
       
   166 CIptvServiceManager::~CIptvServiceManager()
       
   167     {
       
   168     Close();  // Just in case, if the user does not close this explicitely
       
   169     iFsSession.Close();
       
   170     iObservers.Close();
       
   171     delete iSearchUrl;
       
   172     
       
   173 #if defined(__SERIES60_30__) || defined(__SERIES60_31__) || defined(__SERIES60_32__)
       
   174     delete iActiveSpaceHelper;
       
   175     REComSession::FinalClose();
       
   176 #endif // defined(__SERIES60_30__) || defined(__SERIES60_31__) || defined(__SERIES60_32__)
       
   177 
       
   178     }
       
   179 
       
   180 // ---------------------------------------------------------------------------
       
   181 // CIptvServiceManager::OpenL()
       
   182 // ---------------------------------------------------------------------------
       
   183 //
       
   184 void CIptvServiceManager::OpenL()
       
   185     {
       
   186     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::OpenL()");	
       
   187 
       
   188     CreateFilesL(ETrue /* do provisioning */);
       
   189 	  
       
   190     if ( !iServicesDbOpen )
       
   191         {
       
   192         OpenServicesDbL();
       
   193         }
       
   194     if ( !iPasswordsDbOpen )
       
   195         {
       
   196         OpenPasswordsDbL();
       
   197         }
       
   198         
       
   199     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::OpenL() exit");	
       
   200     }
       
   201 
       
   202 // ---------------------------------------------------------------------------
       
   203 // CIptvServiceManager::CreateFilesL()
       
   204 // ---------------------------------------------------------------------------
       
   205 //
       
   206 void CIptvServiceManager::CreateFilesL( TBool aDoProvisioning )
       
   207     {
       
   208     TBool servicesDbFileExists  = BaflUtils::FileExists( iFsSession, iServicesFile );
       
   209     TBool passwordsDbFileExists = BaflUtils::FileExists( iFsSession, iPasswordsFile );
       
   210 
       
   211     // Before creating any new databases, check that there is enough disk space.
       
   212     if ( !servicesDbFileExists || !passwordsDbFileExists )
       
   213         {
       
   214         TBool checkResult = EFalse;
       
   215         TRAPD( checkError, checkResult =
       
   216             SysUtil::DiskSpaceBelowCriticalLevelL(
       
   217                 &iFsSession, KIptvServiceManagerFreeSpace, EDriveC ) );
       
   218         if ( checkError != KErrNone || checkResult )
       
   219             {
       
   220             // Not enough space on disk, return immediately and do not create databases.
       
   221             User::Leave( KErrDiskFull );
       
   222             }
       
   223         }
       
   224     if ( servicesDbFileExists && passwordsDbFileExists )
       
   225         {            
       
   226         //do nothing
       
   227         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: Files exist, nothing done.");
       
   228         return;
       
   229         }
       
   230     if ( !servicesDbFileExists && !passwordsDbFileExists )
       
   231         {
       
   232         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: pwd.dat & services.dat missing");
       
   233         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: creating services.dat");
       
   234         TRAPD( error, CreateServicesDbL() );
       
   235         if( error != KErrNone )
       
   236             {
       
   237             IPTVLOGSTRING2_HIGH_LEVEL("CIptvServiceManager:: couldn't create services.dat (%d)", error);
       
   238             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: deleting services.dat");
       
   239             iFsSession.Delete( iPasswordsFile );
       
   240             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: Panicing!");
       
   241             User::Panic(KIptvServiceManager, EIptvSmCouldNotCreateServicesFile);
       
   242             }
       
   243         iServicesDbOpen = ETrue;
       
   244         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: creating pwd.dat");
       
   245         TRAP(error, CreatePasswordsDbL());
       
   246         if ( error != KErrNone )
       
   247             {
       
   248             IPTVLOGSTRING2_HIGH_LEVEL("CIptvServiceManager:: couldn't create pwd.dat (%d)", error);
       
   249             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: deleting pwd.dat");
       
   250             iFsSession.Delete(iPasswordsFile);
       
   251             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: Panicing!");
       
   252             User::Panic(KIptvServiceManager, EIptvSmCouldNotCreatePasswordsFile);
       
   253             }
       
   254         iPasswordsDbOpen = ETrue;
       
   255         if ( aDoProvisioning )
       
   256             {
       
   257             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: executing provisioning");
       
   258             ExecuteProvisioningL();
       
   259             }
       
   260         return;
       
   261         }
       
   262 
       
   263     if ( servicesDbFileExists && !passwordsDbFileExists )
       
   264         {
       
   265         //create pwd.dat
       
   266         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: pwd.dat missing");
       
   267         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: creating pwd.dat");
       
   268         TRAPD(error, CreatePasswordsDbL());
       
   269         if(error != KErrNone)
       
   270             {
       
   271             IPTVLOGSTRING2_HIGH_LEVEL("CIptvServiceManager:: couldn't create pwd.dat (%d)", error);
       
   272             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: deleting pwd.dat");
       
   273             iFsSession.Delete(iPasswordsFile);
       
   274             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: Panicing!");
       
   275             User::Panic(KIptvServiceManager, EIptvSmCouldNotCreatePasswordsFile);
       
   276             }
       
   277         iPasswordsDbOpen = ETrue;
       
   278         return;
       
   279         }
       
   280 
       
   281     if ( !servicesDbFileExists && passwordsDbFileExists )
       
   282         {
       
   283         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: services.dat missing, pwd.dat exists.");
       
   284         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: deleting pwd.dat");
       
   285         iFsSession.Delete(iPasswordsFile);
       
   286         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: creating services.dat");
       
   287         TRAPD(error, CreateServicesDbL());
       
   288         if ( error != KErrNone )
       
   289             {
       
   290             IPTVLOGSTRING2_HIGH_LEVEL("CIptvServiceManager:: couldn't create services.dat (%d)", error);
       
   291             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: deleting services.dat");
       
   292             iFsSession.Delete(iServicesFile);
       
   293             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: Panicing!");
       
   294             User::Panic(KIptvServiceManager, EIptvSmCouldNotCreateServicesFile);
       
   295             }
       
   296         iServicesDbOpen = ETrue;
       
   297         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: creating pwd.dat");
       
   298         TRAP( error, CreatePasswordsDbL() );
       
   299         if ( error != KErrNone )
       
   300             {
       
   301             IPTVLOGSTRING2_HIGH_LEVEL("CIptvServiceManager:: couldn't create pwd.dat (%d)", error);
       
   302             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: deleting pwd.dat");
       
   303             iFsSession.Delete(iPasswordsFile);
       
   304             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: Panicing!");
       
   305             User::Panic(KIptvServiceManager, EIptvSmCouldNotCreatePasswordsFile);
       
   306             }
       
   307         
       
   308         iPasswordsDbOpen = ETrue;
       
   309         if ( aDoProvisioning )
       
   310             {
       
   311             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: executing provisioning");
       
   312             ExecuteProvisioningL();
       
   313             }
       
   314         
       
   315         return;
       
   316         }        
       
   317     }
       
   318 
       
   319 // ---------------------------------------------------------------------------
       
   320 // CIptvServiceManager::OpenServicesDbL()
       
   321 // Open Services database, it's already checked that file exists
       
   322 // ---------------------------------------------------------------------------
       
   323 void CIptvServiceManager::OpenServicesDbL()
       
   324     {
       
   325     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::OpenServicesDbL()");
       
   326 
       
   327     if ( iServicesDbOpen )
       
   328         {
       
   329         IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager:: was already open.");
       
   330         return;
       
   331         }
       
   332         
       
   333     //open Services database
       
   334     TRAPD(error,
       
   335         {
       
   336         iServicesFileStore = CPermanentFileStore::OpenL(
       
   337             iFsSession, iServicesFile, EFileRead|EFileWrite );
       
   338         iServicesFileStore->SetTypeL( iServicesFileStore->Layout() );
       
   339         iServicesDb.OpenL( iServicesFileStore, iServicesFileStore->Root() );
       
   340         });
       
   341     
       
   342     if ( error != KErrNone )
       
   343         {
       
   344         IPTVLOGSTRING2_LOW_LEVEL("CIptvServiceManager:: Could not open services database(%d)", error);
       
   345 
       
   346         if ( error != KErrNoMemory && 
       
   347              error != KErrLocked && 
       
   348              error != KErrDisMounted &&
       
   349              error != KErrDiskFull &&
       
   350              error != KErrNotReady )
       
   351             {
       
   352             // Delete and recreate database files. Cannot recover other way. 
       
   353             RemoveDbL();
       
   354             TRAPD( err, CreateFilesL( ETrue /* do provisioning */ ));
       
   355             if ( err != KErrNone )
       
   356                 {
       
   357                 IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager:: couldnt recreate services.dat & pwd.dat -> panicing");
       
   358                 User::Panic(KIptvServiceManager, EIptvSmCouldNotOpenServicesDb);
       
   359                 }
       
   360             }
       
   361         else
       
   362             {
       
   363             IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager:: leaving");
       
   364             User::Leave(error);
       
   365             }
       
   366         }
       
   367 
       
   368     iServicesDbOpen = ETrue;
       
   369 
       
   370     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::OpenServicesDbL() exit");
       
   371     return;
       
   372     }
       
   373 
       
   374 // ---------------------------------------------------------------------------
       
   375 // CIptvServiceManager::OpenPasswordsDbL()
       
   376 // Open Passwords database, its already checked that file exists
       
   377 // ---------------------------------------------------------------------------
       
   378 void CIptvServiceManager::OpenPasswordsDbL()
       
   379     {
       
   380     IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager::OpenPasswordsDbL()");
       
   381     
       
   382     if(iPasswordsDbOpen)
       
   383         {
       
   384         IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager:: was already open.");
       
   385         return;
       
   386         }
       
   387         
       
   388     //open Passwords database
       
   389     TRAPD(error,
       
   390         { 
       
   391         iPasswordsFileStore = CPermanentFileStore::OpenL(
       
   392             iFsSession, iPasswordsFile, EFileRead|EFileWrite );
       
   393         iPasswordsFileStore->SetTypeL( iPasswordsFileStore->Layout() );
       
   394         iPasswordsDb.OpenL( iPasswordsFileStore, iPasswordsFileStore->Root() );
       
   395         });
       
   396 
       
   397     if(error != KErrNone)
       
   398         {
       
   399         if ( error != KErrNoMemory &&
       
   400              error != KErrLocked &&
       
   401              error != KErrDisMounted &&
       
   402              error != KErrDiskFull &&
       
   403              error != KErrNotReady )
       
   404             {
       
   405             // Delete and recreate database files. Cannot recover other way. 
       
   406             IPTVLOGSTRING2_LOW_LEVEL("CIptvServiceManager:: Could not open password database(%d)", error);
       
   407             RemoveDbL();
       
   408             TRAPD( err, CreateFilesL( ETrue /* do provisioning */ ));
       
   409             if ( err != KErrNone )
       
   410                 {
       
   411                 IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager:: couldnt recreate services.dat & pwd.dat -> panicing");
       
   412                 User::Panic(KIptvServiceManager, EIptvSmCouldNotOpenPasswordsDb);
       
   413                 }
       
   414             }
       
   415         else
       
   416             {
       
   417             // Database file might be ok, couldn't just temporarily open it
       
   418             IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager:: not removing database files, leaving");
       
   419             User::Leave( error );
       
   420             }
       
   421         }
       
   422         
       
   423     iPasswordsDbOpen = ETrue;
       
   424         
       
   425     IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager::OpenPasswordsDbL() exit");
       
   426     return;
       
   427     }
       
   428 
       
   429 // ---------------------------------------------------------------------------
       
   430 // CIptvServiceManager::CreateServicesDbL()
       
   431 // Create new Services database.
       
   432 // ---------------------------------------------------------------------------
       
   433 void CIptvServiceManager::CreateServicesDbL()
       
   434     {
       
   435     IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager::CreateServicesDbL()");
       
   436         
       
   437     IPTVLOGSTRING2_LOW_LEVEL("CIptvServiceManager:: Services db filename: %S", &iServicesFile);
       
   438 
       
   439     SaveSmDbVersion();
       
   440                 
       
   441     // Create empty services database file.
       
   442     iServicesFileStore = CPermanentFileStore::ReplaceL(iFsSession, iServicesFile, EFileRead|EFileWrite);
       
   443     iServicesFileStore->SetTypeL(iServicesFileStore->Layout()); // Set file store type
       
   444     TStreamId id = iServicesDb.CreateL(iServicesFileStore);     // Create stream object
       
   445     iServicesFileStore->SetRootL(id);                           // Keep database id as root of store
       
   446     iServicesFileStore->CommitL();                              // Complete creation by commiting
       
   447 
       
   448     CreateServicesTableL();
       
   449     CreateServicesDbIndexL();
       
   450      
       
   451     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::CreateServicesDbL() Exit");
       
   452     }
       
   453 
       
   454 // ---------------------------------------------------------------------------
       
   455 // CIptvServiceManager::CreatePasswordsDbL()
       
   456 // Create new Passwords database.
       
   457 // ---------------------------------------------------------------------------
       
   458 void CIptvServiceManager::CreatePasswordsDbL()
       
   459     {
       
   460     IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager::CreatePasswordsDbL()");
       
   461         
       
   462     IPTVLOGSTRING2_LOW_LEVEL("CIptvServiceManager:: Passwords db filename: %S", &iPasswordsFile);
       
   463     
       
   464     // Create empty passwords database file.
       
   465     iPasswordsFileStore = CPermanentFileStore::ReplaceL(iFsSession, iPasswordsFile, EFileRead|EFileWrite);
       
   466     iPasswordsFileStore->SetTypeL(iPasswordsFileStore->Layout()); // Set file store type
       
   467     TStreamId id = iPasswordsDb.CreateL(iPasswordsFileStore);    // Create stream object
       
   468     iPasswordsFileStore->SetRootL(id);                           // Keep database id as root of store
       
   469     iPasswordsFileStore->CommitL();                              // Complete creation by commiting
       
   470 
       
   471     CreatePasswordsTableL();
       
   472     CreatePasswordsDbIndexL();
       
   473     
       
   474     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::CreatePasswordsDbL() Exit");
       
   475     }
       
   476 
       
   477 // ---------------------------------------------------------------------------
       
   478 // CIptvServiceManager::RemoveDbL()
       
   479 // ---------------------------------------------------------------------------
       
   480 void CIptvServiceManager::RemoveDbL()
       
   481     {
       
   482     Close();
       
   483 
       
   484     if ( BaflUtils::FileExists( iFsSession, iServicesFile ) )
       
   485         {
       
   486         IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager:: deleting services.dat");
       
   487         iFsSession.Delete( iServicesFile );
       
   488         }
       
   489 
       
   490     if ( BaflUtils::FileExists( iFsSession, iPasswordsFile ) )
       
   491         {
       
   492         IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager:: deleting pwd.dat");
       
   493         iFsSession.Delete( iPasswordsFile );
       
   494         }
       
   495     }
       
   496 
       
   497 // ---------------------------------------------------------------------------
       
   498 // CIptvServiceManager::Close()
       
   499 // Close databases.
       
   500 // ---------------------------------------------------------------------------
       
   501 void CIptvServiceManager::Close()
       
   502     {
       
   503     IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager::Close() start");
       
   504 
       
   505     if ( iServicesDbOpen )
       
   506         {        
       
   507         iServicesDb.Close();
       
   508         
       
   509         delete iServicesFileStore;
       
   510         iServicesFileStore = NULL;
       
   511 
       
   512         iServicesDbOpen = EFalse;
       
   513         }
       
   514 
       
   515     if ( iPasswordsDbOpen )
       
   516         {        
       
   517         iPasswordsDb.Close();
       
   518         
       
   519         delete iPasswordsFileStore;
       
   520         iPasswordsFileStore = NULL;
       
   521 
       
   522         iPasswordsDbOpen = EFalse;
       
   523         }
       
   524         
       
   525     IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager::Close() exit");
       
   526     }
       
   527 
       
   528 // ---------------------------------------------------------------------------
       
   529 // CIptvServiceManager::CreateServicesTableL()
       
   530 // Creates Services table. Leaves, if the table cannot be created.
       
   531 // ---------------------------------------------------------------------------
       
   532 void CIptvServiceManager::CreateServicesTableL()
       
   533     {
       
   534     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::CreateServicesTableL()");
       
   535     
       
   536     // Specify columns for Services table
       
   537     TDbCol idCol( KIptvSmServicesDbIdCol, EDbColUint32 );
       
   538     idCol.iAttributes |= TDbCol::EAutoIncrement; 
       
   539     TDbCol nameCol( KIptvSmServicesDbNameCol, EDbColText, KIptvSmServicesDbNameMaxLength );
       
   540     TDbCol addressCol( KIptvSmServicesDbAddressCol, EDbColText, KIptvSmServicesDbAddressMaxLength );
       
   541     TDbCol iconPathCol( KIptvSmServicesDbIconPathCol, EDbColText, KIptvSmServicesDbIconPathMaxLength );
       
   542     TDbCol epgPluginUidCol( KIptvSmServicesDbEpgPluginUidCol, EDbColInt32 );
       
   543     TDbCol vodPluginUidCol( KIptvSmServicesDbVodPluginUidCol, EDbColInt32 );
       
   544     TDbCol iptvPluginUidCol( KIptvSmServicesDbIptvPluginUidCol, EDbColInt32 );
       
   545     TDbCol typeCol( KIptvSmServicesDbTypeCol, EDbColUint8);
       
   546     TDbCol iapListCol( KIptvSmServicesDbIapListCol, EDbColText8, KIptvSmServicesDbIapListMaxLength );
       
   547     TDbCol descCol( KIptvSmServicesDbDescCol, EDbColLongText ); // Stream Data
       
   548     TDbCol dateCol( KIptvSmServicesDbDateCol, EDbColDateTime );    
       
   549     TDbCol flagsCol( KIptvSmServicesDbFlagsCol, EDbColUint32 );
       
   550     TDbCol applicationUidCol( KIptvSmServicesDbApplicationUidCol, EDbColInt32 );
       
   551     TDbCol displayOrderCol( KIptvSmServicesDbDisplayOrderCol, EDbColUint32 );
       
   552     TDbCol providerIdCol( KIptvSmServicesDbProviderIdCol, EDbColText, KIptvSmServicesDbProviderIdMaxLength );
       
   553     TDbCol accountManagementUrlCol( KIptvSmServicesDbAccountManagementUrlCol, EDbColText, KIptvSmServicesDbAccountManagementUrlMaxLength );
       
   554     TDbCol epgUpdateTimeCol( KIptvSmServicesDbEpgUpdateTimeCol, EDbColDateTime );
       
   555     epgUpdateTimeCol.iAttributes |= TDbCol::ENotNull;
       
   556     TDbCol iapNameCol( KIptvSmServicesDbIapNameCol, EDbColText, KIptvSmServicesDbIapNameMaxLength );
       
   557     TDbCol searchUrlCol( KIptvSmServicesDbSearchUrlCol, EDbColLongText, KIptvSmServicesDbSearchUrlMaxLength ); // Stream Data
       
   558     TDbCol dlNetwork( KIptvSmServicesDbScheduledDlNetworkCol, EDbColUint32 );
       
   559     TDbCol dlTime( KIptvSmServicesDbScheduledDlTimeCol, EDbColUint32 );
       
   560     TDbCol dlType( KIptvSmServicesDbScheduledDlTypeCol, EDbColUint32 );
       
   561     TDbCol lastDlTimeCol( KIptvSmServicesDbLastScheduledDlTime, EDbColDateTime );
       
   562     TDbCol groupIdCol( KIptvSmServicesDbGroupIdCol, EDbColUint32 );
       
   563     TDbCol useCateCol( KIptvSmServicesDbUseCategoryCol, EDbColUint8 );
       
   564     TDbCol uploadCol( KIptvSmServicesDbUploadProviderCol, EDbColText, KIptvSmServicesDbNameMaxLength );
       
   565     TDbCol iconUrlCol( KIptvSmServicesDbIconUrlCol, EDbColText, KIptvSmServicesDbAddressMaxLength );
       
   566     TDbCol recordUrlCol( KIptvSmServicesDbRecordUrlCol, EDbColLongText, KIptvSmServicesDbRecordUrlMaxLength); // Stream Data
       
   567 
       
   568     // Add columns to column set
       
   569     CDbColSet* serviceColSet = CDbColSet::NewLC();
       
   570     serviceColSet->AddL( idCol );                   // 1
       
   571     serviceColSet->AddL( nameCol );                 // 2
       
   572     serviceColSet->AddL( addressCol );              // 3
       
   573     serviceColSet->AddL( iconPathCol );             // 4
       
   574     serviceColSet->AddL( epgPluginUidCol );         // 5
       
   575     serviceColSet->AddL( vodPluginUidCol );         // 6
       
   576     serviceColSet->AddL( iptvPluginUidCol );        // 7
       
   577     serviceColSet->AddL( typeCol );                 // 8
       
   578     serviceColSet->AddL( iapListCol );              // 9
       
   579     serviceColSet->AddL( descCol );                 // 10
       
   580     serviceColSet->AddL( dateCol );                 // 11
       
   581     serviceColSet->AddL( flagsCol );                // 12
       
   582     serviceColSet->AddL( applicationUidCol );       // 13
       
   583     serviceColSet->AddL( displayOrderCol );         // 14
       
   584     serviceColSet->AddL( providerIdCol );           // 15
       
   585     serviceColSet->AddL( accountManagementUrlCol ); // 16
       
   586     serviceColSet->AddL( epgUpdateTimeCol );        // 17 //last item in vers 1
       
   587     serviceColSet->AddL( iapNameCol );              // 18
       
   588     serviceColSet->AddL( searchUrlCol );            // 19
       
   589     serviceColSet->AddL( dlNetwork );               // 20
       
   590     serviceColSet->AddL( dlTime );                  // 21
       
   591     serviceColSet->AddL( dlType );                  // 22
       
   592     serviceColSet->AddL( lastDlTimeCol );           // 23 //last item in vers 2
       
   593     serviceColSet->AddL( groupIdCol );              // 24 
       
   594     serviceColSet->AddL( useCateCol );              // 25 
       
   595     serviceColSet->AddL( uploadCol );               // 26
       
   596     serviceColSet->AddL( iconUrlCol );              // 27 //last item in vers 3
       
   597     serviceColSet->AddL( recordUrlCol );            // 28 //last item in vers 3
       
   598 
       
   599     // Create the Services table
       
   600     User::LeaveIfError(
       
   601         iServicesDb.CreateTable( KIptvSmServicesTable, *serviceColSet ) );
       
   602     CleanupStack::PopAndDestroy( serviceColSet );
       
   603     
       
   604     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::CreateServicesTableL() exit");
       
   605     }
       
   606 
       
   607 // ---------------------------------------------------------------------------
       
   608 // CIptvServiceManager::CreatePasswordsTableL()
       
   609 // Creates Passwords table. Leaves, if the table cannot be created.
       
   610 // ---------------------------------------------------------------------------
       
   611 void CIptvServiceManager::CreatePasswordsTableL()
       
   612     {
       
   613     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::CreatePasswordsTableL()");
       
   614     
       
   615     // Specify columns for Passwords table
       
   616     TDbCol idCol( KIptvSmPasswordsDbIdCol, EDbColUint32 );
       
   617     TDbCol userNameCol( KIptvSmPasswordsDbUserNameCol,
       
   618         EDbColText, KIptvSmPasswordsDbUserNameMaxLength );
       
   619     TDbCol passwordCol( KIptvSmPasswordsDbPasswordCol,
       
   620         EDbColText, KIptvSmPasswordsDbPasswordMaxLength );
       
   621 
       
   622     // Add columns to column set
       
   623     CDbColSet* passwordColSet = CDbColSet::NewLC();
       
   624     passwordColSet->AddL( idCol );
       
   625     passwordColSet->AddL( userNameCol );
       
   626     passwordColSet->AddL( passwordCol );
       
   627 
       
   628     // Create the Passwords table
       
   629     User::LeaveIfError(
       
   630         iPasswordsDb.CreateTable( KIptvSmPasswordsTable, *passwordColSet ) );
       
   631     CleanupStack::PopAndDestroy( passwordColSet );
       
   632     
       
   633     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::CreatePasswordsTableL() exit");
       
   634     }
       
   635 
       
   636 // ---------------------------------------------------------------------------
       
   637 // CIptvServiceManager::CreateServicesDbIndexL()
       
   638 // Creates an index for Services table. Leaves, if the index cannot be created.
       
   639 // ---------------------------------------------------------------------------
       
   640 void CIptvServiceManager::CreateServicesDbIndexL()
       
   641     {
       
   642     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::CreateServicesDbIndexL()");
       
   643 
       
   644     // Create index consisting of ID
       
   645     TDbKeyCol idCol( KIptvSmServicesDbIdCol );
       
   646 
       
   647     CDbKey* index = CDbKey::NewLC();   // create index key set
       
   648     index->AddL( idCol );
       
   649     index->MakeUnique();
       
   650     User::LeaveIfError( iServicesDb.CreateIndex(
       
   651         KIptvSmServicesDbIndexName, KIptvSmServicesTable, *index ) );
       
   652     CleanupStack::PopAndDestroy( index );
       
   653 
       
   654     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::CreateServicesDbIndexL() exit");
       
   655     }
       
   656 
       
   657 // ---------------------------------------------------------------------------
       
   658 // CIptvServiceManager::CreatePasswordsDbIndexL()
       
   659 // Creates an index for Passwords table in Passwords db. Leaves, if the index
       
   660 // cannot be created.
       
   661 // ---------------------------------------------------------------------------
       
   662 void CIptvServiceManager::CreatePasswordsDbIndexL()
       
   663     {
       
   664     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::CreatePasswordsDbIndexL()");
       
   665 
       
   666     // Create index consisting of ID
       
   667     TDbKeyCol idCol( KIptvSmPasswordsDbIdCol );
       
   668 
       
   669     CDbKey* index = CDbKey::NewLC();   // create index key set
       
   670     index->AddL( idCol );
       
   671     index->MakeUnique();
       
   672     User::LeaveIfError( iPasswordsDb.CreateIndex(
       
   673         KIptvSmPasswordsDbIndexName, KIptvSmPasswordsTable, *index ) );
       
   674     CleanupStack::PopAndDestroy( index );
       
   675 
       
   676     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::CreatePasswordsDbIndexL() exit");
       
   677     }
       
   678 
       
   679 // ---------------------------------------------------------------------------
       
   680 // CIptvServiceManager::AddOrUpdateServiceL
       
   681 // ---------------------------------------------------------------------------
       
   682 //
       
   683 void CIptvServiceManager::AddOrUpdateServiceL(
       
   684     CIptvService& aService,
       
   685     TUint32 aModeFlags )
       
   686     {
       
   687     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::AddOrUpdateServiceL()");
       
   688     
       
   689     if ( !( aModeFlags & EDontOpen ) )
       
   690         {
       
   691         OpenL();
       
   692         CleanupClosePushL( *this ); // 1->
       
   693         }
       
   694     
       
   695     CompactDatabases();
       
   696     
       
   697     RDbTable servicesTable;
       
   698     RDbTable passwordsTable;
       
   699 
       
   700     TBool sendServiceAddedEvent = EFalse;
       
   701     TBool sendServiceModifiedEvent = EFalse;
       
   702     TBool sendServiceScheduledEvent = EFalse;
       
   703     TBool sendServiceSelectionModifiedEvent = EFalse;
       
   704 
       
   705     // Create an updateable database table object
       
   706     TInt err( servicesTable.Open(
       
   707         iServicesDb, KIptvSmServicesTable, servicesTable.EUpdatable ) );
       
   708     if ( err != KErrNone )
       
   709         {
       
   710         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: Could not get services table, aborting!");
       
   711         User::Leave( KErrGeneral );
       
   712         }
       
   713     CleanupClosePushL( servicesTable ); // 2->
       
   714 
       
   715     err = passwordsTable.Open(
       
   716         iPasswordsDb, KIptvSmPasswordsTable, passwordsTable.EUpdatable );
       
   717     if ( err != KErrNone )
       
   718         {
       
   719         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: Could not get passwords table, aborting!");
       
   720         User::Leave( KErrGeneral );
       
   721         }
       
   722     CleanupClosePushL( passwordsTable ); // 3->
       
   723 
       
   724     if ( servicesTable.CountL() >= KIptvSmMaxServices )
       
   725         {
       
   726         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: DB max entries exceeded, aborting!");
       
   727         User::Leave( KErrOverflow );    
       
   728         }
       
   729         
       
   730     CDbColSet* servicesColSet = servicesTable.ColSetL();
       
   731     CleanupStack::PushL( servicesColSet );  // 4->
       
   732     CDbColSet* passwordsColSet = passwordsTable.ColSetL();
       
   733     CleanupStack::PushL( passwordsColSet ); // 5->
       
   734 
       
   735     servicesTable.SetIndex( KIptvSmServicesDbIndexName );
       
   736     servicesTable.Reset();
       
   737     passwordsTable.SetIndex( KIptvSmPasswordsDbIndexName );
       
   738     passwordsTable.Reset();
       
   739 
       
   740     TUint serviceId = 0;
       
   741     
       
   742     if ( aModeFlags & EAddService )
       
   743         {
       
   744         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: Adding a new service");
       
   745 		
       
   746         // add new entry to Services table, ID is autoincrement
       
   747         servicesTable.InsertL();
       
   748         
       
   749         // Get ID
       
   750         serviceId = servicesTable.ColUint32(
       
   751             servicesColSet->ColNo( KIptvSmServicesDbIdCol ) );
       
   752 
       
   753         // Inform observers later if not grouped service
       
   754         if ( !( aService.GetFlags() & CIptvService::EGroupedService ) )
       
   755             {
       
   756             sendServiceAddedEvent = ETrue;
       
   757             }
       
   758         
       
   759         // Add id+1 as default display order value for service
       
   760         servicesTable.SetColL( servicesColSet->ColNo(
       
   761             KIptvSmServicesDbDisplayOrderCol ), serviceId + 1 );
       
   762         
       
   763         // Add new entry to Passwords db, use ID from Services table
       
   764         passwordsTable.InsertL();
       
   765         passwordsTable.SetColL( passwordsColSet->ColNo(
       
   766             KIptvSmPasswordsDbIdCol ), serviceId );
       
   767         
       
   768         // Update new Service Id to the actual service.
       
   769         aService.SetId( serviceId );
       
   770         }
       
   771     else
       
   772         {
       
   773         IPTVLOGSTRING_HIGH_LEVEL("Updating existing service");
       
   774         //Mode = EUpdateService 
       
   775 		
       
   776         if ( aService.GetDisplayOrder() == 0 )
       
   777             {
       
   778             aService.SetDisplayOrder( MaxDisplayOrderL() );
       
   779             }
       
   780 
       
   781         //-> find the service item to update
       
   782         if ( !servicesTable.SeekL( ( TInt )( aService.GetId() ) ) )
       
   783             {
       
   784             //not found
       
   785             IPTVLOGSTRING2_HIGH_LEVEL("CIptvServiceManager:: Service(%d) not found, aborting", aService.GetId() );			
       
   786             User::Leave( KErrNotFound );
       
   787             }
       
   788 
       
   789         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: Service entry found, updating..");
       
   790         servicesTable.UpdateL();
       
   791 
       
   792         if ( passwordsTable.SeekL( ( TInt )( aService.GetId() ) ) )
       
   793             {
       
   794             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: Password entry found, updating..");
       
   795             passwordsTable.UpdateL();
       
   796             }
       
   797         else
       
   798             {
       
   799             IPTVLOGSTRING2_HIGH_LEVEL("CIptvServiceManager:: Passwords db does not contain ID: %d", aService.GetId() );
       
   800             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: Adding it.");
       
   801             passwordsTable.InsertL();
       
   802             passwordsTable.SetColL( passwordsColSet->ColNo(
       
   803                 KIptvSmPasswordsDbIdCol ), aService.GetId() );
       
   804             }
       
   805         }
       
   806         
       
   807     servicesTable.SetColL( servicesColSet->ColNo(
       
   808         KIptvSmServicesDbNameCol ), aService.GetName() );
       
   809 
       
   810     //check if address is modified and do we need to send event
       
   811     TPtrC address;
       
   812     address.Set( servicesTable.ColDes(
       
   813         servicesColSet->ColNo( KIptvSmServicesDbAddressCol ) ) );
       
   814 
       
   815     if ( address != aService.GetAddress() ) //has address changed
       
   816         {
       
   817         if ( !( aModeFlags & EAddService ) ) //is this a modify call
       
   818             {
       
   819             sendServiceModifiedEvent = ETrue;
       
   820             }
       
   821         }
       
   822 
       
   823     // check if the scheduled download settings have been changed and
       
   824     TUint dlNetwork = servicesTable.ColUint32(
       
   825         servicesColSet->ColNo( KIptvSmServicesDbScheduledDlNetworkCol ) );
       
   826     TUint dlTime = servicesTable.ColUint32(
       
   827         servicesColSet->ColNo( KIptvSmServicesDbScheduledDlTimeCol ) );
       
   828     TUint dlType = servicesTable.ColUint32(
       
   829         servicesColSet->ColNo( KIptvSmServicesDbScheduledDlTypeCol ) );
       
   830     TTime dlLastTime = servicesTable.ColTime(
       
   831         servicesColSet->ColNo( KIptvSmServicesDbLastScheduledDlTime ) );
       
   832 
       
   833     if ( dlNetwork != aService.ScheduleDlNetwork() ||
       
   834          dlTime != aService.ScheduleDlTime() ||
       
   835          dlType != aService.ScheduleDlType() ||
       
   836          dlLastTime != aService.GetScheduledLastDownloadTime() )
       
   837         {
       
   838         // changed, we need to send event
       
   839         sendServiceScheduledEvent = ETrue;
       
   840         }
       
   841 
       
   842     // check if Selected flag is modified and do we need to send event
       
   843     TUint newSelectedFlag = aService.GetFlags() & CIptvService::ESelected;
       
   844     TUint oldSelectedFlag = servicesTable.ColUint32(
       
   845         servicesColSet->ColNo( KIptvSmServicesDbFlagsCol ) ) & CIptvService::ESelected;
       
   846     if ( newSelectedFlag != oldSelectedFlag )
       
   847         {
       
   848         sendServiceSelectionModifiedEvent = ETrue;
       
   849         }
       
   850 
       
   851     servicesTable.SetColL( servicesColSet->ColNo(
       
   852         KIptvSmServicesDbAddressCol ), aService.GetAddress() );
       
   853     servicesTable.SetColL( servicesColSet->ColNo(
       
   854         KIptvSmServicesDbIconPathCol ), aService.GetIconPath() );
       
   855     servicesTable.SetColL( servicesColSet->ColNo(
       
   856         KIptvSmServicesDbEpgPluginUidCol ), aService.GetEpgPluginUid().iUid );
       
   857     servicesTable.SetColL( servicesColSet->ColNo(
       
   858         KIptvSmServicesDbVodPluginUidCol ), aService.GetVodPluginUid().iUid );
       
   859     servicesTable.SetColL( servicesColSet->ColNo(
       
   860         KIptvSmServicesDbIptvPluginUidCol ), aService.GetIptvPluginUid().iUid );
       
   861     servicesTable.SetColL( servicesColSet->ColNo(
       
   862         KIptvSmServicesDbTypeCol), aService.GetType() );
       
   863     
       
   864     CIptvIapList& iapList = aService.GetIapList();
       
   865     RDesWriteStream writeStream;
       
   866     HBufC8* iapListDes = HBufC8::NewL( iapList.CountExternalizeSize() +1 );
       
   867     CleanupStack::PushL( iapListDes ); // 5->
       
   868     TPtr8 iapListPtr8( iapListDes->Des() );
       
   869     writeStream.Open( iapListPtr8 );
       
   870     CleanupClosePushL( writeStream ); // 6->
       
   871     iapList.ExternalizeL( writeStream );
       
   872     CleanupStack::PopAndDestroy( &writeStream ); // <-6
       
   873     servicesTable.SetColL( servicesColSet->ColNo( KIptvSmServicesDbIapListCol ), iapListPtr8 );
       
   874     CleanupStack::PopAndDestroy( iapListDes ); // <-5
       
   875     
       
   876     passwordsTable.SetColL( passwordsColSet->ColNo(
       
   877         KIptvSmPasswordsDbUserNameCol ), aService.GetUserName() );
       
   878     passwordsTable.SetColL( passwordsColSet->ColNo(
       
   879         KIptvSmPasswordsDbPasswordCol ), aService.GetPassword() );
       
   880     servicesTable.SetColL( servicesColSet->ColNo(
       
   881         KIptvSmServicesDbDescCol ), aService.GetDesc() );
       
   882     
       
   883     if ( aModeFlags & EAddService )
       
   884         {
       
   885         TTime time;	
       
   886         time.HomeTime();
       
   887         servicesTable.SetColL( servicesColSet->ColNo( KIptvSmServicesDbDateCol ), time );
       
   888         }
       
   889         
       
   890     servicesTable.SetColL( servicesColSet->ColNo(
       
   891         KIptvSmServicesDbFlagsCol ), aService.GetFlags() );
       
   892     servicesTable.SetColL( servicesColSet->ColNo(
       
   893         KIptvSmServicesDbApplicationUidCol ), aService.GetApplicationUid() );
       
   894     
       
   895     servicesTable.SetColL( servicesColSet->ColNo(
       
   896         KIptvSmServicesDbProviderIdCol ), aService.GetProviderId() );
       
   897     servicesTable.SetColL( servicesColSet->ColNo(
       
   898         KIptvSmServicesDbAccountManagementUrlCol ), aService.GetAccountManagementUrl() );
       
   899     servicesTable.SetColL( servicesColSet->ColNo(
       
   900         KIptvSmServicesDbEpgUpdateTimeCol ), aService.GetEpgUpdateTimeL() );
       
   901     servicesTable.SetColL( servicesColSet->ColNo(
       
   902         KIptvSmServicesDbIapNameCol ), aService.GetIapNameL() );
       
   903 
       
   904     servicesTable.SetColL( servicesColSet->ColNo(
       
   905         KIptvSmServicesDbSearchUrlCol ), aService.SearchUrl() );
       
   906     servicesTable.SetColL( servicesColSet->ColNo(
       
   907         KIptvSmServicesDbScheduledDlNetworkCol ), aService.ScheduleDlNetwork() );
       
   908     servicesTable.SetColL( servicesColSet->ColNo(
       
   909         KIptvSmServicesDbScheduledDlTimeCol ), aService.ScheduleDlTime() );
       
   910     servicesTable.SetColL( servicesColSet->ColNo(
       
   911         KIptvSmServicesDbScheduledDlTypeCol ), aService.ScheduleDlType() );
       
   912     servicesTable.SetColL( servicesColSet->ColNo(
       
   913         KIptvSmServicesDbLastScheduledDlTime ), aService.GetScheduledLastDownloadTime() );
       
   914 
       
   915     servicesTable.SetColL( servicesColSet->ColNo(
       
   916         KIptvSmServicesDbGroupIdCol ), aService.GetGroupId() );
       
   917     servicesTable.SetColL( servicesColSet->ColNo(
       
   918         KIptvSmServicesDbUseCategoryCol), aService.GetUseCategory() );
       
   919     servicesTable.SetColL( servicesColSet->ColNo(
       
   920         KIptvSmServicesDbUploadProviderCol ), aService.GetUploadProvider() );
       
   921     servicesTable.SetColL( servicesColSet->ColNo(
       
   922         KIptvSmServicesDbIconUrlCol ), aService.GetIconUrl() );
       
   923     servicesTable.SetColL( servicesColSet->ColNo(
       
   924         KIptvSmServicesDbRecordUrlCol ), aService.RecordUrl() );
       
   925 
       
   926     //check if display order is modified
       
   927     TUint32 sourceDisplayOrder = servicesTable.ColUint32( servicesColSet->ColNo( KIptvSmServicesDbDisplayOrderCol ) );
       
   928     TUint32 targetDisplayOrder = aService.GetDisplayOrder();
       
   929 
       
   930     if ( !( aModeFlags & EAddService ) 
       
   931         && sourceDisplayOrder != targetDisplayOrder )
       
   932         {
       
   933         // Set defined display order value for service 
       
   934         servicesTable.SetColL( servicesColSet->
       
   935             ColNo( KIptvSmServicesDbDisplayOrderCol ), targetDisplayOrder );
       
   936         sendServiceModifiedEvent = ETrue;
       
   937         }
       
   938                    
       
   939     servicesTable.PutL();    // Complete changes (the insertion)			
       
   940     passwordsTable.PutL();   // Complete changes (the insertion)			
       
   941  
       
   942     CleanupStack::PopAndDestroy( passwordsColSet ); // <-5
       
   943     CleanupStack::PopAndDestroy( servicesColSet );	// <-4           
       
   944     
       
   945     if ( !( aModeFlags & EAddService ) 
       
   946         && sourceDisplayOrder != targetDisplayOrder )
       
   947         {
       
   948         for ( servicesTable.FirstL(); servicesTable.AtRow(); servicesTable.NextL() )
       
   949             { 
       
   950               //Get row
       
   951             servicesTable.GetL();
       
   952             servicesColSet = servicesTable.ColSetL();
       
   953             CleanupStack::PushL( servicesColSet );
       
   954             
       
   955             TUint32 currentServiceId =
       
   956                 servicesTable.ColUint32( servicesColSet->ColNo( KIptvSmServicesDbIdCol ) ); 
       
   957             
       
   958             TUint32 currentDisplayOrder = 
       
   959                 servicesTable.ColUint32( servicesColSet->ColNo( KIptvSmServicesDbDisplayOrderCol ) );
       
   960                 
       
   961             if ( currentServiceId != aService.GetId() )
       
   962                 {
       
   963                 servicesTable.UpdateL();
       
   964                 
       
   965                 if ( ( currentDisplayOrder <= targetDisplayOrder ) &&
       
   966                      ( currentDisplayOrder >= sourceDisplayOrder ) )
       
   967                     {
       
   968                     currentDisplayOrder--;
       
   969                     }
       
   970                 else if ( ( currentDisplayOrder >= targetDisplayOrder ) &&
       
   971                           ( currentDisplayOrder <= sourceDisplayOrder ) ) 
       
   972                     {
       
   973                     currentDisplayOrder++;
       
   974                     }
       
   975                     
       
   976                 servicesTable.SetColL( servicesColSet->ColNo(
       
   977                     KIptvSmServicesDbDisplayOrderCol ), currentDisplayOrder );
       
   978                 servicesTable.PutL(); 
       
   979                 }
       
   980                 
       
   981             CleanupStack::PopAndDestroy( servicesColSet );
       
   982             }
       
   983         }    
       
   984 
       
   985     CleanupStack::PopAndDestroy( &passwordsTable ); // <-3
       
   986     CleanupStack::PopAndDestroy( &servicesTable );  // <-2
       
   987 
       
   988     if ( !( aModeFlags & EDontOpen ) )
       
   989         {
       
   990         CleanupStack::PopAndDestroy( this );        // <-1
       
   991         }
       
   992 
       
   993     if ( sendServiceAddedEvent )
       
   994         {
       
   995         InformObserversL( CIptvSmEvent::EServiceAdded, serviceId, &aService );
       
   996         }
       
   997 
       
   998     if ( sendServiceModifiedEvent )
       
   999         {
       
  1000         InformObserversL( CIptvSmEvent::EServiceModified, aService.GetId(), &aService );
       
  1001         }
       
  1002 
       
  1003     if ( sendServiceScheduledEvent )
       
  1004         {
       
  1005         InformObserversL( CIptvSmEvent::EServiceScheduleModified, aService.GetId(), &aService );
       
  1006         }
       
  1007 
       
  1008     if ( sendServiceSelectionModifiedEvent )
       
  1009         {
       
  1010         InformObserversL( CIptvSmEvent::EServiceSelectionModified, aService.GetId(), &aService );
       
  1011         }
       
  1012 
       
  1013     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::AddOrUpdateServiceL() exit");
       
  1014     }
       
  1015 
       
  1016 #if defined(__SERIES60_30__) || defined(__SERIES60_31__) || defined(__SERIES60_32__)
       
  1017 // -----------------------------------------------------------------------------
       
  1018 // CIptvServiceManager::UpdateVideoStoreSpaceDbL()
       
  1019 // Updates Video Store Service to active space database.
       
  1020 // -----------------------------------------------------------------------------
       
  1021 //
       
  1022 void CIptvServiceManager::UpdateVideoStoreSpaceDbL( CIptvService& aService )
       
  1023     {
       
  1024     if ( !iActiveSpaceHelper )
       
  1025         {
       
  1026         User::Leave( KErrNotFound );
       
  1027         }
       
  1028 
       
  1029     IPTVLOGSTRING_LOW_LEVEL( "IptvUiEngine ## CIptvServiceManager::UpdateSpaceDb() - Enter" );
       
  1030 
       
  1031     CActiveSpaceBase* activeSpace = iActiveSpaceHelper->NewActiveSpaceL();
       
  1032     CleanupStack::PushL( activeSpace );
       
  1033     IPTVLOGSTRING_LOW_LEVEL( "IptvUiEngine ## CIptvServiceManager::UpdateSpaceDb() - CActiveSpace Created" );
       
  1034 
       
  1035     // Create item to 6th category as Video Store.
       
  1036     TActiveSpaceDataProperties serviceProperties;
       
  1037     serviceProperties.iGenre = EActiveSpaceGenreWatch;
       
  1038     serviceProperties.iSource = EActiveSpaceSourceLocal;
       
  1039     serviceProperties.iOwner = EActiveSpaceOwnerCommercial;
       
  1040     serviceProperties.iFlags = EActiveSpaceFlagNone;
       
  1041     serviceProperties.iCategory = EActiveSpaceCategory6th;
       
  1042 
       
  1043     // Create parent
       
  1044     CActiveSpaceDataBase* parentData = iActiveSpaceHelper->NewActiveSpaceDataL();
       
  1045     CleanupStack::PushL( parentData );
       
  1046     IPTVLOGSTRING_LOW_LEVEL( "IptvUiEngine ## CIptvServiceManager::UpdateVideoStoreSpaceDbL() - CActiveSpaceData Parent Created" );
       
  1047     parentData->SetParent( CActiveSpaceDataBase::EActiveSpaceRoot );		
       
  1048     parentData->SetName( EVideoTileNokiaVideoShop );
       
  1049     parentData->SetProperties( serviceProperties );
       
  1050 
       
  1051     if ( ( aService.GetIconPath().Length() > KIptvMaxLengthOfEmptyThumbPath ) &&
       
  1052          BaflUtils::FileExists( iFsSession, aService.GetIconPath() ) )
       
  1053         {
       
  1054         parentData->SetIconL( aService.GetIconPath() );
       
  1055         IPTVLOGSTRING_LOW_LEVEL( "IptvUiEngine ## CIptvServiceManager::UpdateVideoStoreSpaceDbL() - Icon added" );
       
  1056         }
       
  1057     else
       
  1058         {
       
  1059         parentData->SetIcon( EAspaLogicalIconVideoTileDefault );
       
  1060         IPTVLOGSTRING_LOW_LEVEL( "IptvUiEngine ## CIptvServiceManager::UpdateVideoStoreSpaceDbL() - Default icon added" );
       
  1061         }
       
  1062 
       
  1063     TInt parentId = activeSpace->AddDataL( parentData, CActiveSpaceDataBase::EActiveSpaceRoot );   	  
       
  1064     IPTVLOGSTRING_LOW_LEVEL("IptvUiEngine ## CIptvServiceManager::UpdateVideoStoreSpaceDbL() - Parent data added");  
       
  1065 
       
  1066     if ( parentId > 0 )          // No error
       
  1067         {
       
  1068         // Create child
       
  1069         CActiveSpaceDataBase* childData = iActiveSpaceHelper->NewActiveSpaceDataL();
       
  1070         CleanupStack::PushL( childData );  
       
  1071         childData->SetParent( parentId );
       
  1072         childData->SetName( EVideoTileNokiaVideoShopAd );
       
  1073         serviceProperties.iCategory = EActiveSpaceCategory1st;
       
  1074         childData->SetProperties( serviceProperties );
       
  1075 	    TInt childId = activeSpace->AddDataL( childData, parentId );
       
  1076 	    IPTVLOGSTRING_LOW_LEVEL("IptvUiEngine ## CIptvServiceManager::UpdateVideoStoreSpaceDbL() - Child data added");
       
  1077 	
       
  1078         CASpaAppLauncherActionDataBase* serviceData = iActiveSpaceHelper->NewAppLauncherActionDataL();
       
  1079 	    CleanupStack::PushL( serviceData );
       
  1080 
       
  1081 	    TUid videoCenterAppUid = KIptvMain;
       
  1082 	    TUid vodPlayerUid = TUid::Uid( KIptvVodStartPlayer );
       
  1083 	
       
  1084         TIptvActivationMessage params;
       
  1085 		params.iMsgType = TIptvActivationMessage::EOpenVideoShop;
       
  1086 		params.iMsgSender = TIptvActivationMessage::EActiveSpace;
       
  1087 		params.iFileId = 0;
       
  1088 		params.iDrive = 0;
       
  1089 		TPckg<TIptvActivationMessage> paramsPckg( params );
       
  1090 	
       
  1091 	    serviceData->SetApplicationUid( videoCenterAppUid );
       
  1092 	    serviceData->SetApaLaunchingMethod(
       
  1093 	        CASpaAppLauncherActionDataBase::
       
  1094             EApaLaunchMethodStartApaMessageWithTail );               
       
  1095 	    serviceData->SetAdditionalDataL( paramsPckg );                 
       
  1096 	    serviceData->SetMessageUid( vodPlayerUid );	
       
  1097 	    
       
  1098 	    const TInt KCASpaAppLauncherPlugin = 0x10282E5F;
       
  1099 	    TUid AppLauncherPlugin( TUid::Uid( KCASpaAppLauncherPlugin ) );
       
  1100 
       
  1101 	    CActiveSpaceActionBase* serviceAction = iActiveSpaceHelper->NewActiveSpaceActionL();
       
  1102 	    CleanupStack::PushL( serviceAction );
       
  1103 
       
  1104 	    serviceAction->SetType( EActiveSpaceActionTypeLaunchApplication );
       
  1105 	    serviceAction->SetActionHandlerUid( TUid::Uid( KCASpaAppLauncherPlugin ) );	
       
  1106 	    serviceAction->SetKeys( EActiveSpaceActionKeySelect );
       
  1107 	    serviceAction->SetDataL( serviceData->GetBufferL() );
       
  1108 		
       
  1109 	    IPTVLOGSTRING_LOW_LEVEL("IptvUiEngine ## CIptvServiceManager::UpdateVideoStoreSpaceDbL() - CActiveSpaceAction Object Created"); 									
       
  1110 										
       
  1111 	    TInt actionId = activeSpace->AddActionL( serviceAction );
       
  1112 	    activeSpace->BindActionToDataL( parentId, actionId );
       
  1113 	    IPTVLOGSTRING_LOW_LEVEL("IptvUiEngine ## CIptvServiceManager::UpdateVideoStoreSpaceDbL() - Action binded");
       
  1114 	
       
  1115 	    CleanupStack::PopAndDestroy( serviceAction );
       
  1116 	    CleanupStack::PopAndDestroy( serviceData );                        
       
  1117 	    CleanupStack::PopAndDestroy( childData );
       
  1118 	    }
       
  1119 	CleanupStack::PopAndDestroy( parentData );	
       
  1120 	CleanupStack::PopAndDestroy( activeSpace );
       
  1121 
       
  1122 	IPTVLOGSTRING_LOW_LEVEL("IptvUiEngine ## CIptvServiceManager::UpdateVideoStoreSpaceDbL() - Exit");
       
  1123 	}
       
  1124 #endif // defined(__SERIES60_30__) || defined(__SERIES60_31__) || defined(__SERIES60_32__)
       
  1125 
       
  1126 // ---------------------------------------------------------------------------
       
  1127 // CIptvServiceManager::GetServicesL()
       
  1128 // Get services from databases.
       
  1129 // ---------------------------------------------------------------------------
       
  1130 //
       
  1131 CIptvServices* CIptvServiceManager::GetServicesL(
       
  1132     TUint32 aSearchLimitFlags,
       
  1133     CIptvServiceManagementClient::TOrder aOrder,
       
  1134     TInt aVersion )
       
  1135     {
       
  1136     IPTVLOGSTRING_LOW_LEVEL( "CIptvServiceManager::GetServicesL(aSearchLimitFlags, aOrder)" );
       
  1137 
       
  1138     OpenL();
       
  1139     CleanupClosePushL( *this ); // 1->
       
  1140 
       
  1141     CIptvServices* services = CIptvServices::NewL();
       
  1142     CleanupStack::PushL( services ); // 2->
       
  1143     
       
  1144     IPTVLOGSTRING2_LOW_LEVEL("CIptvServiceManager:: Search limit flags: %d", aSearchLimitFlags );	
       
  1145 
       
  1146     TInt32 epgPluginUid, vodPluginUid, iptvPluginUid, applicationUid;
       
  1147     TUint32 id, flags, displayOrder;
       
  1148     TPtrC name, address, iconPath, providerId, accountManagementUrl, iapName;
       
  1149     TPtrC8 iapList;
       
  1150     TUint32 dlNetwork( 0 );
       
  1151     TUint32 dlTime( 0 );
       
  1152     TUint32 dlType( 0 );
       
  1153     TUint32 groupId( 0 );
       
  1154     TTime lastScheduledDlTime;
       
  1155     TUint8 type;
       
  1156     TUint8 useCategory( 0 );
       
  1157     TPtrC uploadProvider, iconUrl;
       
  1158     TBuf<KIptvSmServicesDbDescMaxLength> desc;
       
  1159     TTime epgUpdateTime;
       
  1160     
       
  1161     CIptvService* iptvService;
       
  1162     iptvService = CIptvService::NewL();
       
  1163     CleanupStack::PushL( iptvService ); // 3->
       
  1164 
       
  1165     RDbView view;    // Create a view on the database
       
  1166 
       
  1167 /*
       
  1168 SELECT "column_name"
       
  1169 FROM "table_name"
       
  1170 [WHERE "condition"]
       
  1171 ORDER BY "column_name" [ASC, DESC]
       
  1172 */
       
  1173     const TInt32 KIptvSmServiceManagerSelectCmdLength = 32 + 20;
       
  1174     _LIT( KIptvSmSelectCmdFirstPart, "SELECT * FROM Services ORDER BY " );
       
  1175     _LIT( KIptvSmSelectCmdDateAsc, "Date ASC" );
       
  1176     _LIT( KIptvSmSelectCmdDateDescending, "Date DESC" );
       
  1177     _LIT( KIptvSmSelectCmdDisplayOrderAsc, "DisplayOrder ASC" );
       
  1178     _LIT( KIptvSmSelectCmdDisplayOrderDescending, "DisplayOrder DESC" );
       
  1179     
       
  1180     TBuf<KIptvSmServiceManagerSelectCmdLength> selectCmd;
       
  1181     
       
  1182     _LIT( KDesDesFormat, "%S%S" );
       
  1183     
       
  1184     switch ( aOrder )
       
  1185         {
       
  1186         case CIptvServiceManagementClient::EDateAscending:
       
  1187             selectCmd.Format( KDesDesFormat,
       
  1188                              &KIptvSmSelectCmdFirstPart,
       
  1189                              &KIptvSmSelectCmdDateAsc); 
       
  1190             break;
       
  1191         case CIptvServiceManagementClient::EDateDescending:
       
  1192             selectCmd.Format( KDesDesFormat, 
       
  1193                              &KIptvSmSelectCmdFirstPart,
       
  1194                              &KIptvSmSelectCmdDateDescending); 
       
  1195             break;
       
  1196         case CIptvServiceManagementClient::EDisplayOrderAscending:
       
  1197             selectCmd.Format( KDesDesFormat,
       
  1198                              &KIptvSmSelectCmdFirstPart,
       
  1199                              &KIptvSmSelectCmdDisplayOrderAsc); 
       
  1200             break;
       
  1201         case CIptvServiceManagementClient::EDisplayOrderDescending:
       
  1202             selectCmd.Format( KDesDesFormat,
       
  1203                              &KIptvSmSelectCmdFirstPart,
       
  1204                              &KIptvSmSelectCmdDisplayOrderDescending); 
       
  1205             break;
       
  1206             
       
  1207         default:
       
  1208             User::Leave( KErrArgument );
       
  1209         }
       
  1210              
       
  1211     User::LeaveIfError( view.Prepare( iServicesDb, TDbQuery( selectCmd, EDbCompareFolded ) ) );
       
  1212     User::LeaveIfError( view.EvaluateAll() );
       
  1213     CDbColSet* colSet = view.ColSetL();
       
  1214     CleanupStack::PushL( colSet ); // 4->
       
  1215     
       
  1216     for ( view.FirstL(); view.AtRow(); view.NextL() )
       
  1217         {
       
  1218         desc.Zero();
       
  1219         view.GetL();
       
  1220 
       
  1221         //set variables and set pointers to point to db items
       
  1222         id = view.ColUint32( colSet->ColNo( KIptvSmServicesDbIdCol ) );
       
  1223         name.Set( view.ColDes( colSet->ColNo( KIptvSmServicesDbNameCol ) ) );
       
  1224         address.Set( view.ColDes( colSet->ColNo( KIptvSmServicesDbAddressCol ) ) );
       
  1225         iconPath.Set( view.ColDes( colSet->ColNo( KIptvSmServicesDbIconPathCol ) ) );        
       
  1226         epgPluginUid = view.ColInt32( colSet->ColNo( KIptvSmServicesDbEpgPluginUidCol ) );
       
  1227         vodPluginUid = view.ColInt32( colSet->ColNo( KIptvSmServicesDbVodPluginUidCol ) );
       
  1228         iptvPluginUid = view.ColInt32( colSet->ColNo( KIptvSmServicesDbIptvPluginUidCol ) );
       
  1229         type = view.ColUint8( colSet->ColNo( KIptvSmServicesDbTypeCol ) );
       
  1230         iapList.Set( view.ColDes8( colSet->ColNo( KIptvSmServicesDbIapListCol ) ) );
       
  1231         
       
  1232         TDbColNo descrColNo = colSet->ColNo( KIptvSmServicesDbDescCol );
       
  1233         RDbColReadStream readStream; // A stream object for long columns
       
  1234         readStream.OpenLC( view,descrColNo ); // 5->
       
  1235         readStream.ReadL( desc, view.ColLength( descrColNo ) );
       
  1236         readStream.Close();
       
  1237         CleanupStack::Pop(); // <-5
       
  1238 				
       
  1239         //date not needed
       
  1240 
       
  1241         flags = view.ColUint32( colSet->ColNo( KIptvSmServicesDbFlagsCol ) );
       
  1242         applicationUid = view.ColInt32( colSet->ColNo( KIptvSmServicesDbApplicationUidCol ) );
       
  1243         displayOrder = view.ColUint32( colSet->ColNo( KIptvSmServicesDbDisplayOrderCol ) );
       
  1244         providerId.Set( view.ColDes( colSet->ColNo( KIptvSmServicesDbProviderIdCol ) ) );
       
  1245         accountManagementUrl.Set( view.ColDes( colSet->ColNo( KIptvSmServicesDbAccountManagementUrlCol ) ) );
       
  1246         TInt colNum = colSet->ColNo( KIptvSmServicesDbEpgUpdateTimeCol );
       
  1247         
       
  1248         if ( aVersion >= 2 || aVersion == 0 )
       
  1249             {
       
  1250             epgUpdateTime = view.ColTime( colNum );
       
  1251             iapName.Set( view.ColDes( colSet->ColNo( KIptvSmServicesDbIapNameCol ) ) );
       
  1252             
       
  1253             delete iSearchUrl;
       
  1254             iSearchUrl = NULL;
       
  1255             TDbColNo searchUrlColNo = colSet->ColNo( KIptvSmServicesDbSearchUrlCol );
       
  1256             iSearchUrl = HBufC::NewL( view.ColLength( searchUrlColNo ) );
       
  1257             RDbColReadStream readStream;
       
  1258             readStream.OpenLC( view, searchUrlColNo ); // 6->
       
  1259             TPtr searchUrlPtr( iSearchUrl->Des() );
       
  1260             readStream.ReadL( searchUrlPtr, view.ColLength( searchUrlColNo ) );
       
  1261             readStream.Close();
       
  1262             CleanupStack::Pop(); // <-6
       
  1263 
       
  1264             dlNetwork = view.ColUint32( colSet->ColNo( KIptvSmServicesDbScheduledDlNetworkCol ) );
       
  1265             dlTime = view.ColUint32( colSet->ColNo( KIptvSmServicesDbScheduledDlTimeCol ) );
       
  1266             dlType = view.ColUint32( colSet->ColNo( KIptvSmServicesDbScheduledDlTypeCol ) );
       
  1267             lastScheduledDlTime = view.ColTime( colSet->ColNo( KIptvSmServicesDbLastScheduledDlTime ) );
       
  1268             }
       
  1269             
       
  1270         if ( aVersion >= 3 || aVersion == 0 )
       
  1271             {
       
  1272             groupId = view.ColUint32( colSet->ColNo( KIptvSmServicesDbGroupIdCol ) );
       
  1273             useCategory = view.ColUint8( colSet->ColNo( KIptvSmServicesDbUseCategoryCol ) );
       
  1274             uploadProvider.Set( view.ColDes( colSet->ColNo( KIptvSmServicesDbUploadProviderCol ) ) );
       
  1275             iconUrl.Set( view.ColDes( colSet->ColNo( KIptvSmServicesDbIconUrlCol ) ) );
       
  1276 
       
  1277             TDbColNo recordUrlColNo = colSet->ColNo( KIptvSmServicesDbRecordUrlCol );
       
  1278             delete iRecordUrl;
       
  1279             iRecordUrl = NULL;
       
  1280             iRecordUrl = HBufC::NewL( view.ColLength( recordUrlColNo ) );
       
  1281 
       
  1282             RDbColReadStream readStreamRec;
       
  1283             readStreamRec.OpenLC( view, recordUrlColNo ); // 6-> 
       
  1284             TPtr recordUrlPtr( iRecordUrl->Des() );
       
  1285             readStreamRec.ReadL( recordUrlPtr, view.ColLength( recordUrlColNo ) );
       
  1286             readStreamRec.Close();
       
  1287             CleanupStack::Pop(); // <-6
       
  1288             }
       
  1289         
       
  1290         //construct CIptvService object using set methods
       
  1291         iptvService->SetId( id );
       
  1292         iptvService->SetName( name );
       
  1293         iptvService->SetAddress( address );
       
  1294         iptvService->SetIconPath( iconPath );        
       
  1295 
       
  1296         TUid uid;
       
  1297         uid.iUid = epgPluginUid;
       
  1298         iptvService->SetEpgPluginUid( uid );
       
  1299         uid.iUid = vodPluginUid;
       
  1300         iptvService->SetVodPluginUid( uid );        
       
  1301         uid.iUid = iptvPluginUid;
       
  1302         iptvService->SetIptvPluginUid( uid );
       
  1303         iptvService->SetType( ( CIptvService::TServiceType)type );
       
  1304 
       
  1305         //set IAP list        
       
  1306         RDesReadStream readStreamIap;
       
  1307         readStreamIap.Open( iapList );
       
  1308         CleanupClosePushL( readStreamIap ); // 6->
       
  1309         iptvService->iIapList->InternalizeL( readStreamIap );
       
  1310         CleanupStack::PopAndDestroy( &readStreamIap ); // <-6
       
  1311         
       
  1312         iptvService->SetDesc( desc );
       
  1313         iptvService->SetFlags( flags );
       
  1314         iptvService->SetApplicationUid( applicationUid );
       
  1315         iptvService->SetDisplayOrder( displayOrder );
       
  1316         iptvService->SetProviderId( providerId );
       
  1317         iptvService->SetAccountManagementUrl( accountManagementUrl );
       
  1318         
       
  1319         if ( aVersion >= 2 || aVersion == 0 )
       
  1320             {
       
  1321             iptvService->SetEpgUpdateTimeL( epgUpdateTime );
       
  1322             iptvService->SetIapNameL( iapName );
       
  1323 
       
  1324             TPtr searchUrlPtr( iSearchUrl->Des() );
       
  1325             iptvService->SetSearchUrlL( searchUrlPtr );
       
  1326             delete iSearchUrl;
       
  1327             iSearchUrl = NULL;
       
  1328             iptvService->SetScheduleDlNetwork( dlNetwork );
       
  1329             iptvService->SetScheduleDlTime( dlTime );
       
  1330             iptvService->SetScheduleDlType( dlType );
       
  1331             iptvService->SetScheduledLastDownloadTime( lastScheduledDlTime );
       
  1332             }
       
  1333                 
       
  1334         if ( aVersion >= 3 || aVersion == 0 )
       
  1335             {
       
  1336             iptvService->SetGroupId( groupId );
       
  1337             iptvService->SetUseCategory( useCategory );
       
  1338             iptvService->SetUploadProvider( uploadProvider );
       
  1339             iptvService->SetIconUrl( iconUrl );
       
  1340             TPtr recordUrlPtr( iRecordUrl->Des() );
       
  1341             iptvService->SetRecordUrlL( recordUrlPtr );
       
  1342             delete iRecordUrl;
       
  1343             iRecordUrl = NULL; 
       
  1344             }
       
  1345 
       
  1346         // Copy service to services if limit flags allow, get stuff from Password db too
       
  1347         if ( IsServiceIncludedInSearch( aSearchLimitFlags, *iptvService ) )
       
  1348             {
       
  1349             TIptvSmPassword password;            
       
  1350             TRAPD(err, GetPasswordL(iptvService->GetId(), password));
       
  1351             
       
  1352             if ( err != KErrNone )
       
  1353                 {
       
  1354                 IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager:: could not get password, using empty descriptor!");
       
  1355                 iptvService->SetPassword( KIptvEmptyDes );
       
  1356                 iptvService->SetUserName( KIptvEmptyDes );
       
  1357                 }
       
  1358             else
       
  1359                 {
       
  1360                 iptvService->SetPassword( password.iPassword );
       
  1361                 iptvService->SetUserName( password.iUserName );                        
       
  1362                 }
       
  1363             
       
  1364             services->AddL( *iptvService );
       
  1365             }
       
  1366 
       
  1367         } //for(
       
  1368 
       
  1369     view.Close();
       
  1370 
       
  1371     CleanupStack::PopAndDestroy( colSet );     //<-4
       
  1372     CleanupStack::PopAndDestroy( iptvService );//<-3
       
  1373     CleanupStack::Pop( services );             //<-2 ownership moves to caller
       
  1374     CleanupStack::PopAndDestroy( this );       //<-1
       
  1375     
       
  1376     IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager::GetServicesL(aSearchLimitFlags, aOrder) exit");
       
  1377     return services;
       
  1378     }
       
  1379 
       
  1380 // ---------------------------------------------------------------------------
       
  1381 // CIptvServiceManager::GetServicesUsingIdL()
       
  1382 // Get services from the database using index search.
       
  1383 // ---------------------------------------------------------------------------
       
  1384 //
       
  1385 CIptvServices* CIptvServiceManager::GetServicesUsingIdL(
       
  1386     TUint32 aStartId,
       
  1387     TUint32 aEndId,
       
  1388     TBool aDoOpenClose)
       
  1389     {	    
       
  1390     IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager::GetServicesUsingIdL(aStartId, aEndId)");
       
  1391 	
       
  1392 	if ( aDoOpenClose )
       
  1393         {
       
  1394 	    OpenL();
       
  1395 	    CleanupClosePushL( *this ); // 1->
       
  1396         }
       
  1397         	  
       
  1398     TUint i;
       
  1399     RDbTable rowset;
       
  1400     TDbSeekKey seekKey;
       
  1401 
       
  1402     TInt err( rowset.Open(iServicesDb, KIptvSmServicesTable, rowset.EReadOnly ) );		
       
  1403     if ( err != KErrNone )
       
  1404         {
       
  1405         IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager::GetServicesL(aStartId, aEndId, aResultArray) Can't open service DB, aborting!!!");
       
  1406         User::Leave( err );
       
  1407         }
       
  1408     
       
  1409     CleanupClosePushL( rowset ); // 2->
       
  1410     err = rowset.SetIndex( KIptvSmServicesDbIndexName );
       
  1411     if ( err != KErrNone )
       
  1412         {
       
  1413         IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager::GetServicesL(aStartId, aEndId, aResultArray) Can't use index, aborting!!!");
       
  1414         User::Leave( err );
       
  1415         }
       
  1416 
       
  1417     IPTVLOGSTRING3_LOW_LEVEL("CIptvServiceManager:: startId = %d, endId = %d", aStartId, aEndId);
       
  1418     CDbColSet* colSet = rowset.ColSetL();
       
  1419     CleanupStack::PushL( colSet ); // 3->
       
  1420     
       
  1421     TInt idColumnNo                   = colSet->ColNo( KIptvSmServicesDbIdCol );
       
  1422     TInt nameColumnNo                 = colSet->ColNo( KIptvSmServicesDbNameCol );
       
  1423     TInt addressColumnNo              = colSet->ColNo( KIptvSmServicesDbAddressCol );
       
  1424     TInt iconPathColumnNo             = colSet->ColNo( KIptvSmServicesDbIconPathCol );
       
  1425     TInt epgPluginUidColumnNo         = colSet->ColNo( KIptvSmServicesDbEpgPluginUidCol );
       
  1426     TInt vodPluginUidColumnNo         = colSet->ColNo( KIptvSmServicesDbVodPluginUidCol );
       
  1427     TInt iptvPluginUidColumnNo        = colSet->ColNo( KIptvSmServicesDbIptvPluginUidCol );
       
  1428     TInt typeColumnNo                 = colSet->ColNo( KIptvSmServicesDbTypeCol );
       
  1429     TInt iapListColumnNo              = colSet->ColNo( KIptvSmServicesDbIapListCol );
       
  1430     TInt descColumnNo                 = colSet->ColNo( KIptvSmServicesDbDescCol );
       
  1431     TInt flagsColumnNo                = colSet->ColNo( KIptvSmServicesDbFlagsCol );
       
  1432     TInt applicationUidColumnNo       = colSet->ColNo( KIptvSmServicesDbApplicationUidCol );
       
  1433     TInt displayOrderColumnNo         = colSet->ColNo( KIptvSmServicesDbDisplayOrderCol );
       
  1434     TInt providerIdColumnNo           = colSet->ColNo( KIptvSmServicesDbProviderIdCol );
       
  1435     TInt accountManagementUrlColumnNo = colSet->ColNo( KIptvSmServicesDbAccountManagementUrlCol );
       
  1436     TInt epgUpdateTimeColumnNo        = colSet->ColNo( KIptvSmServicesDbEpgUpdateTimeCol );
       
  1437     TInt iapNameColumnNo              = colSet->ColNo( KIptvSmServicesDbIapNameCol );
       
  1438     TInt searchUrlColumnNo            = colSet->ColNo( KIptvSmServicesDbSearchUrlCol );
       
  1439     TInt dlNetworklColumnNo           = colSet->ColNo( KIptvSmServicesDbScheduledDlNetworkCol );
       
  1440     TInt dlTimeColumnNo               = colSet->ColNo( KIptvSmServicesDbScheduledDlTimeCol );
       
  1441     TInt dlTypeColumnNo               = colSet->ColNo( KIptvSmServicesDbScheduledDlTypeCol );
       
  1442     TInt dlLastTimeColumnNo           = colSet->ColNo( KIptvSmServicesDbLastScheduledDlTime );
       
  1443     TInt groupIdColumnNo              = colSet->ColNo( KIptvSmServicesDbGroupIdCol );
       
  1444     TInt useCatColumnNo               = colSet->ColNo( KIptvSmServicesDbUseCategoryCol );
       
  1445     TInt uploadColumnNo               = colSet->ColNo( KIptvSmServicesDbUploadProviderCol );
       
  1446     TInt iconUrlColumnNo              = colSet->ColNo( KIptvSmServicesDbIconUrlCol );
       
  1447     TInt recordUrlColumnNo            = colSet->ColNo(KIptvSmServicesDbRecordUrlCol);
       
  1448 
       
  1449     CleanupStack::PopAndDestroy( colSet ); // <-3
       
  1450     CIptvServices* services = CIptvServices::NewL();
       
  1451     CleanupStack::PushL( services ); // 3->
       
  1452 
       
  1453     CIptvService* service = CIptvService::NewL();
       
  1454     CleanupStack::PushL( service ); // 4->
       
  1455 	
       
  1456     for ( i = aStartId; i <= aEndId; i++ )
       
  1457         {
       
  1458         seekKey = i;
       
  1459 		
       
  1460         // Search the index
       
  1461         if ( rowset.SeekL( seekKey ) )
       
  1462             {
       
  1463             TUid uid;
       
  1464 
       
  1465             rowset.GetL();
       
  1466             service->SetId( rowset.ColUint32( idColumnNo ) );
       
  1467             service->SetName( rowset.ColDes( nameColumnNo ) );
       
  1468             service->SetAddress( rowset.ColDes( addressColumnNo ) );
       
  1469             service->SetIconPath( rowset.ColDes( iconPathColumnNo ) );
       
  1470 
       
  1471       	    uid.iUid = rowset.ColInt32( epgPluginUidColumnNo );
       
  1472             service->SetEpgPluginUid( uid );
       
  1473             uid.iUid = rowset.ColInt32( vodPluginUidColumnNo );
       
  1474             service->SetVodPluginUid( uid );
       
  1475             uid.iUid = rowset.ColInt32( iptvPluginUidColumnNo );
       
  1476             service->SetIptvPluginUid( uid );
       
  1477 
       
  1478             service->SetType( ( CIptvService::TServiceType )( rowset.ColUint8( typeColumnNo ) ) );
       
  1479             
       
  1480             //set IAP-list
       
  1481             RDesReadStream readStreamIap;
       
  1482             readStreamIap.Open(rowset.ColDes8( iapListColumnNo ) );
       
  1483             CleanupClosePushL( readStreamIap ); // 5->
       
  1484             service->iIapList->InternalizeL( readStreamIap );
       
  1485             CleanupStack::PopAndDestroy( &readStreamIap ); // <-5
       
  1486                         
       
  1487             TBuf<KIptvSmServicesDbDescMaxLength> desc;
       
  1488             RDbColReadStream readStream;       // A stream object for long columns
       
  1489             readStream.OpenLC( rowset,descColumnNo ); // 5->
       
  1490             readStream.ReadL( desc, rowset.ColLength( descColumnNo ) );
       
  1491             CleanupStack::PopAndDestroy( &readStream ); // <-5
       
  1492 
       
  1493             service->SetDesc( desc );
       
  1494 
       
  1495             service->SetFlags(rowset.ColUint32( flagsColumnNo ) );
       
  1496             service->SetApplicationUid( rowset.ColInt32( applicationUidColumnNo ) );
       
  1497             service->SetDisplayOrder( rowset.ColUint32( displayOrderColumnNo ) );
       
  1498             service->SetProviderId( rowset.ColDes( providerIdColumnNo ) );
       
  1499             service->SetAccountManagementUrl( rowset.ColDes( accountManagementUrlColumnNo ) );
       
  1500             service->SetEpgUpdateTimeL( rowset.ColTime( epgUpdateTimeColumnNo ) );
       
  1501             service->SetIapNameL( rowset.ColDes( iapNameColumnNo ) );
       
  1502 
       
  1503             delete iSearchUrl;
       
  1504             iSearchUrl = NULL;
       
  1505             iSearchUrl = HBufC::NewL( rowset.ColLength( searchUrlColumnNo ) );
       
  1506             TPtr searchUrlPtr( iSearchUrl->Des() );
       
  1507             readStream.OpenLC( rowset, searchUrlColumnNo ); // 5->
       
  1508             readStream.ReadL( searchUrlPtr, rowset.ColLength( searchUrlColumnNo ) );
       
  1509             CleanupStack::PopAndDestroy( &readStream ); // <-5
       
  1510             service->SetSearchUrlL( searchUrlPtr );
       
  1511             delete iSearchUrl;
       
  1512             iSearchUrl = NULL;
       
  1513             
       
  1514             service->SetScheduleDlNetwork( rowset.ColUint32( dlNetworklColumnNo ) );
       
  1515             service->SetScheduleDlTime( rowset.ColUint32( dlTimeColumnNo ) );
       
  1516             service->SetScheduleDlType( rowset.ColUint32( dlTypeColumnNo ) );
       
  1517             service->SetScheduledLastDownloadTime( rowset.ColTime( dlLastTimeColumnNo ) );
       
  1518 
       
  1519             service->SetGroupId( rowset.ColUint32( groupIdColumnNo ) );
       
  1520             service->SetUseCategory( ( CIptvService::TServiceType )( rowset.ColUint8( useCatColumnNo ) ) );
       
  1521             service->SetUploadProvider( rowset.ColDes( uploadColumnNo ) );
       
  1522             service->SetIconUrl( rowset.ColDes( iconUrlColumnNo ) );
       
  1523 
       
  1524             delete iRecordUrl;
       
  1525             iRecordUrl = NULL;
       
  1526             iRecordUrl = HBufC::NewL( rowset.ColLength(recordUrlColumnNo) );
       
  1527             TPtr recordUrlPtr( iRecordUrl->Des() );
       
  1528             readStream.OpenLC(rowset, recordUrlColumnNo); // 5->
       
  1529             readStream.ReadL(recordUrlPtr, rowset.ColLength(recordUrlColumnNo));
       
  1530             CleanupStack::PopAndDestroy(&readStream); // <-5
       
  1531             service->SetRecordUrlL( recordUrlPtr );
       
  1532             delete iRecordUrl;
       
  1533             iRecordUrl = NULL;
       
  1534             
       
  1535             //get password and user name from Passwords DB
       
  1536             TIptvSmPassword password;
       
  1537             TRAP( err, GetPasswordL( service->GetId(), password ) )
       
  1538             if ( err != KErrNone )
       
  1539                 {
       
  1540                 IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager:: Couldn't get password, using empty descriptor");
       
  1541                 service->SetUserName(KIptvEmptyDes);
       
  1542                 service->SetPassword(KIptvEmptyDes);
       
  1543                 }
       
  1544             else
       
  1545                 {
       
  1546                 service->SetUserName( password.iUserName );
       
  1547                 service->SetPassword( password.iPassword );
       
  1548                 }
       
  1549                 
       
  1550             services->AddL( *service );
       
  1551             }
       
  1552         else
       
  1553             {
       
  1554             /* was not found, that's ok, there might be gaps in indexes */
       
  1555             }
       
  1556 
       
  1557         if ( i == 0xffffffff )
       
  1558             {
       
  1559             //this is necessarry since 0xffffffff + 0x01 = 0x00 -> for loop gets messed.
       
  1560             break; //break from for()
       
  1561             }
       
  1562 
       
  1563         } //for()
       
  1564 	
       
  1565     CleanupStack::PopAndDestroy( service ); // <-4	
       
  1566     CleanupStack::Pop( services );          // <-3 ownership moves to caller
       
  1567     CleanupStack::PopAndDestroy( &rowset ); // <-2
       
  1568 
       
  1569 	if ( aDoOpenClose )
       
  1570         {
       
  1571         CleanupStack::PopAndDestroy( this ); // <-1
       
  1572         }
       
  1573             
       
  1574     IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager::GetServicesUsingIdL(aStartId, aEndId) exit");
       
  1575 
       
  1576     return services;
       
  1577     }
       
  1578 // ---------------------------------------------------------------------------
       
  1579 // CIptvServiceManager::DeleteServiceL()
       
  1580 //
       
  1581 // Delete a service using id and RDbUpdate (DML)
       
  1582 // ---------------------------------------------------------------------------
       
  1583 TInt CIptvServiceManager::DeleteServiceL( TUint32 aId )
       
  1584     {
       
  1585     IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager::DeleteServiceL()");
       
  1586     
       
  1587     OpenL();
       
  1588 
       
  1589     CompactDatabases();
       
  1590             
       
  1591     const TInt KIptvSmIdDesMaxSize = 20;
       
  1592     TBuf<KIptvSmIdDesMaxSize> idDes;
       
  1593     idDes.Zero();
       
  1594     idDes.Num(aId);
       
  1595 
       
  1596     IPTVLOGSTRING2_LOW_LEVEL("CIptvServiceManager:: idDes = %S", &idDes);
       
  1597     
       
  1598     _LIT(KIptvSmSqlDeleteCmd, "DELETE FROM %S WHERE %S = ");
       
  1599                	
       
  1600     // Sql: DELETE FROM Services WHERE %S LIKE 'aId'
       
  1601     TBuf<KIptvSmCustomSqlMaxLength+KIptvSmIdDesMaxSize> sqlStr;
       
  1602     sqlStr.Format(KIptvSmSqlDeleteCmd, 
       
  1603                   &KIptvSmServicesTable,
       
  1604                   &KIptvSmServicesDbIdCol);
       
  1605 
       
  1606     sqlStr.Append(idDes);
       
  1607 
       
  1608     TInt servicesDbDeleteError = ExecuteSqlCmd(sqlStr, iServicesDb);
       
  1609 
       
  1610     sqlStr.Format(KIptvSmSqlDeleteCmd, 
       
  1611                   &KIptvSmPasswordsTable,
       
  1612                   &KIptvSmPasswordsDbIdCol);
       
  1613 
       
  1614     sqlStr.Append(idDes);
       
  1615 
       
  1616     TInt passwordsDbDeleteError = ExecuteSqlCmd(sqlStr, iPasswordsDb);
       
  1617     
       
  1618     Close();
       
  1619     
       
  1620     InformObserversL(CIptvSmEvent::EServiceDeleted, aId, NULL);
       
  1621 
       
  1622     TInt error = KErrNone;
       
  1623         
       
  1624     if(servicesDbDeleteError != KErrNone)
       
  1625         {
       
  1626         IPTVLOGSTRING2_LOW_LEVEL("CIptvServiceManager:: error in deleting from Services db: %d", servicesDbDeleteError);
       
  1627         error = servicesDbDeleteError;
       
  1628         }
       
  1629     if(passwordsDbDeleteError != KErrNone)
       
  1630         {
       
  1631         IPTVLOGSTRING2_LOW_LEVEL("CIptvServiceManager:: error in deleting from Passwords db: %d", passwordsDbDeleteError);
       
  1632         error = passwordsDbDeleteError;
       
  1633         }
       
  1634 
       
  1635     IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager::DeleteServiceL() exit");
       
  1636     return error;
       
  1637     }
       
  1638 
       
  1639 // ---------------------------------------------------------------------------
       
  1640 // CIptvServiceManager::IsServiceIncludedInSearch()
       
  1641 //
       
  1642 // Decides if the service is included in the search. 
       
  1643 // ---------------------------------------------------------------------------
       
  1644 //
       
  1645 TBool CIptvServiceManager::IsServiceIncludedInSearch(
       
  1646     TUint32 aSearchLimitFlags,
       
  1647     CIptvService& aIptvService ) const
       
  1648     {
       
  1649         
       
  1650     if ( aSearchLimitFlags == 0 )
       
  1651         {
       
  1652         return ETrue;
       
  1653         }
       
  1654         
       
  1655     if ( ( aSearchLimitFlags & CIptvServiceManagementClient::ESelectedServices ) &&
       
  1656         !( aIptvService.GetFlags() & CIptvService::ESelected ) )
       
  1657         {
       
  1658         return EFalse;
       
  1659         }
       
  1660 
       
  1661     if ( ( aSearchLimitFlags & CIptvServiceManagementClient::EMainServices ) &&
       
  1662         !( aIptvService.GetFlags() & CIptvService::EMainService ) )
       
  1663         {
       
  1664         return EFalse;
       
  1665         }
       
  1666             
       
  1667     if ( ( aSearchLimitFlags & CIptvServiceManagementClient::ESubServices ) &&
       
  1668          ( aIptvService.GetFlags() & CIptvService::EMainService ) )
       
  1669         {
       
  1670         return EFalse;
       
  1671         }
       
  1672 
       
  1673     if ( ( aSearchLimitFlags & CIptvServiceManagementClient::EVod ) &&
       
  1674          ( aIptvService.GetType() != CIptvService::EVod ) )
       
  1675         {
       
  1676         return EFalse;
       
  1677         }
       
  1678 
       
  1679     if ( ( aSearchLimitFlags & CIptvServiceManagementClient::EVodCast ) &&
       
  1680          ( aIptvService.GetType() != CIptvService::EVodCast ) )
       
  1681         {
       
  1682         return EFalse;
       
  1683         }
       
  1684 
       
  1685     if ( ( aSearchLimitFlags & CIptvServiceManagementClient::ELiveTv ) &&
       
  1686          ( aIptvService.GetType() != CIptvService::ELiveTv ) )
       
  1687         {
       
  1688         return EFalse;
       
  1689         }
       
  1690 
       
  1691     if ( ( aSearchLimitFlags & CIptvServiceManagementClient::EBrowser ) &&
       
  1692          ( aIptvService.GetType() != CIptvService::EBrowser ) )
       
  1693         {
       
  1694         return EFalse;
       
  1695         }
       
  1696 
       
  1697     if ( ( aSearchLimitFlags & CIptvServiceManagementClient::EVideoRemote ) &&
       
  1698          ( aIptvService.GetType() != CIptvService::EVideoRemote ) )
       
  1699         {
       
  1700         return EFalse;
       
  1701         }
       
  1702 
       
  1703     if ( ( aSearchLimitFlags & CIptvServiceManagementClient::EApplication ) &&
       
  1704          ( aIptvService.GetType() != CIptvService::EApplication ) )
       
  1705         {
       
  1706         return EFalse;
       
  1707         }
       
  1708 
       
  1709     if ( ( aSearchLimitFlags & CIptvServiceManagementClient::EMobileTv ) &&
       
  1710          ( aIptvService.GetType() != CIptvService::EMobileTv ) )
       
  1711         {
       
  1712         return EFalse;
       
  1713         }
       
  1714 
       
  1715     if ( ( aSearchLimitFlags & CIptvServiceManagementClient::EVodServices ) &&
       
  1716          ( aIptvService.GetType() != CIptvService::EVod ) &&
       
  1717          ( aIptvService.GetType() != CIptvService::EVodCast ) &&
       
  1718          ( aIptvService.GetType() != CIptvService::EServiceGroup ) )
       
  1719         {
       
  1720         return EFalse;
       
  1721         }
       
  1722 
       
  1723     if ( ( aSearchLimitFlags & CIptvServiceManagementClient::EServiceGroup ) &&
       
  1724          ( aIptvService.GetType() != CIptvService::EServiceGroup ||
       
  1725            aIptvService.GetGroupId() == 0 ) )
       
  1726         {
       
  1727         return EFalse;
       
  1728         }
       
  1729     
       
  1730     if ( ( aSearchLimitFlags & CIptvServiceManagementClient::EGroupedServices ) &&
       
  1731        ( !( aIptvService.GetFlags() & CIptvService::EGroupedService ) ||
       
  1732            aIptvService.GetGroupId() == 0 ) )
       
  1733         {
       
  1734         return EFalse;
       
  1735         }
       
  1736 
       
  1737     if ( ( aSearchLimitFlags & CIptvServiceManagementClient::EOther ) &&
       
  1738          ( aIptvService.GetType() != CIptvService::EOther ) )
       
  1739         {
       
  1740         return EFalse;
       
  1741         }
       
  1742         
       
  1743     return ETrue;
       
  1744     }
       
  1745 
       
  1746 // ---------------------------------------------------------------------------
       
  1747 // CIptvServiceManager::GetServiceL()
       
  1748 // ---------------------------------------------------------------------------
       
  1749 //
       
  1750 CIptvService* CIptvServiceManager::GetServiceL(
       
  1751     TUint32 aServiceId,
       
  1752     TBool aDoOpenClose )
       
  1753     {
       
  1754     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::GetServiceL()");
       
  1755 
       
  1756     if ( aDoOpenClose )
       
  1757         {
       
  1758         OpenL();
       
  1759         CleanupClosePushL( *this ); // 1->
       
  1760         }
       
  1761         
       
  1762     CIptvServices* services = GetServicesUsingIdL( aServiceId, aServiceId, aDoOpenClose );
       
  1763     
       
  1764     CIptvService* service = NULL;
       
  1765 
       
  1766     if ( services )
       
  1767         {
       
  1768         CleanupStack::PushL( services ); // 2->
       
  1769         
       
  1770         if ( services->Count() != 1 )
       
  1771             {
       
  1772             IPTVLOGSTRING2_HIGH_LEVEL("CIptvServiceManager:: Could not get service data from Service Manager with ID: %x, returning NULL", aServiceId);
       
  1773             }
       
  1774         else
       
  1775             {    
       
  1776             service = services->GetServiceL( 0 );
       
  1777             }
       
  1778         
       
  1779         CleanupStack::PopAndDestroy( services ); //<-2
       
  1780         }
       
  1781         
       
  1782     if ( aDoOpenClose )
       
  1783         {
       
  1784         CleanupStack::PopAndDestroy( this ); //<-1
       
  1785         }
       
  1786             
       
  1787     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::GetServiceL() exit");            
       
  1788     return service;
       
  1789     }
       
  1790 
       
  1791 // ---------------------------------------------------------------------------
       
  1792 // CIptvServiceManager::RegisterObserver()
       
  1793 // ---------------------------------------------------------------------------
       
  1794 //
       
  1795 TInt CIptvServiceManager::RegisterObserver(MIptvServiceManagerObserver* aObserver)
       
  1796     {
       
  1797     return iObservers.Append(aObserver);
       
  1798     }
       
  1799 
       
  1800 // ---------------------------------------------------------------------------
       
  1801 // CIptvServiceManager::DeRegisterObserver()
       
  1802 // ---------------------------------------------------------------------------
       
  1803 //
       
  1804 void CIptvServiceManager::DeRegisterObserver(MIptvServiceManagerObserver* aObserver)
       
  1805     {
       
  1806     TInt i;
       
  1807     for(i = 0; i < iObservers.Count(); i++)
       
  1808         {
       
  1809         if(aObserver == iObservers[i])
       
  1810             {
       
  1811             iObservers.Remove(i);
       
  1812             return;
       
  1813             }
       
  1814         }
       
  1815     }
       
  1816 
       
  1817 // ---------------------------------------------------------------------------
       
  1818 // CIptvServiceManager::InformObserversL()
       
  1819 // ---------------------------------------------------------------------------
       
  1820 //
       
  1821 void CIptvServiceManager::InformObserversL(
       
  1822     CIptvSmEvent::TEvent aEvent,
       
  1823     TUint32 aServiceId,
       
  1824     CIptvService* aService)
       
  1825     {
       
  1826     CIptvSmEvent* event = CIptvSmEvent::NewL();
       
  1827     CleanupStack::PushL(event); // 1->
       
  1828 
       
  1829     event->iEvent = aEvent;
       
  1830     event->iServiceId = aServiceId;
       
  1831     event->iService = aService;
       
  1832 
       
  1833     TInt i;
       
  1834     for ( i = 0; i < iObservers.Count(); i++ )
       
  1835         {
       
  1836         iObservers[i]->HandleSmEvent( *event );
       
  1837         }
       
  1838     CleanupStack::PopAndDestroy( event ); // <-1
       
  1839     }
       
  1840 
       
  1841 // ---------------------------------------------------------------------------
       
  1842 // CIptvServiceManager::GetServicesL()
       
  1843 // ---------------------------------------------------------------------------
       
  1844 //
       
  1845 CIptvServices* CIptvServiceManager::GetServicesL(
       
  1846     const TDesC& aProviderId,
       
  1847     TBool aDoOpenClose)
       
  1848     {
       
  1849     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::GetServicesL(aProviderId)");
       
  1850 
       
  1851     if ( aDoOpenClose )
       
  1852         {
       
  1853         OpenL();
       
  1854         CleanupClosePushL( *this ); // 1->
       
  1855         }
       
  1856     
       
  1857     // Create a view on the database    
       
  1858     RDbView view;
       
  1859 
       
  1860     _LIT(KSelectCmdFormat, "SELECT ID FROM Services WHERE ProviderId = '%S' ORDER BY ID");
       
  1861     const TInt KSelectCmdFormatLength = 70;
       
  1862     TBuf<KIptvSmServicesDbProviderIdMaxLength + KSelectCmdFormatLength> selectCmd;
       
  1863     selectCmd.Format(KSelectCmdFormat, &aProviderId);
       
  1864     User::LeaveIfError(view.Prepare(iServicesDb, TDbQuery(selectCmd, EDbCompareFolded)));
       
  1865     User::LeaveIfError(view.EvaluateAll());
       
  1866 
       
  1867     CDbColSet* colSet = view.ColSetL();
       
  1868     CleanupStack::PushL(colSet); // 2->
       
  1869     
       
  1870     CIptvService* service   = NULL;
       
  1871     CIptvServices* services = CIptvServices::NewL();
       
  1872     CleanupStack::PushL(services); // 3->
       
  1873     
       
  1874     TUint32 id;
       
  1875     for (view.FirstL(); view.AtRow(); view.NextL())
       
  1876         {
       
  1877         view.GetL();
       
  1878         id = view.ColUint32(colSet->ColNo(KIptvSmServicesDbIdCol));
       
  1879         IPTVLOGSTRING2_HIGH_LEVEL("CIptvServiceManager:: found ID: %d", id);
       
  1880         service = GetServiceL(id, aDoOpenClose);
       
  1881         if ( service )
       
  1882             {
       
  1883             CleanupStack::PushL(service); // 4->
       
  1884             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: got service for the id.");
       
  1885             TInt err = services->AddL(*service);
       
  1886             if(err == KErrNone)
       
  1887                 {
       
  1888                 IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: added to the list");
       
  1889                 }
       
  1890             else
       
  1891                 {
       
  1892                 IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: failed to add to the list");
       
  1893                 }
       
  1894             CleanupStack::PopAndDestroy(service); // <-4
       
  1895             service = NULL;
       
  1896             }
       
  1897         else
       
  1898             {
       
  1899             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: failed to get service for the ID");
       
  1900             }
       
  1901         }
       
  1902 
       
  1903     view.Close();
       
  1904 
       
  1905     CleanupStack::Pop(services);         // <-3
       
  1906     CleanupStack::PopAndDestroy(colSet); // <-2
       
  1907     
       
  1908     if (aDoOpenClose)
       
  1909         {
       
  1910         CleanupStack::PopAndDestroy(this); // <-1
       
  1911         }
       
  1912         
       
  1913     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::GetServicesL(aProviderId) exit");
       
  1914     return services;
       
  1915     }
       
  1916 
       
  1917 // ---------------------------------------------------------------------------
       
  1918 // CIptvServiceManager::GetPasswordL()
       
  1919 // Databases are already open when this func is called
       
  1920 // ---------------------------------------------------------------------------
       
  1921 //
       
  1922 void CIptvServiceManager::GetPasswordL( TUint32 aId, TIptvSmPassword& aPassword )
       
  1923     {    
       
  1924     RDbTable passwordsTable;
       
  1925     TDbSeekKey seekKey;
       
  1926     	
       
  1927     TInt err = passwordsTable.Open(iPasswordsDb, KIptvSmPasswordsTable, passwordsTable.EReadOnly);
       
  1928     if(err != KErrNone)
       
  1929         {
       
  1930         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: Could not get passwords table, aborting!");
       
  1931         User::Leave(KErrGeneral);
       
  1932         }
       
  1933     CleanupClosePushL(passwordsTable); // 1->
       
  1934 
       
  1935     err = passwordsTable.SetIndex(KIptvSmPasswordsDbIndexName);
       
  1936 
       
  1937     if(err != KErrNone)
       
  1938         {
       
  1939         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: could not set index for password table");
       
  1940         User::Leave(err);
       
  1941         }
       
  1942 
       
  1943     // Search the index
       
  1944     seekKey = (TUint)aId;
       
  1945     if(!passwordsTable.SeekL(seekKey))
       
  1946         {
       
  1947         IPTVLOGSTRING2_HIGH_LEVEL("CIptvServiceManager:: could not find id %d from passwords table", aId);
       
  1948         User::Leave(KErrNotFound);
       
  1949         }
       
  1950 
       
  1951     passwordsTable.GetL();
       
  1952 
       
  1953     CDbColSet* colSet = passwordsTable.ColSetL();
       
  1954     CleanupStack::PushL(colSet); // 2->
       
  1955     
       
  1956     aPassword.iUserName = passwordsTable.ColDes(colSet->ColNo(KIptvSmPasswordsDbUserNameCol));
       
  1957     aPassword.iPassword = passwordsTable.ColDes(colSet->ColNo(KIptvSmPasswordsDbPasswordCol));
       
  1958     
       
  1959     CleanupStack::PopAndDestroy(colSet);          // <-2
       
  1960     CleanupStack::PopAndDestroy(&passwordsTable); // <-1    
       
  1961     }
       
  1962 
       
  1963 // ---------------------------------------------------------------------------
       
  1964 // CIptvServiceManager::ExecuteSqlCmd()
       
  1965 // ---------------------------------------------------------------------------
       
  1966 //
       
  1967 TInt CIptvServiceManager::ExecuteSqlCmd(const TDesC& aSqlCmd, RDbStoreDatabase& aDb)
       
  1968     {    
       
  1969     // Initialize execution and perform the first step.
       
  1970     // Note: Execute() returns 0 (=KErrNone), but it does not affect database
       
  1971     //       until Next() is called.
       
  1972     RDbUpdate updOp;
       
  1973     TInt incStat = updOp.Execute(aDb, aSqlCmd, EDbCompareFolded);
       
  1974     incStat = updOp.Next(); // This will leave, if Execute() failed.
       
  1975 
       
  1976     while( incStat == 1 ) // Just in case, if the operation has more steps
       
  1977         {
       
  1978         incStat = updOp.Next();
       
  1979         }
       
  1980     updOp.RowCount();
       
  1981     updOp.Close();
       
  1982     return incStat;
       
  1983     }
       
  1984 
       
  1985 // ---------------------------------------------------------------------------
       
  1986 // CIptvServiceManager::ExecuteProvisioningL()
       
  1987 // ---------------------------------------------------------------------------
       
  1988 //
       
  1989 void CIptvServiceManager::ExecuteProvisioningL()
       
  1990     {
       
  1991     IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager:: doing provisioning");
       
  1992 
       
  1993     TFileName servicesXmlFile;
       
  1994     TBool foundFile = EFalse;
       
  1995     TBool updateFound = EFalse;
       
  1996 
       
  1997     //  Check if there is update file
       
  1998     foundFile = IsServiceUpdateFoundL(servicesXmlFile);
       
  1999     updateFound = foundFile;
       
  2000 
       
  2001     if (!foundFile)
       
  2002         {
       
  2003         //  Update file not found, look service file from c-disk
       
  2004         servicesXmlFile.Zero();
       
  2005         servicesXmlFile.Append(KIptvSmCDiskPrefix);
       
  2006         servicesXmlFile.Append(iPrivatePath);
       
  2007         servicesXmlFile.Append(KIptvSmServicesXmlFile);
       
  2008                 
       
  2009         IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager:: looking xml-file from c-disk");
       
  2010         foundFile = BaflUtils::FileExists(iFsSession, servicesXmlFile);
       
  2011         }
       
  2012 
       
  2013     if (!foundFile)
       
  2014         {
       
  2015         //  Wasn't on the c-disk, look service file from z-disk
       
  2016         servicesXmlFile.Zero();
       
  2017         servicesXmlFile.Append(KIptvSmZDiskPrefix);
       
  2018         servicesXmlFile.Append(iPrivatePath);
       
  2019         servicesXmlFile.Append(KIptvSmServicesXmlFile);
       
  2020                 
       
  2021         IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager:: looking xml-file from z-disk");
       
  2022         foundFile = BaflUtils::FileExists(iFsSession, servicesXmlFile);
       
  2023         }
       
  2024 
       
  2025     if (foundFile)
       
  2026         {
       
  2027         //  Some file found, provision it
       
  2028         DoProvisioningL(servicesXmlFile);
       
  2029 
       
  2030         //  Was it update file
       
  2031         if (updateFound)
       
  2032             {
       
  2033             //  Yes, remove it
       
  2034             iFsSession.Delete(servicesXmlFile);
       
  2035             }
       
  2036         }
       
  2037     }
       
  2038 
       
  2039 // ---------------------------------------------------------------------------
       
  2040 // CIptvServiceManager::IsServiceUpdateFoundL()
       
  2041 // ---------------------------------------------------------------------------
       
  2042 //
       
  2043 TBool CIptvServiceManager::IsServiceUpdateFoundL(TFileName& aServicesXmlFile)
       
  2044     {
       
  2045     //  Look update file from c-disk
       
  2046     aServicesXmlFile.Zero();
       
  2047     aServicesXmlFile.Append(KIptvSmCDiskPrefix);
       
  2048     aServicesXmlFile.Append(iPrivatePath);
       
  2049     aServicesXmlFile.Append(KIptvSmServicesUpdateXmlFile);
       
  2050             
       
  2051     IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager:: looking update xml-file from c-disk");
       
  2052 
       
  2053     return BaflUtils::FileExists(iFsSession, aServicesXmlFile);
       
  2054     }
       
  2055 
       
  2056 // ---------------------------------------------------------------------------
       
  2057 // CIptvServiceManager::DoProvisioningL()
       
  2058 // ---------------------------------------------------------------------------
       
  2059 //
       
  2060 void CIptvServiceManager::DoProvisioningL(const TDesC& aServicesXmlFile)
       
  2061     {
       
  2062     IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager::DoProvisioning()");
       
  2063 
       
  2064     CIptvProvisioningProcessor* provisioningProcessor = CIptvProvisioningProcessor::NewL();
       
  2065     CleanupStack::PushL(provisioningProcessor); // 1->
       
  2066 
       
  2067     CIptvServices* services = NULL;
       
  2068     TInt provisionError( KErrNone );
       
  2069     TRAPD(error, services = provisioningProcessor->LocalProvisionL(aServicesXmlFile, provisionError));
       
  2070     
       
  2071     if ( error == KErrNone && provisionError == KErrNone && services )
       
  2072         {
       
  2073         IPTVLOGSTRING2_LOW_LEVEL("CIptvServiceManager:: services got from xml-file: %d", services->Count());
       
  2074 
       
  2075         CleanupStack::PushL(services); // 2->
       
  2076         TInt i;
       
  2077         for(i = 0; i < services->Count(); i++)
       
  2078             {
       
  2079             AddServiceL(services->GetServiceRefL(i), EFalse /* dont do open/close */);
       
  2080             }
       
  2081         CleanupStack::PopAndDestroy(services); // <-2
       
  2082         }
       
  2083     else
       
  2084         {
       
  2085         IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager:: error while executing LocalProvisionL()");
       
  2086         }
       
  2087 
       
  2088     CleanupStack::PopAndDestroy(provisioningProcessor); // <-1
       
  2089 
       
  2090     IPTVLOGSTRING_LOW_LEVEL("CIptvServiceManager::DoProvisioning() exit");
       
  2091     }
       
  2092     
       
  2093 // ---------------------------------------------------------------------------
       
  2094 // CIptvServiceManager::CreateWritableRootDirL
       
  2095 // ---------------------------------------------------------------------------
       
  2096 //
       
  2097 void CIptvServiceManager::CreateWritableRootDirL(TDriveNumber aDrive)
       
  2098     {
       
  2099     //Create private writable root path to aDrive if it does not exist yet
       
  2100     TFileName rootPath;
       
  2101     TRAPD( err,
       
  2102            CIptvUtil::GetPathL( iFsSession, EIptvPathWritableRoot, rootPath, aDrive ) );
       
  2103 		
       
  2104     if ( err != KErrNone ) 
       
  2105         {
       
  2106         if ( aDrive == EDriveC )
       
  2107             {
       
  2108             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: Can't create writable root path to C:, panicing");	
       
  2109             IPTVLOGSTRING2_HIGH_LEVEL("CIptvServiceManager:: system wide error code = %d", err);	
       
  2110             // This is fatal, we need this folder
       
  2111             User::Panic(KIptvServiceManager, EIptvSmCouldNotCreateWritableRootPath);
       
  2112             }
       
  2113         else
       
  2114             {
       
  2115             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: Can't create writable root path to E:, skipping");
       
  2116             }
       
  2117         }
       
  2118     }
       
  2119 
       
  2120 // ---------------------------------------------------------------------------
       
  2121 // CIptvServiceManager::CheckSmDbVersionL
       
  2122 // ---------------------------------------------------------------------------
       
  2123 //
       
  2124 void CIptvServiceManager::CheckSmDbVersionL(void)
       
  2125     {         
       
  2126     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::CheckSmDbVersionL() start");
       
  2127 
       
  2128     TFileName smDbVersionFileName;
       
  2129     
       
  2130     smDbVersionFileName = KIptvSmCDiskPrefix;			
       
  2131     smDbVersionFileName.Append(iPrivatePath);
       
  2132     smDbVersionFileName.Append(KSmDbVersionFileName);
       
  2133 
       
  2134     TUint32 dbVersion = 0;
       
  2135     RFile versionFile;
       
  2136 
       
  2137     TInt err = versionFile.Open(iFsSession, smDbVersionFileName, EFileRead);
       
  2138     if (err == KErrNone)
       
  2139         {
       
  2140         const TInt KVersionLength = 4;
       
  2141         
       
  2142         TBuf8<KVersionLength> data;
       
  2143         err = versionFile.Read(data, KVersionLength);
       
  2144         versionFile.Close();
       
  2145         
       
  2146         if ( data.Length() == KVersionLength  && err == KErrNone )
       
  2147             {
       
  2148             dbVersion |= (data[0] << 24);
       
  2149             dbVersion |= (data[1] << 16);
       
  2150             dbVersion |= (data[2] << 8);
       
  2151             dbVersion |= data[3];
       
  2152             }
       
  2153         else
       
  2154             {
       
  2155             IPTVLOGSTRING2_HIGH_LEVEL("CIptvServiceManager:: version file is corrupted -> 0 version assumed (= unknown format), err: %d", err);
       
  2156             }
       
  2157         }
       
  2158     
       
  2159     if ( dbVersion != 0 )
       
  2160         {
       
  2161         if ( dbVersion != KIptvSmDbVersion )
       
  2162             {
       
  2163             IPTVLOGSTRING3_HIGH_LEVEL("CIptvServiceManager:: db is version %d and sw is version %d -> doing db conversion!", dbVersion, KIptvSmDbVersion);
       
  2164             ConvertDatabaseL( dbVersion );
       
  2165             }
       
  2166         else
       
  2167             {
       
  2168             IPTVLOGSTRING2_HIGH_LEVEL("CIptvServiceManager:: Both databases and sw are version %d", dbVersion);
       
  2169             }
       
  2170         }
       
  2171     else 
       
  2172         {
       
  2173         IPTVLOGSTRING2_HIGH_LEVEL("CIptvServiceManager:: db is version 0 (= unknown format), sw is version %d, deleting old databases.", KIptvSmDbVersion);
       
  2174         iFsSession.Delete(iServicesFile);
       
  2175         iFsSession.Delete(iPasswordsFile);
       
  2176         }
       
  2177         
       
  2178     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::CheckSmDbVersionL() exit");
       
  2179     }        
       
  2180 
       
  2181 // ---------------------------------------------------------------------------
       
  2182 // CIptvServiceManager::SaveSmDbVersion
       
  2183 // ---------------------------------------------------------------------------
       
  2184 //
       
  2185 void CIptvServiceManager::SaveSmDbVersion(void)
       
  2186     {
       
  2187     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::SaveSmDbVersion() start");
       
  2188 
       
  2189     TFileName smDbVersionFileName;
       
  2190     
       
  2191     smDbVersionFileName = KIptvSmCDiskPrefix;			
       
  2192     smDbVersionFileName.Append(iPrivatePath);
       
  2193     smDbVersionFileName.Append(KSmDbVersionFileName);
       
  2194 
       
  2195     RFile versionFile;
       
  2196     TInt err = versionFile.Replace(iFsSession, smDbVersionFileName, EFileWrite);
       
  2197     if (err == KErrNone)
       
  2198         {
       
  2199         TBuf8<4> versionDes;
       
  2200         versionDes.Append( static_cast<TUint8>( ( KIptvSmDbVersion >> 24 ) & 0x000000ff ) );
       
  2201         versionDes.Append( static_cast<TUint8>( ( KIptvSmDbVersion >> 16 ) & 0x000000ff ) );
       
  2202         versionDes.Append( static_cast<TUint8>( ( KIptvSmDbVersion >> 8 ) & 0x000000ff ) );
       
  2203         versionDes.Append( static_cast<TUint8>( KIptvSmDbVersion & 0x000000ff ) );
       
  2204         err = versionFile.Write(versionDes);
       
  2205         versionFile.Close();
       
  2206         if ( err == KErrNone )
       
  2207             {
       
  2208             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: version saved");    
       
  2209             }
       
  2210         else
       
  2211             {
       
  2212             IPTVLOGSTRING2_HIGH_LEVEL("CIptvServiceManager:: version file write FAILED: %d", err );
       
  2213             }
       
  2214         
       
  2215         }
       
  2216     else
       
  2217         {
       
  2218         IPTVLOGSTRING2_HIGH_LEVEL("CIptvServiceManager:: couldn't create %S file!", &KSmDbVersionFileName );
       
  2219         }
       
  2220         
       
  2221     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::SaveSmDbVersion() exit");
       
  2222     }
       
  2223 
       
  2224 // ---------------------------------------------------------------------------
       
  2225 // CIptvServiceManager::ConvertDatabaseL
       
  2226 // ---------------------------------------------------------------------------
       
  2227 //
       
  2228 void CIptvServiceManager::ConvertDatabaseL( TInt aOldVersion )
       
  2229     {
       
  2230     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::ConvertDatabaseL() start");
       
  2231 
       
  2232     //read old services
       
  2233     CIptvServices* oldDbServices = GetServicesL(
       
  2234         0 /* 0 = all services */,
       
  2235         CIptvServiceManagementClient::EDateAscending,
       
  2236         aOldVersion );
       
  2237     CleanupStack::PushL( oldDbServices ); // 1->
       
  2238 
       
  2239     //delete old db
       
  2240     iFsSession.Delete( iServicesFile );
       
  2241     iFsSession.Delete( iPasswordsFile );
       
  2242 
       
  2243     //create empty db files   
       
  2244     CreateFilesL( EFalse /* dont do provisioning */);
       
  2245     
       
  2246     //open db, does not execute provisioning since files are already created
       
  2247     OpenL();
       
  2248     CleanupClosePushL( *this ); // 2->
       
  2249 
       
  2250     //write to new db
       
  2251     IPTVLOGSTRING2_HIGH_LEVEL("CIptvServiceManager:: %d services found for conversion", oldDbServices->Count());
       
  2252 
       
  2253     for ( TInt i( 0 ); i < oldDbServices->Count(); i++ )
       
  2254         {
       
  2255         IPTVLOGSTRING2_HIGH_LEVEL("CIptvServiceManager:: converting %S", &(oldDbServices->GetServiceRefL(i).GetName()));
       
  2256         AddServiceL(oldDbServices->GetServiceRefL(i), EFalse /* dont do open/close */);
       
  2257         }
       
  2258     
       
  2259     ExecuteProvisioningL();
       
  2260     
       
  2261     CleanupStack::PopAndDestroy( this ); // <-2
       
  2262     CleanupStack::PopAndDestroy( oldDbServices ); // <-1
       
  2263          
       
  2264     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::ConvertDatabaseL() exit");
       
  2265     }
       
  2266 
       
  2267 // ---------------------------------------------------------------------------
       
  2268 // CIptvServiceManager::AddService
       
  2269 // ---------------------------------------------------------------------------
       
  2270 //
       
  2271 void CIptvServiceManager::AddServiceL( 
       
  2272     CIptvService& aService,
       
  2273     TBool aDoOpenClose )
       
  2274     {
       
  2275     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::AddServiceL() start");
       
  2276 
       
  2277     if ( aDoOpenClose )
       
  2278         {
       
  2279         OpenL();
       
  2280         CleanupClosePushL( *this ); // 1->
       
  2281         }
       
  2282         
       
  2283     //find out mode (update or add)
       
  2284     TUint32 mode = 0;
       
  2285     if ( aService.GetProviderId() != KIptvEmptyDes )
       
  2286         {
       
  2287         CIptvServices* servicesAlready = GetServicesL(aService.GetProviderId(), EFalse /* Dont do open & close */);
       
  2288         CleanupStack::PushL(servicesAlready); // 2->
       
  2289         if ( servicesAlready->Count() > 0 )
       
  2290             {
       
  2291             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: found service with same provider id, using update mode");
       
  2292             mode = 0; //0 = update
       
  2293             aService.SetId(servicesAlready->GetServiceRefL(0).GetId()); //set id to correct value
       
  2294             }
       
  2295         else
       
  2296             {
       
  2297             IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: provider id was unique, using add mode");
       
  2298             mode = EAddService;
       
  2299             }
       
  2300         CleanupStack::PopAndDestroy( servicesAlready ); // <-2
       
  2301         }
       
  2302     else
       
  2303         {
       
  2304         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: provider id was empty, using add mode");
       
  2305         mode = EAddService;
       
  2306         }
       
  2307                 
       
  2308     mode |= EDontOpen;
       
  2309     
       
  2310     AddOrUpdateServiceL( aService, mode );
       
  2311 
       
  2312 #if defined(__SERIES60_30__) || defined(__SERIES60_31__) || defined(__SERIES60_32__)
       
  2313     // Provision Video Store service to Active Space.
       
  2314     if ( ( aService.GetName().CompareF( KQtnIptvVideoStoreList ) == 0 ) &&
       
  2315          ( aService.GetType() == CIptvService::EBrowser ) )
       
  2316         {
       
  2317         // Note; missing Active Space helper or leave in Active Space must
       
  2318         // not interrupt finishing of for example factory provisioning.
       
  2319         TRAP_IGNORE( UpdateVideoStoreSpaceDbL( aService ) );
       
  2320         }
       
  2321 #endif // defined(__SERIES60_30__) || defined(__SERIES60_31__) || defined(__SERIES60_32__)
       
  2322 
       
  2323     if ( aDoOpenClose )
       
  2324         {
       
  2325         CleanupStack::PopAndDestroy( this ); // <-1
       
  2326         }
       
  2327         
       
  2328     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::AddServiceL() exit");
       
  2329     }
       
  2330 
       
  2331 // ---------------------------------------------------------------------------
       
  2332 // CIptvServiceManager::UpdateService
       
  2333 // ---------------------------------------------------------------------------
       
  2334 //
       
  2335 void CIptvServiceManager::UpdateServiceL( CIptvService& aService )
       
  2336     {
       
  2337     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::UpdateServiceL() start");
       
  2338 
       
  2339     AddOrUpdateServiceL( aService, 0 /* 0 = update */ );
       
  2340 
       
  2341 #if defined(__SERIES60_30__) || defined(__SERIES60_31__) || defined(__SERIES60_32__)
       
  2342     // Provision Video Store service to Active Space.
       
  2343     if ( ( aService.GetName().CompareF( KQtnIptvVideoStoreList ) == 0 ) &&
       
  2344          ( aService.GetType() == CIptvService::EBrowser ) )
       
  2345         {
       
  2346         TRAP_IGNORE( UpdateVideoStoreSpaceDbL( aService ) );
       
  2347         }
       
  2348 #endif // defined(__SERIES60_30__) || defined(__SERIES60_31__) || defined(__SERIES60_32__)
       
  2349 
       
  2350     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::UpdateServiceL() exit");
       
  2351     }
       
  2352 
       
  2353 // ---------------------------------------------------------------------------
       
  2354 // CIptvServiceManager::CompactDatabases
       
  2355 // ---------------------------------------------------------------------------
       
  2356 //
       
  2357 void CIptvServiceManager::CompactDatabases()
       
  2358     {
       
  2359     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::CompactDatabases() start");
       
  2360 
       
  2361     if ( iCompactCounter == 0 )
       
  2362         {
       
  2363         IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager:: running compact for databases");
       
  2364         iServicesDb.Compact();
       
  2365         iPasswordsDb.Compact();
       
  2366         }
       
  2367 
       
  2368     iCompactCounter++;
       
  2369 
       
  2370     if ( iCompactCounter > KIptvSmServiceDbCompactingFrequency )
       
  2371         {
       
  2372         iCompactCounter = 0;
       
  2373         }
       
  2374 
       
  2375     IPTVLOGSTRING_HIGH_LEVEL("CIptvServiceManager::CompactDatabases() exit");
       
  2376     }
       
  2377 
       
  2378 // ---------------------------------------------------------------------------
       
  2379 // CIptvServiceManager::MaxDisplayOrderL
       
  2380 // ---------------------------------------------------------------------------
       
  2381 //
       
  2382 TUint32 CIptvServiceManager::MaxDisplayOrderL()
       
  2383     {
       
  2384     TUint32 maxDisplayOrder( 0 );
       
  2385 
       
  2386     // Query "SELECT DisplayOrder FROM Services ORDER BY DisplayOrder DESC" to
       
  2387     // find highest DisplayOrder for moving updated service to top of list
       
  2388     _LIT( KMaxDisplayOrderQueryFormat, "SELECT %S FROM %S ORDER BY %S DESC" );
       
  2389     HBufC* maxDisplayOrderQueryBuf = HBufC::NewLC(
       
  2390             KMaxDisplayOrderQueryFormat().Length()
       
  2391             + KIptvSmServicesDbDisplayOrderCol().Length()
       
  2392             + KIptvSmServicesTable().Length()
       
  2393             + KIptvSmServicesDbDisplayOrderCol().Length() );
       
  2394     maxDisplayOrderQueryBuf->Des().Format(
       
  2395             KMaxDisplayOrderQueryFormat,
       
  2396             &KIptvSmServicesDbDisplayOrderCol,
       
  2397             &KIptvSmServicesTable,
       
  2398             &KIptvSmServicesDbDisplayOrderCol );
       
  2399 
       
  2400     IPTVLOGSTRING2_HIGH_LEVEL("CIptvServiceManager::MaxDisplayOrder() query: %S", maxDisplayOrderQueryBuf);
       
  2401     
       
  2402     RDbView servicesView;
       
  2403     User::LeaveIfError( servicesView.Prepare(
       
  2404             iServicesDb,
       
  2405             TDbQuery( *maxDisplayOrderQueryBuf ) ) );
       
  2406     CleanupClosePushL( servicesView );
       
  2407     User::LeaveIfError( servicesView.EvaluateAll() );
       
  2408     
       
  2409     // First result has the highest DisplayOrder value
       
  2410     if ( servicesView.FirstL() )
       
  2411         {
       
  2412         servicesView.GetL();
       
  2413         maxDisplayOrder = servicesView.ColUint32( 1 );
       
  2414         IPTVLOGSTRING2_HIGH_LEVEL("CIptvServiceManager::MaxDisplayOrder() maxDisplayOrder: %d", maxDisplayOrder);
       
  2415         }
       
  2416 
       
  2417     CleanupStack::PopAndDestroy( &servicesView );
       
  2418     CleanupStack::PopAndDestroy( maxDisplayOrderQueryBuf );
       
  2419 
       
  2420     return maxDisplayOrder;
       
  2421     }