htiui/HtiServicePlugins/HtiPIMServicePlugin/src/PIMHandler.cpp
branchRCL_3
changeset 20 48060abbbeaf
parent 19 d40e813b23c0
child 21 b3cee849fa46
equal deleted inserted replaced
19:d40e813b23c0 20:48060abbbeaf
     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:  Functional implementation of PIM service (for vCalendar, vCard)
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "../../../symbian_version.hrh"
       
    21 
       
    22 #include "HtiPIMServicePlugin.h"
       
    23 #include "PIMHandler.h"
       
    24 
       
    25 #include <HtiDispatcherInterface.h>
       
    26 #include <HtiLogging.h>
       
    27 
       
    28 #include <utf.h>
       
    29 #include <calcommon.h>
       
    30 #include <calsession.h>
       
    31 #include <calentryview.h>
       
    32 
       
    33 #include <cntdb.h> 
       
    34 #include <cntitem.h> 
       
    35 #include <cntfldst.h> 
       
    36 #include <cntvcard.h> 
       
    37 #include <cntfilt.h> 
       
    38 #include <caldataexchange.h> 
       
    39 #include <caldataformat.h> 
       
    40 
       
    41 // CONSTANTS
       
    42 _LIT8( KErrorUnrecognizedCommand, "Unrecognized command" );
       
    43 _LIT8( KErrorCalendarFileFormat, "Invalid format of calendar file name");
       
    44 _LIT8( KErrorVCardImportFailed, "vCard import failed" );
       
    45 _LIT8( KErrorVCalendarImportFailed, "vCalendar import failed" );
       
    46 _LIT8( KErrorMissingVCalendar, "Missing vCalendar object" );
       
    47 _LIT8( KErrorMissingVCard, "Missing vCard object" );
       
    48 _LIT8( KErrorInvalidId, "Invalid ID parameter" );
       
    49 _LIT8( KErrorItemNotFound, "Item not found" );
       
    50 _LIT8( KErrorFailedDelete, "Failed to delete item" );
       
    51 _LIT8( KErrorFailedDeleteAll, "Failed to delete all items" );
       
    52 _LIT8( KErrorFailedOpenCalendar, "Failed to open calendar file");
       
    53 //_LIT8( KErrorFailedOpenContact, "Failed to open contact database");
       
    54 
       
    55 
       
    56 
       
    57 _LIT( KDefaultAgendaFile, "" ); // A default file is opened if fileName is KNullDesC
       
    58 
       
    59 #if ( SYMBIAN_VERSION_SUPPORT < SYMBIAN_4 )  
       
    60 _LIT8( KErrorMissingText, "Text parameter missing" );
       
    61 _LIT8( KErrorMissingFilepath, "Filepath parameter missing" );
       
    62 _LIT8( KErrorNotepadAddMemoFailed, "Notepad add memo failed" );
       
    63 _LIT8( KErrorNotepadAddMemoFromFileFailed, "Notepad add memo from file failed" );
       
    64 _LIT8( KErrorNotepadDeleteAllFailed, "Notepad delete all failed" );
       
    65 
       
    66 _LIT( KHtiNpdHlpExe,       "HtiNpdHlp.exe" );
       
    67 _LIT( KCmdAddMemo,         "AddMemo" );
       
    68 _LIT( KCmdAddMemoFromFile, "AddMemoFromFile" );
       
    69 _LIT( KCmdDeleteAll,       "DeleteAll" );
       
    70 _LIT( KCmdDelim,           " " );
       
    71 #else
       
    72 _LIT8( KErrorNotepadNotSupported, "Notepad not supported" );
       
    73 #endif
       
    74 
       
    75 // ----------------------------------------------------------------------------
       
    76 CPIMHandler* CPIMHandler::NewL()
       
    77     {
       
    78     HTI_LOG_FUNC_IN( "CPIMHandler::NewL" );
       
    79     CPIMHandler* self = new (ELeave) CPIMHandler();
       
    80     CleanupStack::PushL ( self );
       
    81     self->ConstructL();
       
    82     CleanupStack::Pop();
       
    83     HTI_LOG_FUNC_OUT( "CPIMHandler::Done" );
       
    84     return self;
       
    85     }
       
    86 
       
    87 // ----------------------------------------------------------------------------
       
    88 CPIMHandler::CPIMHandler():iIsBusy( EFalse ), iEntryViewErr( KErrNone ),
       
    89         iCalSession(NULL)
       
    90     {
       
    91     }
       
    92 
       
    93 // ----------------------------------------------------------------------------
       
    94 CPIMHandler::~CPIMHandler()
       
    95     {
       
    96     HTI_LOG_TEXT( "CPIMHandler destroy" );
       
    97     HTI_LOG_TEXT( "Deleting iCalSession");
       
    98     delete iCalSession;
       
    99     HTI_LOG_TEXT( "Deleting iWaiter" );
       
   100     delete iWaiter;
       
   101     }
       
   102 
       
   103 // ----------------------------------------------------------------------------
       
   104 void CPIMHandler::ConstructL()
       
   105     {
       
   106     HTI_LOG_TEXT( "CPIMHandler::ConstructL" );
       
   107     iWaiter = new ( ELeave ) CActiveSchedulerWait;
       
   108     }
       
   109 
       
   110 // ----------------------------------------------------------------------------
       
   111 void CPIMHandler::SetDispatcher( MHtiDispatcher* aDispatcher )
       
   112     {
       
   113     iDispatcher = aDispatcher;
       
   114     }
       
   115 
       
   116 // ----------------------------------------------------------------------------
       
   117 void CPIMHandler::ProcessMessageL( const TDesC8& aMessage,
       
   118     THtiMessagePriority /*aPriority*/ )
       
   119     {
       
   120     HTI_LOG_FUNC_IN( "CPIMHandler::ProcessMessageL" );
       
   121 
       
   122     iIsBusy = ETrue;
       
   123     TInt err = KErrNone;
       
   124 
       
   125     // Zero legth of aMessage tested already in CHtiPIMServicePlugin.
       
   126     // Other sanity checks must be done here.
       
   127 
       
   128     TUint8 command = aMessage.Ptr()[0];
       
   129     switch ( command )
       
   130         {
       
   131         case CHtiPIMServicePlugin::EImportVCard:
       
   132             {
       
   133             TRAP( err, HandleVCardImportFuncL(
       
   134                     aMessage.Right( aMessage.Length() - 1 ) ) );
       
   135             break;
       
   136             }
       
   137         case CHtiPIMServicePlugin::EImportVCalendar:
       
   138             {
       
   139             TRAP( err, HandleVCalendarImportFuncL(
       
   140                     aMessage.Right( aMessage.Length() - 1 ) ) );
       
   141             break;
       
   142             }
       
   143         case CHtiPIMServicePlugin::EDeleteContact:
       
   144             {
       
   145             TRAP( err, HandleContactDeleteFuncL(
       
   146                     aMessage.Right( aMessage.Length() - 1 ) ) );
       
   147             break;
       
   148             }
       
   149         case CHtiPIMServicePlugin::EDeleteCalendar:
       
   150             {
       
   151             TRAP( err, HandleCalendarDeleteFuncL(
       
   152                     aMessage.Right( aMessage.Length() - 1 ) ) );
       
   153             break;
       
   154             }
       
   155         case CHtiPIMServicePlugin::EOpenCalendarFile:
       
   156             {
       
   157             TRAP( err, HandleOpenCalendarFileL(
       
   158                     aMessage.Right( aMessage.Length() - 1 )));
       
   159             break;
       
   160             }
       
   161         case CHtiPIMServicePlugin::EListCalendarFiles:
       
   162             {
       
   163             TRAP( err, HandleListCalendarFilesL());
       
   164             break;
       
   165             }
       
   166         case CHtiPIMServicePlugin::ENotepadAddMemo:
       
   167             {
       
   168             TRAP( err, HandleNotepadAddMemoFuncL( aMessage.Mid( 1 ) ) );
       
   169             break;
       
   170             }
       
   171         case CHtiPIMServicePlugin::ENotepadAddMemoFromFile:
       
   172             {
       
   173             TRAP( err, HandleNotepadAddMemoFromFileFuncL( aMessage.Mid( 1 ) ) );
       
   174             break;
       
   175             }
       
   176         case CHtiPIMServicePlugin::ENotepadDeleteAll:
       
   177             {
       
   178             TRAP( err, HandleNotepadDeleteAllFuncL() );
       
   179             break;
       
   180             }
       
   181         default:
       
   182             {
       
   183             TRAP( err, SendErrorMessageL(
       
   184                     KErrArgument, KErrorUnrecognizedCommand ) );
       
   185             break;
       
   186             }
       
   187         }
       
   188 
       
   189     if ( err != KErrNone )
       
   190         {
       
   191         iIsBusy = EFalse;
       
   192         User::Leave( err );
       
   193         }
       
   194 
       
   195     HTI_LOG_FUNC_OUT( "CPIMHandler::ProcessMessageL: Done" );
       
   196     }
       
   197 
       
   198 // ----------------------------------------------------------------------------
       
   199 TBool CPIMHandler::IsBusy()
       
   200     {
       
   201     return iIsBusy;
       
   202     }
       
   203 
       
   204 // ----------------------------------------------------------------------------
       
   205 void CPIMHandler::HandleOpenCalendarFileL( const TDesC8& aData )
       
   206     {
       
   207     HTI_LOG_FUNC_IN( "CPIMHandler::HandleOpenCalendarFileL" );
       
   208     
       
   209     // check the format, the correct format is DriveLetter:FileName
       
   210     if (aData.Length() != 0)
       
   211         {
       
   212         if(aData.Length() < 3 || aData[1] != ':'||
       
   213                 !(aData[0] >= 'a' && aData[0] <= 'z' || aData[0] >= 'A' && aData[0] <= 'Z'))
       
   214             {
       
   215             SendErrorMessageL( KErrArgument, KErrorCalendarFileFormat );
       
   216             return;
       
   217             }
       
   218         }
       
   219     
       
   220     delete iCalSession;
       
   221     iCalSession = NULL;
       
   222     
       
   223     // Open iCalSession
       
   224     HTI_LOG_TEXT("Open calendar session");
       
   225     iCalSession = CCalSession::NewL();
       
   226     TBuf<KMaxFileName + 2> calFile;
       
   227     calFile.Copy(aData);
       
   228     TRAPD(err, iCalSession->OpenL(calFile));
       
   229     if(err == KErrNone)
       
   230         {
       
   231         HTI_LOG_TEXT("Calendar session open");
       
   232         SendOkMsgL( KNullDesC8 );
       
   233         }
       
   234     else
       
   235         {
       
   236         HTI_LOG_TEXT("Failed to open calendar file");
       
   237         SendErrorMessageL( err, KErrorFailedOpenCalendar );
       
   238         delete iCalSession;
       
   239         iCalSession = NULL;
       
   240         }
       
   241     HTI_LOG_FUNC_OUT( "CPIMHandler::HandleOpenCalendarFileL: Done" );
       
   242     }
       
   243 
       
   244 // ----------------------------------------------------------------------------
       
   245 void CPIMHandler::HandleListCalendarFilesL()
       
   246     {
       
   247     HTI_LOG_FUNC_IN( "CPIMHandler::HandleListCalendarFiles" );
       
   248     
       
   249     CCalSession* calSession = CCalSession::NewL();
       
   250     CleanupStack::PushL(calSession);
       
   251     HTI_LOG_TEXT("List all calendar files");
       
   252     CDesCArray* calFiles = calSession->ListCalFilesL();
       
   253     
       
   254     if(calFiles == NULL) //No calendar files
       
   255         {
       
   256         TBuf8<2> reply;
       
   257         reply.AppendFill(0, 2);
       
   258         SendOkMsgL(reply);
       
   259         CleanupStack::PopAndDestroy(); // calSession
       
   260         return;
       
   261         }
       
   262     
       
   263     CleanupStack::PushL(calFiles);
       
   264     TInt count = calFiles->Count();
       
   265     
       
   266     // files count + file count * (file name length + max file name + driver letter + ':')
       
   267     TInt bufSize = 2 + count * (2 + KMaxFileName + 1 + 1);
       
   268     CBufFlat* calListBuf = CBufFlat::NewL( bufSize );
       
   269     CleanupStack::PushL(calListBuf);
       
   270     
       
   271     HBufC8* calArray = HBufC8::NewLC( bufSize );
       
   272     TPtr8 calArrayPtr = calArray->Des();
       
   273     
       
   274     calArrayPtr.Append((TUint8*)(&count), 2);
       
   275     TInt pos = 0;
       
   276     calListBuf->ExpandL(pos, 2);
       
   277     calListBuf->Write(pos, *calArray, 2);
       
   278     calArrayPtr.Zero();
       
   279     pos += 2;
       
   280     
       
   281     for(int i = 0; i < count; ++i)
       
   282         {
       
   283         TInt len = calFiles->MdcaPoint(i).Length();
       
   284         calArrayPtr.Append((TUint8*)(&len), 2);
       
   285         calArrayPtr.Append(calFiles->MdcaPoint(i));
       
   286         calListBuf->ExpandL(pos, calArray->Length());
       
   287         calListBuf->Write(pos,*calArray, calArray->Length());
       
   288         pos += calArray->Length();
       
   289         calArrayPtr.Zero();
       
   290         }
       
   291     SendOkMsgL( calListBuf->Ptr( 0 ) );
       
   292     CleanupStack::PopAndDestroy(4); //calArray, calListBuf, calFiles, calSession;    
       
   293     HTI_LOG_FUNC_OUT( "CPIMHandler::HandleListCalendarFiles: Done" );
       
   294     }
       
   295 
       
   296 // ----------------------------------------------------------------------------
       
   297 void CPIMHandler::HandleVCardImportFuncL( const TDesC8& aData )
       
   298     {
       
   299     HTI_LOG_FUNC_IN( "CPIMHandler::HandleVCardImportFuncL" );
       
   300     if ( aData.Length() == 0 )
       
   301         {
       
   302         SendErrorMessageL( KErrArgument, KErrorMissingVCard );
       
   303         return;
       
   304         }
       
   305     
       
   306     CContactDatabase* contactDatabase = CContactDatabase::OpenL();
       
   307     HTI_LOG_TEXT( "CPIMHandler: Contact database open" );
       
   308     CleanupStack::PushL(contactDatabase);
       
   309     
       
   310     CBufFlat* buffer = CBufFlat::NewL(aData.Length());
       
   311     CleanupStack::PushL(buffer);
       
   312     buffer->ExpandL(0, aData.Length());
       
   313     buffer->Ptr(0).Copy(aData.Right(aData.Length()));
       
   314     RBufReadStream readStream;
       
   315     readStream.Open(*buffer, 0);
       
   316     CleanupClosePushL<RBufReadStream>( readStream );
       
   317     
       
   318     // Imports vCard
       
   319     TBool success = EFalse;
       
   320     TUid format = TUid::Uid(KUidVCardConvDefaultImpl);
       
   321     CArrayPtr<CContactItem>* contacts = NULL;
       
   322     TRAPD(err,contacts = contactDatabase->ImportContactsL(format, readStream, success, 
       
   323             CContactDatabase::EImportSingleContact | CContactDatabase::ETTFormat));
       
   324     CleanupStack::PushL(contacts);
       
   325     if(err != KErrNone || success == EFalse || contacts == NULL || contacts->Count() == 0)
       
   326         {
       
   327         HTI_LOG_TEXT("Failed to import vCard");
       
   328         SendErrorMessageL( err, KErrorVCardImportFailed );
       
   329         }
       
   330     else
       
   331         {
       
   332         // Returns the imported contact id 
       
   333         TInt32 entryId = contacts->At(0)->Id();
       
   334         TBuf8<4> idBuf;
       
   335         idBuf.Append( ( TUint8* ) &entryId, 4 );
       
   336         HTI_LOG_TEXT("vCard imported");
       
   337         SendOkMsgL( idBuf );
       
   338         }
       
   339     
       
   340     contacts->ResetAndDestroy();
       
   341     
       
   342     CleanupStack::PopAndDestroy(4); // contacts, readStream, buffer, contactDatabase
       
   343     HTI_LOG_FUNC_OUT( "CPIMHandler::HandleVCardImportFuncL: Done" );
       
   344     }
       
   345 
       
   346 // ----------------------------------------------------------------------------
       
   347 void CPIMHandler::HandleVCalendarImportFuncL( const TDesC8& aData )
       
   348     {
       
   349     HTI_LOG_FUNC_IN( "CPIMHandler::HandleVCalendarImportFuncL" );
       
   350 
       
   351     if ( aData.Length() == 0 )
       
   352         {
       
   353         HTI_LOG_TEXT( "CPIMHandler::HandleVCalendarImportFuncL: Error: length of data is zero" )
       
   354         SendErrorMessageL( KErrArgument, KErrorMissingVCalendar );
       
   355         return;
       
   356         }
       
   357 
       
   358     CBufFlat* buffer = CBufFlat::NewL( aData.Length() );
       
   359     CleanupStack::PushL(buffer);
       
   360     buffer->ExpandL( 0, aData.Length() );
       
   361     buffer->Ptr( 0 ).Copy( aData );
       
   362     RBufReadStream readStream;
       
   363     readStream.Open( *buffer, 0 );
       
   364     CleanupClosePushL( readStream );
       
   365 
       
   366     if(iCalSession == NULL)
       
   367         {
       
   368         HTI_LOG_TEXT( "CPIMHandler: Open default calendar file" );
       
   369         iCalSession = CCalSession::NewL();
       
   370         iCalSession->OpenL( KDefaultAgendaFile );
       
   371         HTI_LOG_TEXT( "CPIMHandler: Calendar session open" );
       
   372         }
       
   373 
       
   374     CCalDataExchange* importer = CCalDataExchange::NewL(*iCalSession);
       
   375     CleanupStack::PushL( importer );
       
   376     HTI_LOG_TEXT( "CPIMHandler: Calendar importer created" );
       
   377 
       
   378     RPointerArray<CCalEntry> entryArray;
       
   379     CleanupClosePushL( entryArray );
       
   380 
       
   381     TInt err = KErrNone;
       
   382     TInt size = 0;
       
   383     // Import as VCalendar
       
   384     TRAP( err, importer->ImportL( KUidVCalendar, readStream, entryArray ) );
       
   385     HTI_LOG_FORMAT( "ImportL return value %d", err );
       
   386     size = entryArray.Count();
       
   387     HTI_LOG_FORMAT( "Import VCalendarL imported %d entries", size );
       
   388     TCalLocalUid uniqueId = 0;
       
   389     TInt success = 0;
       
   390     if ( size > 0 )
       
   391         {
       
   392         CCalEntryView* entryView = CCalEntryView::NewL( *iCalSession, *this );
       
   393         iWaiter->Start();
       
   394         CleanupStack::PushL(entryView);
       
   395         if ( iEntryViewErr == KErrNone )
       
   396             {
       
   397             TRAP( err, entryView->StoreL( entryArray, success ) );
       
   398             HTI_LOG_FORMAT( "StoreL return value %d", err );
       
   399             HTI_LOG_FORMAT( "Successfully stored %d entries", success );
       
   400             uniqueId = entryArray[0]->LocalUidL();
       
   401             }
       
   402         CleanupStack::PopAndDestroy();
       
   403         }
       
   404     entryArray.ResetAndDestroy();
       
   405     CleanupStack::PopAndDestroy(); // entryArray
       
   406 
       
   407     if ( err == KErrNone && success > 0 )
       
   408         {
       
   409         HTI_LOG_TEXT("vCalendar imported");
       
   410         TBuf8<8> uniqueIdStr;
       
   411         uniqueIdStr.Copy( ( TUint8* ) ( &uniqueId ), sizeof( uniqueId ) );
       
   412         SendOkMsgL( uniqueIdStr );
       
   413         }
       
   414     else
       
   415         {
       
   416         HTI_LOG_TEXT("Failed to import vCalendar");
       
   417         if ( err == KErrNone ) 
       
   418             {
       
   419             err = KErrGeneral;
       
   420             }
       
   421         SendErrorMessageL( err, KErrorVCalendarImportFailed );
       
   422         }
       
   423 
       
   424     CleanupStack::PopAndDestroy( 3 ); // buffer, readStream, importer
       
   425     HTI_LOG_FUNC_OUT( "CPIMHandler::HandleVCalendarImportFuncL: Done" );
       
   426     }
       
   427 
       
   428 // ----------------------------------------------------------------------------
       
   429 void CPIMHandler::HandleContactDeleteFuncL( const TDesC8& aData )
       
   430     {
       
   431     HTI_LOG_FUNC_IN( "CPIMHandler::HandleContactDeleteFuncL" );
       
   432     TInt dataLength = aData.Length();
       
   433     if ( dataLength != 0 && dataLength != 4 )
       
   434         {
       
   435         HTI_LOG_TEXT( "CPIMHandler: Error: wrong length of data" )
       
   436         SendErrorMessageL( KErrArgument, KErrorInvalidId );
       
   437         return;
       
   438         }
       
   439     CContactDatabase* contactDatabase = CContactDatabase::OpenL();
       
   440     HTI_LOG_TEXT( "CPIMHandler: Contact database open" );
       
   441     CleanupStack::PushL(contactDatabase);
       
   442     
       
   443     if(dataLength == 0)   // delete all contacts
       
   444         {
       
   445         //const CContactIdArray* array = iContactDatabase->SortedItemsL();
       
   446         CCntFilter *filter = CCntFilter::NewLC();
       
   447         filter->SetContactFilterTypeCard(ETrue);
       
   448         filter->SetContactFilterTypeGroup(EFalse);
       
   449         contactDatabase->FilterDatabaseL(*filter);
       
   450         TRAPD(err, contactDatabase->DeleteContactsL(*filter->iIds));
       
   451         CleanupStack::PopAndDestroy();
       
   452         if(err == KErrNone)
       
   453             {
       
   454             HTI_LOG_TEXT("All contacts deleted");
       
   455             SendOkMsgL( KNullDesC8 );
       
   456             }
       
   457         else
       
   458             {
       
   459             HTI_LOG_TEXT("Failed to delete all contacts");
       
   460             SendErrorMessageL( err, KErrorFailedDeleteAll );
       
   461             }
       
   462         }
       
   463     else  // delete one contact by id
       
   464         {
       
   465         TUint id = aData[0] + ( aData[1] << 8 )
       
   466                 + ( aData[2] << 16 )
       
   467                 + ( aData[3] << 24 );
       
   468         TRAPD(err, contactDatabase->DeleteContactL(id));
       
   469         
       
   470         if(err == KErrNone)
       
   471             {
       
   472             HTI_LOG_TEXT("Contact deleted");
       
   473             SendOkMsgL( KNullDesC8 );
       
   474             }
       
   475         else
       
   476             {
       
   477             HTI_LOG_TEXT("Failed to delete contact");
       
   478             SendErrorMessageL( err, KErrorFailedDelete );
       
   479             }
       
   480         }
       
   481     
       
   482     CleanupStack::PopAndDestroy();
       
   483     HTI_LOG_FUNC_OUT( "CPIMHandler::HandleContactDeleteFuncL" );
       
   484     }
       
   485 
       
   486 // ----------------------------------------------------------------------------
       
   487 void CPIMHandler::HandleCalendarDeleteFuncL( const TDesC8& aData )
       
   488     {
       
   489     HTI_LOG_FUNC_IN( "CPIMHandler::HandleVCalendarDeleteFuncL" );
       
   490 
       
   491     TInt dataLength = aData.Length();
       
   492     if ( dataLength != 0 && dataLength != 4 )
       
   493         {
       
   494         HTI_LOG_TEXT( "CPIMHandler: Error: wrong length of data" )
       
   495         SendErrorMessageL( KErrArgument, KErrorInvalidId );
       
   496         return;
       
   497         }
       
   498 
       
   499 
       
   500     if(iCalSession == NULL)
       
   501         {
       
   502         HTI_LOG_TEXT( "CPIMHandler: Open default calendar file" );
       
   503         iCalSession = CCalSession::NewL();
       
   504         iCalSession->OpenL( KDefaultAgendaFile );
       
   505         HTI_LOG_TEXT( "CPIMHandler: Calendar session open" );
       
   506         }
       
   507 
       
   508     HTI_LOG_TEXT( "CPIMHandler: Creating entry view" );
       
   509     CCalEntryView* entryView = CCalEntryView::NewL( *iCalSession, *this );
       
   510     iWaiter->Start();
       
   511     CleanupStack::PushL(entryView);
       
   512     if ( iEntryViewErr != KErrNone )
       
   513         {
       
   514         User::Leave( iEntryViewErr );
       
   515         }
       
   516 
       
   517     // If dataLength is 0, no ID given, delete all calendar entries
       
   518     if ( dataLength == 0 )
       
   519         {
       
   520         HTI_LOG_TEXT( "CPIMHandler: Deleting all calendar entries" );
       
   521         TCalTime minTime;
       
   522         TCalTime maxTime;
       
   523         minTime.SetTimeUtcL( TCalTime::MinTime() );
       
   524         maxTime.SetTimeUtcL( TCalTime::MaxTime() );
       
   525         CalCommon::TCalTimeRange timeRange( minTime, maxTime );
       
   526         TRAPD( err, entryView->DeleteL( timeRange,
       
   527                 CalCommon::EIncludeAll, *this ) );
       
   528         iWaiter->Start();
       
   529         if ( err == KErrNone && iEntryViewErr == KErrNone )
       
   530             {
       
   531             HTI_LOG_TEXT("All calendar entries deleted");
       
   532             SendOkMsgL( KNullDesC8 );
       
   533             }
       
   534         else
       
   535             {
       
   536             HTI_LOG_TEXT("Failed to delete all calendar entries");
       
   537             SendErrorMessageL( KErrGeneral, KErrorFailedDeleteAll );
       
   538             }
       
   539         }
       
   540 
       
   541     // If id given, delete only calendar entry having that id
       
   542     else
       
   543         {
       
   544         TCalLocalUid id = aData[0] + ( aData[1] << 8 )
       
   545                              + ( aData[2] << 16 )
       
   546                              + ( aData[3] << 24 );
       
   547         HTI_LOG_FORMAT( "CPIMHandler: Deleting one calendar entry %d", id );
       
   548         CCalEntry* entryToDelete = NULL;
       
   549         TRAPD( err, entryToDelete = entryView->FetchL( id ) );
       
   550 
       
   551         if ( err || entryToDelete == NULL )
       
   552             {
       
   553             HTI_LOG_TEXT( "CPIMHandler: Calendar entry not found" );
       
   554             SendErrorMessageL( KErrNotFound, KErrorItemNotFound );
       
   555             }
       
   556         else
       
   557             {
       
   558             CleanupStack::PushL( entryToDelete );
       
   559             TRAP( err, entryView->DeleteL( *entryToDelete ) );
       
   560             if ( err == KErrNone )
       
   561                 {
       
   562                 HTI_LOG_TEXT("calendar entrie deleted");
       
   563                 SendOkMsgL( KNullDesC8 );
       
   564                 }
       
   565             else
       
   566                 {
       
   567                 HTI_LOG_TEXT( "Failed to delete calendar entry" )
       
   568                 SendErrorMessageL( KErrGeneral, KErrorFailedDelete );
       
   569                 }
       
   570             CleanupStack::PopAndDestroy( entryToDelete );
       
   571             }
       
   572         }
       
   573     CleanupStack::PopAndDestroy(); //entryView;
       
   574     HTI_LOG_FUNC_OUT( "CPIMHandler::HandleVCalendarDeleteFuncL" );
       
   575     }
       
   576 
       
   577 #if ( SYMBIAN_VERSION_SUPPORT < SYMBIAN_4 )  
       
   578 TInt CallNpdHlp( const TDesC& aCmd )
       
   579     {
       
   580     HTI_LOG_FUNC_IN( "CallNpdHlp" );
       
   581 
       
   582     RProcess HtiNpdHlp;
       
   583     TInt err = HtiNpdHlp.Create( KHtiNpdHlpExe, aCmd );
       
   584     if ( err )
       
   585         {
       
   586         HTI_LOG_FORMAT( "Could not create HtiNpdHlp.Exe process %d", err );
       
   587         return err;
       
   588         }
       
   589 
       
   590     TRequestStatus status;
       
   591     HtiNpdHlp.Logon( status );
       
   592     HtiNpdHlp.Resume();
       
   593     User::WaitForRequest( status );
       
   594     if ( status.Int() != KErrNone )
       
   595         {
       
   596         HTI_LOG_FORMAT( "status     %d", status.Int() );
       
   597         HTI_LOG_FORMAT( "ExitReason %d", HtiNpdHlp.ExitReason() );
       
   598         HTI_LOG_FORMAT( "ExitType   %d", HtiNpdHlp.ExitType() );
       
   599         HtiNpdHlp.Close();
       
   600         return status.Int();
       
   601         }
       
   602 
       
   603     HtiNpdHlp.Close();
       
   604 
       
   605     HTI_LOG_FUNC_OUT( "CallNpdHlp" );
       
   606     return KErrNone;
       
   607     }
       
   608 #endif
       
   609 
       
   610 // ----------------------------------------------------------------------------
       
   611 void CPIMHandler::HandleNotepadAddMemoFuncL( const TDesC8& aData )
       
   612     {
       
   613     HTI_LOG_FUNC_IN( "CPIMHandler::HandleNotepadAddMemoFuncL" );
       
   614 #if ( SYMBIAN_VERSION_SUPPORT < SYMBIAN_4 ) 
       
   615     if ( aData.Length() < 1 )
       
   616         {
       
   617         SendErrorMessageL( KErrArgument, KErrorMissingText );
       
   618         return;
       
   619         }
       
   620 
       
   621     // convert text from TDesC8 -> TDesC
       
   622     // expecting the input TDesC8 contains UTF-8 data
       
   623     HBufC* text = CnvUtfConverter::ConvertToUnicodeFromUtf8L( aData );
       
   624     HTI_LOG_TEXT( "CPIMHandler: Conversion to Unicode done" );
       
   625     CleanupStack::PushL( text );
       
   626 
       
   627     HBufC* cmd = HBufC::NewLC( KCmdAddMemo().Length() + 1 + ( *text ).Length() );
       
   628     cmd->Des().Copy( KCmdAddMemo );
       
   629     cmd->Des().Append( KCmdDelim );
       
   630     cmd->Des().Append( *text );
       
   631 
       
   632     TInt err = CallNpdHlp( *cmd );
       
   633     if ( err )
       
   634         {
       
   635         SendErrorMessageL( err, KErrorNotepadAddMemoFailed );
       
   636         }
       
   637     else
       
   638         {
       
   639         SendNotepadOkMsgL( CHtiPIMServicePlugin::ENotepadAddMemo );
       
   640         }
       
   641 
       
   642     CleanupStack::PopAndDestroy( 2 ); // text, cmd
       
   643 #else
       
   644     SendErrorMessageL(KErrNotSupported, KErrorNotepadNotSupported);
       
   645 #endif   
       
   646     HTI_LOG_FUNC_OUT( "CPIMHandler::HandleNotepadAddMemoFuncL" );
       
   647     }
       
   648 
       
   649 // ----------------------------------------------------------------------------
       
   650 void CPIMHandler::HandleNotepadAddMemoFromFileFuncL( const TDesC8& aData )
       
   651     {
       
   652     HTI_LOG_FUNC_IN( "CPIMHandler::HandleNotepadAddMemoFromFileFuncL" );
       
   653 #if ( SYMBIAN_VERSION_SUPPORT < SYMBIAN_4 ) 
       
   654     if ( aData.Length() < 1 )
       
   655         {
       
   656         SendErrorMessageL( KErrArgument, KErrorMissingFilepath );
       
   657         return;
       
   658         }
       
   659 
       
   660     // convert filename from TDesC8 -> TDesC
       
   661     // expecting the input TDesC8 contains UTF-8 data
       
   662     HBufC* filename = CnvUtfConverter::ConvertToUnicodeFromUtf8L( aData );
       
   663     HTI_LOG_TEXT( "CPIMHandler: Conversion to Unicode done" );
       
   664     CleanupStack::PushL( filename );
       
   665 
       
   666     HBufC* cmd = HBufC::NewLC( KCmdAddMemoFromFile().Length() + 1 + ( *filename ).Length() );
       
   667     cmd->Des().Copy( KCmdAddMemoFromFile );
       
   668     cmd->Des().Append( KCmdDelim );
       
   669     cmd->Des().Append( *filename );
       
   670 
       
   671     TInt err = CallNpdHlp( *cmd );
       
   672     if ( err )
       
   673         {
       
   674         SendErrorMessageL( err, KErrorNotepadAddMemoFromFileFailed );
       
   675         }
       
   676     else
       
   677         {
       
   678         SendNotepadOkMsgL( CHtiPIMServicePlugin::ENotepadAddMemoFromFile );
       
   679         }
       
   680 
       
   681     CleanupStack::PopAndDestroy( 2 ); // filename, cmd
       
   682 #else
       
   683     SendErrorMessageL(KErrNotSupported, KErrorNotepadNotSupported);
       
   684 #endif 
       
   685     HTI_LOG_FUNC_OUT( "CPIMHandler::HandleNotepadAddMemoFromFileFuncL" );
       
   686     }
       
   687 
       
   688 // ----------------------------------------------------------------------------
       
   689 void CPIMHandler::HandleNotepadDeleteAllFuncL()
       
   690     {
       
   691     HTI_LOG_FUNC_IN( "CPIMHandler::HandleNotepadDeleteAllFuncL" );
       
   692 #if ( SYMBIAN_VERSION_SUPPORT < SYMBIAN_4 ) 
       
   693     TInt err = CallNpdHlp( KCmdDeleteAll() );
       
   694     if ( err )
       
   695         {
       
   696         SendErrorMessageL( err, KErrorNotepadDeleteAllFailed );
       
   697         }
       
   698     else
       
   699         {
       
   700         SendNotepadOkMsgL( CHtiPIMServicePlugin::ENotepadDeleteAll );
       
   701         }
       
   702 #else
       
   703     SendErrorMessageL(KErrNotSupported, KErrorNotepadNotSupported);
       
   704 #endif 
       
   705     HTI_LOG_FUNC_OUT( "CPIMHandler::HandleNotepadDeleteAllFuncL" );
       
   706     }
       
   707 
       
   708 // ----------------------------------------------------------------------------
       
   709 void CPIMHandler::SendNotepadOkMsgL( CHtiPIMServicePlugin::TCommand aCommand )
       
   710     {
       
   711     HTI_LOG_FUNC_IN( "CPIMHandler::SendNotepadOkMsgL" );
       
   712     TBuf8<1> msg;
       
   713     msg.Append( aCommand );
       
   714     User::LeaveIfError( iDispatcher->DispatchOutgoingMessage(
       
   715                         msg.AllocL(), KPIMServiceUid ) );
       
   716     iIsBusy = EFalse;
       
   717     HTI_LOG_FUNC_OUT( "CPIMHandler::SendNotepadOkMsgL" );
       
   718     }
       
   719 
       
   720 // ----------------------------------------------------------------------------
       
   721 void CPIMHandler::SendOkMsgL( const TDesC8& aData )
       
   722     {
       
   723     HTI_LOG_FUNC_IN( "CPIMHandler::SendOkMsgL: Starting" );
       
   724 
       
   725     User::LeaveIfNull( iDispatcher );
       
   726 
       
   727     HBufC8* temp = HBufC8::NewL( aData.Length() + 1 );
       
   728     TPtr8 response = temp->Des();
       
   729     response.Append( ( TChar ) CHtiPIMServicePlugin::EResultOk );
       
   730     response.Append( aData );
       
   731     User::LeaveIfError( iDispatcher->DispatchOutgoingMessage(
       
   732         temp, KPIMServiceUid ) );
       
   733     iIsBusy = EFalse;
       
   734     HTI_LOG_FUNC_OUT( "CPIMHandler::SendOkMsgL: Done" );
       
   735     }
       
   736 
       
   737 // ----------------------------------------------------------------------------
       
   738 void CPIMHandler::SendErrorMessageL( TInt aError, const TDesC8& aDescription )
       
   739     {
       
   740     HTI_LOG_FUNC_IN( "CPIMHandler::SendErrorMessageL: Starting" );
       
   741     User::LeaveIfNull( iDispatcher );
       
   742     User::LeaveIfError( iDispatcher->DispatchOutgoingErrorMessage(
       
   743         aError, aDescription, KPIMServiceUid ) );
       
   744     iIsBusy = EFalse;
       
   745     HTI_LOG_FUNC_OUT( "CPIMHandler::SendErrorMessageL: Done" );
       
   746     }
       
   747 
       
   748 // ----------------------------------------------------------------------------
       
   749 // CPIMHandler::Progress
       
   750 // Called during calendar entry view creation and operations.
       
   751 // Called only if NotifyProgress returns ETrue.
       
   752 // From MCalProgressCallBack
       
   753 // ----------------------------------------------------------------------------
       
   754 void CPIMHandler::Progress( TInt /*aProgress*/ )
       
   755     {
       
   756     }
       
   757 
       
   758 // ----------------------------------------------------------------------------
       
   759 // CPIMHandler::Completed
       
   760 // Called on completion of calendar entry view creation and operations
       
   761 // From MCalProgressCallBack
       
   762 // ----------------------------------------------------------------------------
       
   763 void CPIMHandler::Completed( TInt aError )
       
   764     {
       
   765     HTI_LOG_FUNC_IN( "CPIMHandler::Completed" );
       
   766     HTI_LOG_FORMAT( "Completed with error code %d", aError );
       
   767     iEntryViewErr = aError;
       
   768     iWaiter->AsyncStop();
       
   769     HTI_LOG_FUNC_OUT( "CPIMHandler::Completed" );
       
   770     }
       
   771 
       
   772 // ----------------------------------------------------------------------------
       
   773 // CPIMHandler::NotifyProgress
       
   774 // Returns whether or not progress notification is required
       
   775 // From MCalProgressCallBack
       
   776 // ----------------------------------------------------------------------------
       
   777 TBool CPIMHandler::NotifyProgress()
       
   778     {
       
   779     HTI_LOG_FUNC_IN( "CPIMHandler::NotifyProgress" );
       
   780     HTI_LOG_FUNC_OUT( "CPIMHandler::NotifyProgress" );
       
   781     return EFalse; // Don't notify about progress
       
   782     }