browserutilities/feedsengine/FeedsServer/Server/src/UpdateAllFeedsTask.cpp
changeset 0 dd21522fd290
child 25 0ed94ceaa377
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of 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:  A task to update all Feeds.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "FolderAttributes.h"
       
    20 #include "FeedsDatabase.h"
       
    21 #include "FeedsServer.h"
       
    22 #include "LeakTracker.h"
       
    23 #include "Logger.h"
       
    24 #include "PackedFeed.h"
       
    25 #include "PackedFolder.h"
       
    26 #include "UpdateAllFeedsTask.h"
       
    27 #include <SysUtil.h>
       
    28 
       
    29 
       
    30 // -----------------------------------------------------------------------------
       
    31 // CUpdateAllFeedsTask::NewL
       
    32 //
       
    33 // Two-phased constructor.
       
    34 // -----------------------------------------------------------------------------
       
    35 //
       
    36 CUpdateAllFeedsTask* CUpdateAllFeedsTask::NewL(CFeedsServer& aFeedsServer,
       
    37         TInt aFolderListId, MUpdateAllFeedsTaskObserver& aObserver)
       
    38     {
       
    39     CUpdateAllFeedsTask* self = new (ELeave) CUpdateAllFeedsTask(aFeedsServer, 
       
    40             aFolderListId, aObserver, EFalse);
       
    41     
       
    42     CleanupStack::PushL(self);
       
    43     self->ConstructL();
       
    44     CleanupStack::Pop();
       
    45 
       
    46     return self;
       
    47     }
       
    48     
       
    49         
       
    50 // -----------------------------------------------------------------------------
       
    51 // CUpdateAllFeedsTask::NewL
       
    52 //
       
    53 // Two-phased constructor.
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 CUpdateAllFeedsTask* CUpdateAllFeedsTask::NewL(CFeedsServer& aFeedsServer,
       
    57         TInt aFolderListId, const RArray<TInt>& aFeedIds, MUpdateAllFeedsTaskObserver& aObserver)
       
    58     {
       
    59     CUpdateAllFeedsTask* self = new (ELeave) CUpdateAllFeedsTask(aFeedsServer, 
       
    60             aFolderListId, aObserver, ETrue);
       
    61     
       
    62     CleanupStack::PushL(self);
       
    63     self->ConstructL();
       
    64     
       
    65     for (TInt i = 0; i < aFeedIds.Count(); i++)
       
    66         {
       
    67         // Add it to the list of entries to update (duplicate ids are ignored).
       
    68         (void) self->iFeedIds.InsertInOrder(aFeedIds[i]);
       
    69         }
       
    70         
       
    71     CleanupStack::Pop();
       
    72 
       
    73     return self;
       
    74     }
       
    75     
       
    76         
       
    77 // -----------------------------------------------------------------------------
       
    78 // CUpdateAllFeedsTask::CUpdateAllFeedsTask
       
    79 // C++ default constructor can NOT contain any code, that
       
    80 // might leave.
       
    81 // -----------------------------------------------------------------------------
       
    82 //
       
    83 CUpdateAllFeedsTask::CUpdateAllFeedsTask(CFeedsServer& aFeedsServer,
       
    84         TInt aFolderListId, MUpdateAllFeedsTaskObserver& aObserver, TBool aSelected):
       
    85         CTask(aFeedsServer), iLeakTracker(CLeakTracker::EUpdateAllFeedsTask), 
       
    86         iObserver(aObserver), iFolderListId(aFolderListId), iSelected(aSelected)
       
    87     {
       
    88     }
       
    89         
       
    90 
       
    91 // -----------------------------------------------------------------------------
       
    92 // CUpdateAllFeedsTask::ConstructL
       
    93 // Symbian 2nd phase constructor can leave.
       
    94 // -----------------------------------------------------------------------------
       
    95 //
       
    96 void CUpdateAllFeedsTask::ConstructL()
       
    97     {
       
    98     BaseConstructL(ETrue);
       
    99     iCheckDiskSpace = CCheckDiskSpace::NewL(this);
       
   100     iCheckDiskSpace->StartListening();
       
   101     }        
       
   102 
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // CUpdateAllFeedsTask::~CUpdateAllFeedsTask
       
   106 // Deconstructor.
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 CUpdateAllFeedsTask::~CUpdateAllFeedsTask()
       
   110     {
       
   111     iFeedIds.Close();
       
   112     delete iUpdateFeedTask;
       
   113     delete iCheckDiskSpace;
       
   114     iCheckDiskSpace = NULL;
       
   115     }
       
   116 
       
   117 
       
   118 // -----------------------------------------------------------------------------
       
   119 // CUpdateAllFeedsTask::StartTaskL.
       
   120 //
       
   121 // Starts the task.
       
   122 // -----------------------------------------------------------------------------
       
   123 //
       
   124 void CUpdateAllFeedsTask::StartTaskL()
       
   125     {
       
   126 
       
   127     TInt drive( EDriveC );
       
   128     TBool isSpace( EFalse );
       
   129     RFs                 rfs;
       
   130 
       
   131     User::LeaveIfError(rfs.Connect());
       
   132     CleanupClosePushL(rfs);
       
   133 
       
   134     TRAP_IGNORE( isSpace = !SysUtil::DiskSpaceBelowCriticalLevelL(
       
   135                                                 &rfs,
       
   136                                                 KMinFreebytes,
       
   137                                                 drive ));
       
   138 
       
   139     CleanupStack::PopAndDestroy();  //rfs
       
   140     // Abort the updation of feeds under low memory.
       
   141     if(!isSpace)
       
   142         {
       
   143         iObserver.UpdateAllFeedsCompleted(KErrNoMemory);
       
   144         return;
       
   145         }
       
   146 
       
   147     // Extract all of the ids if not selected update.
       
   148     if ( !iSelected )
       
   149         {
       
   150         if( iFeedIds.Count() != 0 )
       
   151             {
       
   152             FEED_LOG1(_L("Feeds"), _L("Feeds_Errors.log"), 
       
   153                 EFileLoggingModeAppend, _L("CUpdateAllFeedsTask::StartTaskL iFeedIds count is %d when updating all"), 
       
   154                 iFeedIds.Count());
       
   155             }
       
   156         iFeedsServer.Database().AllFeedIdsL( iFeedIds, iFolderListId );
       
   157         }
       
   158     // Otherwise just use the feed ids already in iFeedIds.
       
   159     
       
   160     // Start the first update.
       
   161     UpdateNextFeedL();
       
   162     }
       
   163 
       
   164 
       
   165 // -----------------------------------------------------------------------------
       
   166 // CUpdateAllFeedsTask::StartWait
       
   167 //
       
   168 // Notifies the observer that the UrlHandler is about to start a lengthy operation.
       
   169 // In response the observer could display somekind of status indicator.
       
   170 // -----------------------------------------------------------------------------
       
   171 //
       
   172 void CUpdateAllFeedsTask::StartWait()
       
   173     {
       
   174     //iObserver.StartWait();
       
   175     }
       
   176 
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // CUpdateAllFeedsTask::Completed
       
   180 //
       
   181 // Called upon completion of the task.
       
   182 // -----------------------------------------------------------------------------
       
   183 //
       
   184 void CUpdateAllFeedsTask::Completed(CPackedFeed* aPackedFeed, const TDesC& aFeedUrl,TInt aFeedId, 
       
   185         TInt aStatusCode)
       
   186     {
       
   187     TInt err;
       
   188     
       
   189     // Delete the task.
       
   190     iUpdateFeedTask->AutoDelete();
       
   191     iUpdateFeedTask = NULL;
       
   192     
       
   193     // A new packed feed is ready update the database.
       
   194     if (aPackedFeed != NULL)
       
   195         {
       
   196         // Update the database.
       
   197         TRAP_IGNORE(iFeedsServer.Database().UpdateFeedL(*aPackedFeed, aFeedUrl,aFeedId, iFolderListId));
       
   198         iFeedIds.Remove(iNextFeedIndex);
       
   199         delete aPackedFeed;
       
   200         }
       
   201     else
       
   202         {
       
   203         iFeedIds.Remove(iNextFeedIndex);
       
   204         }
       
   205     
       
   206     if(aStatusCode != KErrCancel && aStatusCode != KErrNoMemory)
       
   207     	{
       
   208         // Update the database.
       
   209         TRAP_IGNORE(iFeedsServer.Database().UpdateFeedStatusL(aFeedId,aStatusCode));
       
   210     	}
       
   211     // Stop updating if a fatal error ocurrs.
       
   212     // TODO: What other errors should stop this?
       
   213     if ((aStatusCode == KErrCancel) || (aStatusCode == KErrNoMemory))
       
   214         {
       
   215         iObserver.UpdateAllFeedsCompleted(aStatusCode);
       
   216         }
       
   217         
       
   218     // Otherwise start the next update.  Note: this is done even if the last 
       
   219     // update failed.
       
   220     else
       
   221         {        
       
   222         TRAP(err, UpdateNextFeedL());
       
   223         
       
   224         // Just stop updating if an error occurred.
       
   225         if (err != KErrNone)
       
   226             {
       
   227             iObserver.UpdateAllFeedsCompleted(err);
       
   228             }
       
   229         }
       
   230     }
       
   231     
       
   232     
       
   233 // -----------------------------------------------------------------------------
       
   234 // CUpdateAllFeedsTask::UpdateNextFeed
       
   235 //
       
   236 // Starts the update of the next feed.
       
   237 // -----------------------------------------------------------------------------
       
   238 //
       
   239 void CUpdateAllFeedsTask::UpdateNextFeedL()
       
   240     {
       
   241     TBool  done = EFalse;
       
   242     
       
   243     // TODO: Allow several feeds to be updated at the same time.
       
   244 
       
   245     while (!done)
       
   246         {        
       
   247         // Update the next feed.
       
   248         if (iNextFeedIndex < iFeedIds.Count())
       
   249             {
       
   250             HBufC*  feedUrl = NULL;
       
   251             
       
   252             if(iFeedIds[iNextFeedIndex] > 0)
       
   253                 {
       
   254                 // Get feed's url from the datbase.
       
   255                 (void) iFeedsServer.Database().UrlFromFeedIdL(
       
   256                         iFeedIds[iNextFeedIndex], feedUrl);
       
   257                 CleanupStack::PushL(feedUrl);
       
   258                 
       
   259                 // Create a new task to update the feed.
       
   260                 iUpdateFeedTask = CUpdateFeedTask::NewL(iFeedsServer, 
       
   261                         *feedUrl,iFeedIds[iNextFeedIndex], *this);        
       
   262                 TRAPD(err, iUpdateFeedTask->StartTaskL());
       
   263                 
       
   264                 // Done if the task was started.
       
   265                 if (err == KErrNone)
       
   266                     {
       
   267                     done = ETrue;
       
   268                     }
       
   269                     
       
   270                 // Otherwise the task wasn't started so delete it.
       
   271                 else
       
   272                     {
       
   273                     if(err != KErrCancel && err != KErrNoMemory)
       
   274     	                {
       
   275     		            iFeedsServer.Database().UpdateFeedStatusL(iFeedIds[iNextFeedIndex],err);
       
   276             	        }
       
   277                     delete iUpdateFeedTask;
       
   278                     iUpdateFeedTask = NULL;
       
   279                     }
       
   280                 
       
   281                 CleanupStack::PopAndDestroy(feedUrl);
       
   282                 }
       
   283             }
       
   284 
       
   285         // Otherwise notify the observer that there are no more feeds to update.
       
   286         else
       
   287             {
       
   288 #if defined(_DEBUG)
       
   289             iFeedsServer.Database().DebugPrintItemTableL();
       
   290 #endif
       
   291             iObserver.UpdateAllFeedsCompleted(KErrNone);
       
   292             done = ETrue;
       
   293             }
       
   294         }
       
   295     }
       
   296 // -----------------------------------------------------------------------------
       
   297 // CUpdateAllFeedsTask::AddFeedL
       
   298 //
       
   299 // return integer
       
   300 // -----------------------------------------------------------------------------
       
   301 //
       
   302 TInt CUpdateAllFeedsTask::AddFeedL( RArray<TInt> aFeedIds)
       
   303     {
       
   304     TBool taskRunning = EFalse;
       
   305 
       
   306     if(iFeedIds.Count())
       
   307         {
       
   308         taskRunning = ETrue;    
       
   309         }   
       
   310 
       
   311     for(TInt i=0 ; i< aFeedIds.Count(); i++)
       
   312         {
       
   313         if( iFeedIds.Find(aFeedIds[i]) < 0 )
       
   314             {
       
   315             iFeedIds.Append(aFeedIds[i]);
       
   316             }
       
   317         }
       
   318     if(!taskRunning)
       
   319         {
       
   320         iNextFeedIndex = 0;
       
   321         UpdateNextFeedL();
       
   322         }
       
   323 
       
   324     return KErrNone;
       
   325     }
       
   326 // -----------------------------------------------------------------------------
       
   327 // CUpdateAllFeedsTask::HttpConnection
       
   328 //
       
   329 // return connection
       
   330 // -----------------------------------------------------------------------------
       
   331 //
       
   332 CHttpConnection& CUpdateAllFeedsTask::HttpConnection()
       
   333     {
       
   334     return iObserver.HttpConnection();
       
   335     }
       
   336 
       
   337 // -----------------------------------------------------------------------------
       
   338 // CUpdateAllFeedsTask::UpdateAllFeedsCompleted
       
   339 //
       
   340 // return connection
       
   341 // -----------------------------------------------------------------------------
       
   342 //
       
   343 void CUpdateAllFeedsTask::UpdateAllFeedsCompleted(TInt aStatusCode)
       
   344     {
       
   345     iObserver.UpdateAllFeedsCompleted(aStatusCode);
       
   346     }
       
   347 
       
   348 // -----------------------------------------------------------------------------
       
   349 // CCheckDiskSpace::NewL
       
   350 //
       
   351 // Two-phased constructor.
       
   352 // -----------------------------------------------------------------------------
       
   353 CCheckDiskSpace* CCheckDiskSpace::NewL(CUpdateAllFeedsTask* aUpdateAllFeedsTask)
       
   354     {
       
   355     CCheckDiskSpace* self = new (ELeave) CCheckDiskSpace();
       
   356     CleanupStack::PushL(self);
       
   357     self->ConstructL(aUpdateAllFeedsTask);
       
   358     CleanupStack::Pop();
       
   359     return self;
       
   360     }
       
   361 
       
   362 // -----------------------------------------------------------------------------
       
   363 // CCheckDiskSpace::~CCheckDiskSpace
       
   364 //
       
   365 // Destructor
       
   366 // -----------------------------------------------------------------------------
       
   367 CCheckDiskSpace::~CCheckDiskSpace()
       
   368     {
       
   369     if(iIsListening)
       
   370         {
       
   371         Cancel();
       
   372         iIsListening = EFalse;
       
   373         }
       
   374     iRfs.Close();
       
   375     }
       
   376 
       
   377 // -----------------------------------------------------------------------------
       
   378 // CCheckDiskSpace::ConstructL
       
   379 //
       
   380 // Symbian 2nd phase constructor can leave.
       
   381 // -----------------------------------------------------------------------------
       
   382 void CCheckDiskSpace::ConstructL(CUpdateAllFeedsTask* aUpdateAllFeedsTask)
       
   383     {
       
   384     CActiveScheduler::Add(this);
       
   385     iUpdateAllFeedsTask = aUpdateAllFeedsTask;  
       
   386     User::LeaveIfError(iRfs.Connect());
       
   387     }
       
   388 
       
   389 // -----------------------------------------------------------------------------
       
   390 // CCheckDiskSpace::CCheckDiskSpace
       
   391 //
       
   392 //C++ default constructor can NOT contain any code that
       
   393 // might leave.
       
   394 // -----------------------------------------------------------------------------
       
   395 CCheckDiskSpace::CCheckDiskSpace():CActive(EPriorityStandard ), iIsListening(EFalse)
       
   396     {
       
   397     }
       
   398 
       
   399 // -----------------------------------------------------------------------------
       
   400 // CCheckDiskSpace::RunL
       
   401 //
       
   402 // To Receive event from Subscribe and publish server and
       
   403 // based on the C: drive memory. 
       
   404 // -----------------------------------------------------------------------------
       
   405 void CCheckDiskSpace::RunL()
       
   406     {
       
   407     if(iStatus == KErrNone)
       
   408         {
       
   409         TInt drive( EDriveC );
       
   410         TBool isSpace( EFalse );
       
   411 
       
   412         TRAP_IGNORE( isSpace = !SysUtil::DiskSpaceBelowCriticalLevelL(
       
   413                                                     &iRfs,
       
   414                                                    KMinFreebytes,
       
   415                                                    drive ));
       
   416         if(!isSpace)
       
   417             {
       
   418             iUpdateAllFeedsTask->UpdateAllFeedsCompleted(KErrNoMemory);
       
   419             }
       
   420         else
       
   421             {
       
   422             StartListening();
       
   423             }    
       
   424         }
       
   425     else
       
   426         {
       
   427         iIsListening = EFalse;
       
   428         }
       
   429     }
       
   430 
       
   431 // -----------------------------------------------------------------------------
       
   432 // CCheckDiskSpace::RunError
       
   433 // 
       
   434 // -----------------------------------------------------------------------------
       
   435 void CCheckDiskSpace::RunError()
       
   436     {
       
   437     }
       
   438 
       
   439 // -----------------------------------------------------------------------------
       
   440 // CCheckDiskSpace::DoCancel
       
   441 // 
       
   442 // -----------------------------------------------------------------------------
       
   443 void CCheckDiskSpace::DoCancel()
       
   444     {
       
   445     iRfs.NotifyDiskSpaceCancel(iStatus);
       
   446     
       
   447     }
       
   448 
       
   449 // -----------------------------------------------------------------------------
       
   450 // CCheckDiskSpace::StartListening
       
   451 //
       
   452 // This function subscribe to receive events on C: drive memory activites.
       
   453 // -----------------------------------------------------------------------------
       
   454 void CCheckDiskSpace::StartListening()
       
   455     {
       
   456 
       
   457     if(!IsActive())
       
   458         {
       
   459         TInt drive( EDriveC );
       
   460 
       
   461         iRfs.NotifyDiskSpace( KMinFreebytes, drive, iStatus);
       
   462         iIsListening = ETrue;
       
   463 
       
   464         SetActive();
       
   465         }
       
   466     }