piprofilerui/ui/hb/src/piprofilerengineprivate.cpp
changeset 51 b048e15729d6
parent 44 5db69f4c3d06
child 52 36d60d12b4af
equal deleted inserted replaced
44:5db69f4c3d06 51:b048e15729d6
     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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32std.h>
       
    20 #include <qstring.h>
       
    21 #include <qlist.h>
       
    22 #include <qstringlist.h>
       
    23 #include <f32file.h>
       
    24 #include <xqconversions.h>
       
    25 #include <utf.h>
       
    26 #include <bautils.h>
       
    27 #include <sysutil.h>
       
    28 #include "piprofilerengineprivate.h"
       
    29 #include <piprofiler/ProfilerSession.h>
       
    30 #include "pluginattributes.h"
       
    31 #include "generalattributes.h"
       
    32 #include "piprofilerengine.h"
       
    33 
       
    34 // literals for default general setting values
       
    35 _LIT8(KTraceOutput, "file_system");
       
    36 _LIT8(KTraceDebugOutput, "debug_output");
       
    37 _LIT8(KProfilerDefaultDrive, "E:\\data");
       
    38 _LIT8(KProfilerDefaultPrefix, "Profiler_#");
       
    39 _LIT(KProfilerEngineExe, "PIProfilerEngine.exe");
       
    40 const TInt KProfilerDefaultTimedSamplingPeriod = 60; // Sampling time in seconds  
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 
       
    44 PIProfilerEnginePrivate::PIProfilerEnginePrivate(PIProfilerEngine *aEngine) :
       
    45     iPublic(aEngine), iSamplerAttributes(0), iStatusChecker(0), iLeaveProfilingOnAfterClosing(
       
    46         EFalse)
       
    47 {
       
    48 
       
    49 }
       
    50 
       
    51 // ---------------------------------------------------------------------------
       
    52 
       
    53 PIProfilerEnginePrivate::~PIProfilerEnginePrivate()
       
    54 {
       
    55 
       
    56     // remove profiler client
       
    57     RProfiler::RemoveClient();
       
    58     // Terminate engine in case it is running.
       
    59     if (iLeaveProfilingOnAfterClosing == EFalse) {
       
    60         terminateEngine();
       
    61     }
       
    62 
       
    63     // delete sampler attributes.
       
    64     if (iSamplerAttributes) {
       
    65         delete iSamplerAttributes;
       
    66         iSamplerAttributes = 0;
       
    67     }
       
    68 
       
    69     if (iStatusChecker) {
       
    70         iStatusChecker->Cancel();
       
    71         delete iStatusChecker;
       
    72         iStatusChecker = NULL;
       
    73     }
       
    74 }
       
    75 
       
    76 // ---------------------------------------------------------------------------
       
    77 
       
    78 bool PIProfilerEnginePrivate::Init()
       
    79 {
       
    80     TRAPD(error, this->LaunchEngineL());
       
    81     if (error != KErrNone) {
       
    82         return false;
       
    83     }
       
    84 
       
    85     // initialize attribute arrays
       
    86 
       
    87     TRAP(error, iSamplerAttributes = new (ELeave) CArrayFixFlat<TSamplerAttributes> (20)); // max sampler count is 20
       
    88     if (error != KErrNone) {
       
    89         return false;
       
    90     }
       
    91 
       
    92     // engine status checker
       
    93     TRAP(error, iStatusChecker = CProfilerEngineStatusChecker::NewL());
       
    94     if (error != KErrNone) {
       
    95         return false;
       
    96     }    
       
    97 
       
    98     iStatusChecker->SetObserver(this);
       
    99 
       
   100     TRAP(error, LoadGeneralSettingsL());
       
   101     if (error != KErrNone) {
       
   102         return false;
       
   103     }
       
   104 
       
   105     return true;
       
   106 }
       
   107 
       
   108 // ---------------------------------------------------------------------------
       
   109 
       
   110 
       
   111 int PIProfilerEnginePrivate::LaunchEngineL()
       
   112 {
       
   113 
       
   114     TRequestStatus stat = KRequestPending;
       
   115     RProcess proc;
       
   116 
       
   117     TInt err(KErrNone);
       
   118 
       
   119     // check if process exists
       
   120     err = FindProcessL(proc);
       
   121 
       
   122     // check if already exists and don't start a new eshell profiling
       
   123     if (err == KErrNotFound) {
       
   124         // try create new process
       
   125         err = proc.Create(KProfilerEngineExe, _L(""));
       
   126 
       
   127         // check if RProcess::Create() succeeded
       
   128         if (err == KErrNone) {
       
   129             // Trigger rendezvous on the supplied TRequestStatus object
       
   130             proc.Rendezvous(stat);
       
   131 
       
   132             // kick off the engine process
       
   133             proc.Resume();
       
   134 
       
   135             // wait for the constructor to complete 
       
   136             User::WaitForRequest(stat);
       
   137 
       
   138             // just lose the handle
       
   139             proc.Close();
       
   140         }
       
   141     }
       
   142 
       
   143     // Increase the client reference count in server:
       
   144     AttachClient();
       
   145 }
       
   146 
       
   147 // ---------------------------------------------------------------------------
       
   148 
       
   149 void PIProfilerEnginePrivate::AttachClient()
       
   150 {
       
   151     RProfiler::AttachClient();
       
   152 }
       
   153 // ---------------------------------------------------------------------------
       
   154 
       
   155 int PIProfilerEnginePrivate::FindProcessL(RProcess& aProc)
       
   156 {
       
   157     TProcessId engId;
       
   158     TFindProcess procName;
       
   159     procName.Find(_L("PIProfilerEngine.exe*"));
       
   160     TFullName aResult;
       
   161     TFullName aResult2;
       
   162     TInt err(KErrNone);
       
   163 
       
   164     // find the first appearance
       
   165     err = procName.Next(aResult);
       
   166     if (err != KErrNone) {
       
   167         // did not find any engine process
       
   168         return err;
       
   169     }
       
   170     else {
       
   171         err = aProc.Open(procName);
       
   172         if (err == KErrNone) {
       
   173             if (aProc.ExitCategory().Length() > 0) {
       
   174                 aProc.Close();
       
   175                 // process already exited => create a new one
       
   176                 return KErrNotFound;
       
   177             }
       
   178             aProc.Close();
       
   179         }
       
   180     }
       
   181 
       
   182     // check now if a second appearance exists in process list, 
       
   183     // i.e. engine started from eshell => two engine processes appear in normal case
       
   184     procName.Next(aResult2);
       
   185 
       
   186     // check if aResult2 contained the second appearance of profiler engine
       
   187     if(aResult2.CompareF(aResult) > 0)
       
   188         {
       
   189         // other process found, i.e. right process to communicate with, in case started from eshell
       
   190         err = aProc.Open(procName);
       
   191         if(err == KErrNone)
       
   192             {
       
   193             if(aProc.ExitCategory().Length() > 0)
       
   194                 {
       
   195                 // process already exited => create a new one
       
   196                 return KErrNotFound;
       
   197                 }
       
   198             aProc.Close();
       
   199             }
       
   200         }
       
   201 
       
   202     return err;
       
   203 }
       
   204 
       
   205 // ---------------------------------------------------------------------------
       
   206 
       
   207 void PIProfilerEnginePrivate::terminateEngine()
       
   208 {
       
   209     // exit profiler engine 
       
   210     RProfiler::ExitProfiler();
       
   211 }
       
   212 // ---------------------------------------------------------------------------
       
   213 
       
   214 void PIProfilerEnginePrivate::NotifyUIReady()
       
   215 {
       
   216     // load initial plugins
       
   217     loadPlugins();
       
   218 
       
   219     // get the initial state
       
   220     int initialState = iStatusChecker->GetInitialState();
       
   221     if (initialState == ERunning) {
       
   222         HandleProfilerStatusChange(ERunning);
       
   223     }
       
   224 }
       
   225 
       
   226 // ---------------------------------------------------------------------------
       
   227 
       
   228 
       
   229 void PIProfilerEnginePrivate::loadPlugins()
       
   230 {
       
   231     // get samplers from Profiler Engine (client-server session) 
       
   232     // and add the to the samplers list for the first time
       
   233     LOGTEXT(_L("CProfilerGuiModel::LoadPlugins - get sampler plugins"));
       
   234 
       
   235     TInt err = RProfiler::GetSamplerAttributes(*iSamplerAttributes);
       
   236 
       
   237     // check if engine provided a list of samplers
       
   238     if (err != KErrNone) {
       
   239         // could not get samplers from engine
       
   240         LOGTEXT(_L("CProfilerGuiModel::LoadPlugins - failed to connect engine"));
       
   241     }
       
   242     else {
       
   243         LOGTEXT(_L("CProfilerGuiModel::LoadPlugins - adding new samplers into view"));
       
   244         addNewSamplers(*iSamplerAttributes);
       
   245     }LOGTEXT(_L("CProfilerGuiModel::LoadPlugins - exit"));
       
   246 }
       
   247 // ---------------------------------------------------------------------------
       
   248 
       
   249 void PIProfilerEnginePrivate::addNewSamplers(CArrayFixFlat<TSamplerAttributes>& aAttributes)
       
   250 {
       
   251     TSamplerAttributes item;
       
   252 
       
   253     TInt count(aAttributes.Count());
       
   254 
       
   255     // loop the attribute array and insert them into view list
       
   256 
       
   257     QList<PluginAttributes> samplerList;
       
   258 
       
   259     for (TInt i(0); i < count; i++) {
       
   260         // get a TSamplerAttributes from list at a time  
       
   261         item = aAttributes.At(i);
       
   262 
       
   263         PluginAttributes samplerAttributes;
       
   264         convertTSamplerAttributesToPluginAttributes(item, samplerAttributes);
       
   265 
       
   266         samplerList.append(samplerAttributes);
       
   267     }
       
   268 
       
   269     emit iPublic->pluginListUpdated(samplerList);
       
   270 
       
   271 }
       
   272 
       
   273 // ---------------------------------------------------------------------------
       
   274 
       
   275 void PIProfilerEnginePrivate::convertTSamplerAttributesToPluginAttributes(
       
   276     TSamplerAttributes &tSamplerAttributes, PluginAttributes &samplerAttributes)
       
   277 {
       
   278 
       
   279     samplerAttributes.mUid = tSamplerAttributes.iUid;
       
   280     samplerAttributes.mSampleRate = tSamplerAttributes.iSampleRate;
       
   281     samplerAttributes.mEnabled = tSamplerAttributes.iEnabled;
       
   282     samplerAttributes.mIsHidden = tSamplerAttributes.iIsHidden;
       
   283     samplerAttributes.mItemCount = tSamplerAttributes.iItemCount;
       
   284 
       
   285     TBuf16<8> temp8;
       
   286     temp8.Copy(tSamplerAttributes.iShortName);
       
   287     samplerAttributes.mShortName = QString((QChar*) temp8.Ptr(), temp8.Length());
       
   288 
       
   289     TBuf16<64> temp64;
       
   290     temp64.Copy(tSamplerAttributes.iName);
       
   291     samplerAttributes.mName = QString((QChar*) temp64.Ptr(), temp64.Length());
       
   292 
       
   293     TBuf16<256> temp256;
       
   294     temp256.Copy(tSamplerAttributes.iDescription);
       
   295     samplerAttributes.mDescription = QString((QChar*) temp256.Ptr(), temp256.Length());
       
   296 
       
   297     convertTSettingItemToSettingItem(tSamplerAttributes.iSettingItem1,
       
   298         samplerAttributes.mSettingItem1);
       
   299     convertTSettingItemToSettingItem(tSamplerAttributes.iSettingItem2,
       
   300         samplerAttributes.mSettingItem2);
       
   301     convertTSettingItemToSettingItem(tSamplerAttributes.iSettingItem3,
       
   302         samplerAttributes.mSettingItem3);
       
   303     convertTSettingItemToSettingItem(tSamplerAttributes.iSettingItem4,
       
   304         samplerAttributes.mSettingItem4);
       
   305     convertTSettingItemToSettingItem(tSamplerAttributes.iSettingItem5,
       
   306         samplerAttributes.mSettingItem5);
       
   307     convertTSettingItemToSettingItem(tSamplerAttributes.iSettingItem6,
       
   308         samplerAttributes.mSettingItem6);
       
   309 }
       
   310 
       
   311 // ---------------------------------------------------------------------------
       
   312 
       
   313 bool PIProfilerEnginePrivate::SavePluginSettings(const PluginAttributes &samplerAttributes)
       
   314 {
       
   315     for (int index = 0; index < iSamplerAttributes->Count(); index++) {
       
   316         if (samplerAttributes.mUid == this->iSamplerAttributes->At(index).iUid) {
       
   317             iSamplerAttributes->At(index).iEnabled = samplerAttributes.mEnabled;
       
   318             iSamplerAttributes->At(index).iSampleRate = samplerAttributes.mSampleRate;
       
   319 
       
   320             convertSettingItemToTSettingItem(this->iSamplerAttributes->At(index).iSettingItem1,
       
   321                 samplerAttributes.mSettingItem1);
       
   322             convertSettingItemToTSettingItem(this->iSamplerAttributes->At(index).iSettingItem2,
       
   323                 samplerAttributes.mSettingItem2);
       
   324             convertSettingItemToTSettingItem(this->iSamplerAttributes->At(index).iSettingItem3,
       
   325                 samplerAttributes.mSettingItem3);
       
   326             convertSettingItemToTSettingItem(this->iSamplerAttributes->At(index).iSettingItem4,
       
   327                 samplerAttributes.mSettingItem4);
       
   328             convertSettingItemToTSettingItem(this->iSamplerAttributes->At(index).iSettingItem5,
       
   329                 samplerAttributes.mSettingItem5);
       
   330             convertSettingItemToTSettingItem(this->iSamplerAttributes->At(index).iSettingItem6,
       
   331                 samplerAttributes.mSettingItem6);
       
   332 
       
   333             TSamplerAttributes attr = iSamplerAttributes->At(index);
       
   334 
       
   335             if (RProfiler::SetSamplerAttributes(this->iSamplerAttributes->At(index)) == KErrNone) {
       
   336                 return true;
       
   337             }
       
   338             else {
       
   339                 return false;
       
   340             }
       
   341         }
       
   342     }
       
   343     return false;
       
   344 
       
   345 }
       
   346 
       
   347 // ---------------------------------------------------------------------------
       
   348 
       
   349 void PIProfilerEnginePrivate::StartAllSamplerItemsL(TProfilingMode aProfilingMode)
       
   350 {
       
   351     TBuf<256> activeWriterDes;
       
   352     TBuf8<256> writer8;
       
   353 
       
   354     //iState = MProfilerStatusObserver::EInitializing;
       
   355 
       
   356     RProfiler::TProfilingMode profilingMode = aProfilingMode == EProfilingModeTimed
       
   357         ? RProfiler::EProfilingModeTimed : RProfiler::EProfilingModeNormal;
       
   358 
       
   359     // try to start profiling process through client-server interface
       
   360     if (RProfiler::StartSampling(profilingMode) == KErrNotFound) {
       
   361         // profiler stopped (e.g. from eshell) and must be restarted 
       
   362         LaunchEngineL();
       
   363         // try to launch sampling again
       
   364         RProfiler::StartSampling(profilingMode);
       
   365     }
       
   366 }
       
   367 
       
   368 // ---------------------------------------------------------------------------
       
   369 // --------------------------------------------------------------------------------------------
       
   370 
       
   371 void PIProfilerEnginePrivate::StopProfiling()
       
   372 {
       
   373     // Stop profiling process through CS session
       
   374     RProfiler::StopSampling();
       
   375 
       
   376 }
       
   377 
       
   378 // --------------------------------------------------------------------------------------------
       
   379 
       
   380 void PIProfilerEnginePrivate::HandleProfilerStatusChange(KProfilerStatus aStatus)
       
   381 {
       
   382     if (aStatus == EIdle || aStatus == ERunning) {
       
   383 
       
   384         if (iGeneralAttributes.iTraceOutput == KTraceOutput) {
       
   385 
       
   386             TBuf<256> buf;
       
   387             // get profiler file name 
       
   388             TBool valu = RProfiler::GetFileName(buf);
       
   389 
       
   390             QString filename = QString((QChar*) buf.Ptr(), buf.Length());
       
   391 
       
   392             // Let ui know that status has changed
       
   393 
       
   394             if (aStatus == EIdle) {
       
   395                 QString text = QString("Wrote trace data to: \n");
       
   396                 text.append(filename);
       
   397                 emit iPublic->profilingStatusChanged(PI_FINISHED_SUCCEFULLY, text,
       
   398                     PI_PROFILINGMODENORMAL, PI_FILE_OUTPUT);
       
   399             }
       
   400             else if (aStatus == ERunning) {
       
   401                 QString text = QString("Writing trace data to: \n");
       
   402                 text.append(filename);
       
   403                 emit iPublic->profilingStatusChanged(PI_PROFILING, text, PI_PROFILINGMODENORMAL,
       
   404                     PI_FILE_OUTPUT);
       
   405             }
       
   406         }
       
   407         else {
       
   408             // Let ui know that status has changed
       
   409             if (aStatus == EIdle) {
       
   410                 emit iPublic->profilingStatusChanged(PI_FINISHED_SUCCEFULLY, QString(
       
   411                     "Wrote trace data to debug output"), PI_PROFILINGMODENORMAL, PI_DEBUG_OUTPUT);
       
   412             }
       
   413             else if (aStatus == ERunning) {
       
   414                 emit iPublic->profilingStatusChanged(PI_PROFILING, QString(
       
   415                     "Writing trace data to debug output"), PI_PROFILINGMODENORMAL, PI_DEBUG_OUTPUT);
       
   416             }
       
   417         }
       
   418     }
       
   419 
       
   420 }
       
   421 
       
   422 // ---------------------------------------------------------------------------
       
   423 
       
   424 void PIProfilerEnginePrivate::HandleProfilerErrorL(TInt aError)
       
   425 {
       
   426     {
       
   427         QString errorMsg = QString("Error: ");
       
   428         QString KNoMemory = QString("Cannot write to file, check settings");
       
   429 
       
   430         // message from pwr sampler
       
   431         if (aError < -1000) {
       
   432             errorMsg.append(QString("Stop other power measurement tools!"));
       
   433         }
       
   434         else if (aError == KErrAlreadyExists || aError == 11) {
       
   435             errorMsg.append(QString("Close old Profiler before start!"));
       
   436         }
       
   437         else if (aError == KErrNotReady) {
       
   438             errorMsg.append(QString("Memory card removed, failed to write!"));
       
   439         }
       
   440         else if (aError == KErrPathNotFound) {
       
   441             errorMsg.append(QString("Given trace data location does not exist"));
       
   442         }
       
   443         else {
       
   444             if (aError == KErrNoMemory || aError == KErrOverflow || aError == KErrDirFull || aError
       
   445                 == KErrDiskFull || aError == KErrNotReady) {
       
   446                 errorMsg.append(KNoMemory);
       
   447             }
       
   448             else {
       
   449                 errorMsg.append(QString("code: "));
       
   450                 errorMsg.append(aError);
       
   451             }
       
   452         }
       
   453 
       
   454         emit iPublic->profilingStatusChanged(PI_ERROR, errorMsg);
       
   455     }
       
   456 
       
   457 }
       
   458 // ---------------------------------------------------------------------------
       
   459 
       
   460 bool PIProfilerEnginePrivate::StartProfiling()
       
   461 {
       
   462     TRAPD(error, this->StartAllSamplerItemsL(PIProfilerEnginePrivate::EProfilingModeNormal));
       
   463     if (error == KErrNone) {
       
   464         return true;
       
   465     }
       
   466     return false;
       
   467 
       
   468 }
       
   469 
       
   470 // ---------------------------------------------------------------------------
       
   471 
       
   472 bool PIProfilerEnginePrivate::StartTimedProfiling()
       
   473 {
       
   474     TRAPD(error, this->StartAllSamplerItemsL(PIProfilerEnginePrivate::EProfilingModeTimed));
       
   475     if (error == KErrNone) {
       
   476         return true;
       
   477     }
       
   478     return false;
       
   479 }
       
   480 
       
   481 // ---------------------------------------------------------------------------
       
   482 
       
   483 void PIProfilerEnginePrivate::convertTGeneralAttributesToGeneralAttributes(
       
   484     TGeneralAttributes &tSamplerAttributes, GeneralAttributes &samplerAttributes)
       
   485 {
       
   486     samplerAttributes.mTimedSamplingPeriod = tSamplerAttributes.iTimedSamplingPeriod;
       
   487     int test = tSamplerAttributes.iSaveFileDrive.Length();
       
   488     TBuf16<KPrefixMaxLength> test2;
       
   489     test2.Copy(tSamplerAttributes.iSaveFileDrive);
       
   490     samplerAttributes.mSaveFileDrive = QString((QChar*) test2.Ptr(), test2.Length());
       
   491     test2.Copy(tSamplerAttributes.iTraceFilePrefix);
       
   492     samplerAttributes.mTraceFilePrefix = QString((QChar*) test2.Ptr(), test2.Length());
       
   493     test2.Copy(tSamplerAttributes.iTraceOutput);
       
   494     samplerAttributes.mTraceOutput = QString((QChar*) test2.Ptr(), test2.Length());
       
   495 }
       
   496 // ---------------------------------------------------------------------------
       
   497 
       
   498 void PIProfilerEnginePrivate::convertGeneralAttributesToTGeneralAttributes(
       
   499     TGeneralAttributes &tSamplerAttributes, GeneralAttributes &samplerAttributes)
       
   500 {
       
   501     tSamplerAttributes.iTimedSamplingPeriod = samplerAttributes.mTimedSamplingPeriod;
       
   502 
       
   503     tSamplerAttributes.iTraceOutput.Copy(TBuf<KPrefixMaxLength> (
       
   504         samplerAttributes.mTraceOutput.utf16()));
       
   505     tSamplerAttributes.iSaveFileDrive.Copy(TBuf<KPrefixMaxLength> (
       
   506         samplerAttributes.mSaveFileDrive.utf16()));
       
   507     tSamplerAttributes.iTraceFilePrefix.Copy(TBuf<KPrefixMaxLength> (
       
   508         samplerAttributes.mTraceFilePrefix.utf16()));
       
   509 
       
   510 }
       
   511 // ---------------------------------------------------------------------------
       
   512 
       
   513 TInt PIProfilerEnginePrivate::LoadGeneralSettingsL()
       
   514 {
       
   515     // local variable for getting saved settings from profiler engine
       
   516     TGeneralAttributes generalAttr;
       
   517     TInt err(KErrNone);
       
   518 
       
   519     // before loading saved settings (from settings file) set the default values 
       
   520     iGeneralAttributes.iTraceOutput.Copy(KTraceOutput);
       
   521     iGeneralAttributes.iTraceFilePrefix.Copy(KProfilerDefaultPrefix);
       
   522     iGeneralAttributes.iSaveFileDrive.Copy(KProfilerDefaultDrive);
       
   523     iGeneralAttributes.iTimedSamplingPeriod = KProfilerDefaultTimedSamplingPeriod;
       
   524 
       
   525     // request to 
       
   526     err = RProfiler::GetGeneralAttributes(generalAttr);
       
   527 
       
   528     // check that request succesfull
       
   529     if (err != KErrNone) {
       
   530         // could not connect profiler engine, use 
       
   531         return err;
       
   532     }
       
   533 
       
   534     // check if saved settings different than the default
       
   535     if (generalAttr.iTraceOutput.MatchF(iGeneralAttributes.iTraceOutput) == KErrNotFound) {
       
   536         iGeneralAttributes.iTraceOutput.Copy(generalAttr.iTraceOutput);
       
   537     }
       
   538 
       
   539     if (generalAttr.iTraceFilePrefix.MatchF(iGeneralAttributes.iTraceFilePrefix) == KErrNotFound) {
       
   540         iGeneralAttributes.iTraceFilePrefix.Copy(generalAttr.iTraceFilePrefix);
       
   541     }
       
   542 
       
   543     if (generalAttr.iSaveFileDrive.MatchF(iGeneralAttributes.iSaveFileDrive) == KErrNotFound) {
       
   544         iGeneralAttributes.iSaveFileDrive.Copy(generalAttr.iSaveFileDrive);
       
   545     }
       
   546 
       
   547     if (generalAttr.iTimedSamplingPeriod > 0) {
       
   548         iGeneralAttributes.iTimedSamplingPeriod = generalAttr.iTimedSamplingPeriod;
       
   549     }
       
   550 
       
   551     return err;
       
   552 }
       
   553 
       
   554 // ---------------------------------------------------------------------------
       
   555 
       
   556 void PIProfilerEnginePrivate::GetGeneralSettings(GeneralAttributes &settings)
       
   557 {
       
   558     convertTGeneralAttributesToGeneralAttributes(iGeneralAttributes, settings);
       
   559 }
       
   560 // ---------------------------------------------------------------------------
       
   561 
       
   562 bool PIProfilerEnginePrivate::SaveGeneralSettings(GeneralAttributes &settings)
       
   563 {
       
   564     convertGeneralAttributesToTGeneralAttributes(iGeneralAttributes, settings);
       
   565     TRAPD(error, SaveGeneralSettingsL());
       
   566     if (error != KErrNone) {
       
   567         return false;
       
   568     }
       
   569     return true;
       
   570 }
       
   571 
       
   572 // ---------------------------------------------------------------------------
       
   573 
       
   574 // --------------------------------------------------------------------------------------------
       
   575 
       
   576 void PIProfilerEnginePrivate::SaveGeneralSettingsL()
       
   577 {
       
   578     TInt err(KErrNone);
       
   579 
       
   580     // save general attributes to Profiler Engine
       
   581     err = RProfiler::SetGeneralAttributes(iGeneralAttributes);
       
   582 
       
   583     // check if save failed
       
   584     if (err == KErrNotFound) {
       
   585         // profiler stopped (e.g. from eshell) and must be restarted 
       
   586         LaunchEngineL();
       
   587 
       
   588         err = RProfiler::SetGeneralAttributes(iGeneralAttributes);
       
   589         if (err != KErrNone) {
       
   590             // leave no use to continue
       
   591             User::Leave(err);
       
   592         }
       
   593     }
       
   594 }
       
   595 // --------------------------------------------------------------------------------------------
       
   596 
       
   597 int PIProfilerEnginePrivate::GetTimeLimit()
       
   598 {
       
   599     return iGeneralAttributes.iTimedSamplingPeriod;
       
   600 }
       
   601 
       
   602 // --------------------------------------------------------------------------------------------
       
   603 
       
   604 void PIProfilerEnginePrivate::convertTSettingItemToSettingItem(TSettingItem &tSettingItem,
       
   605     SettingItem &settingItem)
       
   606 {
       
   607 
       
   608     settingItem.mType = tSettingItem.iType;
       
   609     settingItem.mSettingDescription = QString((QChar*) tSettingItem.iSettingDescription.Ptr(),
       
   610         tSettingItem.iSettingDescription.Length());
       
   611     settingItem.mSettingText = QString((QChar*) tSettingItem.iSettingText.Ptr(),
       
   612         tSettingItem.iSettingText.Length());
       
   613     settingItem.mUIText = QString((QChar*) tSettingItem.iUIText.Ptr(),
       
   614         tSettingItem.iUIText.Length());
       
   615     settingItem.mValue = QString((QChar*) tSettingItem.iValue.Ptr(), tSettingItem.iValue.Length());
       
   616 
       
   617 }
       
   618 // --------------------------------------------------------------------------------------------
       
   619 
       
   620 void PIProfilerEnginePrivate::convertSettingItemToTSettingItem(TSettingItem &tSettingItem,
       
   621     const SettingItem &settingItem)
       
   622 {
       
   623     tSettingItem.iType = settingItem.mType;
       
   624     tSettingItem.iSettingDescription.Copy(TBuf<256> (settingItem.mSettingDescription.utf16()));
       
   625     tSettingItem.iSettingText.Copy(TBuf<64> (settingItem.mSettingText.utf16()));
       
   626     tSettingItem.iUIText.Copy(TBuf<64> (settingItem.mUIText.utf16()));
       
   627     tSettingItem.iValue.Copy(TBuf<128> (settingItem.mValue.utf16()));
       
   628 
       
   629 }
       
   630 
       
   631 // --------------------------------------------------------------------------------------------
       
   632 
       
   633 void PIProfilerEnginePrivate::LeaveProfilingOnAfterClosing()
       
   634 {
       
   635     iLeaveProfilingOnAfterClosing = ETrue;
       
   636 }
       
   637 
       
   638 // --------------------------------------------------------------------------------------------
       
   639 
       
   640 bool PIProfilerEnginePrivate::CheckTraceLocationSanity(QString& location)
       
   641 {
       
   642     TBool value = EFalse;
       
   643 
       
   644     TBuf8<KPrefixMaxLength> fileLocation;
       
   645     fileLocation.Copy(TBuf<KPrefixMaxLength> (location.utf16()));
       
   646     TRAPD(error, value = CheckTraceLocationSanityL(fileLocation));
       
   647     if (error != KErrNone) {
       
   648         return false;
       
   649     }
       
   650     return value;
       
   651 }
       
   652 
       
   653 // --------------------------------------------------------------------------------------------
       
   654 
       
   655 TBool PIProfilerEnginePrivate::CheckTraceLocationSanityL(TBuf8<KPrefixMaxLength> &aAttr)
       
   656 {
       
   657     RFs fs;
       
   658     User::LeaveIfError(fs.Connect());
       
   659 
       
   660     TBuf<32> drive;
       
   661 
       
   662     CnvUtfConverter::ConvertToUnicodeFromUtf8(drive, aAttr);
       
   663 
       
   664     TDriveUnit driveUnit = TDriveUnit(drive);
       
   665 
       
   666     TBool ret(EFalse);
       
   667 
       
   668     // check that the root folder is correct
       
   669     if (drive.Length() > 2 && BaflUtils::CheckFolder(fs, drive.Left(3)) == KErrNone) {
       
   670         // check then if drive has still some space
       
   671         if (!SysUtil::DiskSpaceBelowCriticalLevelL(&fs, 0, driveUnit)) {
       
   672             ret = ETrue;
       
   673         }
       
   674     }
       
   675 
       
   676     fs.Close();
       
   677     return ret;
       
   678 }