browserutilities/feedsengine/FeedsServer/Server/src/UpdateManager.cpp
changeset 0 dd21522fd290
child 10 a359256acfc6
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2008 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:  UpdateManager it controls the updation of feed.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "UpdateManager.h"
       
    20 #include "UpdateQueue.h"
       
    21 #include "FeedsServer.h"
       
    22 #include "FeedsDatabase.h"
       
    23 #include "ServerHttpConnection.h"
       
    24 
       
    25 const TInt KWmlSettingsAutomaticUpdatingNotSet = 32767;
       
    26 const TInt KMinFrequency = 15; // IN MINS
       
    27 
       
    28 // -----------------------------------------------------------------------------
       
    29 // CUpdateManager::NewL
       
    30 //
       
    31 // Two-phased constructor.
       
    32 // -----------------------------------------------------------------------------
       
    33 CUpdateManager* CUpdateManager::NewL( TInt aFolderListId, TUint32 aAutoUpdateAP, TInt aAutoUpdateFreq, 
       
    34                 	                            TBool aAutoUpdateWhileRoaming, CFeedsServer& aFeedsServer)                                        
       
    35     {
       
    36     CUpdateManager* self = new (ELeave) CUpdateManager( aFolderListId, aAutoUpdateFreq, aAutoUpdateWhileRoaming, aFeedsServer );
       
    37 
       
    38     CleanupStack::PushL(self);
       
    39     self->ConstructL( aAutoUpdateAP );
       
    40     CleanupStack::Pop();
       
    41 
       
    42     return self;
       
    43     }
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CUpdateManager::ConstructL
       
    47 //
       
    48 // Symbian 2nd phase constructor can leave.
       
    49 // -----------------------------------------------------------------------------	
       
    50 void CUpdateManager::ConstructL( TUint32 aAutoUpdateAP)
       
    51     {
       
    52     CTimer::ConstructL();
       
    53     CActiveScheduler::Add(this);
       
    54     iUpdateAllFeedTask = CUpdateAllFeedsTask::NewL( iFeedsServer, iFolderListId , *this ); 
       
    55     iLazyCaller = CIdle::NewL(CActive::EPriorityIdle );
       
    56     iHttpConnection = CServerHttpConnection::NewL( aAutoUpdateAP );
       
    57     iRoamingInfo = CRoamingInfo::NewL(this);
       
    58     iAutoUpdateAp = aAutoUpdateAP;
       
    59     }
       
    60 
       
    61  // -----------------------------------------------------------------------------
       
    62 // CUpdateManager::CUpdateManager
       
    63 //
       
    64 // constructor.
       
    65 // -----------------------------------------------------------------------------
       
    66  CUpdateManager::CUpdateManager( TInt aFolderListId, TInt aAutoUpdateFreq, TBool aAutoUpdateWhileRoaming, CFeedsServer& aFeedsServer ):
       
    67         CTimer(EPriorityIdle), iFeedsServer(aFeedsServer), iUpdateAllFeedTask(NULL)
       
    68     {
       
    69     iFolderListId = aFolderListId;
       
    70     iAutoUpdateFreq = aAutoUpdateFreq; 
       
    71     iAutoUpdateWhileRoaming = aAutoUpdateWhileRoaming;
       
    72     }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // CUpdateManager::~CUpdateManager
       
    76 //
       
    77 // Deconstructor.
       
    78 // ----------------------------------------------------------------------------- 
       
    79 CUpdateManager::~CUpdateManager()
       
    80     {
       
    81     Stop();
       
    82     for( int i=0; i<iQueueArray.Count(); i++ )
       
    83         {
       
    84         delete iQueueArray[ i ];
       
    85         }
       
    86     if( iUpdateAllFeedTask == NULL )
       
    87         {
       
    88         delete iUpdateAllFeedTask;
       
    89         iUpdateAllFeedTask = NULL;    
       
    90         }
       
    91     delete iLazyCaller;
       
    92     delete iHttpConnection;
       
    93     delete iRoamingInfo;
       
    94     iHttpConnection = NULL;
       
    95     iRoamingInfo = NULL;
       
    96    	Deque();
       
    97     }
       
    98 
       
    99 // -----------------------------------------------------------------------------
       
   100 // CUpdateManager::Stop
       
   101 //
       
   102 // Handles the stoping of AutoUpdate. 
       
   103 // -----------------------------------------------------------------------------     
       
   104 void CUpdateManager::Stop()
       
   105     {
       
   106     if(IsActive())
       
   107         {
       
   108         Cancel();
       
   109         }
       
   110 
       
   111     if(iLazyCaller->IsActive())
       
   112         {
       
   113         iLazyCaller->Cancel();
       
   114         }
       
   115     }
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // CUpdateManager::RunL
       
   119 //
       
   120 // Handles an active object's timer event.
       
   121 // -----------------------------------------------------------------------------  
       
   122 void CUpdateManager::RunL()
       
   123     {
       
   124     if (iStatus.Int() == KErrNone || iStatus.Int() == KErrAbort)
       
   125         {      
       
   126         StartTimer();
       
   127         if(iAutoUpdateWhileRoaming)
       
   128             {
       
   129             UpdateL();
       
   130             }
       
   131         else
       
   132             {
       
   133             iRoamingInfo->CheckForRoaming();
       
   134             }
       
   135         }      
       
   136     }
       
   137 
       
   138 // -----------------------------------------------------------------------------
       
   139 // CUpdateManager::StartL
       
   140 // 
       
   141 // Handles the Update of feed
       
   142 // -----------------------------------------------------------------------------     
       
   143 TInt CUpdateManager::StartL()
       
   144     {
       
   145     iCurrentFeedCount = 0;
       
   146 
       
   147     iFeedsServer.Database().AllFeedIdsL( iFeedIds, iFolderListId );
       
   148     iLazyCaller->Start(TCallBack(CUpdateManager::LazyCallBack,this));
       
   149     }
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 // CUpdateManager::UpdateAllFeedsProgress
       
   153 // 
       
   154 // Called to notify the obsever how many feeds remain to be updated
       
   155 // -----------------------------------------------------------------------------      
       
   156 void CUpdateManager::UpdateAllFeedsProgress(TInt /*aMaxCount*/, TInt /*aRemaining*/)
       
   157     {
       
   158      // Ignore.
       
   159     }
       
   160 
       
   161 // -----------------------------------------------------------------------------
       
   162 // CUpdateManager::UpdateAllFeedsCompleted
       
   163 // 
       
   164 // Called upon completion of the task.
       
   165 // -----------------------------------------------------------------------------      
       
   166 void CUpdateManager::UpdateAllFeedsCompleted(TInt aStatusCode)
       
   167     {
       
   168     // S60 bug fix <RFON-6QKRV7>: Disconnect after done with auto update
       
   169     iHttpConnection->Disconnect();
       
   170     if (aStatusCode == KErrNone)
       
   171         {
       
   172         iFeedsServer.NotifyFolderListChanged(iFolderListId);
       
   173         }    
       
   174     }
       
   175 
       
   176 // -----------------------------------------------------------------------------
       
   177 // CUpdateManager::HttpConnection
       
   178 // 
       
   179 // Returns the http-connection instance
       
   180 // -----------------------------------------------------------------------------      
       
   181 CHttpConnection& CUpdateManager::HttpConnection()
       
   182     {
       
   183     return (CHttpConnection&) *iHttpConnection;
       
   184     }
       
   185 
       
   186 // -----------------------------------------------------------------------------
       
   187 // CUpdateManager::FolderListId
       
   188 //
       
   189 // Returns the folder list ID.
       
   190 // -----------------------------------------------------------------------------
       
   191 //
       
   192 TInt CUpdateManager::FolderListId() 
       
   193     {
       
   194     return iFolderListId;
       
   195     }
       
   196 
       
   197 // -----------------------------------------------------------------------------
       
   198 // CUpdateManager::Update
       
   199 //
       
   200 // Starts Update
       
   201 // -----------------------------------------------------------------------------
       
   202 //
       
   203  TInt CUpdateManager::UpdateL()
       
   204     {
       
   205     for(TInt i =0 ; i < iQueueArray.Count(); i++)
       
   206         {
       
   207         iQueueArray[i]->UpdateL(iMins);    
       
   208         }    
       
   209     return KErrNone;
       
   210     }
       
   211 
       
   212 // -----------------------------------------------------------------------------
       
   213 // CUpdateManager::StartTimer
       
   214 //
       
   215 // Start timer.
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 void CUpdateManager::StartTimer()
       
   219     {
       
   220     TTime  time;
       
   221     time.HomeTime();
       
   222     TDateTime dateTime = time.DateTime();
       
   223     iMins = dateTime.Hour() * 60 + dateTime.Minute();
       
   224     TTimeIntervalMinutes interval(KMinFrequency - iMins % KMinFrequency);
       
   225     time += interval;
       
   226 
       
   227     At(time);
       
   228     }
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // CUpdateManager::UpdateFeedL
       
   232 //
       
   233 // Update Feed in queue.
       
   234 // -----------------------------------------------------------------------------
       
   235 //    
       
   236 void CUpdateManager::UpdateFeedL(TInt aFeedId, TBool aDeleteFeed)
       
   237     {
       
   238     for(TInt k=0 ; k < iQueueArray.Count(); k++)
       
   239         {
       
   240         iQueueArray[k]->RemoveFeed(aFeedId);
       
   241         if(iQueueArray[k]->Count() <= 0)
       
   242             {
       
   243             delete iQueueArray[k];
       
   244             iQueueArray.Remove(k);
       
   245             }
       
   246         }
       
   247     if(!aDeleteFeed)
       
   248         {
       
   249         if(iLazyCaller->IsActive())
       
   250             {
       
   251             iFeedIds.Append(aFeedId);
       
   252             }
       
   253         else
       
   254             {
       
   255             AddFeedL(aFeedId);
       
   256             }
       
   257         }
       
   258     }
       
   259 
       
   260 // -----------------------------------------------------------------------------
       
   261 // CUpdateManager::LazyCallBack
       
   262 //
       
   263 // 
       
   264 // -----------------------------------------------------------------------------
       
   265 //
       
   266 TBool CUpdateManager::LazyCallBack(TAny* aPtr)
       
   267     {
       
   268     CUpdateManager* ptr = (CUpdateManager*)aPtr;
       
   269     for(TInt i = 0; i < 10; i++)
       
   270         {
       
   271         if(ptr->iCurrentFeedCount< ptr->iFeedIds.Count())
       
   272             {
       
   273             TRAP_IGNORE(ptr->AddFeedL(ptr->iFeedIds[ptr->iCurrentFeedCount++]));
       
   274             }
       
   275         else
       
   276             {
       
   277             ptr->iCurrentFeedCount = 0;
       
   278             ptr->iFeedIds.Reset();
       
   279             ptr->StartTimer();
       
   280             return EFalse;
       
   281             }    
       
   282         }
       
   283     return ETrue;
       
   284     }
       
   285 
       
   286 // -----------------------------------------------------------------------------
       
   287 // CUpdateManager::AddFeedL
       
   288 //
       
   289 // Add Feed IDs in queue.
       
   290 // -----------------------------------------------------------------------------
       
   291 //  
       
   292 void CUpdateManager::AddFeedL(TInt aFeedId)
       
   293     {
       
   294     TInt freq;
       
   295     iFeedsServer.Database().FreqFromFeedIdL(aFeedId,freq);
       
   296     freq = iAutoUpdateFreq == KWmlSettingsAutomaticUpdatingNotSet ? freq : iAutoUpdateFreq;
       
   297     if(freq > 0)
       
   298         {
       
   299         TBool found = EFalse;
       
   300         for(TInt k=0 ; k < iQueueArray.Count(); k++)
       
   301             {
       
   302             if(freq == iQueueArray[k]->GetFreq())
       
   303                 {
       
   304                 iQueueArray[k]->AddFeed(aFeedId);
       
   305                 found = ETrue;
       
   306                 }
       
   307             }
       
   308         if(!found)
       
   309             {
       
   310             CUpdateQueue* updateQueue = CUpdateQueue::NewL( iUpdateAllFeedTask, freq); 
       
   311             iQueueArray.Append( updateQueue);
       
   312             updateQueue->AddFeed( aFeedId);
       
   313             }
       
   314         }
       
   315     }
       
   316 
       
   317 // -----------------------------------------------------------------------------
       
   318 // CRoamingInfo::NewL
       
   319 // Two-phased constructor.
       
   320 // -----------------------------------------------------------------------------
       
   321 //
       
   322 CRoamingInfo* CRoamingInfo::NewL(CUpdateManager *aBackgroundUpdater)                                        
       
   323     {
       
   324     CRoamingInfo* self = new (ELeave) CRoamingInfo(aBackgroundUpdater);
       
   325 
       
   326     CleanupStack::PushL(self);
       
   327     self->ConstructL();
       
   328     CleanupStack::Pop();
       
   329 
       
   330     return self;
       
   331     }
       
   332 
       
   333 // -----------------------------------------------------------------------------
       
   334 // CRoamingInfo::ConstructL
       
   335 //
       
   336 // Symbian 2nd phase constructor can leave.
       
   337 // -----------------------------------------------------------------------------
       
   338 //
       
   339 void CRoamingInfo::ConstructL()
       
   340     {
       
   341     CActiveScheduler::Add(this);
       
   342     iTelephony = CTelephony::NewL();
       
   343     }
       
   344 
       
   345 // -----------------------------------------------------------------------------
       
   346 // CRoamingInfo::CRoamingInfo
       
   347 //
       
   348 // C++ default constructor can NOT contain any code that
       
   349 // might leave.
       
   350 // -----------------------------------------------------------------------------
       
   351 //
       
   352 CRoamingInfo::CRoamingInfo(CUpdateManager *aUpdateManager)
       
   353 	:CActive(CActive::EPriorityStandard),iUpdateManager(aUpdateManager)
       
   354     {
       
   355     }
       
   356 
       
   357 // -----------------------------------------------------------------------------
       
   358 // CRoamingInfo::~CRoamingInfo
       
   359 //
       
   360 // Destructor
       
   361 // -----------------------------------------------------------------------------
       
   362 //
       
   363 CRoamingInfo::~CRoamingInfo()
       
   364     {
       
   365     Cancel();
       
   366     delete iTelephony;
       
   367     iTelephony = NULL;
       
   368     }
       
   369 
       
   370 // -----------------------------------------------------------------------------
       
   371 // CRoamingInfo::CheckForRoaming
       
   372 // 
       
   373 // Check for roaming.
       
   374 // -----------------------------------------------------------------------------
       
   375 void CRoamingInfo::CheckForRoaming()
       
   376     {
       
   377 
       
   378 #ifndef __WINSCW__
       
   379 
       
   380     CTelephony::TNetworkRegistrationV1Pckg RegStatusPkg(iRegStatus);
       
   381     iTelephony->GetNetworkRegistrationStatus(iStatus, RegStatusPkg);
       
   382     SetActive();
       
   383 
       
   384 #else
       
   385     TRAP_IGNORE( iUpdateManager->UpdateL() );
       
   386 #endif
       
   387     }
       
   388 
       
   389 // -----------------------------------------------------------------------------
       
   390 // CRoamingInfo::RunL
       
   391 //
       
   392 // Handles an active object's request completion event.
       
   393 // -----------------------------------------------------------------------------
       
   394 //
       
   395 void CRoamingInfo::RunL()
       
   396     {  
       
   397     if (iStatus.Int() == KErrNone)
       
   398         {
       
   399         if(iRegStatus.iRegStatus != CTelephony::ERegisteredRoaming)
       
   400         	{
       
   401         	iUpdateManager->UpdateL();
       
   402         	}
       
   403 		}
       
   404     }
       
   405 
       
   406 // -----------------------------------------------------------------------------
       
   407 // CRoamingInfo::DoCancel
       
   408 //
       
   409 // Implements cancellation of an outstanding request.
       
   410 // -----------------------------------------------------------------------------
       
   411 //
       
   412 void CRoamingInfo::DoCancel()
       
   413     {
       
   414     iTelephony->CancelAsync(CTelephony::EGetCurrentNetworkInfoCancel );
       
   415     }