imagehandlingutilities/thumbnailmanager/thumbagdaemon/src/thumbagdaemon.cpp
changeset 0 2014ca87e772
child 5 82749d516180
child 14 2edacbf5d3f9
equal deleted inserted replaced
-1:000000000000 0:2014ca87e772
       
     1 /*
       
     2 * Copyright (c) 2006-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 "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:  Thumbnail Auto Generate Daemon 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32svr.h>
       
    20 #include <centralrepository.h>
       
    21 
       
    22 #include <mdesession.h>
       
    23 #include <mdeconstants.h>
       
    24 #include <mdequery.h>
       
    25 #include <mdeobject.h>
       
    26 
       
    27 #include "thumbagdaemon.h"
       
    28 #include "thumbnaillog.h"
       
    29 #include "thumbnailmanagerconstants.h"
       
    30 #include "thumbnailmanagerprivatecrkeys.h"
       
    31 
       
    32 
       
    33 // ---------------------------------------------------------------------------
       
    34 // NewLC
       
    35 // ---------------------------------------------------------------------------
       
    36 //
       
    37 CThumbAGDaemon* CThumbAGDaemon::NewLC()
       
    38     {
       
    39     TN_DEBUG1( "CThumbAGDaemon::NewLC() - begin" );
       
    40     
       
    41 	CThumbAGDaemon* self = new (ELeave) CThumbAGDaemon();
       
    42 	CleanupStack::PushL( self );
       
    43 	self->ConstructL();
       
    44 	return self;
       
    45 	}
       
    46 	
       
    47 // ---------------------------------------------------------------------------
       
    48 // NewL
       
    49 // ---------------------------------------------------------------------------
       
    50 //
       
    51 CThumbAGDaemon* CThumbAGDaemon::NewL()
       
    52 	{
       
    53 	TN_DEBUG1( "CThumbAGDaemon::NewL() - begin" );
       
    54     
       
    55 	CThumbAGDaemon* self = CThumbAGDaemon::NewLC();
       
    56 	CleanupStack::Pop( self );
       
    57 	return self;
       
    58 	}
       
    59 
       
    60 // ---------------------------------------------------------------------------
       
    61 // CThumbAGDaemon
       
    62 // ---------------------------------------------------------------------------
       
    63 //
       
    64 CThumbAGDaemon::CThumbAGDaemon()
       
    65  	: CServer2( CActive::EPriorityStandard, CServer2::EUnsharableSessions )
       
    66  	{
       
    67  	// No implementation required
       
    68  	}
       
    69 
       
    70 // ---------------------------------------------------------------------------
       
    71 // ConstructL
       
    72 // ---------------------------------------------------------------------------
       
    73 //
       
    74 void CThumbAGDaemon::ConstructL()
       
    75 	{
       
    76 	TN_DEBUG1( "CThumbAGDaemon::ConstructL() - begin" );
       
    77 	
       
    78 	StartL( KTAGDaemonName );
       
    79 	
       
    80 #ifdef _DEBUG
       
    81     iAddCounter = 0;
       
    82     iModCounter = 0;
       
    83     iDelCounter = 0;
       
    84 #endif
       
    85 	
       
    86     if (DaemonEnabledL())
       
    87         {
       
    88         TN_DEBUG1( "CThumbAGDaemon::ConstructL() - create observers" );
       
    89         
       
    90     	// create shutdown observer
       
    91         iMDSShutdownObserver = CTMShutdownObserver::NewL( *this, KMdSPSShutdown, KMdSShutdown, EFalse );
       
    92     	iShutdownObserver = CTMShutdownObserver::NewL( *this, KTAGDPSNotification, KShutdown, ETrue );  
       
    93     	iShutdown = EFalse;
       
    94     	
       
    95         // create processor
       
    96         iProcessor = NULL;
       
    97         iProcessor = CThumbAGProcessor::NewL();	
       
    98     	
       
    99         TN_DEBUG1( "CThumbAGDaemon::ConstructL() - connect to MDS" );
       
   100         
       
   101     	// connect to MDS
       
   102     	iMdESession = NULL;
       
   103     	iMdESession = CMdESession::NewL( *this );
       
   104         }
       
   105     else
       
   106         {
       
   107         // no error here, but need to shutdown daemon neatly
       
   108         User::Leave(KErrNone);
       
   109         }
       
   110 	
       
   111 	TN_DEBUG1( "CThumbAGDaemon::ConstructL() - end" );
       
   112 	}
       
   113 
       
   114 // ---------------------------------------------------------------------------
       
   115 // ~CThumbAGDaemon
       
   116 // ---------------------------------------------------------------------------
       
   117 //
       
   118 CThumbAGDaemon::~CThumbAGDaemon()
       
   119     {
       
   120     TN_DEBUG1( "CThumbAGDaemon::~CThumbAGDaemon() - begin" );
       
   121     
       
   122     iShutdown = ETrue;    
       
   123     
       
   124     delete iMDSShutdownObserver;
       
   125     delete iShutdownObserver;
       
   126     
       
   127     if (iProcessor)
       
   128         {
       
   129         delete iProcessor;
       
   130         iProcessor = NULL;
       
   131         }
       
   132     
       
   133     if (iMdESession)
       
   134         {
       
   135         // 3 observers
       
   136         TRAP_IGNORE( iMdESession->RemoveObjectObserverL( *this ) );
       
   137         TRAP_IGNORE( iMdESession->RemoveObjectObserverL( *this ) );
       
   138         TRAP_IGNORE( iMdESession->RemoveObjectObserverL( *this ) );
       
   139         TRAP_IGNORE(iMdESession->RemoveObjectPresentObserverL( * this  ));
       
   140         
       
   141         delete iMdESession;
       
   142         iMdESession = NULL;
       
   143         }
       
   144     
       
   145     TN_DEBUG1( "CThumbAGDaemon::~CThumbAGDaemon() - end" );
       
   146     }
       
   147 
       
   148 // -----------------------------------------------------------------------------
       
   149 // CThumbnailServer::NewSessionL()
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 CSession2* CThumbAGDaemon::NewSessionL( const TVersion& /*aVersion*/,
       
   153                                         const RMessage2& /*aMessage*/ )const
       
   154     {
       
   155     // no services, no clients, no sessions
       
   156     User::Leave(KErrNotSupported);
       
   157     
       
   158     //just for getting rid of compiler warning about missing return value
       
   159     return NULL;
       
   160     }
       
   161 
       
   162 // ---------------------------------------------------------------------------
       
   163 // CThumbAGDaemon::ThreadFunctionL
       
   164 // ---------------------------------------------------------------------------
       
   165 //
       
   166 void CThumbAGDaemon::ThreadFunctionL()
       
   167     {
       
   168 	TN_DEBUG1( "CThumbAGDaemon::ThreadFunctionL() - begin" );
       
   169     
       
   170     User::LeaveIfError( User::RenameThread( KTAGDaemonName ) );
       
   171 
       
   172     CThumbAGDaemon* server = NULL;
       
   173     CActiveScheduler* scheduler = new( ELeave )CActiveScheduler();
       
   174 
       
   175     if ( scheduler )
       
   176         {
       
   177         CActiveScheduler::Install( scheduler );
       
   178         
       
   179         CleanupStack::PushL( scheduler );
       
   180         server = CThumbAGDaemon::NewL(); // Adds server in scheduler
       
   181 
       
   182         RProcess::Rendezvous( KErrNone );
       
   183 
       
   184         CActiveScheduler::Start();
       
   185        
       
   186         // comes here if server gets shut down
       
   187         delete server;
       
   188         
       
   189         CleanupStack::PopAndDestroy( scheduler );
       
   190         }
       
   191     
       
   192     TN_DEBUG1( "CThumbAGDaemon::ThreadFunctionL() - end" );
       
   193 	}
       
   194 
       
   195 // -----------------------------------------------------------------------------
       
   196 // CThumbAGDaemon::HandleSessionOpened
       
   197 // -----------------------------------------------------------------------------
       
   198 //
       
   199 void CThumbAGDaemon::HandleSessionOpened( CMdESession& /* aSession */, TInt aError )
       
   200     {
       
   201     TN_DEBUG1( "CThumbAGDaemon::HandleSessionOpened");
       
   202     
       
   203     if (aError == KErrNone)
       
   204         {
       
   205         iProcessor->SetMdESession(iMdESession);
       
   206         TRAP_IGNORE(iProcessor->QueryForPlaceholdersL());
       
   207         
       
   208         TRAPD( err, AddObserversL() );
       
   209         if (err != KErrNone)
       
   210             {
       
   211             TN_DEBUG2( "CThumbAGDaemon::HandleSessionOpened, AddObserversL error == %d", err );
       
   212             }
       
   213         }
       
   214     else
       
   215         {
       
   216         TN_DEBUG2( "CThumbAGDaemon::HandleSessionOpened error == %d", aError );
       
   217         }
       
   218     }
       
   219 
       
   220 // -----------------------------------------------------------------------------
       
   221 // CThumbAGDaemon::HandleSessionError
       
   222 // -----------------------------------------------------------------------------
       
   223 //
       
   224 void CThumbAGDaemon::HandleSessionError( CMdESession& /*aSession*/, TInt aError )
       
   225     {
       
   226     if (aError != KErrNone)
       
   227         {
       
   228         TN_DEBUG2( "CThumbAGDaemon::HandleSessionError == %d", aError );
       
   229         }   
       
   230     }
       
   231 
       
   232 // -----------------------------------------------------------------------------
       
   233 // CThumbAGDaemon::HandleObjectNotification
       
   234 // -----------------------------------------------------------------------------
       
   235 //
       
   236 void CThumbAGDaemon::HandleObjectNotification( CMdESession& /*aSession*/, 
       
   237                                                TObserverNotificationType aType,
       
   238                                                const RArray<TItemId>& aObjectIdArray )
       
   239     {
       
   240     TN_DEBUG1( "CThumbAGDaemon::HandleObjectNotification() - begin" );
       
   241 
       
   242     // no processor or shutting down
       
   243     if (!iProcessor || iShutdown)
       
   244         {
       
   245         return;
       
   246         }
       
   247     
       
   248 #ifdef _DEBUG
       
   249     if (aType == ENotifyAdd)
       
   250         {
       
   251         TN_DEBUG2( "CThumbAGDaemon::HandleObjectNotification() - ENotifyAdd %d", aObjectIdArray.Count() );
       
   252         iAddCounter = iAddCounter + aObjectIdArray.Count();
       
   253         }
       
   254     else if (aType == ENotifyModify)
       
   255         {
       
   256         TN_DEBUG2( "CThumbAGDaemon::HandleObjectNotification() - ENotifyModify %d", aObjectIdArray.Count() );
       
   257         iModCounter = iModCounter + aObjectIdArray.Count();
       
   258         }
       
   259     else if (aType == ENotifyRemove)
       
   260         {
       
   261         TN_DEBUG2( "CThumbAGDaemon::HandleObjectNotification() - ENotifyRemove %d", aObjectIdArray.Count() );
       
   262         iDelCounter = iDelCounter + aObjectIdArray.Count();
       
   263         }
       
   264 #endif
       
   265     
       
   266     if ( (aType == ENotifyAdd || aType == ENotifyModify || aType == ENotifyRemove) &&
       
   267          (aObjectIdArray.Count() > 0) )
       
   268         {
       
   269         TN_DEBUG1( "CThumbAGDaemon::HandleObjectNotification() - AddToQueueL" );
       
   270 		
       
   271         // If delete event, remove IDs from Modify and Add queues
       
   272         if ( aType == ENotifyRemove )
       
   273             {
       
   274             iProcessor->RemoveFromQueues( aObjectIdArray );
       
   275             }
       
   276         
       
   277         // Add event to processing queue by type and enable force run
       
   278         TRAPD(err, iProcessor->AddToQueueL(aType, aObjectIdArray, EFalse));
       
   279         if (err != KErrNone)
       
   280             {
       
   281             TN_DEBUG1( "CThumbAGDaemon::HandleObjectNotification() - error adding to queue" );
       
   282             }
       
   283         }
       
   284     else
       
   285         {
       
   286         TN_DEBUG1( "CThumbAGDaemon::HandleObjectNotification() - bad notification" );
       
   287         }
       
   288     
       
   289 #ifdef _DEBUG
       
   290     TN_DEBUG6( "CThumbAGDaemon::IN-COUNTERS---------- Type: %d Amount: %d, Add = %d Modify = %d Delete = %d", 
       
   291                aType, aObjectIdArray.Count(), iAddCounter, iModCounter, iDelCounter );
       
   292 #endif
       
   293 
       
   294     TN_DEBUG1( "CThumbAGDaemon::HandleObjectNotification() - end" );
       
   295     }
       
   296 
       
   297 // -----------------------------------------------------------------------------
       
   298 // CThumbAGDaemon::HandleObjectPresentNotification
       
   299 // -----------------------------------------------------------------------------
       
   300 //
       
   301 void CThumbAGDaemon::HandleObjectPresentNotification(CMdESession& /*aSession*/, 
       
   302                TBool aPresent, const RArray<TItemId>& aObjectIdArray)
       
   303     {
       
   304     TN_DEBUG3( "CThumbAGDaemon::HandleObjectPresentNotification() - aPresent == %d count == %d", aPresent, aObjectIdArray.Count() );
       
   305     
       
   306     // no processor or shutting down
       
   307     if (!iProcessor || iShutdown)
       
   308         {
       
   309         return;
       
   310         }
       
   311     
       
   312     TInt err = 0;
       
   313     
       
   314     //tread present objects as added
       
   315     if(aPresent)
       
   316         {
       
   317         TRAP_IGNORE( iProcessor->QueryForPlaceholdersL());
       
   318         if ( aObjectIdArray.Count() > 0) 
       
   319            {
       
   320 		   // do not force run of these items
       
   321            TRAP(err, iProcessor->AddToQueueL(ENotifyModify, aObjectIdArray, ETrue));
       
   322            
       
   323            TN_DEBUG2( "CThumbAGDaemon::HandleObjectPresentNotification() - ENotifyAdd %d", aObjectIdArray.Count() );
       
   324            
       
   325            
       
   326 #ifdef _DEBUG
       
   327            iAddCounter = iAddCounter + aObjectIdArray.Count();
       
   328            if (err != KErrNone)
       
   329                {
       
   330                TN_DEBUG1( "CThumbAGDaemon::HandleObjectPresentNotification() - error adding to queue" );
       
   331                }
       
   332 #endif
       
   333            }
       
   334         }
       
   335     else
       
   336         {
       
   337         TN_DEBUG1( "CThumbAGDaemon::HandleObjectPresentNotification() - handle not present" );
       
   338         TRAP_IGNORE( iProcessor->QueryForPlaceholdersL() );
       
   339 #ifdef _DEBUG    
       
   340         if( iModCounter < aObjectIdArray.Count() )
       
   341             {
       
   342             iModCounter = 0;
       
   343             }
       
   344         else
       
   345             {
       
   346             iModCounter = iModCounter - aObjectIdArray.Count();
       
   347             }
       
   348 #endif
       
   349            
       
   350         if ( aObjectIdArray.Count() > 0) 
       
   351            {
       
   352            iProcessor->RemoveFromQueues( aObjectIdArray, ETrue );
       
   353            }
       
   354         }
       
   355     
       
   356     #ifdef _DEBUG
       
   357     TN_DEBUG5( "CThumbAGDaemon::IN-COUNTERS---------- Amount: %d, Add = %d Modify = %d Delete = %d", 
       
   358                aObjectIdArray.Count(), iAddCounter, iModCounter, iDelCounter );
       
   359     #endif
       
   360     
       
   361     TN_DEBUG1( "CThumbAGDaemon::HandleObjectPresentNotification() - end" );
       
   362     }
       
   363 
       
   364 // -----------------------------------------------------------------------------
       
   365 // CThumbAGDaemon::ShutdownNotification
       
   366 // -----------------------------------------------------------------------------
       
   367 //
       
   368 void CThumbAGDaemon::ShutdownNotification()
       
   369     {
       
   370     TN_DEBUG1( "CThumbAGDaemon::ShutdownNotification()" );
       
   371     
       
   372     if (!iShutdown)
       
   373         {
       
   374         CActiveScheduler::Stop();
       
   375         iShutdown = ETrue;
       
   376         }
       
   377     }
       
   378 
       
   379 // ---------------------------------------------------------------------------
       
   380 // CThumbAGDaemon::AddObserversL
       
   381 // ---------------------------------------------------------------------------
       
   382 //
       
   383 void CThumbAGDaemon::AddObserversL()
       
   384     {
       
   385     TN_DEBUG1( "CThumbAGDaemon::AddObserversL() - begin" );
       
   386     
       
   387     CMdENamespaceDef& defaultNamespace = iMdESession->GetDefaultNamespaceDefL();
       
   388     CMdEObjectDef& imageDef = defaultNamespace.GetObjectDefL( MdeConstants::Image::KImageObject );
       
   389     CMdEObjectDef& videoDef = defaultNamespace.GetObjectDefL( MdeConstants::Video::KVideoObject );
       
   390     CMdEObjectDef& audioDef = defaultNamespace.GetObjectDefL( MdeConstants::Audio::KAudioObject );
       
   391     
       
   392     // set observing conditions
       
   393     CMdELogicCondition* addCondition = CMdELogicCondition::NewLC( ELogicConditionOperatorOr );
       
   394     addCondition->AddObjectConditionL( imageDef );
       
   395     addCondition->AddObjectConditionL( videoDef );
       
   396     addCondition->AddObjectConditionL( audioDef );
       
   397     CleanupStack::Pop( addCondition );  
       
   398     
       
   399     CMdELogicCondition* modifyCondition = CMdELogicCondition::NewLC( ELogicConditionOperatorOr );
       
   400     modifyCondition->AddObjectConditionL( imageDef );
       
   401     modifyCondition->AddObjectConditionL( videoDef );
       
   402     modifyCondition->AddObjectConditionL( audioDef );
       
   403     CleanupStack::Pop( modifyCondition );
       
   404     
       
   405     // add observer
       
   406     iMdESession->AddObjectObserverL( *this, addCondition, ENotifyAdd ); 
       
   407     
       
   408     // modify observer
       
   409     iMdESession->AddObjectObserverL( *this, modifyCondition, ENotifyModify );
       
   410  
       
   411     // remove observer
       
   412     iMdESession->AddObjectObserverL( *this, NULL, ENotifyRemove );
       
   413     
       
   414     // object present observer
       
   415     iMdESession->AddObjectPresentObserverL( *this );
       
   416     
       
   417     TN_DEBUG1( "CThumbAGDaemon::AddObserversL() - end" );
       
   418     }
       
   419 
       
   420 // ---------------------------------------------------------------------------
       
   421 // CThumbAGDaemon::DaemonEnabledL
       
   422 // ---------------------------------------------------------------------------
       
   423 //
       
   424 TBool CThumbAGDaemon::DaemonEnabledL()
       
   425     {
       
   426     TN_DEBUG1( "CThumbAGDaemon::DaemonEnabledL() - begin" );
       
   427     CRepository* rep = CRepository::NewL( TUid::Uid( THUMBNAIL_CENREP_UID ));
       
   428     
       
   429     // get value
       
   430     TBool val( EFalse );
       
   431     TInt ret = rep->Get( KEnableDaemon, val );
       
   432     
       
   433     delete rep;
       
   434     TN_DEBUG3( "CThumbAGDaemon::DaemonEnabledL() - val == %d, ret == %d", val, ret );
       
   435     return val;
       
   436     }
       
   437 
       
   438 // ---------------------------------------------------------------------------
       
   439 // E32Main
       
   440 // ---------------------------------------------------------------------------
       
   441 //
       
   442 TInt E32Main()
       
   443     {    
       
   444     TN_DEBUG1( "CThumbAGDaemon::E32Main() - begin" );
       
   445 
       
   446     __UHEAP_MARK;
       
   447 
       
   448     CTrapCleanup* cleanup = CTrapCleanup::New();
       
   449     TInt result = KErrNoMemory;
       
   450     
       
   451     if ( cleanup )
       
   452         {
       
   453         TRAP( result, CThumbAGDaemon::ThreadFunctionL());
       
   454         delete cleanup;
       
   455         }
       
   456     
       
   457     if ( result != KErrNone )
       
   458         {
       
   459         TN_DEBUG1( "CThumbAGDaemon::E32Main() - error" );
       
   460         
       
   461         // Signal the client that server creation failed
       
   462         RProcess::Rendezvous( result );
       
   463         }    
       
   464     
       
   465     __UHEAP_MARKEND;
       
   466 
       
   467     TN_DEBUG1( "CThumbAGDaemon::E32Main() - end" );
       
   468     
       
   469     return result;
       
   470     }
       
   471 
       
   472 // End of file