menucontentsrv/srvsrc/mcsrunningappshandler.cpp
changeset 0 79c6a41cd166
equal deleted inserted replaced
-1:000000000000 0:79c6a41cd166
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 #include <mcsdef.h>
       
    18 #include "mcsmenuitem.h"
       
    19 #include "mcsrunningappshandler.h"
       
    20 #include "mcsrunningappswgmonitor.h"
       
    21 #include "mcsrunningappsfswmonitor.h"
       
    22 #include "mcsrunningappshiddenattrscanner.h"
       
    23 #include "mcsrunningappsaddremovehandler.h"
       
    24 #include "mcsmenunotifier.h"
       
    25 #include "mcscachehandler.h"
       
    26 
       
    27 
       
    28 // ---------------------------------------------------------
       
    29 // CMcsRunningAppsHandler::NewL
       
    30 // ---------------------------------------------------------
       
    31 //
       
    32 CMcsRunningAppsHandler* CMcsRunningAppsHandler::NewL(
       
    33     CMenuEng& aEng, CMcsCacheHandler& aCacheHandler )
       
    34 	{
       
    35 	CMcsRunningAppsHandler* self;
       
    36 	self = new ( ELeave ) CMcsRunningAppsHandler( aEng, aCacheHandler );
       
    37 	CleanupStack::PushL( self );
       
    38 	self->ConstructL();
       
    39 	CleanupStack::Pop( self );
       
    40 	return self;
       
    41 	}
       
    42 
       
    43 // ---------------------------------------------------------
       
    44 // CMcsRunningAppsHandler::~CMcsRunningAppsHandler
       
    45 // ---------------------------------------------------------
       
    46 //
       
    47 CMcsRunningAppsHandler::~CMcsRunningAppsHandler()
       
    48     {
       
    49     delete iWindowGroupMonitor;
       
    50     delete iFswMonitor;
       
    51     delete iHiddenAttrScanner;
       
    52     delete iAddRemoveHandler;
       
    53     iRunningApps.Close();
       
    54     iRunningFolderStatus.Close();
       
    55     }
       
    56 
       
    57 // ---------------------------------------------------------
       
    58 // CMcsRunningAppsHandler::CMcsRunningAppsHandler
       
    59 // ---------------------------------------------------------
       
    60 //
       
    61 CMcsRunningAppsHandler::CMcsRunningAppsHandler(
       
    62 	CMenuEng& aEng, CMcsCacheHandler& aCacheHandler ):
       
    63 	iEng(aEng), 
       
    64 	iCacheHandler(aCacheHandler)
       
    65     {
       
    66     }
       
    67 
       
    68 // ---------------------------------------------------------
       
    69 // CMcsRunningAppsHandler::ConstructL
       
    70 // ---------------------------------------------------------
       
    71 //
       
    72 void CMcsRunningAppsHandler::ConstructL()
       
    73 	{
       
    74 	//iFswMonitor and iHiddenAttrScanner must be initialized before
       
    75 	//iWindowGroupMonitor, because they update lists which are used by
       
    76 	//iWindowGroupMonitor
       
    77 	iFswMonitor = CMcsRunningAppsFswMonitor::NewL( iEng, this );
       
    78     iHiddenAttrScanner = CMcsRunningAppsHiddenAttrScanner::NewL( iEng, this );
       
    79     iWindowGroupMonitor = CMcsRunningAppsWgMonitor::NewL( iEng, this );
       
    80     iAddRemoveHandler = CMcsRunningAppsAddRemoveHandler::NewL( iEng, this );
       
    81     UpdateFoldersRunningStatusL();
       
    82     
       
    83 	}
       
    84 
       
    85 // ---------------------------------------------------------
       
    86 // CMcsRunningAppsHandler::EngineEventsL
       
    87 // ---------------------------------------------------------
       
    88 //
       
    89 void CMcsRunningAppsHandler::EngineEventsL( TInt aFolder, TInt aEvents )
       
    90 	{
       
    91     if ( aEvents & RMenuNotifier::EItemsAddedRemoved )
       
    92         {
       
    93         iAddRemoveHandler->HandleAddRemoveL( aFolder );
       
    94         iHiddenAttrScanner->ScanL();
       
    95         }    
       
    96 	}
       
    97 
       
    98 // ---------------------------------------------------------
       
    99 // CMcsRunningAppsHandler::EngineEvents
       
   100 // ---------------------------------------------------------
       
   101 //
       
   102 void CMcsRunningAppsHandler::EngineEvents( TInt aFolder, TInt aEvents )
       
   103 	{
       
   104 	TRAP_IGNORE( EngineEventsL( aFolder, aEvents ) );      
       
   105 	}
       
   106 
       
   107 
       
   108 // ---------------------------------------------------------
       
   109 // CMcsRunningAppsHandler::EngineEvents
       
   110 // ---------------------------------------------------------
       
   111 //
       
   112 void CMcsRunningAppsHandler::GetRunningAppsL( RArray<TUid>& aArray )
       
   113 	{
       
   114 	for ( TInt i = 0; i< iRunningApps.Count(); i++ )
       
   115 		{
       
   116 		if ( iRunningApps[i].IsRunning() &&
       
   117 				!iRunningApps[i].IsHidden() &&
       
   118 				!iRunningApps[i].IsFswHidden() )
       
   119 			{
       
   120 			aArray.AppendL( iRunningApps[i].GetUid() );
       
   121 			}
       
   122 		}
       
   123 	}
       
   124 
       
   125 // ---------------------------------------------------------
       
   126 // CMcsRunningAppsHandler::HandleListUpdateL
       
   127 // ---------------------------------------------------------
       
   128 //
       
   129 void CMcsRunningAppsHandler::HandleListUpdateL(const RArray<TUid>& aArray, 
       
   130 		TArrayDataType aType )
       
   131 	{
       
   132 	switch ( aType )
       
   133 		{
       
   134 		case ERunningApps:
       
   135 			HandleRunningAppsChangedL( aArray );
       
   136 			break;
       
   137 		case EFswHiddenApps:
       
   138 			HandleFswHiddenAppsChangedL( aArray );
       
   139 			break;
       
   140 		case EHiddenApps:
       
   141 			HandleHiddenAppsChangedL( aArray );
       
   142 			break;
       
   143 		}
       
   144 	}
       
   145 
       
   146 // ---------------------------------------------------------
       
   147 // CMcsRunningAppsHandler::HandleRunningAppsChangedL
       
   148 // ---------------------------------------------------------
       
   149 //
       
   150 void CMcsRunningAppsHandler::HandleRunningAppsChangedL(
       
   151 		const RArray<TUid>& aArray )
       
   152 	{
       
   153 	TInt count = iRunningApps.Count();
       
   154 	//add new items to iRunningApps
       
   155 	for ( TInt i = 0; i < aArray.Count(); i++ )
       
   156 		{
       
   157 		TRunningAppsAttr item( aArray[i] );
       
   158 		TInt id = iRunningApps.Find( item, TRunningAppsAttr::MatchByUid );
       
   159 		if (KErrNotFound == id)
       
   160 			{
       
   161 			item.SetRunning( ETrue );
       
   162 			item.SetFswHidden( iFswMonitor->IsFswHidden( aArray[i] ) );
       
   163 			item.SetHidden( iHiddenAttrScanner->IsHidden( aArray[i] ) );
       
   164 			iRunningApps.AppendL( item );
       
   165 			if ( !item.IsFswHidden() && !item.IsHidden() )
       
   166 				{
       
   167 				HandleNotificationL( aArray[i] );
       
   168 				}
       
   169 			}
       
   170 		}
       
   171 	//set proper running attribute value in iRunningApps 
       
   172 	//(only for applications that were previously in Array)
       
   173 	for ( TInt i = 0; i< count; i++ )
       
   174 		{
       
   175 		TUid appUid ( iRunningApps[i].GetUid() );
       
   176 		
       
   177 		TInt id = aArray.Find( appUid );
       
   178 		if ( ( KErrNotFound == id ) && ( iRunningApps[i].IsRunning() ) )
       
   179 			{
       
   180 			iRunningApps[i].SetRunning( EFalse );
       
   181 			if ( !iRunningApps[i].IsFswHidden() && 
       
   182 					!iRunningApps[i].IsHidden() )
       
   183 				{
       
   184 				HandleNotificationL( appUid );
       
   185 				}
       
   186 			}
       
   187 		else if ( ( KErrNotFound != id ) && ( !iRunningApps[i].IsRunning() ) )
       
   188 			{
       
   189 			iRunningApps[i].SetRunning( ETrue );
       
   190 			if ( !iRunningApps[i].IsFswHidden() && 
       
   191 					!iRunningApps[i].IsHidden() )
       
   192 				{
       
   193 				HandleNotificationL( appUid );
       
   194 				}
       
   195 			}
       
   196 		}
       
   197 	}
       
   198 
       
   199 // ---------------------------------------------------------
       
   200 // CMcsRunningAppsHandler::HandleFswHiddenAppsChangedL
       
   201 // ---------------------------------------------------------
       
   202 //
       
   203 void CMcsRunningAppsHandler::HandleFswHiddenAppsChangedL(
       
   204 		const RArray<TUid>& aArray )
       
   205 	{
       
   206 	//set proper FSW hidden value in iRunningApps 
       
   207 	for ( TInt i = 0; i< iRunningApps.Count(); i++ )
       
   208 		{
       
   209 		TUid appUid ( iRunningApps[i].GetUid() );
       
   210 		TInt id = aArray.Find( appUid );
       
   211 		if ( ( KErrNotFound == id ) && ( iRunningApps[i].IsFswHidden() ) )
       
   212 			{
       
   213 			iRunningApps[i].SetFswHidden( EFalse );
       
   214 			if ( iRunningApps[i].IsRunning() && 
       
   215 					!iRunningApps[i].IsHidden() )
       
   216 				{
       
   217 				HandleNotificationL( appUid );
       
   218 				}
       
   219 			}
       
   220 		else if ( ( KErrNotFound != id ) && ( !iRunningApps[i].IsFswHidden() ) )
       
   221 			{
       
   222 			iRunningApps[i].SetFswHidden( ETrue );
       
   223 			if ( iRunningApps[i].IsRunning() && 
       
   224 					!iRunningApps[i].IsHidden() )
       
   225 				{
       
   226 				HandleNotificationL( appUid );
       
   227 				}
       
   228 			}
       
   229 		}	
       
   230 	}
       
   231 
       
   232 // ---------------------------------------------------------
       
   233 // CMcsRunningAppsHandler::HandleHiddenAppsChangedL
       
   234 // ---------------------------------------------------------
       
   235 //
       
   236 void CMcsRunningAppsHandler::HandleHiddenAppsChangedL(
       
   237 		const RArray<TUid>& aArray )
       
   238 	{
       
   239 	//set proper hidden value in iRunningApps 
       
   240 	for ( TInt i = 0; i< iRunningApps.Count(); i++ )
       
   241 		{
       
   242 		TUid appUid ( iRunningApps[i].GetUid() );
       
   243 		TInt id = aArray.Find( appUid );
       
   244 		if ( ( KErrNotFound == id ) && ( iRunningApps[i].IsHidden() ) )
       
   245 			{
       
   246 			iRunningApps[i].SetHidden( EFalse );
       
   247 			if ( iRunningApps[i].IsRunning() && 
       
   248 					!iRunningApps[i].IsFswHidden() )
       
   249 				{
       
   250 				HandleNotificationL( appUid );
       
   251 				}
       
   252 			}
       
   253 		else if ( ( KErrNotFound != id ) && ( !iRunningApps[i].IsHidden() ) )
       
   254 			{
       
   255 			iRunningApps[i].SetHidden( ETrue );
       
   256 			if ( iRunningApps[i].IsRunning() && 
       
   257 					!iRunningApps[i].IsFswHidden() )
       
   258 				{
       
   259 				HandleNotificationL( appUid );
       
   260 				}			
       
   261 			}
       
   262 		}		
       
   263 	}
       
   264 
       
   265 
       
   266 
       
   267 // ---------------------------------------------------------
       
   268 // CMcsRunningAppsHandler::HandleNotificationL
       
   269 // ---------------------------------------------------------
       
   270 //
       
   271 void CMcsRunningAppsHandler::HandleNotificationL( TInt aId )
       
   272 	{
       
   273     TInt root;
       
   274     iEng.RootFolderL( root ); 	
       
   275 	TInt parentId(0);                
       
   276 	if ( aId != KErrNotFound )
       
   277 		{
       
   278 		//remove running status from cache (for application)
       
   279 		iCacheHandler.HandleRemoveAttribute( aId, KRunningStatus );
       
   280 		TBool oldStatus;
       
   281 		TBool newStatus;
       
   282 	    while( aId != root )
       
   283 	    	{
       
   284 	        iEng.ParentFolderL( aId, parentId );
       
   285 	        iEng.AppendNotifyL(parentId, 
       
   286 	        		RMenuNotifier::EItemAttributeChanged);
       
   287 	        
       
   288 	        oldStatus = GetFolderPreviousRunningStatusL( parentId );
       
   289 	        newStatus = GetFolderRunningStatusL( parentId );
       
   290 	        if( oldStatus != newStatus )
       
   291 				{
       
   292 				iCacheHandler.HandleRemoveAttribute( 
       
   293 						parentId, KRunningStatus );
       
   294 				UpdateFolderRunningStatusL( parentId, newStatus );
       
   295 				aId = parentId;
       
   296 				}
       
   297 	        else
       
   298 	        	{
       
   299 	        	break;
       
   300 	        	}
       
   301 	    	}//while
       
   302 		}//if
       
   303 	}
       
   304 
       
   305 // ---------------------------------------------------------
       
   306 // CMcsRunningAppsHandler::HandleNotificationL
       
   307 // ---------------------------------------------------------
       
   308 //
       
   309 void CMcsRunningAppsHandler::HandleNotificationL( TUid aAppUid )
       
   310 	{
       
   311 	RArray<TMenuItem> itemArray;
       
   312 	CleanupClosePushL( itemArray );
       
   313 	//list of all items representing specified uid 
       
   314 	TInt error = GetItemsListL( aAppUid, itemArray );
       
   315 	if( error == KErrNone )
       
   316 		{
       
   317 		TInt count = itemArray.Count();
       
   318 		for( TInt i = 0; i < count; i++ )
       
   319 			{
       
   320 			HandleNotificationL( itemArray[i].Id() );
       
   321 			}
       
   322 		}
       
   323     CleanupStack::PopAndDestroy( &itemArray );
       
   324 	}
       
   325 
       
   326 
       
   327 // ---------------------------------------------------------
       
   328 // CMcsRunningAppsHandler::GetItemsListL
       
   329 // ---------------------------------------------------------
       
   330 //
       
   331 
       
   332 TInt CMcsRunningAppsHandler::GetItemsListL( TUid aAppUid, RArray<TMenuItem>& aItemArray )
       
   333     {
       
   334     TInt result(KErrNotFound);
       
   335     TInt root;
       
   336     iEng.RootFolderL( root );
       
   337     TMenuSrvTypeAttrFilter appFilter;
       
   338     appFilter.SetType( KMenuTypeApp() );
       
   339     TBuf<KUidChars> uidString;
       
   340     TInt appuid( aAppUid.iUid );
       
   341     MenuSrvUtil::UidToStringL( appuid, uidString, EFalse, EHex );
       
   342     appFilter.SetAttr( KMenuAttrUid(), uidString );
       
   343     iEng.GetItemsL( aItemArray, root, &appFilter, ETrue );
       
   344     //if not found in mcs try with UID in decimal format
       
   345     if (!aItemArray.Count())
       
   346         {
       
   347         MenuSrvUtil::UidToStringL( appuid, uidString, EFalse, EDecimal );
       
   348         appFilter.SetAttr( KMenuAttrUid(), uidString );
       
   349         iEng.GetItemsL( aItemArray, root, &appFilter, ETrue );
       
   350         }
       
   351    if ( aItemArray.Count() )
       
   352         {
       
   353         result = KErrNone;
       
   354         }
       
   355     return result;
       
   356     }    
       
   357 
       
   358 
       
   359 // ---------------------------------------------------------
       
   360 // CMcsRunningAppsHandler::UpdateFolderRunningStatusL
       
   361 // ---------------------------------------------------------
       
   362 //
       
   363 void CMcsRunningAppsHandler::UpdateFolderRunningStatusL( TInt aFolderId, 
       
   364 		TBool aNewRunningStatus )
       
   365     {
       
   366     TInt id = iRunningFolderStatus.Find( aFolderId );
       
   367     if ( aNewRunningStatus && ( id == KErrNotFound ))
       
   368     	{
       
   369     	iRunningFolderStatus.AppendL( aFolderId );
       
   370     	}
       
   371     if ( !aNewRunningStatus && ( id != KErrNotFound ))
       
   372     	{
       
   373     	iRunningFolderStatus.Remove( id );
       
   374     	}
       
   375     }
       
   376 
       
   377 
       
   378 // ---------------------------------------------------------
       
   379 // CMcsRunningAppsHandler::UpdateFoldersRunningStatusL
       
   380 // ---------------------------------------------------------
       
   381 //
       
   382 void CMcsRunningAppsHandler::UpdateFoldersRunningStatusL(  )
       
   383     {
       
   384     TMenuSrvTypeFilter filter;
       
   385     filter.SetType( KMenuTypeFolder() );
       
   386       
       
   387     TInt root;
       
   388     iEng.RootFolderL( root );
       
   389     
       
   390     RArray<TMenuItem> aItemArray;
       
   391     CleanupClosePushL(aItemArray);
       
   392     iEng.GetItemsL( aItemArray , root, &filter, ETrue );
       
   393     for ( TInt i = 0; i < aItemArray.Count(); i++ )
       
   394     	{
       
   395     	if ( GetFolderRunningStatusL( aItemArray[i].Id() ) )
       
   396     		{
       
   397     		iRunningFolderStatus.AppendL( aItemArray[i].Id() );
       
   398     		}
       
   399     	}
       
   400     CleanupStack::PopAndDestroy( &aItemArray );
       
   401     }
       
   402 
       
   403 // ---------------------------------------------------------
       
   404 // CMcsRunningAppsHandler::GetRunningStatusL
       
   405 // ---------------------------------------------------------
       
   406 //
       
   407 TBool CMcsRunningAppsHandler::GetRunningStatusL( TInt aItemId )
       
   408     {
       
   409     const CMenuEngObject& obj = iEng.ObjectL( aItemId );
       
   410     return GetRunningStatusL( obj );
       
   411     }
       
   412 
       
   413 // ---------------------------------------------------------
       
   414 // CMcsRunningAppsHandler::GetRunningStatusL
       
   415 // ---------------------------------------------------------
       
   416 //
       
   417 TBool CMcsRunningAppsHandler::GetRunningStatusL( 
       
   418 		const CMenuEngObject& aEngObj )
       
   419     {
       
   420     TBool runningApp( EFalse );
       
   421     if ( aEngObj.Type().CompareF( KMenuTypeApp ) == KErrNone )
       
   422         {
       
   423         TUid appUid;
       
   424         TInt errorUid = CMenuSrvEngUtils::GetAppUid( aEngObj, appUid );
       
   425         
       
   426         if( !errorUid )
       
   427             {
       
   428             runningApp = GetAppRunningStatusL( appUid );
       
   429             }
       
   430         }
       
   431     else if( aEngObj.Type().CompareF( KMenuTypeFolder ) == KErrNone )
       
   432         {
       
   433         runningApp = GetFolderRunningStatusL( aEngObj.Id() );
       
   434         }
       
   435     return runningApp;
       
   436     }
       
   437 
       
   438 // ---------------------------------------------------------
       
   439 // CMcsRunningAppsHandler::GetFolderPreviousRunningStatusL
       
   440 // ---------------------------------------------------------
       
   441 //
       
   442 TBool CMcsRunningAppsHandler::GetFolderPreviousRunningStatusL( TInt aId )
       
   443 	{
       
   444 	if( KErrNotFound == iRunningFolderStatus.Find( aId ) )
       
   445 		{
       
   446 		return EFalse;
       
   447 		}
       
   448 	else
       
   449 		{
       
   450 		return ETrue;
       
   451 		}
       
   452 	
       
   453 	}
       
   454 
       
   455 // ---------------------------------------------------------
       
   456 // CMcsRunningAppsHandler::GetFolderRunningStatusL
       
   457 // ---------------------------------------------------------
       
   458 //
       
   459 TBool CMcsRunningAppsHandler::GetFolderRunningStatusL( TInt aId )
       
   460     {
       
   461     TBool running ( EFalse );
       
   462     TMenuSrvTypeFilter filter;
       
   463     filter.SetType( KMenuTypeApp() );
       
   464     
       
   465     RArray<TMenuItem> aItemArray;
       
   466     CleanupClosePushL(aItemArray);
       
   467     iEng.GetItemsL( aItemArray , aId , &filter, ETrue );
       
   468     for( TInt i = 0; i < aItemArray.Count(); i++)
       
   469         {
       
   470         const CMenuEngObject& obj = iEng.ObjectL( aItemArray[i].Id() );
       
   471         TUid appUid;
       
   472         TInt errorUid = CMenuSrvEngUtils::GetAppUid( obj, appUid );
       
   473         
       
   474         if( !errorUid &&  
       
   475                 !(obj.Flags() & TMenuItem::EHidden) )
       
   476             {
       
   477             running = GetAppRunningStatusL( appUid  );
       
   478             }
       
   479         if( running )
       
   480             {
       
   481             break;
       
   482             }
       
   483         }
       
   484     CleanupStack::PopAndDestroy( &aItemArray );
       
   485     return running;
       
   486     }
       
   487 
       
   488 // ---------------------------------------------------------
       
   489 // CMcsRunningAppsHandler::GetAppRunningStatusL
       
   490 // ---------------------------------------------------------
       
   491 //
       
   492 TBool CMcsRunningAppsHandler::GetAppRunningStatusL( TUid aUid )
       
   493     {
       
   494     TBool running ( EFalse );
       
   495     
       
   496 	TRunningAppsAttr item( aUid );
       
   497 	TInt id = iRunningApps.Find( item, TRunningAppsAttr::MatchByUid );
       
   498 	
       
   499 	if( ( id != KErrNotFound ) &&
       
   500 			( iRunningApps[id].IsRunning() ) && 
       
   501 			( !iRunningApps[id].IsHidden() ) && 
       
   502 			( !iRunningApps[id].IsFswHidden() ) )
       
   503 		{
       
   504 		running = ETrue;
       
   505 		}
       
   506     return running;
       
   507     }
       
   508 
       
   509 // ---------------------------------------------------------
       
   510 // CMcsRunningAppsHandler::HandleAddRemoveRunningAppL
       
   511 // ---------------------------------------------------------
       
   512 //
       
   513 void CMcsRunningAppsHandler::HandleAddRemoveRunningAppL( TInt aFolder )
       
   514 	{
       
   515 	TBool oldStatus = GetFolderPreviousRunningStatusL( aFolder );
       
   516 	TBool newStatus = GetFolderRunningStatusL( aFolder );
       
   517 	if ( oldStatus != newStatus )
       
   518 		{
       
   519 		UpdateFolderRunningStatusL( aFolder, newStatus );
       
   520 		HandleNotificationL( aFolder );
       
   521 		}
       
   522 	}
       
   523 
       
   524 //  End of File