perfapps/piprofilerui/ui/avkon/src/profiler_gui_model.cpp
changeset 51 b048e15729d6
parent 26 41ebde60981f
equal deleted inserted replaced
44:5db69f4c3d06 51:b048e15729d6
       
     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 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "profiler_gui_samplersettingsviewdlg.h"
       
    22 #include "profiler_gui_model.h"
       
    23 #include "profiler_gui_app.h"
       
    24 #include "profiler_gui_settingsviewdlg.h"
       
    25 #include "profiler_gui_maincontainer.h"
       
    26 #include "profiler_gui.hrh"
       
    27 #include <piprofilerui.rsg>
       
    28 
       
    29 #include <coeutils.h>
       
    30 #include <bautils.h>
       
    31 #include <eikenv.h>
       
    32 #include <AknQueryDialog.h>
       
    33 #include <AknGlobalNote.h>
       
    34 #include <aknmessagequerydialog.h> 
       
    35 #include <e32math.h> 
       
    36 #include <akntitle.h> 
       
    37 #include <s32file.h>
       
    38 #include <aknnotewrappers.h>
       
    39 #include <sysutil.h>
       
    40 
       
    41 // UIDs
       
    42 #include <piprofiler/EngineUIDs.h>
       
    43 
       
    44 #include <piprofiler/ProfilerTraces.h>
       
    45 #include <piprofiler/ProfilerSession.h>
       
    46 
       
    47 // LITERALS
       
    48 _LIT(KAppName, "PI Profiler");
       
    49 _LIT(KWarningNote, "NOTE: output changed!\n");
       
    50 _LIT(KProfilerEngineExe, "PIProfilerEngine.exe");
       
    51 
       
    52 // literals for default general setting values
       
    53 _LIT8(KTraceOutput, "file_system");
       
    54 _LIT8(KTraceDebugOutput, "debug_output");
       
    55 _LIT8(KProfilerDefaultDrive, "E:\\data");
       
    56 _LIT8(KProfilerDefaultPrefix, "Profiler_#");
       
    57 
       
    58 // ===================================== MEMBER FUNCTIONS =====================================
       
    59 
       
    60 CProfilerGuiModel* CProfilerGuiModel::NewL() 
       
    61 	{
       
    62 	CProfilerGuiModel* self = new(ELeave) CProfilerGuiModel;
       
    63 	CleanupStack::PushL(self);
       
    64 	self->ConstructL();
       
    65 	CleanupStack::Pop();
       
    66 	return self;
       
    67 	}
       
    68 
       
    69 // --------------------------------------------------------------------------------------------
       
    70 
       
    71 CProfilerGuiModel::CProfilerGuiModel() : CActive(EPriorityStandard)
       
    72 	{
       
    73 	}
       
    74 	
       
    75 // --------------------------------------------------------------------------------------------
       
    76 
       
    77 void CProfilerGuiModel::ConstructL()
       
    78 	{
       
    79 	// initialize basic settings
       
    80 	iDrawState = EDrawStateInvalid;
       
    81 	iReferenceNumber = 0;
       
    82 	iState = MProfilerStatusObserver::EIdle;
       
    83 	
       
    84     iEnv = CEikonEnv::Static();
       
    85 
       
    86     // profiler engine specific initialization
       
    87     LaunchProfilerEngineL();
       
    88     
       
    89     // max sampler item list length is 64, i.e. max 64 sampler plugins loaded 
       
    90     iSamplerItemList = new(ELeave) CSamplerItemList(64);
       
    91 
       
    92     // initialize attribute arrays
       
    93     iSamplerAttributes = new(ELeave) CArrayFixFlat<TSamplerAttributes>(20); // max sampler count is 20
       
    94     
       
    95     // engine status checker
       
    96     iStatusChecker = CProfilerEngineStatusChecker::NewL();
       
    97     iStatusChecker->SetObserver(this);
       
    98  	
       
    99 	CActiveScheduler::Add(this);
       
   100 	}
       
   101 
       
   102 void CProfilerGuiModel::UpdateUIRunningStateL()
       
   103     {
       
   104     // prevent the control of sampler specific settings during the trace 
       
   105     iMainContainer->SetDimmed(ETrue);
       
   106     
       
   107     // update status pane
       
   108     iMainView->UpdateStatusPaneL(iState);
       
   109 
       
   110     // show an info popup showing the logging method
       
   111     iMainContainer->ShowWriterInfoPopupL(KNullDesC);
       
   112     
       
   113     // refresh view
       
   114     RefreshViewL();
       
   115     }
       
   116 
       
   117 // --------------------------------------------------------------------------------------------
       
   118 void CProfilerGuiModel::NotifyContainerReadyL()
       
   119 	{
       
   120 	// load initial plugins
       
   121     LoadPluginsL();
       
   122     
       
   123     // get the initial state
       
   124     if( iStatusChecker->GetInitialState() == MProfilerStatusObserver::ERunning )
       
   125         {
       
   126         // set model state to restarting and grabbing an existing profiler process
       
   127         iState = MProfilerStatusObserver::ERestarting;
       
   128         
       
   129         // update status pane to correspond the running mode
       
   130         UpdateUIRunningStateL();
       
   131 
       
   132         // set model state to running
       
   133         iState = MProfilerStatusObserver::ERunning;
       
   134 
       
   135         }
       
   136 	}
       
   137 
       
   138 // --------------------------------------------------------------------------------------------
       
   139 
       
   140 void CProfilerGuiModel::ActivateModelL()
       
   141 	{
       
   142 	// load general settings
       
   143 	if( LoadGeneralSettingsL() != KErrNone )
       
   144 	    {
       
   145 	    LOGTEXT(_L("ProfilerGuiAppUi::ActivateModelL - could not connect profiler engine"));
       
   146 	    }
       
   147 	}
       
   148 
       
   149 // --------------------------------------------------------------------------------------------
       
   150 
       
   151 void CProfilerGuiModel::DeActivateModelL()
       
   152 	{
       
   153 	Cancel();
       
   154 	
       
   155 	// for a faster exit, send the application to background
       
   156     TApaTask selfTask(iEnv->WsSession());
       
   157     selfTask.SetWgId(iEnv->RootWin().Identifier());
       
   158     selfTask.SendToBackground();
       
   159 	}
       
   160 	
       
   161 // --------------------------------------------------------------------------------------------
       
   162 
       
   163 CProfilerGuiModel::~CProfilerGuiModel()
       
   164 	{
       
   165     if (iSamplerItemList)
       
   166         {
       
   167         DeleteAllSamplerItems();
       
   168         delete iSamplerItemList;
       
   169         iSamplerItemList = NULL;
       
   170         }
       
   171     
       
   172     if(iStatusChecker)
       
   173         {
       
   174         iStatusChecker->Cancel();
       
   175         delete iStatusChecker;
       
   176         iStatusChecker = NULL;
       
   177         }
       
   178     
       
   179   
       
   180     if(iSamplerAttributes)
       
   181         {
       
   182         iSamplerAttributes->Reset();
       
   183         delete iSamplerAttributes;
       
   184         iSamplerAttributes = NULL;
       
   185         }
       
   186      
       
   187     }
       
   188 
       
   189 // --------------------------------------------------------------------------------------------
       
   190 
       
   191 void CProfilerGuiModel::DoCancel()
       
   192 	{
       
   193     
       
   194 	}
       
   195 
       
   196 // --------------------------------------------------------------------------------------------
       
   197 
       
   198 void CProfilerGuiModel::RunL()
       
   199 	{
       
   200 
       
   201 	}
       
   202 	
       
   203 // --------------------------------------------------------------------------------------------
       
   204 
       
   205 void CProfilerGuiModel::SetMainContainer(CProfilerGuiMainContainer* aContainer)
       
   206     {
       
   207     iMainContainer = aContainer;
       
   208     iDrawState = EDrawStateMain;
       
   209     }
       
   210 
       
   211 void CProfilerGuiModel::UpdateState(TInt aState)
       
   212 	{
       
   213 	iState = aState;
       
   214 	}
       
   215 
       
   216 TInt CProfilerGuiModel::FindProcessL(RProcess& aProc)
       
   217     {
       
   218     TProcessId engId;
       
   219     TFindProcess procName;
       
   220     procName.Find(_L("PIProfilerEngine.exe*"));
       
   221     TFullName aResult;
       
   222     TFullName aResult2;
       
   223     TInt err(KErrNone);
       
   224 
       
   225     // find the first appearance
       
   226     err = procName.Next(aResult);
       
   227     if(err != KErrNone)
       
   228         {
       
   229         // did not find any engine process
       
   230         return err;
       
   231         }
       
   232     else
       
   233         {
       
   234         err = aProc.Open(procName);
       
   235         if(err == KErrNone)
       
   236             {
       
   237             if(aProc.ExitCategory().Length() > 0)
       
   238                 {
       
   239                 aProc.Close();
       
   240                 // process already exited => create a new one
       
   241                 return KErrNotFound;
       
   242                 }
       
   243             aProc.Close();
       
   244             }
       
   245         }
       
   246     
       
   247 //    // check now if a second appearance exists in process list, 
       
   248 //    // i.e. engine started from eshell => two engine processes appear in normal case
       
   249 //    procName.Next(aResult2);
       
   250 //
       
   251 //    // check if aResult2 contained the second appearance of profiler engine
       
   252 //    if(aResult2.CompareF(aResult) > 0)
       
   253 //        {
       
   254 //        // other process found, i.e. right process to communicate with, in case started from eshell
       
   255 //        err = aProc.Open(procName);
       
   256 //        if(err == KErrNone)
       
   257 //            {
       
   258 //            if(aProc.ExitCategory().Length() > 0)
       
   259 //                {
       
   260 //                // process already exited => create a new one
       
   261 //                return KErrNotFound;
       
   262 //                }
       
   263 //            aProc.Close();
       
   264 //            }
       
   265 //        }
       
   266     
       
   267     return err;
       
   268     }
       
   269 
       
   270 void CProfilerGuiModel::LaunchProfilerEngineL()
       
   271     {
       
   272     LOGTEXT(_L("CProfilerGuiModel::LaunchProfilerEngineL - start"));
       
   273 
       
   274     TRequestStatus stat = KRequestPending;
       
   275     RProcess proc;
       
   276     
       
   277     TInt err(KErrNone);
       
   278     
       
   279     // check if process exists
       
   280     err = FindProcessL(proc);
       
   281     
       
   282     // check if already exists and don't start a new eshell profiling
       
   283     if( err == KErrNotFound )
       
   284         {
       
   285         // try create new process
       
   286         err = proc.Create(KProfilerEngineExe, _L(""));
       
   287 
       
   288         // check if RProcess::Create() succeeded
       
   289         if( err == KErrNone )
       
   290             {
       
   291             // Trigger rendezvous on the supplied TRequestStatus object
       
   292             proc.Rendezvous(stat); 
       
   293 
       
   294             // kick off the engine process
       
   295             proc.Resume();
       
   296             
       
   297             // wait for the constructor to complete 
       
   298             User::WaitForRequest(stat); 
       
   299             
       
   300             // just lose the handle
       
   301             proc.Close();
       
   302             }
       
   303         }
       
   304     
       
   305     // Increase the client reference count in server:
       
   306     AttachClient();    
       
   307     }
       
   308 
       
   309 //-----------------------------------------------------------------------------
       
   310 // CProfilerGuiModel::TerminateProfilerL()
       
   311 // Stops Profiler Engine if it has been launched by this launcher. 
       
   312 //-----------------------------------------------------------------------------
       
   313 
       
   314 void CProfilerGuiModel::TerminateProfilerL()
       
   315     {
       
   316     LOGTEXT(_L("CProfilerGuiModel::TerminateProfiler - entry"));
       
   317     
       
   318     // exit profiler engine 
       
   319     RProfiler::ExitProfiler();
       
   320         
       
   321     LOGTEXT(_L("CProfilerGuiModel::TerminateProfiler - exit"));
       
   322     
       
   323     }
       
   324 
       
   325 void CProfilerGuiModel::AttachClient()
       
   326     {
       
   327     LOGTEXT(_L("CProfilerGuiModel::AttachClient - entry"));
       
   328     
       
   329     // 
       
   330     RProfiler::AttachClient();
       
   331         
       
   332     LOGTEXT(_L("CProfilerGuiModel::AttachClient - exit"));
       
   333     
       
   334     }
       
   335 
       
   336 void CProfilerGuiModel::RemoveClient()
       
   337     {
       
   338     LOGTEXT(_L("CProfilerGuiModel::RemoveClient - entry"));
       
   339     
       
   340     // 
       
   341     RProfiler::RemoveClient();
       
   342         
       
   343     LOGTEXT(_L("CProfilerGuiModel::RemoveClient - exit"));
       
   344     
       
   345     }
       
   346 
       
   347 void CProfilerGuiModel::AddNewSamplersL(CArrayFixFlat<TSamplerAttributes>& aAttributes)
       
   348 	{
       
   349 	TSamplerAttributes item;
       
   350 	
       
   351 	TInt count(aAttributes.Count());
       
   352 
       
   353 	// loop the attribute array and insert them into view list
       
   354 	for (TInt i(0);i<count;i++)
       
   355 		{
       
   356     	// get a TSamplerAttributes from list at a time  
       
   357 		item = aAttributes.At(i);
       
   358 
       
   359 		iReferenceNumber++;
       
   360 	
       
   361         // add item to the array
       
   362         AppendToSamplerItemListL(item);
       
   363         
       
   364         // update the listbox
       
   365         RefreshViewL(EFalse);
       
   366         
       
   367         // set item index to the begin	
       
   368         iMainContainer->ListBox()->SetCurrentItemIndexAndDraw(iMainContainer->ListBox()->Model()->NumberOfItems()-1);
       
   369 
       
   370 		}
       
   371     // refresh again
       
   372     if(iReferenceNumber > 0)
       
   373         {
       
   374         RefreshViewL(ETrue);
       
   375         }
       
   376 	}
       
   377 
       
   378 TBool CProfilerGuiModel::CheckTraceLocationSanityL(TGeneralAttributes& aAttr, TBool aQuietCheck)
       
   379     {
       
   380     RFs fs;
       
   381     User::LeaveIfError(fs.Connect());
       
   382 
       
   383     TBuf<32> drive;
       
   384     
       
   385     CnvUtfConverter::ConvertToUnicodeFromUtf8(drive, aAttr.iSaveFileDrive);
       
   386 
       
   387     TDriveUnit driveUnit = TDriveUnit(drive);
       
   388 
       
   389     TBool ret(EFalse);
       
   390 
       
   391     // check that the root folder is correct
       
   392     if (drive.Length() > 2 && BaflUtils::CheckFolder(fs, drive.Left(3)) == KErrNone )
       
   393         {
       
   394         // check then if drive has still some space
       
   395         if(!SysUtil::DiskSpaceBelowCriticalLevelL(&fs, 0, driveUnit))
       
   396             {
       
   397             ret = ETrue;
       
   398             }
       
   399         }
       
   400     
       
   401     // check if the sanity check failed
       
   402     if(!ret)
       
   403         {
       
   404         // show an error note
       
   405         if(aQuietCheck == EFalse)
       
   406             {
       
   407             CAknErrorNote* note = new(ELeave) CAknErrorNote();
       
   408             note->ExecuteLD(_L("Invalid path, check settings!"));
       
   409             }
       
   410         }
       
   411     fs.Close();
       
   412     return ret;
       
   413     }
       
   414 
       
   415 // --------------------------------------------------------------------------------------------
       
   416 
       
   417 TInt CProfilerGuiModel::LaunchSettingsDialogL()
       
   418     {
       
   419     // launch the settings dialog
       
   420     TGeneralAttributes newSettings = iGeneralAttributes;
       
   421     TBool quietCheck(EFalse);
       
   422     
       
   423     CProfilerGuiSettingsViewDlg* dlg = CProfilerGuiSettingsViewDlg::NewL(newSettings);
       
   424     TInt returnValue = dlg->ExecuteLD(R_PROFILER_GUI_SETTINGS_DIALOG);
       
   425     
       
   426     // check if exit command => no error note to user
       
   427     if(returnValue == EAknCmdExit)
       
   428         quietCheck = ETrue;
       
   429     
       
   430     // always save settings since the settings dialog does not provide a possibility to cancel
       
   431     iGeneralAttributes.iTraceOutput.Copy(newSettings.iTraceOutput);
       
   432     iGeneralAttributes.iTraceFilePrefix.Copy(newSettings.iTraceFilePrefix);
       
   433     iGeneralAttributes.iTimedSamplingPeriod = newSettings.iTimedSamplingPeriod;
       
   434 
       
   435     // check if debug output selected no check of
       
   436     if(newSettings.iTraceOutput.CompareF(KTraceDebugOutput) != 0)
       
   437         {
       
   438         // Check save file drive sanity
       
   439         if(CheckTraceLocationSanityL(newSettings, quietCheck))
       
   440             {
       
   441             // save the new location
       
   442             iGeneralAttributes.iSaveFileDrive.Copy(newSettings.iSaveFileDrive);
       
   443             }
       
   444         }
       
   445     
       
   446     // saves the general settings to profiler engine
       
   447     SaveGeneralSettingsL();   
       
   448     
       
   449     // make sure that the title of the application is correct
       
   450     CEikStatusPane* sp = iEnv->AppUiFactory()->StatusPane();
       
   451     CAknTitlePane* tp = static_cast<CAknTitlePane*>( sp->ControlL( TUid::Uid( EEikStatusPaneUidTitle ) ) );
       
   452     tp->SetTextL(KAppName);
       
   453     
       
   454     return returnValue;
       
   455     }
       
   456 
       
   457 
       
   458 TPtrC CProfilerGuiModel::GetWriterInfoNoteL(const TDesC& aNote)
       
   459 	{
       
   460     TBuf<256> buf;
       
   461     TBuf<128> descBuf;
       
   462     
       
   463     buf.Zero();
       
   464 
       
   465     // get active profiler since it 
       
   466     RProfiler::GetActiveWriter(buf);
       
   467     
       
   468 	if(iGeneralAttributes.iTraceOutput.CompareF(KTraceOutput) == 0)
       
   469 		{
       
   470 		if(iState == MProfilerStatusObserver::EInitializing ||
       
   471                 iState == MProfilerStatusObserver::ERestarting )
       
   472 			{
       
   473 			descBuf.Zero();
       
   474 			// set the additional note if available
       
   475 			buf.Copy(aNote); 
       
   476 			RProfiler::GetFileName(descBuf);
       
   477 			buf.Append(_L("Writing to "));
       
   478 			buf.Append(descBuf);
       
   479 			}
       
   480 		else if(iState == MProfilerStatusObserver::EIdle ||
       
   481 		        iState == MProfilerStatusObserver::EStopping ||
       
   482 		        iState == MProfilerStatusObserver::ERunning )
       
   483 			{
       
   484 			descBuf.Zero();
       
   485             // set the additional note if available
       
   486             buf.Copy(aNote); 
       
   487             RProfiler::GetFileName(descBuf);
       
   488 			buf.Append(_L("Wrote trace data to "));
       
   489 			buf.Append(descBuf);
       
   490 			}
       
   491 		else
       
   492 		    {
       
   493 	          buf.Copy(KNullDesC);
       
   494 		    }
       
   495 		}
       
   496 	else if(iGeneralAttributes.iTraceOutput.CompareF(KTraceDebugOutput) == 0)
       
   497 		{
       
   498 		if(iState == MProfilerStatusObserver::EInitializing ||
       
   499                 iState == MProfilerStatusObserver::ERestarting )
       
   500 			{
       
   501 			buf.Copy(_L("Writing to debug output..."));
       
   502 			}
       
   503 		else if( iState == MProfilerStatusObserver::EIdle || 
       
   504 	             iState == MProfilerStatusObserver::EStopping ||
       
   505 		         iState == MProfilerStatusObserver::ERunning )
       
   506 			{
       
   507 			buf.Copy(_L("Wrote trace data to debug output"));
       
   508 			}
       
   509 		else
       
   510 		    {
       
   511 		    buf.Copy(KNullDesC);
       
   512 		    }
       
   513 		}
       
   514 	else
       
   515 	    {
       
   516 	    // should not reach this point
       
   517         buf.Copy(KNullDesC);
       
   518 	    }
       
   519     
       
   520     return TPtrC(buf);	
       
   521     }
       
   522 
       
   523 // --------------------------------------------------------------------------------------------
       
   524 
       
   525 TInt CProfilerGuiModel::EditSamplerL(TSamplerAttributes& aItem)
       
   526     {
       
   527     // edit sampler specific settings i.e. attributes
       
   528     TSamplerAttributes& newSettings = aItem;
       
   529     TInt indexToReplace(iMainContainer->CurrentListBoxItemIndex());
       
   530     
       
   531     // create a new settings editor dialog
       
   532     CProfilerSamplerSettingsViewDlg* dlg = CProfilerSamplerSettingsViewDlg::NewL(newSettings);
       
   533     TInt returnValue = dlg->ExecuteLD(R_PROFILER_SAMPLER_SETTINGS_DIALOG);
       
   534     
       
   535     // save settings
       
   536     aItem = newSettings;
       
   537 
       
   538     // replace the old attribute container with saved values
       
   539     iSamplerItemList->Delete(indexToReplace);
       
   540     iSamplerItemList->InsertL(indexToReplace, newSettings);
       
   541     
       
   542     // save the settings to sampler item
       
   543     RProfiler::SetSamplerAttributes(newSettings);
       
   544     
       
   545     // make sure that the title of the application is correct
       
   546     CEikStatusPane* sp = iEnv->AppUiFactory()->StatusPane();
       
   547     CAknTitlePane* tp = static_cast<CAknTitlePane*>( sp->ControlL( TUid::Uid( EEikStatusPaneUidTitle ) ) );
       
   548     tp->SetTextL(KAppName);
       
   549 
       
   550     return returnValue;
       
   551     }
       
   552 
       
   553 
       
   554 void CProfilerGuiModel::SamplerInfoL(TSamplerAttributes& aItem)
       
   555     {
       
   556     TBuf<64> header;
       
   557     TBuf<256> info;
       
   558     
       
   559     _LIT(KSamplerStr, "Sampler Info");
       
   560     
       
   561 //    CnvUtfConverter::ConvertToUnicodeFromUtf8(header, aItem.iName);
       
   562     
       
   563     header.Append(KSamplerStr);
       
   564 
       
   565     CnvUtfConverter::ConvertToUnicodeFromUtf8(info, aItem.iDescription);
       
   566 
       
   567     CAknMessageQueryDialog* dialog = new(ELeave) CAknMessageQueryDialog;
       
   568     //dialog->ExecuteLD(info);
       
   569     dialog->PrepareLC( R_PROFILER_GUI_EMPTY_ABOUT_DIALOG );
       
   570     dialog->SetHeaderText(header);
       
   571     dialog->SetMessageTextL(info);
       
   572     dialog->RunLD(); 
       
   573 
       
   574     RefreshViewL(EFalse);
       
   575     }
       
   576 
       
   577 // --------------------------------------------------------------------------------------------
       
   578 
       
   579 void CProfilerGuiModel::StartAllSamplerItemsL(TProfilingMode aProfilingMode)
       
   580     {
       
   581     TBuf<256> activeWriterDes;
       
   582     TBuf8<256> writer8;
       
   583     
       
   584     iState = MProfilerStatusObserver::EInitializing;
       
   585     
       
   586     RProfiler::TProfilingMode profilingMode = aProfilingMode == EProfilingModeTimed ? RProfiler::EProfilingModeTimed : RProfiler::EProfilingModeNormal;
       
   587     
       
   588     // update status pane to correspond the initializing mode
       
   589     // prevent the control of sampler specific settings during the trace 
       
   590     iMainContainer->SetDimmed(ETrue);
       
   591     
       
   592     // update status pane
       
   593     iMainView->UpdateStatusPaneL(iState);
       
   594     
       
   595     // refresh view
       
   596     RefreshViewL();
       
   597     
       
   598     // try to start profiling process through client-server interface
       
   599     if(RProfiler::StartSampling(profilingMode) == KErrNotFound)
       
   600         {
       
   601         
       
   602         // profiler stopped (e.g. from eshell) and must be restarted 
       
   603         LaunchProfilerEngineL();
       
   604         
       
   605         // set general attributes
       
   606         SaveGeneralSettingsL();
       
   607         
       
   608         // set sampler attributes
       
   609         for(TInt i(0);i<iSamplerAttributes->Count();i++)
       
   610             {
       
   611             // set the attributes for each sampler loaded in the UI
       
   612             RProfiler::SetSamplerAttributes(iSamplerAttributes->At(i));
       
   613             }
       
   614         
       
   615         // try to launch sampling again
       
   616         RProfiler::StartSampling(profilingMode);
       
   617         }
       
   618     
       
   619     // get selected writer
       
   620     RProfiler::GetActiveWriter(activeWriterDes);
       
   621     CnvUtfConverter::ConvertFromUnicodeToUtf8(writer8, activeWriterDes);
       
   622     
       
   623     // check that output mode has not changed for a problem with trace file name
       
   624     // problem cases:
       
   625     // - trace file name and/or path false
       
   626     // - disk full, cannot write to given location
       
   627     // - false drive, e.g. x:
       
   628     if(writer8.CompareF(iGeneralAttributes.iTraceOutput) != 0)
       
   629         {
       
   630         // save change also to general attributes
       
   631         iGeneralAttributes.iTraceOutput.Copy(writer8);
       
   632         // show an info popup showing the logging method
       
   633         iMainContainer->ShowWriterInfoPopupL(KWarningNote);
       
   634         }
       
   635     else
       
   636         {
       
   637         // show an info popup showing the logging method
       
   638         iMainContainer->ShowWriterInfoPopupL(KNullDesC);
       
   639         }
       
   640     
       
   641     // update the view
       
   642     RefreshViewL();    
       
   643     }
       
   644     
       
   645 // --------------------------------------------------------------------------------------------
       
   646 
       
   647 void CProfilerGuiModel::DeleteAllSamplerItemsL()
       
   648     {
       
   649     DeleteAllSamplerItems();
       
   650     
       
   651     RefreshViewL();
       
   652     }
       
   653 
       
   654 // --------------------------------------------------------------------------------------------
       
   655 
       
   656 void CProfilerGuiModel::StopAllSamplerItemsL()
       
   657     {
       
   658     // show an info popup showing the logging method
       
   659     iMainContainer->ShowWriterInfoPopupL(KNullDesC);
       
   660     
       
   661     // Stop profiling process through CS session
       
   662     RProfiler::StopSampling();
       
   663     
       
   664     RefreshViewL();
       
   665     }
       
   666     
       
   667 // --------------------------------------------------------------------------------------------
       
   668 
       
   669 void CProfilerGuiModel::RefreshViewL(TBool aClearSelection)
       
   670     {
       
   671     // refresh the main list view
       
   672     if (iMainContainer && iDrawState == EDrawStateMain && iMainContainer->ListBox())
       
   673         {
       
   674         // clear selections if any
       
   675         iMainContainer->ListBox()->ClearSelection();
       
   676         
       
   677         // set item index to 0
       
   678         if (aClearSelection)
       
   679             iMainContainer->ListBox()->SetCurrentItemIndex(0); 
       
   680         
       
   681         // set text items
       
   682         iMainContainer->SetListBoxTextArrayL(GenerateListBoxItemTextArrayL());
       
   683         }
       
   684     }
       
   685         
       
   686 // --------------------------------------------------------------------------------------------
       
   687 
       
   688 void CProfilerGuiModel::AppendToSamplerItemListL(TSamplerAttributes& aItem)
       
   689     {
       
   690     // append single sampler item into sampler item list
       
   691     if (iSamplerItemList)
       
   692         iSamplerItemList->AppendL( aItem );
       
   693     else
       
   694         User::Leave(KErrNotReady);
       
   695     }
       
   696 
       
   697 // --------------------------------------------------------------------------------------------
       
   698 
       
   699 void CProfilerGuiModel::DisableOrEnableFromSamplerItemListL(TInt aIndex)
       
   700     {
       
   701     if (iSamplerItemList)
       
   702         {
       
   703         if (iSamplerItemList->Count() > aIndex && aIndex >= 0)
       
   704             {
       
   705             TSamplerAttributes& attr = iSamplerItemList->At(aIndex);
       
   706             if (attr.iEnabled)
       
   707                 {
       
   708                 attr.iEnabled = EFalse;
       
   709                 }
       
   710             else if (!attr.iEnabled)
       
   711                 {
       
   712                 attr.iEnabled = ETrue;
       
   713                 }
       
   714             // save settings to engine
       
   715             RProfiler::SetSamplerAttributes(attr);
       
   716             }
       
   717         else
       
   718             User::Leave(KErrNotFound);
       
   719         }
       
   720     else
       
   721         {
       
   722         User::Leave(KErrNotReady);
       
   723         }
       
   724 
       
   725     }
       
   726 
       
   727 // --------------------------------------------------------------------------------------------
       
   728 
       
   729 TInt CProfilerGuiModel::SamplerItemCount() const
       
   730     {
       
   731     TInt count(0);
       
   732     
       
   733     if (iSamplerItemList)
       
   734         count = iSamplerItemList->Count();
       
   735     
       
   736     return count;
       
   737     }
       
   738     
       
   739 // --------------------------------------------------------------------------------------------
       
   740 
       
   741 void CProfilerGuiModel::DeleteAllSamplerItems()
       
   742     {
       
   743     // reset sampler array
       
   744     iSamplerItemList->Reset();    
       
   745     }  
       
   746           
       
   747 // --------------------------------------------------------------------------------------------
       
   748 
       
   749 CDesCArray* CProfilerGuiModel::GenerateListBoxItemTextArrayL()
       
   750     {
       
   751     CDesCArray* textArray = new(ELeave) CDesCArrayFlat(64);
       
   752     CleanupStack::PushL(textArray);
       
   753 
       
   754     _LIT(KEntryTemplateRedIcon,   "1\t%S\t\t");
       
   755     _LIT(KEntryTemplateGreenIcon, "2\t%S\t\t");
       
   756     
       
   757     TSamplerAttributes attr;
       
   758     TInt itemCount(SamplerItemCount()); 
       
   759     for (TInt i(0); i<itemCount; i++)
       
   760         {
       
   761         // add description from each entry
       
   762         TBuf<512> textEntry;
       
   763         TBuf<512> description; 
       
   764 
       
   765         attr = iSamplerItemList->At(i);
       
   766         
       
   767         description.Copy(attr.iName);
       
   768         
       
   769         if (attr.iEnabled)
       
   770             textEntry.Format(KEntryTemplateGreenIcon, &description);
       
   771         else
       
   772             textEntry.Format(KEntryTemplateRedIcon, &description);
       
   773         
       
   774         textArray->AppendL(textEntry);
       
   775         }    
       
   776     
       
   777     CleanupStack::Pop(); //textArray
       
   778     return textArray;
       
   779     }
       
   780 
       
   781 // --------------------------------------------------------------------------------------------
       
   782 
       
   783 TBool CProfilerGuiModel::GetSelectedItemEnabled()
       
   784     {
       
   785     TInt currentItemIndex = iMainContainer->CurrentListBoxItemIndex();
       
   786     
       
   787     if (SamplerItemCount() > currentItemIndex && currentItemIndex >= 0)
       
   788         {
       
   789         return iSamplerItemList->At(currentItemIndex).iEnabled;
       
   790         }
       
   791     return EFalse;
       
   792     }
       
   793 
       
   794 // --------------------------------------------------------------------------------------------
       
   795 
       
   796 TBool CProfilerGuiModel::GetSelectedItemHasSettings()
       
   797     {
       
   798     TInt currentItemIndex = iMainContainer->CurrentListBoxItemIndex();
       
   799     
       
   800     if (SamplerItemCount() > currentItemIndex && currentItemIndex >= 0)
       
   801         {
       
   802         if(iSamplerItemList->At(currentItemIndex).iItemCount != 0 || 
       
   803                 iSamplerItemList->At(currentItemIndex).iSampleRate != KErrNotFound)
       
   804             {
       
   805             return ETrue;
       
   806             }
       
   807         else
       
   808             {
       
   809             return EFalse;
       
   810             }
       
   811         }
       
   812     return EFalse;
       
   813     }
       
   814 
       
   815 // --------------------------------------------------------------------------------------------
       
   816 
       
   817 TBool CProfilerGuiModel::GetSelectedItemHidden()
       
   818     {
       
   819     TInt currentItemIndex = iMainContainer->CurrentListBoxItemIndex();
       
   820     
       
   821     if (SamplerItemCount() > currentItemIndex && currentItemIndex >= 0)
       
   822         {
       
   823         if(iSamplerItemList->At(currentItemIndex).iIsHidden)
       
   824             {
       
   825             return ETrue;
       
   826             }
       
   827         else
       
   828             {
       
   829             return EFalse;
       
   830             }
       
   831         }
       
   832     return EFalse;
       
   833     }
       
   834 
       
   835 // --------------------------------------------------------------------------------------------
       
   836 
       
   837 TInt CProfilerGuiModel::EditSelectedSamplerL(TInt index)
       
   838     {
       
   839     TSamplerAttributes sampler;
       
   840     if(SamplerItemCount() > index && index >= 0)
       
   841         {
       
   842         sampler = iSamplerItemList->At(index);
       
   843         return EditSamplerL(sampler);
       
   844         }
       
   845     return KErrNone;
       
   846     }
       
   847 
       
   848 // --------------------------------------------------------------------------------------------
       
   849 
       
   850 void CProfilerGuiModel::SelectedSamplerInfoL(TInt index)
       
   851     {
       
   852     if(SamplerItemCount() > index && index >= 0)
       
   853         {
       
   854         SamplerInfoL(iSamplerItemList->At(index));
       
   855         }
       
   856     }
       
   857 
       
   858 // --------------------------------------------------------------------------------------------
       
   859 
       
   860 void CProfilerGuiModel::ShowItemActionMenuL()
       
   861     {
       
   862     TInt currentItemIndex = iMainContainer->CurrentListBoxItemIndex();
       
   863     
       
   864     if (SamplerItemCount() > currentItemIndex && currentItemIndex >= 0)
       
   865         {
       
   866         // show a query dialog    
       
   867         TInt queryIndex(0);
       
   868         CAknListQueryDialog* listQueryDlg = new(ELeave) CAknListQueryDialog(&queryIndex);
       
   869         TInt resource = 0;
       
   870         TSamplerAttributes sampler = iSamplerItemList->At(currentItemIndex);
       
   871         
       
   872         // check if sampler enabled
       
   873         if(sampler.iEnabled)
       
   874         	{
       
   875         	resource = R_ITEM_ACTION_QUERY_ENABLED;
       
   876             }
       
   877         else
       
   878         	{
       
   879         	resource = R_ITEM_ACTION_QUERY_DISABLED;
       
   880         	}
       
   881         
       
   882         // check if sampler plugin hidden => don't allow starting and stopping of sampling
       
   883         if(sampler.iIsHidden)
       
   884         	{
       
   885         	resource = R_ITEM_ACTION_QUERY_HIDDEN_START_STOP;
       
   886         	}
       
   887         else if(( sampler.iItemCount == 0 && 
       
   888         		sampler.iSampleRate == KErrNotFound ) && 
       
   889         		sampler.iEnabled )
       
   890             {
       
   891             resource = R_ITEM_ACTION_QUERY_HIDDEN_SETTINGS_ENABLED;
       
   892             }
       
   893         else if(( sampler.iItemCount == 0 && 
       
   894                 sampler.iSampleRate == KErrNotFound ) && 
       
   895                 !sampler.iEnabled )
       
   896             {
       
   897             resource = R_ITEM_ACTION_QUERY_HIDDEN_SETTINGS_DISABLED;
       
   898             }
       
   899         
       
   900         // execute query dialog with the given resource
       
   901         if (listQueryDlg->ExecuteLD(resource))
       
   902             {
       
   903             if(!sampler.iIsHidden)
       
   904             	{
       
   905             	if(sampler.iItemCount != 0 || 
       
   906             	        sampler.iSampleRate != KErrNotFound )
       
   907             	    {
       
   908                     // enable/disable sampler
       
   909                     if (queryIndex == EItemActionMenuTypeEnable)
       
   910                         {
       
   911                         DisableOrEnableSelectedOrHighlightedItemsL();
       
   912                         }
       
   913                     // edit sampler settings
       
   914                     else if (queryIndex == EItemActionMenuTypeEditSettings - 1)
       
   915                         {
       
   916                         EditSamplerL(sampler);
       
   917                         }
       
   918                     // info
       
   919                     else if (queryIndex == EItemActionMenuTypeInfo - 1)
       
   920                         {
       
   921                         SamplerInfoL(sampler);
       
   922                         }
       
   923             	    }
       
   924             	else
       
   925             	    {
       
   926                     // special case, use raw query indexes
       
   927                      if (queryIndex == 0)
       
   928                          {
       
   929                          DisableOrEnableSelectedOrHighlightedItemsL();
       
   930                          }
       
   931                      else if (queryIndex == 1)
       
   932                          {
       
   933                          SamplerInfoL(sampler);
       
   934                          }
       
   935             	    }
       
   936             	}
       
   937             else
       
   938 				{
       
   939 				if(sampler.iItemCount != 0 || 
       
   940 				        sampler.iSampleRate != KErrNotFound)
       
   941 					{
       
   942 					// special case, use raw query indexes
       
   943 					// edit sampler settings
       
   944 					if (queryIndex == 0)
       
   945 						{
       
   946 						EditSamplerL(sampler);
       
   947 						}
       
   948 					// sampler info
       
   949 					else if (queryIndex == 1)
       
   950 						{
       
   951 						SamplerInfoL(sampler);
       
   952 						}
       
   953 					}
       
   954 				else
       
   955 					{
       
   956 					// only sampler info available if hidden and no setting items to be set
       
   957 					if (queryIndex == 0)
       
   958 						{
       
   959 						SamplerInfoL(sampler);
       
   960 						}
       
   961 					
       
   962 					}
       
   963 				}
       
   964             }
       
   965         }
       
   966     }
       
   967 
       
   968 void CProfilerGuiModel::DisableOrEnableSelectedOrHighlightedItemsL()
       
   969     {
       
   970     const CArrayFix<TInt>* selectionIndexes = iMainContainer->ListBoxSelectionIndexes();
       
   971 
       
   972     // by default use selected items
       
   973     if (selectionIndexes && selectionIndexes->Count() > 0)
       
   974         {
       
   975         TInt ref(0);
       
   976         TKeyArrayFix key(0, ECmpTUint16);
       
   977         TInt index(0);
       
   978 
       
   979         for (TInt i=0; i<SamplerItemCount(); i++)
       
   980             {
       
   981             ref = i;
       
   982 
       
   983             if (selectionIndexes->Find(ref, key, index) == 0)  
       
   984                 {
       
   985                 DisableOrEnableFromSamplerItemListL(i);
       
   986                 }
       
   987             }
       
   988         
       
   989         RefreshViewL(EFalse);
       
   990         }
       
   991     
       
   992     // or if none selected, use the current item index
       
   993     else
       
   994         {
       
   995         TInt currentItemIndex = iMainContainer->CurrentListBoxItemIndex();
       
   996         
       
   997         if (SamplerItemCount() > currentItemIndex && currentItemIndex >= 0)
       
   998             {
       
   999             DisableOrEnableFromSamplerItemListL(currentItemIndex);
       
  1000             
       
  1001             RefreshViewL(EFalse);
       
  1002             }
       
  1003         }  
       
  1004     }
       
  1005 
       
  1006 void CProfilerGuiModel::LoadPluginsL()
       
  1007 	{
       
  1008     // get samplers from Profiler Engine (client-server session) 
       
  1009 	// and add the to the samplers list for the first time
       
  1010     LOGTEXT(_L("CProfilerGuiModel::LoadPlugins - get sampler plugins"));
       
  1011     TInt err = RProfiler::GetSamplerAttributes(*iSamplerAttributes);
       
  1012     
       
  1013     // check if engine provided a list of samplers
       
  1014     if( err != KErrNone )
       
  1015         {
       
  1016         // could not get samplers from engine
       
  1017         LOGTEXT(_L("CProfilerGuiModel::LoadPlugins - failed to connect engine"));
       
  1018         }
       
  1019     else
       
  1020         {
       
  1021         LOGTEXT(_L("CProfilerGuiModel::LoadPlugins - adding new samplers into view"));
       
  1022         AddNewSamplersL(*iSamplerAttributes);
       
  1023         }
       
  1024     
       
  1025     LOGTEXT(_L("CProfilerGuiModel::LoadPlugins - exit"));
       
  1026 	}
       
  1027 
       
  1028 // --------------------------------------------------------------------------------------------
       
  1029 
       
  1030 TInt CProfilerGuiModel::LoadGeneralSettingsL()
       
  1031     {
       
  1032     // local variable for getting saved settings from profiler engine
       
  1033     TGeneralAttributes generalAttr;
       
  1034     TInt err(KErrNone);
       
  1035     
       
  1036     // before loading saved settings (from settings file) set the default values 
       
  1037     iGeneralAttributes.iTraceOutput.Copy(KTraceOutput);
       
  1038     iGeneralAttributes.iTraceFilePrefix.Copy(KProfilerDefaultPrefix);
       
  1039     iGeneralAttributes.iSaveFileDrive.Copy(KProfilerDefaultDrive);
       
  1040     iGeneralAttributes.iTimedSamplingPeriod = KDefaultTimedSamplingPeriod;
       
  1041     
       
  1042     // request to 
       
  1043     err = RProfiler::GetGeneralAttributes(generalAttr);
       
  1044     
       
  1045     // check that request succesfull
       
  1046     if( err != KErrNone )
       
  1047         {
       
  1048         // could not connect profiler engine, use 
       
  1049         return err;
       
  1050         }
       
  1051     
       
  1052     // check if saved settings different than the default
       
  1053     if(generalAttr.iTraceOutput.MatchF(iGeneralAttributes.iTraceOutput) == KErrNotFound)
       
  1054         {
       
  1055         iGeneralAttributes.iTraceOutput.Copy(generalAttr.iTraceOutput);
       
  1056         }
       
  1057 
       
  1058     if(generalAttr.iTraceFilePrefix.MatchF(iGeneralAttributes.iTraceFilePrefix) == KErrNotFound)
       
  1059         {
       
  1060         iGeneralAttributes.iTraceFilePrefix.Copy(generalAttr.iTraceFilePrefix);
       
  1061         }
       
  1062 
       
  1063     if(generalAttr.iSaveFileDrive.MatchF(iGeneralAttributes.iSaveFileDrive) == KErrNotFound)
       
  1064         {
       
  1065         iGeneralAttributes.iSaveFileDrive.Copy(generalAttr.iSaveFileDrive);
       
  1066         }
       
  1067     
       
  1068     if( generalAttr.iTimedSamplingPeriod > 0)
       
  1069         {
       
  1070         iGeneralAttributes.iTimedSamplingPeriod = generalAttr.iTimedSamplingPeriod;
       
  1071         }
       
  1072     
       
  1073     return err;
       
  1074     }
       
  1075 
       
  1076 // --------------------------------------------------------------------------------------------
       
  1077 
       
  1078 void CProfilerGuiModel::SaveGeneralSettingsL()
       
  1079     {
       
  1080     TInt err(KErrNone);
       
  1081     
       
  1082     // save general attributes to Profiler Engine
       
  1083     err = RProfiler::SetGeneralAttributes(iGeneralAttributes);
       
  1084     
       
  1085     // check if save failed
       
  1086     if(err == KErrNotFound)
       
  1087         {
       
  1088         // profiler stopped (e.g. from eshell) and must be restarted 
       
  1089         LaunchProfilerEngineL();
       
  1090         
       
  1091         err = RProfiler::SetGeneralAttributes(iGeneralAttributes);
       
  1092         if(err != KErrNone)
       
  1093             {
       
  1094             // leave no use to continue
       
  1095             User::Leave(err);
       
  1096             }
       
  1097         }
       
  1098     }
       
  1099 
       
  1100 void CProfilerGuiModel::SetMainView(CProfilerGuiMainView* aMainView)
       
  1101     {
       
  1102     iMainView = aMainView;
       
  1103     }
       
  1104 
       
  1105 // --------------------------------------------------------------------------------------------
       
  1106 
       
  1107 void CProfilerGuiModel::HandleProfilerStatusChange( KProfilerStatus aStatus )
       
  1108     {
       
  1109     iState = aStatus;
       
  1110     if( aStatus == EIdle )
       
  1111         {
       
  1112         iMainContainer->ShowWriterInfoPopupL(KNullDesC);
       
  1113         // set plugin list back to enabled
       
  1114         iMainContainer->SetDimmed(EFalse);
       
  1115         }
       
  1116     TRAP_IGNORE(iMainView->UpdateStatusPaneL(aStatus));
       
  1117     }
       
  1118 
       
  1119 void CProfilerGuiModel::HandleProfilerErrorL( TInt aError )
       
  1120     {
       
  1121     TBuf<64> errorMsg;
       
  1122     _LIT(KErrorMessage, "Error: ");
       
  1123     _LIT(KNoMemory, "Cannot write to file, check settings");
       
  1124     
       
  1125     errorMsg.Copy(KErrorMessage);
       
  1126     // message from pwr sampler
       
  1127     if( aError < -1000 )
       
  1128         {
       
  1129         errorMsg.Append(_L("Stop other power measurement tools!"));
       
  1130         }
       
  1131     else if( aError == KErrAlreadyExists || aError == 11 )
       
  1132         {
       
  1133         errorMsg.Append(_L("Close old Profiler before start!"));
       
  1134         }
       
  1135     else if( aError == KErrNotReady )
       
  1136         {
       
  1137         errorMsg.Append(_L("Memory card removed, failed to write!"));
       
  1138         }
       
  1139     else if( aError == KErrPathNotFound )
       
  1140         {
       
  1141         errorMsg.Append(_L("Given trace data location does not exist"));
       
  1142         }
       
  1143     else
       
  1144         {
       
  1145         if( aError == KErrNoMemory || 
       
  1146                 aError == KErrOverflow || 
       
  1147                 aError == KErrDirFull || 
       
  1148                 aError == KErrDiskFull || 
       
  1149                 aError == KErrNotReady )
       
  1150             {
       
  1151             errorMsg.Append(KNoMemory);
       
  1152             }
       
  1153         else
       
  1154             {
       
  1155             errorMsg.Append(_L("code: "));
       
  1156             errorMsg.AppendNum(aError);
       
  1157             }
       
  1158         }
       
  1159     // simply show an error note
       
  1160     CAknErrorNote* note = new(ELeave) CAknErrorNote();
       
  1161     note->ExecuteLD(errorMsg);
       
  1162     
       
  1163     // set state idle in all error cases
       
  1164     iState = MProfilerStatusObserver::EIdle;
       
  1165     
       
  1166     // update status pane
       
  1167     iMainView->UpdateStatusPaneL(iState);
       
  1168     
       
  1169     // set plugin list back to enabled
       
  1170     iMainContainer->SetDimmed(EFalse);
       
  1171     
       
  1172     // refresh and set menus etc. in correct state
       
  1173     RefreshViewL();
       
  1174     }
       
  1175 
       
  1176 // End of File