harvesterplugins/media/audio/src/mediaplugin.cpp
changeset 0 ccd0fd43f247
child 2 208a4ba3894c
equal deleted inserted replaced
-1:000000000000 0:ccd0fd43f247
       
     1 /*
       
     2 * Copyright (c) 2010 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:  Media Audio harvester plugin implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "mediaplugin.h"
       
    20 
       
    21 #include <e32base.h> 
       
    22 
       
    23 #include "harvesterserverlogger.h"
       
    24 #include "common.h"
       
    25 #include "csearchdocument.h"
       
    26 #include "ccpixindexer.h"
       
    27 #include "mdeharvester.h"
       
    28 #include "mdeobjectqueuemanager.h"
       
    29 #include "mdsmediamonitor.h"
       
    30 #include "mdssessionobjectutils.h"
       
    31 #include "cpixmediaaudiodoc.h"
       
    32 #include "cpixindexerutils.h"
       
    33 #include "mmcmonitor.h"
       
    34 #include "cpixmdedbmanager.h"
       
    35 #include "mdsitementity.h"
       
    36 
       
    37 //Constants
       
    38 _LIT(KPathTrailer, "\\root\\media\\audio");
       
    39 
       
    40 //***** MEDAI AUDIO*****
       
    41 #define MEDIA_QBASEAPPCLASS   "@0:root media audio"
       
    42 #define LMEDIA_QBASEAPPCLASS  L"@0:root media audio"
       
    43 #define MEDIAAPPCLASS   "root media audio"
       
    44 #define LMEDIAAPPCLASS  L"root media audio"
       
    45 
       
    46 #define MEDIAGENERICAPPCLASS   ":root media audio"
       
    47 #define LMEDIAGENERICAPPCLASS  L":root media audio"
       
    48 #define DATABASEPATH           "\\root\\media\\audio"
       
    49 #define LDATABASEPATH           "\\root\\media\\audio"
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 CAudioPlugin* CAudioPlugin::NewL()
       
    53 	{
       
    54 	CAudioPlugin* instance = CAudioPlugin::NewLC();
       
    55     CleanupStack::Pop(instance);
       
    56     return instance;
       
    57 	}
       
    58 
       
    59 // -----------------------------------------------------------------------------
       
    60 CAudioPlugin* CAudioPlugin::NewLC()
       
    61 	{
       
    62 	CAudioPlugin* instance = new (ELeave) CAudioPlugin();
       
    63     CleanupStack::PushL(instance);
       
    64     instance->ConstructL();
       
    65     return instance;
       
    66 	}
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 CAudioPlugin::CAudioPlugin()
       
    70 	{
       
    71 	//No implementation required. 
       
    72 	//Necessary things done in ConstructL()
       
    73 #ifdef __PERFORMANCE_DATA
       
    74     count = 0;
       
    75 #endif
       
    76 	}
       
    77 
       
    78 // -----------------------------------------------------------------------------
       
    79 CAudioPlugin::~CAudioPlugin()
       
    80 	{
       
    81 	delete iMdeHarvester;
       
    82 	delete iMdsMonitor;
       
    83 	delete iObjectJobQueueManager;
       
    84 	delete iMdsUtils;
       
    85 	delete iAudioDocument;
       
    86 	delete iIndexerUtil;
       
    87 	delete iMMcMonitor;
       
    88 	delete iDBManager;
       
    89 	delete iMdsItem;
       
    90 	}
       
    91 	
       
    92 // -----------------------------------------------------------------------------
       
    93 void CAudioPlugin::ConstructL()
       
    94 	{
       
    95 	
       
    96 	}
       
    97 
       
    98 // -----------------------------------------------------------------------------
       
    99 void CAudioPlugin::StartPluginL()
       
   100 	{
       
   101     CPIXLOGSTRING("CAudioPlugin::StartPluginL");
       
   102 	// Define this base application class, use default location
       
   103 	iIndexerUtil = CCPixIndexerUtils::NewL(iSearchSession);
       
   104 	//Test well before use
       
   105 	TBuf<KMaxFileName> databasePathTemp;
       
   106 	databasePathTemp.Append(KPathTrailer);
       
   107 	iIndexerUtil->MountAllAvailableDriveL(_L(MEDIAGENERICAPPCLASS),databasePathTemp);
       
   108 	//Instantiate all monitoring and harvesting class here
       
   109 	iMdsUtils = CMdsSessionObjectUtils::NewL();
       
   110 	iMdsUtils->InitializeL(); //Create valid session in it
       
   111 	iObjectJobQueueManager = CMdeObjectQueueManager::NewL(this);
       
   112 	iMdeHarvester = CMdeHarvester::NewL(iMdsUtils->GetSession(),
       
   113 	                                    this,iObjectJobQueueManager);
       
   114 	iMdsMonitor = CMdsMediaMonitor::NewL(iMdsUtils->GetSession(),iObjectJobQueueManager);
       
   115 	iMMcMonitor = CMMCMonitorUtil::NewL(this);//Observer is self
       
   116 	iMMcMonitor->StartMonitoring();
       
   117 	//Start Monitoring now
       
   118 	iMdsMonitor->StartMonitoringL(MdeConstants::Audio::KAudioObject);//Start monitoring Audio objects
       
   119 	iAudioDocument = CCPIXMediaAudioDoc::NewL(); //Doc fetcher
       
   120 	// Start harvester for this plugin
       
   121 	iObserver->AddHarvestingQueue(this, _L(MEDIA_QBASEAPPCLASS) );
       
   122 	TUid uidOfPlugin = {0x20029AB9};
       
   123 	iDBManager = CCPIXMDEDbManager::NewL(uidOfPlugin);
       
   124 	iMdsItem = CMDSEntity::NewL();
       
   125 	}
       
   126 
       
   127 void CAudioPlugin::HarvestingCompletedL()
       
   128     {
       
   129 #ifdef __PERFORMANCE_DATA
       
   130     UpdatePerformaceDataL();
       
   131 #endif 
       
   132     iObserver->HarvestingCompleted(this, _L(MEDIA_QBASEAPPCLASS), KErrNone);
       
   133     }
       
   134 // -----------------------------------------------------------------------------
       
   135 void CAudioPlugin::StartHarvestingL( const TDesC& /*aMedia*/ )
       
   136     {
       
   137     //iIndexer->ResetL();//reset any indexes if exist already
       
   138     CPIXLOGSTRING("CAudioPlugin::StartHarvestingL");
       
   139     iDBManager->ResetL();
       
   140     iIndexerUtil->ResetAllL();
       
   141     //Start Media harvesting here
       
   142 #ifdef __PERFORMANCE_DATA  
       
   143     iStartTime.UniversalTime();
       
   144 #endif  
       
   145     iMdeHarvester->DoHarvesetL(MdeConstants::Audio::KAudioObject);//Start Harvesting
       
   146     }
       
   147 
       
   148 //handle MMC event for the Drive
       
   149 void CAudioPlugin::HandleMMCEventL(const TDriveNumber aDrive,const TBool aMMCInsertRemoval)
       
   150     {
       
   151     TBuf<KMaxFileName> databasePathTemp;
       
   152     databasePathTemp.Append(KPathTrailer);
       
   153     if(aMMCInsertRemoval) //MMC inserted
       
   154         {
       
   155         //Test well before use
       
   156         iIndexerUtil->MountDriveL(aDrive,_L(MEDIAGENERICAPPCLASS),databasePathTemp);
       
   157         //reset indexes existing first
       
   158         //With new Implementation no need to reset
       
   159         //iIndexerUtil->GetIndexerFromDrive(aDrive)->ResetL();
       
   160         }
       
   161     else
       
   162         {
       
   163         //MMC is removed but we can not reset index here as Media physically not avilable so
       
   164         // we have to do at the time of insertion only
       
   165         //Unmount drive and also reset the index as we anyhow has to reindex on insertion back 
       
   166         iIndexerUtil->UnMountDriveL(aDrive,_L(MEDIAGENERICAPPCLASS),ETrue);
       
   167         }
       
   168     }
       
   169 
       
   170 void CAudioPlugin::HandleMdeItemL( TItemId aObjId, TCPixActionType aActionType)
       
   171     {
       
   172     //Get the object from object utils and get the CPIX document and add delete or update
       
   173     CPIXLOGSTRING2("CAudioPlugin::HandleMdeItemL(): aObjId = %d ", aObjId );
       
   174     // creating CSearchDocument object with unique ID for this application
       
   175     TBuf<KMaxFileName> docid_str;
       
   176     if (aActionType == ECPixAddAction || aActionType == ECPixUpdateAction )
       
   177         {
       
   178         //To Improve performance check here only before Getobject if it is Add event
       
   179         //and we already have in our database just return in case of update move forward
       
   180         if(iDBManager->IsAlreadyExistL(aObjId) && aActionType == ECPixAddAction)//Avoid reharvesting
       
   181             {
       
   182             CPIXLOGSTRING("CAudioPlugin::HandleMdeItemL(): Already harvested returning");
       
   183             return;
       
   184             }
       
   185         //The caller should always handle leave of HandleMdeItemL
       
   186         CMdEObject& object= iMdsUtils->GetObjectL(aObjId,MdeConstants::Audio::KAudioObject);
       
   187         CMdENamespaceDef& defaultNamespaceDef = iMdsUtils->GetSession()->GetDefaultNamespaceDefL();
       
   188         CMdEObjectDef& audioObjDef = defaultNamespaceDef.GetObjectDefL(MdeConstants::Audio::KAudioObject );
       
   189         CSearchDocument* index_item = iAudioDocument->GetCpixDocumentL(object,_L(MEDIAAPPCLASS),audioObjDef);
       
   190         iIndexer = iIndexerUtil->GetIndexerFromMediaId(object.MediaId());
       
   191         if(!iIndexer)
       
   192             {
       
   193             delete index_item;//Remove
       
   194             CPIXLOGSTRING("CAudioPlugin::HandleMdeItemL(): Indexer not found");
       
   195             return;
       
   196             }
       
   197         // Send for indexing
       
   198         if (aActionType == ECPixAddAction)
       
   199             {
       
   200 #ifdef __PERFORMANCE_DATA
       
   201             ++count;
       
   202 #endif
       
   203             CPIXLOGSTRING("CAudioPlugin::HandleMdeItemL(): Indexer found calling AddL");
       
   204             //First check if it already exist in database
       
   205             TRAPD(err, iIndexer->UpdateL(*index_item)); //Always Update to avoid reharvesting
       
   206             if (err == KErrNone)
       
   207                 {
       
   208                 //Add entity to database for later use
       
   209                 iMdsItem->Reset();//reset existing values
       
   210                 iMdsItem->Setkey(aObjId);
       
   211                 iMdsItem->SetUri(iAudioDocument->GetUri());//Use the Absolute URI
       
   212                 TDriveNumber drive;
       
   213                 iIndexerUtil->GetDriveFromMediaId(object.MediaId(),drive);
       
   214                 iMdsItem->SetDrive(drive);
       
   215                 iDBManager->AddL(aObjId,*iMdsItem);//Add to SyncDB
       
   216                 CPIXLOGSTRING("CAudioPlugin::HandleMdeItemL(): Added.");
       
   217                 }
       
   218             else
       
   219                 {
       
   220                 CPIXLOGSTRING2("CAudioPlugin::HandleMdeItemL(): Error %d in adding.", err);
       
   221                 }
       
   222             }
       
   223         else if (aActionType == ECPixUpdateAction)
       
   224             {
       
   225             //If DocID for e.g \music\something.mp3 itself changes the we have to compare
       
   226             //existing URI with new one and Remove that first and Update to avoid 
       
   227             //Duplicate entries
       
   228             iMdsItem->Reset();//reset existing values
       
   229             TRAPD(err, iDBManager->GetItemL(aObjId,*iMdsItem));
       
   230             if(err == KErrNone)
       
   231                {
       
   232                //Get the drivenumber from the syncDB to delete the appropriate docId 
       
   233                //in case an item is moved to different drive, delete from the old drive first and later update.
       
   234                //comparing the absolute paths along with the root drives
       
   235                 if(iMdsItem->GetPath().Compare(object.Uri()) != 0)
       
   236                    {                        
       
   237                    CCPixIndexer *tempIndexer;            
       
   238                    tempIndexer = iIndexerUtil->GetIndexerFromDrive(iMdsItem->DriveNumber());
       
   239                    tempIndexer->DeleteL(iMdsItem->Uri());
       
   240                    CPIXLOGSTRING("CAudioPlugin::HandleMdeItemL(): Deleted existing URI for update");
       
   241                    }
       
   242                 TRAPD(error, iIndexer->UpdateL(*index_item));
       
   243                 if (error == KErrNone)
       
   244                     {
       
   245                     //Add as new
       
   246                     iMdsItem->Reset();//reset existing values
       
   247                     TDriveNumber drive;
       
   248                     iIndexerUtil->GetDriveFromMediaId(object.MediaId(),drive);
       
   249                     iMdsItem->Set(aObjId,iAudioDocument->GetUri(),drive);
       
   250                     iDBManager->UpdateL(aObjId,*iMdsItem);//Add to SyncDB
       
   251                     CPIXLOGSTRING("CAudioPlugin::HandleMdeItemL(): Updated.");
       
   252                     }
       
   253                 else
       
   254                     {
       
   255                     CPIXLOGSTRING2("CAudioPlugin::HandleMdeItemL(): Error %d in updating.", error);
       
   256                     }
       
   257                }
       
   258             //If a media item is added when downloading or streaming begins, add event is surpassed
       
   259             //by update or delete event because of delay introduced on monitered object by objectqueue manager
       
   260             //So add the Item first, when GetItemL() leaves with item not found
       
   261             else if(err == KErrNotFound)
       
   262                 {
       
   263                 TRAPD(error, iIndexer->UpdateL(*index_item));
       
   264                 if (error == KErrNone)
       
   265                     {
       
   266                     //Add entity to database for later use
       
   267                     iMdsItem->Reset();//reset existing values
       
   268                     iMdsItem->Setkey(aObjId);
       
   269                     iMdsItem->SetUri(iAudioDocument->GetUri());//Use the Absolute URI
       
   270                     TDriveNumber drive;
       
   271                     iIndexerUtil->GetDriveFromMediaId(object.MediaId(),drive);
       
   272                     iMdsItem->SetDrive(drive);
       
   273                     iDBManager->AddL(aObjId,*iMdsItem);//Add to SyncDB
       
   274                     CPIXLOGSTRING("CAudioPlugin::HandleMdeItemL(): Added Item before Update.");
       
   275                     }
       
   276                 }            
       
   277             }
       
   278         delete index_item;
       
   279         iIndexer = NULL;//Assign to null not pointing to any memory
       
   280         }
       
   281     else if (aActionType == ECPixRemoveAction)
       
   282         {
       
   283         // Remove the document
       
   284         //Get the Item from SyncDB for removed Item
       
   285         iMdsItem->Reset();//reset existing values
       
   286         TRAPD(error, iDBManager->GetItemL(aObjId,*iMdsItem));
       
   287         if (error == KErrNone)
       
   288             {
       
   289             iIndexer = iIndexerUtil->GetIndexerFromDrive(iMdsItem->DriveNumber());//Get Indexer from drive
       
   290             if(iIndexer) //If indexer found delete
       
   291                 {
       
   292                 docid_str.Copy(iMdsItem->Uri());
       
   293                 TRAPD(err, iIndexer->DeleteL(docid_str));//Delete it
       
   294                 if (err == KErrNone)
       
   295                     {
       
   296                     CPIXLOGSTRING("CAudioPlugin::HandleMdeItemL(): Deleted.");
       
   297                     }
       
   298                 else
       
   299                     {
       
   300                     CPIXLOGSTRING2("CAudioPlugin::HandleMdeItemL(): Error %d in deleting.", err);
       
   301                     }
       
   302                 }
       
   303             //delete entry from database
       
   304             iDBManager->RemoveL(iMdsItem->Key());
       
   305             }
       
   306         iIndexer = NULL;//Assign to null not pointing to any memory
       
   307         }    
       
   308     }
       
   309 
       
   310 #ifdef __PERFORMANCE_DATA
       
   311 void CAudioPlugin::UpdateLogL()
       
   312     {
       
   313     UpdatePerformaceDataL();
       
   314     }
       
   315 
       
   316 void CAudioPlugin::UpdatePerformaceDataL()
       
   317     {
       
   318     TTime now;
       
   319    
       
   320     
       
   321     iCompleteTime.UniversalTime();
       
   322     TTimeIntervalMicroSeconds timeDiff = iCompleteTime.MicroSecondsFrom(iStartTime);
       
   323     
       
   324     RFs fileSession;
       
   325     RFile perfFile;
       
   326     User::LeaveIfError(fileSession.Connect ());
       
   327     
       
   328     
       
   329     /* Open file if it exists, otherwise create it and write content in it */
       
   330     
       
   331     if(perfFile.Open(fileSession, _L("c:\\data\\AudioPerf.txt"), EFileWrite))
       
   332     User::LeaveIfError(perfFile.Create (fileSession, _L("c:\\data\\AudioPerf.txt"), EFileWrite));
       
   333     
       
   334     HBufC8 *heap = HBufC8::NewL(100);
       
   335     TPtr8 ptr = heap->Des();
       
   336     now.HomeTime();
       
   337     TBuf<100> timeString;             
       
   338                 
       
   339     _LIT(KOwnTimeFormat,"%:0%H%:1%T%:2%S");
       
   340     now.FormatL(timeString,KOwnTimeFormat);
       
   341     ptr.AppendNum(now.DateTime().Day());
       
   342     ptr.Append(_L("/"));
       
   343     ptr.AppendNum(now.DateTime().Month());
       
   344     ptr.Append(_L("/"));
       
   345     ptr.AppendNum(now.DateTime().Year());
       
   346     ptr.Append(_L(":"));
       
   347     ptr.Append(timeString);
       
   348     ptr.Append(_L("--"));
       
   349     ptr.AppendNum(count);
       
   350     ptr.Append( _L(" :Time took for Harvesting Audio is : "));
       
   351     ptr.AppendNum(timeDiff.Int64()/1000) ;
       
   352     ptr.Append(_L(" MilliSeonds \n"));
       
   353     TInt myInt = 0;
       
   354     perfFile.Seek(ESeekEnd,myInt);
       
   355     perfFile.Write (ptr);
       
   356     perfFile.Close ();
       
   357     fileSession.Close ();
       
   358     delete heap;
       
   359     }
       
   360 #endif
       
   361 
       
   362 // End of file